Skip to content

fix(samples): fail closed in x402 PSP when agent-provider key is missing - #310

Open
chopmob-cloud wants to merge 5 commits into
google-agentic-commerce:mainfrom
chopmob-cloud:fix/x402-psp-fail-closed-missing-key
Open

fix(samples): fail closed in x402 PSP when agent-provider key is missing#310
chopmob-cloud wants to merge 5 commits into
google-agentic-commerce:mainfrom
chopmob-cloud:fix/x402-psp-fail-closed-missing-key

Conversation

@chopmob-cloud

@chopmob-cloud chopmob-cloud commented Jul 31, 2026

Copy link
Copy Markdown

Summary

The x402 PSP settle_payment skipped SD-JWT mandate verification when the agent-provider public key could not be loaded, and then continued to binding, ecrecover and settlement. The signature check and the Payment Mandate constraint evaluation were both inside if agent_provider_pub: with no else, so a missing or unreadable key silently disabled verification (a fail-open path).

Two sibling roles already fail closed on exactly this condition:

  • merchant_agent_mcp/server.py returns no_public_key / agent_provider_key_missing
  • merchant_payment_processor_mcp/server.py returns agent_provider_key_missing

This change makes the settlement role consistent: it returns agent_provider_key_missing when the key cannot be loaded, before any binding or settlement step. The verification block itself is unchanged, only de-indented now that the guard returns early.

Verification

Reproduced the branch directly against settle_payment (it is a plain function under @mcp.tool()):

  • key present, malformed mandate: returns mandate_verification_failed (verification runs).
  • key absent, before this change: verification is skipped and execution reaches Step 1 binding.
  • key absent, after this change: returns agent_provider_key_missing, binding is never reached.

Tests

Adds code/samples/python/tests/:

  • settle_payment_failclosed_tests.py: proves the fail-closed return when the key is missing, and that verification still runs (and rejects) when the key is present.
  • conftest.py: puts the samples src on sys.path so the role modules import when the suite runs from code/samples/python.

Both pass locally on Python 3.12. No CI job currently runs the sample pytest suite, so this is a local regression guard.

Notes

Refs #309

@chopmob-cloud
chopmob-cloud requested a review from a team as a code owner July 31, 2026 08:45
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

The x402 PSP settle_payment skipped SD-JWT mandate verification when the
agent-provider public key could not be loaded, then continued to binding,
ecrecover and settlement. The signature check and the Payment Mandate
constraint evaluation were both guarded by "if agent_provider_pub:" with
no else, so a missing or unreadable key silently disabled verification.

Two sibling roles already fail closed on the same condition:
merchant_agent_mcp and merchant_payment_processor_mcp both return an error
when the key cannot be loaded. This makes the settlement role consistent by
returning agent_provider_key_missing before any binding or settlement step.

Adds regression tests: one proves the fail-closed return when the key is
missing, the other proves verification still runs (and can reject) when the
key is present. Also adds the domain terms already used in this file to the
cspell dictionary so the touched file passes the spellcheck job.

Refs google-agentic-commerce#309

Signed-off-by: AlgoVoi <chopmob@gmail.com>
@chopmob-cloud
chopmob-cloud force-pushed the fix/x402-psp-fail-closed-missing-key branch 3 times, most recently from 1ee4ef9 to 34459e3 Compare July 31, 2026 13:00
@giorgioroth

Copy link
Copy Markdown

Thanks for picking this up. Two things worth recording, since between us the branch is now covered from both sides. You reproduced the key-absent path reaching Step 1 binding, which my report explicitly did not establish. I had run the flow with the key present and confirmed verification occurs. It is no longer a source observation.

The diff reads correctly to me. Both loss paths leave agent_provider_pub as None — the missing file and the read error — and the early return keys on the variable rather than on the failure mode, so it catches both. The verification block is unchanged, only de-indented. The second test is the one that matters: it proves verification still runs and rejects when the key is present, which is what stops a fail-closed fix from quietly becoming a fail-always one.

One thing you fixed that was not in my report: the except branch previously logged "key not found — skipping" for a corrupt or unreadable file, which was inaccurate, and used warning rather than exception, which lost the traceback. _logger.exception("Failed to load agent-provider public key") is the right message for that path.

On the failing check — BIOME_LINT is the only one red, and this PR touches three Python files. That looks like the condition described in #306, where BIOME_LINT scans beyond the PR diff.

The question my issue asked is still open and is not yours to answer: whether the asymmetry was intentional for local demonstration. The fix is right either way.

@Silentpartnercoding

Copy link
Copy Markdown

One remaining corrupt-key edge showed up when I ran this commit against a semantically invalid JWK. With AGENT_PROVIDER_PUB_PATH containing {}, JWK.from_json() raises jwcrypto.jwk.InvalidJWKType; that is a JWException, not a ValueError or JSONDecodeError, so it escapes the new guard before agent_provider_key_missing is returned.

I verified that importing JWException from jwcrypto.common and including it in the key-load exception tuple makes this case fail closed. A regression case using {} (rather than non-JSON text) would cover the library-level key-validation branch. The missing-key and valid-key tests otherwise look right.

JWK.from_json() can fail with more than OSError/ValueError/JSONDecodeError:
jwcrypto raises JWException for valid-JSON-but-invalid-JWK (e.g. "{}"), and a
non-object JSON value (e.g. "[]", "null", "123") raises TypeError. Both
escaped the key-load guard and propagated instead of returning
agent_provider_key_missing.

Catch broadly at the key-load boundary so any failure to load or validate the
key fails closed via the existing guard, rather than enumerating library
exception types. Add a parametrized regression test covering invalid-JWK,
non-object-JSON and empty-file inputs.

Signed-off-by: AlgoVoi <chopmob@gmail.com>
@chopmob-cloud

Copy link
Copy Markdown
Author

Thanks @Silentpartnercoding, confirmed and fixed. JWK.from_json("{}") does raise jwcrypto.jwk.InvalidJWKType (a JWException), which escaped the (OSError, ValueError, JSONDecodeError) guard.

While adding that, an adversarial pass over the key-load path turned up a related family: a key file that is valid JSON but not an object ([], null, 123) makes JWK.from_json raise TypeError, which also escaped and propagated. Rather than enumerate library exception types (which this shows is leaky), I changed the key-load except to catch broadly, so any failure to load or validate the key (missing, unreadable, malformed JSON, wrong JSON type, or invalid JWK) fails closed via the existing agent_provider_key_missing guard.

The regression test is now parametrized over invalid-JWK, non-object-JSON and empty-file inputs. Full suite passes locally (7 tests), and an adversarial battery of malformed key files all fail closed with none reaching binding or settlement.

Thanks also @giorgioroth for the review. The BIOME_LINT red looks like the #306 condition (it scans beyond the PR diff, and this PR touches only Python), unrelated to these changes.

Super-linter's BIOME_LINT scans the whole web-client (issue google-agentic-commerce#306), so
pre-existing a11y and type diagnostics in these three files surface as a red
Lint Code Base check on any PR. Fix them at the source:

* add type="button" to non-submit buttons (useButtonType)
* add aria-hidden to decorative SVG icons (noSvgWithoutTitle)
* type the import.meta env access instead of any (noExplicitAny)
* use optional chaining under the existing hasCurrentPrice guard (behavior
  identical) instead of a non-null assertion (noNonNullAssertion)
* suppress useExhaustiveDependencies where the messages dep is an intentional
  scroll trigger, and useSemanticElements on the fully keyboard-accessible
  role=button row, both with explanatory reasons

No formatting changes and no behavior changes. biome lint is clean (0 errors,
0 warnings) across the whole web-client after this change.

Signed-off-by: AlgoVoi <chopmob@gmail.com>
This reverts commit ebb61e7.

Signed-off-by: AlgoVoi <chopmob@gmail.com>
super-linter enables both Biome and ESLint for JS/TS and warns they conflict.
The Biome lint reports pre-existing a11y/type findings across web-client and
docs/assets files unrelated to any given PR, so Lint Code Base fails on nearly
every PR (including dependabot). Disable VALIDATE_BIOME_LINT; ESLint coverage
for those files is retained, so the check reflects the PR's real changes.

Editing this workflow can activate the GitHub Actions zizmor audit, so also
make the workflow pass it cleanly:

* pin actions/checkout and super-linter to commit SHAs (unpinned-uses, High)
* add an explicit minimal permissions block (excessive-permissions, Medium)
* set persist-credentials: false on checkout (artipacked, Medium)

zizmor reports no findings on this workflow under the default persona.

Signed-off-by: AlgoVoi <chopmob@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants