Skip to content

Commit 87b93b5

Browse files
fix(browser): bound batch action files (#115882)
* fix(browser): bound batch action files * fix(browser): preserve legacy file input while bounding batches Co-authored-by: sunlit-deng <[email protected]> --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent 430de50 commit 87b93b5

5 files changed

Lines changed: 111 additions & 9 deletions

File tree

‎docs/cli/browser.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,8 @@ openclaw browser batch --actions-file - --continue
283283
284284
`openclaw browser batch` sends a `kind="batch"` `/act` request with nested `BrowserActRequest` actions (`wait`, `click`, `type`, `evaluate`, ...) — not `open`/`navigate`/`snapshot`/`screenshot`, which are CLI subcommands, not `/act` kinds. `--continue` sets `stopOnError=false` (default stops on first error); `--target-id` scopes the whole batch to one tab. A failed nested action makes the command exit nonzero; use `--json` to retain the ordered `results` response. See [Browser batch CLI](/tools/browser-control#browser-batch-cli) for the full contract (ref lifecycle, target id conflicts, error summary). `batch` is not supported on `profile="user"` / existing-session profiles.
285285
286+
`--actions-file` and `--actions-file -` stdin input are capped at 1,000,000 bytes. Split larger plans into multiple `openclaw browser batch` commands.
287+
286288
## State and storage
287289
288290
Viewport + emulation:

‎docs/tools/browser-control.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,8 @@ profiles; send actions individually there.
365365
--actions-file plan.json`, or `openclaw browser batch --actions-file -` to
366366
read the JSON array from stdin. `--continue` sets `stopOnError=false`; the
367367
default is to stop on first error. `--target-id` scopes the whole batch to
368-
one tab.
368+
one tab. `--actions-file` and stdin input are capped at 1,000,000 bytes;
369+
split larger plans into multiple batch commands.
369370
- Ref lifecycle: refs come from a `snapshot` run before the batch (snapshot is
370371
not a nested action). A nested action that changes page state — such as a
371372
`click` that triggers navigation, or an `evaluate` that mutates the DOM — can

‎extensions/browser/skills/browser-automation/SKILL.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Use this skill when you need the `browser` tool for anything beyond a single pag
4141

4242
`openclaw browser batch` runs an array of nested `/act` actions in one `/act` call (the same `kind="batch"` runtime reached through the agent tool), so CLI users and scripts can combine actions like `wait`, `click`, `type`, and `evaluate` into a single replayable plan without per-action round trips. Each entry in `actions[]` is a `BrowserActRequest` — the closed union the `/act` route accepts — not arbitrary `openclaw browser` subcommands. `batch` is not supported on `profile="user"` and other existing-session (chrome-mcp) profiles; send actions individually there.
4343

44-
- CLI: `openclaw browser batch --actions '<json>'`, `--actions-file plan.json`, or `--actions-file -` for stdin. `--continue` sets `stopOnError=false`; default stops on first error.
44+
- CLI: `openclaw browser batch --actions '<json>'`, `--actions-file plan.json`, or `--actions-file -` for stdin. `--actions-file` and stdin input are capped at 1,000,000 bytes; split larger plans into multiple batch commands. `--continue` sets `stopOnError=false`; default stops on first error.
4545
- Ref lifecycle: refs come from a `snapshot` run before the batch (snapshot is not a nested action). A nested action that changes page state — such as a `click` that triggers navigation, or an `evaluate` that mutates the DOM — can invalidate earlier refs for the rest of the batch; put state-changing actions first, or split into a follow-up batch after re-snapshotting. Navigation and re-snapshotting happen outside the batch, since `open`, `navigate`, and `snapshot` are not `/act` kinds.
4646
- Target id: nested actions share the request's tab; an explicit nested `targetId` that resolves to a different tab is rejected with `ACT_TARGET_ID_MISMATCH`.
4747
- Response: `{ "results": [{ "ok": true } | { "ok": false, "error": "..." }, ...] }` in order; with default `stopOnError` the array ends at the first failure. Any failed entry exits nonzero; use `--json` to preserve the full response in scripts.

‎extensions/browser/src/cli/browser-cli-actions-input/shared.test.ts‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
// Browser tests cover shared plugin behavior.
2+
import { execFileSync } from "node:child_process";
3+
import fs from "node:fs/promises";
4+
import path from "node:path";
5+
import { withTempDir } from "openclaw/plugin-sdk/test-env";
26
import { describe, expect, it } from "vitest";
37
import { readActionsPayload, readFields } from "./shared.js";
48

@@ -44,6 +48,17 @@ describe("readFields", () => {
4448
readFields({ fields: "[]", fieldsFile: "/tmp/openclaw-browser-fields.json" }),
4549
).rejects.toThrow("Specify only one of --fields or --fields-file");
4650
});
51+
52+
it("preserves oversized fields files that normalize to a small request", async () => {
53+
await withTempDir("openclaw-browser-fields-", async (tempDir) => {
54+
const fieldsPath = path.join(tempDir, "fields.json");
55+
await fs.writeFile(fieldsPath, `[${" ".repeat(1_048_577)}{"ref":"1","value":"ok"}]`);
56+
57+
await expect(readFields({ fieldsFile: fieldsPath })).resolves.toEqual([
58+
{ ref: "1", type: "text", value: "ok" },
59+
]);
60+
});
61+
});
4762
});
4863

4964
describe("readActionsPayload", () => {
@@ -52,4 +67,65 @@ describe("readActionsPayload", () => {
5267
readActionsPayload({ actions: "[]", actionsFile: "/tmp/openclaw-browser-actions.json" }),
5368
).rejects.toThrow("Specify only one of --actions or --actions-file");
5469
});
70+
71+
it("preserves inline actions larger than the file input ceiling", async () => {
72+
const actions = " ".repeat(1_000_001);
73+
await expect(readActionsPayload({ actions })).resolves.toBe(actions);
74+
});
75+
76+
it("bounds action files with the same byte limit as stdin", async () => {
77+
const maxBytes = 1_000_000;
78+
await withTempDir("openclaw-browser-actions-", async (tempDir) => {
79+
const actionsPath = path.join(tempDir, "actions.json");
80+
await fs.writeFile(actionsPath, Buffer.alloc(maxBytes + 1, 0x20));
81+
await expect(readActionsPayload({ actionsFile: actionsPath })).rejects.toMatchObject({
82+
code: "too-large",
83+
message: expect.stringContaining("Split the batch plan into smaller files"),
84+
});
85+
86+
await fs.writeFile(actionsPath, Buffer.alloc(maxBytes, 0x20));
87+
const payload = await readActionsPayload({ actionsFile: actionsPath });
88+
expect(Buffer.byteLength(payload)).toBe(maxBytes);
89+
});
90+
});
91+
92+
it("follows a symlinked action file to its bounded target", async () => {
93+
const maxBytes = 1_000_000;
94+
await withTempDir("openclaw-browser-actions-", async (tempDir) => {
95+
const targetPath = path.join(tempDir, "actions-target.json");
96+
const linkPath = path.join(tempDir, "actions-link.json");
97+
await fs.writeFile(targetPath, Buffer.alloc(maxBytes, 0x20));
98+
await fs.symlink(targetPath, linkPath);
99+
100+
const payload = await readActionsPayload({ actionsFile: linkPath });
101+
expect(Buffer.byteLength(payload)).toBe(maxBytes);
102+
});
103+
});
104+
105+
it("rejects an oversized symlinked action file target", async () => {
106+
const maxBytes = 1_000_000;
107+
await withTempDir("openclaw-browser-actions-", async (tempDir) => {
108+
const targetPath = path.join(tempDir, "actions-target.json");
109+
const linkPath = path.join(tempDir, "actions-link.json");
110+
await fs.writeFile(targetPath, Buffer.alloc(maxBytes + 1, 0x20));
111+
await fs.symlink(targetPath, linkPath);
112+
113+
await expect(readActionsPayload({ actionsFile: linkPath })).rejects.toMatchObject({
114+
code: "too-large",
115+
message: expect.stringContaining("Split the batch plan into smaller files"),
116+
});
117+
});
118+
});
119+
120+
it.skipIf(process.platform === "win32")(
121+
"rejects FIFO action files without opening them",
122+
async () => {
123+
await withTempDir("openclaw-browser-actions-", async (tempDir) => {
124+
const fifoPath = path.join(tempDir, "actions.pipe");
125+
execFileSync("mkfifo", [fifoPath]);
126+
127+
await expect(readActionsPayload({ actionsFile: fifoPath })).rejects.toThrow("regular file");
128+
});
129+
},
130+
);
55131
});

‎extensions/browser/src/cli/browser-cli-actions-input/shared.ts‎

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44
import fs from "node:fs/promises";
55
import type { Command } from "commander";
6+
import { FsSafeError, readRegularFile } from "openclaw/plugin-sdk/security-runtime";
67
import {
78
callBrowserRequest,
89
withBrowserActionTimeoutSlack,
@@ -74,8 +75,20 @@ export function requireRef(ref: string | undefined) {
7475
return refValue;
7576
}
7677

77-
async function readFile(path: string): Promise<string> {
78-
return await fs.readFile(path, "utf8");
78+
async function readFile(filePath: string, maxBytes?: number): Promise<string> {
79+
if (maxBytes === undefined) {
80+
return await fs.readFile(filePath, "utf8");
81+
}
82+
try {
83+
// Preserve existing symlinked inputs while rejecting oversized files and FIFOs.
84+
const { buffer } = await readRegularFile({ filePath: await fs.realpath(filePath), maxBytes });
85+
return buffer.toString("utf8");
86+
} catch (cause) {
87+
if (cause instanceof FsSafeError && cause.code === "too-large") {
88+
throw createActionsInputTooLargeError("--actions-file", cause);
89+
}
90+
throw cause;
91+
}
7992
}
8093

8194
/** Reads and validates JSON form-field descriptors from inline text or a file. */
@@ -119,21 +132,29 @@ export async function readFields(opts: {
119132
});
120133
}
121134

122-
/** Cap on batch action JSON read from stdin; keeps a runaway pipe from filling memory. */
123-
const ACTIONS_STDIN_MAX_BYTES = 1_000_000;
135+
/** Cap on batch action JSON read from files or stdin. */
136+
const ACTIONS_INPUT_MAX_BYTES = 1_000_000;
137+
138+
function createActionsInputTooLargeError(source: string, cause?: unknown): FsSafeError {
139+
return new FsSafeError(
140+
"too-large",
141+
`${source} exceeds ${ACTIONS_INPUT_MAX_BYTES} bytes. Split the batch plan into smaller files or run multiple openclaw browser batch commands.`,
142+
{ cause },
143+
);
144+
}
124145

125146
/** Reads stdin to a UTF-8 string, throwing once the byte cap is exceeded. */
126147
async function readStdinText(
127148
stream: NodeJS.ReadableStream = process.stdin,
128-
maxBytes = ACTIONS_STDIN_MAX_BYTES,
149+
maxBytes = ACTIONS_INPUT_MAX_BYTES,
129150
): Promise<string> {
130151
const chunks: Buffer[] = [];
131152
let total = 0;
132153
for await (const chunk of stream) {
133154
const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
134155
total += buf.length;
135156
if (total > maxBytes) {
136-
throw new Error(`actions stdin exceeds ${maxBytes} bytes.`);
157+
throw createActionsInputTooLargeError("--actions-file - stdin");
137158
}
138159
chunks.push(buf);
139160
}
@@ -149,7 +170,9 @@ export async function readActionsPayload(opts: {
149170
throw new Error("Specify only one of --actions or --actions-file");
150171
}
151172
if (opts.actionsFile) {
152-
return opts.actionsFile === "-" ? await readStdinText() : await readFile(opts.actionsFile);
173+
return opts.actionsFile === "-"
174+
? await readStdinText()
175+
: await readFile(opts.actionsFile, ACTIONS_INPUT_MAX_BYTES);
153176
}
154177
return opts.actions ?? "";
155178
}

0 commit comments

Comments
 (0)