Repository navigation
fix(code-executors): isolate ContainerCodeExecutor state per session - #887
Merged
kalenkevich merged 3 commits intoOct 13, 2026
Merged
kalenkevich merged 3 commits into
kalenkevich merged 3 commits into
Conversation
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.
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. |
kalenkevich
approved these changes
Sep 10, 2026
kalenkevich
reviewed
Sep 10, 2026
kalenkevich
left a comment
Collaborator
There was a problem hiding this comment.
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.
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. |
Contributor
Author
|
@kalenkevich are we ready to merge this PR? |
kalenkevich
approved these changes
Oct 13, 2026
kalenkevich
approved these changes
Oct 13, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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: