Skip to content

API contract rules

Reviews HTTP endpoints and their callers for method misuse, breaking changes, inconsistent responses and missing protection.

  • Agent id: api_contract
  • Rules: 15 (source: api_contract.yaml)
  • Deterministic checks: api_patterns, orphan_endpoints

API-001: Breaking change to a published endpoint

  • Severity: high
  • Languages: all

Look for a change that removes or renames an endpoint, a response field or an accepted parameter, changes a field's type or meaning, adds a new required request field, or tightens validation, on an API that clients outside the repository call. Additive changes (new optional fields, new endpoints) and changes behind a new version prefix are not violations.

API-002: Caller and endpoint disagree

  • Severity: high
  • Languages: all

Look for client code in the repository whose URL, HTTP method, parameters or expected response fields do not match the server route it calls, and for orphan calls to routes that no handler defines. Start from the orphan endpoints and calls the check reports and confirm each one, since routes registered dynamically or served by another service can look orphaned.

API-003: Unsafe method used for a safe operation, or the reverse

  • Severity: medium
  • Languages: all
  • References: RFC 9110

Look for GET or HEAD handlers that create, change or delete data, and for POST used only to read data that could be a GET with query parameters. POST for a search with a large or structured filter body, documented as such, is not a violation.

API-004: GET or DELETE with a request body

  • Severity: medium
  • Languages: all
  • References: RFC 9110

Look for routes or clients that send or read a body on GET requests, or rely on a body for DELETE. RFC 9110 gives such bodies no defined meaning and proxies may drop them; use query parameters, the path, or POST. Clients that never send a body are not violations.

API-005: Verb in a resource path

  • Severity: low
  • Languages: all

Look for paths such as /getUsers, /createOrder, /deleteItem/{id} that repeat the action the HTTP method already carries. Action sub-resources for operations that are not CRUD (/orders/{id}/cancel), RPC-style APIs that the project uses consistently, and GraphQL or JSON-RPC endpoints are not violations.

API-006: Inconsistent naming across endpoints

  • Severity: low
  • Languages: all

Look for the same API mixing plural and singular collection names (/users and /product), mixing camelCase, snake_case and kebab-case in paths or JSON fields, or naming the same concept differently in two endpoints. Report the convention most endpoints already follow as the one to align on.

API-007: Wrong or inconsistent status codes

  • Severity: medium
  • Languages: all
  • References: RFC 9110

Look for handlers that return 200 with an error payload, 500 for invalid input, 404 for an authorization failure that should be 403 (or the reverse without a stated reason), or 201 without the created resource or its location. Returning 404 instead of 403 on purpose to hide whether a resource exists is not a violation when it is consistent.

API-008: Inconsistent error response format

  • Severity: medium
  • Languages: all
  • References: RFC 9457

Look for endpoints in the same API returning errors in different shapes (a bare string, {error}, {message}, {errors: []}), so clients cannot handle them in one place. Suggest one format such as RFC 9457 problem details. Framework default error pages for routes outside the API are not violations.

API-009: Endpoint without function-level authorization

  • Severity: critical
  • Languages: all
  • References: CWE-285, OWASP API5:2023

Look for admin or privileged API routes (user and role management, bulk export, configuration changes) that check only that the caller is signed in, not that the caller holds the role the function needs, and for HTTP methods on a resource that skip the role check its other methods apply. Report missing authentication and object-level checks under SEC-009 instead. Routes every signed-in user may call by design are not violations.

API-010: Response exposes internal or sensitive fields

  • Severity: high
  • Languages: all
  • References: CWE-200, OWASP API3:2023

Look for handlers that serialize whole database models or internal objects (return user, model_to_dict, toJSON of an entity) so that password hashes, tokens, internal ids, flags or other users' data reach the client. Responses built from an explicit output schema or DTO are not violations.

API-011: Mass assignment from the request body

  • Severity: high
  • Languages: all
  • References: CWE-915, OWASP API3:2023

Look for request bodies copied wholesale into a model or update query (Model(**body), Object.assign(entity, req.body), update(req.body)) so a client can set fields such as role, is_admin, owner_id or price. Bodies parsed through an input schema that lists only the writable fields are not violations.

API-012: Unbounded list endpoint or payload

  • Severity: medium
  • Languages: all
  • References: CWE-770, OWASP API4:2023

Look for collection endpoints without pagination or a maximum page size, request bodies or uploads without a size limit, and filters that let a client request every row. Endpoints over a small fixed set (countries, enum values) are not violations.

API-013: Missing input validation on an endpoint

  • Severity: medium
  • Languages: all
  • References: CWE-20

Look for handlers that read path, query or body values and use them without checking type, range, length or format at the boundary. Handlers whose framework validates the request against a declared schema (FastAPI models, zod, class-validator, JSON schema) are not violations.

API-014: Endpoint missing from the API description

  • Severity: low
  • Languages: all
  • References: OWASP API9:2023

When the project keeps an OpenAPI or Swagger file, or generates one from annotations, look for routes that it does not list and for documented routes that no handler serves. Projects with no API description at all get one low finding for the whole API, not one per route.

API-015: Non-idempotent retry-prone operation

  • Severity: medium
  • Languages: all
  • References: RFC 9110

Look for POST endpoints that create payments, orders or messages with no idempotency key or duplicate check, when clients or queues may retry on timeout. PUT and DELETE handlers that are already idempotent, and operations whose duplicates are harmless, are not violations.