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..1fbeb1f66 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). +export 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/src/gateway/dispatcher.test.ts b/src/gateway/dispatcher.test.ts index 6791c6d51..5ae382ac4 100644 --- a/src/gateway/dispatcher.test.ts +++ b/src/gateway/dispatcher.test.ts @@ -2500,6 +2500,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(); diff --git a/src/http/app.test.ts b/src/http/app.test.ts index 3ee4bd7ec..d5d5473c1 100644 --- a/src/http/app.test.ts +++ b/src/http/app.test.ts @@ -1981,6 +1981,7 @@ describe("lease routes", () => { { args: [], tool: "simctl", unexpected: true }, { args: [], requesterId: "a\u001b[2Jb", tool: "simctl" }, { args: [], requesterId: "x".repeat(257), tool: "simctl" }, + { args: [], stdin: "A".repeat(8_388_609), tool: "simctl" }, ]) { const response = await postExec(app, body); expect(response.status).toBe(400); diff --git a/src/http/lease-declarations.ts b/src/http/lease-declarations.ts index c40d4d595..4f0fcfaa6 100644 --- a/src/http/lease-declarations.ts +++ b/src/http/lease-declarations.ts @@ -5,6 +5,7 @@ import { deviceRecordSchema, baselineNameSchema, imageTagSchema, + EXEC_STDIN_MAX_LENGTH, INPUT_ORIENTATIONS, leaseIdSchema, OPERATIONS, @@ -137,6 +138,7 @@ const execBodySchema = z args: z.array(z.string()), stdin: z .string() + .max(EXEC_STDIN_MAX_LENGTH) .nullish() .transform((value) => value ?? undefined), requesterId: requesterIdSchema.nullish().transform((value) => value ?? undefined), diff --git a/src/source-hygiene.test.ts b/src/source-hygiene.test.ts index 79a79e280..f854b2983 100644 --- a/src/source-hygiene.test.ts +++ b/src/source-hygiene.test.ts @@ -40,10 +40,10 @@ const SHRINK_ONLY: Record = { "src/daemon/server.ts": 1850, "src/drivers/ios/index.test.ts": 5950, "src/drivers/ios/index.ts": 2700, - "src/gateway/dispatcher.test.ts": 3023, + "src/gateway/dispatcher.test.ts": 3057, "src/gateway/fleet-coordinator.test.ts": 5283, "src/gateway/fleet-coordinator.ts": 1659, - "src/http/app.test.ts": 3487, + "src/http/app.test.ts": 3488, "src/leasing/create-leasing.test.ts": 5276, "src/leasing/lease-acquisition-coordinator.test.ts": 3691, "src/leasing/lease-acquisition-coordinator.ts": 1622, diff --git a/website/src/content/docs/docs/getting-started/connect-your-agent.mdx b/website/src/content/docs/docs/getting-started/connect-your-agent.mdx index a86fc25ba..f1da9a78b 100644 --- a/website/src/content/docs/docs/getting-started/connect-your-agent.mdx +++ b/website/src/content/docs/docs/getting-started/connect-your-agent.mdx @@ -126,7 +126,7 @@ The server starts the daemon if it is not running. It renews the agent's lease w | `lease_simulator` | Leases one simulator or emulator for this session. Takes `platform` and, optionally, `model` or `class`, `osVersion`, `ttlMs`, `noWait`, `timeoutMs` and `allowDownload`, the same options as `simlock lease`. Returns the same lease the CLI prints. | | `release_simulator` | Releases this session's lease. | | `lease_status` | Reports this session's lease, or `held: false` when it has none. Safe to call any time, for example after the agent's context was compacted. | -| `run_on_device` | Runs one `simctl`, `adb` or `devicectl` command on this session's device. Takes `tool`, `args` and, optionally, `stdin`. | +| `run_on_device` | Runs one `simctl`, `adb` or `devicectl` command on this session's device. Takes `tool`, `args` and, optionally, `stdin` (at most 8,388,608 characters; a longer one fails the tool's input validation, before Simlock sees it). | | `rotate_device` | Turns this session's iOS simulator to `portrait`, `portrait-upside-down`, `landscape-left` or `landscape-right`. | To install and launch an app, the agent calls `run_on_device`, for example `{"tool":"simctl","args":["install","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/builds/App.app"]}`. Things to know: diff --git a/website/src/content/docs/docs/reference/cli.mdx b/website/src/content/docs/docs/reference/cli.mdx index b403f3ab6..dcf2aef13 100644 --- a/website/src/content/docs/docs/reference/cli.mdx +++ b/website/src/content/docs/docs/reference/cli.mdx @@ -171,7 +171,7 @@ simlock simctl [--lease ] [--stdin] | Flag | Meaning | Default | | --- | --- | --- | | `--lease ` | Through a gateway: the lease whose device the command runs against. Must come before the simctl arguments. On a worker it is accepted and ignored. | Through a gateway: your only lease | -| `--stdin` | Through a gateway: read stdin to its end (Ctrl-D at a terminal) and send it to the command. Must come before the simctl arguments, in either order with `--lease`. On a worker it is accepted and ignored. | Off: the command gets empty input and stdin is never read | +| `--stdin` | Through a gateway: read stdin to its end (Ctrl-D at a terminal) and send it to the command, at most 8,388,608 characters; more is refused with `BAD_REQUEST`. Must come before the simctl arguments, in either order with `--lease`. On a worker it is accepted and ignored. | Off: the command gets empty input and stdin is never read | | `` | Passed to `xcrun simctl` as written. | — | Every other argument goes to `simctl`, including `--help`. On a worker the command runs on this machine with your terminal. Through a gateway it runs on the worker that holds the lease, its output is streamed back. Stdin is sent only with `--stdin`; without it the command gets empty input and a pipe that never closes does not hold it up. The command exits with `simctl`'s own exit code; a Simlock failure also writes a `{"error":...}` line to stderr. Through a gateway the worker puts the lease's UDID into a command that leaves the device out, so write `simlock --gateway simctl install ./MyApp.app`. Simlock refuses `create`, `erase`, `delete`, `shutdown all`, `runtime delete`, a caller-supplied `--set` or `--profiles`, and (through a gateway) a command that names another device such as `booted`, with `PASSTHROUGH_REFUSED` (exit 2). Use `simlock release` and `simlock cleanup` to give devices back. @@ -187,7 +187,7 @@ simlock adb [--lease ] [--stdin] | Flag | Meaning | Default | | --- | --- | --- | | `--lease ` | Through a gateway: the lease whose device the command runs against. Must come before the adb arguments. On a worker it is accepted and ignored. | Through a gateway: your only lease | -| `--stdin` | Through a gateway: read stdin to its end (Ctrl-D at a terminal) and send it to the command. Must come before the adb arguments, in either order with `--lease`. On a worker it is accepted and ignored. | Off: the command gets empty input and stdin is never read | +| `--stdin` | Through a gateway: read stdin to its end (Ctrl-D at a terminal) and send it to the command, at most 8,388,608 characters; more is refused with `BAD_REQUEST`. Must come before the adb arguments, in either order with `--lease`. On a worker it is accepted and ignored. | Off: the command gets empty input and stdin is never read | | `` | Passed to `adb` as written. | — | The command exits with `adb`'s own exit code. Through a gateway, stdin is sent only with `--stdin`; without it the command gets empty input. Simlock refuses `adb kill-server`, `adb disconnect`, flags that move adb to a different server, and (through a gateway) `adb shell` with no command, with `PASSTHROUGH_REFUSED` (exit 2). `-s`, `-t`, `-d` and `-e` are allowed before the subcommand. Through a gateway the worker adds `-s ` for an emulator, and refuses `-s` with another serial, `-e`, `-d` and `-t`. 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`.