Confirming injection and cross-site scripting safely

Unit 4 of 5 in Testing web applications.

You will be able to explain why injection and cross-site scripting happen, confirm them with the smallest evidence, say how sure you are, and write the fix the developer needs.

Lesson

One idea: data treated as commands

The Top 10:2025 describes injection as an application flaw that lets untrusted input be sent to an interpreter, such as a browser, a database or the command line, which then runs parts of that input as commands. The concept is the same for every interpreter. The page's background section reports that injection includes cross-site scripting (high frequency, lower impact) and SQL injection (lower frequency, higher impact).

The OWASP cheat sheets describe the same root cause for each. For SQL injection, untrusted data becomes part of the query's syntax and changes its logic, and the data can come from an HTTP request, another service, an email, a directory or the database itself. For cross-site scripting, the application puts untrusted data into a page in a way that lets the browser treat it as code.

How a tester finds and confirms an injection point

WSTG-INPV-05 states two objectives: identify SQL injection points, and assess the severity of the injection and the level of access that could be reached. It says to list every input whose value could end up in a query, including hidden fields, headers and cookies, and to test each separately. It also describes how an application's error messages change the job: visible database errors make it easier to understand the query, and hidden ones mean the tester has to infer the query's logic some other way.

The Top 10 says detection is best achieved by combining source code review with automated testing, including fuzzing, of every input: parameters, headers, URL, cookies and request bodies. For an authorized tester, the careful version of confirmation is small and paired. Read the code first if you have it. Send the smallest harmless change that would behave differently if input reached the query, send a matching control, and compare. Record whether the finding is demonstrated or only reasoned from the code, and rate it accordingly. The Juice Shop lab shows a surprising case: a probe that stays quiet does not rule an injection out.

The cheat sheet warns on the other side too: the impact of a successful injection depends on database features and privileges. Confirming that input changes a query is not the same as showing how much data was at risk, and you do not need to pull data out to prove either.

The fix is to keep data and commands apart

The SQL injection cheat sheet's resolution is to define the SQL structure separately from the data values and bind the values through the database API: parameterized queries for values, and fixed, application-owned choices for table names, column names and sort order. The Top 10 agrees that the preferred option is a safe API with a parameterized interface, and adds two cautions: stored procedures can still be vulnerable if they build queries by joining strings, and structure names such as table and column names cannot be escaped, so user-chosen structure names are dangerous.

Both pages put input validation in its place. A positive, server-side check on the expected form of a value helps, but the Top 10 says it is not a complete defense because many fields legitimately need special characters. The cheat sheet makes a point worth carrying into every report: parameter binding stops a value from changing the query's syntax, but it does not decide whether the caller may access the matching record. Injection and access control are separate checks.

Cross-site scripting: encode for the place the data lands

The XSS cheat sheet's philosophy is coverage: every place untrusted data enters a page needs protection, and an application that protects ninety-nine places and misses one is still vulnerable. It recommends relying on framework defaults that escape every value rendered, preferring safe APIs that treat data as text, using context-specific output encoding where syntax is interpreted, and using a maintained sanitizer when users may submit HTML.

It names the places modern frameworks let developers step around their protection, such as React's dangerouslySetInnerHTML without sanitizing, Angular's bypassSecurityTrust functions and Lit's unsafeHTML. For a tester, those names are things to look for when you have the code, and for a developer they are the lines to review. Input validation does not replace these protections.

A content security policy is described as an additional layer, not the primary defense, because it is easy to get wrong. Trusted Types, where supported, make covered DOM sinks reject plain strings. When you write the finding, say which layer failed (encoding at output, a framework escape hatch, or no policy) so the fix goes to the right place.

Key points

Sources

Lecture

Web app security for builders: The builder's view of the same list of bugs: safe queries, output handling and review habits.

Practice in the labs

Understand each bug on paper, confirm one carefully in a practice app, then write and prove the fix.

Knowledge check

  1. What is the root idea behind injection in the Top 10:2025 description? Answer: Untrusted input is sent to an interpreter, which runs parts of it as commands. The A05 description says the concept is identical across interpreters: data and commands are mixed.
  2. What does the SQL injection cheat sheet say parameter binding does not do? Answer: Decide whether the caller may access the matching record. Binding fixes the syntax problem. Whether the user may see a record is an access-control question.
  3. A table name is chosen by the user and placed in a query. What do OWASP's pages advise? Answer: Map the user's choice to a fixed, application-owned list, because structure names cannot be escaped or bound. The Top 10 notes structure names cannot be escaped, and the cheat sheet says to use fixed application-owned choices.
  4. How does the XSS cheat sheet rank a content security policy? Answer: As an additional layer of defense, not the primary one, because it is easy to get wrong. The cheat sheet recommends framework protections, output encoding and sanitization first, and a policy as an extra layer.

Back to the syllabus or see all classes.

Related