Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape keys/values when rendering JSON objects #1498

Merged
merged 13 commits into from
Jun 14, 2023

Conversation

yurishkuro
Copy link
Member

@yurishkuro yurishkuro commented Jun 13, 2023

The json-markup library does not properly escape the keys. Not clear if it is maintained or not, last changes were several years ago. Since it's just a single file, copy it directly and fix in place:

  • escape keys
  • parse URL and use safe href property in <a href="...">
  • remove handling of date type which will never be in the JSON that we obtain from span tags via JSON.parse()
  • remove skinning ability since we explicitly define css

Add tests:

  • use Jest snapshot to validate that the JSON is escaped correctly, which was not tested by the previous code
  • use more complex JSON with different unsafe values and exercising all code paths of the ported code

Signed-off-by: Yuri Shkuro <ysh@meta.com>
Signed-off-by: Yuri Shkuro <ysh@meta.com>
})

return forEach(keys, '{', '}', function (key) {
return '<span ' + style('json-markup-key') + '>"' + escape(key) + '":</span> ' + visit(obj[key])
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change here: s/key/escape(key)/

Signed-off-by: Yuri Shkuro <ysh@meta.com>
Signed-off-by: Yuri Shkuro <ysh@meta.com>
Signed-off-by: Yuri Shkuro <ysh@meta.com>
Signed-off-by: Yuri Shkuro <ysh@meta.com>
@codecov
Copy link

codecov bot commented Jun 13, 2023

Codecov Report

Patch coverage: 97.87% and project coverage change: +0.01 🎉

Comparison is base (723d353) 95.59% compared to head (675408f) 95.60%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1498      +/-   ##
==========================================
+ Coverage   95.59%   95.60%   +0.01%     
==========================================
  Files         244      245       +1     
  Lines        7665     7712      +47     
  Branches     2014     2028      +14     
==========================================
+ Hits         7327     7373      +46     
- Misses        338      339       +1     
Impacted Files Coverage Δ
.../TraceTimelineViewer/SpanDetail/KeyValuesTable.tsx 97.82% <ø> (ø)
...ePage/TraceTimelineViewer/SpanDetail/jsonMarkup.js 97.87% <97.87%> (ø)

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@yurishkuro yurishkuro changed the title Escape keys of JSON objects Escape keys of JSON objects and disable links rendering Jun 13, 2023
Signed-off-by: Yuri Shkuro <ysh@meta.com>
Signed-off-by: Yuri Shkuro <ysh@meta.com>
if (Array.isArray(doc)) return 'array';
if (typeof doc === 'string' && /^https?:/.test(doc)) {
try {
const u = new URL(doc);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added validation of URL string

'<span ' +
style('json-markup-string') +
'>"<a href="' +
(url.href) +
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use safe URL value only

});

return forEach(keys, '{', '}', function (key) {
return '<span ' + style('json-markup-key') + '>"' + escape(key) + '":</span> ' + visit(obj[key]);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replaced key with escape(key)

Signed-off-by: Yuri Shkuro <ysh@meta.com>
@yurishkuro yurishkuro changed the title Escape keys of JSON objects and disable links rendering Escape keys/values when rendering JSON objects Jun 14, 2023
Signed-off-by: Yuri Shkuro <ysh@meta.com>
Signed-off-by: Yuri Shkuro <ysh@meta.com>
Signed-off-by: Yuri Shkuro <ysh@meta.com>
Signed-off-by: Yuri Shkuro <ysh@meta.com>
@yurishkuro yurishkuro merged commit 7df099c into jaegertracing:main Jun 14, 2023
@yurishkuro yurishkuro deleted the escape-keys branch June 14, 2023 17:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant