I escape &, <, >, " and '. Every time. So when a page turned blank because a JSON blob contained the text </script>, my first instinct was to blame the escape function. It was correct. Four things around it weren't.
1. </script> is not made of the five characters
The HTML tokenizer ends script data at the literal sequence </script, regardless of quotes, comments or JSON syntax. So this is valid JSON and still fatal HTML:
<script>
const payload = {"note": "</script>"};
</script>
I checked this in a headless browser: the script block was terminated at that </script, the payload object was never assigned, and the statement after it never ran. The page just sat there with its placeholder text.
Fix: escape the < when you serialize JSON into HTML.
const safe = JSON.stringify(data).replace(/</g, '\\u003C');
<\/script> works inside a JS literal too. Moving the JSON into <script type="application/json"> does not help β same tokenizer, same break.
2. encodeURIComponent does not escape the apostrophe
encodeURIComponent("O'Reilly") returns O'Reilly. It escapes & and " (as %26 and %22) but leaves the apostrophe alone, because ' is a legal URI character. That's correct for URLs and broken for any attribute you happened to wrap in single quotes: the value walks straight out of the attribute.
If you build markup by string concatenation, the quoting style has to match the escaper. Better: set attributes through setAttribute, which has no quoting to get wrong.
3. Decoding silently rewrites your text
Decoding through a detached <textarea> is the standard trick, and the browser normalizes as it parses. Feed it it's a&b😀 and you get back it's a&bπ β the numeric apostrophe became a literal one, the numeric emoji became a raw character, and stayed a U+00A0 that only looks like a space. Re-serialize that node and the entity forms have changed.
So an encode β decode β compare test is not a round-trip. Store the decoded value; don't re-derive it and don't diff it against the encoded original.
4. Missing semicolons still decode
In text, ©2024 renders Β©2024, and ¬in renders Β¬in because ¬ is a valid prefix that the parser matches first. Meanwhile my "is this already escaped?" check was /&[a-z]+;/, which misses ' β no semicolon β and that still becomes '.
Valid check: parse the string, don't regex it.
The rule
Escape once, for the exact context you are writing into, at the boundary where the data crosses. Text, attribute, script block and URL have four different rules, and &<>"' only covers the first two.
When I need to see what a string actually contains β which reference, which code point, whether the entity was even consumed β I keep this page open. It escapes the five characters, optionally turns every non-ASCII character into a numeric reference by code point (so one emoji becomes one 😀 instead of two surrogate halves), and decodes through the browser's own parser into a textarea while showing the before/after length β so a double-encode is visible immediately instead of three screens later. Nothing is uploaded; the decode is a detached node in the tab.
Top comments (0)