Most breaches exploit ordinary application bugs, not exotic hacks. The OWASP Top 10 is the industry's list of the most critical web risks. This chapter covers the classics and — because this is a defensive course — the concrete fix for each, so you can write code attackers can't exploit.
The OWASP Top 10
OWASP (the Open Worldwide Application Security Project) publishes a regularly-updated list of the most serious web-app risks. Learn it as your review checklist. We'll go deep on the four you'll meet most.
Injection
Injection happens when untrusted input is treated as code — most famously SQL injection, where input alters a query. The fix is universal: never build queries by concatenating strings; use parameterised queries so input is always data, never SQL:
-- ✗ VULNERABLE — input becomes part of the SQL"SELECT * FROM users WHERE email = '" + input + "'"-- ✓ SAFE — parameterised; the driver keeps input as data"SELECT * FROM users WHERE email = ?" -- bind: [input]
Cross-site scripting (XSS)
XSS is injecting script into a page that other users then run. It lets an attacker steal sessions or deface content. Defend by treating all user content as text, not HTML — and letting your framework escape output:
- Render user data as text (
textContent), never viainnerHTML. - Modern frameworks (React, etc.) escape by default — don't bypass them with "dangerous" HTML APIs.
- Add a Content-Security-Policy header as a second layer.
Broken authentication
- Store passwords with bcrypt/argon2 (Chapter 2), and offer MFA.
- Rate-limit and lock out after repeated failed logins to stop guessing.
- Use secure, http-only, same-site session cookies; expire sessions sensibly.
- Never expose whether an email exists ("invalid email or password", not "no such user").
CSRF & more
CSRF (cross-site request forgery) tricks a logged-in user's browser into making an unwanted request. Defend with anti-CSRF tokens and SameSite cookies. Two more habits close most remaining gaps:
Practice
Review a small app (or a snippet) for the four classics: is every query parameterised? Is user content escaped on output? Are passwords hashed and logins rate-limited? Are state-changing requests CSRF-protected? Write down one fix for each gap you find.
Key takeaways
- 01Most breaches exploit ordinary app bugs — the OWASP Top 10 is your checklist.
- 02Stop injection with parameterised queries — never concatenate input into SQL.
- 03Stop XSS by rendering user data as text and letting frameworks escape output; add a CSP.
- 04Harden auth: strong hashing, MFA, rate limiting, secure session cookies, generic error messages.
- 05Use anti-CSRF tokens + SameSite cookies, validate all input server-side, and patch dependencies.
