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"]],