Module 11 · Web Application Security · Lesson 32 of 75

Injection, XSS, CSRF, SSRF and file flaws

How each classic web vulnerability works, what an attacker gains, and the coding fix that removes it.

In this lesson · about 45 minutes

  • Explain SQL injection and parameterised queries
  • Tell reflected, stored and DOM XSS apart and fix them with output encoding and CSP
  • Describe CSRF, SSRF, path traversal and unsafe uploads with their defences

SQL injection

query = "SELECT * FROM users WHERE email = '" + email + "' AND pass = '" + pw + "'"
email = admin@shop.test' --
→ SELECT * FROM users WHERE email = 'admin@shop.test' --' AND pass = '...'

The -- comments out the password check. Variants: UNION-based (append the attacker's own query to read other tables), error-based, and blind (boolean or time-based: ' AND SLEEP(5)--). Impact: reading or changing the whole database, sometimes running commands on the server.

Fix: parameterised queries (prepared statements) or a safe ORM, always. Plus least-privilege database accounts and input validation as extra layers. Escaping by hand is fragile.

Command injection

User input reaching a shell: ping -c 1 8.8.8.8; cat /etc/passwd. Fix: avoid the shell; pass arguments as a list; allow-list inputs (an IP address must look like one).

Cross-site scripting (XSS)

The attacker gets their JavaScript to run in other users' browsers on the vulnerable site, where it can read the page, act as the user, or steal tokens.

  • Reflected: the script is in a link (/search?q=<script>...) and bounced back in the response.
  • Stored: saved in the database (a comment, a profile name) and served to everyone who views it. The most dangerous.
  • DOM-based: the page's own JavaScript writes untrusted data into the page (innerHTML = location.hash).

Fix: context-aware output encoding (turn < into &lt; in HTML; modern frameworks such as React do this by default unless you bypass them), sanitise rich HTML with a library, avoid innerHTML, and add a Content Security Policy that blocks inline scripts. HttpOnly cookies limit the damage.

CSRF (cross-site request forgery)

A malicious page makes the victim's browser send a request to a site where they are logged in, such as changing their email address, and the browser attaches the cookie automatically. Fix: anti-CSRF tokens, SameSite cookies, and re-authentication for sensitive actions.

SSRF (server-side request forgery)

The app fetches a URL the user provides ("import image from URL"). The attacker gives an internal address instead, such as http://169.254.169.254/latest/meta-data/, the cloud metadata service, which can return cloud credentials (Module 20). Fix: allow-list destinations, block internal ranges after DNS resolution, and require IMDSv2 in AWS.

Path traversal and file inclusion

/download?file=../../../../etc/passwd climbs out of the intended folder. Fix: map IDs to files on the server instead of using names from the user; canonicalise and check the final path.

Unsafe file upload

Uploading shell.php as a "profile picture" to a server that runs PHP from the upload folder gives remote code execution. Fix: allow-list extensions and check content, rename files, store them outside the web root or in object storage, and never execute uploads.

Other server-side issues worth knowing

XXE (XML external entities read local files), insecure deserialisation (objects from users turned into running code), server-side template injection, and open redirects (used to make phishing links look legitimate).

Sorting drill

Name the vulnerability

Which flaw does each example show?

  • Login with the email ' OR 1=1 -- logs you in as the first user
  • A forum post containing <script> runs for everyone who reads it
  • Visiting an evil page silently changes your shop account's email
  • 'Fetch image from URL' accepts http://10.0.0.5:8080/admin
  • /invoice?file=../../config/database.yml returns the DB password
  • ?q=<img src=x onerror=alert(1)> in a search link pops an alert

0 of 6 sorted

Check your understanding0 of 4 correct

Question 1

1.What is the reliable fix for SQL injection?

Question 2

2.Which XSS type is saved on the server and served to other users?

Question 3

3.Why is http://169.254.169.254 a favourite SSRF target?

Question 4

4.Which header helps stop injected scripts from running?

Quiz score: 0 of 4

Tip: answer every quiz question correctly first.

Written by Noviq Academy in its own words. All labs are simulations: no real systems are touched, and every name, address, password and hash is made up. The offensive techniques taught here are for defending systems and for authorised testing only; never scan, access or test a system without the owner's written permission. Tool and product names belong to their owners, and command output may differ slightly from yours. Read-aloud uses your device's built-in voices.