# API Gateway Security

> Use when securing an API gateway on an authorized engagement or review. Covers rate limiting, authentication at the edge, WAF rules, request schema validation, and gateway logging. Use when an API sits behind a gateway or reverse proxy, when edge controls are the first line of defence, when testing OWASP API Security Top 10 issues at the gateway layer, or when reviewing gateway config with Burp Suite traffic as evidence.

- Skill: `mizazhaider-ceh/api-gateway-security` (Agent Skill)
- Install (CLI): `npx get-skill add mizazhaider-ceh/api-gateway-security`
- Raw SKILL.md: https://api.skillmd.com/api/skills/mizazhaider-ceh/api-gateway-security/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Security
- Author: mizazhaider-ceh (https://skillmd.com/u/mizazhaider-ceh)
- Updated: 2026-11-22
- Page: https://skillmd.com/skills/mizazhaider-ceh/api-gateway-security

---


The gateway is where every API request passes first, so it is where
authentication, rate limiting, input validation, and logging either
hold or fail together. A gateway that forwards everything and trusts
the backend to sort it out is not a control, it is a router. This
skill covers testing and hardening the edge on systems you are
authorized to assess: prove each control with live traffic, then
check the config that is supposed to enforce it.

## Quick Workflow

1. Map the gateway: identify the product, the routes, and which
   paths are public, authenticated, or admin only.
2. Test authentication at the edge: no token, expired token, and a
   token for the wrong audience on every route class.
3. Test rate limiting: burst a single endpoint and a single
   credential, then check whether limits are per IP, per key, or
   absent.
4. Test schema validation: oversized bodies, wrong types, unknown
   fields, and malformed JSON against a validated route.
5. Test WAF behaviour: classic injection strings and path traversal
   against protected routes, and note what is blocked versus logged.
6. Test logging: trigger a blocked request and a failed auth, then
   confirm both appear in gateway logs with client, route, and
   decision.
7. Review the config: compare the live behaviour with the declared
   routes, plugins, and policies, and report drift.
8. Re-test after fixes: the same requests, in the same order, to
   prove each control now holds.

## 1. Authentication at the edge

```bash
# Route classes first: which paths demand auth at the gateway
for p in /api/v1/public/health /api/v1/users/me /api/v1/admin/users; do
  echo "== $p without token"
  curl -s -o /dev/null -w '%{http_code}\n' "https://target.example$p"
  echo "== $p with a garbage token"
  curl -s -o /dev/null -w '%{http_code}\n' \
    -H 'Authorization: Bearer garbage-token-value' "https://target.example$p"
done
echo "a public route returning 200 and an admin route returning 200"
echo "without a token are both findings, in different severity bands"
```

```bash
# Token confusion at the edge: gateway trusts a token the backend
# would reject, or vice versa. Test both layers and compare.
curl -s "https://target.example/api/v1/users/me" \
  -H 'Authorization: Bearer expired-or-wrong-audience-token' | jq .
echo "gateway 200 plus backend 401 means the edge is not validating"
echo "gateway 401 plus backend 200 (direct) means defence in depth"
echo "is missing at the service layer, note it as a hardening gap"
```

## 2. Rate limiting

```bash
# Burst test: 60 requests in a loop, count the status codes
for i in $(seq 1 60); do
  curl -s -o /dev/null -w '%{http_code}\n' \
    -H 'Authorization: Bearer user-token-value' \
    "https://target.example/api/v1/users/me"
done | sort | uniq -c
echo "all 200s means no effective limit for this credential"
echo "a 429 that arrives late means the window is generous, record"
echo "the threshold and the window, not just the presence of a 429"
```

```python
#!/usr/bin/env python3
"""Rate limit probe across dimensions: same IP, same key, same user.

Run against an authorized target only. Reports which dimension the
limit (if any) is keyed on, which decides how an attacker evades it.
"""
import subprocess

TARGET = "https://target.example/api/v1/users/me"


def burst(headers, label):
    codes = []
    for _ in range(40):
        out = subprocess.run(
            ["curl", "-s", "-o", "/dev/null", "-w", "%{http_code}",
             TARGET] + headers,
            capture_output=True, text=True, check=False).stdout.strip()
        codes.append(out)
    limited = codes.count("429")
    print(f"{label}: 429s in 40 requests = {limited}")


burst(["-H", "Authorization: Bearer user-token-value"], "same key")
burst([], "no token")
print("limit keyed on IP only means rotating keys does not help")
print("limit keyed on key only means rotating IPs does not help")
print("no limit on either dimension is the credential stuffing risk")
```

## 3. Schema validation and WAF behaviour

```bash
# Schema validation: oversized body, wrong type, unknown field
python3 - << 'PY'
import json, subprocess
base = ["curl", "-s", "-o", "/dev/null", "-w", "%{http_code}",
        "https://target.example/api/v1/users",
        "-H", "Authorization: Bearer user-token-value",
        "-H", "Content-Type: application/json"]
cases = {
    "oversized name (100k chars)": {"name": "A" * 100000},
    "wrong type for age": {"name": "test", "age": "not-a-number"},
    "unknown admin field": {"name": "test", "is_admin": True},
}
for label, body in cases.items():
    out = subprocess.run(base + ["-d", json.dumps(body)],
                         capture_output=True, text=True).stdout.strip()
    print(f"{label}: HTTP {out} (want 400 for the first two)")
PY
```

```bash
# WAF probe: one injection string per class, record block vs pass
for payload in "' OR '1'='1" "../../etc/passwd" "<script>alert(1)</script>"; do
  code=$(curl -s -o /dev/null -w '%{http_code}' -G \
    --data-urlencode "q=$payload" "https://target.example/api/v1/search")
  echo "payload [$payload] -> HTTP $code"
done
echo "a 403 is a block, a 200 with the payload reflected is a gap,"
echo "a 200 that is only logged is a detection gap, not a block"
```

## 4. Gateway logging check

```bash
# Trigger one of each event class, then ask for the log lines
curl -s -o /dev/null "https://target.example/api/v1/admin/users"
curl -s -o /dev/null -H 'Authorization: Bearer garbage-token-value' \
  "https://target.example/api/v1/users/me"
echo "each request above must appear in gateway logs with client IP,"
echo "route, auth decision, and status. Missing auth failures in"
echo "logs means brute force at the edge is invisible to defenders."
```

## Edge cases

- The gateway passes auth through to the backend without validating
  it. Then edge tests look clean while direct-to-service traffic
  bypasses everything; test the service directly where authorized.
- Rate limits exist but are per IP behind a shared NAT or CDN. Then
  one abusive tenant can throttle everyone else; check for a
  per-credential limit as the second layer.
- The WAF runs in log-only mode. Findings must say detection only,
  not protection, and the report should show the logged payloads.
- Schema validation rejects valid clients after a spec change. That
  is a breaking change risk, so strict validation belongs in a
  report-and-enforce rollout, not a silent switch-on.
- Multiple gateways or versions serve the same API. Test each; the
  old gateway usually carries the old, weaker policy.

## Key references

- OWASP API Security Top 10, especially unrestricted resource
  consumption and broken authentication.
- OWASP Web Application Firewall guidance and the gateway vendor
  documentation for the product in front of the target.
- Related skills: api-security-testing, openapi-security-review,
  api-key-management, api-webhook-security, and the sub-skills
  api-authentication-matrix and openapi-ci-gates.

