Output encoding
Output encoding converts a value into a form the receiving program reads as data, at the moment the value is written into a page, a URL, a query, a command or another structured text. On the web it is used mainly against cross-site scripting. A less-than sign in a user's comment goes into the page as the character reference <, and the browser displays < without starting a tag.1 Missing or wrong encoding can be the primary weakness behind every kind of injection.2
Encoding and escaping
A page, a SQL statement or a shell command is a structured message in which some characters carry structure. When a value containing those characters is inserted without conversion, the receiving component reads part of the value as control information, and the message no longer has the structure the developer wrote.2
Two techniques prevent this. Encoding translates a special character into an equivalent form that the target interpreter does not treat as syntax, as with < to < in HTML. Escaping puts a marker character in front of a special character: a backslash before a double quote inside a JavaScript string keeps the quote from ending the string.1 The two words are often used loosely. Some languages treat them as synonyms, and the stated purpose of the JavaScript function escape is encoding.2
Context
Browsers parse HTML, JavaScript, URLs and CSS by different rules, so each place in a page needs its own encoding, and an encoding chosen for the wrong place can leave the value dangerous or break the page.3 A value encoded for one interpreter and then read by another is a weakness of its own. The standard example is HTML entity encoding on a value placed in an event-handler attribute, where the value can still carry working JavaScript.4 The HTML parser decodes character references before the script engine reads the attribute, so adding more entities does not help.3
| Context | Example | Protection |
|---|---|---|
| HTML body | <div>VALUE</div> | HTML entity encoding: &, <, " and ' become &, <, " and ' |
| Quoted attribute | <input value="VALUE"> | HTML attribute encoding; the value always inside quotes |
| URL parameter | <a href="/s?q=VALUE"> | Percent encoding of the parameter, then attribute encoding of the whole URL |
| JavaScript string | <script>const m = "VALUE";</script> | An encoder documented for a JavaScript string inside an HTML script block |
| CSS | <span style="color: VALUE"> | No interpolation; style.setProperty() with a fixed property name and an allowlisted value |
Some places stay unsafe even with encoding: a value written directly into a script block, an HTML comment or CSS, a value used as a tag or attribute name, and anything passed to eval(), setTimeout() or setInterval().3 Outside HTML, shell escaping applies to values in operating system commands (see Command injection), and XML attributes and LDAP names have escaping rules of their own.1
When to encode
Encoding belongs at the point of output, immediately before the value reaches the interpreter. A value HTML-escaped before it is stored in the database, and escaped again by the template that displays it, appears on screen with visible & sequences, and the encoded form can interfere with other uses of the data in the program.1 Data is kept as the user typed it and converted once for each place it is written.
Input validation cannot stand in for it. The surname O'Reilly passes any sensible validation, and its apostrophe still breaks a quoted string. Stripping the apostrophe would record the wrong name, so free text has to be encoded or escaped at output.2
Framework defaults
Many frameworks encode every value they render unless told otherwise. That default is the safer design, since it does not depend on anyone deciding correctly which data is untrusted, and a page that protects 99 insertion points and misses one is still vulnerable.3 React DOM escapes any value embedded in JSX before rendering it.5 Vue escapes text interpolation and attribute bindings through the browser's own textContent and setAttribute.6 Flask turns on Jinja autoescaping for templates ending in .html, .htm, .xml, .xhtml and .svg.7 In PHP, htmlspecialchars() converts both kinds of quote by default since PHP 8.1.0; before that version single quotes were left as they were.8
Each framework also has a way to write raw HTML, and that is where encoding stops: dangerouslySetInnerHTML in React, v-html in Vue, the |safe filter and Markup in Jinja.967 In plain browser code the equivalent is innerHTML, which passes its value to the HTML parser.10 The text-only properties do not parse markup at all, and the value is passed to them unencoded:3
// Parsed as HTML: a <script> or <img onerror> in name runs
el.innerHTML = "Hello " + name;
// Inserted as text: the browser shows the characters as typed
el.textContent = "Hello " + name;
// One query parameter: & ? = / are percent-encoded
const url = "/search?q=" + encodeURIComponent(query);encodeURIComponent() leaves only letters, digits and - _ . ! ~ * ' ( ) unchanged, so an ampersand in "Jack & Jill" no longer starts a new parameter.11 It encodes one component and does not check a whole URL: a link whose entire address comes from a user also needs a check that it starts with http: or https:, since a javascript: address runs script when clicked.36
Where users may write formatted HTML, encoding would show their tags as text, and an HTML sanitizer such as DOMPurify cleans the markup instead. A Content Security Policy adds a layer behind encoding and is not the main defence.3
In SecFlash
SecFlash reports HTML built from values without escaping as SF-SEC-710 on the Security page. A value from the address or a form that lands inside an HTML attribute is reported as SF-SEC-635, and scripts that use eval or innerHTML as SF-SEC-646, both on the same page.
See also
- Cross-site scripting
- Input validation
- SQL injection
- Command injection
- Content Security Policy
- Category: Security concepts
References
- a b c d OWASP (2018). "C4: Encode and Escape Data". OWASP Top 10 Proactive Controls, 2018 edition.
- a b c d MITRE (28 October 2026). "CWE-116: Improper Encoding or Escaping of Output". Common Weakness Enumeration, version 5.0.
- a b c d e f g "Cross Site Scripting Prevention Cheat Sheet". OWASP Cheat Sheet Series.
- back MITRE. "CWE-838: Inappropriate Encoding for Output Context". Common Weakness Enumeration.
- back "Introducing JSX". React documentation (legacy).
- a b c "Security". Vue.js guide.
- a b "Templates". Flask documentation. Pallets.
- back "htmlspecialchars". PHP Manual.
- back "Common components (e.g. <div>)". React documentation.
- back MDN contributors. "Node: textContent property". MDN Web Docs. Mozilla.
- back MDN contributors. "encodeURIComponent()". MDN Web Docs. Mozilla.
External links
- Cross Site Scripting Prevention Cheat Sheet, OWASP Cheat Sheet Series
- OWASP Java Encoder, OWASP Foundation