Visitar URL original
client/dpop: round-4 nonce-surface correctness fixes · PieterKas/conformance@2030af2 · GitHub
Skip to content

Commit 2030af2

Browse files
PieterKasclaude
andcommitted
client/dpop: round-4 nonce-surface correctness fixes
Address review findings on the auth/dpop-nonce posture: - Record the token-request proof observation BEFORE the §8 nonce challenge, so a client that is challenged but does not retry is no longer mis-reported by token-request-proof as "never completed a token request" (its proof was just validated). (#2) - Record the resource-request jti/replay on ANY valid proof (before the §9 nonce gate), so fresh-proof is never asserted on zero evidence and a client that never honours the RS nonce fails ONLY rs-nonce. (modelcontextprotocol#5) - Require challengeIssued && honored for as-nonce/rs-nonce SUCCESS, closing a vacuous-pass path (a client that pre-sends the nonce is never challenged). (#3a) - Gate the AS nonce observation on the authorization_code exchange, matching recordTokenRequestProof (a refresh exchange must not satisfy §8). (#3c) - Collapse duplicate shared token-flow check IDs (token-request, pkce-*) that the §8 round-trip re-POST would otherwise emit twice. (modelcontextprotocol#4) - Example client: retry the token request only on a use_dpop_nonce error code, not any 400 carrying a DPoP-Nonce header (consistent with the RS path). (modelcontextprotocol#6) - Fix the DpopTokenRequestObservation docstring ("last write wins" → sticky-failure). (modelcontextprotocol#7) Adds an expectedSuccessSlugs option to the client test helper and tightens the two nonce negative tests to pin the now-clean behavior (token-request-proof SUCCESS for no-as-nonce; only rs-nonce fails for no-rs-nonce). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent b61b68d commit 2030af2

6 files changed

Lines changed: 117 additions & 31 deletions

File tree

‎examples/clients/typescript/helpers/dpopClientFlow.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,20 @@ export async function runDpopClient(
140140
};
141141
let tokenResponse = await requestToken();
142142
// RFC 9449 §8: the AS may answer with `use_dpop_nonce` (HTTP 400 + DPoP-Nonce);
143-
// a conformant client retries the token request with the supplied nonce.
143+
// a conformant client retries the token request with the supplied nonce. Match
144+
// on the `use_dpop_nonce` error code (not merely any 400 carrying a nonce), so
145+
// an unrelated error (e.g. invalid_grant) that an AS proactively decorates with
146+
// a DPoP-Nonce header does not burn the retry and mask the real failure —
147+
// consistent with the resource-side check below.
144148
const asNonce = tokenResponse.headers.get('DPoP-Nonce');
145149
if (tokenResponse.status === 400 && asNonce && options.handleAsNonce) {
146-
tokenResponse = await requestToken(asNonce);
150+
const challenge = await tokenResponse
151+
.clone()
152+
.json()
153+
.catch(() => ({}) as { error?: string });
154+
if (challenge?.error === 'use_dpop_nonce') {
155+
tokenResponse = await requestToken(asNonce);
156+
}
147157
}
148158
if (!tokenResponse.ok) {
149159
throw new Error(`Token request failed: HTTP ${tokenResponse.status}`);

‎src/scenarios/client/auth/dpop.ts‎

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ export class DPoPClientScenario implements Scenario {
173173

174174
getChecks(): ConformanceCheck[] {
175175
const checks: ConformanceCheck[] = [
176-
...this.checks,
176+
...this.dedupeSharedChecks(),
177177
this.tokenRequestProofCheck(),
178178
this.authSchemeCheck(),
179179
this.freshProofCheck()
@@ -187,38 +187,64 @@ export class DPoPClientScenario implements Scenario {
187187
return checks;
188188
}
189189

190+
/**
191+
* The RFC 9449 §8/§9 nonce round-trip re-POSTs /token (challenge → retry), so
192+
* the shared token-flow conformance checks (`token-request`, `pkce-*`) are
193+
* appended twice. Collapse duplicate non-INFO shared-check IDs to the last
194+
* occurrence so each is reported once; per-request INFO log entries are left
195+
* intact. Our own sep-1932-client-* checks are unique by construction.
196+
*/
197+
private dedupeSharedChecks(): ConformanceCheck[] {
198+
const lastIndex = new Map<string, number>();
199+
this.checks.forEach((c, i) => {
200+
if (c.status !== 'INFO') lastIndex.set(c.id, i);
201+
});
202+
return this.checks.filter(
203+
(c, i) => c.status === 'INFO' || lastIndex.get(c.id) === i
204+
);
205+
}
206+
190207
private asNonceCheck(): ConformanceCheck {
208+
const challenged = this.tokenReqObs.asNonceChallengeIssued;
191209
const honored = this.tokenReqObs.asNonceHonored;
210+
// SUCCESS requires that a challenge was actually issued AND then honored —
211+
// grading `honored` alone would let a client that pre-sends the nonce
212+
// (never challenged) pass vacuously.
213+
const pass = challenged && honored;
192214
return this.build(
193215
'sep-1932-client-as-nonce',
194-
honored ? 'SUCCESS' : 'FAILURE',
216+
pass ? 'SUCCESS' : 'FAILURE',
195217
{
196-
errorMessage: honored
218+
errorMessage: pass
197219
? undefined
198-
: this.tokenReqObs.asNonceChallengeIssued
220+
: challenged
199221
? 'Client did not retry the token request with the server-supplied nonce after a use_dpop_nonce challenge'
200-
: 'Client never completed a token request that could be nonce-challenged',
222+
: 'Client never presented a valid proof that was answered with a use_dpop_nonce challenge',
201223
details: {
202-
challengeIssued: this.tokenReqObs.asNonceChallengeIssued,
224+
challengeIssued: challenged,
203225
nonceHonored: honored
204226
}
205227
}
206228
);
207229
}
208230

209231
private rsNonceCheck(): ConformanceCheck {
232+
const challenged = this.obs.rsNonceChallengeIssued;
210233
const honored = this.obs.rsNonceHonored;
234+
// SUCCESS requires that a challenge was actually issued AND then honored —
235+
// see asNonceCheck; grading `honored` alone permits a vacuous pass.
236+
const pass = challenged && honored;
211237
return this.build(
212238
'sep-1932-client-rs-nonce',
213-
honored ? 'SUCCESS' : 'FAILURE',
239+
pass ? 'SUCCESS' : 'FAILURE',
214240
{
215-
errorMessage: honored
241+
errorMessage: pass
216242
? undefined
217-
: this.obs.rsNonceChallengeIssued
243+
: challenged
218244
? 'Client did not retry the MCP request with the server-supplied nonce after a use_dpop_nonce challenge'
219-
: 'Client never made an MCP request that could be nonce-challenged',
245+
: 'Client never made an MCP request with a valid proof that was answered with a use_dpop_nonce challenge',
220246
details: {
221-
challengeIssued: this.obs.rsNonceChallengeIssued,
247+
challengeIssued: challenged,
222248
nonceHonored: honored
223249
}
224250
}

‎src/scenarios/client/auth/helpers/createAuthServer.ts‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,11 @@ export interface TokenRequestError {
138138

139139
/**
140140
* Sink for the DPoP token-request observation (RFC 9449 §5). The AS writes to
141-
* it on the authorization_code exchange (last write wins; refresh grants are
142-
* ignored) so the client scenario can emit sep-1932-client-token-request-proof
143-
* unconditionally — FAILURE by default when the client never reaches the token
144-
* endpoint, matching its sibling checks.
141+
* it on the authorization_code exchange (sticky-failure: once any exchange
142+
* lacks a valid proof it stays FAILURE; refresh grants are ignored) so the
143+
* client scenario can emit sep-1932-client-token-request-proof unconditionally
144+
* — FAILURE by default when the client never reaches the token endpoint,
145+
* matching its sibling checks.
145146
*/
146147
export interface DpopTokenRequestObservation {
147148
recorded: boolean;
@@ -611,9 +612,18 @@ export function createAuthServer(
611612
});
612613
return;
613614
}
615+
// The proof itself is valid — record that now, BEFORE any nonce
616+
// challenge, so a client that is challenged but does not retry is not
617+
// mis-reported by token-request-proof as "never completed a token
618+
// request" (its proof was just verified). Sticky + gated on
619+
// authorization_code inside recordTokenRequestProof.
620+
recordTokenRequestProof(grantType, true);
621+
614622
// RFC 9449 §8: require a server-provided nonce. A proof without the
615623
// correct nonce is challenged (400 use_dpop_nonce + DPoP-Nonce); the
616-
// client is expected to retry with it.
624+
// client is expected to retry with it. The nonce observation is gated
625+
// on the authorization_code exchange, matching recordTokenRequestProof
626+
// (honoring a challenge on a refresh exchange must not satisfy §8).
617627
if (dpopRequireNonce) {
618628
let proofNonce: unknown;
619629
try {
@@ -622,17 +632,17 @@ export function createAuthServer(
622632
proofNonce = undefined;
623633
}
624634
if (proofNonce !== AS_DPOP_NONCE) {
625-
if (dpopTokenRequestObs)
635+
if (grantType === 'authorization_code' && dpopTokenRequestObs)
626636
dpopTokenRequestObs.asNonceChallengeIssued = true;
627637
res.status(400).set('DPoP-Nonce', AS_DPOP_NONCE).json({
628638
error: 'use_dpop_nonce',
629639
error_description: 'Authorization server requires a DPoP nonce'
630640
});
631641
return;
632642
}
633-
if (dpopTokenRequestObs) dpopTokenRequestObs.asNonceHonored = true;
643+
if (grantType === 'authorization_code' && dpopTokenRequestObs)
644+
dpopTokenRequestObs.asNonceHonored = true;
634645
}
635-
recordTokenRequestProof(grantType, true);
636646

637647
if (dpopMisbehavior === 'unbound-token') {
638648
// Misbehaviour: ignore the binding and issue a plain Bearer token.

‎src/scenarios/client/auth/helpers/dpopResourceAuth.ts‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,19 @@ export function createDpopResourceAuth(
108108
return;
109109
}
110110

111+
// Record proof freshness on ANY valid proof, before the nonce gate. A
112+
// nonce-challenged (but otherwise valid) proof still demonstrates freshness,
113+
// so fresh-proof is never asserted on zero `jti` evidence, and a client that
114+
// never honours the nonce fails only rs-nonce (not fresh-proof as well).
115+
if (obs.jtisSeen.includes(result.jti)) {
116+
obs.replayDetected = true;
117+
} else {
118+
obs.jtisSeen.push(result.jti);
119+
}
120+
111121
// RFC 9449 §9: require a server-provided nonce. A valid proof without the
112122
// correct nonce is challenged (401 use_dpop_nonce + DPoP-Nonce); the client
113-
// is expected to retry with it. Only nonce-honoured requests are recorded
114-
// for the freshness check.
123+
// is expected to retry with it.
115124
if (requireNonce) {
116125
let proofNonce: unknown;
117126
try {
@@ -134,12 +143,6 @@ export function createDpopResourceAuth(
134143
obs.rsNonceHonored = true;
135144
}
136145

137-
if (obs.jtisSeen.includes(result.jti)) {
138-
obs.replayDetected = true;
139-
} else {
140-
obs.jtisSeen.push(result.jti);
141-
}
142-
143146
next();
144147
};
145148
}

‎src/scenarios/client/auth/index.test.ts‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,15 +382,35 @@ describe('DPoP client negative tests (SEP-1932)', () => {
382382

383383
test('auth/dpop-nonce: client ignores the authorization-server nonce challenge', async () => {
384384
const runner = new InlineClientRunner(dpopNoAsNonceClient);
385+
// The client presents a valid proof but never retries with the nonce, so it
386+
// never obtains a token — as-nonce fails and the downstream token-dependent
387+
// checks legitimately cascade. But token-request-proof MUST stay SUCCESS:
388+
// the client DID present a valid proof at the token endpoint (regression
389+
// guard for the "never completed a token request" misattribution).
385390
await runClientAgainstScenario(runner, 'auth/dpop-nonce', {
386-
expectedFailureSlugs: ['sep-1932-client-as-nonce']
391+
expectedFailureSlugs: [
392+
'sep-1932-client-as-nonce',
393+
'sep-1932-client-dpop-auth-scheme',
394+
'sep-1932-client-fresh-proof',
395+
'sep-1932-client-rs-nonce'
396+
],
397+
expectedSuccessSlugs: ['sep-1932-client-token-request-proof']
387398
});
388399
});
389400

390401
test('auth/dpop-nonce: client ignores the MCP-server nonce challenge', async () => {
391402
const runner = new InlineClientRunner(dpopNoRsNonceClient);
403+
// Clean one-defect isolation: the client obtains a token and presents a
404+
// valid fresh proof (recorded before the nonce gate), so only rs-nonce
405+
// fails — everything else stays SUCCESS.
392406
await runClientAgainstScenario(runner, 'auth/dpop-nonce', {
393-
expectedFailureSlugs: ['sep-1932-client-rs-nonce']
407+
expectedFailureSlugs: ['sep-1932-client-rs-nonce'],
408+
expectedSuccessSlugs: [
409+
'sep-1932-client-token-request-proof',
410+
'sep-1932-client-dpop-auth-scheme',
411+
'sep-1932-client-fresh-proof',
412+
'sep-1932-client-as-nonce'
413+
]
394414
});
395415
});
396416
});

‎src/scenarios/client/auth/test_helpers/testClient.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ export class InlineClientRunner implements ClientRunner {
8787

8888
export interface RunClientOptions {
8989
expectedFailureSlugs?: string[];
90+
/**
91+
* Slugs that MUST be present and SUCCESS. Use alongside expectedFailureSlugs
92+
* to pin that a specific check is *not* collateral damage of the injected
93+
* defect (e.g. a client that skips the nonce retry still passes
94+
* token-request-proof because it did present a valid proof).
95+
*/
96+
expectedSuccessSlugs?: string[];
9097
allowClientError?: boolean;
9198
/**
9299
* Spec version to run the scenario at. Defaults to the latest dated spec
@@ -103,6 +110,7 @@ export async function runClientAgainstScenario(
103110
): Promise<void> {
104111
const {
105112
expectedFailureSlugs = [],
113+
expectedSuccessSlugs = [],
106114
allowClientError = false,
107115
specVersion
108116
} = options;
@@ -152,6 +160,15 @@ export async function runClientAgainstScenario(
152160
// Filter out INFO checks
153161
const nonInfoChecks = checks.filter((c) => c.status !== 'INFO');
154162

163+
// Slugs that must be present and SUCCESS (independent of the failure set).
164+
for (const slug of expectedSuccessSlugs) {
165+
const check = checks.find((c) => c.id === slug);
166+
if (!check) {
167+
throw new Error(`Expected-success check ${slug} not found`);
168+
}
169+
expect(check.status).toBe('SUCCESS');
170+
}
171+
155172
// Check for expected failures
156173
if (expectedFailureSlugs.length > 0) {
157174
// Verify that the expected failures are present

0 commit comments

Comments
 (0)