Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
import json
import time
from unittest.mock import Mock
import httpx
import pytest
from e2b import SandboxNetworkOpts
from e2b.sandbox.commands.command_handle import CommandExitException
def wait_for_status(
client: httpx.Client,
url: str,
status_code: int,
headers: dict[str, str] | None = None,
timeout: float = 15,
) -> httpx.Response:
deadline = time.monotonic() + timeout
response: httpx.Response | None = None
last_transport_error: httpx.TransportError | None = None
while time.monotonic() < deadline:
try:
response = client.get(url, headers=headers, follow_redirects=True)
except httpx.TransportError as error:
last_transport_error = error
else:
if response.status_code == status_code:
return response
time.sleep(1)
if response is not None:
return response
assert last_transport_error is not None
raise last_transport_error
def test_wait_for_status_retries_transport_errors():
response = httpx.Response(200)
client = Mock()
client.get.side_effect = [httpx.ConnectError("not ready"), response]
assert wait_for_status(client, "https://sandbox.test", 200) is response
assert client.get.call_count == 2
@pytest.mark.skip_debug()
def test_allow_specific_ip_with_deny_all(sandbox_factory):
"""Test that sandbox with denyOut all and allowOut creates a whitelist."""
sandbox = sandbox_factory(
network=SandboxNetworkOpts(
deny_out=lambda ctx: [ctx.all_traffic], allow_out=["1.1.1.1"]
)
)
# Test that allowed IP works
result = sandbox.commands.run(
"curl -s -o /dev/null -w '%{http_code}' https://1.1.1.1"
)
assert result.exit_code == 0
assert result.stdout.strip() == "301"
# Test that other IPs are denied
with pytest.raises(CommandExitException) as exc_info:
sandbox.commands.run(
"curl --connect-timeout 3 --max-time 5 -Is https://8.8.8.8"
)
assert exc_info.value.exit_code != 0
@pytest.mark.skip_debug()
def test_deny_specific_ip(sandbox_factory):
"""Test that sandbox with denyOut denies specified IP addresses."""
sandbox = sandbox_factory(network=SandboxNetworkOpts(deny_out=["8.8.8.8"]))
# Test that denied IP fails
with pytest.raises(CommandExitException) as exc_info:
sandbox.commands.run(
"curl --connect-timeout 3 --max-time 5 -Is https://8.8.8.8"
)
assert exc_info.value.exit_code != 0
# Test that other IPs work
result = sandbox.commands.run(
"curl -s -o /dev/null -w '%{http_code}' https://1.1.1.1"
)
assert result.exit_code == 0
assert result.stdout.strip() == "301"
@pytest.mark.skip_debug()
def test_deny_all_traffic(sandbox_factory):
"""Test that sandbox can deny all traffic using the all_traffic selector."""
sandbox = sandbox_factory(
network=SandboxNetworkOpts(deny_out=lambda ctx: [ctx.all_traffic]), timeout=30
)
# Test that all traffic is denied
with pytest.raises(CommandExitException) as exc_info:
sandbox.commands.run(
"curl --connect-timeout 3 --max-time 5 -Is https://1.1.1.1"
)
assert exc_info.value.exit_code != 0
with pytest.raises(CommandExitException) as exc_info:
sandbox.commands.run(
"curl --connect-timeout 3 --max-time 5 -Is https://8.8.8.8"
)
assert exc_info.value.exit_code != 0
@pytest.mark.skip_debug()
def test_allow_takes_precedence_over_deny(sandbox_factory):
"""Test that allowOut takes precedence over denyOut."""
sandbox = sandbox_factory(
network=SandboxNetworkOpts(
deny_out=lambda ctx: [ctx.all_traffic], allow_out=["1.1.1.1", "8.8.8.8"]
)
)
# Test that 1.1.1.1 works (explicitly allowed)
result1 = sandbox.commands.run(
"curl -s -o /dev/null -w '%{http_code}' https://1.1.1.1"
)
assert result1.exit_code == 0
assert result1.stdout.strip() == "301"
# Test that 8.8.8.8 also works (explicitly allowed, takes precedence over deny_out)
result2 = sandbox.commands.run(
"curl -s -o /dev/null -w '%{http_code}' https://8.8.8.8"
)
assert result2.exit_code == 0
assert result2.stdout.strip() == "302"
@pytest.mark.skip_debug()
def test_allow_public_traffic_false(sandbox_factory):
"""Test that sandbox with allow_public_traffic=False requires traffic access token."""
sandbox = sandbox_factory(network=SandboxNetworkOpts(allow_public_traffic=False))
# Verify the sandbox was created successfully and has a traffic access token
assert sandbox.traffic_access_token is not None
# Start a simple HTTP server in the sandbox
port = 8080
sandbox.commands.run(
f"python3 -m http.server {port}",
background=True,
)
# Wait for server to start
time.sleep(3)
# Get the public URL for the sandbox
sandbox_url = f"https://{sandbox.get_host(port)}"
with httpx.Client() as client:
# Test 1: Request without traffic access token should fail with 403
response = client.get(sandbox_url, follow_redirects=True)
assert response.status_code == 403
# Test 2: Request with valid traffic access token should succeed
headers = {"e2b-traffic-access-token": sandbox.traffic_access_token}
response = wait_for_status(client, sandbox_url, 200, headers=headers)
assert response.status_code == 200
@pytest.mark.skip_debug()
def test_allow_public_traffic_true(sandbox_factory):
"""Test that sandbox with allow_public_traffic=True works without token."""
sandbox = sandbox_factory(network=SandboxNetworkOpts(allow_public_traffic=True))
# Start a simple HTTP server in the sandbox
port = 8080
sandbox.commands.run(
f"python3 -m http.server {port}",
background=True,
)
# Wait for server to start
time.sleep(3)
# Get the public URL for the sandbox
sandbox_url = f"https://{sandbox.get_host(port)}"
with httpx.Client() as client:
# Request without traffic access token should succeed (public access enabled)
response = wait_for_status(client, sandbox_url, 200)
assert response.status_code == 200
@pytest.mark.skip_debug()
def test_firewall_transform_injects_headers(sandbox_factory, httpbin_template):
"""Test that a firewall rule with a transform injects headers into outbound requests."""
injected_header = "X-Test-Token"
injected_value = "e2b-transform-value-123"
# Port the httpbin template's start command listens on.
httpbin_port = 8080
# The transform is applied by the egress proxy on the way out of the
# sandbox, so the target has to be reachable from the public internet — a
# CI service container would not be. A sidecar sandbox running the httpbin
# template is that target, which keeps the test off any externally hosted
# service. Its ready command has already passed by the time create returns,
# so the server is serving.
httpbin = sandbox_factory(
template_name=httpbin_template,
network=SandboxNetworkOpts(allow_public_traffic=True),
)
httpbin_host = httpbin.get_host(httpbin_port)
network: SandboxNetworkOpts = {
"rules": {
httpbin_host: [
{"transform": {"headers": {injected_header: injected_value}}},
],
},
}
sandbox = sandbox_factory(network=network)
result = sandbox.commands.run(
f"curl -sS --retry 5 --retry-connrefused --max-time 10 "
f"https://{httpbin_host}/headers"
)
assert result.exit_code == 0
parsed = json.loads(result.stdout)
reflected = parsed["headers"].get(injected_header)
assert reflected == [injected_value], (
f"expected httpbin to reflect {injected_header}={injected_value}, "
f"got headers: {parsed['headers']}"
)
@pytest.mark.skip_debug()
def test_update_network_applies_restrictions(sandbox_factory):
"""update_network can add egress restrictions to a running sandbox."""
sandbox = sandbox_factory()
# Baseline: 8.8.8.8 reachable.
before = sandbox.commands.run(
"curl -s -o /dev/null -w '%{http_code}' https://8.8.8.8"
)
assert before.exit_code == 0
sandbox.update_network({"deny_out": ["8.8.8.8"]})
# 8.8.8.8 is now denied.
with pytest.raises(CommandExitException) as exc_info:
sandbox.commands.run(
"curl --connect-timeout 3 --max-time 5 -Is https://8.8.8.8"
)
assert exc_info.value.exit_code != 0
# Other destinations stay reachable.
result = sandbox.commands.run(
"curl -s -o /dev/null -w '%{http_code}' https://1.1.1.1"
)
assert result.exit_code == 0
@pytest.mark.skip_debug()
def test_update_network_clears_existing_rules(sandbox_factory):
"""update_network replaces all egress rules; omitted fields are cleared."""
sandbox = sandbox_factory(
network=SandboxNetworkOpts(
deny_out=lambda ctx: [ctx.all_traffic],
allow_out=["1.1.1.1"],
)
)
# Baseline from create-time config: 8.8.8.8 denied.
with pytest.raises(CommandExitException):
sandbox.commands.run(
"curl --connect-timeout 3 --max-time 5 -Is https://8.8.8.8"
)
# Empty update clears allow_out / deny_out entirely.
sandbox.update_network({})
r1 = sandbox.commands.run("curl -s -o /dev/null -w '%{http_code}' https://1.1.1.1")
assert r1.exit_code == 0
r2 = sandbox.commands.run("curl -s -o /dev/null -w '%{http_code}' https://8.8.8.8")
assert r2.exit_code == 0
def dns_probe_cmd(server: str) -> str:
"""Sends one DNS query for example.com to `server` over UDP. Fails if no reply comes back."""
return f"""python3 -c "
import socket
q = bytes.fromhex('abcd01000001000000000000076578616d706c6503636f6d0000010001')
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(3)
s.sendto(q, ('{server}', 53))
s.recv(512)
"
"""
DOMAIN_ONLY = SandboxNetworkOpts(
allow_out=["example.com"],
deny_out=lambda ctx: [ctx.all_traffic],
)
def skip_unless_dns_servers_supported(sandbox):
# These tests need an API that supports dns_servers. Call this on a sandbox
# created with dns_servers set: if get_info doesn't report them back, skip.
info = sandbox.get_info()
if info.network is None or "dns_servers" not in info.network:
pytest.skip("the API doesn't report dns_servers")
@pytest.mark.skip_debug()
def test_empty_dns_servers_allows_no_dns_server(sandbox_factory):
"""dns_servers=[] blocks 8.8.8.8; the allowed domain stays reachable."""
sandbox = sandbox_factory(network={**DOMAIN_ONLY, "dns_servers": []})
skip_unless_dns_servers_supported(sandbox)
with pytest.raises(CommandExitException):
sandbox.commands.run(dns_probe_cmd("8.8.8.8"))
with pytest.raises(CommandExitException):
sandbox.commands.run(
"curl --connect-timeout 3 --max-time 5 -Is https://8.8.8.8"
)
# Allowed domains are matched by name, not by IP, so any public IP in
# /etc/hosts works.
sandbox.commands.run("echo '1.1.1.1 example.com' >> /etc/hosts", user="root")
result = sandbox.commands.run(
"curl -s -o /dev/null -w '%{http_code}' https://example.com"
)
assert result.stdout.strip() == "200"
info = sandbox.get_info()
assert info.network is not None
assert info.network.get("dns_servers") == []
@pytest.mark.skip_debug()
def test_dns_servers_allows_listed_servers_instead_of_8888(sandbox_factory):
"""dns_servers=["1.1.1.1"] opens 1.1.1.1 and closes 8.8.8.8."""
sandbox = sandbox_factory(network={**DOMAIN_ONLY, "dns_servers": ["1.1.1.1"]})
skip_unless_dns_servers_supported(sandbox)
assert (sandbox.commands.run(dns_probe_cmd("1.1.1.1"))).exit_code == 0
with pytest.raises(CommandExitException):
sandbox.commands.run(dns_probe_cmd("8.8.8.8"))
info = sandbox.get_info()
assert info.network is not None
assert info.network.get("dns_servers") == ["1.1.1.1"]
@pytest.mark.skip_debug()
def test_update_network_resets_dns_servers_when_omitted(sandbox_factory):
"""Leaving dns_servers out of update_network resets it to 8.8.8.8."""
sandbox = sandbox_factory(network={**DOMAIN_ONLY, "dns_servers": []})
skip_unless_dns_servers_supported(sandbox)
network = {"allow_out": ["example.com"], "deny_out": ["0.0.0.0/0"]}
# Left out, so it goes back to the default (8.8.8.8).
sandbox.update_network(network)
assert (sandbox.commands.run(dns_probe_cmd("8.8.8.8"))).exit_code == 0
info = sandbox.get_info()
assert info.network is not None
assert "dns_servers" not in info.network
sandbox.update_network({**network, "dns_servers": []})
with pytest.raises(CommandExitException):
sandbox.commands.run(dns_probe_cmd("8.8.8.8"))
sandbox.update_network({**network, "dns_servers": ["8.8.8.8"]})
assert (sandbox.commands.run(dns_probe_cmd("8.8.8.8"))).exit_code == 0
@pytest.mark.skip_debug()
def test_domain_allowlist_allows_8888_by_default(sandbox_factory):
"""Without dns_servers, a domain allowlist still resolves through 8.8.8.8."""
sandbox = sandbox_factory(network=DOMAIN_ONLY)
assert (sandbox.commands.run(dns_probe_cmd("8.8.8.8"))).exit_code == 0
info = sandbox.get_info()
assert info.network is not None
assert "dns_servers" not in info.network
@pytest.mark.skip_debug()
def test_https_ports(sandbox_factory):
"""Test that a port listed in https_ports proxies to an HTTPS backend."""
port = 8443
sandbox = sandbox_factory(network=SandboxNetworkOpts(https_ports=[port]))
# Generate a self-signed certificate inside the sandbox
keygen = sandbox.commands.run(
"openssl req -x509 -newkey rsa:2048 "
"-keyout /tmp/https-backend-key.pem -out /tmp/https-backend-cert.pem "
'-days 1 -nodes -subj "/CN=localhost"'
)
assert keygen.exit_code == 0
# Start an HTTPS server on the configured port
sandbox.commands.run(
f"""python3 -c "
import http.server, ssl
class H(http.server.BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.end_headers()
self.wfile.write(b'https backend')
def log_message(self, *a): pass
server = http.server.ThreadingHTTPServer(('0.0.0.0', {port}), H)
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
context.load_cert_chain('/tmp/https-backend-cert.pem', '/tmp/https-backend-key.pem')
server.socket = context.wrap_socket(server.socket, server_side=True)
server.serve_forever()
" """,
background=True,
)
sandbox_url = f"https://{sandbox.get_host(port)}"
with httpx.Client() as client:
response = wait_for_status(client, sandbox_url, 200)
assert response.status_code == 200
assert response.text == "https backend"
# The port is reported back in the sandbox info
info = sandbox.get_info()
assert info.network is not None
assert info.network.get("https_ports") == [port]
@pytest.mark.skip_debug()
def test_mask_request_host(sandbox_factory):
"""Test that mask_request_host modifies the Host header correctly."""
sandbox = sandbox_factory(
network=SandboxNetworkOpts(mask_request_host="custom-host.example.com:${PORT}"),
timeout=60,
)
port = 8080
output_file = "/tmp/headers.txt"
# Start a Python HTTP server that captures request headers and writes them to a file
sandbox.commands.run(
f"""python3 -c "
import http.server, json
class H(http.server.BaseHTTPRequestHandler):
def do_GET(self):
with open('{output_file}', 'w') as f:
for k, v in self.headers.items():
f.write(k + ': ' + v + chr(10))
self.send_response(200)
self.end_headers()
def log_message(self, *a): pass
http.server.HTTPServer(('', {port}), H).serve_forever()
" """,
background=True,
)
time.sleep(2)
# Get the public URL for the sandbox
sandbox_url = f"https://{sandbox.get_host(port)}"
# Make a request from OUTSIDE the sandbox through the proxy
# The Host header should be modified according to mask_request_host
with httpx.Client() as client:
response = wait_for_status(client, sandbox_url, 200)
assert response.status_code == 200
# Read the captured headers from inside the sandbox
result = sandbox.commands.run(f"cat {output_file}")
# Verify the Host header was modified according to mask_request_host
assert "Host:" in result.stdout
assert "custom-host.example.com" in result.stdout
assert str(port) in result.stdout