OWASP Security
Learn how to ensure your code meets OWASP (Open Web Application Security Project) security standards.
OWASP Top 10
A01: Broken Access Control
- Inadequate access controls
- Privilege escalation
- Insecure direct object references
A02: Cryptographic Failures
- Weak encryption
- Insecure data transmission
- Inadequate key management
A03: Injection
- SQL injection
- NoSQL injection
- Command injection
A04: Insecure Design
- Security by design
- Threat modeling
- Secure architecture
A05: Security Misconfiguration
- Default configurations
- Insecure headers
- Unnecessary features
A06: Vulnerable Components
- Outdated dependencies
- Known vulnerabilities
- Insecure libraries
A07: Authentication Failures
- Weak authentication
- Session management
- Multi-factor authentication
A08: Software and Data Integrity
- Supply chain security
- Code integrity
- Data validation
A09: Security Logging
- Insufficient logging
- Log tampering
- Monitoring gaps
A10: Server-Side Request Forgery
- SSRF vulnerabilities
- Insecure requests
- Input validation
Common Violations
Access Control
// Bad: No access control
app.get('/admin', (req, res) => {
res.send(adminData); // No authentication check
});
// Good: Proper access control
app.get('/admin', authenticate, authorize('admin'), (req, res) => {
res.send(adminData);
});
Input Validation
// Bad: No input validation
app.post('/user', (req, res) => {
const user = req.body; // No validation
createUser(user);
});
// Good: Input validation
app.post('/user', validateUser, (req, res) => {
const user = req.body; // Validated
createUser(user);
});
Data Encryption
// Bad: No encryption
const data = await getData(); // No encryption
localStorage.setItem('data', data);
// Good: Encrypted data
const encryptedData = await encrypt(data);
localStorage.setItem('data', encryptedData);
Best Practices
Security Headers
// Good: Security headers
app.use((req, res, next) => {
res.setHeader('X-Content-Type-Options', 'nosniff');
res.setHeader('X-Frame-Options', 'DENY');
res.setHeader('X-XSS-Protection', '1; mode=block');
next();
});
Input Sanitization
// Good: Input sanitization
const sanitizeInput = (input) => {
return input
.replace(/<script[^<]*(?:(?!</script>)<[^<]*)*</script>/gi, '')
.replace(/[<>]/g, '');
};
Secure Authentication
// Good: Secure authentication
const authenticate = async (req, res, next) => {
try {
const token = req.headers.authorization?.split(' ')[1];
const decoded = jwt.verify(token, process.env.JWT_SECRET);
req.user = decoded;
next();
} catch (error) {
res.status(401).json({ error: 'Invalid token' });
}
};
Compliance Checklist
- Access controls are implemented
- Input validation is in place
- Data encryption is used
- Security headers are set
- Dependencies are up to date
- Authentication is secure
- Logging is adequate
- Regular security audits are conducted