fix(explorer): escape HTML before injecting raw JSON into the DOM - #496
Open
memosr wants to merge 1 commit into
Open
fix(explorer): escape HTML before injecting raw JSON into the DOM#496memosr wants to merge 1 commit into
memosr wants to merge 1 commit into
Conversation
The transaction, block and order "Raw" views serialize an object with JSON.stringify and pass each resulting line to dangerouslySetInnerHTML. JSON.stringify does not escape &, < or >, so string fields in the serialized object were rendered as markup instead of as text. Escape the serialized JSON before the syntax-highlight step, so the highlighter's own span tags are unaffected. Quotes are deliberately left unescaped because the highlight patterns match on them.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The transaction, block and order Raw views serialize an object with
JSON.stringify, split the result into lines, and hand each line todangerouslySetInnerHTML.JSON.stringifydoes not escape&,<or>, so anystring field in the serialized object reaches the DOM as markup rather than as text.
Several of these fields are chain-supplied rather than controlled by the person viewing
the page.
transaction.memoin particular is free-form and only length-limited(200 bytes,
lib/tx.go), with no content validation.Changes
src/lib/utils.ts- addedescapeHtml.src/components/transaction/TransactionDetailPage.tsxsrc/components/block/BlockDetailInfo.tsxsrc/components/token-swaps/OrderDetailPage.tsxEach raw view now escapes the serialized JSON before the syntax-highlight step.
Ordering matters: escaping first leaves the highlighter's own
<span>tags, which areinserted afterwards, intact. Quotes are deliberately not escaped, because the highlight
patterns match on
".Verification
Running
{ height: 12, memo: '<b>markup</b> & "quoted"', ok: true }through thetransaction view's highlight chain:
<b>reaches outputtsc --noEmitpasses.eslintreports no new findings; the twoprefer-consterrors itreports in
utils.tsare pre-existing and untouched by this change.Notes
CnpyColorIcon.tsxalso usesdangerouslySetInnerHTML, but its input is a locallygenerated SVG string rather than API data, so it is left alone.