Skip to content

Commit de5b6e8

Browse files
committed
fix(code_executors): harden ContainerCodeExecutor sandbox by default
ContainerCodeExecutor runs model-generated code, which can be influenced by untrusted input (e.g. via prompt injection). It previously started the container with default Docker networking and no capability restrictions, so the executed code could reach the cloud metadata endpoint (169.254.169.254) and exfiltrate the host service-account credentials, reach internal services, or escalate privileges. Start the container with networking disabled (configurable via a new `network_disabled` field, default True), drop all Linux capabilities, and forbid privilege escalation -- aligning with the isolation posture of GkeCodeExecutor and the managed executors. Add unit tests covering the hardened defaults and the opt-in network path.
1 parent 4100a24 commit de5b6e8

2 files changed

Lines changed: 86 additions & 0 deletions

File tree

src/google/adk/code_executors/container_code_executor.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,25 @@
3737
class ContainerCodeExecutor(BaseCodeExecutor):
3838
"""A code executor that uses a custom container to execute code.
3939
40+
Security note: this executor runs model-generated code, which may be
41+
influenced by untrusted input (e.g. via prompt injection). By default the
42+
container is started with networking disabled and all Linux capabilities
43+
dropped so that the executed code cannot reach the network (including the
44+
cloud metadata endpoint at ``169.254.169.254``) or escalate privileges. For
45+
stronger, kernel-level isolation of untrusted code prefer
46+
``GkeCodeExecutor`` (gVisor) or a managed executor
47+
(``VertexAiCodeExecutor`` / ``AgentEngineSandboxCodeExecutor``).
48+
4049
Attributes:
4150
base_url: Optional. The base url of the user hosted Docker client.
4251
image: The tag of the predefined image or custom image to run on the
4352
container. Either docker_path or image must be set.
4453
docker_path: The path to the directory containing the Dockerfile. If set,
4554
build the image from the dockerfile path instead of using the predefined
4655
image. Either docker_path or image must be set.
56+
network_disabled: Whether to start the container with networking disabled.
57+
Defaults to True. Set to False only if the executed code must make
58+
network requests and you trust it.
4759
"""
4860

4961
base_url: Optional[str] = None
@@ -64,6 +76,17 @@ class ContainerCodeExecutor(BaseCodeExecutor):
6476
predefined image. Either docker_path or image must be set.
6577
"""
6678

79+
network_disabled: bool = True
80+
"""
81+
Whether to start the code execution container with networking disabled.
82+
83+
Defaults to True so that untrusted, model-generated code cannot reach the
84+
network -- in particular the cloud metadata endpoint at 169.254.169.254
85+
(which can yield the host's service-account credentials), internal services,
86+
or arbitrary exfiltration destinations. Set to False only if the executed
87+
code must make network requests and you trust it.
88+
"""
89+
6790
# Overrides the BaseCodeExecutor attribute: this executor cannot be stateful.
6891
stateful: bool = Field(default=False, frozen=True, exclude=True)
6992

@@ -183,6 +206,13 @@ def __init_container(self):
183206
image=self.image,
184207
detach=True,
185208
tty=True,
209+
# Harden the sandbox for untrusted, model-generated code: no network
210+
# (blocks metadata/SSRF/exfil), drop all Linux capabilities, and
211+
# forbid privilege escalation. Networking can be re-enabled via
212+
# `network_disabled=False` when the executed code is trusted.
213+
network_disabled=self.network_disabled,
214+
cap_drop=['ALL'],
215+
security_opt=['no-new-privileges'],
186216
)
187217
logger.info('Container %s started.', self._container.id)
188218

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Tests for the ContainerCodeExecutor container hardening defaults."""
16+
17+
from unittest import mock
18+
19+
from google.adk.code_executors.container_code_executor import ContainerCodeExecutor
20+
21+
22+
def _mock_docker_client():
23+
"""Returns a mock Docker client whose container passes python verification."""
24+
client = mock.MagicMock()
25+
container = mock.MagicMock()
26+
# `_verify_python_installation` runs `exec_run(['which', 'python3'])` and
27+
# checks `exit_code == 0`.
28+
container.exec_run.return_value = mock.MagicMock(exit_code=0)
29+
client.containers.run.return_value = container
30+
return client
31+
32+
33+
@mock.patch('google.adk.code_executors.container_code_executor.docker')
34+
def test_container_is_hardened_by_default(mock_docker):
35+
client = _mock_docker_client()
36+
mock_docker.from_env.return_value = client
37+
38+
ContainerCodeExecutor(image='test-image')
39+
40+
_, kwargs = client.containers.run.call_args
41+
# Untrusted model-generated code must not be able to reach the network
42+
# (e.g. the cloud metadata endpoint) or escalate privileges by default.
43+
assert kwargs['network_disabled'] is True
44+
assert kwargs['cap_drop'] == ['ALL']
45+
assert kwargs['security_opt'] == ['no-new-privileges']
46+
47+
48+
@mock.patch('google.adk.code_executors.container_code_executor.docker')
49+
def test_container_network_can_be_explicitly_enabled(mock_docker):
50+
client = _mock_docker_client()
51+
mock_docker.from_env.return_value = client
52+
53+
ContainerCodeExecutor(image='test-image', network_disabled=False)
54+
55+
_, kwargs = client.containers.run.call_args
56+
assert kwargs['network_disabled'] is False

0 commit comments

Comments
 (0)