Prompt
How do I set up a WAF for an API gateway?
Latest observation
A WAF for an API gateway is usually set up to sit in front of the gateway so it can inspect and block bad traffic before it reaches your API. The exact steps depend on your stack, but the pattern is the same.
Typical architecture
Client → WAF → API Gateway → Services
You can also place the WAF at the CDN/load balancer layer if that’s where your traffic first enters.
What to protect against
For APIs, a WAF should primarily help with:
- SQL injection
- Cross-site scripting
- Path traversal
- Known exploit signatures
- Bot abuse / scraping
- Request flooding / basic L7 abuse
- Malformed JSON or protocol anomalies
For APIs, you’ll often want to combine WAF with:
- Authentication/authorization
- Rate limiting / throttling
- Schema validation
- IP allow/deny lists
- JWT validation
- mTLS for internal APIs
Setup steps
1) Put the WAF in front of the gateway
Choose where to attach it:
- Cloud WAF: AWS WAF + CloudFront/ALB/API Gateway, Azure WAF, Cloud Armor, Cloudflare WAF
- Self-managed: ModSecurity with Nginx/Apache, Envoy filters, Kong/Apigee plugins, NGINX App Protect
If your API gateway is public, the WAF should be the first public entry point.
2) Enable managed rules
Start with a managed rule set from your provider:
- OWASP Top 10 protections
- Known bad IPs / reputation feeds
- Bot protection rules
- Protocol enforcement rules
These give you baseline protection without writing everything yourself.
3) Add API-specific rules
APIs need more than generic web rules:
- Enforce allowed HTTP methods only
- Block unexpected content types
- Limit request size
- Require valid JSON for JSON endpoints
- Block suspicious query strings and headers
- Restrict allowed paths
- Validate
Host,Content-Type, and auth headers
Example policies:
- Only allow
GET/POST/PUT/PATCH/DELETEwhere needed - Reject requests over 1 MB
- Reject
Content-Typenot equal toapplication/jsonfor JSON endpoints - Block requests with empty/missing auth headers on protected routes
4) Use “detect” mode first
Before blocking traffic:
- Run the WAF in monitor/log-only mode
- Review false positives
- Tune exceptions for legitimate clients
- Then switch to blocking mode
This avoids breaking real API consumers.
5) Tune by route
Do not apply one generic policy to everything if your API has different behavior per route.
Examples:
- Public endpoints: stricter bot/rate protections
- Auth endpoints: brute-force protections
- Upload endpoints: larger body size, file-type restrictions
- Admin endpoints: IP allowlisting, stricter auth, more aggressive blocking
6) Add rate limiting separately
A WAF can help, but API gateways are usually better for:
- per-client rate limits
- burst limits
- quotas by API key / JWT / tenant
Use both together.
7) Log and monitor
Make sure you can see:
- blocked requests
- matched rules
- source IP / geo / ASN
- route targeted
- request ID / trace ID
Send logs to your SIEM or observability stack.
8) Test thoroughly
Use:
- harmless penetration tests
- OWASP ZAP or Burp Suite in a controlled way
- replay of known-good production traffic
- negative tests for blocked payloads
Check for:
- false positives
- latency overhead
- unexpected header/body rewriting
- health-check traffic being blocked
Common cloud examples
AWS
Common setup:
- CloudFront + AWS WAF in front of API Gateway or ALB
- Or AWS WAF directly with API Gateway for regional APIs
Typical actions:
- attach managed rule groups
- add custom rules for allowed methods/content types
- rate-based rules
- allowlist internal IPs
Azure
- Azure WAF with Application Gateway or Front Door in front of API Management
GCP
- Cloud Armor in front of external HTTPS load balancer / gateway layer
Cloudflare
- Put API behind Cloudflare proxy
- Apply WAF, bot management, rate limits, and API shielding rules
Practical recommendations for APIs
- Prefer positive security models where possible: allow only known methods, paths, content types, and headers
- Validate against your OpenAPI/JSON schema at the gateway if supported
- Don’t rely on WAF alone for auth or business logic protection
- Exclude or carefully tune rules for:
- GraphQL
- file uploads
- webhook receivers
- large JSON bodies
- Use mTLS, OAuth/JWT, and rate limits alongside the WAF
Minimal implementation checklist
- WAF placed before the API gateway
- Managed rules enabled
- API-specific custom rules added
- Log-only mode tested
- False positives tuned
- Route-specific policies applied
- Rate limiting enabled
- Alerts and dashboards configured
- Regular rule review and updates scheduled
If you want, I can give you a provider-specific walkthrough for AWS, Azure, GCP, Cloudflare, or an open-source setup with NGINX/ModSecurity.