OWASP Top 10 Explained with Practical Examples
You've probably heard of the OWASP Top 10, but do you know how each vulnerability looks in real code? Many developers treat it as a theoretical checklist, yet these flaws are behind countless breaches. This article walks through each OWASP Top 10 category with practical examples and concrete fixes, so you can spot and stop them in your projects.
1. Broken Access Control
Access control enforces what authenticated users are allowed to do. When it's broken, users can access data or perform actions outside their permissions.
Example: Insecure Direct Object Reference (IDOR)
// Vulnerable: any logged-in user can view any order by changing the ID
app.get('/orders/:id', (req, res) => {
const order = db.getOrder(req.params.id);
res.json(order);
});
// Fixed: check ownership
app.get('/orders/:id', (req, res) => {
const order = db.getOrder(req.params.id);
if (order.userId !== req.user.id) return res.status(403).end();
res.json(order);
});
Fix: Enforce access control server-side, deny by default, and log failures.
2. Cryptographic Failures
Sensitive data must be encrypted in transit and at rest. Common mistakes include using weak algorithms or hardcoding keys.
Example: Storing passwords with MD5
// Vulnerable
const hash = md5(password);
// Fixed: use bcrypt or argon2
const hash = await bcrypt.hash(password, 12);
Fix: Use strong, salted hashing for passwords; encrypt sensitive data with AES-256; always use HTTPS.
3. Injection
Injection occurs when untrusted data is sent to an interpreter as part of a command or query.
Example: SQL Injection
// Vulnerable: string concatenation
const query = "SELECT * FROM users WHERE name = '" + name + "'";
// Fixed: parameterized query
const query = 'SELECT * FROM users WHERE name = ?';
db.execute(query, [name]);
Fix: Use parameterized queries, ORMs, and validate input. For OS commands, avoid shell execution or use safe APIs.
4. Insecure Design
This category focuses on flaws in design and architecture, not just implementation bugs. Threat modeling and secure design patterns are essential.
Example: No rate limiting on password reset
Without rate limiting, attackers can brute-force reset tokens. Fix: Implement rate limiting, use strong tokens, and expire them quickly.
5. Security Misconfiguration
Default configurations, verbose errors, and unnecessary features are common culprits.
Example: Exposing stack traces
// Vulnerable: detailed error to client
app.use((err, req, res, next) => {
res.status(500).send(err.stack);
});
// Fixed: generic message, log details server-side
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send('Something went wrong');
});
Fix: Harden configurations, remove unused features, and use security headers like CSP and HSTS.
6. Vulnerable and Outdated Components
Using libraries with known vulnerabilities is like leaving a door open.
Example: Old version of lodash with prototype pollution
Fix: Regularly update dependencies, use tools like npm audit or OWASP Dependency-Check, and monitor advisories.
7. Identification and Authentication Failures
Weak authentication allows attackers to compromise passwords or session tokens.
Example: Session ID in URL
// Vulnerable: session ID in query string
const sessionId = req.query.sessionId;
// Fixed: use secure, HttpOnly cookies
res.cookie('session', sessionId, { httpOnly: true, secure: true });
Fix: Implement multi-factor authentication, enforce strong passwords, and use secure session management.
8. Software and Data Integrity Failures
This category relates to code and infrastructure that doesn't protect against integrity violations, such as insecure deserialization or using untrusted CDNs.
Example: Insecure deserialization
// Vulnerable: deserializing untrusted data
const obj = deserialize(userInput);
// Fixed: use safe formats like JSON and validate schema
const obj = JSON.parse(userInput);
if (!isValidSchema(obj)) throw new Error('Invalid');
Fix: Verify integrity with digital signatures, use safe serialization formats, and review CI/CD pipelines.
9. Security Logging and Monitoring Failures
Without proper logging, breaches go undetected. Logs must be generated, stored securely, and monitored.
Example: Not logging failed logins
// Vulnerable: no logging
if (!valid) return res.status(401).end();
// Fixed: log the event
if (!valid) {
logger.warn('Failed login attempt', { ip: req.ip, username });
return res.status(401).end();
}
Fix: Log security events, set up alerts, and ensure logs are tamper-proof.
10. Server-Side Request Forgery (SSRF)
SSRF occurs when an attacker can make the server send requests to unintended locations.
Example: Fetching a URL without validation
// Vulnerable: fetches any URL
app.get('/fetch', async (req, res) => {
const response = await fetch(req.query.url);
res.send(await response.text());
});
// Fixed: validate URL against allowlist
const allowedHosts = ['api.example.com'];
app.get('/fetch', async (req, res) => {
const url = new URL(req.query.url);
if (!allowedHosts.includes(url.hostname)) return res.status(400).end();
const response = await fetch(url);
res.send(await response.text());
});
Fix: Use allowlists, validate URLs, and restrict outbound traffic.
Comparison Table: OWASP Top 10 at a Glance
| Category | Common Example | Primary Fix |
|---|---|---|
| Broken Access Control | IDOR | Server-side checks |
| Cryptographic Failures | Weak password hashing | Use bcrypt/argon2 |
| Injection | SQL injection | Parameterized queries |
| Insecure Design | No rate limiting | Threat modeling |
| Security Misconfiguration | Verbose errors | Harden configs |
| Vulnerable Components | Outdated library | Regular updates |
| Auth Failures | Session in URL | Secure cookies |
| Integrity Failures | Insecure deserialization | Validate and sign |
| Logging Failures | No failed login logs | Centralized logging |
| SSRF | Unvalidated URL fetch | Allowlist validation |
FAQ
What is the OWASP Top 10?
The OWASP Top 10 is a standard awareness document for developers and web application security. It represents a broad consensus about the most critical security risks to web applications.
How often is the OWASP Top 10 updated?
OWASP updates the list periodically to reflect emerging threats and changes in technology. The most recent version is from 2021, with updates expected in the future.
Is the OWASP Top 10 enough to secure my application?
No, it's a starting point. A comprehensive security program includes secure coding practices, regular testing, and continuous monitoring beyond the Top 10.
Ready to check your JSON payloads for injection flaws? Use our JSON Formatter to validate and pretty-print JSON safely.