From 2940e5f4739cd03e80d811fe7e5f090bd1381bb9 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sat, 22 Aug 2026 21:35:31 -0700 Subject: [PATCH 1/2] fix(wsl): budget the whole command line, not just the script The argv/stdin threshold measured `script.length`, but the cap applies to the finished command line -- which also carries `PATH=` and `HOME=`. A login PATH is itself a few KB. That produced a perverse band: with a long enough PATH, a 7,999-char hook was placed on argv and CreateProcess refused it, while the SAME hook at 8,001 chars flipped to stdin and ran. Size decided how a hook behaved, in the wrong direction, and the failure looked like "your setup hook failed" with nothing pointing at length. Now the argv form is built, measured, and only used if the whole line fits; otherwise the script goes to stdin as before. The count over-estimates slightly (it charges quoting for every argument) because over-counting is the safe direction for a cap. The regression test uses a 7,000-char script -- deliberately under any script-only threshold -- with a 27KB PATH, and asserts it lands on stdin. My first attempt used 7,999 + `echo `, which is 8,004 and flipped under the old rule too, so it passed either way and proved nothing. Credit: Grok. --- src/main/wsl/wsl-runner.test.ts | 21 ++++++++++++++++++++- src/main/wsl/wsl-runner.ts | 30 +++++++++++++++++++++++++----- 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/src/main/wsl/wsl-runner.test.ts b/src/main/wsl/wsl-runner.test.ts index f92da4d18e35..ab62c67ecfcf 100644 --- a/src/main/wsl/wsl-runner.test.ts +++ b/src/main/wsl/wsl-runner.test.ts @@ -159,13 +159,32 @@ describe('scripts', () => { // command line at 32767, so argv would fail to spawn at all; stdin still // runs it, which is the lesser loss. seedWslGuestEnvironmentForTests(undefined, ENVIRONMENT) - const script = `echo ${'x'.repeat(9_000)}` + const script = `echo ${'x'.repeat(31_000)}` await runWslProcess({ loginPath: 'preferred', shell: 'bash', script }) const spec = runProcessMock.mock.calls.at(-1)?.[0] expect(spec.input).toBe(script) expect(lastArgv().slice(-3)).toEqual(['bash', '-s', '--']) }) + it('flips to stdin when the login PATH, not the script, is what overflows', async () => { + // The perverse band a script-only threshold created: with a long enough + // PATH spliced in as `PATH=...`, a 7,999-char hook went to argv and failed + // to spawn, while the SAME hook at 8,001 chars flipped to stdin and ran. + // Size decided how a hook behaved, in the wrong direction. + seedWslGuestEnvironmentForTests(undefined, { + ...ENVIRONMENT, + path: '/opt/x/bin:'.repeat(2_500) + }) + // Deliberately UNDER any script-only threshold: 7k of script with 27k of + // PATH overflows the line, and the old rule sent exactly this to argv. + const script = `echo ${'x'.repeat(7_000)}` + expect(script.length).toBeLessThan(8_000) + await runWslProcess({ loginPath: 'preferred', shell: 'bash', script }) + const spec = runProcessMock.mock.calls.at(-1)?.[0] + expect(spec.input).toBe(script) + expect(spec.args.join(' ')).not.toContain(script) + }) + it('keeps an ordinary-sized script in argv', async () => { seedWslGuestEnvironmentForTests(undefined, ENVIRONMENT) const script = `echo ${'x'.repeat(100)}` diff --git a/src/main/wsl/wsl-runner.ts b/src/main/wsl/wsl-runner.ts index f1a51d4b9231..14b1d8d25e4d 100644 --- a/src/main/wsl/wsl-runner.ts +++ b/src/main/wsl/wsl-runner.ts @@ -169,8 +169,14 @@ function withGuestCwd(cwd: string | undefined, argv: readonly string[]): string[ * failing to spawn at all and accepting the stdin caveat. Degrading beats * failing: a large script that also reads stdin was already broken, while a * large script that does not now works where it would have died. + * + * Measured on the WHOLE command line, not on the script alone. A login PATH is + * itself a few KB and is spliced in as `PATH=...`, so a script-only threshold + * produced a perverse band: with a long enough PATH, a 7,999-char hook went to + * argv and failed to spawn while the same hook at 8,001 chars flipped to stdin + * and ran. Size decided how a hook behaved, in the wrong direction. */ -const MAX_ARGV_SCRIPT_CHARS = 8_000 +const MAX_COMMAND_LINE_CHARS = 30_000 /** ` -c`/`-s` for a script, otherwise the program itself. */ function guestCommandArgv(spec: WslSpec, delivery: 'argv' | 'stdin'): string[] { @@ -184,6 +190,16 @@ function guestCommandArgv(spec: WslSpec, delivery: 'argv' | 'stdin'): string[] { : [shell, '-c', spec.script, '--', ...(spec.args ?? [])] } +/** + * What `CreateProcess` will count, near enough to decide on. + * + * Node quotes an argument containing a space or a quote, so this over-counts + * slightly rather than under-counting -- the safe direction for a cap. + */ +function commandLineLength(args: readonly string[]): number { + return args.reduce((total, arg) => total + arg.length + 3, 0) +} + /** Shell-free argv, with the cached environment applied when one is available. */ function buildGuestArgv( environment: WslGuestEnvironment | null, @@ -229,11 +245,15 @@ export async function runWslProcess(spec: WslSpec): Promise { // the probe most often fails *because* the distro is slow, so the fallback // would hit the hazard exactly when it is worst. Run shell-free with the // distro's default PATH instead: degraded, never blocking. - // Resolved once: argv shape and stdin payload must agree, and computing it - // in both places invites them to drift. + // Build the argv form first and measure it: the env prefix is part of the + // budget, so only the finished line can say whether argv fits. Resolved once + // here because the argv shape and the stdin payload must agree. + const argvForm = buildGuestArgv(environment, spec, 'argv') const delivery: 'argv' | 'stdin' = - spec.script !== undefined && spec.script.length > MAX_ARGV_SCRIPT_CHARS ? 'stdin' : 'argv' - const argv = buildGuestArgv(environment, spec, delivery) + spec.script !== undefined && commandLineLength(argvForm) > MAX_COMMAND_LINE_CHARS + ? 'stdin' + : 'argv' + const argv = delivery === 'argv' ? argvForm : buildGuestArgv(environment, spec, 'stdin') // One budget for the whole call: the probe used to run on its own 10s timer // ahead of the timed leg, so a 5s caller could wait 15s. From 10e135c05260b6b2953deb67be2004924b041a6c Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sat, 22 Aug 2026 22:46:07 -0700 Subject: [PATCH 2/2] fix(wsl): charge quoting and measure the line that is actually spawned Two under-counts the review found in the estimator I added. The doc comment claimed it over-counts. It did not: libuv escapes every `"` and doubles a backslash run before a quote, so a quote-dense script costs more than its length. And `wsl.exe` plus `-d --exec` are prepended AFTER the measurement, so ~45 characters of the budget were never counted. Together those put a quote-heavy ~26KB script on argv and over the real 32767 ceiling -- where the old script-only rule would have sent it to stdin and it would have run. A narrower band than the one this PR removes, but the same shape of bug, so worth closing before merge rather than after. Now charges one character per `"` or backslash and measures the full spawn line. New test: 26,000 quote characters must land on stdin; verified to fail with the quoting charge removed. --- src/main/wsl/wsl-runner.test.ts | 10 ++++++++++ src/main/wsl/wsl-runner.ts | 16 +++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/main/wsl/wsl-runner.test.ts b/src/main/wsl/wsl-runner.test.ts index ab62c67ecfcf..4e6c36d45da3 100644 --- a/src/main/wsl/wsl-runner.test.ts +++ b/src/main/wsl/wsl-runner.test.ts @@ -185,6 +185,16 @@ describe('scripts', () => { expect(spec.args.join(' ')).not.toContain(script) }) + it('charges quoting, so a quote-dense script cannot slip over the real cap', async () => { + // libuv escapes every `"` and doubles backslash runs, so a script's cost is + // more than its length. Counting length alone put a quote-heavy ~26KB + // script on argv and over the 32767 ceiling. + seedWslGuestEnvironmentForTests(undefined, ENVIRONMENT) + const script = '"'.repeat(26_000) + await runWslProcess({ loginPath: 'preferred', shell: 'bash', script }) + expect(runProcessMock.mock.calls.at(-1)?.[0].input).toBe(script) + }) + it('keeps an ordinary-sized script in argv', async () => { seedWslGuestEnvironmentForTests(undefined, ENVIRONMENT) const script = `echo ${'x'.repeat(100)}` diff --git a/src/main/wsl/wsl-runner.ts b/src/main/wsl/wsl-runner.ts index 14b1d8d25e4d..6a0fbc051ac2 100644 --- a/src/main/wsl/wsl-runner.ts +++ b/src/main/wsl/wsl-runner.ts @@ -191,13 +191,16 @@ function guestCommandArgv(spec: WslSpec, delivery: 'argv' | 'stdin'): string[] { } /** - * What `CreateProcess` will count, near enough to decide on. + * What `CreateProcess` will count. * - * Node quotes an argument containing a space or a quote, so this over-counts - * slightly rather than under-counting -- the safe direction for a cap. + * libuv escapes every `"` and doubles a backslash run before a quote, so a + * quote-dense script costs more than its length. Charging one extra character + * per `"` or `\\` keeps the estimate on the safe side of the cap; an earlier + * version claimed to over-count and in fact under-counted, which put a + * quote-heavy ~26KB script on argv and over the real limit. */ function commandLineLength(args: readonly string[]): number { - return args.reduce((total, arg) => total + arg.length + 3, 0) + return args.reduce((total, arg) => total + arg.length + 3 + (arg.match(/["\\]/g)?.length ?? 0), 0) } /** Shell-free argv, with the cached environment applied when one is available. */ @@ -249,8 +252,11 @@ export async function runWslProcess(spec: WslSpec): Promise { // budget, so only the finished line can say whether argv fits. Resolved once // here because the argv shape and the stdin payload must agree. const argvForm = buildGuestArgv(environment, spec, 'argv') + // Measure what is actually spawned: `wsl.exe` and `-d --exec` are + // prepended after this point and are part of the same budget. + const fullLine = [resolveWslExecutablePath(), ...buildWslExecArgs(spec.distro, argvForm)] const delivery: 'argv' | 'stdin' = - spec.script !== undefined && commandLineLength(argvForm) > MAX_COMMAND_LINE_CHARS + spec.script !== undefined && commandLineLength(fullLine) > MAX_COMMAND_LINE_CHARS ? 'stdin' : 'argv' const argv = delivery === 'argv' ? argvForm : buildGuestArgv(environment, spec, 'stdin')