fastmcp/tests/server/auth/test_oauth_consent_flow.py
Jeremiah Lowin 3a9717e6be
Publish docs for v3.2.0 (#3713)
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: Jeremiah Lowin <jlowin@users.noreply.github.com>
Co-authored-by: Marvin Context Protocol <41898282+Marvin Context Protocol@users.noreply.github.com>
Co-authored-by: voidborne-d <voidborne-d@users.noreply.github.com>
Co-authored-by: marvin-context-protocol[bot] <225465937+marvin-context-protocol[bot]@users.noreply.github.com>
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: d 🔹 <258577966+voidborne-d@users.noreply.github.com>
Co-authored-by: Jeremiah Lowin <153965+jlowin@users.noreply.github.com>
Co-authored-by: nightcityblade <nightcityblade@gmail.com>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: Bill Easton <strawgate@users.noreply.github.com>
Co-authored-by: Sumanshu Nankana <sumanshunankana@gmail.com>
Co-authored-by: Eric Robinson <ericrobinson@indeed.com>
Co-authored-by: Martim Santos <martimfasantos@gmail.com>
Co-authored-by: d 🔹 <liusway405@gmail.com>
Co-authored-by: Matthieu B <66959271+mtthidoteu@users.noreply.github.com>
Co-authored-by: Sascha Buehrle <47737812+saschabuehrle@users.noreply.github.com>
Co-authored-by: Hakancan <142545736+hkc5@users.noreply.github.com>
Co-authored-by: nightcityblade <jackchen@haloailabs.com>
Co-authored-by: Matt Hallowell <17804673+mhallo@users.noreply.github.com>
Co-authored-by: nate nowack <thrast36@gmail.com>
Co-authored-by: Bill Easton <williamseaston@gmail.com>
Co-authored-by: Marcus Shu <46469249+shulkx@users.noreply.github.com>
Co-authored-by: Rushabh Doshi <radoshi@gmail.com>
Co-authored-by: AIKAWA Shigechika <shige@aikawa.jp>
Co-authored-by: Jeremy Simon <simonjer805@gmail.com>
Co-authored-by: Miguel Miranda Dias <7780875+pandego@users.noreply.github.com>
Co-authored-by: Anthony James Padavano <padavano.anthony@gmail.com>
Co-authored-by: Mostafa Kamal <hiremostafa@gmail.com>
Fix auto-close MRE script posting comment without closing (#3386)
Fix WorkOS token scope verification bypass 🤖 Generated with Codex (#3407)
Fix initialize McpError fallthrough 🤖 Generated with Codex (#3413)
Fix transform arg collisions with passthrough params (#3431)
Fix get_* returning None when latest version is disabled (#3439)
Fix get_* returning None when latest version is disabled (#3421)
Fix server lifespan overlap teardown (#3415)
Fix $ref output schema object detection regression (#3420)
resolved annotations (#3429)
Fix async partial callables rejected by iscoroutinefunction (#3438)
Fix async partial callables rejected by iscoroutinefunction (#3423)
fix: add version to components (#3458)
fix: use intent-based flag for OIDC scope patch in load_access_token (#3465)
Fixes #3461
fix: normalize Google scope shorthands and surface valid_scopes (#3477)
fix: resolve ty 0.0.23 type-checking errors and bump pin (#3481)
fix: shield lifespan teardown from cancellation (#3480)
fix: forward custom_route endpoints from mounted servers (#3462)
fix updates _get_additional_http_routes() to traverse providers,
Fixes #3457
fix: remove hardcoded version from CLI help text (#3456)
fix: monty 0.0.8 compatibility, drop external_functions from constructor (#3468)
fix: task test teardown hanging 5s per test (#3499)
Closes #3498
fix: validate workspace path is a directory before cursor install (#3440)
Fixes #3426
fix: handle re.error from malformed URI templates in build_regex (#3501)
fix: reject empty/OIDC-only required_scopes in AzureProvider (#3503)
fix: restrict $ref resolution to local refs only (SSRF/LFI) (#3502)
fix warnings and timeouts (#3504)
close upgrade check issue when build passes (#3505)
Closes #3484
fix: URL-encode path params to prevent SSRF/path traversal (GHSA-vv7q-7jx5-f767) (#3507)
fix: prevent path traversal in skill download (#3493)
fix: prefer IdP-granted scopes over client-requested scopes in OAuthProxy (#3492)
fix: remove unrelated transform and http.py changes from PR scope
fix: remove forced follow_redirects from httpx_client_factory calls (#3496)
fix: stop passing follow_redirects to httpx_client_factory
fix: restore follow_redirects=True for custom httpx client factories
Closes #3509
fix: CSRF double-submit cookie check in consent flow (#3519)
fix: validate server names in install commands (#3522)
fix: use raw strings for regex in pytest.raises match (#3523)
fix: reject refresh tokens used as Bearer access tokens (#3524)
fix: route ResourcesAsTools/PromptsAsTools through server middleware (#3495)
fix: resolve Pyright "Module is not callable" on @tool, @resource, @prompt decorators (#3540)
fix: filter warnings by message in KEY_PREFIX test (#3549)
fix: suppress output schema for ToolResult subclass annotations (#3548)
fix: increase sleep duration in proxy cache tests (#3567)
fix: store absolute token expiry to prevent stale expires_in on reload (#3572)
fix: preserve tool properties named 'title' during schema compression (#3582)
Fix loopback redirect URI port matching per RFC 8252 §7.3 (#3589)
Fix app tool routing: visibility check and middleware propagation (#3591)
Fix query parameter serialization to respect OpenAPI explode/style settings (#3595)
Fix dev apps form: union types, textarea support, JSON parsing (#3597)
fix(google): replace deprecated /oauth2/v1/tokeninfo with /oauth2/v3/userinfo (#3603)
fix: resolve EntraOBOToken dependency injection through MultiAuth (#3609)
fix(docs): correct misleading stateless_http header (#3622)
fix: filesystem provider import machinery (#3626)
Closes #3625 (issues 2, 3, 6)
fix: recover StdioTransport after subprocess exits (#3630)
fix(server): preserve mounted tool task metadata (#3632)
fix: scope deprecation warning filter to FastMCPDeprecationWarning (#3649)
fix imports, add PrefabAppConfig (#3650)
fix: resolve CurrentFastMCP/ctx.fastmcp to child server in mounted background tasks (#3651)
Fix blocking docs issues: chart imports, Select API, Rx consistency (#3652)
closed by default (#3657)
Fix prompt caching middleware missing wrap/unwrap round-trip (#3666)
fix: serialize object query params per OpenAPI style/explode rules (#3662)
Fixes #2857
fix: HTTP request headers not accessible in background task workers (#3631)
fix: restore HTTP headers in worker execution path for background tasks (#3681)
fix: strip discriminator after dereferencing schemas (#3682)
fix: remove stale ty:ignore directives for ty 0.0.26 (#3684)
Fix docs gaps in app provider pages (#3690)
fix: dev apps log panel UX improvements (#3698)
fix dev server empty string args (#3700)
2026-03-30 16:48:30 -04:00

698 lines
28 KiB
Python

"""Tests for OAuth Proxy consent flow with server-side storage.
This test suite verifies:
1. OAuth transactions are stored in server-side storage (not in-memory)
2. Authorization codes are stored in server-side storage
3. Consent flow redirects correctly through /consent endpoint
4. CSRF protection works with cookies
5. State persists across storage backends
6. Security headers (X-Frame-Options) are set correctly
7. Cookie signing and tampering detection
8. Auto-approve behavior with valid cookies
9. Consent binding cookie prevents confused deputy attacks (GHSA-rww4-4w9c-7733)
"""
import re
import secrets
import time
from urllib.parse import parse_qs, urlparse
import pytest
from key_value.aio.stores.memory import MemoryStore
from mcp.server.auth.provider import AuthorizationParams
from mcp.shared.auth import OAuthClientInformationFull
from pydantic import AnyUrl
from starlette.applications import Starlette
from starlette.testclient import TestClient
from fastmcp.server.auth.auth import AccessToken, TokenVerifier
from fastmcp.server.auth.oauth_proxy import OAuthProxy
from fastmcp.server.auth.oauth_proxy.models import OAuthTransaction
class MockTokenVerifier(TokenVerifier):
"""Mock token verifier for testing."""
def __init__(self):
self.required_scopes = ["read", "write"]
async def verify_token(self, token: str):
"""Mock token verification."""
return AccessToken(
token=token,
client_id="mock-client",
scopes=self.required_scopes,
expires_at=int(time.time() + 3600),
)
class _Verifier(TokenVerifier):
"""Minimal token verifier for security tests."""
def __init__(self):
self.required_scopes = ["read"]
async def verify_token(self, token: str):
return AccessToken(
token=token, client_id="c", scopes=self.required_scopes, expires_at=None
)
@pytest.fixture
def storage():
"""Create a fresh in-memory storage for each test."""
return MemoryStore()
@pytest.fixture
def oauth_proxy_with_storage(storage):
"""Create OAuth proxy with explicit storage backend."""
return OAuthProxy(
upstream_authorization_endpoint="https://github.com/login/oauth/authorize",
upstream_token_endpoint="https://github.com/login/oauth/access_token",
upstream_client_id="test-upstream-client",
upstream_client_secret="test-upstream-secret",
token_verifier=MockTokenVerifier(),
base_url="https://myserver.com",
redirect_path="/auth/callback",
client_storage=storage, # Use our test storage
jwt_signing_key="test-secret",
)
@pytest.fixture
def oauth_proxy_https():
"""OAuthProxy configured with HTTPS base_url for __Host- cookies."""
return OAuthProxy(
upstream_authorization_endpoint="https://github.com/login/oauth/authorize",
upstream_token_endpoint="https://github.com/login/oauth/access_token",
upstream_client_id="client-id",
upstream_client_secret="client-secret",
token_verifier=_Verifier(),
base_url="https://myserver.example",
client_storage=MemoryStore(),
jwt_signing_key="test-secret",
)
async def _start_flow(
proxy: OAuthProxy, client_id: str, redirect: str
) -> tuple[str, str]:
"""Register client and start auth; returns (txn_id, consent_url)."""
await proxy.register_client(
OAuthClientInformationFull(
client_id=client_id,
client_secret="s",
redirect_uris=[AnyUrl(redirect)],
)
)
params = AuthorizationParams(
redirect_uri=AnyUrl(redirect),
redirect_uri_provided_explicitly=True,
state="client-state-xyz",
code_challenge="challenge",
scopes=["read"],
)
consent_url = await proxy.authorize(
OAuthClientInformationFull(
client_id=client_id,
client_secret="s",
redirect_uris=[AnyUrl(redirect)],
),
params,
)
qs = parse_qs(urlparse(consent_url).query)
return qs["txn_id"][0], consent_url
def _extract_csrf(html: str) -> str | None:
"""Extract CSRF token from HTML form."""
m = re.search(r"name=\"csrf_token\"\s+value=\"([^\"]+)\"", html)
return m.group(1) if m else None
class TestServerSideStorage:
"""Tests verifying OAuth state is stored in AsyncKeyValue storage."""
async def test_transaction_stored_in_storage_not_memory(
self, oauth_proxy_with_storage, storage
):
"""Verify OAuth transactions are stored in AsyncKeyValue, not in-memory dict."""
# Register client
client = OAuthClientInformationFull(
client_id="test-client",
client_secret="test-secret",
redirect_uris=[AnyUrl("http://localhost:54321/callback")],
)
await oauth_proxy_with_storage.register_client(client)
# Start authorization flow
params = AuthorizationParams(
redirect_uri=AnyUrl("http://localhost:54321/callback"),
redirect_uri_provided_explicitly=True,
state="client-state-123",
code_challenge="challenge-abc",
scopes=["read", "write"],
)
redirect_url = await oauth_proxy_with_storage.authorize(client, params)
# Extract transaction ID from consent redirect
parsed = urlparse(redirect_url)
assert "/consent" in parsed.path, "Should redirect to consent page"
query_params = parse_qs(parsed.query)
txn_id = query_params["txn_id"][0]
# Verify transaction is NOT in the old in-memory dict
# (the attribute should not exist or should be empty)
assert (
not hasattr(oauth_proxy_with_storage, "_oauth_transactions")
or len(getattr(oauth_proxy_with_storage, "_oauth_transactions", {})) == 0
)
# Verify transaction IS in storage backend
transaction = await storage.get(collection="mcp-oauth-transactions", key=txn_id)
assert transaction is not None, "Transaction should be in storage"
# Verify transaction has expected structure
assert transaction["client_id"] == "test-client"
assert transaction["client_redirect_uri"] == "http://localhost:54321/callback"
assert transaction["client_state"] == "client-state-123"
assert transaction["code_challenge"] == "challenge-abc"
assert transaction["scopes"] == ["read", "write"]
async def test_authorization_code_stored_in_storage(
self, oauth_proxy_with_storage, storage
):
"""Verify authorization codes are stored in AsyncKeyValue storage."""
# Register client
client = OAuthClientInformationFull(
client_id="test-client",
client_secret="test-secret",
redirect_uris=[AnyUrl("http://localhost:54321/callback")],
)
await oauth_proxy_with_storage.register_client(client)
# Create a test app with OAuth routes
app = Starlette(routes=oauth_proxy_with_storage.get_routes())
with TestClient(app) as test_client:
# Start authorization flow
params = AuthorizationParams(
redirect_uri=AnyUrl("http://localhost:54321/callback"),
redirect_uri_provided_explicitly=True,
state="client-state",
code_challenge="challenge-xyz",
scopes=["read"],
)
redirect_url = await oauth_proxy_with_storage.authorize(client, params)
# Extract txn_id from consent redirect
parsed = urlparse(redirect_url)
query_params = parse_qs(parsed.query)
txn_id = query_params["txn_id"][0]
# Simulate consent approval
# First, get the consent page to establish CSRF cookie
consent_response = test_client.get(
f"/consent?txn_id={txn_id}", follow_redirects=False
)
# Extract CSRF token from response (it's in the HTML form)
csrf_token = None
if consent_response.status_code == 200:
# For this test, we'll generate a CSRF token manually
# In production, this comes from the consent page HTML
csrf_token = secrets.token_urlsafe(32)
# Approve consent with CSRF token
# Set cookies on client instance to avoid deprecation warning
for k, v in consent_response.cookies.items():
test_client.cookies.set(k, v)
approval_response = test_client.post(
"/consent",
data={
"action": "approve",
"txn_id": txn_id,
"csrf_token": csrf_token if csrf_token else "",
},
follow_redirects=False,
)
# After approval, authorization code should be in storage
# The code is returned in the redirect URL
if approval_response.status_code in (302, 303):
location = approval_response.headers.get("location", "")
callback_params = parse_qs(urlparse(location).query)
if "code" in callback_params:
auth_code = callback_params["code"][0]
# Verify code is NOT in old in-memory dict
assert (
not hasattr(oauth_proxy_with_storage, "_client_codes")
or len(getattr(oauth_proxy_with_storage, "_client_codes", {}))
== 0
)
# Verify code IS in storage
code_data = await storage.get(
collection="mcp-authorization-codes", key=auth_code
)
assert code_data is not None, (
"Authorization code should be in storage"
)
assert code_data["client_id"] == "test-client"
assert code_data["scopes"] == ["read"]
async def test_storage_collections_are_isolated(self, oauth_proxy_with_storage):
"""Verify that transactions, codes, and clients use separate collections."""
# Register a client
client = OAuthClientInformationFull(
client_id="isolation-test-client",
client_secret="test-secret",
redirect_uris=[AnyUrl("http://localhost:12345/callback")],
)
await oauth_proxy_with_storage.register_client(client)
# Start authorization to create transaction
params = AuthorizationParams(
redirect_uri=AnyUrl("http://localhost:12345/callback"),
redirect_uri_provided_explicitly=True,
state="test-state",
code_challenge="test-challenge",
scopes=["read"],
)
await oauth_proxy_with_storage.authorize(client, params)
# Get all collections from storage
storage = oauth_proxy_with_storage._client_storage
# Verify client is in client collection
client_data = await storage.get(
collection="mcp-oauth-proxy-clients", key="isolation-test-client"
)
assert client_data is not None
# Verify we can list transactions separately
# (This tests that collections are properly namespaced)
transactions = await storage.keys(collection="mcp-oauth-transactions")
assert len(transactions) > 0, "Should have at least one transaction"
# Verify transaction keys don't collide with client keys
for txn_key in transactions:
assert txn_key != "isolation-test-client"
class TestConsentFlowRedirects:
"""Tests for consent flow redirect behavior."""
async def test_authorize_redirects_to_consent_page(self, oauth_proxy_with_storage):
"""Verify authorize() redirects to /consent instead of upstream."""
client = OAuthClientInformationFull(
client_id="consent-test-client",
client_secret="test-secret",
redirect_uris=[AnyUrl("http://localhost:8080/callback")],
)
await oauth_proxy_with_storage.register_client(client)
params = AuthorizationParams(
redirect_uri=AnyUrl("http://localhost:8080/callback"),
redirect_uri_provided_explicitly=True,
state="test-state",
code_challenge="",
scopes=["read"],
)
redirect_url = await oauth_proxy_with_storage.authorize(client, params)
# Should redirect to consent page, not upstream
assert "/consent" in redirect_url
assert "github.com" not in redirect_url
assert "?txn_id=" in redirect_url
async def test_consent_page_contains_transaction_id(self, oauth_proxy_with_storage):
"""Verify consent page receives and displays transaction ID."""
client = OAuthClientInformationFull(
client_id="txn-test-client",
client_secret="test-secret",
redirect_uris=[AnyUrl("http://localhost:9090/callback")],
)
await oauth_proxy_with_storage.register_client(client)
params = AuthorizationParams(
redirect_uri=AnyUrl("http://localhost:9090/callback"),
redirect_uri_provided_explicitly=True,
state="test-state",
code_challenge="test-challenge",
scopes=["read", "write"],
)
redirect_url = await oauth_proxy_with_storage.authorize(client, params)
# Extract txn_id parameter
parsed = urlparse(redirect_url)
query = parse_qs(parsed.query)
assert "txn_id" in query
txn_id = query["txn_id"][0]
assert len(txn_id) > 0
# Create test client
app = Starlette(routes=oauth_proxy_with_storage.get_routes())
with TestClient(app) as test_client:
# Request consent page
response = test_client.get(
f"/consent?txn_id={txn_id}", follow_redirects=False
)
assert response.status_code == 200
# Consent page should contain transaction reference
assert txn_id.encode() in response.content or b"consent" in response.content
class TestCSRFProtection:
"""Tests for CSRF protection in consent flow."""
async def test_consent_requires_csrf_token(self, oauth_proxy_with_storage):
"""Verify consent submission requires valid CSRF token."""
client = OAuthClientInformationFull(
client_id="csrf-test-client",
client_secret="test-secret",
redirect_uris=[AnyUrl("http://localhost:7070/callback")],
)
await oauth_proxy_with_storage.register_client(client)
params = AuthorizationParams(
redirect_uri=AnyUrl("http://localhost:7070/callback"),
redirect_uri_provided_explicitly=True,
state="test-state",
code_challenge="",
scopes=["read"],
)
redirect_url = await oauth_proxy_with_storage.authorize(client, params)
parsed = urlparse(redirect_url)
query = parse_qs(parsed.query)
txn_id = query["txn_id"][0]
app = Starlette(routes=oauth_proxy_with_storage.get_routes())
with TestClient(app) as test_client:
# Try to submit consent WITHOUT CSRF token
response = test_client.post(
"/consent",
data={"action": "approve", "txn_id": txn_id},
# No CSRF token!
follow_redirects=False,
)
# Should reject or require CSRF
# (Implementation may vary - checking for error response)
assert response.status_code in (
400,
403,
302,
) # Error or redirect to error
async def test_consent_cookie_established_on_page_visit(
self, oauth_proxy_with_storage
):
"""Verify consent page establishes CSRF cookie."""
client = OAuthClientInformationFull(
client_id="cookie-test-client",
client_secret="test-secret",
redirect_uris=[AnyUrl("http://localhost:6060/callback")],
)
await oauth_proxy_with_storage.register_client(client)
params = AuthorizationParams(
redirect_uri=AnyUrl("http://localhost:6060/callback"),
redirect_uri_provided_explicitly=True,
state="test-state",
code_challenge="",
scopes=["read"],
)
redirect_url = await oauth_proxy_with_storage.authorize(client, params)
parsed = urlparse(redirect_url)
query = parse_qs(parsed.query)
txn_id = query["txn_id"][0]
app = Starlette(routes=oauth_proxy_with_storage.get_routes())
with TestClient(app) as test_client:
# Visit consent page
response = test_client.get(
f"/consent?txn_id={txn_id}", follow_redirects=False
)
# Should set cookies for CSRF protection
assert response.status_code == 200
# Cookie may be set via Set-Cookie header
cookies = response.cookies
# Look for any CSRF-related cookie (implementation dependent)
assert len(cookies) > 0 or "csrf" in response.text.lower(), (
"Consent page should establish CSRF protection"
)
class TestCSRFDoubleSubmit:
"""Tests for CSRF double-submit cookie validation (GHSA-rww4-4w9c-7733 bypass)."""
async def test_consent_rejected_without_csrf_cookie(self, oauth_proxy_with_storage):
"""Submitting a valid CSRF token without the matching cookie should be rejected.
This prevents an attacker from using their own tx_id/csrf_token to CSRF
the victim's browser into approving consent.
"""
txn_id, _ = await _start_flow(
oauth_proxy_with_storage,
"csrf-double-submit-client",
"http://localhost:9090/callback",
)
app = Starlette(routes=oauth_proxy_with_storage.get_routes())
with TestClient(app) as test_client:
# Visit consent page to populate the transaction with a CSRF token
consent_resp = test_client.get(f"/consent?txn_id={txn_id}")
assert consent_resp.status_code == 200
csrf_token = _extract_csrf(consent_resp.text)
assert csrf_token
# Simulate the attack: use a FRESH client (no cookies from the consent
# page) to submit the form with a valid CSRF token — as if the attacker
# tricked the victim's browser into POSTing their tx_id/csrf_token.
with TestClient(app) as attacker_client:
response = attacker_client.post(
"/consent",
data={
"action": "approve",
"txn_id": txn_id,
"csrf_token": csrf_token,
},
follow_redirects=False,
)
assert response.status_code == 403
class TestStoragePersistence:
"""Tests for state persistence across storage backends."""
async def test_transaction_persists_after_retrieval(self, oauth_proxy_with_storage):
"""Verify transaction can be retrieved multiple times (until deleted)."""
client = OAuthClientInformationFull(
client_id="persist-test-client",
client_secret="test-secret",
redirect_uris=[AnyUrl("http://localhost:5050/callback")],
)
await oauth_proxy_with_storage.register_client(client)
params = AuthorizationParams(
redirect_uri=AnyUrl("http://localhost:5050/callback"),
redirect_uri_provided_explicitly=True,
state="persist-state",
code_challenge="persist-challenge",
scopes=["read"],
)
redirect_url = await oauth_proxy_with_storage.authorize(client, params)
parsed = urlparse(redirect_url)
query = parse_qs(parsed.query)
txn_id = query["txn_id"][0]
storage = oauth_proxy_with_storage._client_storage
# Retrieve transaction multiple times
txn1 = await storage.get(collection="mcp-oauth-transactions", key=txn_id)
assert txn1 is not None
txn2 = await storage.get(collection="mcp-oauth-transactions", key=txn_id)
assert txn2 is not None
# Should be the same data
assert txn1["client_id"] == txn2["client_id"]
assert txn1["client_state"] == txn2["client_state"]
async def test_storage_uses_pydantic_adapter(self, oauth_proxy_with_storage):
"""Verify that PydanticAdapter serializes/deserializes correctly."""
client = OAuthClientInformationFull(
client_id="pydantic-test-client",
client_secret="test-secret",
redirect_uris=[AnyUrl("http://localhost:4040/callback")],
)
await oauth_proxy_with_storage.register_client(client)
params = AuthorizationParams(
redirect_uri=AnyUrl("http://localhost:4040/callback"),
redirect_uri_provided_explicitly=True,
state="pydantic-state",
code_challenge="pydantic-challenge",
scopes=["read", "write"],
)
redirect_url = await oauth_proxy_with_storage.authorize(client, params)
parsed = urlparse(redirect_url)
query = parse_qs(parsed.query)
txn_id = query["txn_id"][0]
# Retrieve using PydanticAdapter (which is what the proxy uses)
transaction_store = oauth_proxy_with_storage._transaction_store
txn_model = await transaction_store.get(key=txn_id)
# Should be a Pydantic model instance
assert isinstance(txn_model, OAuthTransaction)
assert txn_model.client_id == "pydantic-test-client"
assert txn_model.client_state == "pydantic-state"
assert txn_model.code_challenge == "pydantic-challenge"
assert txn_model.scopes == ["read", "write"]
class TestConsentSecurity:
"""Tests for consent page security features."""
async def test_consent_sets_xfo_header(self, oauth_proxy_https):
"""Verify consent page sets X-Frame-Options header to prevent clickjacking."""
txn_id, _ = await _start_flow(
oauth_proxy_https, "client-a", "http://localhost:5001/callback"
)
app = Starlette(routes=oauth_proxy_https.get_routes())
with TestClient(app) as c:
r = c.get(f"/consent?txn_id={txn_id}")
assert r.status_code == 200
assert r.headers.get("X-Frame-Options") == "DENY"
async def test_deny_sets_cookie_and_redirects_with_error(self, oauth_proxy_https):
"""Verify denying consent sets signed cookie and redirects with error."""
client_redirect = "http://localhost:5002/callback"
txn_id, _ = await _start_flow(oauth_proxy_https, "client-b", client_redirect)
app = Starlette(routes=oauth_proxy_https.get_routes())
with TestClient(app) as c:
consent = c.get(f"/consent?txn_id={txn_id}")
csrf = _extract_csrf(consent.text)
assert csrf
# Persist consent page cookies on client instance to avoid per-request deprecation
for k, v in consent.cookies.items():
c.cookies.set(k, v)
r = c.post(
"/consent",
data={"action": "deny", "txn_id": txn_id, "csrf_token": csrf},
follow_redirects=False,
)
assert r.status_code in (302, 303)
loc = r.headers.get("location", "")
parsed = urlparse(loc)
assert parsed.scheme == "http" and parsed.netloc.startswith("localhost")
q = parse_qs(parsed.query)
assert q.get("error") == ["access_denied"]
assert q.get("state") == ["client-state-xyz"]
# Signed denied cookie should be set
assert "MCP_DENIED_CLIENTS" in ";\n".join(
r.headers.get("set-cookie", "").splitlines()
)
async def test_approve_sets_cookie_and_redirects_to_upstream(
self, oauth_proxy_https
):
"""Verify approving consent sets signed cookie and redirects to upstream."""
txn_id, _ = await _start_flow(
oauth_proxy_https, "client-c", "http://localhost:5003/callback"
)
app = Starlette(routes=oauth_proxy_https.get_routes())
with TestClient(app) as c:
consent = c.get(f"/consent?txn_id={txn_id}")
csrf = _extract_csrf(consent.text)
assert csrf
for k, v in consent.cookies.items():
c.cookies.set(k, v)
r = c.post(
"/consent",
data={"action": "approve", "txn_id": txn_id, "csrf_token": csrf},
follow_redirects=False,
)
assert r.status_code in (302, 303)
loc = r.headers.get("location", "")
assert loc.startswith("https://github.com/login/oauth/authorize")
assert f"state={txn_id}" in loc
# Signed approved cookie should be set with __Host- prefix for HTTPS
set_cookie = ";\n".join(r.headers.get("set-cookie", "").splitlines())
assert "__Host-MCP_APPROVED_CLIENTS" in set_cookie
async def test_tampered_cookie_is_ignored(self, oauth_proxy_https):
"""Verify tampered approval cookie is ignored and consent page shown."""
txn_id, _ = await _start_flow(
oauth_proxy_https, "client-d", "http://localhost:5004/callback"
)
app = Starlette(routes=oauth_proxy_https.get_routes())
with TestClient(app) as c:
# Create a tampered cookie (invalid signature)
# Value format: payload.signature; using wrong signature to force failure
tampered_value = "W10=.invalidsig"
c.cookies.set("__Host-MCP_APPROVED_CLIENTS", tampered_value)
r = c.get(f"/consent?txn_id={txn_id}", follow_redirects=False)
# Should not auto-redirect to upstream; should show consent page
assert r.status_code == 200
# httpx returns a URL object; compare path or stringify
assert urlparse(str(r.request.url)).path == "/consent"
async def test_autoapprove_cookie_skips_consent(self, oauth_proxy_https):
"""Verify valid approval cookie auto-approves and redirects to upstream."""
client_id = "client-e"
redirect = "http://localhost:5005/callback"
txn_id, _ = await _start_flow(oauth_proxy_https, client_id, redirect)
app = Starlette(routes=oauth_proxy_https.get_routes())
with TestClient(app) as c:
# Approve once to set approved cookie
consent = c.get(f"/consent?txn_id={txn_id}")
csrf = _extract_csrf(consent.text)
for k, v in consent.cookies.items():
c.cookies.set(k, v)
r = c.post(
"/consent",
data={
"action": "approve",
"txn_id": txn_id,
"csrf_token": csrf if csrf else "",
},
follow_redirects=False,
)
# Extract approved cookie value
set_cookie = ";\n".join(r.headers.get("set-cookie", "").splitlines())
m = re.search(r"__Host-MCP_APPROVED_CLIENTS=([^;]+)", set_cookie)
assert m, "approved cookie should be set"
approved_cookie = m.group(1)
# Start a new flow for the same client and redirect
new_txn, _ = await _start_flow(oauth_proxy_https, client_id, redirect)
# Should auto-redirect to upstream when visiting consent due to cookie
c.cookies.set("__Host-MCP_APPROVED_CLIENTS", approved_cookie)
r2 = c.get(f"/consent?txn_id={new_txn}", follow_redirects=False)
assert r2.status_code in (302, 303)
assert r2.headers.get("location", "").startswith(
"https://github.com/login/oauth/authorize"
)