@arcforge/types 2.0.176 → 2.0.178

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arcforge/types",
3
- "version": "2.0.176",
3
+ "version": "2.0.178",
4
4
  "type": "module",
5
5
  "private": false,
6
6
  "main": "./src/index.ts",
@@ -36,6 +36,64 @@ const SHELLS = new Set([
36
36
  */
37
37
  const WRAPPERS = new Set(["env", "nice", "nohup", "stdbuf", "setsid", "time", "timeout", "xargs"])
38
38
 
39
+ /**
40
+ * Characters that make a command line a SHELL PROGRAM rather than one call.
41
+ *
42
+ * `&&`, `;`, `|`, `||` chain commands; `$(…)` and backticks substitute one;
43
+ * `>`/`<` redirect; `&` backgrounds. Every one of them means the string
44
+ * executes something the first token does not name.
45
+ *
46
+ * This is not a lint. `process.run()` and `process.spawn()` hand the WHOLE
47
+ * string to `/bin/sh -c`, so a policy that inspected only the first program
48
+ * was deciding about `pwd` while the shell ran `pwd && git push --force`.
49
+ * Every deny was one `&&` away from being bypassed:
50
+ *
51
+ * process.run("git status") → denied, correctly
52
+ * process.run("pwd && git status") → ALLOWED, ran git
53
+ *
54
+ * Detected here rather than by parsing, because parsing is the trap the file
55
+ * header already names: a matcher that understands four spellings is defeated
56
+ * by the fifth. The honest answer is that a string containing these is a shell
57
+ * invocation, and shells are governed by `raw` — which exists, and already
58
+ * escalates to the user.
59
+ */
60
+ const SHELL_META = /[;&|<>`$()\n]/
61
+
62
+ /**
63
+ * Does this COMMAND STRING invoke a shell?
64
+ *
65
+ * Answered on the raw string, never on split tokens, because splitting
66
+ * destroys both halves of the question:
67
+ *
68
+ * - it drops newlines as ordinary whitespace, so `"pwd\ngit status"` — two
69
+ * commands to `/bin/sh -c` — arrives as three innocent tokens;
70
+ * - it strips quotes, so `echo "a && b"` yields a token containing `&&`
71
+ * that a token-level test reads as a chain, prompting on a command that
72
+ * does exactly one thing.
73
+ *
74
+ * So quoted regions are skipped and everything outside them is examined —
75
+ * which is precisely the distinction a shell itself makes.
76
+ */
77
+ export function isShellCommand(command: string): boolean {
78
+ let quote: '"' | "'" | null = null
79
+
80
+ for (let i = 0; i < command.length; i++) {
81
+ const char = command[i]!
82
+ if (quote) {
83
+ // A backslash-escaped quote inside a double-quoted region does not
84
+ // close it. Single quotes take no escapes, exactly as sh does.
85
+ if (char === "\\" && quote === '"') { i++; continue }
86
+ if (char === quote) quote = null
87
+ continue
88
+ }
89
+ if (char === '"' || char === "'") { quote = char; continue }
90
+ // Escaped outside quotes: `\&` is a literal ampersand, not an operator.
91
+ if (char === "\\") { i++; continue }
92
+ if (SHELL_META.test(char)) return true
93
+ }
94
+ return false
95
+ }
96
+
39
97
  /** A program name stripped of its path: `/usr/bin/git` → `git`. */
40
98
  export function programName(program: string): string {
41
99
  const base = program.split("/").pop() ?? program
@@ -109,11 +167,25 @@ export function decideShell(
109
167
  * unconfigured capsule, the other hands an unconfigured sandbox a shell.
110
168
  */
111
169
  fallback: "allow" | "deny" = "deny",
170
+ /**
171
+ * The command as the caller received it, before splitting.
172
+ *
173
+ * Required to see a chain. `process.run()`/`spawn()` hand the whole string
174
+ * to `/bin/sh -c`, and splitting destroys the evidence — a newline becomes
175
+ * whitespace, a quote disappears. Callers holding a string MUST pass it;
176
+ * a caller holding genuine argv (never a shell line) may omit it.
177
+ */
178
+ command?: string,
112
179
  ): ShellDecision {
113
180
  // No shell block at all: the caller's own default posture decides.
114
181
  if (!shell) return { verdict: fallback, program: programName(argv[0] ?? ""), reason: "no-policy" }
115
182
 
116
- const { program, shell: isShell } = resolveProgram(argv)
183
+ const { program, shell: resolvedShell } = resolveProgram(argv)
184
+
185
+ // A chained/redirected/substituted command line is a SHELL PROGRAM, whatever
186
+ // its first token is — see isShellCommand. Without this the decision was
187
+ // made about `pwd` while `/bin/sh -c` ran `pwd && git push --force`.
188
+ const isShell = resolvedShell || (command !== undefined && isShellCommand(command))
117
189
 
118
190
  /**
119
191
  * A shell turns one grant into arbitrary execution. Gated on its own switch