Skip to content

fix(a2a): harden agent-card resolution against SSRF and a silent file read - #832

Merged
AmaadMartin merged 3 commits into
google:mainfrom
AmaadMartin:fix/a2a-agent-card-ssrf-guard
Sep 9, 2026
Merged

AmaadMartin merged 3 commits into
google:mainfrom
AmaadMartin:fix/a2a-agent-card-ssrf-guard

Conversation

@AmaadMartin

Copy link
Copy Markdown
Collaborator

Please ensure you have read the contribution guide before creating a pull request.

Link to Issue or Description of Change

  1. Link to an existing issue (if applicable): none; reported through a security review of the A2A client.
  2. Description of the change:
    Problem: resolveAgentCard read any source that did not start with http:// or https:// off the local filesystem, so file:///etc/passwd or a typo like htp://host/card.json became an arbitrary file read. The fetch had no address check, so http://169.254.169.254/ reached the cloud metadata endpoint. The fetch also followed redirects, so a card host could move the request to that endpoint itself.

Solution: A card source now resolves by scheme. http(s) fetches, file:// and a bare filesystem path read, and any other scheme throws instead of falling through to fs.readFile. The fetch refuses a host that is or resolves to a link-local address, and refuses a redirect rather than following it. Loopback and private addresses stay allowed, because that is where a locally served or VPC-internal peer agent lives, and blocking them would break the shape toA2a() emits.

Four notes for the reviewer.

file:// sources are newly supported, which is a small behavior addition rather than a fix. On main a file:// source reached fs.readFile('file:///...') and always failed. Now that a URL-shaped source can no longer fall through to a file read, a developer who wants a local card needs a spelling the guard accepts, and this is it. A bare filesystem path keeps working unchanged.

The address checks already existed in core/src/tools/load_web_page.ts. They move to core/src/utils/ssrf_guard.ts rather than being duplicated, which is most of the line count. loadWebPage's 44 existing tests pass unmodified.

The move tightens loadWebPage once, deliberately: an IPv6 address that embeds a non-global IPv4 through NAT64 (64:ff9b::/96), 6to4 (2002::/16) or the IPv4-compatible form is now blocked. http://[64:ff9b::a9fe:a9fe]/ is 64:ff9b::169.254.169.254 and routes to the metadata endpoint on a NAT64 network. adk-python's _embedded_ipv4 already does this; the TypeScript port only un-embedded the IPv4-mapped form.

The design spec asked for the host check twice, once before DefaultAgentCardResolver runs and once inside the injected fetchImpl. One check is enough, and it runs on the configured URL. The resolver appends a relative well-known path, which cannot change the origin, and the injected fetchImpl refuses a redirect, so nothing else can pick where the request lands.

Related work in this repo: #829 pins a fetched card's RPC url(s) to the origin it was fetched from. That is the complement of this change — #829 guards where a card points us next, this one guards where we fetch the card from. The two are independent and can land in either order; whichever is second will need a trivial rebase, as both touch core/src/a2a/agent_card.ts and its test.

This change is not stacked on anything: it applies directly to main.

Testing Plan

Please describe the tests that you ran to verify your changes. This is required for all PRs that are not small documentation or typo fixes.
Unit Tests:
[x] I have added or updated unit tests for my change.
[x] All unit tests pass locally.

npx vitest run --project unit:core core/test/a2a/agent_card_test.ts core/test/utils/ssrf_guard_test.ts core/test/tools/load_web_page_test.ts
  Test Files  3 passed (3)   Tests  121 passed (121)
npx vitest run --project unit:core core/test/a2a/
  Test Files 16 passed (16)  Tests  256 passed (256)
npx vitest run --project integration tests/integration/a2a/
  Test Files  3 passed (3)   Tests  6 passed (6)
npm run ts:check      # clean
npm run lint          # clean
npm run format:check  # clean

Line and branch coverage of the new code is 100% (core/src/utils/ssrf_guard.ts 100/100, core/src/tools/load_web_page.ts 100/100). core/src/a2a/agent_card.ts reports 88%; every uncovered line is pre-existing buildAgentSkills code, none is in this change.

Each new test was run against mutated source to prove it can fail:

Mutation Tests that failed Failure message
Delete await assertHostAllowed(url) 5 expected [Function] to throw error matching /Refusing to fetch agent card from a l…/
redirect: 'manual' back to the default 1 expected "spy" to be called with arguments: [ Any<URL>, ObjectContaining{…} ]
Restore startsWith('http://') and the bare fs.readFile fallback 4 expected [Function] to throw error matching /Unsupported agent card URL scheme/ but got 'Failed to read agent card from file f…'
Drop the NAT64 arm of embeddedIpv4 4 expected false to be true
Block loopback and private too 3 fetches the card from the loopback URL http://localhost:8001/a2a/agent/
Drop the Windows drive-letter carve-out 1 expected [Function] to throw error matching /^Failed to read agent card from file …/ but got 'Unsupported agent card URL scheme "c:…'
Drop the status === 0 arm 1 expected [Function] to throw error matching /status 0, location null/

The loopback mutation is there on purpose: it pins the allowance as a decision, not an accident.

Manual End-to-End (E2E) Tests:
Please provide instructions on how to manually test your changes, including any necessary setup or configuration.

Run npm run build, then drive the built resolveAgentCard against a real local HTTP server with no mocks:

  1. resolveAgentCard('http://169.254.169.254/') throws Refusing to fetch agent card from a link-local address: 169.254.169.254. The server's connection counter stays at 0, so no socket opens.
  2. A card served over loopback resolves and returns the card.
  3. A server answering 302 Location: http://169.254.169.254/computeMetadata/v1/ throws Refusing to follow a redirect while fetching the agent card from … (status 302, location http://169.254.169.254/computeMetadata/v1/). Configure the final URL instead.
  4. resolveAgentCard('ftp://cards.example.com/card.json') throws Unsupported agent card URL scheme "ftp:" and reads no file.

All four behaved as listed. tests/integration/a2a/** also passes unmodified against a live local A2A server, which is the regression proof for local development.

Checklist

[x] I have read the CONTRIBUTING.md document.
[x] I have performed a self-review of my own code.
[x] I have commented my code, particularly in hard-to-understand areas.
[x] I have added tests that prove my fix is effective or that my feature works.
[x] New and existing unit tests pass locally with my changes.

Amaad Martin added 3 commits August 27, 2026 11:34
The agent-card resolver needs the same address checks that loadWebPage
already has, so the helpers move to core/src/utils/ssrf_guard.ts with a
narrow isLinkLocalAddress predicate added.

The move also tightens loadWebPage: an IPv6 address that embeds a
non-global IPv4 through NAT64, 6to4 or the IPv4-compatible form is now
blocked, matching adk-python's _embedded_ipv4.
…read

resolveAgentCard treated any source that did not start with http:// or
https:// as a filesystem path, so file:///etc/passwd and a typo like
htp://host/card.json became an arbitrary file read. The fetch also had no
address check and followed redirects, so a card host could move the
request to the cloud metadata endpoint.

A source now resolves by scheme: http(s) fetches, file:// and a bare path
read, and any other scheme throws. The fetch refuses a link-local host
and refuses a redirect instead of following it. Loopback and private
addresses stay allowed, because that is where a locally served or
VPC-internal peer agent lives.
…uched

The injected fetchImpl rebuilt the request through `new Request(input)`
just to read a URL, then refetched from that URL, which would have
dropped a real request's method, headers and body. It now forwards
`input` and only refuses the redirect.

The host check moves to the configured URL in resolveAgentCard. The
resolver appends a relative well-known path, which cannot change the
origin, so one check still covers the whole fetch.
@AmaadMartin
AmaadMartin merged commit 1bb1120 into google:main Sep 9, 2026
14 checks passed
@kalenkevich kalenkevich mentioned this pull request Sep 9, 2026
prasanna8585 pushed a commit to prasanna8585/adk-js that referenced this pull request Nov 10, 2026
… read (google#832)

* refactor(core): move the SSRF address guard into a shared util

The agent-card resolver needs the same address checks that loadWebPage
already has, so the helpers move to core/src/utils/ssrf_guard.ts with a
narrow isLinkLocalAddress predicate added.

The move also tightens loadWebPage: an IPv6 address that embeds a
non-global IPv4 through NAT64, 6to4 or the IPv4-compatible form is now
blocked, matching adk-python's _embedded_ipv4.

* fix(a2a): guard agent-card resolution against SSRF and a silent file read

resolveAgentCard treated any source that did not start with http:// or
https:// as a filesystem path, so file:///etc/passwd and a typo like
htp://host/card.json became an arbitrary file read. The fetch also had no
address check and followed redirects, so a card host could move the
request to the cloud metadata endpoint.

A source now resolves by scheme: http(s) fetches, file:// and a bare path
read, and any other scheme throws. The fetch refuses a link-local host
and refuses a redirect instead of following it. Loopback and private
addresses stay allowed, because that is where a locally served or
VPC-internal peer agent lives.

* refactor(a2a): check the card host once, and forward the request untouched

The injected fetchImpl rebuilt the request through `new Request(input)`
just to read a URL, then refetched from that URL, which would have
dropped a real request's method, headers and body. It now forwards
`input` and only refuses the redirect.

The host check moves to the configured URL in resolveAgentCard. The
resolver appends a relative well-known path, which cannot change the
origin, so one check still covers the whole fetch.

---------

Co-authored-by: Amaad Martin <[email protected]>
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.

2 participants