Certopolis

CompTIA Security+ (SY0-801)

SQL injection vs cross-site scripting (XSS): Security+ explained

Both are injection: untrusted input is treated as code instead of data. In SQL injection the database runs the input as part of a query. In cross-site scripting the input is served back in a web page and runs as script in other users' browsers.

SQL injection

CWE-89 describes a product that builds an SQL command from external input without neutralizing special elements, so the input is interpreted as SQL instead of ordinary data. The attack runs on the server, against the database.

CWE-89's most effective fix is to separate data from code with prepared statements, parameterized queries or stored procedures, without building query strings dynamically inside them. Allowlist input validation adds depth but does not reliably stop SQL injection alone. Running the application with a low-privilege database account limits the damage.

Cross-site scripting

CWE-79 is a child of CWE-74, Injection. The input ends up in a web page served to other users, and runs in their browsers. CWE-79 names three types. Reflected XSS: the server reads data from the HTTP request and reflects it in the response. Stored XSS: the server saves the data, for example in a forum or database, and later puts it into pages for other visitors. DOM-based XSS: the client-side script itself inserts the data into the page.

The main fix is output encoding for the context the data lands in: HTML body, attribute, URL, JavaScript or CSS. Input validation is defense in depth. Setting the HttpOnly flag on a session cookie keeps it from page scripts, but CWE-79 says that is not a complete solution.

Other injection

CWE-78, OS command injection, is also a child of CWE-74. A page that passes a user-supplied hostname to a ping command, where a semicolon lets a second command run, is command injection. Security+ often groups SQL injection, XSS and command injection under the single answer "injection".

Where the exam catches people

  • Believing XSS attacks the server's database: the script runs in other users' browsers; SQL injection is the one that runs in the database.
  • Believing input validation is the main fix for SQL injection: CWE-89 names parameterized queries as most effective and says validation alone is not reliable.
  • Believing XSS is not injection: CWE-79 is a child of CWE-74, Injection.
  • Believing HttpOnly cookies prevent XSS: they only keep the session cookie from scripts, and CWE-79 calls this incomplete.
  • Believing stored and reflected XSS differ in payload: they differ in whether the server saves the input or echoes it from the current request.

Written from CompTIA's documentation

Try 3 questions on this

Pick an answer to see why each option is right or wrong.

  1. Which of the following attacks occurs when text entered in a web form is built into a database command, so the database runs the text as part of the command?

    Right. In the game that’s Renown for your Threats, Vulnerabilities, and Attacks building.

    Not that one: A is right. In the game your Threats, Vulnerabilities, and Attacks building would lose a little Renown, and this question would come back later.

  2. An attacker posts a forum comment containing a script. The site saves the comment and serves it unchanged, so the script runs in every visitor's browser. Which of the following application attacks does this describe?

    Right. In the game that’s Renown for your Threats, Vulnerabilities, and Attacks building.

    Not that one: D is right. In the game your Threats, Vulnerabilities, and Attacks building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    The forum serving the attacker's script to visitors is an injection attack.

    Written from cwe.mitre.org/data/definitions/79.html

  3. A web page passes a user-supplied hostname to the server's ping command. Entering a semicolon followed by another command makes the server run that command too. Which of the following attacks does this describe?

    Right. In the game that’s Renown for your Threats, Vulnerabilities, and Attacks building.

    Not that one: D is right. In the game your Threats, Vulnerabilities, and Attacks building would lose a little Renown, and this question would come back later.