Slide 24 of 28
Part 4 · PreventionSlide 24
Slide 24 · Mitigation 6
MIT 06
Validate every API response against a defined schema.

Define the exact expected shape of every API response as a schema (OpenAPI, JSON Schema, or your framework's equivalent). Then validate all responses against that schema — in tests and ideally in production.

This does two things:

Catches accidental exposure: When a developer adds a new field to the database model, the response schema doesn't automatically include it. If the serializer starts returning it, the schema validation fails — and you catch the exposure before it ships.

Documents intent: The response schema is a written commitment about what the API returns. It forces a deliberate decision about every field in every response.

Think of the response schema as a list of approved items you're allowed to pack in a box before shipping it to the client. Anything not on the approved list gets flagged before the box leaves the building.

In tests: assert the response shape matches the schema on every API test. In production: use middleware that strips unexpected fields from responses (defensive) or logs a warning when they appear (monitoring). Tools like Spectral (OpenAPI linting) can catch schema violations in CI.

💼 Business takeaway

Ask your QA team whether security tests include sending unexpected fields — specifically admin or privilege fields — in API requests, and confirming they are rejected rather than silently applied.

← Back MIT 07: Enforce at the API layer →