Skip to content

Commit 5b9483e

Browse files
fix(server): the token route checks DPoP proofs without recording them
Recording after the credential is consumed could spend a one-time code without issuing a token when the store write failed. A replay marker at the token endpoint protects nothing: whoever replays a proof also holds the subject token and could sign a fresh proof with their own key, and a spent one-time code already refuses a second exchange. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent c17c4ee commit 5b9483e

4 files changed

Lines changed: 15 additions & 52 deletions

File tree

‎apps/server/src/auth/EnvironmentAuth.ts‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -477,11 +477,6 @@ export class EnvironmentAuth extends Context.Service<
477477
requestMetadata: AuthClientMetadata,
478478
input?: {
479479
readonly proofKeyThumbprint?: string;
480-
/** Records the DPoP proof, run only after the credential is accepted. */
481-
readonly consumeProof?: Effect.Effect<
482-
void,
483-
ServerAuthInvalidCredentialError | ServerAuthInternalError
484-
>;
485480
},
486481
) => Effect.Effect<
487482
AuthAccessTokenResult,
@@ -968,12 +963,11 @@ export const make = Effect.gen(function* () {
968963
const exchangeBootstrapCredentialForAccessToken: EnvironmentAuth["Service"]["exchangeBootstrapCredentialForAccessToken"] =
969964
(credential, requestedScopes, requestMetadata, input) => {
970965
return resolveBootstrapGrant(credential, {
971-
...(input?.proofKeyThumbprint ? { proofKeyThumbprint: input.proofKeyThumbprint } : {}),
966+
...input,
972967
...(requestedScopes !== undefined ? { requestedScopes } : {}),
973968
}).pipe(
974969
Effect.flatMap((grant) =>
975970
Effect.gen(function* () {
976-
if (input?.consumeProof) yield* input.consumeProof;
977971
const grantedScopes =
978972
requestedScopes === undefined
979973
? grant.scopes

‎apps/server/src/auth/dpop.ts‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,7 @@ export const mapDpopReplayStoreError = (
5353
cause: error,
5454
});
5555

56-
/**
57-
* Checks a proof without recording it. `consume` records the proof's jti so it cannot be
58-
* replayed; run it only once the request is otherwise known to be valid, so that requests
59-
* anyone can send do not write replay state.
60-
*/
56+
/** Checks a proof without recording it; `consume` records its jti so it cannot be replayed. */
6157
export const checkRequestDpopProof = Effect.fn("dpop.checkRequestDpopProof")(function* (input: {
6258
readonly request: HttpServerRequest.HttpServerRequest;
6359
/** Overrides the `DPoP` header for requests that cannot carry one, such as a WebSocket upgrade. */

‎apps/server/src/auth/http.test.ts‎

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import * as DateTime from "effect/DateTime";
1212
import * as SecureRandom from "effect/crypto/SecureRandom";
1313
import * as Effect from "effect/Effect";
1414
import * as Layer from "effect/Layer";
15-
import * as PlatformError from "effect/PlatformError";
1615
import * as Redacted from "effect/Redacted";
1716
import * as Schema from "effect/Schema";
1817
import * as Etag from "effect/http/Etag";
@@ -196,27 +195,14 @@ it.effect.each(["DPoP", "dpop"])("challenges a refused %s request with DPoP", (s
196195
);
197196

198197
// The web handler runs on its own runtime, so proofs need wall-clock time.
199-
it.live("records a token-endpoint DPoP proof only once the subject token is accepted", () =>
198+
it.live("checks token-endpoint DPoP proofs without writing replay state", () =>
200199
Effect.gen(function* () {
201200
const random = yield* SecureRandom.SecureRandom;
202-
const written = new Set<string>();
201+
const written: Array<string> = [];
203202
const recordingSecretStore = ServerSecretStore.ServerSecretStore.of({
204203
get: () => Effect.succeedNone,
205204
set: () => Effect.void,
206-
create: (name) =>
207-
written.has(name)
208-
? Effect.fail(
209-
new ServerSecretStore.SecretStorePersistError({
210-
resource: name,
211-
cause: PlatformError.systemError({
212-
_tag: "AlreadyExists",
213-
module: "FileSystem",
214-
method: "open",
215-
pathOrDescriptor: name,
216-
}),
217-
}),
218-
)
219-
: Effect.sync(() => void written.add(name)),
205+
create: (name) => Effect.sync(() => void written.push(name)),
220206
getOrCreateRandom: () => Effect.die("Not used by these routes."),
221207
remove: () => Effect.void,
222208
});
@@ -247,23 +233,21 @@ it.live("records a token-endpoint DPoP proof only once the subject token is acce
247233
);
248234
expect(unknownToken.status).toBe(401);
249235
expect(await unknownToken.json()).not.toHaveProperty("dpopFailureReason");
250-
expect(written.size).toBe(0);
251236

252-
const proof = key.sign({ method: "POST", url, token: "", iat });
253237
const accepted = await environment.handler(
254-
tokenRequest(DEV_TOKEN, proof),
238+
tokenRequest(DEV_TOKEN, key.sign({ method: "POST", url, token: "", iat })),
255239
requestContext,
256240
);
257241
expect(accepted.status).toBe(200);
258-
expect(written.size).toBe(1);
242+
expect(await accepted.json()).toMatchObject({ token_type: "DPoP" });
259243

260-
const replayed = await environment.handler(
261-
tokenRequest(DEV_TOKEN, proof),
244+
const badProof = await environment.handler(
245+
tokenRequest(DEV_TOKEN, "not-a-proof"),
262246
requestContext,
263247
);
264-
expect(replayed.status).toBe(401);
265-
expect(replayed.headers.get("www-authenticate")).toBe("DPoP");
266-
expect(await replayed.json()).toMatchObject({ dpopFailureReason: "replay" });
248+
expect(badProof.status).toBe(401);
249+
expect(badProof.headers.get("www-authenticate")).toBe("DPoP");
250+
expect(written).toEqual([]);
267251
}),
268252
(environment) => Effect.promise(() => environment.dispose()),
269253
);

‎apps/server/src/auth/http.ts‎

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,8 @@ export const layer = HttpApiBuilder.group(
364364
if (requestedScopes === null || requestedScopes?.length === 0) {
365365
return yield* failEnvironmentInvalidRequest("invalid_scope");
366366
}
367-
// Anyone can send a proof here, so it is recorded only once the subject token checks out.
367+
// Not recorded: a replayer also holds the subject token and could sign its own proof.
368+
// Recording would let anyone write replay state to disk.
368369
const proof = args.headers.dpop
369370
? yield* checkRequestDpopProof({ request }).pipe(
370371
Effect.catchIf(EnvironmentAuth.isServerAuthCredentialError, (error) =>
@@ -396,19 +397,7 @@ export const layer = HttpApiBuilder.group(
396397
...(args.payload.client_os ? { os: args.payload.client_os } : {}),
397398
},
398399
}),
399-
proof
400-
? {
401-
proofKeyThumbprint: proof.thumbprint,
402-
consumeProof: proof.consume.pipe(
403-
Effect.tapError((error) =>
404-
EnvironmentAuth.isServerAuthCredentialError(error)
405-
? appendDpopChallengeHeader
406-
: Effect.void,
407-
),
408-
Effect.provideService(HttpServerRequest.HttpServerRequest, request),
409-
),
410-
}
411-
: undefined,
400+
proof ? { proofKeyThumbprint: proof.thumbprint } : undefined,
412401
);
413402
},
414403
traceRelayRequest,

0 commit comments

Comments
 (0)