Introduction: The OWASP Top 10 Threat Landscape
The Open Web Application Security Project (OWASP) Top 10 represents the authoritative industry consensus on the most critical security risks facing web applications. Organizations worldwide rely on the OWASP Top 10 to establish defensive development baselines, compliance standards, and penetration testing scopes. This lesson examines the primary vulnerability classes and provides production-grade remediation patterns.
Core Vulnerabilities & Defensive Engineering
1. Broken Access Control (A01)
Broken access control allows unauthorized users to act outside of their intended permissions. Common manifestations include Insecure Direct Object References (IDOR), where an attacker modifies an ID in a request (e.g., /api/user/102/invoice to /api/user/103/invoice) and accesses another user’s records without tenant validation.
Remediation: Enforce server-side record ownership checks on every request. Never rely on the client to dictate permissions.
2. Injection Vulnerabilities (A03 — SQLi, Command Injection)
Injection occurs when untrusted user data is concatenated directly into a query or command interpreter without contextual escaping or parameterized bindings.
-- VULNERABLE: Direct string concatenation
SELECT * FROM users WHERE email = 'user_input' AND password = 'password';
-- SECURE: Parameterized query (Prepared Statements)
SELECT id, email, password_hash FROM users WHERE email = ?;
3. Cross-Site Scripting (XSS)
XSS occurs when malicious JavaScript is injected into trusted web applications and executed in the victim’s browser:
- Stored XSS: Malicious payload is persisted in a database (e.g. in a comment field) and executed whenever other users view the page.
- Reflected XSS: Payload is reflected off the web server via URL parameters without sanitization.
- DOM-based XSS: Vulnerability exists entirely client-side when JavaScript reads user input from sources like
location.searchand writes it to sinks likeinnerHTML.
Defensive Code Implementation: Sanitization and Prepared Statements
// Secure Database Access with PDO Prepared Statements
$stmt = $pdo->prepare('SELECT id, username, email FROM accounts WHERE tenant_id = :tenant AND id = :id');
$stmt->execute([
':tenant' => $current_user->tenant_id, // Tenant isolation prevents IDOR
':id' => (int) $_GET['id']
]);
$account = $stmt->fetch();
// Preventing XSS via Contextual Output Encoding
// Always encode output before rendering into HTML context
echo '<div class="user-bio">' . htmlspecialchars($account['bio'], ENT_QUOTES | ENT_HTML5, 'UTF-8') . '</div>';
Cross-Site Request Forgery (CSRF) & SameSite Cookies
CSRF tricks an authenticated user’s browser into executing unwanted actions on a trusted site. Mitigations include:
- Synchronizer Token Pattern (Nonces): Generating unique, cryptographically random, session-bound tokens submitted with state-changing requests.
- SameSite Cookie Attribute: Setting cookies with
SameSite=LaxorSameSite=Strictprevents the browser from attaching session cookies on cross-origin requests.
Deep Dive: Anatomy of Broken Access Control (A01:2021)
Broken Access Control is currently ranked as the number one risk in the OWASP Top 10. Unlike technical vulnerabilities such as buffer overflows that can be detected by static signature scanners, access control flaws represent logical flaws in business rules and authorization policies. The most devastating variation is Insecure Direct Object References (IDOR).
An IDOR occurs when an application exposes a direct database primary key or record identifier in a client-facing endpoint (such as GET /api/v1/invoices/10492) without verifying that the authenticated user actually owns or is permitted to view that specific record. An attacker simply increments the integer ID in an automated script to download confidential invoices belonging to every customer in the system.
To eliminate IDOR vulnerabilities, enterprise architectures adopt two mandatory patterns:
- Indirect Reference Mapping: Instead of exposing sequential database auto-increment integers, applications use cryptographically random UUIDv4 identifiers or tenant-scoped hashids that cannot be guessed or enumerated sequentially.
- Enforced Contextual Ownership Checks: Every data access layer query must bind the tenant or user context directly into the SQL query:
SELECT * FROM invoices WHERE id = :invoice_id AND organization_id = :current_user_org_id. If the record exists but belongs to another customer, the query returns zero rows, resulting in an immediate 404 Not Found response.
Mitigating Server-Side Request Forgery (SSRF – A10:2021)
Server-Side Request Forgery occurs when a web application accepts an arbitrary URL from an end user (such as a profile avatar image URL or a webhook target) and issues a server-side HTTP request to fetch that resource without sufficient validation. In cloud environments (such as AWS, GCP, or Azure), an attacker supplies the link-local cloud metadata service IP address: http://169.254.169.254/latest/meta-data/iam/security-credentials/. The vulnerable server fetches the URL and returns the response, handing full IAM administrative credentials directly to the attacker.
A complete SSRF defense requires layered controls:
- DNS Resolution Validation: Resolve the supplied domain to an IP address before connecting, and reject any address belonging to RFC 1918 private subnets (
10.0.0.0/8,172.16.0.0/12,192.168.0.0/16), loopback (127.0.0.0/8), or link-local metadata ranges (169.254.0.0/16). - Prevent DNS Rebinding: Ensure that the HTTP client connects directly to the validated IP address rather than re-resolving the hostname, preventing time-of-check to time-of-use (TOCTOU) DNS rebinding attacks.
- IMDSv2 Enforcement: On AWS EC2 instances, enforce IMDSv2, which requires a session token obtained via an HTTP
PUTrequest with custom headers, neutralizing standard SSRF GET requests.
Common Mistakes & Practical Pitfalls
- Blacklist-Based Input Filtering: Attempting to strip malicious strings like
<script>using regular expressions. Attackers bypass blacklists effortlessly using case variation (<sCrIpt>) or alternate tags (<img src=x onerror=alert(1)>). Always use parameterized inputs and contextual output encoding. - Misconfigured CORS Headers: Setting
Access-Control-Allow-Origin: *alongsideAccess-Control-Allow-Credentials: trueexposes authenticated endpoints to malicious cross-origin scripts. - Relying on Content-Type Headers for File Uploads: Checking only the MIME type provided in the
Content-Typeheader allows attackers to upload executable PHP scripts disguised as images. Always validate magic bytes and store uploads outside document root.
Exam Connection: Certification Blueprint Alignment
This module aligns directly with the Web Security & OWASP Top 10 topic on the Cybersecurity Fundamentals Assessment:
- Identifying SQL Injection attack strings (e.g.
' OR 1=1 --) and the appropriate architectural defense. - Differentiating between Stored, Reflected, and DOM-based Cross-Site Scripting.
- Understanding CSRF prevention via nonces and SameSite cookie attributes.
- Recognizing Broken Access Control / IDOR vulnerability scenarios.
Key Takeaways
- Prepared statements completely neutralize SQL injection by separating code from data.
- Contextual output encoding prevents browser interpreters from executing user data as script.
- Access control checks must verify record ownership on the server for every state-changing request.
Knowledge Check
- Why do parameterized queries (prepared statements) prevent SQL injection?
Answer: The database engine treats parameters strictly as data literals, never executing parameter values as executable SQL syntax regardless of input contents. - What is the primary difference between Stored and Reflected XSS?
Answer: Stored XSS persists the malicious payload in a database or server storage; Reflected XSS immediately reflects the payload back in the HTTP response without persistence. - How does the
SameSite=Laxcookie attribute mitigate CSRF?
Answer: It instructs the browser not to send the cookie on cross-site subrequests (such as image loads or cross-site POST submissions).
Next Step
Proceed to module 3: Network Security, Cryptography, and Secure Access Controls, or test your skills on the Cybersecurity Fundamentals Assessment.
