E2B icon indicating copy to clipboard operation
E2B copied to clipboard

JS SDK: unregistered iam token names toString/valueOf/then/toJSON bypass the transform guard and serialize garbage

Open beran-t opened this issue 3 months ago • 3 comments

Which SDK

JavaScript / TypeScript ([email protected]). Python is not affected.

What happens

Sandbox.create's network transform callback receives iam.tokens, a guarded object that is documented to fail loudly when a callback references a token name that was not registered in iam.tokens:

Referencing a token name that isn't registered in iam.tokens fails at sandbox creation with InvalidArgumentError (JavaScript) / InvalidArgumentException (Python), listing the names that are registered. — https://e2b.dev/docs/sandbox/workload-identity

Four property names are exempt from the guard's get trap so the runtime can serialize, await and coerce the object (RUNTIME_PROBED_PROPS = { toJSON, then, toString, valueOf }). Referencing one of those as an unregistered token name therefore does not raise, and a nonsense value is serialized into the rule instead:

import { Sandbox, Secret } from 'e2b'

await Sandbox.create({
  iam: { tokens: { aws: Secret.iamToken({ audience: 'sts.amazonaws.com', tokenType: 'JWT-SVID' }) } },
  network: {
    allowOut: ({ rules }) => [...rules.keys()],
    denyOut: ({ allTraffic }) => [allTraffic],
    rules: {
      'api.example.com': [{
        // none of these four raise, and none of them are registered
        transform: ({ iam }) => ({ headers: { Authorization: `Bearer ${iam.tokens.then}` } }),
      }],
    },
  },
})

Observed request bodies:

callback expression value sent on the wire
`Bearer ${iam.tokens.then}` Bearer undefined
`Bearer ${iam.tokens.toJSON}` Bearer undefined
`Bearer ${iam.tokens.toString}` Bearer function toString() { [native code] }
`Bearer ${iam.tokens.valueOf}` Bearer function valueOf() { [native code] }
`Bearer ${iam.tokens.anythingElse}` correctly raises InvalidArgumentError

Expected

All five rows raise InvalidArgumentError. The exemption is needed for JSON.stringify(iam.tokens), await, String(...) and toJSON on the context object, but it should not extend to a name lookup that is not present in the registered set.

Three related gaps on the same object

  1. Writable, no set trap. iam.tokens.gcp = '${e2b.identity.tokens.gcp}' followed by a read of gcp bypasses the check entirely.
  2. No getOwnPropertyDescriptor trap. Object.getOwnPropertyDescriptor(iam.tokens, 'gcp')?.value returns undefined silently and puts Bearer undefined on the wire.
  3. delete produces a self-contradictory message. After delete iam.tokens.aws, reading aws raises Network transform references iam token 'aws', which is not registered. Registered tokens: 'aws'.

Why it matters

The guard exists so a typo fails at create time rather than becoming a confusing auth failure at the destination. In these cases it fails silently, and the egress proxy then fails open: an unresolvable placeholder means the header is dropped and the request is forwarded anyway. The customer sees a 401 from the destination service with no error from E2B anywhere in the chain, which is exactly the failure mode the client-side validation was added to prevent.

Python for comparison

Every lookup form on the Python mapping raises, including .get('typo') and .get('typo', 'default'), and membership ('aws' in ctx.iam.tokens) answers without raising. Only mutating the private _names tuple defeats it, which is not a lookup.

Environment

  • [email protected], Node 24.11.1
  • Reproduced against production; the payload rows above were captured by pointing the SDK at a local recorder via apiUrl, so no sandbox is needed to see them.

beran-t avatar Aug 14 '26 13:08 beran-t

/sdk repro

mishushakov avatar Aug 19 '26 10:08 mishushakov

Triage

  • type: bug
  • priority: high
  • complexity: small
  • affected_area: js-sdk / network transform iam.tokens guard
  • actionable: yes
  • needs_clarification: false
  • summary: The JS SDK's iam.tokens proxy exempts toString/valueOf/then/toJSON from its unregistered-token guard, so referencing them as token names serializes garbage instead of raising InvalidArgumentError, causing silent egress auth failures; set/getOwnPropertyDescriptor/delete traps have related gaps.

Written by Devin

@beran-t This is fixed and shipped in [email protected] (JS SDK) via https://github.com/e2b-dev/E2B/pull/1715.

iam.tokens.toJSON / .then / .toString / .valueOf now throw InvalidArgumentError like any other unregistered token name — on use (coercion or serialization) rather than on the read, so JSON.stringify(iam.tokens), await, String(...) and enumeration inside a transform callback keep working. Python was unaffected.

npm i [email protected]

Heads-up on the three related gaps you listed: the set/defineProperty/deleteProperty traps and descriptor-lookup resolution were intentionally left out of the fix as paths nobody hits — please open a separate issue if they bite you in practice.

Thanks for the very detailed report and the captured payloads, they made this easy to pin down.