/ THE IDEA
HTML is the notation used to describe a webpage’s structure. Some characters have special jobs in it: an opening angle bracket can begin an element. Output encoding replaces characters with representations that display as text in the particular place where they are inserted. Context means that place’s interpretation rules. Text between elements, a quoted attribute, a web address and a script are different contexts. A method suitable for ordinary HTML text is not a general way to make arbitrary script content safe. For plain text, an interface that creates a text node avoids interpreting the supplied value as HTML at all. If rich formatting is genuinely required, sanitization uses an explicit policy to keep permitted markup and remove dangerous features. These are different jobs, not interchangeable spellings of the same operation.
THE FORMAL IDEA
untrusted text + intended output context → context-appropriate handling
| The arrow means choose the protection matching the browser’s next interpretation step. | | A text node is page content treated as characters rather than markup. Sanitization is policy-based removal of unsafe structure, not a guarantee from deleting one suspicious word. |
|
RUN THE TINY EXAMPLE
Change one thing. Predict what happens.
Review text is <b>excellent</b>. Put it into an ordinary element using textContent → show the angle brackets literally. Change only the insertion method to raw HTML parsing → produce bold formatting. In an HTML template text context, encoding < as < also keeps it from opening an element.
|
The same characters produced different results because the interpretation changed. The harmless formatting example exposes the boundary without needing malicious code. Dangerous instructions become possible when a broader set of browser features is accidentally allowed through the same route.
/ SO WHAT?
A useful review therefore follows data to its final destination. Validating that a review is under 500 characters does not establish that it is safe inside a script. Keep template protections enabled, prefer safe text interfaces and make any rich-content exceptions explicit and testable.
ONE CAVEAT |
| Encoding is context-specific; some contexts should not accept untrusted values at all. Browser security policies can reduce consequences, but do not replace correct handling at the insertion point. |
KEEP THIS
Text stays harmless when the browser receives it through an interface or encoding that preserves its intended text-only meaning.
|
NEXT: Why does a bound nucleus weigh less than its parts?
|