fix(core): stop repeated zsh q flags from freezing the shell scanner (#60791)

Co-authored-by: yangguan-xue <184704694+yangguan-xue@users.noreply.github.com>
Co-authored-by: Daniel Smolsky <dannysmo@gmail.com>
This commit is contained in:
yangguan-xue 2026-11-05 05:46:18 +08:00 • committed by GitHub
parent 3ca0739383
commit e0300a9bfc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 8 additions and 1 deletions

View file

@ -114,7 +114,7 @@ const FUNCTION_BODY_RE =
const DO_AHEAD_RE = /(?:[ \t\n;]|\\\n|#[^\n]*(?:\n|$))*(?:do(?=(?:\\\n)*(?:[ \t\n;{(]|$))|\{(?=(?:\\\n)*[ \t\n]))/y
const NOFORK_OPEN_RE = /\$\{(?:\\\n)*(?:[ \t\n]|\|)/y
// Zsh flags that neither evaluate nor glob a value; s and j take an argument between repeated delimiters.
const ZSH_SAFE_FLAGS_RE = /\$\{\((?:[@AaBbCcDEfFikLMmNnOopPqStUuVvWwXZz0-]|q[-+]?|[js]([^({[<\\])(?:(?!\1)[^)])*\1)*\)/y
const ZSH_SAFE_FLAGS_RE = /\$\{\((?:[@AaBbCcDEfFikLMmNnOopPqStUuVvWwXZz0-]|q[+]|[js]([^({[<\\])(?:(?!\1)[^)])*\1)*\)/y
const ZSH_QUOTED_FLAG_ARG_RE = /[js]([^({[<\\])(?:(?!\1)[^)])*\1/g
const ZSH_GLOB_MODIFIER_RE = /(?:[\^=~#+]|\\\n)*~/y
// Trailing Zsh glob qualifiers without code execution (e, +cmd), delimiters (f, u, g, P), modifiers (:), or math expressions.

View file

@ -77,6 +77,13 @@ describe("ShellScan adversarial corpus", () => {
expect(performance.now() - start).toBeLessThan(500)
})
test("scans repeated zsh q flags in bounded time", () => {
const start = performance.now()
ShellScan.scan("echo ${(" + "q".repeat(40) + "!x}", "zsh")
ShellScan.scan("echo ${(" + "q-".repeat(40) + "!x}", "zsh")
expect(performance.now() - start).toBeLessThan(500)
})
test.each([
['pwsh --command "Remove-Item victim.txt"', ["pwsh"]],
["Import-Module ./evil.psm1", ["Import-Module"]],