Skip to content

Commit a735b98

Browse files
bhosmer-antclaude
andcommitted
Add comprehensive token validation to ExternalAuthVerifier
Enhance token validation in separate mode to fully comply with MCP specification: - Validate audience (aud) claim to ensure tokens are issued for this specific MCP server - Validate temporal claims (nbf, iat) with appropriate clock skew tolerance - Add configurable canonical URI for audience validation - Improve logging for validation failures These changes prevent token passthrough attacks and ensure tokens are properly scoped to the intended resource server, as required by the MCP OAuth 2.0 specification. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent dbd3e9a commit a735b98

File tree

1 file changed

+40
-4
lines changed

1 file changed

+40
-4
lines changed

src/auth/external-verifier.ts

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { AuthInfo } from '@modelcontextprotocol/sdk/server/auth/types.js';
33
import { InvalidTokenError } from '@modelcontextprotocol/sdk/server/auth/errors.js';
44
import { TokenIntrospectionResponse } from '../../shared/types.js';
55
import { logger } from '../utils/logger.js';
6+
import { BASE_URI } from '../config.js';
67

78
/**
89
* Token verifier that validates tokens with an external authorization server.
@@ -11,15 +12,20 @@ import { logger } from '../utils/logger.js';
1112
export class ExternalAuthVerifier implements OAuthTokenVerifier {
1213
// Token validation cache: token -> { authInfo, expiresAt }
1314
private tokenCache = new Map<string, { authInfo: AuthInfo; expiresAt: number }>();
14-
15+
1516
// Default cache TTL: 60 seconds (conservative for security)
1617
private readonly defaultCacheTTL = 60 * 1000; // milliseconds
17-
18+
19+
// The canonical URI of this MCP server for audience validation
20+
private readonly canonicalUri: string;
21+
1822
/**
1923
* Creates a new external auth verifier.
2024
* @param authServerUrl Base URL of the external authorization server
25+
* @param canonicalUri Optional canonical URI for audience validation (defaults to BASE_URI)
2126
*/
22-
constructor(private authServerUrl: string) {
27+
constructor(private authServerUrl: string, canonicalUri?: string) {
28+
this.canonicalUri = canonicalUri || BASE_URI;
2329
// Periodically clean up expired cache entries
2430
setInterval(() => this.cleanupCache(), 60 * 1000); // Every minute
2531
}
@@ -85,7 +91,37 @@ export class ExternalAuthVerifier implements OAuthTokenVerifier {
8591
if (data.exp && data.exp < Date.now() / 1000) {
8692
throw new InvalidTokenError('Token has expired');
8793
}
88-
94+
95+
// Validate audience (aud) claim to ensure token is for this MCP server
96+
// According to MCP spec, servers MUST validate that tokens were issued specifically for them
97+
if (data.aud) {
98+
const audiences = Array.isArray(data.aud) ? data.aud : [data.aud];
99+
if (!audiences.includes(this.canonicalUri)) {
100+
logger.error('Token audience mismatch', undefined, {
101+
expectedAudience: this.canonicalUri,
102+
actualAudience: data.aud,
103+
});
104+
throw new InvalidTokenError('Token was not issued for this resource server');
105+
}
106+
} else {
107+
// Log warning if no audience claim present (permissive for backwards compatibility)
108+
logger.info('Token introspection response missing audience claim', {
109+
warning: true,
110+
tokenSub: data.sub,
111+
clientId: data.client_id,
112+
});
113+
}
114+
115+
// Validate token is not used before its 'not before' time (nbf) if present
116+
if (data.nbf && data.nbf > Date.now() / 1000) {
117+
throw new InvalidTokenError('Token is not yet valid (nbf)');
118+
}
119+
120+
// Validate token was issued in the past (iat) if present
121+
if (data.iat && data.iat > Date.now() / 1000 + 60) { // Allow 60s clock skew
122+
throw new InvalidTokenError('Token issued in the future (iat)');
123+
}
124+
89125
// Extract user ID from standard 'sub' claim or custom 'userId' field
90126
const userId = data.sub || data.userId;
91127
if (!userId) {

0 commit comments

Comments
 (0)