All lessons Leer en español

Security in depth · Unit 20 · Lesson 10 of 27

APIs and GraphQL

Review object permissions, property permissions, and resource cost across different API styles.

9 minready

Helpful before thisAuthentication and accessHTTP and proxies

After this lesson you can

  • separate object, function, and property authorization
  • explain what schemas disclose without treating documentation as a flaw
  • connect request cost and retries to business rules

A web API gives software a way to request data or operations. Browsers, mobile apps, and services can all be clients. HTTP and JSON are common choices, but APIs are broader than either one. The security decision belongs to the service, regardless of which interface a client uses.

Check each API decisionOperation: Who may invoke this function?. Object and properties: Which records and fields are permitted?. Work and result: Bound cost and handle retries safelyCheck each API decision1OperationWho may invoke this function?2Object and propertiesWhich records and fields arepermitted?3Work and resultBound cost and handle retries safely
Review permission at each level, then the cost and repeated effects of the work.

Three permission questions

Object-level authorization asks which record the caller may use. Function-level authorization asks which operation they may invoke. Property-level authorization asks which fields they may read or change. Mass assignment is one way property checks fail: automatic binding accepts client-controlled values for sensitive state.

An unexpected JSON field alone is not proof of a flaw. It matters whether the service accepts it, gives it sensitive meaning, and violates the intended policy. List, search, export, and batch operations must also preserve object and tenant boundaries.

Scenario: the booking service

A traveler may view their trip and change a contact address. A support agent may issue certain refunds under a separate policy. The server controls price and refund approval state; those values do not become writable just because the client sends them.

Retries complicate the refund operation. An idempotency key needs a defined scope, ownership, lifetime, and relationship to the request. It helps avoid duplicate processing, but does not replace authorization or make every distributed operation exactly once.

Schemas describe; they do not grant access

OpenAPI describes HTTP interfaces. GraphQL exposes a typed execution model with queries, mutations, and potentially subscriptions. Introspection is a schema feature, not a vulnerability by itself. Decide what documentation may be public, then enforce access regardless of its visibility.

GraphQL checks must cover all routes to sensitive data, including nested fields. Limits on depth help, but shallow queries can still be expensive. Consider operation cost, collection sizes, batch size, timeouts, and cancellation as well as request counts.

Different interfaces, shared principles

JSON-RPC describes named remote procedure calls; it is not simply REST with another spelling. SOAP and gRPC have their own message models and tooling. Their permission and data-boundary questions remain familiar.

Token validation should reflect issuer, audience, purpose, and trusted claim semantics. A schema, API gateway, or CORS policy cannot automatically supply every business-level permission check.

Review a contract, not just a URL

Document callers, permitted operations and fields, resource ownership, data sensitivity, cost budgets, and retry behavior. A large accepted page size is a reason to examine actual resource limits, not automatic proof of an outage. Explain the observed control gap and supported impact separately.

Turn an API description into enforceable rules

The fictional booking team gives you this contract. A traveler can read their own booking and change its contact address. A support agent assigned to that booking can request a refund. The server decides price and approval state. Refund requests use an idempotency key scoped to the caller and operation.

Decision Required relationship
Read booking Caller owns the booking, or another explicit policy grants access.
Change contact Permitted actor and contact fields only.
Request refund Assigned support role and an eligible booking state.

The contract separates a resource, an operation, and writable properties. A valid session, a well-formed JSON body, and a documented endpoint are useful facts but do not establish all three permissions.

PredictAn unchanged refund request is retried with the same key by the same caller. Should the server treat the key as permission to issue a new refund?

The key relates the retry to the prior operation under the service’s retry contract. Authorization still applies. The service should preserve the defined duplicate-handling behavior, and reject or otherwise handle a changed request under that key according to its documented rules.

Cost needs a contract too. Suppose a request may select at most twenty records and has a cost budget of fifty units. The supplied estimate is three units per selected record. A request for twenty records satisfies the count limit but costs sixty units, exceeding the cost budget. These are fictional units for this exercise; real estimates need calibration against the work actually performed.

A gateway request-count limit cannot express every nested field’s cost or each booking’s ownership. Enforce the relevant rules at the components that understand them, and record how retries, partial failures, and cancellation affect state and resource consumption.

Verification should compare permitted operations with refused alternatives using supplied test records. Include batch and export paths: one correct single-record response does not establish that every record in a collection receives the same protection.

Terms you met

APIBOLAmass assignmentGraphQLidempotency key

Check yourself

No timer. No penalties. Read the explanation and try again whenever you like.

  1. A traveler owns a booking and may edit its contact address. Which submitted fields should that permission authorize?

    Show the answer

    Correct answer: Only the fields permitted by the contact-update contract. Property authorization narrows what an otherwise permitted actor and operation may change.

  2. A refund retry uses the same caller, key, and unchanged request. What should its treatment follow?

    Show the answer

    Correct answer: The defined duplicate-handling contract, with the relevant authorization still enforced. The key relates a retry to an operation under a specified scope and lifetime; it is not permission by itself.

  3. The API permits twenty records and fifty cost units. At three units per record, how does a twenty-record request compare?

    Show the answer

    Correct answer: It meets the record-count limit but exceeds cost: twenty times three is sixty. Independent limits can yield different results. The count limit does not override the cost budget.

  4. A single-record endpoint enforces booking ownership. What evidence is needed for an export endpoint?

    Show the answer

    Correct answer: Results showing every exported record respects the caller's permitted set under the export policy. Collection paths can differ from single-object handling and need evidence for their actual selection.

  5. GraphQL introspection reveals field names. Which conclusion is appropriate?

    Show the answer

    Correct answer: It describes the interface; disclosure policy, field authorization, and operation cost still need review. Documentation can be intentional while execution continues to require appropriate controls.

Try it

  • WriteWrite an API acceptance table for booking reads, contact updates, and refunds. Include actor, object, writable fields, state, and retry behavior. Calculate the twenty-record request’s cost and identify which limit it fails. Add one batch-authorization verification criterion. Propose the still-unspecified retry lifetime and duplicate-result policy, clearly labeling those design choices.
References