Login
ChallengesLearn
Scoreboard
Teams
Profile

Preferences

Truesapiens

LearnAPI SecurityAPI Security Hardening
API Security·Lesson 9 of 11

API Security Hardening

API gateways, schema validation, response filtering (projection), rate limiting on all endpoints, per-object authorization checks, audit logging of every API call. The production API security checklist.

Intermediate14 min
APIHardeningDefense
Loading lesson…
PreviousAPI Automated Security TestingNextAPI Security Review & Practice

© 2026 Truesapiens.

Terms of ServicePrivacy PolicyCookie Policy

A single misconfigured API gateway or a missing authorisation check can expose millions of records. Defence in depth for APIs means layering gateway controls, schema validation, response filtering, and per-object authorisation checks so that no single bypass compromises the system.

What you'll be able to do
  • Explain defence in depth for API security.
  • Configure API gateway controls (rate limiting, schema validation, auth).
  • Harden GraphQL and REST API endpoints.
  • Apply per-object authorisation checks in every handler.
Key terms
API Gateway
A reverse-proxy layer that sits in front of API backends and enforces cross-cutting policies such as authentication, rate limiting, request validation, and logging. Examples: Kong, AWS API Gateway, Zuul.
Schema Validation
The practice of validating incoming API requests against a contract (e.g. OpenAPI spec) before they reach business logic. Requests that do not match the schema are rejected early.
Per-object Authorisation
An access control check that verifies the authenticated user owns or has permission for the specific resource being accessed, not just that they are authenticated. Must be repeated in every handler.
Depth Limiting
A GraphQL-specific defence that rejects queries exceeding a configured nesting depth, preventing deeply nested malicious queries from exhausting server resources.
What is it?

Defence in depth for APIs

API security cannot rely on a single control. Defence in depth means stacking multiple independent layers so that if one fails, the next still protects the system. Every request should pass through an API gateway that handles authentication, rate limiting, and schema validation before reaching the application controller. Inside the controller, each handler must perform its own per-object authorisation check and filter the response to return only the fields the client is allowed to see.

An API gateway like Kong, AWS API Gateway, or Netflix Zuul acts as the single entry point. It terminates TLS, validates API keys, enforces rate limits, logs every request, and can reject malformed payloads before they reach your application. This pattern is sometimes called the "gateway pattern" or "edge service" pattern. The gateway is the first line of defence, but it must be backed by application-level checks because a gateway bypass (e.g. internal network access to the backend) would otherwise bypass all controls.

Schema validation using the OpenAPI specification (or JSON Schema) ensures that every request body matches a documented contract. Requests with unexpected fields, wrong data types, or excessively large payloads are rejected at the gateway. This prevents a class of attacks where an attacker sends a malformed request that triggers unexpected behaviour in the application logic.

Try it

API Gateway Hardening Lab

Toggle security controls on and off in the gateway configuration, then send a test request to see which layers pass or block it.

gateway-dashboardprod
Aadmin
gateway-dashboard · security controls

API Gateway Security Dashboard

Toggle security controls and send a test request to see the pipeline in action.

Fetch own profile

Request pipeline
Configure controls and send a request to see the pipeline.
Access log

No requests yet.

API request pipeline
Mini Map
Press enter or space to select a node. You can then use the arrow keys to move the node around. Press delete to remove it and escape to cancel.
Press enter or space to select an edge. You can then press delete to remove it or escape to cancel.
Real-world relevance

Capital One 2019 — a gateway misconfiguration exposed 100 million records

In 2019, a former AWS employee exploited a misconfigured Web Application Firewall (WAF) on Capital One's API gateway to perform a Server-Side Request Forgery (SSRF) attack. The attacker used the SSRF to reach the AWS metadata service (169.254.169.254) and exfiltrate temporary credentials that granted access to Capital One's S3 buckets. Over 100 million credit card applications were exposed, including names, addresses, credit scores, and Social Security numbers. Capital One eventually paid $190 million in fines.

The root cause was not a missing auth check in the application logic but a weakness in the gateway layer: the WAF did not block requests to the metadata service, and the gateway's network configuration allowed outbound traffic to internal AWS endpoints. After the breach, Capital One implemented proper gateway hardening: network segmentation between the gateway and backend services, blocking access to the metadata service at the gateway level, and adding SSRF-specific WAF rules. The incident is a textbook case of why every layer in the defence-in-depth stack must be configured correctly.

Mitigation

Harden GraphQL and REST APIs

GraphQL introduces unique attack surfaces. Without depth limiting, an attacker can send a deeply nested query like user { friends { friends { friends ... } } } that causes exponential database load. Cost analysis assigns a cost value to each field and rejects queries that exceed a budget. Field-level middleware ensures that a resolver checks authorisation even if the parent resolver has already checked it — GraphQL's resolver chain makes per-field checks essential. Persisted queries disable ad-hoc GraphQL queries in production, allowing only pre-approved query strings.

For REST APIs, never include sensitive data in URL parameters (e.g. tokens, API keys) because URLs are logged by proxies, load balancers, and browser history. Enforce the Content-Type header and reject requests with unexpected content types (e.g. reject XML if you only accept JSON). Set request body size limits at the gateway to prevent large payload attacks. Use HATEOAS-style responses where the server tells the client which actions are available, preventing enumeration of hidden endpoints.

typescriptsafe
// Per-object authorisation — every handler checks ownership
async function getOrder(req: Request, res: Response) {
  const user = await authenticate(req);
  const order = await db.order.findUnique({
    where: { id: req.params.id },
  });
  // Must check: does this user own this order?
  if (order.userId !== user.id) {
    return res.status(403).json({ error: 'forbidden' });
  }
  // Response filtering — only return allowed fields
  const { id, status, createdAt, items } = order;
  return res.json({ id, status, createdAt, items });
}

Rate limiting should apply to every endpoint, not just login. A search endpoint or a graphql batch endpoint can be abused just as easily as an auth endpoint. Every API call should also be logged with a consistent audit trail that includes the user ID, IP address, endpoint, timestamp, and response status. Logs are essential for post-incident forensics. Finally, configure CORS strictly — allow only the specific origins that need access, never use a wildcard in production.

Key takeaways

What to remember

  • Defence in depth means stacking gateway, schema, auth, rate, and response controls so no single bypass is fatal.
  • An API gateway is not a silver bullet — every handler must still perform per-object authorisation.
  • GraphQL needs depth limiting, cost analysis, field-level authorisation, and persisted queries in production.
  • REST APIs must enforce Content-Type, body size limits, and never expose sensitive data in URLs.
  • The Capital One breach (2019) proves that gateway misconfigurations can be as damaging as application bugs.
Further reading
  • Capital One Data Breach — What Happened and Why(Capital One)
  • OWASP REST Security Cheat Sheet(OWASP)
  • GraphQL Security — Depth Limiting and Cost Analysis(GraphQL Foundation)

Knowledge check

0/3 answered · 0 correct
  1. 1.What is the primary purpose of an API gateway in a defence-in-depth architecture?

  2. 2.Which GraphQL-specific defence prevents attackers from sending exponentially nested queries?

  3. 3.What was the root cause of the 2019 Capital One data breach?