-
Notifications
You must be signed in to change notification settings - Fork 472
fix(samples): authorize the verified mandate amount in x402 credential provider #300
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
chopmob-cloud
wants to merge
6
commits into
google-agentic-commerce:main
Choose a base branch
from
chopmob-cloud:fix/299-x402-verified-amount
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+117
−12
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
236cbb7
fix(samples): authorize the verified mandate amount in x402 credentia…
chopmob-cloud d393193
chore(spelling): add x402 terms to cspell dictionary
chopmob-cloud e0f76bb
ci: exclude demo web-client from super-linter
chopmob-cloud be89af7
ci: disable Biome linter, which ignores the exclude filter
chopmob-cloud bc2b8b8
ci: use correct VALIDATE_BIOME_LINT var to disable Biome lint
chopmob-cloud 2163769
fix(samples): fail closed when verified payment_amount.amount is null
chopmob-cloud 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
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
74 changes: 74 additions & 0 deletions
74
code/samples/python/src/roles/x402_credentials_provider_mcp/server_tests.py
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,74 @@ | ||
| """Regression tests for x402 credential provider amount handling (issue #299). | ||
|
|
||
| The credential provider must authorize the amount signed in the verified | ||
| payment mandate, never a hardcoded fallback. Before the fix, an AttributeError | ||
| on a missing instrument field caused the handler to silently authorize a fixed | ||
| 1250 cents regardless of what the user actually mandated. | ||
| """ | ||
|
|
||
| import sys | ||
|
|
||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
|
|
||
| # Make the samples 'src' root importable (roles.*, common.*). | ||
| _SRC = Path(__file__).resolve().parents[2] | ||
| if str(_SRC) not in sys.path: | ||
| sys.path.insert(0, str(_SRC)) | ||
|
|
||
| from roles.x402_credentials_provider_mcp import server # noqa: E402 | ||
|
|
||
|
|
||
| class _Amount: | ||
|
|
||
| def __init__(self, amount): | ||
| self.amount = amount | ||
|
|
||
|
|
||
| class _Mandate: | ||
|
|
||
| def __init__(self, amount): | ||
| self.payment_amount = _Amount(amount) | ||
|
|
||
|
|
||
| class _Chain: | ||
|
|
||
| def __init__(self, amount): | ||
| self.closed_mandate = _Mandate(amount) | ||
|
|
||
|
|
||
| def test_amount_uses_signed_value(): | ||
| assert server._verified_amount_cents(_Chain(4200)) == 4200 | ||
|
|
||
|
|
||
| def test_amount_honors_any_signed_value(): | ||
| # The pre-fix code authorized a fixed 1250 regardless of the mandate. | ||
| for value in (1, 999, 1249, 1251, 500000): | ||
| assert server._verified_amount_cents(_Chain(value)) == value | ||
|
|
||
|
|
||
| def test_amount_missing_fails_closed(): | ||
| class _NoAmount: | ||
| pass | ||
|
|
||
| # A verified mandate without a payment amount must raise, so the caller can | ||
| # fail closed rather than fabricate a value. | ||
| with pytest.raises(AttributeError): | ||
| server._verified_amount_cents(_NoAmount()) | ||
|
|
||
|
|
||
| def test_amount_null_fails_closed(): | ||
| # payment_amount is present but its amount is null. The null slips past | ||
| # attribute access, so without an explicit guard the function would return | ||
| # None and later crash on None * 10000 instead of failing closed. It must | ||
| # raise AttributeError so the caller returns verification_failed. | ||
| with pytest.raises(AttributeError): | ||
| server._verified_amount_cents(_Chain(None)) | ||
|
|
||
|
|
||
| def test_handler_does_not_fabricate_amount(): | ||
| # Guard against reintroducing the hardcoded amount fallback in the handler. | ||
| source = Path(server.__file__).read_text(encoding="utf-8") | ||
| assert "amount_cents = 1250" not in source | ||
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.