← Dashboard

cybersecurity / web security

Web Application Security for Developers: Find the Bug in Your Own Code

Two vulnerable code snippets and their fixes, a map from each bug to the current OWASP Top 10:2025, the browser settings that block whole bug classes, and the free labs where you can practice. Aimed at people who write web code, not at people planning a security career.

updated Nov 2026·a 10-minute read·OWASP list checked against the 2025 edition

Most web vulnerabilities are not exotic. They are an ordinary line of code that trusts something it should not: a string pasted into a query, a record ID taken from the URL, a cookie readable by any script. If you build web applications, the fastest way to learn security is to read code the way an attacker would and ask one question of every input: what happens if this value is hostile? This page is organized around that question. It uses real, runnable examples first, then points to the standard references and the labs.

01 · A VULNERABLE LOGIN

One quote character changes the query

Here is a login check written the way many first applications are. It builds the SQL by pasting the user's input into a string. The version below it uses a parameterized query instead. The script runs on any machine with Python and uses the built-in SQLite module.

login.pyrun: python3 login.py
import sqlite3

db = sqlite3.connect(":memory:")
db.execute("CREATE TABLE users (name TEXT, pw TEXT)")
db.executemany("INSERT INTO users VALUES (?, ?)", [("alice", "s3cret"), ("bob", "hunter2")])

def login_unsafe(name, pw):
    q = f"SELECT name FROM users WHERE name = '{name}' AND pw = '{pw}'"
    return q, db.execute(q).fetchall()

def login_safe(name, pw):
    q = "SELECT name FROM users WHERE name = ? AND pw = ?"
    return q, db.execute(q, (name, pw)).fetchall()

attack = ("alice' --", "wrong")
print(login_unsafe(*attack))
print(login_safe(*attack))
print(login_unsafe("x' OR '1'='1", "x' OR '1'='1")[1])
outputwhat it printed
("SELECT name FROM users WHERE name = 'alice' --' AND pw = 'wrong'", [('alice',)])
('SELECT name FROM users WHERE name = ? AND pw = ?', [])
[('alice',), ('bob',)]

The attacker typed alice' -- as the username and a wrong password. In the unsafe version, the quote closes the string and -- turns the rest of the query, including the password check, into a comment, so "alice" logs in. The second attack makes the condition always true and returns every user. The safe version sends the query structure and the values separately, so the same input is just an odd username that matches nobody. The OWASP SQL Injection Prevention Cheat Sheet lists parameterized queries as its first defense option, ahead of stored procedures, allow-list validation and escaping, which it strongly discourages as a primary defense.

02 · A MISSING OWNERSHIP CHECK

Changing 1042 to 1043

The second bug has no strange characters at all. A logged-in user opens /invoices/1042, edits the number to 1043, and sees someone else's invoice. The handler checked that the user was logged in but never checked that the invoice belongs to them.

invoices.jsExpress-style pseudocode
// Vulnerable: authenticated, but not authorized for this record
app.get('/invoices/:id', (req, res) => res.json(db.invoice(req.params.id)));

// Fixed: the server decides who may see what
app.get('/invoices/:id', (req, res) => {
  const inv = db.invoice(req.params.id);
  if (!inv || inv.ownerId !== req.user.id) return res.sendStatus(404);
  res.json(inv);
});

Returning 404 for both "does not exist" and "not yours" avoids confirming which IDs exist. PortSwigger's Web Security Academy has a page on insecure direct object references, the name for this class, with labs where you exploit it.

03 · NAMING THE BUG

Where these land in the OWASP Top 10:2025

OWASP published a 2025 edition of its Top 10, so older tutorials that cite the 2021 numbering are out of date. The categories below are the ones the examples fall under, with the fix and the place to practice.

Mapping bugs to the OWASP Top 10:2025
BugOWASP categoryFixPractice
SQL built by string concatenationA05 InjectionParameterized queriesPortSwigger: SQL injection
Script injected into a page (cross-site scripting)A05 InjectionEncode output for its contextPortSwigger: cross-site scripting
Viewing another user's record by changing an IDA01 Broken Access ControlServer-side ownership check on every requestPortSwigger: access control and IDOR
Forged cross-site requestA01 Broken Access ControlAnti-CSRF tokens and SameSite cookiesPortSwigger: CSRF
Default settings left openA02 Security MisconfigurationHardening and secure headersOWASP Juice Shop

OWASP's own pages note that Injection covers both cross-site scripting and SQL injection, and that broken access control covers insecure direct object references, CSRF and SSRF. The full 2025 list runs from A01 Broken Access Control to A10 Mishandling of Exceptional Conditions.

04 · BROWSER-LEVEL DEFENSES

Settings that block whole classes of bugs

Some protections are not code fixes but response headers and cookie attributes, which makes them cheap to add. MDN's web security guide recommends serving everything over HTTPS, setting a Content Security Policy (strict if possible, otherwise at least one that disallows inline JavaScript), and setting the frame-ancestors directive to control who can embed your pages.

Hardening settings and what each one limits
SettingWhereWhat it limits
HTTPS everywhereServer and certificatesEavesdropping and tampering in transit
Content-Security-PolicyResponse headerWhich scripts and resources a page may run, blunting cross-site scripting
frame-ancestorsCSP directiveWhich sites may embed your page in a frame
HttpOnlySet-Cookie attributeStops JavaScript from reading the cookie, so injected script cannot steal a session cookie
SameSiteSet-Cookie attributeControls whether the cookie is sent on cross-site requests, reducing CSRF

These are defense in depth. A strong CSP does not excuse an unencoded output, and HttpOnly does not stop injected script from acting as the user. Fix the bug and add the header.

05 · PRACTICE LEGALLY

Targets you are allowed to break

PortSwigger's Web Security Academy is free, with written explanations and interactive labs grouped by vulnerability and organized into learning paths. OWASP Juice Shop is a deliberately insecure application you run yourself, described by OWASP as probably the most modern and sophisticated insecure web application for security training. OWASP WebGoat is another deliberately vulnerable application. The OWASP Cheat Sheet Series holds the secure-coding counterpart for each topic.

WATCHThe legal line is permission. Test only applications you own or that explicitly invite testing, such as the training targets above or a program with written scope. Trying a SQL injection string on a real site you do not control can be illegal even if you mean no harm.

06 · FAQ

Frequently asked questions

What is the OWASP Top 10?

The OWASP Top 10 is a standard awareness document for web application security, a consensus list of the most critical risks. The current edition is the 2025 version, which begins with A01 Broken Access Control and includes Injection, Security Misconfiguration and Authentication Failures.

Can I learn web security for free?

Yes. PortSwigger's Web Security Academy is free with interactive labs, OWASP publishes the Top 10 and a large cheat sheet series at no cost, and training apps such as Juice Shop and WebGoat are open for you to run yourself.

What do I need to know before learning web security?

You should be comfortable with how HTTP requests and responses work, basic HTML and JavaScript, and one server-side language with a database. You cannot reason about a vulnerable query if you cannot read the query.

Is it legal to practice web hacking?

Only on systems you own or have written permission to test. Intentionally vulnerable training applications and labs such as PortSwigger's are built for it. Probing a real website without authorization can break the law.

What is the difference between SQL injection and XSS?

SQL injection sends hostile input to a database through a query the application builds, letting an attacker read or change data. Cross-site scripting injects script into a page that other users' browsers then run. OWASP groups both under Injection in its 2025 list.

Go deeper

Web security builds on networking and cryptography.