Why Security Testing
- A single SQL injection can leak an entire user database
- A missing rate limit enables brute-force password attacks
- Improper authorization lets users access each other’s data
- These bugs are not caught by functional tests
OWASP API Security Top 10 (2023)
API1: Broken Object Level Authorization
Users access other users’ resources by changing IDs in URLs.
/invoices/123 -> /invoices/124. Fix: check ownership on every request.API2: Broken Authentication
Weak passwords, missing MFA, flawed token handling. Fix: JWT with strong secrets, short expiry, algorithm enforcement.
API3: Broken Object Property Level Authorization
API returns fields the user shouldn’t see (passwordHash, isAdmin, balance). Fix: whitelist fields in response.
API4: Unrestricted Resource Consumption
Missing rate limits. Attackers can make unlimited requests to drain resources. Fix: express-rate-limit.
API5: Broken Function Level Authorization
Regular users access admin endpoints by guessing URLs. Fix: checkRole middleware on all admin routes.
API6: Unrestricted Sensitive Business Flows
Bots abuse flows like mass account creation or ticket purchasing. Fix: CAPTCHA, rate limits on sensitive flows.
API7: Server-Side Request Forgery (SSRF)
API fetches a URL from user input, allowing probing of internal services. Fix: whitelist allowed URLs.
API8: Security Misconfiguration
Default credentials, verbose error messages, unnecessary services, missing HTTPS. Fix: helmet, hide error details in production.
API9: Improper Inventory Management
Old API versions and shadow endpoints remain exposed. Fix: version your API, deprecate and remove old versions.
API10: Unsafe API Consumption
Your API trusts data from third-party APIs without validation. Fix: validate and sanitize all external data.
SQL Injection
Vulnerable Code
GET /users?name=' OR '1'='1 returns ALL users.
Prevention: Parameterized Queries
? placeholder values automatically, preventing SQL injection.
Extra: Input Sanitization with express-validator
Rate Limiting
Helmet: Secure HTTP Headers
CORS Misconfiguration
JWT Security
Algorithm Confusion Attack
If you don’t specify allowed algorithms, some library versions acceptalg: "none":
Weak JWT Secret Check
Security Testing Tools
npm audit
Snyk
OWASP ZAP (Automated Scanner)
Jest Security Tests
Write security-specific tests to verify protections are active:Key Terms
Common Mistakes
Trusting client-side validation only
Browser validation can be bypassed with Postman or curl. Always validate on the server.
Returning stack traces in production
Stack traces reveal internal structure to attackers. Return generic messages; log details server-side.
Hardcoded secrets in code
API keys in source code are visible to anyone with repo access. Always use environment variables.
Not running npm audit in CI
Vulnerable dependencies go unnoticed. Add npm audit —audit-level=high to your CI pipeline and fail on critical findings.