Skip to content

Add nonce validation #770

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Glitched
Copy link

Add nonce validation for OpenID Connect flows to prevent replay attacks and improve OIDC compliance.

Motivation and Context

The SDK was generating and passing nonce parameters for OpenID Connect flows but wasn't validating them in ID tokens. This left the implementation vulnerable to replay attacks where an attacker could reuse intercepted ID tokens. This change completes the OIDC security model by validating that the nonce in the ID token matches what the client generated.

Additionally adds audience validation to ensure ID tokens are intended for our client.

How Has This Been Tested?

Added unit tests

Breaking Changes

TypeScript users only: The return type of startAuthorization() changed from { authorizationUrl: URL; codeVerifier: string } to { authorizationUrl: URL; codeVerifier: string; nonce?: string }. JavaScript users are unaffected.

OAuth provider implementations should add the optional saveNonce and nonce methods to support OpenID Connect flows, but these are optional and backward compatible.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

Glitched and others added 3 commits July 14, 2025 13:24
- Add optional nonce parameter to client startAuthorization()
- Auto-generate nonce when scope includes 'openid'
- Pass nonce through server authorization handler
- Update AuthorizationParams type to include nonce
- Add comprehensive tests for nonce handling

This enables proper OpenID Connect security by preventing replay attacks
on ID tokens.
- Add afterEach to restore all mocks
- Remove unnecessary mockClear() calls
- Ensures tests are properly isolated
Implements proper nonce validation to prevent replay attacks when using
OpenID Connect (scope includes 'openid'). Also adds audience validation
for additional security.

- Automatically generates nonce for OIDC flows
- Validates nonce in ID tokens during token exchange
- Validates audience (aud) claim matches client_id
- Adds optional saveNonce/nonce methods to provider interface
- Uses inline JWT decoder for better compatibility
- Includes comprehensive test coverage

Note: startAuthorization() now returns an optional nonce field when
scope includes 'openid'. This is backward compatible for JavaScript
users but may require TypeScript users to update explicit type annotations.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@ihrpr ihrpr added this to the auth milestone Jul 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants