Skip to content

Handle '&scope=' in authorize request to return empty list #979

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 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/mcp/shared/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class OAuthClientMetadata(BaseModel):
def validate_scope(self, requested_scope: str | None) -> list[str] | None:
if requested_scope is None:
return None
if requested_scope == "":
return []
requested_scopes = requested_scope.split(" ")
allowed_scopes = [] if self.scope is None else self.scope.split(" ")
for scope in requested_scopes:
Expand Down
23 changes: 23 additions & 0 deletions tests/client/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,29 @@ async def test_refresh_token_request(self, oauth_provider, valid_tokens):
class TestProtectedResourceMetadata:
"""Test protected resource handling."""

@pytest.mark.anyio
async def test_client_metadata_validate_scopes_none(self, client_metadata):
"""Test that validate_scopes method handles None and empty string correctly."""
# Should return None
requested_scopes = client_metadata.validate_scope(None)
assert requested_scopes is None

# No scopes should be requested; this can happen when a client authorizes with "&scope=".
requested_scopes = client_metadata.validate_scope("")
assert requested_scopes == []

@pytest.mark.anyio
async def test_state_parameter_validation_uses_constant_time(
self, oauth_provider, oauth_metadata, oauth_client_info
):
"""Test that state parameter validation uses constant-time comparison."""
oauth_provider._metadata = oauth_metadata
oauth_provider._client_info = oauth_client_info

# Mock callback handler to return mismatched state
async def mock_callback_handler() -> tuple[str, str | None]:
return "test_auth_code", "wrong_state"

@pytest.mark.anyio
async def test_resource_param_included_with_recent_protocol_version(self, oauth_provider: OAuthClientProvider):
"""Test resource parameter is included for protocol version >= 2025-06-18."""
Expand Down
Loading