@orchestraai/agent-tools 0.10.15 → 0.10.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (188) hide show
  1. package/.agents/plugins/marketplace.json +1 -1
  2. package/.claude-plugin/marketplace.json +1 -1
  3. package/.claude-plugin/plugin.json +1 -1
  4. package/.codex-plugin/plugin.json +1 -1
  5. package/.cursor-plugin/plugin.json +1 -1
  6. package/.devin/adapter.json +1 -1
  7. package/.hermes/adapter.json +1 -1
  8. package/.opencode/adapter.json +1 -1
  9. package/README.md +9 -0
  10. package/dist/commands/claude-setup.d.ts +1 -1
  11. package/dist/onboarding/init.js +6 -2
  12. package/dist/onboarding/init.js.map +1 -1
  13. package/dist/runtime/conversation/command-guard.js +1023 -63
  14. package/dist/runtime/conversation/command-guard.js.map +1 -1
  15. package/dist/runtime/conversation/shell-parse.d.ts +42 -0
  16. package/dist/runtime/conversation/shell-parse.js +517 -0
  17. package/dist/runtime/conversation/shell-parse.js.map +1 -0
  18. package/docs/security.md +11 -2
  19. package/install.sh +41 -7
  20. package/node_modules/@hono/node-server/dist/index.cjs +63 -17
  21. package/node_modules/@hono/node-server/dist/index.mjs +63 -17
  22. package/node_modules/@hono/node-server/dist/serve-static.cjs +1 -1
  23. package/node_modules/@hono/node-server/dist/serve-static.mjs +1 -1
  24. package/node_modules/@hono/node-server/package.json +1 -1
  25. package/node_modules/cross-spawn/README.md +19 -3
  26. package/node_modules/cross-spawn/lib/parse.js +38 -10
  27. package/node_modules/cross-spawn/lib/util/escape.js +84 -25
  28. package/node_modules/cross-spawn/lib/util/readShebang.js +8 -2
  29. package/node_modules/cross-spawn/lib/util/resolveCommand.js +8 -1
  30. package/node_modules/cross-spawn/package.json +21 -24
  31. package/node_modules/hono/README.md +2 -3
  32. package/node_modules/hono/dist/adapter/aws-lambda/handler.js +2 -2
  33. package/node_modules/hono/dist/adapter/bun/websocket.js +19 -4
  34. package/node_modules/hono/dist/adapter/lambda-edge/handler.js +5 -1
  35. package/node_modules/hono/dist/cjs/adapter/aws-lambda/handler.js +2 -2
  36. package/node_modules/hono/dist/cjs/adapter/bun/websocket.js +19 -4
  37. package/node_modules/hono/dist/cjs/adapter/lambda-edge/handler.js +5 -1
  38. package/node_modules/hono/dist/cjs/client/fetch-result-please.js +1 -1
  39. package/node_modules/hono/dist/cjs/client/utils.js +9 -5
  40. package/node_modules/hono/dist/cjs/context.js +1 -1
  41. package/node_modules/hono/dist/cjs/helper/accepts/accepts.js +7 -1
  42. package/node_modules/hono/dist/cjs/helper/css/common.js +11 -8
  43. package/node_modules/hono/dist/cjs/helper/css/index.js +1 -1
  44. package/node_modules/hono/dist/cjs/helper/proxy/index.js +2 -0
  45. package/node_modules/hono/dist/cjs/helper/route/index.js +3 -1
  46. package/node_modules/hono/dist/cjs/helper/ssg/ssg.js +5 -3
  47. package/node_modules/hono/dist/cjs/helper/streaming/sse.js +6 -2
  48. package/node_modules/hono/dist/cjs/hono-base.js +9 -10
  49. package/node_modules/hono/dist/cjs/hono.js +0 -1
  50. package/node_modules/hono/dist/cjs/jsx/base.js +51 -32
  51. package/node_modules/hono/dist/cjs/jsx/components.js +2 -2
  52. package/node_modules/hono/dist/cjs/jsx/constants.js +2 -0
  53. package/node_modules/hono/dist/cjs/jsx/context.js +15 -2
  54. package/node_modules/hono/dist/cjs/jsx/dom/components.js +1 -1
  55. package/node_modules/hono/dist/cjs/jsx/dom/hooks/index.js +38 -8
  56. package/node_modules/hono/dist/cjs/jsx/dom/intrinsic-element/components.js +8 -5
  57. package/node_modules/hono/dist/cjs/jsx/dom/render.js +9 -3
  58. package/node_modules/hono/dist/cjs/jsx/jsx-runtime.js +13 -17
  59. package/node_modules/hono/dist/cjs/middleware/basic-auth/index.js +3 -1
  60. package/node_modules/hono/dist/cjs/middleware/bearer-auth/index.js +9 -1
  61. package/node_modules/hono/dist/cjs/middleware/body-limit/index.js +9 -10
  62. package/node_modules/hono/dist/cjs/middleware/combine/index.js +4 -0
  63. package/node_modules/hono/dist/cjs/middleware/compress/index.js +2 -3
  64. package/node_modules/hono/dist/cjs/middleware/csrf/index.js +2 -2
  65. package/node_modules/hono/dist/cjs/middleware/etag/index.js +3 -1
  66. package/node_modules/hono/dist/cjs/middleware/jwk/jwk.js +2 -2
  67. package/node_modules/hono/dist/cjs/middleware/jwt/jwt.js +2 -2
  68. package/node_modules/hono/dist/cjs/middleware/method-not-allowed/index.js +1 -1
  69. package/node_modules/hono/dist/cjs/middleware/method-override/index.js +35 -12
  70. package/node_modules/hono/dist/cjs/middleware/mount/index.js +5 -4
  71. package/node_modules/hono/dist/cjs/middleware/pretty-json/index.js +1 -0
  72. package/node_modules/hono/dist/cjs/middleware/request-id/request-id.js +3 -3
  73. package/node_modules/hono/dist/cjs/middleware/secure-headers/secure-headers.js +6 -3
  74. package/node_modules/hono/dist/cjs/middleware/serve-static/index.js +4 -2
  75. package/node_modules/hono/dist/cjs/middleware/timing/timing.js +4 -3
  76. package/node_modules/hono/dist/cjs/router/reg-exp-router/node.js +13 -11
  77. package/node_modules/hono/dist/cjs/router/reg-exp-router/prepared-router.js +4 -4
  78. package/node_modules/hono/dist/cjs/router/reg-exp-router/router.js +39 -51
  79. package/node_modules/hono/dist/cjs/router/reg-exp-router/trie.js +15 -5
  80. package/node_modules/hono/dist/cjs/router/trie-router/node.js +6 -4
  81. package/node_modules/hono/dist/cjs/utils/body.js +2 -1
  82. package/node_modules/hono/dist/cjs/utils/cookie.js +3 -3
  83. package/node_modules/hono/dist/cjs/utils/html.js +5 -0
  84. package/node_modules/hono/dist/cjs/utils/jwt/jwt.js +8 -5
  85. package/node_modules/hono/dist/cjs/utils/mime.js +3 -2
  86. package/node_modules/hono/dist/cjs/utils/mount.js +59 -0
  87. package/node_modules/hono/dist/cjs/utils/url.js +23 -11
  88. package/node_modules/hono/dist/cjs/validator/validator.js +5 -5
  89. package/node_modules/hono/dist/client/fetch-result-please.js +1 -1
  90. package/node_modules/hono/dist/client/utils.js +9 -5
  91. package/node_modules/hono/dist/context.js +1 -1
  92. package/node_modules/hono/dist/helper/accepts/accepts.js +7 -1
  93. package/node_modules/hono/dist/helper/css/common.js +11 -8
  94. package/node_modules/hono/dist/helper/css/index.js +1 -1
  95. package/node_modules/hono/dist/helper/proxy/index.js +2 -0
  96. package/node_modules/hono/dist/helper/route/index.js +3 -1
  97. package/node_modules/hono/dist/helper/ssg/ssg.js +5 -3
  98. package/node_modules/hono/dist/helper/streaming/sse.js +6 -2
  99. package/node_modules/hono/dist/hono-base.js +9 -10
  100. package/node_modules/hono/dist/hono.js +0 -1
  101. package/node_modules/hono/dist/jsx/base.js +51 -34
  102. package/node_modules/hono/dist/jsx/components.js +2 -2
  103. package/node_modules/hono/dist/jsx/constants.js +2 -1
  104. package/node_modules/hono/dist/jsx/context.js +15 -2
  105. package/node_modules/hono/dist/jsx/dom/components.js +2 -2
  106. package/node_modules/hono/dist/jsx/dom/hooks/index.js +39 -9
  107. package/node_modules/hono/dist/jsx/dom/intrinsic-element/components.js +8 -5
  108. package/node_modules/hono/dist/jsx/dom/render.js +9 -3
  109. package/node_modules/hono/dist/jsx/jsx-runtime.js +15 -19
  110. package/node_modules/hono/dist/middleware/basic-auth/index.js +3 -1
  111. package/node_modules/hono/dist/middleware/bearer-auth/index.js +9 -1
  112. package/node_modules/hono/dist/middleware/body-limit/index.js +9 -10
  113. package/node_modules/hono/dist/middleware/combine/index.js +4 -0
  114. package/node_modules/hono/dist/middleware/compress/index.js +2 -3
  115. package/node_modules/hono/dist/middleware/csrf/index.js +2 -2
  116. package/node_modules/hono/dist/middleware/etag/index.js +3 -1
  117. package/node_modules/hono/dist/middleware/jwk/jwk.js +2 -2
  118. package/node_modules/hono/dist/middleware/jwt/jwt.js +2 -2
  119. package/node_modules/hono/dist/middleware/method-not-allowed/index.js +1 -1
  120. package/node_modules/hono/dist/middleware/method-override/index.js +35 -12
  121. package/node_modules/hono/dist/middleware/mount/index.js +5 -4
  122. package/node_modules/hono/dist/middleware/pretty-json/index.js +1 -0
  123. package/node_modules/hono/dist/middleware/request-id/request-id.js +3 -3
  124. package/node_modules/hono/dist/middleware/secure-headers/secure-headers.js +6 -3
  125. package/node_modules/hono/dist/middleware/serve-static/index.js +4 -2
  126. package/node_modules/hono/dist/middleware/timing/timing.js +4 -3
  127. package/node_modules/hono/dist/router/reg-exp-router/node.js +13 -11
  128. package/node_modules/hono/dist/router/reg-exp-router/prepared-router.js +5 -5
  129. package/node_modules/hono/dist/router/reg-exp-router/router.js +40 -52
  130. package/node_modules/hono/dist/router/reg-exp-router/trie.js +15 -5
  131. package/node_modules/hono/dist/router/trie-router/node.js +6 -4
  132. package/node_modules/hono/dist/types/adapter/aws-lambda/conninfo.d.ts +1 -1
  133. package/node_modules/hono/dist/types/adapter/bun/conninfo.d.ts +1 -1
  134. package/node_modules/hono/dist/types/adapter/bun/ssg.d.ts +1 -1
  135. package/node_modules/hono/dist/types/adapter/cloudflare-pages/conninfo.d.ts +1 -1
  136. package/node_modules/hono/dist/types/adapter/cloudflare-workers/conninfo.d.ts +1 -1
  137. package/node_modules/hono/dist/types/adapter/deno/conninfo.d.ts +1 -1
  138. package/node_modules/hono/dist/types/adapter/deno/ssg.d.ts +1 -1
  139. package/node_modules/hono/dist/types/adapter/lambda-edge/conninfo.d.ts +1 -1
  140. package/node_modules/hono/dist/types/adapter/netlify/conninfo.d.ts +1 -1
  141. package/node_modules/hono/dist/types/adapter/vercel/conninfo.d.ts +1 -1
  142. package/node_modules/hono/dist/types/helper/accepts/accepts.d.ts +5 -0
  143. package/node_modules/hono/dist/types/helper/factory/index.d.ts +1 -1
  144. package/node_modules/hono/dist/types/helper/ssg/ssg.d.ts +6 -1
  145. package/node_modules/hono/dist/types/helper/streaming/sse.d.ts +1 -1
  146. package/node_modules/hono/dist/types/helper/streaming/stream.d.ts +1 -1
  147. package/node_modules/hono/dist/types/helper/streaming/text.d.ts +1 -1
  148. package/node_modules/hono/dist/types/helper/testing/index.d.ts +2 -1
  149. package/node_modules/hono/dist/types/helper/websocket/index.d.ts +13 -3
  150. package/node_modules/hono/dist/types/hono-base.d.ts +23 -9
  151. package/node_modules/hono/dist/types/index.d.ts +1 -1
  152. package/node_modules/hono/dist/types/jsx/base.d.ts +6 -3
  153. package/node_modules/hono/dist/types/jsx/components.d.ts +2 -2
  154. package/node_modules/hono/dist/types/jsx/constants.d.ts +1 -0
  155. package/node_modules/hono/dist/types/jsx/context.d.ts +2 -2
  156. package/node_modules/hono/dist/types/jsx/dom/components.d.ts +2 -2
  157. package/node_modules/hono/dist/types/jsx/dom/css.d.ts +2 -2
  158. package/node_modules/hono/dist/types/jsx/dom/hooks/index.d.ts +5 -3
  159. package/node_modules/hono/dist/types/jsx/dom/index.d.ts +1 -1
  160. package/node_modules/hono/dist/types/jsx/dom/server.d.ts +1 -1
  161. package/node_modules/hono/dist/types/jsx/index.d.ts +1 -1
  162. package/node_modules/hono/dist/types/jsx/jsx-runtime.d.ts +2 -1
  163. package/node_modules/hono/dist/types/jsx/streaming.d.ts +4 -3
  164. package/node_modules/hono/dist/types/middleware/basic-auth/index.d.ts +1 -0
  165. package/node_modules/hono/dist/types/middleware/bearer-auth/index.d.ts +2 -1
  166. package/node_modules/hono/dist/types/middleware/ip-restriction/index.d.ts +3 -3
  167. package/node_modules/hono/dist/types/middleware/jsx-renderer/index.d.ts +3 -3
  168. package/node_modules/hono/dist/types/middleware/mount/index.d.ts +2 -0
  169. package/node_modules/hono/dist/types/middleware/request-id/request-id.d.ts +3 -3
  170. package/node_modules/hono/dist/types/middleware/secure-headers/secure-headers.d.ts +2 -0
  171. package/node_modules/hono/dist/types/router/reg-exp-router/node.d.ts +3 -3
  172. package/node_modules/hono/dist/types/router/reg-exp-router/trie.d.ts +1 -1
  173. package/node_modules/hono/dist/types/utils/cookie.d.ts +1 -1
  174. package/node_modules/hono/dist/types/utils/ipaddr.d.ts +1 -1
  175. package/node_modules/hono/dist/types/utils/jwt/jwt.d.ts +4 -2
  176. package/node_modules/hono/dist/types/utils/mount.d.ts +4 -0
  177. package/node_modules/hono/dist/types/utils/types.d.ts +12 -2
  178. package/node_modules/hono/dist/utils/body.js +2 -1
  179. package/node_modules/hono/dist/utils/cookie.js +3 -3
  180. package/node_modules/hono/dist/utils/html.js +5 -0
  181. package/node_modules/hono/dist/utils/jwt/jwt.js +8 -5
  182. package/node_modules/hono/dist/utils/mime.js +3 -2
  183. package/node_modules/hono/dist/utils/mount.js +58 -0
  184. package/node_modules/hono/dist/utils/url.js +23 -11
  185. package/node_modules/hono/dist/validator/validator.js +5 -5
  186. package/node_modules/hono/package.json +1 -1
  187. package/package.json +1 -1
  188. package/skills/install-agent-adapter/reference.md +8 -0
@@ -1,3 +1,4 @@
1
+ import { parseShell, ShellParseError, } from "./shell-parse.js";
1
2
  const SHELL_TOOL_NAMES = new Set([
2
3
  "bash",
3
4
  "exec_command",
@@ -6,18 +7,91 @@ const SHELL_TOOL_NAMES = new Set([
6
7
  "shell",
7
8
  "terminal",
8
9
  ]);
9
- function executableText(command) {
10
+ /** Rule metadata, in priority order. When several rules match, the first critical one wins, then the first high one. */
11
+ const RULE_INFO = {
12
+ "shell.fork-bomb": {
13
+ reason: "This command appears to create an unbounded process fork bomb.",
14
+ severity: "critical",
15
+ },
16
+ "filesystem.rm-critical-target": {
17
+ reason: "Recursive forced deletion targets a root, home, working directory, or variable-derived path.",
18
+ severity: "critical",
19
+ },
20
+ "filesystem.rm-recursive-force": {
21
+ reason: "Recursive forced deletion can permanently remove uncommitted or user data.",
22
+ severity: "high",
23
+ },
24
+ "storage.raw-device-write": {
25
+ reason: "This command formats, erases, or writes directly to a block device.",
26
+ severity: "critical",
27
+ },
28
+ "process.mass-kill": {
29
+ reason: "This command can terminate every process available to the current user or system.",
30
+ severity: "critical",
31
+ },
32
+ "git.discard-worktree": {
33
+ reason: "This Git operation can discard uncommitted work or unreachable local commits.",
34
+ severity: "high",
35
+ },
36
+ "git.rewrite-remote": {
37
+ reason: "This Git operation can overwrite remote history.",
38
+ severity: "high",
39
+ },
40
+ "database.destructive-ddl": {
41
+ reason: "This command contains destructive database DDL.",
42
+ severity: "high",
43
+ },
44
+ "infrastructure.destroy": {
45
+ reason: "This command destroys infrastructure, namespaces, or persistent container data.",
46
+ severity: "high",
47
+ },
48
+ "cloud.bulk-delete": {
49
+ reason: "This command can recursively delete cloud data or an entire cloud project.",
50
+ severity: "high",
51
+ },
52
+ "supply-chain.remote-pipe-shell": {
53
+ reason: "Piping downloaded content directly into a shell executes unreviewed remote code.",
54
+ severity: "high",
55
+ },
56
+ "shell.program-launcher-option": {
57
+ reason: "This option makes the tool run another program (for example a preprocessor, pager, or config-defined command), which the guard cannot review.",
58
+ severity: "high",
59
+ },
60
+ "shell.excessive-nesting": {
61
+ reason: "This command nests shells or substitutions too deeply for the guard to review.",
62
+ severity: "high",
63
+ },
64
+ "shell.unparsed-command": {
65
+ reason: "The guard could not parse this command, so it cannot check what it would run.",
66
+ severity: "high",
67
+ },
68
+ "shell.unresolved-command-name": {
69
+ reason: "The program name is quoted, escaped, or built from an expansion, so the guard cannot tell which program would run. Write the program name as a plain word.",
70
+ severity: "high",
71
+ },
72
+ };
73
+ const RULE_ORDER = Object.keys(RULE_INFO);
74
+ /**
75
+ * Text the pattern rules see. Normalization is for pattern matching only:
76
+ * the parser reads the raw text, because bash treats look-alike characters
77
+ * (fullwidth quotes, Unicode dashes) as ordinary letters.
78
+ */
79
+ function patternText(command) {
10
80
  return command
11
81
  .normalize("NFKC")
12
82
  .replace(/\\\r?\n/g, "")
13
- .replace(/[\u2010-\u2015\u2212]/g, "-")
83
+ .replace(/[‐-―−]/g, "-")
14
84
  .trim();
15
85
  }
16
- function isClearlyInertTextCommand(command) {
17
- if (/[;&|\n]|\$\(|`|<\(|>\(/.test(command))
18
- return false;
19
- return /^(?:(?:command\s+)?(?:rg|grep|ag|ack)\b|git\b[^\n]*\b(?:commit|log)\b|(?:echo|printf)\b)/i.test(command);
20
- }
86
+ /* ------------------------------------------------------------------------ */
87
+ /* Text-pattern rules */
88
+ /* */
89
+ /* These run over the raw command text in every case. They catch payloads */
90
+ /* the parser cannot see into, such as code passed to an interpreter */
91
+ /* (python -c, psql -c). The only text they do not see is the arguments of */
92
+ /* commands that cannot run anything (see TEXT_ONLY below), and only when */
93
+ /* every command in the line is such a command. */
94
+ /* ------------------------------------------------------------------------ */
21
95
  function invocation(command, executable) {
22
96
  const matches = [];
23
97
  const pattern = new RegExp(`\\b${executable}\\s+([^;&|\\n]+)`, "gi");
@@ -25,90 +99,67 @@ function invocation(command, executable) {
25
99
  matches.push(match[1]);
26
100
  return matches;
27
101
  }
28
- function shellWords(value) {
102
+ function patternWords(value) {
29
103
  return (value.match(/"[^"]*"|'[^']*'|\S+/g) ?? []).map((word) => word.replace(/^(?:"([\s\S]*)"|'([\s\S]*)')$/, "$1$2"));
30
104
  }
31
- function recursiveForcedRemove(command) {
105
+ function rmTargetsAreCritical(operands) {
106
+ return operands.some((operand) => {
107
+ const target = operand.replace(/\/+$/, "") || "/";
108
+ return (["/", ".", "..", "~", "$HOME", "${HOME}"].includes(target) ||
109
+ /^(?:\/|~|\$HOME|\$\{HOME\})\/\*$/.test(operand) ||
110
+ /\$(?:\{?[A-Za-z_][A-Za-z0-9_]*\}?)(?:\/|\*|$)/.test(operand));
111
+ });
112
+ }
113
+ function patternRecursiveForcedRemove(command) {
32
114
  for (const args of invocation(command, "rm")) {
33
- const words = shellWords(args);
34
- const recursive = words.some((word) => word === "--recursive" || /^-[^-]*r/.test(word));
115
+ const words = patternWords(args);
116
+ const recursive = words.some((word) => word === "--recursive" || /^-[^-]*r/i.test(word));
35
117
  const forced = words.some((word) => word === "--force" || /^-[^-]*f/.test(word));
36
118
  if (!recursive || !forced)
37
119
  continue;
38
- const operands = words.filter((word) => !word.startsWith("-"));
39
- const critical = operands.some((operand) => {
40
- const target = operand.replace(/\/+$/, "") || "/";
41
- return (["/", ".", "..", "~", "$HOME", "${HOME}"].includes(target) ||
42
- /^(?:\/|~|\$HOME|\$\{HOME\})\/\*$/.test(operand) ||
43
- /\$(?:\{?[A-Za-z_][A-Za-z0-9_]*\}?)(?:\/|\*|$)/.test(operand));
44
- });
45
- return { matched: true, critical };
120
+ return { matched: true, critical: rmTargetsAreCritical(words.filter((word) => !word.startsWith("-"))) };
46
121
  }
47
122
  return { matched: false, critical: false };
48
123
  }
49
- const RULES = [
124
+ const PATTERN_RULES = [
50
125
  {
51
126
  id: "shell.fork-bomb",
52
- reason: "This command appears to create an unbounded process fork bomb.",
53
- severity: "critical",
54
127
  matches: (command) => /:\s*\(\s*\)\s*\{\s*:\s*\|\s*:\s*&\s*\}\s*;?\s*:/.test(command),
55
128
  },
56
- {
57
- id: "filesystem.rm-critical-target",
58
- reason: "Recursive forced deletion targets a root, home, working directory, or variable-derived path.",
59
- severity: "critical",
60
- matches: (command) => recursiveForcedRemove(command).critical,
61
- },
62
- {
63
- id: "filesystem.rm-recursive-force",
64
- reason: "Recursive forced deletion can permanently remove uncommitted or user data.",
65
- severity: "high",
66
- matches: (command) => recursiveForcedRemove(command).matched,
67
- },
129
+ { id: "filesystem.rm-critical-target", matches: (command) => patternRecursiveForcedRemove(command).critical },
130
+ { id: "filesystem.rm-recursive-force", matches: (command) => patternRecursiveForcedRemove(command).matched },
68
131
  {
69
132
  id: "storage.raw-device-write",
70
- reason: "This command formats, erases, or writes directly to a block device.",
71
- severity: "critical",
72
133
  matches: (command) => /\b(?:mkfs(?:\.[A-Za-z0-9_+-]+)?|wipefs)\b/i.test(command) ||
73
134
  /\bdiskutil\s+(?:eraseDisk|zeroDisk|secureErase)\b/i.test(command) ||
74
- /\bdd\b[^;&|\n]*\bof\s*=\s*\/dev\//i.test(command),
135
+ /\bdd\b[^;&|\n]*\bof\s*=\s*\/dev\/(?!(?:null|zero|stdout|stderr|shm\/)\b)/i.test(command),
75
136
  },
76
137
  {
77
138
  id: "process.mass-kill",
78
- reason: "This command can terminate every process available to the current user or system.",
79
- severity: "critical",
80
139
  matches: (command) => /\bkill\b[^;&|\n]*(?:-9\s+-1|--signal\s+KILL\s+-1)\b/i.test(command) ||
81
140
  /\bpkill\b[^;&|\n]*\s-(?:9|KILL)\b[^;&|\n]*(?:\.\*|\^\.\*\$)/i.test(command),
82
141
  },
83
142
  {
84
143
  id: "git.discard-worktree",
85
- reason: "This Git operation can discard uncommitted work or unreachable local commits.",
86
- severity: "high",
87
144
  matches: (command) => /\bgit\b[^;&|\n]*\breset\b[^;&|\n]*--hard\b/i.test(command) ||
88
145
  /\bgit\b[^;&|\n]*\bclean\b(?=[^;&|\n]*-[A-Za-z]*f)[^;&|\n]*/i.test(command) ||
89
146
  /\bgit\b[^;&|\n]*\bcheckout\s+--(?:\s|$)/i.test(command) ||
90
147
  /\bgit\b[^;&|\n]*\brestore\b/i.test(command) ||
91
- /\bgit\b[^;&|\n]*\bbranch\b[^;&|\n]*(?:-D|--delete\s+--force)\b/i.test(command) ||
148
+ /\bgit\b[^;&|\n]*\bbranch\b[^;&|\n]*(?:-D|--delete\s+--force)\b/.test(command) ||
92
149
  /\bgit\b[^;&|\n]*\bstash\s+(?:clear|drop)\b/i.test(command),
93
150
  },
94
151
  {
95
152
  id: "git.rewrite-remote",
96
- reason: "This Git operation can overwrite remote history.",
97
- severity: "high",
98
153
  matches: (command) => /\bgit\b[^;&|\n]*\bpush\b[^;&|\n]*--force(?:=|\s|$)/i.test(command),
99
154
  },
100
155
  {
101
156
  id: "database.destructive-ddl",
102
- reason: "This command contains destructive database DDL.",
103
- severity: "high",
104
157
  matches: (command) => /\bDROP\s+(?:DATABASE|SCHEMA|TABLE)\b/i.test(command) ||
105
158
  /\bTRUNCATE\s+(?:TABLE\s+)?[A-Za-z_]/i.test(command) ||
106
159
  /\b(?:dropdb|redis-cli\s+(?:FLUSHALL|FLUSHDB))\b/i.test(command),
107
160
  },
108
161
  {
109
162
  id: "infrastructure.destroy",
110
- reason: "This command destroys infrastructure, namespaces, or persistent container data.",
111
- severity: "high",
112
163
  matches: (command) => /\b(?:terraform|tofu|pulumi)\s+destroy\b/i.test(command) ||
113
164
  /\bkubectl\b[^;&|\n]*\bdelete\s+(?:namespace|ns)\b/i.test(command) ||
114
165
  /\bdocker\s+(?:system|volume)\s+prune\b/i.test(command) ||
@@ -116,8 +167,6 @@ const RULES = [
116
167
  },
117
168
  {
118
169
  id: "cloud.bulk-delete",
119
- reason: "This command can recursively delete cloud data or an entire cloud project.",
120
- severity: "high",
121
170
  matches: (command) => /\baws\s+s3\s+rm\b[^;&|\n]*--recursive\b/i.test(command) ||
122
171
  /\bgsutil\b[^;&|\n]*\brm\b[^;&|\n]*-[A-Za-z]*r/i.test(command) ||
123
172
  /\bgcloud\s+projects\s+delete\b/i.test(command) ||
@@ -125,11 +174,929 @@ const RULES = [
125
174
  },
126
175
  {
127
176
  id: "supply-chain.remote-pipe-shell",
128
- reason: "Piping downloaded content directly into a shell executes unreviewed remote code.",
129
- severity: "high",
130
177
  matches: (command) => /\b(?:curl|wget)\b[^|\n]*\|\s*(?:sudo\s+)?(?:ba|z|k|fi)?sh\b/i.test(command),
131
178
  },
132
179
  ];
180
+ /* ------------------------------------------------------------------------ */
181
+ /* Parsed-command rules */
182
+ /* ------------------------------------------------------------------------ */
183
+ const SHELLS = new Set(["sh", "bash", "zsh", "dash", "ksh", "mksh", "ash", "fish", "yash"]);
184
+ const FETCHERS = new Set(["curl", "wget"]);
185
+ const SHELL_KEYWORDS = new Set([
186
+ "!", "{", "}", "if", "then", "else", "elif", "fi", "do", "done", "while", "until",
187
+ ]);
188
+ /** Segments that start with these words do not run their remaining words as a command. */
189
+ const NON_COMMAND_KEYWORDS = new Set(["for", "select", "case", "esac", "in"]);
190
+ /** Environment variables whose value is a command line that a later program runs. */
191
+ const COMMAND_ENV_VARS = new Set([
192
+ "GIT_PAGER", "PAGER", "GIT_EDITOR", "EDITOR", "VISUAL", "GIT_SEQUENCE_EDITOR", "GIT_EXTERNAL_DIFF",
193
+ "GIT_SSH_COMMAND", "GIT_SSH", "GIT_ASKPASS", "SSH_ASKPASS", "GIT_PROXY_COMMAND", "LESSOPEN", "LESSCLOSE",
194
+ "MANPAGER", "BROWSER", "PROMPT_COMMAND", "BASH_ENV", "ENV",
195
+ ]);
196
+ /** Environment variables that inject Git configuration. */
197
+ const GIT_CONFIG_ENV = /^(?:GIT_CONFIG_PARAMETERS|GIT_CONFIG_COUNT|GIT_CONFIG_KEY_\d+|GIT_CONFIG_VALUE_\d+|GIT_CONFIG|GIT_CONFIG_GLOBAL|GIT_CONFIG_SYSTEM)$/;
198
+ /** Git config keys that may be set with `git -c`: they only hold data, never a command. */
199
+ const GIT_SAFE_CONFIG_KEY = /^(?:color\.[a-z0-9.-]+|user\.(?:name|email)|author\.(?:name|email)|committer\.(?:name|email)|core\.(?:quotepath|autocrlf|safecrlf|filemode|ignorecase|longpaths|abbrev|precomposeunicode)|advice\.[a-z0-9-]+|init\.defaultbranch|(?:commit|tag)\.gpgsign|log\.(?:showsignature|date|decorate|follow)|safe\.directory|protocol\.version|pull\.(?:rebase|ff)|merge\.(?:ff|conflictstyle)|fetch\.(?:prune|parallel)|i18n\.[a-z]+|diff\.(?:noprefix|renames|algorithm|mnemonicprefix|colormoved)|status\.[a-z]+|column\.ui|blame\.[a-z]+|format\.pretty|gc\.auto|maintenance\.auto|submodule\.recurse|rerere\.enabled|grep\.[a-z]+|http\.(?:sslverify|postbuffer|version)|rebase\.(?:autosquash|autostash)|push\.(?:default|autosetupremote))$/;
200
+ /** Git config keys whose value is run as a program (or that pull in other config). */
201
+ const GIT_EXEC_CONFIG_KEY = /^(?:core\.(?:pager|editor|sshcommand|fsmonitor|hookspath|askpass|gitproxy)|alias\..+|pager\..+|diff\.external|diff\..+\.(?:textconv|command)|filter\..+\.(?:clean|smudge|process)|merge\..+\.driver|credential\.(?:.+\.)?helper|sequence\.editor|gpg\.(?:.+\.)?program|uploadpack\.packobjectshook|web\.browser|browser\..+\.cmd|(?:diff|merge)tool\..+\.cmd|remote\..+\.(?:uploadpack|receivepack)|include\.path|includeif\..+|sendemail\..+|ssh\.variant|interactive\.difffilter|core\.alternaterefscommand|trailer\..+\.(?:cmd|command)|gpg\.ssh\.defaultkeycommand|man\..+\.(?:cmd|path)|(?:diff|merge)tool\..+\.path|browser\..+\.path)$/;
202
+ const SAFE_COMPRESSORS = new Set([
203
+ "gzip", "gunzip", "pigz", "zstd", "unzstd", "pzstd", "xz", "unxz", "pixz", "bzip2", "bunzip2", "pbzip2",
204
+ "lz4", "lzip", "lzma", "brotli", "zcat", "compress", "uncompress",
205
+ ]);
206
+ const SAFE_DEVICE = /^\/dev\/(?:null|zero|stdin|stdout|stderr|tty\w*|fd\/\d+|random|urandom|ptmx|pts\/\d+|console|shm\/.*|(?:tcp|udp)\/.*)$/;
207
+ /**
208
+ * Commands that only read or print text and cannot run any program named in
209
+ * their arguments, with their allowed form. Their arguments are treated as
210
+ * plain text (so `rg "rm -rf" src` is not mistaken for a deletion), but only
211
+ * when every command in the line is one of these.
212
+ */
213
+ const TEXT_ONLY = new Set([
214
+ "echo", "printf", "rg", "grep", "egrep", "fgrep", "ag", "ack", "cat", "head", "tail", "wc", "ls", "pwd",
215
+ "true", "false", "sort", "uniq", "cut", "tr", "nl", "basename", "dirname", "realpath", "cd",
216
+ ]);
217
+ const TEXT_ONLY_GIT = new Set([
218
+ "status", "log", "show", "diff", "commit", "add", "grep", "blame", "shortlog", "rev-parse", "ls-files",
219
+ ]);
220
+ function basename(value) {
221
+ return (value.split("/").pop() ?? value).toLowerCase();
222
+ }
223
+ function isAssignment(word) {
224
+ return /^[A-Za-z_][A-Za-z0-9_]*(?:\[[^\]]*\])?\+?=/.test(word);
225
+ }
226
+ function longValue(arg, name, args, index) {
227
+ if (arg === name)
228
+ return args[index + 1];
229
+ if (arg.startsWith(`${name}=`))
230
+ return arg.slice(name.length + 1);
231
+ return undefined;
232
+ }
233
+ function compressorIsPlain(value) {
234
+ if (value === undefined)
235
+ return false;
236
+ const words = value.trim().split(/\s+/);
237
+ // A bare name resolved from PATH only; an explicit path could be any program.
238
+ return SAFE_COMPRESSORS.has(words[0] ?? "") && words.slice(1).every((word) => /^-[A-Za-z0-9=]+$/.test(word));
239
+ }
240
+ /** Separates option-like words from operands, honouring `--`. */
241
+ function splitOptions(args) {
242
+ const options = [];
243
+ const operands = [];
244
+ let ended = false;
245
+ for (const arg of args) {
246
+ if (!ended && arg === "--")
247
+ ended = true;
248
+ else if (!ended && arg.startsWith("-") && arg !== "-")
249
+ options.push(arg);
250
+ else
251
+ operands.push(arg);
252
+ }
253
+ return { options, operands };
254
+ }
255
+ /** Returns option words that are not the value of a preceding value-taking option. */
256
+ function optionsOutsideValues(args, takesValue) {
257
+ const options = [];
258
+ for (let i = 0; i < args.length; i += 1) {
259
+ const arg = args[i];
260
+ if (arg === "--")
261
+ break;
262
+ if (!arg.startsWith("-") || arg === "-")
263
+ continue;
264
+ options.push(arg);
265
+ if (takesValue.has(arg))
266
+ i += 1;
267
+ }
268
+ return options;
269
+ }
270
+ const RG_VALUE_OPTIONS = new Set([
271
+ "-e", "--regexp", "-f", "--file", "-g", "--glob", "--iglob", "-t", "--type", "-T", "--type-not", "-m", "--max-count",
272
+ "-A", "--after-context", "-B", "--before-context", "-C", "--context", "-M", "--max-columns", "-j", "--threads",
273
+ "-E", "--encoding", "-r", "--replace", "--type-add", "--type-clear", "--sort", "--sortr", "-d", "--max-depth",
274
+ "--max-filesize", "--path-separator", "--ignore-file", "--colors", "--color", "--context-separator",
275
+ "--field-match-separator", "--field-context-separator", "--pre-glob", "--engine", "--dfa-size-limit",
276
+ "--regex-size-limit",
277
+ ]);
278
+ const GIT_GREP_VALUE_OPTIONS = new Set(["-e", "-f", "-A", "-B", "-C", "-m", "--max-count", "--max-depth", "--threads"]);
279
+ /** Wrappers that only change how a program runs (privileges, environment, limits), not what it runs. */
280
+ const TRANSPARENT_WRAPPERS = new Set([
281
+ "command", "builtin", "exec", "nohup", "nice", "timeout", "stdbuf", "ionice", "sudo", "doas", "env", "time", "setsid",
282
+ "flock", "strace", "ltrace",
283
+ ]);
284
+ /** Compound commands that can follow `coproc NAME`. */
285
+ const COMPOUND_STARTS = new Set(["{", "while", "until", "if", "for", "select", "case"]);
286
+ const STRACE_VALUE_OPTIONS = new Set([
287
+ "-a", "-b", "-e", "-E", "-I", "-o", "-O", "-p", "-P", "-s", "-S", "-u", "-U", "-X",
288
+ "--output", "--trace", "--attach", "--user", "--env", "--signal", "--status", "--detach-on",
289
+ ]);
290
+ const LTRACE_VALUE_OPTIONS = new Set([
291
+ "-a", "-A", "-D", "-e", "-F", "-l", "-n", "-o", "-p", "-s", "-u", "-w", "-x", "--align", "--output", "--library", "--indent",
292
+ ]);
293
+ /**
294
+ * True when a word in command position names a program the guard can identify:
295
+ * an unquoted, unescaped word with no expansion, glob, or brace list.
296
+ */
297
+ function plainCommandWord(raw) {
298
+ if (!/^~?[A-Za-z0-9_.\/+:@%=\[\]!{}-]+$/.test(raw))
299
+ return false;
300
+ return !/\{[^{}]*(?:,|\.\.)[^{}]*\}/.test(raw);
301
+ }
302
+ /** True when a span can be hidden from the pattern rules: bash and the parser cannot disagree about it. */
303
+ function safeToMask(span, kind) {
304
+ if (span !== span.normalize("NFKC"))
305
+ return false;
306
+ if (kind !== "word")
307
+ return true;
308
+ // A bare word with no shell metacharacters, or one fully single- or double-quoted string that cannot expand.
309
+ return /^[^\s;|&$(){}<>`'"\\]+$/.test(span) || /^'[^'\n]*'$/.test(span) || /^"[^"$`\\\n]*"$/.test(span);
310
+ }
311
+ function shortCluster(options, letter) {
312
+ return options.some((option) => /^-[A-Za-z0-9]+$/.test(option) && option.slice(1).includes(letter));
313
+ }
314
+ class Analysis {
315
+ source;
316
+ constructor(source) {
317
+ this.source = source;
318
+ }
319
+ /** Source text of the arguments of the program currently being checked, when known. */
320
+ currentRawArgs;
321
+ findings = new Set();
322
+ extraTexts = [];
323
+ allTextOnly = true;
324
+ /** Spans of the top-level text that hold only plain text. */
325
+ maskable = [];
326
+ add(id) {
327
+ this.findings.add(id);
328
+ }
329
+ /** Parses a string the shell would run as a command line and checks it. */
330
+ nestedScript(text, depth) {
331
+ this.allTextOnly = false;
332
+ this.extraTexts.push(text);
333
+ try {
334
+ this.script(parseShell(text, 0, depth), depth, false);
335
+ }
336
+ catch (error) {
337
+ this.add(parseFailureRule(error));
338
+ }
339
+ }
340
+ script(script, depth, topLevel) {
341
+ const names = new Set();
342
+ if (topLevel) {
343
+ for (const [start, end] of script.comments) {
344
+ const before = start === 0 ? " " : this.source[start - 1];
345
+ if (/[ \t\n;&|()<>]/.test(before) && safeToMask(this.source.slice(start, end), "comment")) {
346
+ this.maskable.push([start, end]);
347
+ }
348
+ }
349
+ }
350
+ for (const pipeline of script.pipelines) {
351
+ const pipelineNames = pipeline.commands.map((command) => this.command(command, depth, topLevel));
352
+ pipelineNames.forEach((set) => set.forEach((name) => names.add(name)));
353
+ // A download piped into a shell later in the same pipeline.
354
+ const fetchAt = pipelineNames.findIndex((set) => [...set].some((name) => FETCHERS.has(name)));
355
+ if (fetchAt !== -1 && pipelineNames.slice(fetchAt + 1).some((set) => [...set].some((name) => SHELLS.has(name)))) {
356
+ this.add("supply-chain.remote-pipe-shell");
357
+ }
358
+ }
359
+ return names;
360
+ }
361
+ /** Checks one simple command. Returns the names of the programs it runs (including nested ones). */
362
+ command(command, depth, topLevel) {
363
+ const names = new Set();
364
+ const wordFetches = [];
365
+ for (const word of command.words) {
366
+ let fetches = false;
367
+ for (const sub of word.substitutions) {
368
+ const inner = this.script(sub, depth + 1, false);
369
+ inner.forEach((name) => names.add(name));
370
+ if ([...inner].some((name) => FETCHERS.has(name)))
371
+ fetches = true;
372
+ }
373
+ wordFetches.push(fetches);
374
+ }
375
+ let redirectFetches = false;
376
+ for (const redirect of command.redirects) {
377
+ for (const sub of [...(redirect.target?.substitutions ?? []), ...(redirect.heredoc?.substitutions ?? [])]) {
378
+ const inner = this.script(sub, depth + 1, false);
379
+ inner.forEach((name) => names.add(name));
380
+ if ([...inner].some((name) => FETCHERS.has(name)))
381
+ redirectFetches = true;
382
+ }
383
+ this.redirect(redirect);
384
+ }
385
+ if (command.casePattern)
386
+ return names;
387
+ // Brace expansion turns one word into several; keep a map back to the source word.
388
+ const argv = [];
389
+ const wordOf = [];
390
+ command.words.forEach((word, index) => {
391
+ for (const value of word.braceExpansion ?? [word.value]) {
392
+ argv.push(value);
393
+ wordOf.push(index);
394
+ }
395
+ });
396
+ const raws = argv.map((_, i) => command.words[wordOf[i]].raw);
397
+ const effective = this.argv(argv, depth, command, raws);
398
+ effective.names.forEach((name) => names.add(name));
399
+ // Running the output of a download: `$(curl ...)` as the command, or fed to a shell.
400
+ const commandWord = command.words.findIndex((word) => !isAssignment(word.value));
401
+ if (commandWord !== -1 && wordFetches[commandWord])
402
+ this.add("supply-chain.remote-pipe-shell");
403
+ if ((effective.scriptWords ?? []).some((index) => wordFetches[wordOf[index]]))
404
+ this.add("supply-chain.remote-pipe-shell");
405
+ if (effective.readsStdin && redirectFetches)
406
+ this.add("supply-chain.remote-pipe-shell");
407
+ if (!effective.textOnly)
408
+ this.allTextOnly = false;
409
+ else if (topLevel) {
410
+ // Only the final program's own arguments are plain text; wrapper options are not.
411
+ const firstArgument = effective.runnerIndex === undefined ? command.words.length : wordOf[effective.runnerIndex] + 1;
412
+ this.markTextOnly(command, firstArgument);
413
+ }
414
+ return names;
415
+ }
416
+ markTextOnly(command, firstArgument) {
417
+ for (const word of command.words.slice(firstArgument)) {
418
+ if (safeToMask(this.source.slice(word.start, word.end), "word"))
419
+ this.maskable.push([word.start, word.end]);
420
+ }
421
+ for (const redirect of command.redirects) {
422
+ const heredoc = redirect.heredoc;
423
+ if (heredoc?.quoted && safeToMask(this.source.slice(heredoc.start, heredoc.end), "heredoc")) {
424
+ this.maskable.push([heredoc.start, heredoc.end]);
425
+ }
426
+ }
427
+ }
428
+ redirect(redirect) {
429
+ const target = redirect.target?.value;
430
+ if (target && /[>]/.test(redirect.op) && target.startsWith("/dev/") && !SAFE_DEVICE.test(target)) {
431
+ this.add("storage.raw-device-write");
432
+ }
433
+ }
434
+ /**
435
+ * Unwraps wrappers (sudo, env, xargs, find -exec, sh -c, ...) and applies
436
+ * the per-program rules. Returns the program names involved, the final
437
+ * runner, and whether the command is text-only.
438
+ */
439
+ argv(input, depth, command, raws) {
440
+ const names = new Set();
441
+ let argv = [...input];
442
+ let textOnly = true;
443
+ let afterWrapper = false;
444
+ for (let guard = 0; guard < 32; guard += 1) {
445
+ // Leading assignments: FOO=bar cmd
446
+ while (argv.length && isAssignment(argv[0])) {
447
+ if (this.assignment(argv[0], depth))
448
+ textOnly = false;
449
+ argv = argv.slice(1);
450
+ }
451
+ if (!argv.length)
452
+ return { names, textOnly };
453
+ const index = input.length - argv.length;
454
+ const raw = raws?.[index];
455
+ if (raw !== undefined && !plainCommandWord(raw))
456
+ this.add("shell.unresolved-command-name");
457
+ // A wrapper whose options were misread would leave an option value here.
458
+ if (afterWrapper && /^(?:-|\d+$)/.test(argv[0]))
459
+ this.add("shell.unresolved-command-name");
460
+ const name = basename(argv[0]);
461
+ if (NON_COMMAND_KEYWORDS.has(name))
462
+ return { names, textOnly };
463
+ if (name === "coproc") {
464
+ // coproc [NAME] COMMAND: NAME is only present before a compound command.
465
+ argv = argv.length > 2 && COMPOUND_STARTS.has(argv[2]) ? argv.slice(2) : argv.slice(1);
466
+ textOnly = false;
467
+ continue;
468
+ }
469
+ if (name === "function") {
470
+ // `function NAME { body; }`: the body follows as its own commands.
471
+ argv = argv.slice(2);
472
+ textOnly = false;
473
+ continue;
474
+ }
475
+ if (SHELL_KEYWORDS.has(name)) {
476
+ argv = argv.slice(1);
477
+ continue;
478
+ }
479
+ names.add(name);
480
+ const args = argv.slice(1);
481
+ // The effective argv is always a suffix of the input, so positions map back to words.
482
+ const offset = input.length - args.length;
483
+ if (SHELLS.has(name)) {
484
+ const info = this.shell(args, depth, command);
485
+ return {
486
+ names,
487
+ runner: name,
488
+ textOnly: false,
489
+ scriptWords: info.scriptIndex === undefined ? [] : [offset + info.scriptIndex],
490
+ readsStdin: info.readsStdin,
491
+ };
492
+ }
493
+ if (name === "source" || name === ".") {
494
+ return { names, runner: name, textOnly: false, scriptWords: args.length ? [offset] : [], readsStdin: false };
495
+ }
496
+ if (name === "eval") {
497
+ this.nestedScript(args.join(" "), depth + 1);
498
+ return { names, runner: name, textOnly: false, scriptWords: args.map((_, i) => offset + i), readsStdin: false };
499
+ }
500
+ this.currentRawArgs = raws?.slice(offset);
501
+ const unwrapped = this.unwrap(name, args, depth);
502
+ if (unwrapped === "stop")
503
+ return { names, runner: name, textOnly: false };
504
+ if (unwrapped) {
505
+ if (!TRANSPARENT_WRAPPERS.has(name))
506
+ textOnly = false;
507
+ argv = unwrapped;
508
+ afterWrapper = true;
509
+ continue;
510
+ }
511
+ this.currentRawArgs = raws?.slice(offset);
512
+ this.programRules(name, args, depth);
513
+ if (!this.isTextOnly(name, args))
514
+ textOnly = false;
515
+ return { names, runner: name, runnerIndex: index, textOnly };
516
+ }
517
+ this.add("shell.excessive-nesting");
518
+ return { names, textOnly: false };
519
+ }
520
+ /** Records an assignment; returns true when it carries a command. */
521
+ assignment(word, depth) {
522
+ const eq = word.indexOf("=");
523
+ const name = word.slice(0, eq).replace(/\+$/, "");
524
+ const value = word.slice(eq + 1);
525
+ if (GIT_CONFIG_ENV.test(name)) {
526
+ this.add("shell.program-launcher-option");
527
+ return true;
528
+ }
529
+ if (COMMAND_ENV_VARS.has(name)) {
530
+ this.nestedScript(value, depth + 1);
531
+ return true;
532
+ }
533
+ return false;
534
+ }
535
+ /**
536
+ * For wrappers, returns the wrapped argv to keep checking. Returns "stop"
537
+ * when the wrapper's payload was handed off as a nested script. Returns
538
+ * undefined for ordinary programs.
539
+ */
540
+ unwrap(name, args, depth) {
541
+ switch (name) {
542
+ case "sudo":
543
+ case "doas":
544
+ return skipOptions(args, new Set([
545
+ "-u", "-g", "-C", "-h", "-p", "-r", "-t", "-T", "-U", "-D", "-R", "--user", "--group", "--host", "--prompt",
546
+ "--role", "--type", "--close-from", "--chdir", "--chroot", "--login-class", "--other-user", "--command-timeout",
547
+ ]));
548
+ case "time":
549
+ return skipOptions(args, new Set(["-f", "--format", "-o", "--output"]));
550
+ case "setsid":
551
+ return skipOptions(args, new Set());
552
+ case "strace": {
553
+ // strace writes to a pipe when the -o value starts with | or !.
554
+ args.forEach((arg, i) => {
555
+ const value = arg === "-o" ? args[i + 1] : longValue(arg, "--output", args, i) ?? (/^-o./.test(arg) ? arg.slice(2) : undefined);
556
+ if (value !== undefined && /^[|!]/.test(value))
557
+ this.nestedScript(value.slice(1), depth + 1);
558
+ });
559
+ return skipOptions(args, STRACE_VALUE_OPTIONS);
560
+ }
561
+ case "ltrace":
562
+ return skipOptions(args, LTRACE_VALUE_OPTIONS);
563
+ case "flock": {
564
+ // flock [options] FILE COMMAND... | flock [options] FILE -c COMMAND | flock [options] FD
565
+ for (let i = 0; i < args.length; i += 1) {
566
+ const arg = args[i];
567
+ const command = arg === "-c" ? args[i + 1] : longValue(arg, "--command", args, i);
568
+ if (command !== undefined) {
569
+ this.nestedScript(command, depth + 1);
570
+ return "stop";
571
+ }
572
+ if (["-w", "--wait", "--timeout", "-E", "--conflict-exit-code"].includes(arg)) {
573
+ i += 1;
574
+ continue;
575
+ }
576
+ if (arg.startsWith("-"))
577
+ continue;
578
+ const rest = args.slice(i + 1);
579
+ // -c only counts directly after the lock file; anything later belongs to the command.
580
+ const first = rest[0];
581
+ if (first === "-c" || first === "--command" || first?.startsWith("--command=")) {
582
+ const value = first.startsWith("--command=") ? first.slice(10) : rest[1];
583
+ if (value !== undefined)
584
+ this.nestedScript(value, depth + 1);
585
+ return "stop";
586
+ }
587
+ return rest;
588
+ }
589
+ return "stop";
590
+ }
591
+ case "env": {
592
+ const rest = [];
593
+ for (let i = 0; i < args.length; i += 1) {
594
+ const arg = args[i];
595
+ if (arg === "-S" || arg === "--split-string") {
596
+ this.nestedScript(args.slice(i + 1).join(" "), depth + 1);
597
+ return "stop";
598
+ }
599
+ if (arg.startsWith("-S") && arg.length > 2) {
600
+ this.nestedScript([arg.slice(2), ...args.slice(i + 1)].join(" "), depth + 1);
601
+ return "stop";
602
+ }
603
+ if (arg.startsWith("--split-string=")) {
604
+ this.nestedScript([arg.slice(15), ...args.slice(i + 1)].join(" "), depth + 1);
605
+ return "stop";
606
+ }
607
+ if (["-u", "--unset", "-C", "--chdir", "-P"].includes(arg)) {
608
+ i += 1;
609
+ continue;
610
+ }
611
+ if (arg.startsWith("-") && rest.length === 0)
612
+ continue;
613
+ rest.push(...args.slice(i));
614
+ break;
615
+ }
616
+ return rest;
617
+ }
618
+ case "command":
619
+ case "builtin":
620
+ if (args[0] === "-v" || args[0] === "-V")
621
+ return [];
622
+ return skipOptions(args, new Set());
623
+ case "exec":
624
+ return skipOptions(args, new Set(["-a"]));
625
+ case "nohup":
626
+ case "unbuffer":
627
+ case "busybox":
628
+ return args;
629
+ case "nice":
630
+ return skipOptions(args, new Set(["-n", "--adjustment"]));
631
+ case "ionice":
632
+ return skipOptions(args, new Set(["-c", "-n", "-p", "-P", "-u", "--class", "--classdata"]));
633
+ case "stdbuf":
634
+ return skipOptions(args, new Set(["-i", "-o", "-e", "--input", "--output", "--error"]));
635
+ case "chrt":
636
+ case "taskset":
637
+ return skipOptions(args, new Set()).slice(1);
638
+ case "timeout": {
639
+ const rest = skipOptions(args, new Set(["-s", "--signal", "-k", "--kill-after"]));
640
+ return rest.slice(1);
641
+ }
642
+ case "xargs":
643
+ return skipOptions(args, new Set(["-a", "--arg-file", "-d", "--delimiter", "-E", "-e", "-I", "-i", "-L", "-l", "-n", "--max-args", "-P", "--max-procs", "-s", "--max-chars"]));
644
+ case "watch": {
645
+ const rest = skipOptions(args, new Set(["-n", "--interval", "-d", "--differences", "-q", "--equexit"]));
646
+ this.nestedScript(rest.join(" "), depth + 1);
647
+ return "stop";
648
+ }
649
+ case "su":
650
+ case "runuser":
651
+ return this.switchUser(name, args, depth);
652
+ case "find": {
653
+ for (let i = 0; i < args.length; i += 1) {
654
+ if (["-exec", "-execdir", "-ok", "-okdir"].includes(args[i])) {
655
+ const end = args.findIndex((arg, j) => j > i && (arg === ";" || arg === "+"));
656
+ const inner = args.slice(i + 1, end === -1 ? undefined : end);
657
+ const rawArgs = this.currentRawArgs;
658
+ this.argv(inner, depth + 1, undefined, rawArgs?.slice(i + 1, end === -1 ? undefined : end));
659
+ this.allTextOnly = false;
660
+ i = end === -1 ? args.length : end;
661
+ }
662
+ }
663
+ return undefined;
664
+ }
665
+ default:
666
+ return undefined;
667
+ }
668
+ }
669
+ /** su / runuser: check -c, --command and --session-command, and runuser's direct `-u user cmd...` form. */
670
+ switchUser(name, args, depth) {
671
+ let direct = false;
672
+ for (let i = 0; i < args.length; i += 1) {
673
+ const arg = args[i];
674
+ if (arg === "--")
675
+ return direct ? args.slice(i + 1) : "stop";
676
+ const command = longValue(arg, "--command", args, i) ??
677
+ longValue(arg, "--session-command", args, i) ??
678
+ (arg === "-c" ? args[i + 1] : arg.startsWith("-c") && arg.length > 2 ? arg.slice(2) : undefined);
679
+ if (command !== undefined) {
680
+ this.nestedScript(command, depth + 1);
681
+ if (arg === "-c" || arg === "--command" || arg === "--session-command")
682
+ i += 1;
683
+ continue;
684
+ }
685
+ if (name === "runuser" && (arg === "-u" || arg === "--user")) {
686
+ direct = true;
687
+ i += 1;
688
+ continue;
689
+ }
690
+ if (["-g", "--group", "-G", "--supp-group", "-s", "--shell", "-w", "--whitelist-environment"].includes(arg)) {
691
+ i += 1;
692
+ continue;
693
+ }
694
+ if (arg.startsWith("-"))
695
+ continue;
696
+ // runuser -u USER COMMAND...: the rest is the command to run.
697
+ if (direct)
698
+ return args.slice(i);
699
+ }
700
+ return "stop";
701
+ }
702
+ /**
703
+ * Works out where a shell gets its program from: a -c string, a script
704
+ * file, or standard input. Checks the -c string, and standard input
705
+ * (heredoc or here-string) only when the shell reads its program from it.
706
+ */
707
+ shell(args, depth, command) {
708
+ let wantsScript = false;
709
+ let fromStdin = false;
710
+ let scriptIndex;
711
+ for (let i = 0; i < args.length; i += 1) {
712
+ const arg = args[i];
713
+ if (arg === "--") {
714
+ if ((wantsScript || !fromStdin) && i + 1 < args.length)
715
+ scriptIndex = i + 1;
716
+ break;
717
+ }
718
+ if (["-o", "+o", "--rcfile", "--init-file", "-O", "+O"].includes(arg)) {
719
+ i += 1;
720
+ continue;
721
+ }
722
+ if (/^[-+][A-Za-z]+$/.test(arg)) {
723
+ if (arg.startsWith("-") && arg.includes("c"))
724
+ wantsScript = true;
725
+ if (arg.startsWith("-") && arg.includes("s"))
726
+ fromStdin = true;
727
+ continue;
728
+ }
729
+ if (arg.startsWith("--"))
730
+ continue;
731
+ // With -c the first operand is the program, even when -s is also given.
732
+ if (wantsScript || !fromStdin)
733
+ scriptIndex = i;
734
+ break;
735
+ }
736
+ if (wantsScript && scriptIndex !== undefined) {
737
+ this.nestedScript(args[scriptIndex], depth + 1);
738
+ return { scriptIndex, readsStdin: false };
739
+ }
740
+ if (scriptIndex !== undefined && !wantsScript)
741
+ return { scriptIndex, readsStdin: false };
742
+ for (const redirect of command?.redirects ?? []) {
743
+ if (redirect.heredoc)
744
+ this.nestedScript(redirect.heredoc.body, depth + 1);
745
+ if (redirect.op === "<<<" && redirect.target)
746
+ this.nestedScript(redirect.target.value, depth + 1);
747
+ }
748
+ return { readsStdin: true };
749
+ }
750
+ isTextOnly(name, args) {
751
+ if (name === "git") {
752
+ const parsed = parseGit(args);
753
+ return parsed.launcher === false && TEXT_ONLY_GIT.has(parsed.subcommand ?? "");
754
+ }
755
+ return TEXT_ONLY.has(name);
756
+ }
757
+ programRules(name, args, depth) {
758
+ const { options, operands } = splitOptions(args);
759
+ const has = (...values) => options.some((option) => values.includes(option));
760
+ switch (name) {
761
+ case "rm": {
762
+ const recursive = has("--recursive") || shortCluster(options, "r") || shortCluster(options, "R");
763
+ const forced = has("--force") || shortCluster(options, "f");
764
+ if (recursive && forced) {
765
+ this.add(rmTargetsAreCritical(operands) ? "filesystem.rm-critical-target" : "filesystem.rm-recursive-force");
766
+ }
767
+ return;
768
+ }
769
+ case "dd":
770
+ if (args.some((arg) => /^of=\/dev\//i.test(arg) && !SAFE_DEVICE.test(arg.slice(3)))) {
771
+ this.add("storage.raw-device-write");
772
+ }
773
+ return;
774
+ case "wipefs":
775
+ this.add("storage.raw-device-write");
776
+ return;
777
+ case "diskutil":
778
+ if (args.some((arg) => /^(?:erasedisk|zerodisk|secureerase)$/i.test(arg)))
779
+ this.add("storage.raw-device-write");
780
+ return;
781
+ case "kill": {
782
+ const killSignal = has("-9", "-KILL", "-SIGKILL", "-s9") ||
783
+ args.some((arg, i) => ["-s", "--signal", "-n"].includes(arg) && /^(?:9|KILL|SIGKILL)$/i.test(args[i + 1] ?? ""));
784
+ if (killSignal && args.includes("-1"))
785
+ this.add("process.mass-kill");
786
+ return;
787
+ }
788
+ case "pkill":
789
+ case "killall":
790
+ if (has("-9", "-KILL", "-SIGKILL") && operands.some((arg) => arg.includes(".*")))
791
+ this.add("process.mass-kill");
792
+ return;
793
+ case "git":
794
+ this.git(args, depth);
795
+ return;
796
+ case "terraform":
797
+ case "tofu":
798
+ case "pulumi": {
799
+ const sub = operands.find((arg) => !arg.startsWith("-"));
800
+ if (sub === "destroy" || (name === "pulumi" && sub === "down") || (sub === "apply" && has("-destroy", "--destroy"))) {
801
+ this.add("infrastructure.destroy");
802
+ }
803
+ return;
804
+ }
805
+ case "kubectl": {
806
+ const at = operands.indexOf("delete");
807
+ const kind = at === -1 ? undefined : operands[at + 1];
808
+ if (kind && /^(?:namespace|namespaces|ns)(?:\/|$)/i.test(kind))
809
+ this.add("infrastructure.destroy");
810
+ return;
811
+ }
812
+ case "docker":
813
+ case "docker-compose":
814
+ case "podman": {
815
+ const pairs = operands.map((arg, i) => `${arg} ${operands[i + 1] ?? ""}`);
816
+ if (pairs.includes("system prune") || pairs.includes("volume prune"))
817
+ this.add("infrastructure.destroy");
818
+ const composeDown = (name === "docker-compose" || operands.includes("compose")) && operands.includes("down");
819
+ if (composeDown && (has("--volumes") || shortCluster(options, "v") || options.some((o) => o.startsWith("--volumes=")))) {
820
+ this.add("infrastructure.destroy");
821
+ }
822
+ return;
823
+ }
824
+ case "aws":
825
+ if (operands.includes("s3") && operands.includes("rm") && has("--recursive"))
826
+ this.add("cloud.bulk-delete");
827
+ return;
828
+ case "gsutil": {
829
+ const at = args.indexOf("rm");
830
+ if (at !== -1 && args.slice(at + 1).some((arg) => /^-[A-Za-z]*[rR]/.test(arg)))
831
+ this.add("cloud.bulk-delete");
832
+ return;
833
+ }
834
+ case "gcloud":
835
+ case "az": {
836
+ const pairs = operands.map((arg, i) => `${arg} ${operands[i + 1] ?? ""}`);
837
+ if (pairs.includes("projects delete") || pairs.includes("group delete"))
838
+ this.add("cloud.bulk-delete");
839
+ return;
840
+ }
841
+ case "dropdb":
842
+ this.add("database.destructive-ddl");
843
+ return;
844
+ case "redis-cli":
845
+ if (args.some((arg) => /^(?:flushall|flushdb)$/i.test(arg)))
846
+ this.add("database.destructive-ddl");
847
+ return;
848
+ case "rg":
849
+ if (optionsOutsideValues(args, RG_VALUE_OPTIONS).some((option) => option === "--pre" || option.startsWith("--pre="))) {
850
+ this.add("shell.program-launcher-option");
851
+ }
852
+ return;
853
+ case "ag":
854
+ case "ack":
855
+ if (options.some((option) => /^--(?:pager|output)(?:=|$)/.test(option)))
856
+ this.add("shell.program-launcher-option");
857
+ return;
858
+ case "sort":
859
+ args.forEach((arg, i) => {
860
+ const value = longValue(arg, "--compress-program", args, i);
861
+ if (value !== undefined && !compressorIsPlain(value))
862
+ this.add("shell.program-launcher-option");
863
+ });
864
+ return;
865
+ case "tar":
866
+ case "gtar":
867
+ case "bsdtar":
868
+ args.forEach((arg, i) => {
869
+ if (/^--(?:to-command|info-script|new-volume-script|rsh-command)(?:=|$)/.test(arg) || arg === "-F") {
870
+ this.add("shell.program-launcher-option");
871
+ }
872
+ if (/^--checkpoint-action=.*exec/.test(arg))
873
+ this.add("shell.program-launcher-option");
874
+ const value = arg === "-I" ? args[i + 1] : longValue(arg, "--use-compress-program", args, i);
875
+ if (value !== undefined && !compressorIsPlain(value))
876
+ this.add("shell.program-launcher-option");
877
+ });
878
+ return;
879
+ default:
880
+ if (/^mkfs(?:\..+)?$/.test(name))
881
+ this.add("storage.raw-device-write");
882
+ }
883
+ }
884
+ git(args, depth) {
885
+ const parsed = parseGit(args);
886
+ if (parsed.launcher)
887
+ this.add("shell.program-launcher-option");
888
+ const sub = parsed.subcommand;
889
+ const rest = parsed.rest;
890
+ const { options, operands } = splitOptions(rest);
891
+ const has = (...values) => options.some((option) => values.includes(option));
892
+ switch (sub) {
893
+ case "reset":
894
+ if (has("--hard"))
895
+ this.add("git.discard-worktree");
896
+ break;
897
+ case "clean":
898
+ if (has("--force") || shortCluster(options, "f"))
899
+ this.add("git.discard-worktree");
900
+ break;
901
+ case "checkout":
902
+ if (rest[0] === "--")
903
+ this.add("git.discard-worktree");
904
+ break;
905
+ case "restore":
906
+ this.add("git.discard-worktree");
907
+ break;
908
+ case "branch":
909
+ if (shortCluster(options, "D") ||
910
+ ((has("--delete") || shortCluster(options, "d")) && (has("--force") || shortCluster(options, "f")))) {
911
+ this.add("git.discard-worktree");
912
+ }
913
+ break;
914
+ case "stash":
915
+ if (operands[0] === "clear" || operands[0] === "drop")
916
+ this.add("git.discard-worktree");
917
+ break;
918
+ case "push":
919
+ if (has("--force") ||
920
+ options.some((option) => option.startsWith("--force=")) ||
921
+ shortCluster(options, "f") ||
922
+ operands.some((operand) => operand.startsWith("+"))) {
923
+ this.add("git.rewrite-remote");
924
+ }
925
+ if (options.some((option) => /^--(?:receive-pack|exec)(?:=|$)/.test(option))) {
926
+ this.add("shell.program-launcher-option");
927
+ }
928
+ break;
929
+ case "fetch":
930
+ case "pull":
931
+ case "ls-remote":
932
+ case "archive":
933
+ case "clone":
934
+ if (options.some((option) => /^--(?:upload-pack|exec)(?:=|$)/.test(option)) || (sub === "clone" && has("-u"))) {
935
+ this.add("shell.program-launcher-option");
936
+ }
937
+ break;
938
+ case "grep":
939
+ if (optionsOutsideValues(rest, GIT_GREP_VALUE_OPTIONS).some((option) => /^(?:-O.*|--open-files-in-pager(?:=.*)?)$/.test(option))) {
940
+ this.add("shell.program-launcher-option");
941
+ }
942
+ break;
943
+ case "config": {
944
+ const positional = operandsAfterValues(rest, GIT_CONFIG_VALUE_OPTIONS).filter((operand) => !["set", "add", "replace-all"].includes(operand));
945
+ const unsetting = options.some((option) => /^--(?:unset|unset-all|get|get-all|get-regexp|list|remove-section)/.test(option));
946
+ const key = positional[0]?.toLowerCase() ?? "";
947
+ const value = positional[1] ?? "";
948
+ if (!unsetting &&
949
+ positional.length >= 2 &&
950
+ (GIT_EXEC_CONFIG_KEY.test(key) || (/^submodule\..+\.update$/.test(key) && value.startsWith("!")))) {
951
+ this.add("shell.program-launcher-option");
952
+ }
953
+ break;
954
+ }
955
+ case "rebase":
956
+ rest.forEach((arg, i) => {
957
+ const value = arg === "-x" ? rest[i + 1] : /^-x./.test(arg) ? arg.slice(2) : longValue(arg, "--exec", rest, i);
958
+ if (value !== undefined)
959
+ this.nestedScript(value, depth + 1);
960
+ });
961
+ break;
962
+ case "difftool":
963
+ rest.forEach((arg, i) => {
964
+ const value = arg === "-x" ? rest[i + 1] : /^-x./.test(arg) ? arg.slice(2) : longValue(arg, "--extcmd", rest, i);
965
+ if (value !== undefined)
966
+ this.nestedScript(value, depth + 1);
967
+ });
968
+ break;
969
+ case "submodule": {
970
+ const at = rest.indexOf("foreach");
971
+ if (at !== -1) {
972
+ const command = skipOptions(rest.slice(at + 1), new Set());
973
+ this.nestedScript(command.join(" "), depth + 1);
974
+ }
975
+ break;
976
+ }
977
+ case "bisect":
978
+ if (rest[0] === "run")
979
+ this.argv(rest.slice(1), depth + 1);
980
+ break;
981
+ case "filter-branch":
982
+ this.add("shell.program-launcher-option");
983
+ break;
984
+ default:
985
+ break;
986
+ }
987
+ }
988
+ }
989
+ const GIT_CONFIG_VALUE_OPTIONS = new Set(["-f", "--file", "--blob", "--type", "-t", "--default", "--comment", "--value"]);
990
+ /** Operands of a command, skipping the values of value-taking options. */
991
+ function operandsAfterValues(args, takesValue) {
992
+ const operands = [];
993
+ for (let i = 0; i < args.length; i += 1) {
994
+ const arg = args[i];
995
+ if (arg === "--") {
996
+ operands.push(...args.slice(i + 1));
997
+ break;
998
+ }
999
+ if (arg.startsWith("-") && arg !== "-") {
1000
+ if (takesValue.has(arg))
1001
+ i += 1;
1002
+ continue;
1003
+ }
1004
+ operands.push(arg);
1005
+ }
1006
+ return operands;
1007
+ }
1008
+ function parseFailureRule(error) {
1009
+ return error instanceof ShellParseError && /nesting/.test(error.message) ? "shell.excessive-nesting" : "shell.unparsed-command";
1010
+ }
1011
+ function skipOptions(args, takesValue) {
1012
+ let i = 0;
1013
+ while (i < args.length) {
1014
+ const arg = args[i];
1015
+ if (arg === "--")
1016
+ return args.slice(i + 1);
1017
+ if (!arg.startsWith("-") || arg === "-")
1018
+ break;
1019
+ i += takesValue.has(arg) ? 2 : 1;
1020
+ }
1021
+ return args.slice(i);
1022
+ }
1023
+ /** Splits `git [global options] <subcommand> [args]` and flags global options that run programs. */
1024
+ function parseGit(args) {
1025
+ let launcher = false;
1026
+ let i = 0;
1027
+ while (i < args.length) {
1028
+ const arg = args[i];
1029
+ if (arg === "-c") {
1030
+ if (!gitConfigPairIsSafe(args[i + 1] ?? ""))
1031
+ launcher = true;
1032
+ i += 2;
1033
+ }
1034
+ else if (arg.startsWith("-c") && arg.length > 2) {
1035
+ if (!gitConfigPairIsSafe(arg.slice(2)))
1036
+ launcher = true;
1037
+ i += 1;
1038
+ }
1039
+ else if (arg === "--config-env" || arg.startsWith("--config-env=")) {
1040
+ launcher = true;
1041
+ i += arg === "--config-env" ? 2 : 1;
1042
+ }
1043
+ else if (arg.startsWith("--exec-path=")) {
1044
+ launcher = true;
1045
+ i += 1;
1046
+ }
1047
+ else if (["-C", "--git-dir", "--work-tree", "--namespace", "--super-prefix"].includes(arg)) {
1048
+ i += 2;
1049
+ }
1050
+ else if (arg.startsWith("-")) {
1051
+ i += 1;
1052
+ }
1053
+ else {
1054
+ break;
1055
+ }
1056
+ }
1057
+ return { subcommand: args[i]?.toLowerCase(), rest: args.slice(i + 1), launcher };
1058
+ }
1059
+ function gitConfigPairIsSafe(pair) {
1060
+ const key = pair.split("=")[0].toLowerCase();
1061
+ return GIT_SAFE_CONFIG_KEY.test(key);
1062
+ }
1063
+ /** Blanks out the given [start, end) spans, keeping newlines so line structure is preserved. */
1064
+ function maskSpans(text, spans) {
1065
+ const units = text.split("");
1066
+ for (const [start, end] of spans) {
1067
+ for (let i = Math.max(0, start); i < Math.min(units.length, end); i += 1) {
1068
+ if (units[i] !== "\n")
1069
+ units[i] = " ";
1070
+ }
1071
+ }
1072
+ return units.join("");
1073
+ }
1074
+ function pickRule(findings) {
1075
+ const ordered = RULE_ORDER.filter((id) => findings.has(id));
1076
+ return ordered.find((id) => RULE_INFO[id].severity === "critical") ?? ordered[0];
1077
+ }
1078
+ /** Checks a raw command line and returns every rule it matches. */
1079
+ function findRules(command) {
1080
+ const analysis = new Analysis(command);
1081
+ let parsed = false;
1082
+ try {
1083
+ analysis.script(parseShell(command), 0, true);
1084
+ parsed = true;
1085
+ }
1086
+ catch (error) {
1087
+ // A line the guard cannot parse is not allowed: bash may still run the part before the error.
1088
+ analysis.add(parseFailureRule(error));
1089
+ }
1090
+ // Pattern rules always run. Plain-text arguments are hidden from them only
1091
+ // when the whole line parsed and every command in it is text-only.
1092
+ const visible = parsed && analysis.allTextOnly ? maskSpans(command, analysis.maskable) : command;
1093
+ for (const text of [visible, ...analysis.extraTexts].map(patternText)) {
1094
+ for (const rule of PATTERN_RULES)
1095
+ if (rule.matches(text))
1096
+ analysis.add(rule.id);
1097
+ }
1098
+ return analysis.findings;
1099
+ }
133
1100
  export function shellCommandFromToolCall(toolName, input) {
134
1101
  if (!SHELL_TOOL_NAMES.has(toolName.toLowerCase()))
135
1102
  return undefined;
@@ -154,18 +1121,11 @@ export function classifyShellToolCall(toolName, input) {
154
1121
  severity: "high",
155
1122
  };
156
1123
  }
157
- const command = executableText(raw);
158
- if (isClearlyInertTextCommand(command))
1124
+ const match = pickRule(findRules(raw.trim()));
1125
+ if (!match)
159
1126
  return { decision: "allow" };
160
- const match = RULES.find((rule) => rule.matches(command));
161
- return match
162
- ? {
163
- decision: "block",
164
- rule_id: match.id,
165
- reason: match.reason,
166
- severity: match.severity,
167
- }
168
- : { decision: "allow" };
1127
+ const info = RULE_INFO[match];
1128
+ return { decision: "block", rule_id: match, reason: info.reason, severity: info.severity };
169
1129
  }
170
1130
  export function commandGuardBlockMessage(decision) {
171
1131
  return `Blocked by Orchestra command guard [${decision.rule_id}]: ${decision.reason} Review and run this command manually if the destructive action is intentional.`;