Skip to content

fix: implement RFC 6750 Section 3.1 compliance for missing authentication #785

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 1 commit into
base: main
Choose a base branch
from

Conversation

runeb
Copy link

@runeb runeb commented Jul 18, 2025

The MCP SDK's requireBearerAuth middleware was incorrectly treating missing Authorization headers as invalid tokens, violating RFC 6750 Section 3.1 compliance.

Motivation and Context

The current implementation violates RFC 6750 Section 3.1, which states:

"If the request lacks any authentication information, the resource server SHOULD NOT include an error code or other error information."

Current behavior (non-compliant):

WWW-Authenticate: Bearer error="invalid_token", error_description="Missing Authorization header"

RFC 6750 compliant behavior:

WWW-Authenticate: Bearer realm="protected"

This fix improves interoperability with OAuth 2.0 clients that expect standards-compliant bearer token authentication.

How Has This Been Tested?

  • ✅ All existing tests pass
  • ✅ New test cases verify RFC 6750 compliant behavior for missing authentication
  • ✅ Existing invalid/malformed token handling preserved
  • ✅ Resource metadata URL inclusion works correctly
  • ✅ Built and linted successfully

Test scenarios covered:

  • Missing Authorization header → returns Bearer realm="protected"
  • Invalid tokens → still return Bearer error="invalid_token"
  • Insufficient scope → still return Bearer error="insufficient_scope"
  • Resource metadata URL inclusion in all scenarios

Breaking Changes

None. This is a compliance fix that only changes the WWW-Authenticate header format for missing authentication. All other authentication error scenarios remain unchanged.

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

Implementation approach:

  1. Created new MissingAuthenticationError class following existing error patterns
  2. Updated requireBearerAuth middleware to throw MissingAuthenticationError for missing headers
  3. Added specific error handling for missing authentication that returns RFC-compliant headers
  4. Preserved all existing behavior for invalid/malformed tokens

Standards reference:

Files changed:

  • src/server/auth/errors.ts - Added MissingAuthenticationError class
  • src/server/auth/middleware/bearerAuth.ts - Updated middleware logic
  • src/server/auth/middleware/bearerAuth.test.ts - Updated test expectations

…tion

The requireBearerAuth middleware was incorrectly treating missing Authorization
headers as invalid tokens, violating RFC 6750 Section 3.1 which states:

"If the request lacks any authentication information, the resource server
SHOULD NOT include an error code or other error information."

Changes:
- Add MissingAuthenticationError class for missing authentication cases
- Update bearerAuth middleware to throw MissingAuthenticationError when no Authorization header is present
- Handle MissingAuthenticationError by returning WWW-Authenticate header with only realm parameter (no error codes)
- Update tests to expect RFC 6750 compliant behavior for missing authentication
- Preserve existing behavior for invalid/malformed tokens

Before (non-compliant):
WWW-Authenticate: Bearer error="invalid_token", error_description="Missing Authorization header"

After (RFC 6750 compliant):
WWW-Authenticate: Bearer realm="protected"

Fixes RFC 6750 compliance issue while maintaining backward compatibility for all other authentication error scenarios.
@runeb runeb requested a review from a team as a code owner July 18, 2025 23:13
@runeb runeb requested a review from pcarleton July 18, 2025 23:13
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.

1 participant