From e0300a9bfc849b2cf06b6507982977e86993a302 Mon Sep 17 00:00:00 2001 From: yangguan-xue Date: Thu, 5 Nov 2026 05:46:18 +0800 Subject: [PATCH] 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 --- packages/core/src/shell/scan.ts | 2 +- packages/core/test/shell-scan/adversarial.test.ts | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/core/src/shell/scan.ts b/packages/core/src/shell/scan.ts index b6537ffdb48..828ad20ff45 100644 --- a/packages/core/src/shell/scan.ts +++ b/packages/core/src/shell/scan.ts @@ -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. diff --git a/packages/core/test/shell-scan/adversarial.test.ts b/packages/core/test/shell-scan/adversarial.test.ts index a65a08f6114..4a64505a458 100644 --- a/packages/core/test/shell-scan/adversarial.test.ts +++ b/packages/core/test/shell-scan/adversarial.test.ts @@ -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"]],