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.
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.
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])
("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.
// 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.
| Bug | OWASP category | Fix | Practice |
|---|---|---|---|
| SQL built by string concatenation | A05 Injection | Parameterized queries | PortSwigger: SQL injection |
| Script injected into a page (cross-site scripting) | A05 Injection | Encode output for its context | PortSwigger: cross-site scripting |
| Viewing another user's record by changing an ID | A01 Broken Access Control | Server-side ownership check on every request | PortSwigger: access control and IDOR |
| Forged cross-site request | A01 Broken Access Control | Anti-CSRF tokens and SameSite cookies | PortSwigger: CSRF |
| Default settings left open | A02 Security Misconfiguration | Hardening and secure headers | OWASP 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.
- OWASP Top 10 project page ↗The project home, with the current 2025 edition and the data behind it. It redirects to OWASP's projects directory.owasp.org
- OWASP: SQL Injection Prevention Cheat Sheet ↗Primary defenses in order: prepared statements, stored procedures, allow-list validation and escaping.cheatsheetseries.owasp.org
- OWASP: Cross Site Scripting Prevention Cheat Sheet ↗Output encoding rules for HTML, attribute, JavaScript, CSS and URL contexts.cheatsheetseries.owasp.org
- PortSwigger: Insecure direct object references ↗How IDOR works with database objects and static files, with labs.portswigger.net
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.
| Setting | Where | What it limits |
|---|---|---|
| HTTPS everywhere | Server and certificates | Eavesdropping and tampering in transit |
| Content-Security-Policy | Response header | Which scripts and resources a page may run, blunting cross-site scripting |
| frame-ancestors | CSP directive | Which sites may embed your page in a frame |
| HttpOnly | Set-Cookie attribute | Stops JavaScript from reading the cookie, so injected script cannot steal a session cookie |
| SameSite | Set-Cookie attribute | Controls 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.
- MDN: Web security ↗The overview of web security practices, including the recommended baseline of HTTPS, CSP and frame-ancestors.developer.mozilla.org
- MDN: Content Security Policy ↗How the Content-Security-Policy header works, how to build a policy and how to test it.developer.mozilla.org
- MDN: Using HTTP cookies ↗Cookie attributes in detail, including HttpOnly and SameSite.developer.mozilla.org
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.
- PortSwigger Web Security Academy ↗Free training from the makers of Burp Suite, with interactive labs and learning paths.portswigger.net
- OWASP Juice Shop ↗An intentionally vulnerable web shop for training, demos and capture-the-flag practice. Run it on your own machine.owasp.org
- OWASP WebGoat ↗An OWASP application with known vulnerabilities, built for learning web application security and for testing security tools.owasp.org
- OWASP Cheat Sheet Series ↗Concise secure-coding guidance by topic, from access control to authentication to XSS.cheatsheetseries.owasp.org
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.