01 //Introduction
REST APIs have become the backbone of modern web and mobile applications. They power everything from social media platforms to banking systems. However, this ubiquity makes them prime targets for attackers. API security vulnerabilities can expose sensitive data, enable unauthorized actions, and compromise entire systems.
Unlike traditional web applications where server-side rendering provides some inherent protection, APIs expose raw data and functionality directly. This means every endpoint is a potential attack surface that must be explicitly secured. The OWASP API Security Top 10 highlights that API-specific vulnerabilities differ significantly from traditional web app vulnerabilities.
Why are APIs particularly attractive targets for attackers?
02 //API Security Layers
Effective API security requires defense in depth - multiple layers of protection that work together. If one layer fails, others should still protect the system.
HTTPS · TLS 1.3
JWT · OAuth 2.0 · API Keys
RBAC · ABAC · Scopes
Schema · Sanitization
Encryption · Masking
Throttling · Quotas · DDoS
OWASP API Security Top 10 (2023)
| Rank | Vulnerability | Description |
|---|---|---|
| API1 | Broken Object Level Authorization | Accessing objects belonging to other users |
| API2 | Broken Authentication | Weak or missing authentication mechanisms |
| API3 | Broken Object Property Level Auth | Exposing or modifying sensitive properties |
| API4 | Unrestricted Resource Consumption | No limits on requests or data size |
| API5 | Broken Function Level Authorization | Accessing admin functions as regular user |
| API6 | Unrestricted Access to Sensitive Flows | Automating sensitive operations |
| API7 | Server Side Request Forgery | Making server request attacker-controlled URLs |
| API8 | Security Misconfiguration | Improper security settings |
| API9 | Improper Inventory Management | Undocumented or deprecated endpoints |
| API10 | Unsafe Consumption of APIs | Trusting third-party API responses |
03 //Authentication Flaws
API authentication vulnerabilities allow attackers to impersonate legitimate users or bypass authentication entirely. Common flaws include weak credentials, insecure token handling, and missing authentication on sensitive endpoints.
1// VULNERABLE: No authentication on sensitive endpoint
2app.get('/api/admin/users', async (req, res) => {
3 const users = await User.find({});
4 res.json(users); // Anyone can access all users!
5});
6
7// VULNERABLE: Authentication bypass via parameter
8app.get('/api/user/:id', async (req, res) => {
9 if (req.query.admin === 'true') {
10 // Bypassed with ?admin=true
11 return res.json(await User.findById(req.params.id));
12 }
13 // ... normal auth check
14});
15
16// VULNERABLE: Weak API key validation
17app.use((req, res, next) => {
18 const apiKey = req.headers['x-api-key'];
19 if (apiKey) {
20 next(); // Any non-empty key is accepted!
21 } else {
22 res.status(401).json({ error: 'Unauthorized' });
23 }
24});
25
26// VULNERABLE: Hardcoded credentials
27const ADMIN_TOKEN = 'admin123'; // In source code!
28if (req.headers.authorization === ADMIN_TOKEN) {
29 // Grant admin access
30}1const jwt = require('jsonwebtoken');
2
3// Middleware to verify JWT
4const authenticate = async (req, res, next) => {
5 try {
6 const authHeader = req.headers.authorization;
7 if (!authHeader?.startsWith('Bearer ')) {
8 return res.status(401).json({ error: 'Missing token' });
9 }
10
11 const token = authHeader.substring(7);
12 const decoded = jwt.verify(token, process.env.JWT_SECRET, {
13 algorithms: ['HS256'],
14 issuer: 'https://api.myapp.com'
15 });
16
17 // Attach user to request
18 req.user = await User.findById(decoded.sub);
19 if (!req.user) {
20 return res.status(401).json({ error: 'User not found' });
21 }
22
23 next();
24 } catch (error) {
25 return res.status(401).json({ error: 'Invalid token' });
26 }
27};
28
29// Apply to all routes that need authentication
30app.use('/api/protected/*', authenticate);
31
32// API key validation with proper lookup
33const validateApiKey = async (req, res, next) => {
34 const apiKey = req.headers['x-api-key'];
35 if (!apiKey) {
36 return res.status(401).json({ error: 'API key required' });
37 }
38
39 // Hash and compare (never store plain API keys)
40 const hashedKey = crypto.createHash('sha256').update(apiKey).digest('hex');
41 const keyRecord = await ApiKey.findOne({ hashedKey, active: true });
42
43 if (!keyRecord) {
44 return res.status(401).json({ error: 'Invalid API key' });
45 }
46
47 req.apiClient = keyRecord.client;
48 next();
49};What's wrong with checking if(apiKey) to validate API keys?
05 //Input Validation
APIs must validate all input data - not just for security, but also for data integrity. Without proper validation, APIs are vulnerable to injection attacks, data corruption, and crashes.
1// VULNERABLE: No validation - SQL Injection
2app.get('/api/users/search', async (req, res) => {
3 const query = "SELECT * FROM users WHERE name = '" + req.query.name + "'";
4 // Injection: ?name=' OR '1'='1
5});
6
7// VULNERABLE: No validation - NoSQL Injection
8app.post('/api/login', async (req, res) => {
9 const user = await User.findOne({
10 email: req.body.email,
11 password: req.body.password // { "$ne": "" } bypasses auth!
12 });
13});
14
15// VULNERABLE: No type checking
16app.post('/api/products', async (req, res) => {
17 const product = new Product({
18 name: req.body.name,
19 price: req.body.price, // Could be negative, string, or object!
20 quantity: req.body.quantity
21 });
22});
23
24// VULNERABLE: Path traversal
25app.get('/api/files/:filename', (req, res) => {
26 const filePath = './uploads/' + req.params.filename;
27 // Attacker: /api/files/../../etc/passwd
28 res.sendFile(filePath);
29});1const Joi = require('joi');
2
3// Define schemas for all inputs
4const schemas = {
5 createProduct: Joi.object({
6 name: Joi.string().min(1).max(100).required(),
7 price: Joi.number().positive().precision(2).required(),
8 quantity: Joi.number().integer().min(0).required(),
9 category: Joi.string().valid('electronics', 'clothing', 'food').required()
10 }),
11
12 searchUsers: Joi.object({
13 name: Joi.string().max(50).pattern(/^[a-zA-Z ]+$/),
14 page: Joi.number().integer().min(1).default(1),
15 limit: Joi.number().integer().min(1).max(100).default(20)
16 })
17};
18
19// Validation middleware
20const validate = (schemaName, source = 'body') => (req, res, next) => {
21 const { error, value } = schemas[schemaName].validate(req[source], {
22 abortEarly: false,
23 stripUnknown: true // Remove unexpected fields
24 });
25
26 if (error) {
27 return res.status(400).json({
28 error: 'Validation failed',
29 details: error.details.map(d => d.message)
30 });
31 }
32
33 req.validated = value;
34 next();
35};
36
37// Usage
38app.post('/api/products',
39 authenticate,
40 validate('createProduct', 'body'),
41 async (req, res) => {
42 // req.validated contains sanitized data
43 const product = new Product(req.validated);
44 await product.save();
45 res.status(201).json(product);
46 }
47);
48
49// Prevent path traversal
50const path = require('path');
51app.get('/api/files/:filename', authenticate, (req, res) => {
52 const uploadsDir = path.resolve('./uploads');
53 const filePath = path.resolve(uploadsDir, req.params.filename);
54
55 // Ensure the resolved path is within uploads directory
56 if (!filePath.startsWith(uploadsDir)) {
57 return res.status(400).json({ error: 'Invalid filename' });
58 }
59
60 res.sendFile(filePath);
61});What is the purpose of 'stripUnknown: true' in Joi validation?
06 //Excessive Data Exposure
APIs often return more data than necessary, relying on clients to filter sensitive information. This 'excessive data exposure' violates the principle of least privilege and can leak sensitive information.
1// VULNERABLE: Returns entire user object
2app.get('/api/users/:id', authenticate, async (req, res) => {
3 const user = await User.findById(req.params.id);
4 res.json(user); // Includes password hash, SSN, internal flags!
5});
6
7// Response includes sensitive data:
8{
9 "id": "123",
10 "name": "John Doe",
11 "email": "john@example.com",
12 "passwordHash": "$2b$10$...", // LEAKED!
13 "ssn": "123-45-6789", // LEAKED!
14 "internalNotes": "VIP customer", // LEAKED!
15 "failedLoginAttempts": 3, // LEAKED!
16 "roles": ["admin"] // LEAKED!
17}
18
19// VULNERABLE: Debug information in errors
20app.use((err, req, res, next) => {
21 res.status(500).json({
22 error: err.message,
23 stack: err.stack, // Reveals internal code paths!
24 query: req.query, // Reveals request data!
25 dbError: err.originalError // Reveals DB details!
26 });
27});
28
29// VULNERABLE: Exposing internal IDs
30app.get('/api/orders', authenticate, async (req, res) => {
31 const orders = await Order.find({ userId: req.user.id });
32 // Response includes: "_id", "__v", "userId", "internalStatus"
33});1// Define explicit response schemas (DTOs)
2const userPublicFields = ['id', 'name', 'email', 'avatar', 'createdAt'];
3
4const sanitizeUser = (user) => {
5 const sanitized = {};
6 for (const field of userPublicFields) {
7 sanitized[field] = user[field];
8 }
9 return sanitized;
10};
11
12app.get('/api/users/:id', authenticate, async (req, res) => {
13 const user = await User.findById(req.params.id);
14 res.json(sanitizeUser(user)); // Only public fields
15});
16
17// Using select() to limit database queries
18app.get('/api/users', authenticate, async (req, res) => {
19 const users = await User.find({})
20 .select('id name email avatar createdAt') // Only fetch needed fields
21 .lean(); // Plain objects, no Mongoose overhead
22 res.json(users);
23});
24
25// Transform output with toJSON
26const userSchema = new mongoose.Schema({
27 name: String,
28 email: String,
29 passwordHash: String,
30 ssn: String
31});
32
33userSchema.set('toJSON', {
34 transform: (doc, ret) => {
35 delete ret.passwordHash;
36 delete ret.ssn;
37 delete ret.__v;
38 ret.id = ret._id;
39 delete ret._id;
40 return ret;
41 }
42});
43
44// Safe error handling
45app.use((err, req, res, next) => {
46 // Log full error internally
47 console.error('API Error:', {
48 error: err,
49 path: req.path,
50 userId: req.user?.id
51 });
52
53 // Return sanitized error to client
54 const statusCode = err.statusCode || 500;
55 res.status(statusCode).json({
56 error: statusCode === 500 ? 'Internal server error' : err.message,
57 code: err.code || 'UNKNOWN_ERROR'
58 });
59});Why should APIs avoid returning entire database objects?
07 //Prevention Techniques
Securing REST APIs requires a combination of authentication, authorization, input validation, and output filtering. Here are essential security controls for API development.
API Security Checklist
| Category | Control | Priority |
|---|---|---|
| Authentication | Use OAuth 2.0 or JWT with proper validation | Critical |
| Authentication | Implement token expiration and rotation | High |
| Authentication | Use secure password hashing (bcrypt, Argon2) | Critical |
| Authorization | Verify resource ownership on every request | Critical |
| Authorization | Implement role-based access control (RBAC) | High |
| Authorization | Use UUIDs instead of sequential IDs | Medium |
| Input | Validate all inputs with schema validation | Critical |
| Input | Use parameterized queries for databases | Critical |
| Input | Limit request body size | High |
| Output | Define explicit response schemas (DTOs) | High |
| Output | Never expose stack traces in production | Critical |
| Transport | Enforce HTTPS everywhere | Critical |
| Transport | Set security headers (CORS, CSP) | High |
| Monitoring | Log authentication failures | High |
| Monitoring | Implement anomaly detection | Medium |
1const helmet = require('helmet');
2const cors = require('cors');
3
4// Security headers
5app.use(helmet());
6
7// Strict CORS configuration
8app.use(cors({
9 origin: ['https://myapp.com', 'https://admin.myapp.com'],
10 methods: ['GET', 'POST', 'PUT', 'DELETE'],
11 allowedHeaders: ['Content-Type', 'Authorization'],
12 credentials: true,
13 maxAge: 86400 // Cache preflight for 24 hours
14}));
15
16// Additional security headers
17app.use((req, res, next) => {
18 // Prevent caching of API responses with sensitive data
19 res.set('Cache-Control', 'no-store');
20 res.set('Pragma', 'no-cache');
21
22 // Prevent MIME type sniffing
23 res.set('X-Content-Type-Options', 'nosniff');
24
25 // Remove server fingerprint
26 res.removeHeader('X-Powered-By');
27
28 next();
29});
30
31// Request size limits
32app.use(express.json({ limit: '100kb' }));
33app.use(express.urlencoded({ limit: '100kb', extended: true }));Why is strict CORS configuration important for APIs?
08 //Rate Limiting & DoS
APIs without rate limiting are vulnerable to denial of service attacks, brute force attacks, and resource exhaustion. Proper rate limiting protects both availability and security.
1const rateLimit = require('express-rate-limit');
2const RedisStore = require('rate-limit-redis');
3const Redis = require('ioredis');
4
5const redis = new Redis(process.env.REDIS_URL);
6
7// General API rate limit
8const apiLimiter = rateLimit({
9 store: new RedisStore({ client: redis }),
10 windowMs: 15 * 60 * 1000, // 15 minutes
11 max: 100, // 100 requests per window
12 message: { error: 'Too many requests, please try again later' },
13 standardHeaders: true, // Return rate limit info in headers
14 legacyHeaders: false
15});
16
17// Strict limit for authentication endpoints
18const authLimiter = rateLimit({
19 store: new RedisStore({ client: redis }),
20 windowMs: 60 * 60 * 1000, // 1 hour
21 max: 5, // 5 attempts per hour
22 message: { error: 'Too many login attempts' },
23 skipSuccessfulRequests: true // Only count failures
24});
25
26// Per-user rate limiting
27const userLimiter = rateLimit({
28 store: new RedisStore({ client: redis }),
29 windowMs: 60 * 1000, // 1 minute
30 max: 30,
31 keyGenerator: (req) => req.user?.id || req.ip, // Limit per user
32});
33
34// Apply limiters
35app.use('/api/', apiLimiter);
36app.use('/api/auth/login', authLimiter);
37app.use('/api/protected/', authenticate, userLimiter);
38
39// Slow down repeated requests (progressive delay)
40const slowDown = require('express-slow-down');
41
42const speedLimiter = slowDown({
43 windowMs: 15 * 60 * 1000,
44 delayAfter: 50, // Allow 50 requests, then...
45 delayMs: 500 // Add 500ms delay per request
46});
47
48app.use('/api/', speedLimiter);Rate Limit Recommendations
| Endpoint Type | Window | Limit | Reason |
|---|---|---|---|
| Login/Auth | 1 hour | 5-10 | Prevent brute force |
| Password Reset | 1 hour | 3 | Prevent enumeration |
| API General | 15 min | 100-1000 | Fair usage |
| Search/List | 1 min | 20-50 | Expensive queries |
| File Upload | 1 hour | 10 | Resource intensive |
| Webhooks | 1 min | 100 | Burst handling |
Why should authentication endpoints have stricter rate limits?
09 //Mass Assignment Attacks
Mass assignment occurs when an API blindly accepts all user-provided fields and uses them to create or update objects. Attackers can inject unexpected fields to escalate privileges or modify protected data.
1// VULNERABLE: Spreading request body directly
2app.post('/api/users', async (req, res) => {
3 const user = new User(req.body); // Accepts ALL fields!
4 await user.save();
5});
6
7// Attacker sends:
8{
9 "name": "Attacker",
10 "email": "attacker@evil.com",
11 "role": "admin", // INJECTED!
12 "verified": true, // INJECTED!
13 "subscriptionTier": "enterprise" // INJECTED!
14}
15
16// VULNERABLE: Object.assign for updates
17app.put('/api/users/:id', authenticate, async (req, res) => {
18 const user = await User.findById(req.params.id);
19 Object.assign(user, req.body); // Overwrites any field!
20 await user.save();
21});
22
23// VULNERABLE: Mongoose findByIdAndUpdate
24app.patch('/api/products/:id', async (req, res) => {
25 const product = await Product.findByIdAndUpdate(
26 req.params.id,
27 req.body, // Can modify price, inventory, anything!
28 { new: true }
29 );
30});1// Solution 1: Explicit field allowlists
2const allowedUserFields = ['name', 'email', 'bio', 'avatar'];
3
4const pickAllowedFields = (obj, allowedFields) => {
5 const picked = {};
6 for (const field of allowedFields) {
7 if (obj[field] !== undefined) {
8 picked[field] = obj[field];
9 }
10 }
11 return picked;
12};
13
14app.post('/api/users', async (req, res) => {
15 const userData = pickAllowedFields(req.body, allowedUserFields);
16 // role, verified, etc. are NEVER accepted from input
17 const user = new User({
18 ...userData,
19 role: 'user', // Set by server
20 verified: false // Set by server
21 });
22 await user.save();
23});
24
25// Solution 2: Joi schema validation with stripUnknown
26const createUserSchema = Joi.object({
27 name: Joi.string().required(),
28 email: Joi.string().email().required(),
29 password: Joi.string().min(8).required(),
30 bio: Joi.string().max(500)
31}).options({ stripUnknown: true });
32
33app.post('/api/users', async (req, res) => {
34 const { value, error } = createUserSchema.validate(req.body);
35 if (error) return res.status(400).json({ error: error.message });
36
37 // value only contains schema-defined fields
38 const user = new User(value);
39 await user.save();
40});
41
42// Solution 3: Different schemas for create vs update
43const userCreateSchema = Joi.object({
44 name: Joi.string().required(),
45 email: Joi.string().email().required(),
46 password: Joi.string().min(8).required()
47});
48
49const userUpdateSchema = Joi.object({
50 name: Joi.string(),
51 bio: Joi.string().max(500),
52 avatar: Joi.string().uri()
53 // email, password require separate flows with verification
54});What is the best defense against mass assignment?
10 //Real-World Case Studies
These real-world API breaches demonstrate the severe consequences of API security failures and the importance of proper security controls.
1# The attacker discovered an unauthenticated API endpoint
2GET /api/customers/{phoneNumber}
3
4# By enumerating phone numbers, they accessed:
5- Names, addresses, SSNs
6- Driver's license numbers
7- Account PINs
8- 54 million customer records
9
10# Root causes:
111. Missing authentication on internal API
122. API exposed to internet (misconfiguration)
133. No rate limiting allowed enumeration
144. Excessive data in responseWhat common factor contributed to most of these API breaches?
11 //Tools & Resources
These tools help identify API security vulnerabilities during development and testing.
API Security Testing Tools
| Tool | Type | Use Case |
|---|---|---|
| Burp Suite | Proxy/Scanner | Intercepting and testing API requests |
| OWASP ZAP | Scanner | Automated API vulnerability scanning |
| Postman | Testing | API testing with security collections |
| Insomnia | Testing | API client with authentication support |
| SQLMap | Scanner | SQL injection detection in APIs |
| Nuclei | Scanner | Template-based API scanning |
| Arjun | Discovery | Hidden API parameter discovery |
| Kiterunner | Discovery | API endpoint enumeration |
- OWASP API Security Project
- PortSwigger API Testing Guide
- OpenAPI Specification
- OWASP REST Security Cheat Sheet
- API Security Checklist (GitHub)
12 //Conclusion
REST API security requires a comprehensive approach addressing authentication, authorization, input validation, and data protection. The stateless and data-centric nature of APIs makes them both powerful and vulnerable.
When reviewing API code, focus on the authorization layer first - it is the most commonly exploited weakness. Then verify input validation and output filtering. A secure API is one where every request is authenticated, authorized, validated, and returns only the minimum necessary data.