Repository navigation
fix(a2a): stop restoring event branch from A2A peer metadata - #606
Conversation
createAdkEventFromMetadata() restored `branch` straight from a remote
A2A peer's own response metadata (adk_branch), unlike `author` which is
always force-set by the caller. getContents() (content_processor_utils.ts)
uses an event's branch to keep sibling sub-agent conversation contexts
isolated from each other (a branch is visible in a given context only if
it is an ancestor of, or equal to, that context's current branch).
A malicious or compromised remote peer delegated a sub-task therefore
had two ways to break that isolation and inject its response into an
unrelated sibling sub-agent's LLM context:
- setting adk_branch to a shared ancestor branch (e.g. the parent
coordinator's branch instead of its own), or
- omitting adk_branch entirely, which the filter treats as "always
visible, in every branch".
This is the same class of bug fixed in google#596 for actions.transferToAgent
(peer-controlled metadata able to corrupt local orchestrator state), on
a field that fix's allowlist didn't cover.
Fix: stop restoring `branch` in createAdkEventFromMetadata at all, and
thread it as an explicit parameter through toAdkEvent and its internal
per-event-type helpers instead, mirroring how `author` is already
force-set by the caller rather than trusted from peer metadata. The one
caller, A2ARemoteAgent.runAsyncImpl, now passes its own
InvocationContext.branch.
AmaadMartin
left a comment
There was a problem hiding this comment.
Read the whole thing and verified the analysis rather than taking the description on trust — it holds up on every point I checked, which is not the norm for a security write-up this confident:
content_processor_utils.ts:73-78only skips an event whenevent.branchis truthy, andisSegmentPrefixindependently returnstruefor a falsytargetBranch. So both attack vectors described are real, and the "omitadk_branch" one is the nastier of the two.- Both call sites are covered —
toAdkEventhas exactly two callers repo-wide, both ina2a_remote_agent.ts, both updated here. No missed sibling. - The parity claim checks out: adk-python sets
branch=invocation_context.branch if invocation_context else Noneat all three converter sites and never readsadk_branchback. This is a port regression, as described. - #596 is merged and is genuinely the same class of bug.
I also want to flag something you did that usually gets flagged: dropping the branch assertion from the existing populates AdkEvent fields from metadata test. Rewriting an existing test normally destroys regression signal and I'd push back on it — but that test encoded the vulnerable behaviour, which is exactly the case where changing it is right, and you called it out explicitly in the description with the #596 precedent. That is the correct way to handle it.
Two findings below: one I'd want addressed before merge, one that is a follow-up rather than a blocker.
CI: the macOS failure is app_loader_test.ts timing out at 40s, and the Windows cancellation is fail-fast from it. That test is a known flake on this repo (it took down an unrelated PR earlier this week) and this change touches nothing near it. I'm re-running it — not something you need to chase.
| event: A2AEvent, | ||
| invocationId: string, | ||
| agentName: string, | ||
| branch?: string, |
There was a problem hiding this comment.
Not a nit. Make this required rather than optional.
branch?: string,The optional marker leaves the insecure outcome as the default-on-omission, and it is the same outcome as one of the two attack vectors this PR fixes. I checked the filter rather than assuming:
// content_processor_utils.ts:73-78
if (
currentBranch &&
event.branch && // <- falsy branch short-circuits
!isSegmentPrefix(currentBranch, event.branch)
) { /* skip */ }An event with no branch is never skipped, so it is visible in every branch — strictly more permissive than a forged shared-ancestor branch. Your own description names this as attack vector two ("or just omit adk_branch"), and your new test pins it: never restores branch from peer-supplied metadata, even without a caller-supplied branch asserts branch comes back undefined.
So after this fix a peer can no longer cause that state, but a caller still can, silently, by forgetting the fourth argument. For a field whose whole job is isolation, that is worth making impossible rather than merely unlikely.
The cost is zero here: toAdkEvent is not exported from core/src/common.ts or core/src/index.ts — it is internal, with exactly the two call sites you already updated. So there is no public-API break.
branch: string | undefined,Required-but-nullable forces every call site to state its intent, and a future caller that genuinely has no branch has to write undefined and think about it for a second. The same argument applies to the five *ToAdkEvent helpers below, though those are module-private so they matter less.
There was a problem hiding this comment.
No, this is expected, branch can be undefined
| const metadata = a2aEvent.metadata || {}; | ||
|
|
||
| return createEvent({ | ||
| branch: metadata[A2AMetadataKeys.BRANCH] as string, | ||
| // `branch` is intentionally NOT restored from peer metadata here (unlike | ||
| // the other fields below): it is the mechanism getContents() (see | ||
| // content_processor_utils.ts) uses to keep sibling sub-agent branches' | ||
| // conversation contexts isolated from each other. A remote A2A peer that | ||
| // controls its own outgoing metadata could otherwise forge `adk_branch` | ||
| // (set it to a shared ancestor branch, or omit it) to leak its content | ||
| // into an unrelated sibling agent's LLM context. Every caller of the | ||
| // `*ToAdkEvent` functions in this file force-sets `branch` from its own | ||
| // local `InvocationContext` instead, the same way `author` is handled. |
There was a problem hiding this comment.
Not a blocker, and probably a separate PR — but this change makes adk_branch write-only, which is worth deciding about deliberately rather than by omission.
// `branch` is intentionally NOT restored from peer metadata here (unlike
// the other fields below): ...The comment is good — it explains the why at the exact place someone would otherwise "helpfully" restore the field, which is where it needs to be.
The loose end is the outbound side. metadata_converter_utils.ts:88 still writes it on every message we send:
[A2AMetadataKeys.BRANCH]: adkEvent.branch,After this PR nothing reads it: not adk-js (you removed the only reader), and not adk-python either — its converters set branch from invocation_context and never look at the metadata key. So we now transmit our internal agent-tree topology to every remote peer and no implementation on either side consumes it.
That is the mirror image of the bug you fixed: harmless in the sense that it can't corrupt local state, but it hands a peer the shape of our sub-agent hierarchy (branch strings like coordinator.sub_agent_a are exactly the names) in exchange for nothing. Given the threat model in your description is a malicious or compromised peer, that seems worth closing too.
Not asking you to do it here — dropping a metadata key is a wire-format change and deserves its own PR and its own compatibility argument. Worth an issue so it does not get lost, since this PR is what makes the field vestigial.
|
CI follow-up: green now on the same head ( The two review findings still stand on their own merits — the |
| event: A2AEvent, | ||
| invocationId: string, | ||
| agentName: string, | ||
| branch?: string, |
There was a problem hiding this comment.
No, this is expected, branch can be undefined
) createAdkEventFromMetadata() restored `branch` straight from a remote A2A peer's own response metadata (adk_branch), unlike `author` which is always force-set by the caller. getContents() (content_processor_utils.ts) uses an event's branch to keep sibling sub-agent conversation contexts isolated from each other (a branch is visible in a given context only if it is an ancestor of, or equal to, that context's current branch). A malicious or compromised remote peer delegated a sub-task therefore had two ways to break that isolation and inject its response into an unrelated sibling sub-agent's LLM context: - setting adk_branch to a shared ancestor branch (e.g. the parent coordinator's branch instead of its own), or - omitting adk_branch entirely, which the filter treats as "always visible, in every branch". This is the same class of bug fixed in google#596 for actions.transferToAgent (peer-controlled metadata able to corrupt local orchestrator state), on a field that fix's allowlist didn't cover. Fix: stop restoring `branch` in createAdkEventFromMetadata at all, and thread it as an explicit parameter through toAdkEvent and its internal per-event-type helpers instead, mirroring how `author` is already force-set by the caller rather than trusted from peer metadata. The one caller, A2ARemoteAgent.runAsyncImpl, now passes its own InvocationContext.branch.
createAdkEventFromMetadata()(core/src/a2a/event_converter_utils.ts) restoredbranchstraight from a remote A2A peer's own response metadata (adk_branch).authordoesn't have this problem because every caller already force-sets it from its own side, but nothing did the same forbranch.getContents()(core/src/agents/processors/content_processor_utils.ts) uses an event'sbranchto keep sibling sub-agent conversation contexts isolated: an event is only visible in a given branch's context if its own branch is an ancestor of (or equal to) that context's current branch (isSegmentPrefix,core/src/utils/branch_trie.ts). So a malicious or compromised remote A2A peer delegated a sub-task had two ways to break that isolation and inject its response into an unrelated sibling sub-agent's LLM context: setadk_branchto a shared ancestor branch (the parent coordinator's branch instead of its own), or just omitadk_branch, which the filter treats as "visible in every branch" since a falsyevent.branchshort-circuits the exclusion check entirely.This is the same class of bug fixed in #596 for
actions.transferToAgent, peer-controlled metadata able to corrupt local orchestrator state, just on a field that fix's allowlist didn't cover, since it only scopedEventActionsand not the rest ofcreateAdkEventFromMetadata's restored fields.It's also a port regression rather than a new gap. adk-python's equivalent converters (
convert_a2a_message_to_event,convert_a2a_task_to_event, etc. insrc/google/adk/a2a/converters/event_converter.py) never restorebranchfrom the A2A event at all. Every one of them setsbranch=invocation_context.branch if invocation_context else Noneand documents that the branch comes from the local context. adk-js applied that same "always local, never peer" treatment toauthorbut never tobranch.Confirmed by direct execution before this fix: a crafted A2A message with
metadata: {adk_branch: 'coordinator'}from a "sub_agent_a" peer, run throughtoAdkEvent()thengetContents()for an unrelated siblingsub_agent_b, produced a non-empty context containing the forged content.Fix
Stops restoring
branchincreateAdkEventFromMetadataat all, and threads it as an explicit parameter throughtoAdkEventand its internal per-event-type helpers instead, mirroring howauthoris already force-set by the caller rather than trusted from peer metadata. The one caller,A2ARemoteAgent.runAsyncImpl(core/src/a2a/a2a_remote_agent.ts), now passes its ownInvocationContext.branchat both the streaming and non-streaming call sites.Regression tests added in
core/test/a2a/event_converter_utils_test.ts(peer-forgedadk_branchis dropped, caller-supplied branch wins) andcore/test/a2a/remote_agent_test.ts(end to end: a forgedadk_branchfrom a mocked remote peer never reaches the emitted event, for both the streaming and non-streaming paths). Also updated the one pre-existing test that asserted the old vulnerable behavior,event_converter_utils_test.ts's "populates AdkEvent fields from metadata", the same situation #596's own PR description flagged for its field.Local verification:
npm run build --workspace=coreclean,vitest run core/test/(2360/2360 pass, whole core workspace, not just the touched files),npx eslinton all 4 touched files clean, repo-wideprettier --checkclean,npx secretlint "**/*"clean.