Use an SDK patch to automate TrafficAccessToken rotation, manage token validity through sandbox-manager policies, and reduce the risk of long-lived credential exposure.
Background information
By default, dynamic token authentication based on JSON Web Token (JWT) issues a TrafficAccessToken that is valid for about 100 years, which is effectively a long-lived credential. This simplifies integration, but poses security risks in the following scenarios:
-
If a token is accidentally exposed in logs, network captures, or on the client side, the attack window is difficult to close on its own.
-
Enterprise compliance requirements, Multi-Level Protection Scheme (MLPS) assessments, and organizations in the financial and government sectors typically require short-lived tokens and mandatory rotation.
-
Long-lived tokens do not support phased revocation by tenant or workload.
Starting from ack-sandbox-manager add-on v0.6.12, TrafficAccessToken auto-rotation is available. Policies on the sandbox-manager side control the token validity period centrally. The SDK data plane (HTTP, RPC, and Jupyter) switches between the old and new tokens transparently, without the application code needing to be aware of it. For information about how to enable JWT-based authentication, see JWT-based dynamic token authentication.
How it works
Auto-rotation is handled jointly by the control plane, which issues tokens, and the data plane, which refreshes them:
-
Control plane (sandbox-manager):
sandbox-managerissues an initial JWT when it creates a sandbox, and continues to issue new tokens based on the configured validity policy. A newly issued token and the old token remain valid in parallel until the old JWT expires. This overlap prevents traffic interruptions during rotation. Concurrent refresh requests for the same sandbox are merged into a single issuance (in-flight coalescing). Refreshes for different sandboxes are independent of each other and protected by rate limiting. -
Data plane (SDK or client): After the auto-rotation patch is enabled, the SDK checks the remaining validity of the current JWT before each request. When the token is close to expiry, the SDK proactively pulls a new token from the control plane and automatically replaces the value of the subsequent
E2B-Traffic-Access-Tokenrequest header. The application code does not need to handle the refresh timing or retry on failure.
|
Item |
Default JWT authentication |
JWT authentication with auto-rotation |
|
Token validity period |
About 100 years (long-lived) |
Controlled by the |
|
Token refresh |
Not refreshed. The token is issued once at creation. |
Refreshed automatically by the SDK without requiring application changes. |
|
Scenarios |
Quick integration and internal verification |
Production environments, compliance audits, and short-lived credential requirements |
|
Failure response |
HTTP |
HTTP |
Applicable scope
-
The
ack-sandbox-manageradd-on is upgraded to v0.6.12 or later on the Add-ons page of the cluster. For more information, see Manage add-ons. -
JWT-based dynamic token authentication is enabled, and the identity provider is configured. For more information, see JWT-based dynamic token authentication.
-
The Python SDK used by the client meets the following version requirements:
-
e2b:>=2.35.0, <2.38.0 -
e2b-code-interpreter:>=2.9.0, <2.10.0
-
If you use an SDK in another language, or an SDK version outside the preceding ranges, keep the default long-lived token policy. Otherwise, the client loses access to the data plane after the token expires.
Enable the SDK auto-rotation patch
patch_traffic_access_token() is an optional patch, independent of the existing patch_e2b() patch (URL and protocol patches). Both patches can be enabled at the same time. Auto-rotation takes effect only after this patch is explicitly called.
Both patches come from the E2B SDK patch package (kruise_agents) provided by the OpenKruise Agents community.from kruise_agents.patch_traffic_token import patch_traffic_access_token
from e2b_code_interpreter import Sandbox
# Enable TrafficAccessToken auto-rotation (separate opt-in)
patch_traffic_access_token()
sbx = Sandbox.create(
template="code-interpreter",
metadata={"security.agents.kruise.io/enable-jwt-auth": "true"},
)
# All subsequent envd HTTP / RPC / Jupyter requests carry the latest JWT automatically
sbx.commands.run("echo hello-from-envd")
patch_traffic_access_token()covers both the synchronous and asynchronous call paths. Therefore, automatic token rotation also applies to application code that usesasyncio.
Carry the latest token in data plane requests
After the patch is enabled, the SDK reads the latest traffic_access_token from the sandbox instance and writes it to the E2B-Traffic-Access-Token request header before each data plane request. No manual token maintenance is required.
If you construct HTTP requests outside the SDK, for example upstream of a gateway or in O&M scripts, use the following template:
curl -v \
--resolve "<PORT>-<NAMESPACE>--<NAME>.<DOMAIN>:7788:<SANDBOX-GATEWAY-SVC-IP>" \
-H "e2b-sandbox-id: <NAMESPACE>--<NAME>" \
-H "e2b-sandbox-port: <PORT>" \
-H "E2B-Traffic-Access-Token: <LATEST-TRAFFIC-ACCESS-TOKEN>" \
http://<PORT>-<NAMESPACE>--<NAME>.<DOMAIN>:7788/
After auto-rotation is enabled, the <LATEST-TRAFFIC-ACCESS-TOKEN> value may differ each time it is retrieved. Do not cache this value in the application code. Read sbx.traffic_access_token again before each request.
(Optional) Adjust the token validity period
The token validity period is controlled by a sandbox-manager policy. By default, the long-lived behavior of about 100 years is retained for backward compatibility. Cluster operators can set a shorter validity period (for example, 24h) by using the --traffic-access-token-validity startup parameter of ack-sandbox-manager.
Before you enable a short validity period, make sure that all clients that access the data plane are upgraded and have patch_traffic_access_token() enabled. Legacy clients without auto-rotation receive HTTP 403 after their token expires, which interrupts data plane communication.
Limits
-
A refresh issues a replacement token instead of immediately revoking the previous one. Both JWTs remain valid in parallel until the
exp(expiration time claim) of the old JWT. After the old token expires, only the new token is valid. -
Concurrent refresh requests for the same sandbox are merged into a single issuance to avoid amplified traffic to the control plane during peak hours.
-
If
--traffic-access-token-validityis set too short (for example, less than 5 minutes), network jitter increases the probability of refresh failures. Set the validity period in hours or days based on your workload's access frequency.
FAQ
How does this feature relate to a manual monkey patch?
A manual SandboxBase.__init__ patch injects the first traffic_access_token into the default request headers of the E2B SDK, but does not replace the token in subsequent refreshes. After patch_traffic_access_token() is enabled, the SDK reads the latest token dynamically before each request and overwrites the request header. Therefore, use the new patch instead of the manual monkey patch. Enabling both at the same time does not cause conflicts.
What happens if a client has not enabled the patch and a short validity period is configured?
A client without the auto-rotation patch receives HTTP 403 after its first JWT expires, and data plane requests fail. Roll out the change in the following order:
-
In a test cluster, keep the default long validity period first, and verify that the patched SDK is compatible with your workload traffic.
-
Progressively shorten
--traffic-access-token-validity, and observe the distribution ofresponse_codein thesandbox-gatewaylogs. -
After you confirm that all production clients have upgraded the SDK and enabled the patch, switch to a short validity period in the production cluster.
How do I confirm in logs that a token has been rotated?
On the Application Logs page, set the Logstore to ack-sandbox-gateway, and focus on the following fields:
-
response_code: A sudden increase in403usually indicates that clients still carry an expired token. -
request_id: Correlate this field with issuance records in thesandbox-managercontrol plane logs to determine when a refresh takes effect on the data plane.
Does this affect the static access token?
No. Auto-rotation applies only to the JWT-based E2B-Traffic-Access-Token path. The static X-Access-Token remains valid for the lifecycle of the sandbox and is not rotated.