The Five Characters That Matter
HTML only reserves a handful of characters. Escape these and almost every injection problem disappears:
| Character | Entity | Why it is escaped |
|---|---|---|
& | & | Starts an entity. Unescaped, © becomes ©. |
< | < | Opens a tag. |
> | > | Closes a tag — needed in text, optional inside a quoted attribute. |
" | " | Ends a double-quoted attribute value. |
' | ' | Ends a single-quoted attribute value. ' works in HTML5 but is not in the HTML4 named set. |
Encoding Is Not Sanitizing
Escaping makes text display literally; it does not make untrusted markup safe to insert as HTML. If you decode a string and assign it to innerHTML, the browser parses the tags you just produced. Decode into a text node, a <textarea>, or textContent — never into innerHTML on a page that handles user input.
Frequently Asked Questions
Why does decoding twice break my text?
Because the first pass produced text that still looks like an entity. &lt; decodes to <, which decodes again to < — so a user who literally typed < ends up with a tag character. Decode exactly once, at the boundary where the data enters your system, and store the decoded value.
Does behave like a space?
No — it is U+00A0, a no-break space that only looks like one. Verify it in your own runtime: in both Node and Python 3, "a\u00a0b".trim() / .strip() keep the character (length stays 3), "a\u00a0b".includes(" ") is false, and split(" ") returns one token instead of two. Word counts, search matching and form validation all disagree with what the page shows. Normalize U+00A0 to a real space before comparing.
How do I write an emoji as a numeric reference?
Use its code point, not its UTF-16 units: 😀 is 😀. In JavaScript the same character has .length === 2 because a string is UTF-16, so a loop that walks indexes will emit two surrogate references instead of one. Iterate code points ([...str] or for...of) and read codePointAt(0). This tool does exactly that for the non-ASCII option.
Do I need to escape & in URLs inside attributes?
Yes. In href="?a=1&b=2" the raw ampersand starts an entity, and a following word like ¬in or © can be parsed as one. Browsers are forgiving, validators are not, and email clients are worse. Escape every & in an attribute value as & — in JavaScript, that is what encodeURIComponent inside an escaped attribute already gives you.
What happens to invalid references like � or �?
The HTML parser maps them to U+FFFD, the replacement character — the same black diamond you see in broken text — rather than failing. If you decode and then compare strings, a NUL or a lone surrogate silently becomes three bytes of replacement and never matches. Reject or log U+FFFD when validating user-supplied data.
Is escaping the five characters enough inside a <script> block?
No. The HTML tokenizer ends script data at the literal sequence </script, no matter what quotes, comments or JSON syntax surround it, so a JSON string containing </script> closes the block and the rest of the script never runs. The five characters do not include /, which is why a correct escaper still breaks here. Write the < as a JSON escape when you serialize (JSON.stringify(v).replace(/</g, '\\u003C')), or use <\/script> inside a JS literal. Moving the payload into <script type="application/json"> does not help: same tokenizer, same break.
Is anything sent to a server?
No. Encoding is a regular-expression pass and decoding uses the browser's own parser in a detached <textarea>; both run in this tab. Disconnect and the page keeps working.