Skip to content

fix: double whole runs of backslashes when escaping arguments - #190

Open
ogffx wants to merge 1 commit into
moxystudio:masterfrom
ogffx:fix/escape-backslash-runs
Open

ogffx wants to merge 1 commit into
moxystudio:masterfrom
ogffx:fix/escape-backslash-runs

Conversation

@ogffx

@ogffx ogffx commented Nov 9, 2026

Copy link
Copy Markdown

Fixes #188.

The regular expressions added in #160 to avoid backtracking match a single backslash (\\+? is lazy), so only one backslash of a run before " or at the end of an argument gets doubled. Arguments passed to a .cmd/.bat can then split or swallow their neighbours:

spawn.sync('node_modules/.bin/echo-cmd-shim', ['x\\\\" --foo \\\\', 'bar'])
// argv received with 7.0.6:   ["x\\\\", "--foo", "\\\"", "bar"]
// argv received with this PR: ["x\\\\\" --foo \\\\", "bar"]

Making the quantifier greedy fixes the output but brings back the quadratic case from #160 (~30 s for 160 KB of backslashes on my machine). This PR replaces both expressions with a single pass that matches each run of backslashes as a whole, so it stays linear (~1 ms for the same input).

Testing:

  • Output is identical to 7.0.3 on 300,000 random arguments (backslashes, quotes and meta chars, with and without doubleEscapeMetaChars).
  • New lib/util/escape.test.js (backslash runs before " and at the end, a linear-time check) and a Windows end-to-end test through the .cmd shim. 6 tests fail without the change; npm test passes with it (92 tests, Windows 11, Node 24), and lint passes.

This change was prepared with AI assistance. I reviewed it and ran the checks above myself.

🤖 Generated with Claude Code

The regular expressions introduced in moxystudio#160 to avoid backtracking match a
single backslash, so only one backslash of a run before a double quote or
at the end of an argument was doubled. Match whole runs in a single pass
instead, which keeps the escaping linear.

Fixes moxystudio#188

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

escapeArgument doubles only one backslash in a run before " or at end of argument, allowing argument splitting/injection on Windows

1 participant