Find a course

Module 5: Defending and RespondingLesson 10 of 1214 min

Web Application Attacks

See how SQL injection, cross-site scripting and broken access control work, why they keep topping vulnerability lists, and the coding habits that stop them.

Step 1 of 12Learn

When input becomes code

Imagine a login form that builds a database query by gluing the user's input into a string:

SELECT * FROM users WHERE name = ' + name + ' AND password = ' + pass + '

A normal user types "maria" and her password. An attacker types this as the name: ' OR '1'='1' --

The query becomes: SELECT * FROM users WHERE name = '' OR '1'='1' --' AND password = '...'

Since '1'='1' is always true, and -- turns the rest of the line into a comment, the database returns every user, and the attacker is logged in, often as the first account in the table, which is frequently the administrator. Variations of this trick can dump entire databases, change data or delete tables.

This is SQL injection, and the root cause is simple: the application lets data be interpreted as code. It has been known since the late 1990s and still turns up regularly, along with other kinds of injection into operating system commands and templates.

The fix is equally well known: parameterized queries (also called prepared statements). The query is written with placeholders, such as WHERE name = ?, and the user's input is passed to the database separately, so it is always treated as a value, never as part of the command. Most modern frameworks and database libraries do this by default. Input validation (rejecting a name that contains odd characters) and least-privilege database accounts add further layers, but they don't replace parameterization.

Step 2 of 12Practice · Choose one

What is the most reliable fix for SQL injection?

Step 3 of 12Compare

Gluing strings vs a prepared statement

AspectString concatenationParameterized query
How input is usedPasted into the SQL textSent separately as a value
Input ’ OR 1=1 --Becomes part of the query logicIs just an odd username that matches nobody
Who parses the SQLDatabase parses attacker text as codeQuery shape is fixed before data arrives
ResultLogin bypass, data theftInjection blocked

Step 4 of 12Learn

Cross-site scripting

Cross-site scripting (XSS) is injection aimed at other users' browsers instead of the database.

Suppose a forum shows comments exactly as people typed them. An attacker posts a comment containing a <script> tag. Every visitor who views the page now runs the attacker's JavaScript as if it came from the forum itself. The script can steal session cookies, read what's on the page, make requests as the logged-in user, or redirect to a phishing page.

There are three common flavors:

  • Stored XSS: the malicious script is saved on the server (in a comment, profile or message) and served to everyone who views it.
  • Reflected XSS: the script is in a link, such as a search URL, and the site echoes it back in the response. The attacker tricks people into clicking that link.
  • DOM-based XSS: the page's own JavaScript takes data from the URL or another source and inserts it unsafely into the page.

Defenses:

  • Output encoding: when displaying user data, convert special characters so <script> is shown as text instead of being run. Modern frameworks such as React, Angular and Vue do this automatically unless a developer deliberately bypasses it.
  • Content Security Policy (CSP): an HTTP header that tells the browser which script sources are allowed, blocking injected inline scripts.
  • HttpOnly cookies: session cookies marked HttpOnly can't be read by JavaScript, limiting what a successful XSS can steal.

Step 5 of 12Practice · Diagram

Stored XSS from post to stolen session

Escaping output breaks this chain at one step. Which one?

Tap the shape that answers the question, then press Check.

Stored XSS from post to stolen sessionProcess diagram: Attacker posts a script → Site stores it as-is → Victim opens the page → Browser runs the script → Session or data stolen.Attacker posts ascriptSite stores it as-isVictim opens the pageBrowser runs thescriptSession or data stolen
Attacker posts a script

Hidden inside an ordinary-looking forum comment.

Site stores it as-is
Victim opens the page
Browser runs the script

It trusts the script because it came from the real site.

Session or data stolen

Step 6 of 12Practice · Code challenge

Write escapeHtml(text) that makes user text safe to insert into HTML by replacing & with &amp;, < with &lt;, > with &gt;, " with &quot; and ' with &#39;. Replace & first so you don't double-escape.

Runs in a sandbox in your browser.
  • escapeHtml("<script>alert(1)</script>")
  • escapeHtml("Tom & Jerry")
  • escapeHtml("say \"hi\"")
  • escapeHtml("it's")
  • escapeHtml("plain text")

Step 7 of 12Learn

Broken access control and the OWASP Top 10

The OWASP Top 10 is a widely used list of the most critical web application security risks, published by the nonprofit Open Worldwide Application Security Project and updated every few years. Recent editions have been led by broken access control, with injection, cryptographic failures, security misconfiguration, vulnerable components and authentication failures also prominent.

Broken access control means users can do things they shouldn't. A classic example is an insecure direct object reference. You log in to a shopping site and view your order at:

https://shop.example/orders?id=1045

What happens if you change the number to 1046? If the site shows someone else's order, including their name, address and purchase, it checked that you were logged in but not that the order belongs to you. Attackers write scripts that walk through thousands of IDs and scrape everything. Real breaches have exposed millions of records exactly this way.

The fix is to perform an authorization check on the server for every request: does this user own this order, or have a role that permits viewing it? Hiding a button in the interface isn't access control, because attackers send requests directly, without using your interface.

Other common web weaknesses include security misconfiguration (default passwords, verbose error messages, unnecessary features left on) and vulnerable components: outdated libraries with known flaws. Modern apps contain hundreds of third-party packages, so dependency scanning and updates are part of secure development.

Step 8 of 12Practice · What would you do?

What would you do?

You are testing your own company's new web app (with permission). Logged in as a regular user, you change the URL from /invoices/2231 to /invoices/2232 and see another customer's invoice.

Step 9 of 12Note

Step 10 of 12Practice · Match the pairs

Match each web vulnerability to its main defense.

Tap an answer, then tap the place where it belongs (or drag it there).

  1. SQL injection
  2. Cross-site scripting
  3. Insecure direct object reference
  4. Vulnerable components
  5. Default admin password left on

Step 11 of 12Flashcards

Web flaws and their fixes

Tap a card to flip it.

Step 12 of 12Practice · True or false

Hiding the "Delete user" button from non-admin users in the interface is enough to prevent them from deleting users.