Trace Untrusted Input to a Dangerous Sink
Follow a URL value through transformations to an HTML sink. Compare innerHTML with textContent, inspect a browser preview, and contrast HTML rendering with SQL parameter binding.
To trace untrusted input to a dangerous sink, identify who controls the value, follow every transformation, and inspect how the final operation interprets it. A value does not become trusted because it was trimmed, renamed, passed to a helper, or stored in a database.
Build on Sources, Sinks, and Data Flow. In this short lecture, you will follow a name from a URL to a greeting, compare two browser APIs, and inspect the same boundary in a SQL query. Work through the demos before answering the final check. The reading and demos are free; sign in to save the completed lecture.
Your brief: a name should stay a name
A welcome page accepts a name query parameter and displays “Hello, Riley”. The product requirement is ordinary text. It does not support rich HTML in names. A helper trims whitespace and caps names at 80 characters before the UI renders them.
| Part of the trace | In this feature | Your question |
|---|---|---|
| Source | The URL’s name value | Who can choose these characters? |
| Transformation | trim().slice(0, 80) | What property does this actually enforce? |
| Sink | A DOM assignment to the greeting | Will the browser receive text or HTML? |
1. Follow the value, not the variable name
Assume the page has already found an ordinary <div id="greeting">. These teaching excerpts show the remaining path. Select each stop and locate the exact expression that carries the caller’s characters forward.
Inspect each handoff. Ask who controls the value and whether its meaning changes.
const params = new URLSearchParams(location.search);
export const rawName = params.get('name') ?? '';
Anyone who can construct the link can choose name. URLSearchParams decodes the query value; it does not establish that the result is safe HTML.
Review question: At which line can caller-controlled characters become document structure, and which change preserves the plain-text greeting?
Write the trace as name → rawName → displayName → renderGreeting → innerHTML. The identifiers change, but provenance does not. At the transformation, name the guarantee narrowly: surrounding whitespace is removed and the result has at most 80 UTF-16 code units. Neither operation is an HTML sanitizer.
At the sink, ask why HTML parsing is needed at all. A plain-text requirement gives you a smaller, clearer fix than designing a markup policy for a feature that does not need markup.
2. Observe the HTML boundary
innerHTML parses a string as HTML. Assigning to textContent on this ordinary div creates text instead. That difference is the boundary you are testing; the same characters can produce different document content. See the MDN innerHTML reference and textContent reference for the API behavior.
Same characters, different interpretation
- 1. Source value
" <strong>Riley</strong> "- 2. Value reaching the sink
"<strong>Riley</strong>"
const displayName = rawName.trim().slice(0, 80);
greeting.innerHTML = `Hello, ${displayName}`;
Predict whether the name will become markup, then render it. Change the sink and compare.
Make these three comparisons
- HTML sample, normalization on, innerHTML. Render the name containing
<strong>. Did removing whitespace stop the browser from creating an element? - The same sample, textContent. Change only the sink and render again. Look for literal angle brackets. The input remains untrusted, while this output path treats it as text.
- The event-handler probe, both sinks. Compare the parsed image element with the literal name. This preview blocks scripts and resource loads, so use the code trace to assess the original application rather than expecting an alert here.
Do not test only a literal <script> tag. Scripts inserted through innerHTML do not execute in the same way as a page’s initial scripts, while other active HTML, including event handlers, can create XSS paths. A single inactive payload is insufficient evidence of safety.
3. Change the sink, preserve the feature
greeting.textContent = `Hello, ${displayName}`;
The greeting still displays names, including names containing punctuation. The change removes HTML interpretation from this plain-text feature. Keep sensible length and type checks for the input contract, but explain that they solve a different problem.
What if the product intentionally accepts rich HTML?
Then displaying every tag literally may violate the feature requirement. Review an explicit allowed-markup policy and a maintained HTML sanitizer at the HTML boundary. Verify that later processing does not undo that protection. A function called sanitize is a starting point for inspection, not proof that the policy is appropriate.
This lecture’s greeting has no rich-text requirement. Its simpler text sink is therefore the intended fix.
Output controls depend on context. Ordinary text, URLs, HTML, and executable code need different handling. For safe sinks and HTML sanitization guidance, consult the OWASP XSS Prevention Cheat Sheet.
4. Compare a database sink
Now a notes search sends a caller’s term to PostgreSQL through node-postgres. Enter an ordinary apostrophe, such as the one in O'Reilly. Inspect where the character lands before and after changing the repository to a bound value.
Where does the search value go?
await db.query(
`SELECT id, title FROM notes WHERE title = '${term}'`
);
SELECT id, title FROM notes WHERE title = 'O'Reilly'
[]
The search term enters SQL text. The apostrophe in an ordinary name can disturb the quoted value. Caller input belongs in a bound parameter, not inside the query string.
This demo shows the driver arguments. It does not run a query or predict database results. The placeholder syntax here belongs to node-postgres.
The safe version keeps the SQL template fixed and passes the term separately. In node-postgres, $1 represents a value parameter. This does not parameterize a table name, column name, or arbitrary SQL fragment. Check the exact driver API when tracing a real query. The node-postgres query documentation describes that contract.
5. State what your trace proves
A useful finding connects the source to a specific sink and a specific missing control. Before answering the check, explain these four points in your own words:
- Control: someone constructing the URL can choose the name.
- Survival: the relevant markup survives trimming and the length cap.
- Interpretation: the original helper passes it to the browser’s HTML parser.
- Fix and verification: a text sink preserves ordinary names and displays markup characters literally.
Compare with a concise review note
The URL’s name reaches renderGreeting after whitespace removal and truncation. Its characters remain caller-controlled. Assigning them through innerHTML permits HTML interpretation despite the plain-text requirement. Use textContent on the greeting div, verify ordinary names still render, and verify markup is displayed literally. Assess active content and victim reachability separately when documenting XSS impact.
For a real application, continue beyond the teaching excerpts: inspect decoding, stored copies, alternate renderers, sanitizer behavior, and the page’s execution controls. Finding an input or a sensitive API alone is not the complete argument; the reachable connection and its missing boundary are what matter.
Complete your source-to-sink trace
Loading question…
Next: find the XSS flaw in real code
This lecture is one required step in Sources, Sinks, and Data Flow. You still need to solve the separate cross-site scripting challenge to complete that topic. Identify its own source, transformations, and rendering sink; do not assume its code is identical to this example.