Skip to content

Secure, Fast, and Extensible Sandbox runtime for AI agents.

fix(sdk-js): treat an infinite or negative redirect deadline as none

Maohaoffbb5a4a386a
diff --git a/sdks/sandbox/javascript/src/config/redirectFetch.ts b/sdks/sandbox/javascript/src/config/redirectFetch.ts
index 4abd2adcf..f1b727e74 100644
--- a/sdks/sandbox/javascript/src/config/redirectFetch.ts
+++ b/sdks/sandbox/javascript/src/config/redirectFetch.ts
@@ -72,6 +72,7 @@ export interface RedirectSafeFetchOptions {
* Overall deadline for the whole redirect chain, in milliseconds. Each hop
* re-enters the transport (and re-arms its per-request timeout), so without
* an overall budget a long chain could run much longer than one request.
+ * Zero, negative and non-finite values disable it.
*/
overallTimeoutMs?: number;
}
@@ -206,7 +207,14 @@ export function createRedirectSafeFetch(
return baseFetch;
}
const maxRedirects = opts.maxRedirects ?? DEFAULT_MAX_REDIRECTS;
- const overallTimeoutMs = opts.overallTimeoutMs;
+ // Like requestTimeoutSeconds, a non-positive or non-finite value means no
+ // deadline. setTimeout would otherwise fire almost at once for either.
+ const overallTimeoutMs =
+ opts.overallTimeoutMs !== undefined &&
+ Number.isFinite(opts.overallTimeoutMs) &&
+ opts.overallTimeoutMs > 0
+ ? opts.overallTimeoutMs
+ : undefined;
return async function redirectSafeFetch(
input: RequestInfo | URL,
diff --git a/sdks/sandbox/javascript/src/factory/defaultAdapterFactory.ts b/sdks/sandbox/javascript/src/factory/defaultAdapterFactory.ts
index 6c3ad63c8..c64a1cece 100644
--- a/sdks/sandbox/javascript/src/factory/defaultAdapterFactory.ts
+++ b/sdks/sandbox/javascript/src/factory/defaultAdapterFactory.ts
@@ -103,9 +103,7 @@ function withRedirectGuard(
): typeof fetch {
return createRedirectSafeFetch(baseUrl, baseFetch, {
overallTimeoutMs:
- overallTimeoutSeconds && overallTimeoutSeconds > 0
- ? overallTimeoutSeconds * 1000
- : undefined,
+ overallTimeoutSeconds === undefined ? undefined : overallTimeoutSeconds * 1000,
});
}
diff --git a/sdks/sandbox/javascript/tests/redirect-headers.test.mjs b/sdks/sandbox/javascript/tests/redirect-headers.test.mjs
index a23b26d35..0bd1d415f 100644
--- a/sdks/sandbox/javascript/tests/redirect-headers.test.mjs
+++ b/sdks/sandbox/javascript/tests/redirect-headers.test.mjs
@@ -276,6 +276,24 @@ test("the overall redirect deadline bounds the first URL-input hop", async () =>
assert.equal(signals[0].aborted, true);
});
+test("a non-finite or negative overall deadline disables it", async () => {
+ // ConnectionConfig treats requestTimeoutSeconds <= 0 or Infinity as "no
+ // timeout"; the redirect guard must not turn that into an immediate abort.
+ for (const overallTimeoutMs of [Infinity, -1000]) {
+ const baseFetch = async (input, init) => {
+ await new Promise((resolve) => setTimeout(resolve, 50));
+ if (init?.signal?.aborted) throw init.signal.reason;
+ return new Response("ok", { status: 200 });
+ };
+ const safeFetch = createRedirectSafeFetch("http://a.test", baseFetch, { overallTimeoutMs });
+
+ const res = await safeFetch("http://a.test/start", {
+ headers: { "OPEN-SANDBOX-API-KEY": "tenant-secret" },
+ });
+ assert.equal(await res.text(), "ok", `overallTimeoutMs=${overallTimeoutMs}`);
+ }
+});
+
test("303 and POST 301/302 convert to GET and drop entity headers", async () => {
const calls = [];
const baseFetch = async (input, init) => {