OWASP Top 10 Explained with Practical Examples

Security2026-11-22TryQuickToolBox

You've probably heard of the OWASP Top 10—the list of the most critical web application security risks. But knowing the names isn't enough. To truly protect your applications, you need to see how these vulnerabilities manifest in real code and understand the concrete steps to fix them.

In this article, we'll walk through each OWASP Top 10 category with practical examples, showing vulnerable code and its secure counterpart. Whether you're a developer, tester, or security enthusiast, these examples will help you spot and prevent common security flaws.

1. Broken Access Control

Broken access control occurs when users can act outside their intended permissions. For example, a user might access another user's data by changing a URL parameter.

Vulnerable code:

// In an Express.js route
app.get('/api/user/:id', (req, res) => {
  const userId = req.params.id;
  // No check if the requesting user is authorized to view this user
  db.getUser(userId, (err, user) => {
    res.json(user);
  });
});

Fix: Always enforce access control checks on the server side.

app.get('/api/user/:id', (req, res) => {
  const userId = req.params.id;
  if (req.user.id !== userId && !req.user.isAdmin) {
    return res.status(403).send('Forbidden');
  }
  db.getUser(userId, (err, user) => {
    res.json(user);
  });
});

2. Cryptographic Failures

Cryptographic failures (previously "Sensitive Data Exposure") happen when sensitive data is not properly protected. Examples include storing passwords in plain text or using weak hashing algorithms.

Vulnerable code:

// Storing password in plain text
user.password = req.body.password;

Fix: Use strong, adaptive hashing algorithms like bcrypt.

const bcrypt = require('bcrypt');
bcrypt.hash(req.body.password, 10, (err, hash) => {
  user.password = hash;
});

3. Injection

Injection flaws, such as SQL injection, occur when untrusted data is sent to an interpreter as part of a command or query.

Vulnerable code:

const query = `SELECT * FROM users WHERE username = '${req.body.username}' AND password = '${req.body.password}'`;
db.query(query, (err, result) => { ... });

Fix: Use parameterized queries or prepared statements.

const query = 'SELECT * FROM users WHERE username = ? AND password = ?';
db.query(query, [req.body.username, req.body.password], (err, result) => { ... });

4. Insecure Design

Insecure design refers to flaws in the application's architecture and logic. For example, a password reset feature that uses a predictable token.

Vulnerable design: Using a timestamp as a reset token.

const token = Date.now();

Fix: Use cryptographically secure random tokens.

const crypto = require('crypto');
const token = crypto.randomBytes(32).toString('hex');

5. Security Misconfiguration

Security misconfiguration includes leaving default accounts and passwords enabled, displaying stack traces to users, or having unnecessary features enabled.

Example: Exposing detailed error messages in production.

app.use((err, req, res, next) => {
  res.status(500).send(err.stack);
});

Fix: Log errors internally and show generic messages to users.

app.use((err, req, res, next) => {
  console.error(err.stack);
  res.status(500).send('Something went wrong!');
});

6. Vulnerable and Outdated Components

Using libraries or frameworks with known vulnerabilities is a common risk. For example, an outdated version of a popular npm package.

Fix: Regularly update dependencies and use tools like npm audit or OWASP Dependency-Check.

npm audit
npm update

7. Identification and Authentication Failures

These failures allow attackers to compromise passwords, keys, or session tokens, or to exploit other implementation flaws to assume other users' identities.

Vulnerable code: Allowing weak passwords.

if (req.body.password.length < 6) {
  return res.status(400).send('Password too short');
}

Fix: Enforce strong password policies and implement multi-factor authentication.

if (req.body.password.length < 12) {
  return res.status(400).send('Password must be at least 12 characters');
}
// Also check against common passwords list

8. Software and Data Integrity Failures

This category relates to code and infrastructure that does not protect against integrity violations. For example, using untrusted CDNs for JavaScript libraries.

Vulnerable code:

<script src="http://untrusted-cdn.com/library.js"></script>

Fix: Use Subresource Integrity (SRI) and trusted sources.

<script src="https://trusted-cdn.com/library.js" integrity="sha384-..." crossorigin="anonymous"></script>

9. Security Logging and Monitoring Failures

Insufficient logging and monitoring allow attackers to persist in the system without being detected.

Vulnerable code: Not logging failed login attempts.

Fix: Implement comprehensive logging and monitoring.

if (!validCredentials) {
  console.warn(`Failed login attempt for user: ${username} from IP: ${req.ip}`);
  // Consider rate limiting or account lockout
}

10. Server-Side Request Forgery (SSRF)

SSRF occurs when an attacker can make the server send requests to unintended locations. For example, a feature that fetches a URL provided by the user.

Vulnerable code:

app.get('/fetch', (req, res) => {
  const url = req.query.url;
  fetch(url).then(response => response.text()).then(data => res.send(data));
});

Fix: Validate and restrict URLs to allowed domains.

const allowedDomains = ['example.com', 'api.example.com'];
app.get('/fetch', (req, res) => {
  const url = new URL(req.query.url);
  if (!allowedDomains.includes(url.hostname)) {
    return res.status(400).send('Invalid URL');
  }
  fetch(url).then(...);
});

Comparison Table of OWASP Top 10

Category Example Vulnerability Prevention
Broken Access Control Accessing other users' data Enforce server-side access checks
Cryptographic Failures Plain text passwords Use strong hashing (bcrypt)
Injection SQL injection Parameterized queries
Insecure Design Predictable reset tokens Use secure random tokens
Security Misconfiguration Exposed stack traces Generic error messages
Vulnerable Components Outdated libraries Regular updates and audits
Authentication Failures Weak passwords Strong password policies
Integrity Failures Untrusted CDNs Use SRI and trusted sources
Logging Failures No failed login logs Comprehensive logging
SSRF Fetching arbitrary URLs Whitelist allowed domains

FAQ

What is the OWASP Top 10?

The OWASP Top 10 is a regularly updated list of the most critical security risks for web applications, published by the Open Web Application Security Project (OWASP). It serves as a baseline for developers and security professionals to prioritize vulnerabilities.

How often is the OWASP Top 10 updated?

OWASP updates the list periodically, typically every few years, to reflect changes in the threat landscape and technology. The most recent version is from 2021.

Can I use the OWASP Top 10 as a checklist for my application?

Yes, but it's not exhaustive. The OWASP Top 10 highlights the most common risks, but you should also follow secure coding practices, conduct regular security testing, and stay informed about emerging threats.

To further secure your applications, consider using tools like the Nginx Log Analyzer to monitor and analyze access logs for suspicious activities.