Repository navigation
fix: pin a fetched agent card's RPC url(s) to the origin it was fetched from - #829
Conversation
…ed from Independent finding, found while auditing a same-day adk-python fix (commit 2685acd3, "fix: stop caching a remote agent card that failed validation") during a routine commit-batch audit. That fix closed a caching bug around adk-python's pre-existing _validate_card_rpc_targets check -- a card fetched from a configured source must declare RPC url(s) sharing that source's origin, https (or http on loopback), preventing a compromised/misconfigured/MITM'd card-hosting endpoint from redirecting all future A2A traffic for that agent to an attacker-chosen origin. adk-js has no equivalent check anywhere. resolveAgentCard() fetches a card via the external @a2a-js/sdk package's DefaultAgentCardResolver and returns it with zero validation. Inspected that SDK package directly (npm pack @a2a-js/sdk) -- its resolver performs no such check either. This is not a caching bug around an existing protection; the protection itself does not exist anywhere in adk-js's own code or the SDK it delegates to. Dynamically confirmed with a real local HTTP server: a card served from a "trusted" configured source, declaring an RPC url pointing to https://attacker.example.net/rpc, was accepted with zero error -- resolveAgentCard() returned it as-is. A real deployment would then send all subsequent A2A traffic for that agent, including whatever credential material the request-forwarding path carries, to the attacker's origin. Fix ports adk-python's _validate_card_rpc_targets logic into resolveAgentCard() itself, the single function both of adk-js's call sites (a2a_remote_agent.ts, agent_to_a2a.ts) already go through: every RPC url the card offers is checked (the primary url and each additionalInterfaces entry, not only whichever one a transport negotiation would select), each must be https or http on a loopback host, and each must share the origin the card was fetched from. A directly-provided card object or one read from a local file is exempt, matching adk-python's design. Verified: re-ran the exact PoC against the patched code -- now correctly throws instead of silently accepting the attacker- controlled url. Built 5 additional PoC cases confirming legitimate usage (same-origin card, same-origin additionalInterfaces, directly- provided object) is unaffected while both attack shapes (off-origin primary url, off-origin additionalInterfaces entry even when the primary url is fine) are blocked. Added 6 new regression tests using a real local HTTP server, not a mock of the SDK's resolver. Full agent_card_test.ts: 14/14 pass. Broader core/test/a2a/ suite: 16 files, 242/242 pass, no regressions. tsc --noEmit, eslint, prettier --check all clean. Dedup checked: no existing issue, PR, or commit in adk-js addresses agent-card RPC-url origin validation.
|
Hi @kalenkevich, I have checked the issue and validated the proposed changes. I was able to reproduce the issue. Could you please review this PR? |
|
Hi Team, |
AmaadMartin
left a comment
There was a problem hiding this comment.
I transcribed validateCardRpcTargets, urlOrigin and isLoopbackHost into a scratch script and probed the origin comparison. It holds: scheme confusion, userinfo (https://[email protected]), subdomain prefixes, IDN homoglyphs, non-special schemes (where origin is the string "null"), data:/file:/blob: and backslash authority breaks all throw, and a missing, empty, relative or malformed url fails closed. card.url and additionalInterfaces[].url are also the only two urls the client ever calls (ClientFactory.createFromAgentCard in the @a2a-js/sdk client bundle), so the field coverage is complete. My findings are about legitimate configurations the check now rejects, and about the missing opt-out. I did not run the test suite.
…omparison, add missing tests Addresses AmaadMartin's review, all five points -- three real false positives that would have broken legitimate deployments, plus two nits: 1. The scheme check (https, or http on a loopback host) rejected a same-origin card using plain http on a non-loopback host (e.g. http://agents.internal:8080 sourcing a card whose RPC url is the exact same origin) -- a genuine second policy bundled into the same throw as the origin pin, with no way to opt out even when an operator knows and accepts the tradeoff for an internal host. Added ResolveAgentCardOptions.allowInsecureRpc, matching the allowUnauthenticated pattern already established in agent_to_a2a.ts's ToA2aOptions: defaults to false (fails closed), logs a loud SECURITY WARNING when set to true, threaded through resolveAgentCard's new second parameter. 2. The origin-pin check itself had no opt-out, but the A2A spec only requires an RPC url to be "a valid absolute HTTPS URL in production" -- it does not require the same origin as the card. A card served from a static host that points RPC at a separate API backend is a legitimate, spec-compliant shape this made a hard failure with no escape hatch. Added ResolveAgentCardOptions.allowCrossOriginRpc, same pattern: defaults to false, warns loudly when enabled. Threaded resolveAgentCardOptions through both real callers of resolveAgentCard (RemoteA2AAgentConfig in a2a_remote_agent.ts, and ToA2aOptions in agent_to_a2a.ts) so either escape hatch is reachable without editing the shared validation function directly. 3. isLoopbackHost missed 0.0.0.0 and :: (IPv6 wildcard), and even where it did recognize both sides as loopback, the origin comparison was a plain string comparison that treated different loopback hostnames as different origins. Reviewer verified with a scratch script that this broke three shapes already in use: toA2a defaults host to localhost while a card might declare 127.0.0.1; adk deploy passes --host=0.0.0.0, and a request to 0.0.0.0:<port> does reach that listener; and the equivalent IPv6 wildcard case. Added 0.0.0.0 and :: to isLoopbackHost. Replaced the origin comparison with sameOriginForRpc(): when both source and RPC url are loopback, compares scheme and port only (added a port normalization helper, effectivePort(), so an omitted default port and an explicit one, e.g. http://host vs http://host:80, aren't treated as a mismatch -- an edge case not in the original review but caught while implementing the fix and verified against it directly); otherwise compares full origin as before. Verified the full comparison logic against a standalone script covering both the three newly-fixed cases and every case from the review's original scratch-script probe (scheme confusion, userinfo, subdomain prefixes, IDN homoglyphs) before touching the real implementation, confirming no regression on the cases that already held. 4. Removed the now-redundant urlOrigin() helper (parsed a URL that had already been parsed one line above) in favor of using the existing URL object's own .origin directly; the one other call site (validating the source URL itself) was inlined the same way. 5. Added the two missing regression tests: a same-origin http:// url on a non-loopback host (using a non-loopback IPv4 interface discovered dynamically at test time via os.networkInterfaces(), with a graceful skip if the environment has none, rather than hardcoding an address that might not be bindable in every CI environment), and a card with an empty url. Both confirmed to throw with the expected message. Also added: explicit test coverage for all three loopback-comparison fixes (localhost/127.0.0.1, 0.0.0.0/0.0.0.0, IPv6 wildcard -- the IPv6 case detects at runtime whether this environment's kernel supports binding to :: at all and skips gracefully if not, since not every sandbox does), a still-rejects-when-unset test for each new escape hatch (pinning that both default to failing closed), and a still-rejects-differing-port test for the loopback comparison itself (pinning that only the specific cases described above are relaxed, not loopback origins generally). Fixed a real regression this introduced in an unrelated existing test: agent_to_a2a_test.ts asserted resolveAgentCard was called with exactly one argument via toHaveBeenCalledWith, which is strict about argument count; the new resolveAgentCardOptions parameter (undefined in those two calls, since neither test sets it) broke that strict match even though the call is still correct. Updated both assertions to match the accurate current call shape, and added a new test confirming resolveAgentCardOptions is actually forwarded to resolveAgentCard when a caller does set it, which neither existing test covered. Verified: full agent_card_test.ts (23/23), full a2a/ directory (16 files, 252/252), and the full unit:core suite (230 files, 3449/3449) all pass. tsc --emitDeclarationOnly + bundler build clean.
|
Related to #832 |
|
Hi @AmaadMartin @kalenkevich, |
|
Hi @AmaadMartin @kalenkevich, |
|
@prasanna8585 could you please rebase on the current main? |
|
Hi @kalenkevich, |
|
@prasanna8585 CI seems to be failed |
|
@kalenkevich Apologies for the inconvenience. Could you please check this now? |
Almost done, linters failing now. |
Collapse agent_card.js import to one line (prettier), export type ResolveAgentCardOptions referenced by public configs (typedoc), and stop linking the internal validateCardRpcTargets from public docs.
|
Fixed. The format check failure is resolved, along with two typedoc docs:check warnings that the newly exported ResolveAgentCardOptions type would have triggered next. All validation gates (build, ts:check, lint, format:check, docs:check, a2a tests, license, secretlint) pass locally |
resolveAgentCard() fetches an agent card over HTTP(S) and returns it without validating that the card's declared RPC URL(s) match the origin it was fetched from.
An agent card is JSON returned by whatever server answers the configured card-source request. If that server is compromised, misconfigured, or intercepted, it can return a card declaring an RPC URL pointing to a completely different, attacker-controlled origin. Without this check, that URL is accepted as-is - every subsequent A2A request for that agent, including any credential material the request-forwarding path carries, would be sent to the attacker's origin instead of the intended one.
This fix validates every RPC URL the card offers (the primary url field and each entry in additionalInterfaces, not only whichever one a given transport negotiation selects):
Each must be https, or http on a loopback host.
Each must share the origin the card was fetched from.
A card provided directly as an object, or read from a local file, is exempt, since that content did not come from the network in this step.
Testing: Added regression tests using a real local HTTP server to exercise the actual fetch path. Confirmed legitimate same-origin cards (including multi-interface cards) resolve correctly, while cards declaring an off-origin URL - on the primary field or on any additional interface - are rejected. Full existing a2a test suite passes with no regressions.