Untrusted input: injection and cross-site scripting

Unit 4 of 5 in Web fundamentals.

You will be able to explain why untrusted data must be kept apart from code, and apply parameterized queries and output encoding.

Lesson

The root cause: data that becomes code

Injection is a flaw that lets untrusted user input be sent to an interpreter, such as a database, a browser or the command line, which then executes parts of that input as commands. OWASP lists when an application is vulnerable: user data is not validated, filtered or sanitized; dynamic queries or non-parameterized calls without context-aware escaping are used directly in the interpreter; or hostile data is directly used or concatenated into a query or command.

The cure follows from the cause: keep data and instructions apart.

SQL: parameterize the query

OWASP's primary defense against SQL injection is the parameterized query. Define the SQL structure separately from its data values and bind the values through the database API. The cheat sheet warns that concatenating untrusted values into the query before preparing it defeats the protection. For parts of a query that cannot be parameterized, such as table or column names, map the user's choice to a strict allow-list of fixed values owned by the application. It says manual escaping is error-prone and strongly discouraged.

OWASP also lists least privilege as a hardening step: give the application's database account only the rights it needs, so that even a successful injection can do less.

Cross-site scripting: output encoding

Cross-site scripting happens when an application puts untrusted data into a page in a way that lets the browser treat it as code. A comment that should appear as plain text instead runs as script. Untrusted data is anything the application did not create itself: form input, URL parameters, stored user data and responses from other services.

The fix is output encoding: when displaying data exactly as typed, encode it for the place it goes, so that variables are not interpreted as code. OWASP says to start with your framework's default output encoding, use an encoding library to cover gaps, and notes that coverage is what matters, since every place that adds untrusted data to the page needs protection. Input validation helps but does not replace encoding, and a content security policy is a second layer rather than the first.

The habit

Whenever you write code that builds a query, a command or a page from data you did not create, ask: is this data going into a place where it could be read as instructions? If yes, use the safe API for that place, a parameterized query or output encoding, and not a clever filter.

Key points

Sources

Lecture

Web app security for builders: A talk on the main ways web apps are weakened and the habits that prevent them.

Practice in the labs

Learn each flaw in a practice app on your own machine, then apply the fix.

Knowledge check

  1. What is the recommended primary defense against SQL injection? Answer: Parameterized queries that keep SQL structure separate from data. OWASP: define the SQL structure separately and bind data values through the database API; concatenation defeats the protection.
  2. A comment box shows whatever a visitor types, and one comment runs as script in other visitors' browsers. What is missing? Answer: Output encoding for the place the data is shown. XSS happens when untrusted data is put into a page so the browser treats it as code. Output encoding keeps data from being interpreted as code.
  3. Why is escaping input by hand discouraged? Answer: It is error-prone compared with safe APIs. OWASP says relying on manually escaping dynamic input is error-prone and strongly discouraged; use parameterization instead.

Back to the syllabus or see all classes.

Related