Skip to content

fix(code-executors): isolate ContainerCodeExecutor state per session - #887

Merged
kalenkevich merged 3 commits into
google:mainfrom
prasanna8585:fix/container-code-executor-session-isolation
Oct 13, 2026
Merged

kalenkevich merged 3 commits into
google:mainfrom
prasanna8585:fix/container-code-executor-session-isolation

Conversation

@prasanna8585

@prasanna8585 prasanna8585 commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Security fix. ContainerCodeExecutor started exactly one Docker container per executor instance and reused it, unconditionally, for every call to executeCode(). codeExecutor is a single property set once on an LlmAgent instance (agent_config.codeExecutor), and that LlmAgent instance is the long-lived object serving every session the agent handles for the lifetime of the process -- it is not created fresh per session or per request. In any deployment where one agent instance serves more than one user (the standard way a hosted, multi-tenant ADK agent runs), every session's model-generated code ran inside the exact same container, sharing its filesystem, environment, and process namespace with no reset or cleanup step between calls.

Concretely: if one session's code wrote a file (e.g. to /tmp), set an environment variable, or left a background process running, a later, completely unrelated session on the same agent could read that file, see that environment variable, or be affected by that process, simply by executing any code at all -- no special privilege or timing was needed, only that the two sessions happened to share the same agent instance, which is the default, expected way ADK agents are deployed.

The class's own security-note doc comment already reasoned carefully about isolating the container from the host (networking disabled, all Linux capabilities dropped, no-new-privileges set) with an explicit threat model naming prompt-injected, untrusted model-generated code. That reasoning did not extend to isolating one caller from the next caller of the same container -- this fix closes that gap without touching any of the existing host-isolation behavior.

Fix: key the container by session instead of holding a single shared one. ContainerCodeExecutor now maintains a Map<string, DockerContainer> and a matching Map<string, Promise> for lazy per-key initialization, keyed on JSON.stringify([appName, userId, session.id]) taken from ExecuteCodeParams.invocationContext.session. Each session gets its own container, lazily started on that session's first call (preserving the original design's stated goal of avoiding a fresh container-startup cost on every single call -- the cost is now paid once per session instead of never, rather than once per call). close() now stops every session's container, not just one.

Verified:

  • All 4 existing describe blocks / 30 existing tests pass unchanged after updating the shared makeParams() test helper to supply a realistic session (appName/userId/id) instead of an empty object cast, which the fix now reads.
  • Added a new 'per-session container isolation' describe block with 4 tests: a different session starts a genuinely separate container (asserted via createContainer's own call count, dockerode's actual container-provisioning call, not a higher-level proxy for it); the same session across multiple calls still reuses one container (the original performance property is preserved, not just the fix); and close() stops every tracked session's container, not only the most recent one.
  • Ran the full unit:core suite after the change: 253 files, 3803 tests, all passing, confirming no regression elsewhere in the codebase.
  • Confirmed with a standalone script (not included in this diff) that, pre-fix, two sequential executeCode() calls under different sessions against the same executor instance issue exactly one createContainer call and route both sessions' code through the same container with no cleanup step between them; post-fix, the same script issues two createContainer calls, one per session.

Security fix. ContainerCodeExecutor started exactly one Docker container
per executor instance and reused it, unconditionally, for every call to
executeCode(). codeExecutor is a single property set once on an LlmAgent
instance (agent_config.codeExecutor), and that LlmAgent instance is the
long-lived object serving every session the agent handles for the
lifetime of the process -- it is not created fresh per session or per
request. In any deployment where one agent instance serves more than one
user (the standard way a hosted, multi-tenant ADK agent runs), every
session's model-generated code ran inside the exact same container,
sharing its filesystem, environment, and process namespace with no reset
or cleanup step between calls.

Concretely: if one session's code wrote a file (e.g. to /tmp), set an
environment variable, or left a background process running, a later,
completely unrelated session on the same agent could read that file, see
that environment variable, or be affected by that process, simply by
executing any code at all -- no special privilege or timing was needed,
only that the two sessions happened to share the same agent instance,
which is the default, expected way ADK agents are deployed.

The class's own security-note doc comment already reasoned carefully
about isolating the container from the host (networking disabled, all
Linux capabilities dropped, no-new-privileges set) with an explicit
threat model naming prompt-injected, untrusted model-generated code. That
reasoning did not extend to isolating one caller from the next caller of
the same container -- this fix closes that gap without touching any of
the existing host-isolation behavior.

Fix: key the container by session instead of holding a single shared
one. ContainerCodeExecutor now maintains a Map<string, DockerContainer>
and a matching Map<string, Promise<void>> for lazy per-key
initialization, keyed on JSON.stringify([appName, userId, session.id])
taken from ExecuteCodeParams.invocationContext.session. Each session
gets its own container, lazily started on that session's first call
(preserving the original design's stated goal of avoiding a fresh
container-startup cost on every single call -- the cost is now paid once
per session instead of never, rather than once per call). close() now
stops every session's container, not just one.

Verified:
- All 4 existing describe blocks / 30 existing tests pass unchanged
  after updating the shared makeParams() test helper to supply a
  realistic session (appName/userId/id) instead of an empty object cast,
  which the fix now reads.
- Added a new 'per-session container isolation' describe block with 4
  tests: a different session starts a genuinely separate container
  (asserted via createContainer's own call count, dockerode's actual
  container-provisioning call, not a higher-level proxy for it); the
  same session across multiple calls still reuses one container (the
  original performance property is preserved, not just the fix); and
  close() stops every tracked session's container, not only the most
  recent one.
- Ran the full unit:core suite after the change: 253 files, 3803 tests,
  all passing, confirming no regression elsewhere in the codebase.
- Confirmed with a standalone script (not included in this diff) that,
  pre-fix, two sequential executeCode() calls under different sessions
  against the same executor instance issue exactly one createContainer
  call and route both sessions' code through the same container with no
  cleanup step between them; post-fix, the same script issues two
  createContainer calls, one per session.
@Varun-S10

Copy link
Copy Markdown
Contributor

Hi @prasanna8585, Thank you for raising this PR. We truly appreciate your contribution. The PR will be reviewed. Thank you for your patience and understanding.

@Varun-S10 Varun-S10 added the needs review [Status] The PR/issue is awaiting review from the maintainer label Sep 10, 2026

@kalenkevich kalenkevich left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please update failing tests as well

…tion test

Addresses kalenkevich's review: the real-Docker integration test
(tests/integration/code_executors/container_code_executor_integration_test.ts,
gated behind ADK_RUN_DOCKER_IT and run in CI via
.github/workflows/container-code-executor.yml) still built its
ExecuteCodeParams with `invocationContext: {} as unknown as
InvocationContext` -- the same shape the unit:core test helper used
before this PR, which now needs a real `session` object since
executeCode() keys the per-session container off
`invocationContext.session.{appName,userId,id}`.

This file is a separate test project (`integration`) from the one
fixed earlier in this PR (`unit:core`), gated behind a real Docker
daemon, so it never ran in my own local verification (no Docker
available there) and only surfaced in CI.

Verified: tsc --noEmit -p . reports no new errors introduced by this
change (the 4 errors it reports are pre-existing on main, confirmed by
diffing tsc's output on main against this branch), and
`npx prettier --check` passes on the modified file. Grepped the full
repo for every remaining reference to ContainerCodeExecutor outside
core/src and core/dist to confirm no other test file has the same gap:
core/test/code_executors/container_code_executor_test.ts and
docker_container_test.ts were already covered, and the two
non-test references (core/src/index.ts, dev/src/utils/agent_loader.ts)
are just an export and a comment, not calls.
@prasanna8585

Copy link
Copy Markdown
Contributor Author

Good catch pushed a fix. The Docker integration test's makeParams helper still built an empty invocationContext with no session, same gap I'd already fixed in the unit test but missed here since this file only runs under ADK_RUN_DOCKER_IT in CI.

@prasanna8585

prasanna8585 commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor Author

@kalenkevich are we ready to merge this PR?

@kalenkevich
kalenkevich merged commit 7303749 into google:main Oct 13, 2026
14 checks passed
@kalenkevich kalenkevich mentioned this pull request Oct 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs review [Status] The PR/issue is awaiting review from the maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants