-
Notifications
You must be signed in to change notification settings - Fork 60
feat: add server-side token audience validation scenario (issue #78) #386
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
Closed
+1,121
−17
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
141 changes: 141 additions & 0 deletions
141
examples/servers/typescript/auth-no-audience-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,141 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| /** | ||
| * Issue #78 Negative Test Server — broken audience validation. | ||
| * | ||
| * An OAuth-protected MCP server that verifies Bearer JWTs (signature via the | ||
| * issuer's JWKS, issuer, expiry) but deliberately SKIPS audience validation, | ||
| * violating "MCP servers MUST validate that access tokens were issued | ||
| * specifically for them as the intended audience" (authorization spec, | ||
| * Token Handling). Against this server the auth-token-audience-validation | ||
| * scenario must emit FAILURE for the wrong-audience and missing-audience | ||
| * checks while the signature/expiry checks still pass. | ||
| * | ||
| * Configuration (same contract as the everything-server's opt-in auth mode): | ||
| * PORT - listen port (default 3000) | ||
| * MCP_CONFORMANCE_AUTH_ISSUER - trusted authorization server issuer URL | ||
| */ | ||
|
|
||
| import express from 'express'; | ||
| import { rateLimit } from 'express-rate-limit'; | ||
| import { createRemoteJWKSet, jwtVerify } from 'jose'; | ||
|
|
||
| const PORT = process.env.PORT || 3000; | ||
| const AUTH_ISSUER = | ||
| process.env.MCP_CONFORMANCE_AUTH_ISSUER ?? 'http://127.0.0.1:9797'; | ||
|
|
||
| const app = express(); | ||
| app.use(express.json()); | ||
|
|
||
| // Rate-limit the endpoint (it performs JWKS fetches and signature | ||
| // verification per request). Generous enough that conformance runs — a | ||
| // handful of probes per scenario — never hit it. | ||
| app.use( | ||
| '/mcp', | ||
| rateLimit({ | ||
| windowMs: 60_000, | ||
| limit: 10_000, | ||
| standardHeaders: true, | ||
| legacyHeaders: false | ||
| }) | ||
| ); | ||
|
|
||
| // Lazily-constructed remote JWKS, discovered from the issuer's RFC 8414 | ||
| // metadata at first use. | ||
| let jwks: ReturnType<typeof createRemoteJWKSet> | undefined; | ||
|
|
||
| app.use('/mcp', async (req, res, next) => { | ||
| const header = req.headers.authorization; | ||
| if (!header?.startsWith('Bearer ')) { | ||
| res.set('WWW-Authenticate', 'Bearer'); | ||
| return res.status(401).json({ | ||
| jsonrpc: '2.0', | ||
| id: null, | ||
| error: { code: -32000, message: 'Unauthorized: missing Bearer token' } | ||
| }); | ||
| } | ||
|
|
||
| try { | ||
| if (!jwks) { | ||
| const metadataRes = await fetch( | ||
| `${AUTH_ISSUER}/.well-known/oauth-authorization-server` | ||
| ); | ||
| if (!metadataRes.ok) { | ||
| throw new Error( | ||
| `authorization server metadata fetch failed: HTTP ${metadataRes.status}` | ||
| ); | ||
| } | ||
| const metadata = (await metadataRes.json()) as { jwks_uri?: string }; | ||
| if (!metadata.jwks_uri) { | ||
| throw new Error('authorization server metadata lacks jwks_uri'); | ||
| } | ||
| jwks = createRemoteJWKSet(new URL(metadata.jwks_uri)); | ||
| } | ||
| // BROKEN ON PURPOSE: no `audience` option — a token minted for any other | ||
| // resource (or with no aud claim at all) passes verification. | ||
| await jwtVerify(header.slice('Bearer '.length), jwks, { | ||
| issuer: AUTH_ISSUER | ||
| }); | ||
| return next(); | ||
| } catch (error) { | ||
| // Strip characters that would break out of the quoted-string. | ||
| const description = ( | ||
| error instanceof Error ? error.message : 'invalid' | ||
| ).replace(/["\r\n]/g, ''); | ||
| res.set( | ||
| 'WWW-Authenticate', | ||
| `Bearer error="invalid_token", error_description="${description}"` | ||
| ); | ||
| return res.status(401).json({ | ||
| jsonrpc: '2.0', | ||
| id: null, | ||
| error: { code: -32000, message: 'Unauthorized: invalid token' } | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| app.post('/mcp', (req, res) => { | ||
| const body = req.body || {}; | ||
| const id = body.id ?? null; | ||
| const method = body.method; | ||
|
|
||
| switch (method) { | ||
| case 'initialize': | ||
| return res.json({ | ||
| jsonrpc: '2.0', | ||
| id, | ||
| result: { | ||
| protocolVersion: body.params?.protocolVersion ?? '2025-11-25', | ||
| capabilities: { tools: {} }, | ||
| serverInfo: { name: 'auth-no-audience-validation', version: '1.0.0' } | ||
| } | ||
| }); | ||
| case 'server/discover': | ||
| return res.json({ | ||
| jsonrpc: '2.0', | ||
| id, | ||
| result: { | ||
| supportedVersions: ['2026-07-28'], | ||
| capabilities: { tools: {} }, | ||
| serverInfo: { name: 'auth-no-audience-validation', version: '1.0.0' } | ||
| } | ||
| }); | ||
| case 'tools/list': | ||
| return res.json({ jsonrpc: '2.0', id, result: { tools: [] } }); | ||
| default: | ||
| if (id === null) return res.status(202).end(); // notification | ||
| return res.json({ | ||
| jsonrpc: '2.0', | ||
| id, | ||
| error: { code: -32601, message: `Method not found: ${method}` } | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| app.listen(PORT, () => { | ||
| console.log( | ||
| `Auth no-audience-validation negative test server running on http://localhost:${PORT}` | ||
| ); | ||
| console.log(` - MCP endpoint: http://localhost:${PORT}/mcp`); | ||
| console.log(` - Trusted issuer: ${AUTH_ISSUER}`); | ||
| }); | ||
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.