Something went wrong. Try again.
mirror of github.com/PrefectHQ/fastmcp
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108"""Authorization-based task isolation (CRITICAL SECURITY).
Tasks are scoped to the caller's authorization identity via the auth-scopedcompound Docket key, so a caller can only resolve tasks it created. A cross-scopetask id is indistinguishable from a missing one (-32602 "not found"), which keepstask existence from leaking across callers. These tests drive the task lifecyclein-process (there is no client task API until Phase 4), binding a differentaccess token per caller through the shared helper."""
from __future__ import annotations
import pytestfrom mcp.shared.exceptions import MCPError
from fastmcp import FastMCPfrom fastmcp_tasks import TasksExtensionfrom tests.tasks.task_helpers import ( get_task, make_access_token, run_task, running_task_server, submit_task, wait_for_task,)
@pytest.fixturedef task_server() -> FastMCP: mcp = FastMCP("security-test-server") mcp.add_extension(TasksExtension())
@mcp.tool(task=True) async def secret_tool(data: str) -> str: return f"Secret result: {data}"
return mcp
async def test_same_client_can_access_all_its_tasks(task_server: FastMCP): """A single authenticated caller can resolve every task it created.""" token = make_access_token("client-a") async with running_task_server(task_server): first = await run_task( task_server, "secret_tool", {"data": "first"}, access_token=token ) second = await run_task( task_server, "secret_tool", {"data": "second"}, access_token=token ) assert first.result is not None assert "first" in first.result["content"][0]["text"] assert second.result is not None assert "second" in second.result["content"][0]["text"]
async def test_unauthenticated_client_can_access_its_tasks(task_server: FastMCP): """An anonymous caller can resolve tasks in the anonymous keyspace.""" async with running_task_server(task_server): final = await run_task(task_server, "secret_tool", {"data": "hello"}) assert final.result is not None assert "hello" in final.result["content"][0]["text"]
async def test_distinct_clients_cannot_access_each_others_tasks( task_server: FastMCP,): """Two distinct client_ids live in disjoint scopes: a peer's id is 'not found'.""" alice = make_access_token("client-a") bob = make_access_token("client-b") async with running_task_server(task_server): created = await submit_task( task_server, "secret_tool", {"data": "a-secret"}, access_token=alice ) with pytest.raises(MCPError, match="not found"): await get_task(task_server, created.task_id, access_token=bob)
async def test_distinct_subs_same_client_id_cannot_access_each_others_tasks( task_server: FastMCP,): """Fixed-OAuth case: one client_id, distinct ``sub`` claims stay isolated.""" shared = "shared-oauth-app" alice = make_access_token(shared, sub="user-alice") bob = make_access_token(shared, sub="user-bob") async with running_task_server(task_server): created = await submit_task( task_server, "secret_tool", {"data": "alice-secret"}, access_token=alice ) with pytest.raises(MCPError, match="not found"): await get_task(task_server, created.task_id, access_token=bob)
async def test_authenticated_and_anonymous_keyspaces_are_disjoint( task_server: FastMCP,): """An anonymous caller cannot read an authenticated caller's task.""" authed = make_access_token("client-a") async with running_task_server(task_server): created = await submit_task( task_server, "secret_tool", {"data": "authed-secret"}, access_token=authed ) # No access_token -> anonymous keyspace -> cannot resolve the authed task. with pytest.raises(MCPError, match="not found"): await get_task(task_server, created.task_id) # And the authenticated caller still resolves it. seen = await wait_for_task(task_server, created.task_id, access_token=authed) assert seen.status == "completed"