From 33c8a15d9281467213e8cefca17a73e087108c7c Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Thu, 5 Nov 2026 14:33:33 +0100 Subject: [PATCH 1/5] =?UTF-8?q?test:=20reproduce=20#1242=20=E2=80=94=20a?= =?UTF-8?q?=20gateway=20forwards=20a=20device.exec=20stdin=20of=20any=20si?= =?UTF-8?q?ze=20to=20the=20worker?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/gateway/dispatcher.test.ts | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/gateway/dispatcher.test.ts b/src/gateway/dispatcher.test.ts index f8f39a38b..b65ff1786 100644 --- a/src/gateway/dispatcher.test.ts +++ b/src/gateway/dispatcher.test.ts @@ -2499,6 +2499,40 @@ describe("GatewayDispatcher", () => { ).rejects.toMatchObject({ code: "FORBIDDEN" }); }); + it("refuses a device.exec whose stdin is longer than 8,388,608 characters with BAD_REQUEST and forwards nothing to the worker, and forwards one at 8,388,608", async () => { + const { coordinator, directory, dispatcher, workers } = harness(); + const client = new ScriptedWorkerClient(); + directory.add("wrk_1", client); + workers.connected("wrk_1", undefined, "0.3.0"); + workers.refresh("wrk_1", { + capacity: statusFixture().capacity, + health: "running", + queueDepth: 0, + catalog: catalogFixture([{ models: ["iPhone 17"], platform: "ios", runtimes: ["26.0"] }]) + .platforms, + downloads: { policy: "on-request" }, + }); + client.requestLeaseQueue.push({ grant: grantFixture(), kind: "grant" }); + const grant = await coordinator.request( + { model: "iPhone 17", platform: "ios" }, + { allowDownload: false, noWait: true, ownerId: "agent-1", requesterId: "agent-1" }, + ); + const exec = (stdin: string) => + dispatcher.dispatch( + "device.exec", + { args: ["spawn", "booted", "untar"], leaseId: grant.lease.id, stdin, tool: "simctl" }, + session({ principal: "agent-1", role: "agent" }), + ); + const forwarded = () => client.calls.filter((call) => call.startsWith("device.exec:")); + + client.execQueue.push({ exitCode: 0, kind: "ok" }); + await expect(exec("A".repeat(8_388_608))).resolves.toEqual({ exitCode: 0 }); + expect(forwarded()).toHaveLength(1); + + await expect(exec("A".repeat(8_388_609))).rejects.toMatchObject({ code: "BAD_REQUEST" }); + expect(forwarded()).toHaveLength(1); + }); + it("calls onStarted for a silent device.exec from the worker's own started push (ADR §19a/§19b/§19e)", async () => { const { coordinator, directory, dispatcher, workers } = harness(); const client = new ScriptedWorkerClient(); From 5eba7067829b34057df13089044b77a3917ef89a Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Thu, 5 Nov 2026 15:02:15 +0100 Subject: [PATCH 2/5] fix(contract): device.exec refuses a stdin over 8 MiB with BAD_REQUEST (#1242) Protocol-bump: none (stdin size cap; wire shape unchanged) --- docs/internal/flows/device-exec.md | 5 +++-- src/contract/operations.ts | 5 ++++- .../content/docs/docs/using-devices/programmatic-client.mdx | 2 +- .../src/content/docs/docs/using-devices/remote-agents.mdx | 2 +- .../docs/docs/using-devices/working-with-a-device.mdx | 2 +- 5 files changed, 10 insertions(+), 6 deletions(-) diff --git a/docs/internal/flows/device-exec.md b/docs/internal/flows/device-exec.md index 83f43dcc3..c2eb1bd92 100644 --- a/docs/internal/flows/device-exec.md +++ b/docs/internal/flows/device-exec.md @@ -36,8 +36,9 @@ Runs the command on the machine that owns the device and streams its output back another device (ADR 0047), see "Resolution per platform". 5. Env — `execBaseEnvironment(process.env)` (`src/daemon/exec-env.ts`, the allowlisted keys, read once at start) + the command's `env`, minus its `unsetEnv`. -6. Spawn — `ProcessRunner.spawnStreaming`, pipes only. `stdin` is written once, - then closed. +6. Spawn — `ProcessRunner.spawnStreaming`, pipes only. `stdin` (at most 8 MiB + of characters, refused `BAD_REQUEST` by the contract before any dispatcher + forwards or spawns) is written once, then closed. 7. `started` push the moment the process exists, then one `output` push per chunk (`{ requestId, stream, chunk }`). The push write is awaited before the child is read again: a slow reader slows the command. Nothing is buffered; no size cap. diff --git a/src/contract/operations.ts b/src/contract/operations.ts index c6007a83d..12d8ca30a 100644 --- a/src/contract/operations.ts +++ b/src/contract/operations.ts @@ -422,6 +422,9 @@ export const driverPassthrough = defineOperation({ output: passthroughCommandSchema, }); +// why: the daemon parses a request as one string, so unbounded stdin exhausts the heap (#1242). +const EXEC_STDIN_MAX_LENGTH = 8_388_608; + /** * Runs `simctl`, `adb` or `devicectl` **on the machine that owns the device** and streams the output back * (ADR 0005 §19a). The difference from `driver.passthrough` is where the process runs, not @@ -468,7 +471,7 @@ export const deviceExec = defineOperation({ leaseId: z.string(), tool: z.string().min(1), args: z.array(z.string()), - stdin: z.string().optional(), + stdin: z.string().max(EXEC_STDIN_MAX_LENGTH).optional(), /** * why: Who this command is being run *for* -- the requester the lease is attributed to (ADR * 0003 §4), not the connection's principal. Read **only on an admin session**, where it diff --git a/website/src/content/docs/docs/using-devices/programmatic-client.mdx b/website/src/content/docs/docs/using-devices/programmatic-client.mdx index d5512ea89..1931c1fb2 100644 --- a/website/src/content/docs/docs/using-devices/programmatic-client.mdx +++ b/website/src/content/docs/docs/using-devices/programmatic-client.mdx @@ -113,7 +113,7 @@ Run it with a TypeScript runner, for example `npx tsx lease-and-launch.mts`. | `requestLease(request, { onProgress, signal })` | Waits for a device and resolves with the grant: `device`, `environment`, `lease` and `timing`, the same fields `simlock lease` prints. | | `renewLease({ leaseId, ttlMs? })` | Moves the deadline to now plus the TTL. Resolves with the lease, including the new `ttlDeadline`. | | `releaseLease({ leaseId })` | Ends the lease. A simulator or emulator is then deleted; a physical device is cleaned. | -| `exec({ leaseId, tool, args, stdin? }, { onOutput })` | Runs `simctl` or `adb` (or `devicectl` on a physical iPhone or iPad) on the device, and resolves with `{ exitCode }`. | +| `exec({ leaseId, tool, args, stdin? }, { onOutput })` | Runs `simctl` or `adb` (or `devicectl` on a physical iPhone or iPad) on the device, and resolves with `{ exitCode }`. `stdin` holds at most 8,388,608 characters. | | `rotate(leaseId, orientation)` | Turns an iPhone or iPad simulator. See [Rotate without a socket](/simlock/docs/using-devices/watching-the-screen#rotate-without-a-socket). | | `listLeases()` | Your leases. | | `getCatalog({ platform? })` | What you can ask for, as `simlock catalog --json` prints it. | diff --git a/website/src/content/docs/docs/using-devices/remote-agents.mdx b/website/src/content/docs/docs/using-devices/remote-agents.mdx index 0c631c09b..172ef7031 100644 --- a/website/src/content/docs/docs/using-devices/remote-agents.mdx +++ b/website/src/content/docs/docs/using-devices/remote-agents.mdx @@ -150,7 +150,7 @@ The command runs on the worker, as the user who runs the worker's daemon. Treat - **Leave the device out.** Simlock adds the lease's device to the command: the UDID for `simctl` (where the subcommand takes one), `-s ` for `adb`, and `--device ` for `devicectl`. Write `["install","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/builds/MyApp.app"]`, not a UDID or serial. - **Naming another device is refused.** A command that names a different device, such as `booted`, another UDID, `adb -s emulator-5556`, `-e`, `-d` or `-t`, is refused with `422 PASSTHROUGH_REFUSED` before anything runs. Naming the lease's own device is accepted, except on a physical Android phone, which refuses any option before the subcommand, even its own `-s `. - **Paths are on the worker.** `{"tool":"simctl","args":["install","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/builds/MyApp.app"]}` needs `/builds/MyApp.app` to exist on the worker. The API can't upload files. Get the file there another way, or use [agent-device](/simlock/docs/using-devices/agent-device), which uploads apps for you. -- **No terminal.** `stdin` in the body is sent to the command once. Interactive commands, such as a bare `adb shell`, are refused. +- **No terminal.** `stdin` in the body is sent to the command once, and may hold at most 8,388,608 characters; a longer one is refused with `BAD_REQUEST`. Interactive commands, such as a bare `adb shell`, are refused. - **Output is text.** Output is decoded as UTF-8, so binary output, such as a screenshot written to stdout, gets corrupted. Write it to a file on the worker instead. - **Time limit.** A command that runs longer than 10 minutes is stopped, and the stream ends with an `error` event carrying `EXEC_TIMEOUT`. Disconnecting doesn't stop the command. - **The same commands are refused as locally.** Everything `simlock simctl` and `simlock adb` refuse (see [Commands Simlock refuses](/simlock/docs/using-devices/working-with-a-device#commands-simlock-refuses)) is refused here with `422 PASSTHROUGH_REFUSED`, before anything runs. So is `simctl` on an Android lease, and `adb` on an iOS one. diff --git a/website/src/content/docs/docs/using-devices/working-with-a-device.mdx b/website/src/content/docs/docs/using-devices/working-with-a-device.mdx index c9e59084e..480b152ed 100644 --- a/website/src/content/docs/docs/using-devices/working-with-a-device.mdx +++ b/website/src/content/docs/docs/using-devices/working-with-a-device.mdx @@ -92,7 +92,7 @@ simlock --gateway simctl --lease 3f81a2c4-9b7d-4e21-8a55-1c0e6f2d7b93.lse_9f2c6a - `--lease ` says which lease the command runs under. Put it right after `simctl` or `adb`. You can leave it out when you hold exactly one lease. Without a gateway, it is accepted and ignored. - File paths in the arguments are paths on the worker, not on your machine. `simctl install` needs the app to be on the worker already. - Commands that need a terminal, such as a bare `adb shell`, are refused. -- The command gets no input unless you pass `--stdin`. With `--stdin`, Simlock reads everything you pipe in (or type, up to Ctrl-D) and sends it once. Put `--stdin` before the command's own arguments, next to `--lease`. +- The command gets no input unless you pass `--stdin`. With `--stdin`, Simlock reads everything you pipe in (or type, up to Ctrl-D) and sends it once, up to 8,388,608 characters; more is refused with `BAD_REQUEST`. Put `--stdin` before the command's own arguments, next to `--lease`.