Nonces and replay handling
A copied DPoP-bound tokenA piece of data issued by a trusted service that a client presents as a credential, for example to call an API or to report a sign-in. Some tokens carry readable claims; others are random strings that only the issuer can interpret. is useless on its own, but a copied request, token and proof together, is a correctly signed request that could be sent again. This lesson follows a captured request for Ava's album through the limits DPoPAn OAuth extension, RFC 9449, that binds tokens to a key pair the client holds. The client signs a fresh proof for each request, so a stolen token is useless without the private key. puts on it: a time window, remembered proof identifiers and values the server chooses, then names what DPoP cannot protect, especially in a browser.
What a captured proof allows
Suppose the photo APIAn application programming interface: a defined way for software to interact with a service. A web API offers endpoints where other programs send HTTP requests. writes complete request headers to a debugging log, and someone with access to that log copies the printer phone app's latest request for Ava's photos: the access tokenA token that a client presents to an API to use access it has been granted, usually limited in what it allows and how long it lasts. It authorizes API calls; it does not tell the client who signed in. and the DPoP proof sent beside it. The token alone fails, as Validating proofs at the server showed. The pair is a different matter, and replaying it needs no key at all. All domains, tokens, and nonces in these examples are fictional.
The copied proof names GET, https://api.photos.example/albums/42/photos and, through ath, one specific token. The copier cannot use it for another address, another method or another token, because the photo API compares all three. What they can do is repeat that exact request. For reading an album, that leaks Ava's photos again. For an API that deletes photos or starts payments, a repeated request could do real harm.
Servers have two tools against it. The first is time. The specification requires a server to accept a proof only for a limited time after it was created, preferably seconds or minutes. The photo API in this example accepts proofs whose iat is no more than 60 seconds old. Clocks are never perfectly aligned, so it also tolerates an iat up to five seconds in the future. Both numbers are this example's choices, not values from the specification.
The second tool is memory. Every proof carries a unique jti, so the API can record each identifier it accepts for an address and refuse any proof whose identifier it has already seen. It only needs to remember an identifier for as long as the proof could pass the time check, measured against the longest proof age the server might ever accept, so a later change to that age cannot revive a used proof. After that the time check rejects it anyway.
Strict tracking is strong and not always easy. If the photo API runs on many servers with no shared storage, a replay sent to a different server is not recognized, so the API needs a store every server shares, or it accepts that the time window is its only defense. A server that tracks identifiers should also reject unreasonably long ones, or store only a hashTurning data into a short, fixed-size value called a hash, in a way that cannot be reversed and where changing one character changes the result completely. Services store password hashes instead of passwords, and signatures are made over a hash. of each, so that a flood of huge values cannot exhaust its memory.
Tracking changes one habit on the client side. A retry, even of a request that would normally be safe to repeat, needs a fresh proof with a new jti and iat. Resending the old proof looks exactly like a replay and is refused.
Proofs made in advance
The time check has a weakness: iat is chosen by whoever signs the proof. Code running inside the app, or a person who controls the phone, could sign a batch of proofs with iat values set hours ahead, copy them elsewhere along with the token, and use them later without the key. At that point the server is checking possession of some proofs, not possession of the key.
The ath claim already limits this. A proof can only name a token that exists, so proofs made in advance stop being useful when that access token expires. With the photo service's ten-minute access tokens, a stack of pre-made proofs is worth ten minutes at most, which is one reason the specification says deployments that do not use nonces should not issue long-lived DPoP-bound access tokens. Nor can proofs be made for an access token that has not been issued yet: someone holding a stolen refresh tokenA longer-lived credential that a client sends to the authorization server, never to an API, to get new access tokens without asking the person again. and pre-made proofs might obtain a new access token, but could not use it at the API.
A server that wants to rule this out completely supplies a value the client cannot predict.
Server-provided nonces
A nonce here is an unpredictable value chosen by the server, which the client must copy into the nonce claim of its next proofs. No one can sign a proof containing it before the server reveals it.
At the token endpointThe authorization server address where clients exchange an authorization code, refresh token, or other grant for tokens, in a direct request rather than through a browser redirect., a request whose proof lacks the current nonce is refused with the error use_dpop_nonce and a DPoP-Nonce header carrying the value to use:
HTTP/1.1 400 Bad Request
Content-Type: application/json
Cache-Control: no-store
DPoP-Nonce: demo-auth-nonce-81
{
"error": "use_dpop_nonce"
}The app retries once, with a fresh proof whose claims now include "nonce": "demo-auth-nonce-81", and keeps using that value in proofs for this server until the server supplies another. A server can send the next nonce in the DPoP-Nonce header of a successful response, which spares the client a refused request each time the nonce changes.
The photo API can do the same. Because it answers in HTTPThe Hypertext Transfer Protocol, the rules browsers, apps, and servers use to send requests and responses on the web. authenticationChecking evidence that someone, or some application, controls an account, such as a password or a passkey. It establishes which account is being used, not what that account may do. challenges, its version is a 401:
HTTP/1.1 401 Unauthorized
WWW-Authenticate: DPoP error="use_dpop_nonce", algs="ES256"
DPoP-Nonce: demo-api-nonce-12Captured request sent again
- jti
- Seen before
- iat
- Recent
- nonce
- Not required
Refused as a replay
Proof signed in advance
- jti
- New
- iat
- Set ahead
- nonce
- Not required
Accepted until the token expires
The same proof, nonce required
- jti
- New
- iat
- Set ahead
- nonce
- Not yet revealed
Refused: use_dpop_nonce
While its code runs it can make requests the servers will accept.
A few rules keep nonces meaningful:
- A nonce belongs to the server that issued it. The app keeps one for the authorization serverIn OAuth, the service that handles a person's sign-in and approval and decides which tokens to issue to a client. It may belong to the same company as the API, but its job is separate. and another for the photo API, and never swaps them. One deployment may run both from a shared nonce source, so a value from one would also work at the other, but the app cannot know that and keeps them apart anyway.
- Once a server has given a client a nonce, it must refuse that client's proofs without one. Otherwise a copier would simply leave the claim out.
- A nonce may be reused across several proofs until the server replaces it. Each proof still needs its own
jti. - A server can build its nonce from its own clock, so that judging freshness no longer depends on the client's clock at all.
A DPoP nonce has nothing to do with the nonce parameter OpenID ConnectAn identity layer on top of OAuth 2.0 that lets an application sign people in with an account at another service. It adds the ID token, standard claims, and discovery to the OAuth exchange. sends in authorizationDeciding whether a requested action is permitted, such as whether a signed-in account may delete a particular photo. Authentication says which account is asking; authorization decides what it can do. requests, described in State and nonce, despite the shared name. That one comes from the client and ends up in an ID token; this one comes from the server and ends up in a proof.
What DPoP does not protect
Token theft and replay noted that binding protects against copies, not against an attacker who controls the client, and DPoP is no exception. Every protection in this group assumes the private key stays under the app's control. If an attacker can run code inside the app, through a compromised phone, a malicious library or injected script in a browser application, that code can ask for signatures exactly as the app does. A non-exportable key stops it carrying the key away, and nonces stop it stockpiling proofs, but while it runs it can make requests the servers will accept.
A proof also covers only the method, the address, the token and the time. The request body and the other headers rely on TLS to stay intact in transit, so an API where the body carries the important decision, such as the amount of a payment, may need the client to sign the whole message as well.
DPoP in a browser application
The first of those limits, code running inside the app, matters most in a browser, where injected script is the usual way in. The photo editor from Browser applications runs entirely in Ava's browser and holds its own tokens, and it can use DPoP much as the phone app does. A browser has no Keychain, but the Web Cryptography API built into current browsers can generate a P-256 key pair and mark the private key non-extractable:
const keys = await crypto.subtle.generateKey(
{ name: 'ECDSA', namedCurve: 'P-256' },
false, // the private key can never be exported
['sign', 'verify'],
);The page receives key objects it can sign with, but no script can read the private key's value out of them. To keep the key across reloads, the editor stores those key objects in IndexedDB, the browser's built-in database, which keeps a non-extractable key non-extractable. The browser can still delete it, for example when Ava clears the site's data, and a lost key makes the bound refresh token useless, so the editor has to send her through sign-in again.
Now suppose the caption bug from Browser applications lets an attacker's script run in the editor's page. The script runs with the page's authority. It can read the access token, load the key from IndexedDB and ask for signatures exactly as the editor does, so while the page is open its requests carry valid proofs and the photo API accepts them. What it cannot do is carry the private key away. A token it copies is useless on the attacker's own machine, and so is the refresh token, which is bound to the same key because the editor is a public client. Nonces matter here: without them, the script could sign a batch of proofs for the current token in advance and send them off with it.
That is a real improvement on bearer tokens in the page, and still less than a backend for frontend offers. There, no token reaches the browser at all, the backend is a confidential client, and injected script has nothing to copy, although, as What injected script can still do explained, it can still send requests through the backend while it runs. DPoP in the browser reduces what a stolen token is worth; a backend for frontend keeps the tokens out of the place where the theft happens, and it can still bind its own tokens.
Check your understanding
Try these questions before moving on. If an answer isn't right, use the feedback and try again.
Try it in the Lab
- Add freshness, jti tracking and server nonces to DPoP proofsReplay a complete DPoP request and see it refused, let a proof made in advance expire, then require server nonces and handle use_dpop_nonce as a client by retrying once with a fresh proof.
Try a Capture the Flag challenge
Apply this lesson against an isolated, intentionally vulnerable application. Capture a flag, explain the mistake, then repair and retest it.