Skip to main content
Security testing verifies that your API cannot be exploited by attackers. A backend that passes all functional tests can still have critical vulnerabilities. This page covers the OWASP API Security Top 10, common attack types and their prevention, and both manual and automated security testing techniques.

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

Attack: GET /users?name=' OR '1'='1 returns ALL users.

Prevention: Parameterized Queries

The mysql2 driver escapes all ? placeholder values automatically, preventing SQL injection.

Extra: Input Sanitization with express-validator

Rate Limiting

Helmet: Secure HTTP Headers

Helmet sets these headers automatically:

CORS Misconfiguration

Never use origin: '*' with credentials: true. This allows any website to make authenticated requests to your API using the user’s cookies.

JWT Security

Algorithm Confusion Attack

If you don’t specify allowed algorithms, some library versions accept alg: "none":

Weak JWT Secret Check

Generate a strong secret:

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.