Skip to content

Commit 915f88a

Browse files
committed
fix(browser): centralize route timeout clamping
1 parent cec50aa commit 915f88a

8 files changed

Lines changed: 44 additions & 15 deletions

File tree

‎extensions/browser/src/browser/routes/agent.act.download.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
import { EXISTING_SESSION_LIMITS } from "./existing-session-limits.js";
1111
import { ensureOutputRootDir, resolveWritableOutputPathOrRespond } from "./output-paths.js";
1212
import { DEFAULT_DOWNLOAD_DIR } from "./path-output.js";
13-
import { readRoutePositiveInteger } from "./route-numeric.js";
13+
import { readRouteTimerTimeoutMs } from "./route-numeric.js";
1414
import type { BrowserRouteRegistrar } from "./types.js";
1515
import { asyncBrowserRoute, jsonError, toStringOrEmpty } from "./utils.js";
1616

@@ -34,7 +34,7 @@ export function registerBrowserAgentActDownloadRoutes(
3434
const out = toStringOrEmpty(body.path) || "";
3535
let timeoutMs: number | undefined;
3636
try {
37-
timeoutMs = readRoutePositiveInteger(body.timeoutMs, "timeoutMs");
37+
timeoutMs = readRouteTimerTimeoutMs(body.timeoutMs);
3838
} catch (err) {
3939
return jsonError(res, 400, formatErrorMessage(err));
4040
}
@@ -87,7 +87,7 @@ export function registerBrowserAgentActDownloadRoutes(
8787
const out = toStringOrEmpty(body.path);
8888
let timeoutMs: number | undefined;
8989
try {
90-
timeoutMs = readRoutePositiveInteger(body.timeoutMs, "timeoutMs");
90+
timeoutMs = readRouteTimerTimeoutMs(body.timeoutMs);
9191
} catch (err) {
9292
return jsonError(res, 400, formatErrorMessage(err));
9393
}

‎extensions/browser/src/browser/routes/agent.act.hooks.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
} from "./agent.shared.js";
1111
import { EXISTING_SESSION_LIMITS } from "./existing-session-limits.js";
1212
import { DEFAULT_UPLOAD_DIR, pathScope } from "./path-output.js";
13-
import { readRoutePositiveInteger } from "./route-numeric.js";
13+
import { readRouteTimerTimeoutMs } from "./route-numeric.js";
1414
import type { BrowserRouteRegistrar } from "./types.js";
1515
import {
1616
asyncBrowserRoute,
@@ -35,7 +35,7 @@ export function registerBrowserAgentActHookRoutes(
3535
const paths = toStringArray(body.paths) ?? [];
3636
let timeoutMs: number | undefined;
3737
try {
38-
timeoutMs = readRoutePositiveInteger(body.timeoutMs, "timeoutMs");
38+
timeoutMs = readRouteTimerTimeoutMs(body.timeoutMs);
3939
} catch (err) {
4040
return jsonError(res, 400, formatErrorMessage(err));
4141
}
@@ -126,7 +126,7 @@ export function registerBrowserAgentActHookRoutes(
126126
const promptText = toStringOrEmpty(body.promptText) || undefined;
127127
let timeoutMs: number | undefined;
128128
try {
129-
timeoutMs = readRoutePositiveInteger(body.timeoutMs, "timeoutMs");
129+
timeoutMs = readRouteTimerTimeoutMs(body.timeoutMs);
130130
} catch (err) {
131131
return jsonError(res, 400, formatErrorMessage(err));
132132
}

‎extensions/browser/src/browser/routes/agent.act.normalize.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { MAX_TIMER_TIMEOUT_MS } from "openclaw/plugin-sdk/number-runtime";
21
import { describe, expect, it } from "vitest";
2+
import { MAX_SAFE_TIMEOUT_DELAY_MS } from "../timer-delay.js";
33
import { normalizeActRequest } from "./agent.act.normalize.js";
44

55
describe("normalizeActRequest numeric fields", () => {
@@ -43,7 +43,7 @@ describe("normalizeActRequest numeric fields", () => {
4343
).toMatchObject({
4444
kind: "wait",
4545
text: "ready",
46-
timeoutMs: MAX_TIMER_TIMEOUT_MS,
46+
timeoutMs: MAX_SAFE_TIMEOUT_DELAY_MS,
4747
});
4848
});
4949

‎extensions/browser/src/browser/routes/agent.act.normalize.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { resolveTimerTimeoutMs } from "openclaw/plugin-sdk/number-runtime";
21
import {
32
ACT_MAX_BATCH_ACTIONS,
43
ACT_MAX_CLICK_DELAY_MS,
@@ -18,6 +17,7 @@ import {
1817
readRouteInteger,
1918
readRouteNonNegativeInteger,
2019
readRoutePositiveInteger,
20+
readRouteTimerTimeoutMs,
2121
} from "./route-numeric.js";
2222
import { toBoolean, toNumber, toStringArray, toStringOrEmpty } from "./utils.js";
2323

@@ -89,8 +89,7 @@ function readActionNonNegativeInteger(
8989
}
9090

9191
function readActionTimeoutMs(body: Record<string, unknown>): number | undefined {
92-
const timeoutMs = readActionPositiveInteger(body, "timeoutMs");
93-
return timeoutMs === undefined ? undefined : resolveTimerTimeoutMs(timeoutMs, 1);
92+
return readRouteTimerTimeoutMs(body.timeoutMs);
9493
}
9594

9695
function readBoundedActionDurationMs(

‎extensions/browser/src/browser/routes/agent.act.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import {
4040
} from "./agent.shared.js";
4141
import { resolveTargetIdAfterNavigate } from "./agent.snapshot-target.js";
4242
import { EXISTING_SESSION_LIMITS } from "./existing-session-limits.js";
43-
import { readRoutePositiveInteger } from "./route-numeric.js";
43+
import { readRoutePositiveInteger, readRouteTimerTimeoutMs } from "./route-numeric.js";
4444
import type { BrowserRouteRegistrar } from "./types.js";
4545
import { asyncBrowserRoute, jsonError, toStringOrEmpty } from "./utils.js";
4646

@@ -701,7 +701,7 @@ export function registerBrowserAgentActRoutes(
701701
let timeoutMs: number | undefined;
702702
let maxChars: number | undefined;
703703
try {
704-
timeoutMs = readRoutePositiveInteger(body.timeoutMs, "timeoutMs");
704+
timeoutMs = readRouteTimerTimeoutMs(body.timeoutMs);
705705
maxChars = readRoutePositiveInteger(body.maxChars, "maxChars");
706706
} catch (err) {
707707
return jsonError(res, 400, formatErrorMessage(err));

‎extensions/browser/src/browser/routes/permissions.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { getChromeWebSocketUrl } from "../chrome.js";
66
import { getPwAiModule } from "../pw-ai-module.js";
77
import type { BrowserRouteContext } from "../server-context.js";
88
import type { ProfileContext } from "../server-context.js";
9-
import { readRoutePositiveInteger } from "./route-numeric.js";
9+
import { readRouteTimerTimeoutMs } from "./route-numeric.js";
1010
import type { BrowserRouteRegistrar } from "./types.js";
1111
import { asyncBrowserRoute, getProfileContext, jsonError, toStringOrEmpty } from "./utils.js";
1212

@@ -163,7 +163,7 @@ export function registerBrowserPermissionRoutes(
163163
const targetId = toStringOrEmpty(body.targetId) || undefined;
164164
let timeoutMs: number;
165165
try {
166-
timeoutMs = Math.max(1_000, readRoutePositiveInteger(body.timeoutMs, "timeoutMs") ?? 5_000);
166+
timeoutMs = readRouteTimerTimeoutMs(body.timeoutMs, "timeoutMs", { minMs: 1_000 }) ?? 5_000;
167167
} catch (err) {
168168
return jsonError(res, 400, formatErrorMessage(err));
169169
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { describe, expect, it } from "vitest";
2+
import { MAX_SAFE_TIMEOUT_DELAY_MS } from "../timer-delay.js";
3+
import { readRouteTimerTimeoutMs } from "./route-numeric.js";
4+
5+
describe("browser route numeric helpers", () => {
6+
it("caps timer timeout fields to Node-safe bounds", () => {
7+
expect(readRouteTimerTimeoutMs("12345")).toBe(12_345);
8+
expect(readRouteTimerTimeoutMs(String(Number.MAX_SAFE_INTEGER))).toBe(
9+
MAX_SAFE_TIMEOUT_DELAY_MS,
10+
);
11+
});
12+
13+
it("preserves timeout validation errors", () => {
14+
expect(() => readRouteTimerTimeoutMs("1e3")).toThrow("timeoutMs must be a positive integer.");
15+
});
16+
17+
it("honors route-specific minimums", () => {
18+
expect(readRouteTimerTimeoutMs("1", "timeoutMs", { minMs: 1_000 })).toBe(1_000);
19+
});
20+
});

‎extensions/browser/src/browser/routes/route-numeric.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
parseStrictNonNegativeInteger,
55
parseStrictPositiveInteger,
66
} from "openclaw/plugin-sdk/number-runtime";
7+
import { normalizeBrowserTimerDelayMs } from "../timer-delay.js";
78

89
function hasRouteInputValue(value: unknown): boolean {
910
return value != null;
@@ -51,6 +52,15 @@ export function readRoutePositiveInteger(
5152
return parsed;
5253
}
5354

55+
export function readRouteTimerTimeoutMs(
56+
value: unknown,
57+
fieldName = "timeoutMs",
58+
opts?: { minMs?: number; invalidMessage?: string },
59+
): number | undefined {
60+
const parsed = readRoutePositiveInteger(value, fieldName, opts);
61+
return parsed === undefined ? undefined : normalizeBrowserTimerDelayMs(parsed, opts);
62+
}
63+
5464
export function readRouteNonNegativeInteger(
5565
value: unknown,
5666
fieldName: string,

0 commit comments

Comments
 (0)