SLOPSHOPPER

shell-guard

Windows shell guard: fixes backslash drive paths in Bash, blocks quote-eating node -e / heredoc patterns, and catches PowerShell/Bash syntax sent to the wrong…

guardtoast
v0.2.0MITupdated 2026-10-26youllook/ClaudeMods/plugins/shell-guard
A shopper browsing a rack in a slop shop
README

shell-guard

Windows 上的 Claude Code shell 防呆 mod。在指令執行之前,攔下 Bash 和 PowerShell 最常見的環境錯誤。

A Claude Code mod that catches the most common Windows shell mistakes — before the command runs.

為什麼需要

在 Windows 上,Claude Code 有 Bash(Git Bash) 和 PowerShell 兩種 shell,模型常常把兩邊的寫法搞混,或被 Bash 的引號規則坑到。下面這些情況會靜默出錯,沒有任何錯誤訊息:

寫法實際發生的事
echo C:\Users\me\.claudeGit Bash 把反斜線吃掉,印出 C:Usersme.claude
node -e "console.log(whoami)"Bash 先執行反引號裡的指令,node 收到的程式碼已經被改壞
cat > a.sh <<EOF + $HOME寫進檔案的是展開後的值,不是原本的 $HOME
Bash 裡寫 echo $env:PATH印出 :PATH
PowerShell 裡寫 export FOO=barexport is not recognized
PowerShell 裡寫 -match "\.codex\\"雙引號裡的反斜線層數很容易算錯,$ 還會被展開,regex 靜默比對失敗

它做什麼

#工具偵測到處理方式
1BashC:\foo\bar 這種反斜線路徑改寫成 C:/foo/bar 後照常執行
2Bashnode -e "…" 或 python -c "…" 的雙引號裡有反引號或 $(擋下,請模型先把腳本寫成檔案
3Bash沒加引號的 heredoc(<<EOF),內容又有 $ 或反引號擋下,請模型改用 <<'EOF'
4BashPowerShell 語法($env:、Get-ChildItem、2>$null…)擋下,請模型改用 PowerShell 工具
5PowerShellBash 語法(export X=、/dev/null、rm -rf、if [ ]; then…)擋下,請模型改用 Bash 工具
6PowerShell-match / -replace / -split(含 -notmatch、-cmatch 等變體)的 pattern 用雙引號又含反斜線擋下,請模型改用單引號,或用 [regex]::Escape()
  • 擋下時,原因會回傳給模型,模型看到就會自己改寫重試。畫面上也會跳一個 🛡 通知。
  • 不會誤擋:單引號和雙引號裡的文字(例如 commit 訊息提到 /dev/null)、heredoc 的內容、明確轉交給另一個 shell 的指令(powershell -Command …、bash -c …)。
  • 逃生口:確實需要那種寫法時,在指令裡加上 # shell-guard: allow 就會原樣放行。

安裝

需要 Claude Code v2.1.287 以上(Mods 從這一版開始提供)。

claude plugin marketplace add youllook/ClaudeMods
claude plugin install shell-guard@claude-mods

在 session 裡也可以用 /plugin marketplace add youllook/ClaudeMods,再從 /plugin 安裝。如果 session 已經開著,裝完打 /reload-plugins 就會載入。

它能碰到什麼

claude plugin validate 的輸出:

hooks: tool.call{tool=Bash}, tool.call{tool=PowerShell}
calls: $.ui.toast

只攔 Bash 和 PowerShell 兩個工具的呼叫,唯一用到的能力是跳通知。不讀寫檔案、不執行程式、不連網路。

測試

node --experimental-strip-types --no-warnings tests/rules.test.mts

46 個案例,涵蓋上表 6 條規則,以及「不該誤擋」的情況。

授權

MIT

Source 1 files
hooks/register.ts 180 lines
1import type { Register } from 'claude-code'
2
3// shell-guard: Windows shell mistakes, caught before the command runs.
4//
5//   Bash tool
6//     1. rewrite  C:\foo\bar -> C:/foo/bar           (Git Bash eats backslashes)
7//     2. deny     node -e "..." / python -c "..." holding ` or $(   (shell eats them)
8//     3. deny     unquoted heredoc (<<EOF) whose body holds ` or $  (shell expands them)
9//     4. deny     PowerShell syntax ($env:, Get-ChildItem, 2>$null, ...)
10//   PowerShell tool
11//     5. deny     Bash syntax (export X=, /dev/null, if [ ... ]; then, ...)
12//     6. deny     -match / -replace / -split with a "double-quoted" pattern holding \
13//
14// Escape hatch: a command containing `shell-guard: allow` (e.g. as a trailing
15// comment) passes untouched, for the rare case the pattern is intended.
16
17const ALLOW = /shell-guard:\s*allow/
18
19// ---------- helpers ----------
20
21/** Split at the first heredoc operator (`<<`, not `<<<`): [command part, heredoc part]. */
22function splitHeredoc(command: string): [string, string] {
23  const m = /<<(?!<)/.exec(command)
24  return m ? [command.slice(0, m.index), command.slice(m.index)] : [command, '']
25}
26
27/** Blank out quoted spans so pattern checks only see bare shell text. */
28function stripQuoted(text: string): string {
29  return text.replace(/'[^']*'|"(?:[^"\\]|\\.)*"/g, m => ' '.repeat(m.length))
30}
31
32// ---------- 1. drive paths ----------
33
34const DRIVE_PATH = /(?<![A-Za-z0-9_])([A-Za-z]):((?:\\{1,2}[^\\\s'"`|;&<>()$]*)+)/g
35
36export function fixPaths(command: string): string {
37  const [head, tail] = splitHeredoc(command)
38  // Odd indexes are single-quoted spans: literal in bash, and where sed/regex live.
39  const fixed = head
40    .split(/('[^']*')/)
41    .map((part, i) =>
42      i % 2 === 1 ? part : part.replace(DRIVE_PATH, (_, d: string, rest: string) => `${d}:${rest.replace(/\\{1,2}/g, '/')}`),
43    )
44    .join('')
45  return fixed + tail
46}
47
48// ---------- 2. inline interpreter code ----------
49
50const INLINE = /\b(node|python3?|py)(?:\.exe)?\s+(-e|--eval|-p|--print|-c)\s+"((?:[^"\\]|\\.)*)"/g
51
52export function checkInline(command: string): string | null {
53  const [head] = splitHeredoc(command)
54  for (const m of head.matchAll(INLINE)) {
55    const body = m[3]
56    if (body.includes('`') || body.includes('$(')) {
57      return (
58        `shell-guard: \`${m[1]} ${m[2]} "..."\` 的雙引號內含反引號或 $(,Bash 會先把它當指令替換吃掉,程式碼會被靜默改壞。` +
59        `請把腳本寫成檔案(Write 工具,或 heredoc 用 <<'EOF' 單引號版)再執行該檔案。`
60      )
61    }
62  }
63  return null
64}
65
66// ---------- 3. unquoted heredoc ----------
67
68const HEREDOC = /<<(?!<)-?\s*(['"]?)([A-Za-z_][A-Za-z0-9_]*)\1/g
69
70export function checkHeredoc(command: string): string | null {
71  for (const m of command.matchAll(HEREDOC)) {
72    if (m[1]) continue // quoted delimiter: body is literal
73    const delim = m[2]
74    const after = command.slice((m.index ?? 0) + m[0].length)
75    const lines = after.split(/\r?\n/).slice(1)
76    const end = lines.findIndex(l => l.trim() === delim)
77    const body = (end === -1 ? lines : lines.slice(0, end)).join('\n')
78    if (body.includes('`') || /\$[({A-Za-z_]/.test(body)) {
79      return (
80        `shell-guard: heredoc <<${delim} 沒加引號,內容裡的反引號 / $變數 會被 Bash 展開或執行。` +
81        `寫檔請用 <<'${delim}'(單引號版)。若確實要展開變數,在指令加註解 \`# shell-guard: allow\`。`
82      )
83    }
84  }
85  return null
86}
87
88// ---------- 4. PowerShell syntax in Bash ----------
89
90const PS_IN_BASH: [RegExp, string][] = [
91  [/\$env:[A-Za-z_]/, '$env:'],
92  [/\b(Get|Set|New|Remove|Test|Copy|Move|Select|Where|ForEach|Write|Out|Invoke|Start|Stop)-[A-Z][A-Za-z]+\b/, 'PowerShell cmdlet'],
93  [/2>\$null\b|\$null\s*=|\|\s*Out-Null\b/, '$null'],
94  [/\$LASTEXITCODE\b|\$PSScriptRoot\b|\$true\b|\$false\b/, 'PowerShell 變數'],
95  [/(^|[\s;|&])(dir|type|del|copy)\s+[A-Za-z]:\\/, 'cmd 指令'],
96]
97
98export function checkPsInBash(command: string): string | null {
99  const [head] = splitHeredoc(command)
100  if (/\b(powershell|pwsh)(\.exe)?\b/i.test(head)) return null // explicitly delegating
101  const bare = stripQuoted(head)
102  for (const [re, label] of PS_IN_BASH) {
103    if (re.test(bare)) {
104      return `shell-guard: Bash 工具收到 PowerShell 語法(${label})。這是 Git Bash,請改用 PowerShell 工具,或改寫成 Bash 寫法。`
105    }
106  }
107  return null
108}
109
110// ---------- 5. Bash syntax in PowerShell ----------
111
112const BASH_IN_PS: [RegExp, string][] = [
113  [/(^|[;\n])\s*export\s+[A-Za-z_]\w*=/, 'export VAR='],
114  [/\/dev\/null\b/, '/dev/null'],
115  [/\bif\s+\[\[?\s/, 'if [ ... ]'],
116  [/;\s*then\b|(^|[;\n])\s*fi\s*($|[;\n])/, 'then / fi'],
117  [/;\s*do\b|(^|[;\n])\s*done\s*($|[;\n])/, 'do / done'],
118  [/(^|[\s;|&])(cat|ls|rm|cp|mv)\s+-[a-zA-Z]*[rfla]\b/, 'Unix 旗標 (-rf/-la)'],
119]
120
121export function checkBashInPs(command: string): string | null {
122  if (/\b(bash|sh|wsl)(\.exe)?\s+-c\b/.test(command)) return null // explicitly delegating
123  const bare = stripQuoted(command)
124  for (const [re, label] of BASH_IN_PS) {
125    if (re.test(bare)) {
126      return `shell-guard: PowerShell 工具收到 Bash 語法(${label})。請改用 Bash 工具,或改寫成 PowerShell 寫法(例如 $env:X='y'、$null、Remove-Item -Recurse -Force)。`
127    }
128  }
129  return null
130}
131
132// ---------- 6. PowerShell regex in double quotes ----------
133
134// -match, -cmatch, -inotmatch, -replace, -csplit, ... followed by a "..." pattern.
135// Inside PowerShell double quotes ` escapes and "" is a literal quote.
136const PS_REGEX_DQ = /-[ci]?(?:not)?(match|replace|split)\s+"((?:[^"`]|`.|"")*)"/gi
137
138export function checkPsRegex(command: string): string | null {
139  for (const m of command.matchAll(PS_REGEX_DQ)) {
140    if (m[2].includes('\\')) {
141      return (
142        `shell-guard: -${m[1]} 的 regex 用雙引號又含反斜線("${m[2]}"),跳脫層數很容易算錯,$ 還會被展開。` +
143        `請改用單引號 '...'(內容原樣交給 regex),或用 [regex]::Escape('字面字串') 組 pattern。`
144      )
145    }
146  }
147  return null
148}
149
150// ---------- hooks ----------
151
152export const register: Register = on => {
153  on('tool.call', { tool: 'Bash' }, ($, e, next) => {
154    if (ALLOW.test(e.command)) return next(e)
155
156    const reason = checkInline(e.command) ?? checkHeredoc(e.command) ?? checkPsInBash(e.command)
157    if (reason) {
158      $.ui.toast('🛡 shell-guard 擋下一個會出錯的 Bash 指令')
159      return { deny: reason }
160    }
161
162    const command = fixPaths(e.command)
163    if (command !== e.command) {
164      $.ui.toast('🔧 shell-guard:Windows 路徑反斜線已轉正斜線')
165      return next({ ...e, command })
166    }
167    return next(e)
168  })
169
170  on('tool.call', { tool: 'PowerShell' }, ($, e, next) => {
171    if (ALLOW.test(e.command)) return next(e)
172    const reason = checkBashInPs(e.command) ?? checkPsRegex(e.command)
173    if (reason) {
174      $.ui.toast('🛡 shell-guard 擋下一個會出錯的 PowerShell 指令')
175      return { deny: reason }
176    }
177    return next(e)
178  })
179}
180