Skip to content

Commit 0ae1ac1

Browse files
committed
fix: validate web guarded fetch timeouts
1 parent 37c5003 commit 0ae1ac1

2 files changed

Lines changed: 57 additions & 4 deletions

File tree

‎src/agents/tools/web-guarded-fetch.test.ts‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,51 @@ describe("web-guarded-fetch", () => {
8989
expect(call?.policy).toBeUndefined();
9090
expect(call?.mode).toBe(GUARDED_FETCH_MODE.STRICT);
9191
});
92+
93+
it("normalizes string timeouts before guarded fetch dispatch", async () => {
94+
vi.mocked(fetchWithSsrFGuard).mockResolvedValue({
95+
response: new Response("ok", { status: 200 }),
96+
finalUrl: "https://example.com",
97+
release: async () => {},
98+
});
99+
100+
await withStrictWebToolsEndpoint(
101+
{ url: "https://example.com", timeoutSeconds: "7" as never },
102+
async () => undefined,
103+
);
104+
expect(firstFetchCall().timeoutMs).toBe(7000);
105+
106+
vi.clearAllMocks();
107+
vi.mocked(fetchWithSsrFGuard).mockResolvedValue({
108+
response: new Response("ok", { status: 200 }),
109+
finalUrl: "https://example.com",
110+
release: async () => {},
111+
});
112+
113+
await withStrictWebToolsEndpoint(
114+
{
115+
url: "https://example.com",
116+
timeoutMs: "2500" as never,
117+
timeoutSeconds: "7" as never,
118+
},
119+
async () => undefined,
120+
);
121+
expect(firstFetchCall().timeoutMs).toBe(2500);
122+
});
123+
124+
it("rejects malformed timeouts before guarded fetch dispatch", async () => {
125+
await expect(
126+
withStrictWebToolsEndpoint(
127+
{ url: "https://example.com", timeoutMs: "2.5" as never },
128+
async () => undefined,
129+
),
130+
).rejects.toThrow("timeoutMs must be a positive integer");
131+
await expect(
132+
withStrictWebToolsEndpoint(
133+
{ url: "https://example.com", timeoutSeconds: -1 },
134+
async () => undefined,
135+
),
136+
).rejects.toThrow("timeoutSeconds must be a positive integer");
137+
expect(fetchWithSsrFGuard).not.toHaveBeenCalled();
138+
});
92139
});

‎src/agents/tools/web-guarded-fetch.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
ssrfPolicyFromHttpBaseUrlFakeIpHostnameAllowlist,
1010
type SsrFPolicy,
1111
} from "../../infra/net/ssrf.js";
12+
import { readPositiveIntegerParam } from "./common.js";
1213

1314
const WEB_TOOLS_SELF_HOSTED_NETWORK_SSRF_POLICY: SsrFPolicy = {
1415
dangerouslyAllowPrivateNetwork: true,
@@ -29,11 +30,16 @@ function resolveTimeoutMs(params: {
2930
timeoutMs?: number;
3031
timeoutSeconds?: number;
3132
}): number | undefined {
32-
if (typeof params.timeoutMs === "number" && Number.isFinite(params.timeoutMs)) {
33-
return params.timeoutMs;
33+
const timeoutMs = readPositiveIntegerParam(params as Record<string, unknown>, "timeoutMs");
34+
if (timeoutMs !== undefined) {
35+
return timeoutMs;
3436
}
35-
if (typeof params.timeoutSeconds === "number" && Number.isFinite(params.timeoutSeconds)) {
36-
return params.timeoutSeconds * 1000;
37+
const timeoutSeconds = readPositiveIntegerParam(
38+
params as Record<string, unknown>,
39+
"timeoutSeconds",
40+
);
41+
if (timeoutSeconds !== undefined) {
42+
return timeoutSeconds * 1000;
3743
}
3844
return undefined;
3945
}

0 commit comments

Comments
 (0)