-
Notifications
You must be signed in to change notification settings - Fork 46
feat: add conformance tests for iss parameter (SEP-2468) #220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
pcarleton
merged 10 commits into
modelcontextprotocol:main
from
max-stytch:feat/iss-parameter-conformance
May 19, 2026
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b7b94c8
feat: add conformance tests for iss parameter (SEP-2468)
max-stytch eaa3f2e
update scenarios
max-stytch d355fa5
fix: createAuthServer iss option type/guard and NotAdvertised scenari…
pcarleton 85fd5a2
fix: rejection scenarios silently pass when client never reaches auth…
pcarleton 3633026
fix: iss-unexpected scenario contradicts SEP-2468 spec table row 3
pcarleton 4378aef
refactor: replace harness-config checks with client-proceeded checks
pcarleton 003987d
refactor: rename check IDs to sep-2468-* and align with spec table rows
pcarleton 955748d
feat: add sep-2468.yaml requirement traceability
pcarleton 2ed8792
fix: migrate iss scenarios specVersions->source (post-#265)
pcarleton 28a83ae
fix: include application_type in iss-validation example DCR (post-#284)
pcarleton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
230 changes: 230 additions & 0 deletions
230
examples/clients/typescript/auth-test-iss-validation.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,230 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| /** | ||
| * Well-behaved client that validates the iss parameter in authorization responses. | ||
| * | ||
| * Per RFC 9207: | ||
| * - If the AS advertises authorization_response_iss_parameter_supported: true, | ||
| * the client MUST require iss in the redirect and MUST validate it against | ||
| * the AS metadata issuer. | ||
| * - If the AS does NOT advertise support, the client MUST reject any redirect | ||
| * that unexpectedly contains an iss parameter. | ||
| */ | ||
|
|
||
| import { createHash, randomBytes } from 'crypto'; | ||
| import { Client } from '@modelcontextprotocol/sdk/client/index.js'; | ||
| import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'; | ||
| import { extractWWWAuthenticateParams } from '@modelcontextprotocol/sdk/client/auth.js'; | ||
| import type { FetchLike } from '@modelcontextprotocol/sdk/shared/transport.js'; | ||
| import type { Middleware } from '@modelcontextprotocol/sdk/client/middleware.js'; | ||
| import { runAsCli } from './helpers/cliRunner'; | ||
| import { logger } from './helpers/logger'; | ||
|
|
||
| interface OAuthTokens { | ||
| access_token: string; | ||
| token_type: string; | ||
| expires_in?: number; | ||
| refresh_token?: string; | ||
| scope?: string; | ||
| } | ||
|
|
||
| function generateCodeVerifier(): string { | ||
| return randomBytes(32) | ||
| .toString('base64') | ||
| .replace(/\+/g, '-') | ||
| .replace(/\//g, '_') | ||
| .replace(/=+$/, ''); | ||
| } | ||
|
|
||
| function computeS256Challenge(codeVerifier: string): string { | ||
| const hash = createHash('sha256').update(codeVerifier).digest(); | ||
| return hash | ||
| .toString('base64') | ||
| .replace(/\+/g, '-') | ||
| .replace(/\//g, '_') | ||
| .replace(/=+$/, ''); | ||
| } | ||
|
|
||
| /** | ||
| * OAuth flow that correctly validates the iss parameter per RFC 9207. | ||
| */ | ||
| async function oauthFlowWithIssValidation( | ||
| _serverUrl: string | URL, | ||
| resourceMetadataUrl: string | URL, | ||
| fetchFn: FetchLike | ||
| ): Promise<OAuthTokens> { | ||
| // 1. Fetch Protected Resource Metadata | ||
| const prmResponse = await fetchFn(resourceMetadataUrl); | ||
| if (!prmResponse.ok) { | ||
| throw new Error(`Failed to fetch PRM: ${prmResponse.status}`); | ||
| } | ||
| const prm = await prmResponse.json(); | ||
| const authServerUrl = prm.authorization_servers?.[0]; | ||
| if (!authServerUrl) { | ||
| throw new Error('No authorization server in PRM'); | ||
| } | ||
|
|
||
| // 2. Fetch Authorization Server Metadata | ||
| const asMetadataUrl = new URL( | ||
| '/.well-known/oauth-authorization-server', | ||
| authServerUrl | ||
| ); | ||
| const asResponse = await fetchFn(asMetadataUrl.toString()); | ||
| if (!asResponse.ok) { | ||
| throw new Error(`Failed to fetch AS metadata: ${asResponse.status}`); | ||
| } | ||
| const asMetadata = await asResponse.json(); | ||
|
|
||
| const expectedIssuer: string = asMetadata.issuer; | ||
| const issParameterSupported: boolean = | ||
| asMetadata.authorization_response_iss_parameter_supported === true; | ||
|
|
||
| // 3. Register client (DCR) | ||
| const dcrResponse = await fetchFn(asMetadata.registration_endpoint, { | ||
| method: 'POST', | ||
| headers: { 'Content-Type': 'application/json' }, | ||
| body: JSON.stringify({ | ||
| client_name: 'test-auth-client-iss-validation', | ||
| redirect_uris: ['http://localhost:3000/callback'], | ||
| application_type: 'native' | ||
| }) | ||
| }); | ||
| if (!dcrResponse.ok) { | ||
| throw new Error(`DCR failed: ${dcrResponse.status}`); | ||
| } | ||
| const clientInfo = await dcrResponse.json(); | ||
|
|
||
| // 4. Build authorization URL with PKCE | ||
| const codeVerifier = generateCodeVerifier(); | ||
| const codeChallenge = computeS256Challenge(codeVerifier); | ||
|
|
||
| const authUrl = new URL(asMetadata.authorization_endpoint); | ||
| authUrl.searchParams.set('response_type', 'code'); | ||
| authUrl.searchParams.set('client_id', clientInfo.client_id); | ||
| authUrl.searchParams.set('redirect_uri', 'http://localhost:3000/callback'); | ||
| authUrl.searchParams.set('state', 'test-state'); | ||
| authUrl.searchParams.set('code_challenge', codeChallenge); | ||
| authUrl.searchParams.set('code_challenge_method', 'S256'); | ||
|
|
||
| // 5. Fetch authorization endpoint (simulates redirect) | ||
| const authResponse = await fetchFn(authUrl.toString(), { | ||
| redirect: 'manual' | ||
| }); | ||
| const location = authResponse.headers.get('location'); | ||
| if (!location) { | ||
| throw new Error('No redirect from authorization endpoint'); | ||
| } | ||
| const redirectUrl = new URL(location); | ||
| const authCode = redirectUrl.searchParams.get('code'); | ||
| if (!authCode) { | ||
| throw new Error('No auth code in redirect'); | ||
| } | ||
|
|
||
| // 6. Validate iss parameter per RFC 9207 | ||
| const issInRedirect = redirectUrl.searchParams.get('iss'); | ||
|
|
||
| if (issParameterSupported) { | ||
| // Server advertised support: iss MUST be present and MUST match metadata issuer | ||
| if (!issInRedirect) { | ||
| throw new Error( | ||
| 'Server advertised authorization_response_iss_parameter_supported but iss is absent from redirect' | ||
| ); | ||
| } | ||
| if (issInRedirect !== expectedIssuer) { | ||
| throw new Error( | ||
| `iss mismatch: expected '${expectedIssuer}', got '${issInRedirect}'` | ||
| ); | ||
| } | ||
| } else { | ||
| // Server did NOT advertise support: if iss is present, compare anyway | ||
| // (SEP-2468 spec table row 3 — local-policy provision per RFC 9207 §2.4) | ||
| if (issInRedirect && issInRedirect !== expectedIssuer) { | ||
| throw new Error( | ||
| `iss mismatch: expected '${expectedIssuer}', got '${issInRedirect}'` | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| // 7. Exchange code for token with PKCE code_verifier | ||
| const tokenResponse = await fetchFn(asMetadata.token_endpoint, { | ||
| method: 'POST', | ||
| headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, | ||
| body: new URLSearchParams({ | ||
| grant_type: 'authorization_code', | ||
| code: authCode, | ||
| redirect_uri: 'http://localhost:3000/callback', | ||
| client_id: clientInfo.client_id, | ||
| code_verifier: codeVerifier | ||
| }).toString() | ||
| }); | ||
|
|
||
| if (!tokenResponse.ok) { | ||
| const error = await tokenResponse.text(); | ||
| throw new Error(`Token request failed: ${tokenResponse.status} - ${error}`); | ||
| } | ||
|
|
||
| return tokenResponse.json(); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a fetch wrapper that uses OAuth with iss parameter validation. | ||
| */ | ||
| function withOAuthIssValidation(baseUrl: string | URL): Middleware { | ||
| let tokens: OAuthTokens | undefined; | ||
|
|
||
| return (next: FetchLike) => { | ||
| return async ( | ||
| input: string | URL, | ||
| init?: RequestInit | ||
| ): Promise<Response> => { | ||
| const makeRequest = async (): Promise<Response> => { | ||
| const headers = new Headers(init?.headers); | ||
| if (tokens) { | ||
| headers.set('Authorization', `Bearer ${tokens.access_token}`); | ||
| } | ||
| return next(input, { ...init, headers }); | ||
| }; | ||
|
|
||
| let response = await makeRequest(); | ||
|
|
||
| if (response.status === 401) { | ||
| const { resourceMetadataUrl } = extractWWWAuthenticateParams(response); | ||
| if (!resourceMetadataUrl) { | ||
| throw new Error('No resource_metadata in WWW-Authenticate'); | ||
| } | ||
| tokens = await oauthFlowWithIssValidation( | ||
| baseUrl, | ||
| resourceMetadataUrl, | ||
| next | ||
| ); | ||
| response = await makeRequest(); | ||
| } | ||
|
|
||
| return response; | ||
| }; | ||
| }; | ||
| } | ||
|
|
||
| export async function runClient(serverUrl: string): Promise<void> { | ||
| const client = new Client( | ||
| { name: 'test-auth-client-iss-validation', version: '1.0.0' }, | ||
| { capabilities: {} } | ||
| ); | ||
|
|
||
| const oauthFetch = withOAuthIssValidation(new URL(serverUrl))(fetch); | ||
|
|
||
| const transport = new StreamableHTTPClientTransport(new URL(serverUrl), { | ||
| fetch: oauthFetch | ||
| }); | ||
|
|
||
| await client.connect(transport); | ||
| logger.debug('Successfully connected to MCP server'); | ||
|
|
||
| await client.listTools(); | ||
| logger.debug('Successfully listed tools'); | ||
|
|
||
| await transport.close(); | ||
| logger.debug('Connection closed successfully'); | ||
| } | ||
|
|
||
| runAsCli(runClient, import.meta.url, 'auth-test-iss-validation <server-url>'); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would have loved to add this behavior to the base
ConformanceOAuthProvider, but it looks like that provider doesn't have access to the AS Metadata currently, so it is not possible for us to do the checks there.runCrossAppAccessCompleteFlowsimilarly needs to fetch the metadata itself.Defer to maintainers on how best to incorporate this code.