Idempotency keys do not replace tenant authorization
An Idempotency-Key is useful when a client cannot tell whether a write succeeded. It lets the server recognize a replay of the same operation and return the stored result instead of charging a card twice, creating two invoices, or starting two jobs. That makes the header a reliability control. It does not decide whether the caller is allowed to perform the operation for the selected tenant, object, or amount.
The IETF HTTPAPI draft describes Idempotency-Key as a structured header for making non-idempotent methods such as POST and PATCH fault-tolerant. The server has to bind a key to a request and manage a stored response or execution state. The draft is explicit that the mechanism depends on server policy and does not make every repeated request harmless. [1]
Two different questions
The first question is: have I already executed this exact request? The second is: may this principal execute this request now?
Both must be answered before any side effect. A common implementation stores the key in a global table, looks up the prior result, and returns it without rechecking ownership. That behavior creates two failures. A caller who learns another tenant's key may receive a cached result from that tenant. A caller who changes only an authorization-sensitive field may receive a response that looks valid but belongs to a different version of the operation.
The key namespace should be scoped to the authenticated principal and to the operation's security context. Hash the full canonical operation, including method, normalized route, principal, tenant, and the request body after schema validation. Store the request hash with the response. If the same key arrives with a different hash, reject the request instead of silently attaching a new meaning to the old key.
Authorization must be evaluated at replay time
Permissions change. A user may be removed from a tenant, a role may be revoked, or an approval may expire while a request is waiting. A stored success response can be returned only when the current principal still has the right to read that result. If the permission has changed, the server should not reveal the previous body. It can return a conflict or authorization failure and leave the original record intact.
Object-level authorization also needs a place in the transaction. OWASP identifies broken object-level authorization as a separate API risk because a request can name a valid object that belongs to another user. The key does not turn an object identifier into a capability. The server must check the relationship between principal, tenant, object, and action before it consults the idempotency ledger. [2]
Design the storage record
A robust record contains the scoped key hash, principal ID, tenant ID, operation hash, status, and the response metadata needed to replay the result. It should not contain provider credentials, access tokens, or unnecessary personal data. If the result is a reference to a long-running job, store the job ID and authorization context needed to prove that the caller may inspect it.
Use a transaction or compare-and-set operation to reserve the key before creating a side effect. The sequence is: validate the request, authorize it, atomically reserve the key for this principal and operation, execute the effect, then commit the response. An interrupted request is an unknown state, not a free retry. A worker should reconcile the external effect before deciding whether to return success or failure.
Expiry and collision policy
Keys need a retention period that matches the client's retry window and the provider's reconciliation window. Deleting a key too early allows a delayed retry to create a duplicate. Keeping it forever creates a growing store of sensitive responses. Expire the record by policy, and make the post-expiry behavior explicit. If a client retries after expiry, the server should not pretend that it can prove the operation never ran.
Do not let a user choose an unbounded key. Validate length and character rules, rate-limit key creation, and separate keys by operation type. A payment endpoint and a report-generation endpoint should not share one global table without a type dimension.
Tests that expose the boundary
Test replay with the same key and the same body. Test the same key with a different body. Test the same key after a role removal. Test two users in different tenants who happen to pick the same key. Test an object identifier that belongs to another tenant. Test a timeout after the database commit but before the response reaches the client.
Log the key hash, principal, tenant, operation hash, decision, and stored state. Do not log the raw key if it can be used as a bearer-like capability. Trace the authorization decision separately from the replay decision so an incident review can tell which control failed.
Idempotency is a contract for one operation. Authorization is a contract for who may act on which objects and under which conditions. Build both, and make the operation hash bind the identity context that made the request meaningful.
References
[1] IETF HTTPAPI Working Group, "The Idempotency-Key HTTP Header Field." https://datatracker.ietf.org/doc/html/draft-ietf-httpapi-idempotency-key-header
[2] OWASP, "API1:2023 Broken Object Level Authorization." https://api-security.owasp.org/editions/2023/en/0xa1-broken-object-level-authorization/
Top comments (0)