Repository navigation
fix(mcp): replay permit_wallet_mismatch 403 with signed denial receipt and details (SEL-482) #218
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
Merged
PetrefiedThunder
merged 8 commits into
main
from
metacode/sel-482-replay-denial-receipt
Oct 26, 2026
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
39528f4
fix(mcp): preserve permit denial receipt on replay (SEL-482)
PetrefiedThunder 38a832f
Merge remote-tracking branch 'origin/main' into metacode/sel-482-repl…
PetrefiedThunder 2ac0ea4
test(gateway): cover denial receipt replay across MCP transports
PetrefiedThunder b33221f
Merge remote-tracking branch 'origin/main' into hermes-tmp-218
PetrefiedThunder c6e9ed3
Merge remote-tracking branch 'origin/main' into hermes-tmp-218
PetrefiedThunder 5161a6b
Merge origin/main into sel-482-replay-denial-receipt
PetrefiedThunder 810127e
Merge remote-tracking branch 'origin/main' into hermes-tmp-218
PetrefiedThunder fac4e31
Merge branch 'main' into metacode/sel-482-replay-denial-receipt
PetrefiedThunder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| """A stored cross-tenant permit denial replays its signed response.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| from uuid import uuid4 | ||
|
|
||
| import pytest | ||
| from httpx import ASGITransport, AsyncClient | ||
| from sqlalchemy import select | ||
|
|
||
| from app.db.database import get_session_factory | ||
| from app.db.models import IdempotencyRecordModel | ||
| from app.main import app | ||
| from app.services.receipts import get_receipt_service | ||
| from tests.test_mcp_idempotency_key_validation import counted_tool # noqa: F401 | ||
| from tests.test_trust_helpers import ( | ||
| BOOTSTRAP_HEADERS, | ||
| create_tool_permit, | ||
| provision_agent_wallet, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.anyio | ||
| async def test_foreign_permit_denial_replays_original_403( | ||
| clean_database, | ||
| counted_tool, # noqa: F811 | ||
| ): | ||
| async with AsyncClient( | ||
| transport=ASGITransport(app=app), base_url="http://test" | ||
| ) as client: | ||
| tenant_a = await provision_agent_wallet(client) | ||
| tenant_b = await provision_agent_wallet(client) | ||
| permit_a = await create_tool_permit( | ||
| client, | ||
| wallet_id=tenant_a["agent_wallet_id"], | ||
| key_id=tenant_a["key_id"], | ||
| tool_name=counted_tool.name, | ||
| ) | ||
| key = f"call_sel482_{uuid4().hex}" | ||
| request = { | ||
| "name": counted_tool.name, | ||
| "arguments": {"text": "one"}, | ||
| "mcp_context": { | ||
| "wallet_id": tenant_b["agent_wallet_id"], | ||
| "permit_id": permit_a["permit_id"], | ||
| "idempotency_key": key, | ||
| }, | ||
| } | ||
| headers = {**tenant_b["agent_headers"], "Idempotency-Key": key} | ||
| path = f"/mcp/tools/{counted_tool.name}/invoke" | ||
|
|
||
| first = await client.post(path, json=request, headers=headers) | ||
| async with get_session_factory()() as session: | ||
| record = ( | ||
| await session.execute( | ||
| select(IdempotencyRecordModel).where( | ||
| IdempotencyRecordModel.idempotency_key == key | ||
| ) | ||
| ) | ||
| ).scalar_one() | ||
| stored = json.loads(record.response_json) | ||
| assert set(stored) >= {"error", "receipt", "details"} | ||
| replay = await client.post(path, json=request, headers=headers) | ||
| other_key = await client.post( | ||
| "/v1/api-keys", | ||
| json={ | ||
| "wallet_id": tenant_b["agent_wallet_id"], | ||
| "key_name": "replay-other-key", | ||
| "expires_in_days": 30, | ||
| }, | ||
| headers=BOOTSTRAP_HEADERS, | ||
| ) | ||
| assert other_key.status_code == 201 | ||
| refused_other_key = await client.post( | ||
| path, | ||
| json=request, | ||
| headers={ | ||
| "X-API-Key": other_key.json()["api_key"], | ||
| "Idempotency-Key": key, | ||
| }, | ||
| ) | ||
|
|
||
| assert first.status_code == replay.status_code == 403 | ||
| detail = first.json()["detail"] | ||
| assert detail["error"] == "permit_wallet_mismatch" | ||
| assert detail["details"] == {"bound_to": "subject_wallet_id"} | ||
| receipt = detail["receipt"] | ||
| assert receipt["wallet_id"] == tenant_b["agent_wallet_id"] | ||
| assert receipt["outcome"] == "denied" | ||
| assert receipt["credits_charged"] == "0" | ||
| assert set(replay.json()["detail"]) == set(detail) | ||
| assert replay.content == first.content | ||
| assert refused_other_key.status_code == 403 | ||
| assert refused_other_key.json()["detail"] == {"error": "permit_wallet_mismatch"} | ||
| assert tenant_a["agent_wallet_id"] not in first.text | ||
| assert counted_tool.effects == [] | ||
|
|
||
| valid, reason, _ = await get_receipt_service().verify_receipt(receipt["receipt_id"]) | ||
| assert (valid, reason) == (True, None) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.