diff options
| author | Claude <noreply@anthropic.com> | 2026-12-04 21:58:30 +0000 |
|---|---|---|
| committer | Claude <noreply@anthropic.com> | 2026-12-04 21:58:30 +0000 |
| commit | 621af087ba003ee12e547725b865a18b2c028086 (patch) | |
| tree | 902ef148e9827cde9acb9a4c407474cf1c9c488d | |
| parent | ba22adefe1bb578d46fa875022c7f55f4e56dda4 (diff) | |
Keep custom rule filters on the path as given, and know more shellsmhegazy/sg-edit-warnings
Co-authored-by: mhegazy <mhegazy@anthropic.com>
4 files changed, 22 insertions, 5 deletions
diff --git a/plugins/security-guidance/hooks/patterns.py b/plugins/security-guidance/hooks/patterns.py index aa33e176..0fdecebe 100644 --- a/plugins/security-guidance/hooks/patterns.py +++ b/plugins/security-guidance/hooks/patterns.py @@ -73,8 +73,8 @@ def _child_process_exec(): one of those has to be named shortly after the call, or before it (options built first). Every repeat is bounded. """ - shell = (r"(?:\bshell\b(?!\s{0,20}:\s{0,20}false)" - r"|[\x22\x27`/\\](?:(?:ba|z|da|k)?sh|cmd|powershell|pwsh)(?:\.exe)?[\x22\x27`])") + shell = (r"(?i:\b(?:shell\b(?![\x22\x27]?\s{0,20}:\s{0,20}false)|comspec\b)" + r"|[\x22\x27`/\\](?:(?:ba|z|da|k|a|fi|c|tc)?sh|cmd|powershell|pwsh)(?:\.exe)?[\x22\x27`])") return (r"child_process\.exec(?!File)" rf"|child_process\.execFile(?=[\s\S]{{0,400}}?{shell})" rf"|{shell}[\s\S]{{0,400}}?child_process\.execFile") diff --git a/plugins/security-guidance/hooks/security_reminder_hook.py b/plugins/security-guidance/hooks/security_reminder_hook.py index 15ed3f4c..298387e2 100755 --- a/plugins/security-guidance/hooks/security_reminder_hook.py +++ b/plugins/security-guidance/hooks/security_reminder_hook.py @@ -755,8 +755,7 @@ def _notebook_cell_sources(content): def check_patterns(file_path, content): """Check if file path or content matches any security patterns. Returns ALL matches.""" - # Claude Code sends backslashes on Windows; the rules are written with "/". - normalized_path = file_path.replace("\\", "/").lstrip("/") + normalized_path = file_path.lstrip("/") matches = [] # A saved notebook is JSON: its string escapes hide code from the rules @@ -784,7 +783,9 @@ def check_patterns(file_path, content): if "path_check" in pattern: try: - if pattern["path_check"](normalized_path): + # Claude Code sends backslashes on Windows. Only here: the + # filter of a custom rule has to see the path as it is. + if pattern["path_check"](normalized_path.replace("\\", "/")): matched = True except Exception: pass diff --git a/plugins/security-guidance/tests/test_pattern_false_positives.py b/plugins/security-guidance/tests/test_pattern_false_positives.py index 1a22d215..0f6176e8 100644 --- a/plugins/security-guidance/tests/test_pattern_false_positives.py +++ b/plugins/security-guidance/tests/test_pattern_false_positives.py @@ -38,6 +38,7 @@ QUIET = [ ("t.js", "child_process.execFileSync('git', ['status'])\n"), ("t.js", "const run = util.promisify(child_process.execFile)\n"), ("t.js", "child_process.execFile('git', [a], { shell: false })\n"), + ("t.js", "child_process.execFile('git', [a], { 'shell': false })\n"), ("t.js", "// use child_process.execFile instead\n"), ] @@ -106,6 +107,11 @@ CAUGHT = [ (EXEC, "c.js", "child_process.execFile('sh', ['-c', cmd], cb)\n"), (EXEC, "c.js", "child_process.execFileSync('/bin/bash', ['-lc', cmd])\n"), (EXEC, "c.js", "child_process.execFile('cmd.exe', ['/c', cmd])\n"), + (EXEC, "c.js", "child_process.execFile('CMD.EXE', ['/c', cmd])\n"), + (EXEC, "c.js", "child_process.execFile('/bin/ash', ['-c', cmd])\n"), + (EXEC, "c.js", "child_process.execFile('fish', ['-c', cmd])\n"), + (EXEC, "c.js", "child_process.execFile(process.env.SHELL, ['-c', cmd])\n"), + (EXEC, "c.js", "child_process.execFileSync(process.env.ComSpec, ['/c', cmd])\n"), (EXEC, "c.js", "options.shell = true\nchild_process.execFileSync(command, args, options)\n"), (EXEC, "c.js", "const s = '/bin/sh'\nchild_process.execFile(s, ['-c', cmd])\n"), ] diff --git a/plugins/security-guidance/tests/test_workflow_reminder.py b/plugins/security-guidance/tests/test_workflow_reminder.py index ee206c38..002e9b2f 100644 --- a/plugins/security-guidance/tests/test_workflow_reminder.py +++ b/plugins/security-guidance/tests/test_workflow_reminder.py @@ -30,6 +30,16 @@ def test_other_files_get_none(path): assert hook.check_patterns(path, OLD) == [] +def test_custom_rule_filters_get_the_path_as_it_is(monkeypatch): + """With slashes a Windows network path would no longer look absolute.""" + seen = [] + monkeypatch.setattr(hook.extensibility, "user_patterns", lambda: [ + {"ruleName": "user:r", "reminder": "r", "substrings": ["x"], "_source": "t", + "path_filter": lambda p: seen.append(p)}]) + hook.check_patterns("\\\\server\\share\\a.py", "x") + assert seen == ["\\\\server\\share\\a.py"] + + @pytest.fixture def edit(hook_env, tmp_path): repo = make_repo(tmp_path / "wf-repo", { |
