context-mode 0.9.16 → 0.9.18
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/.claude-plugin/hooks/hooks.json +38 -0
- package/.claude-plugin/marketplace.json +1 -1
- package/.claude-plugin/plugin.json +2 -2
- package/LICENSE +94 -21
- package/README.md +130 -4
- package/build/cli.js +54 -1
- package/build/executor.js +50 -7
- package/build/runtime.js +37 -1
- package/build/security.d.ts +120 -0
- package/build/security.js +466 -0
- package/build/server.js +196 -67
- package/build/store.d.ts +8 -0
- package/build/store.js +316 -109
- package/hooks/hooks.json +38 -0
- package/hooks/pretooluse.mjs +259 -134
- package/hooks/routing-block.mjs +47 -0
- package/hooks/sessionstart.mjs +30 -0
- package/package.json +8 -3
- package/server.bundle.mjs +149 -136
- package/skills/context-mode/SKILL.md +20 -1
- package/start.mjs +11 -9
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
export type PermissionDecision = "allow" | "deny" | "ask";
|
|
2
|
+
export interface SecurityPolicy {
|
|
3
|
+
allow: string[];
|
|
4
|
+
deny: string[];
|
|
5
|
+
ask: string[];
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Extract the glob from a Bash permission pattern.
|
|
9
|
+
* "Bash(sudo *)" returns "sudo *", "Read(.env)" returns null.
|
|
10
|
+
*/
|
|
11
|
+
export declare function parseBashPattern(pattern: string): string | null;
|
|
12
|
+
/**
|
|
13
|
+
* Parse any tool permission pattern like "ToolName(glob)".
|
|
14
|
+
* Returns { tool, glob } or null if not a valid pattern.
|
|
15
|
+
*/
|
|
16
|
+
export declare function parseToolPattern(pattern: string): {
|
|
17
|
+
tool: string;
|
|
18
|
+
glob: string;
|
|
19
|
+
} | null;
|
|
20
|
+
/**
|
|
21
|
+
* Convert a Bash permission glob to a regex.
|
|
22
|
+
*
|
|
23
|
+
* Two formats:
|
|
24
|
+
* - Colon: "tree:*" becomes /^tree(\s.*)?$/ (command with optional args)
|
|
25
|
+
* - Space: "sudo *" becomes /^sudo .*$/ (literal glob match)
|
|
26
|
+
*/
|
|
27
|
+
export declare function globToRegex(glob: string, caseInsensitive?: boolean): RegExp;
|
|
28
|
+
/**
|
|
29
|
+
* Convert a file path glob to a regex.
|
|
30
|
+
*
|
|
31
|
+
* Unlike `globToRegex` (which handles command patterns with colon and
|
|
32
|
+
* space semantics), this handles file path globs where:
|
|
33
|
+
* - `**` matches any number of path segments (including zero)
|
|
34
|
+
* - `*` matches anything except path separators
|
|
35
|
+
* - Paths are matched with forward slashes (callers normalize first)
|
|
36
|
+
*/
|
|
37
|
+
export declare function fileGlobToRegex(glob: string, caseInsensitive?: boolean): RegExp;
|
|
38
|
+
/**
|
|
39
|
+
* Check if a command matches any Bash pattern in the list.
|
|
40
|
+
* Returns the matching pattern string, or null.
|
|
41
|
+
*/
|
|
42
|
+
export declare function matchesAnyPattern(command: string, patterns: string[], caseInsensitive?: boolean): string | null;
|
|
43
|
+
/**
|
|
44
|
+
* Split a shell command on chain operators (&&, ||, ;, |) while
|
|
45
|
+
* respecting single/double quotes and backticks.
|
|
46
|
+
*
|
|
47
|
+
* "echo hello && sudo rm -rf /" → ["echo hello", "sudo rm -rf /"]
|
|
48
|
+
*
|
|
49
|
+
* This prevents bypassing deny patterns by prepending innocent commands.
|
|
50
|
+
*/
|
|
51
|
+
export declare function splitChainedCommands(command: string): string[];
|
|
52
|
+
/**
|
|
53
|
+
* Read Bash permission policies from up to 3 settings files.
|
|
54
|
+
*
|
|
55
|
+
* Returns policies in precedence order (most local first):
|
|
56
|
+
* 1. .claude/settings.local.json (project-local)
|
|
57
|
+
* 2. .claude/settings.json (project-shared)
|
|
58
|
+
* 3. ~/.claude/settings.json (global)
|
|
59
|
+
*
|
|
60
|
+
* Missing or invalid files are silently skipped.
|
|
61
|
+
*/
|
|
62
|
+
export declare function readBashPolicies(projectDir?: string, globalSettingsPath?: string): SecurityPolicy[];
|
|
63
|
+
/**
|
|
64
|
+
* Read deny patterns for a specific tool from settings files.
|
|
65
|
+
*
|
|
66
|
+
* Reads the same 3-tier settings as `readBashPolicies`, but extracts
|
|
67
|
+
* only deny globs for the given tool. Used for Read and Grep enforcement
|
|
68
|
+
* — checks if file paths should be blocked by deny patterns.
|
|
69
|
+
*
|
|
70
|
+
* Returns an array of arrays (one per settings file, in precedence order).
|
|
71
|
+
* Each inner array contains the extracted glob strings.
|
|
72
|
+
*/
|
|
73
|
+
export declare function readToolDenyPatterns(toolName: string, projectDir?: string, globalSettingsPath?: string): string[][];
|
|
74
|
+
interface CommandDecision {
|
|
75
|
+
decision: PermissionDecision;
|
|
76
|
+
matchedPattern?: string;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Evaluate a command against policies in precedence order.
|
|
80
|
+
*
|
|
81
|
+
* Splits chained commands (&&, ||, ;, |) and checks each segment
|
|
82
|
+
* against deny patterns — prevents bypassing deny by prepending
|
|
83
|
+
* innocent commands like "echo ok && sudo rm -rf /".
|
|
84
|
+
*
|
|
85
|
+
* Within each policy: deny > ask > allow (most restrictive wins).
|
|
86
|
+
* First definitive match across policies wins.
|
|
87
|
+
* Default (no match in any policy): "ask".
|
|
88
|
+
*/
|
|
89
|
+
export declare function evaluateCommand(command: string, policies: SecurityPolicy[], caseInsensitive?: boolean): CommandDecision;
|
|
90
|
+
/**
|
|
91
|
+
* Server-side variant: only enforce deny patterns.
|
|
92
|
+
*
|
|
93
|
+
* The server has no UI for "ask" prompts, so allow/ask patterns are
|
|
94
|
+
* irrelevant. Returns "deny" if any deny pattern matches, otherwise "allow".
|
|
95
|
+
*
|
|
96
|
+
* Also splits chained commands to prevent bypass.
|
|
97
|
+
*/
|
|
98
|
+
export declare function evaluateCommandDenyOnly(command: string, policies: SecurityPolicy[], caseInsensitive?: boolean): {
|
|
99
|
+
decision: "deny" | "allow";
|
|
100
|
+
matchedPattern?: string;
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* Check if a file path should be denied based on deny globs.
|
|
104
|
+
*
|
|
105
|
+
* Normalizes backslashes to forward slashes before matching so that
|
|
106
|
+
* Windows paths work with Unix-style glob patterns.
|
|
107
|
+
*/
|
|
108
|
+
export declare function evaluateFilePath(filePath: string, denyGlobs: string[][], caseInsensitive?: boolean): {
|
|
109
|
+
denied: boolean;
|
|
110
|
+
matchedPattern?: string;
|
|
111
|
+
};
|
|
112
|
+
/**
|
|
113
|
+
* Scan non-shell code for shell-escape calls and extract the embedded
|
|
114
|
+
* command strings.
|
|
115
|
+
*
|
|
116
|
+
* Returns an array of command strings found in the code. For unknown
|
|
117
|
+
* languages or code without shell-escape calls, returns an empty array.
|
|
118
|
+
*/
|
|
119
|
+
export declare function extractShellCommands(code: string, language: string): string[];
|
|
120
|
+
export {};
|
|
@@ -0,0 +1,466 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
// ==============================================================================
|
|
5
|
+
// Pattern Parsing
|
|
6
|
+
// ==============================================================================
|
|
7
|
+
/**
|
|
8
|
+
* Extract the glob from a Bash permission pattern.
|
|
9
|
+
* "Bash(sudo *)" returns "sudo *", "Read(.env)" returns null.
|
|
10
|
+
*/
|
|
11
|
+
export function parseBashPattern(pattern) {
|
|
12
|
+
// .+ is greedy: for "Bash(echo (foo))" it captures "echo (foo)"
|
|
13
|
+
// because $ forces the final \) to match only the last paren.
|
|
14
|
+
const match = pattern.match(/^Bash\((.+)\)$/);
|
|
15
|
+
return match ? match[1] : null;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Parse any tool permission pattern like "ToolName(glob)".
|
|
19
|
+
* Returns { tool, glob } or null if not a valid pattern.
|
|
20
|
+
*/
|
|
21
|
+
export function parseToolPattern(pattern) {
|
|
22
|
+
// .+ is greedy: for "Read(some(path))" it captures "some(path)"
|
|
23
|
+
// because $ forces the final \) to match only the last paren.
|
|
24
|
+
const match = pattern.match(/^(\w+)\((.+)\)$/);
|
|
25
|
+
return match ? { tool: match[1], glob: match[2] } : null;
|
|
26
|
+
}
|
|
27
|
+
// ==============================================================================
|
|
28
|
+
// Glob-to-Regex Conversion
|
|
29
|
+
// ==============================================================================
|
|
30
|
+
/** Escape all regex special characters (including *). */
|
|
31
|
+
function escapeRegex(str) {
|
|
32
|
+
return str.replace(/[.*+?^${}()|[\]\\\/\-]/g, "\\$&");
|
|
33
|
+
}
|
|
34
|
+
/** Escape regex specials except *, then convert * to .* */
|
|
35
|
+
function convertGlobPart(glob) {
|
|
36
|
+
return glob
|
|
37
|
+
.replace(/[.+?^${}()|[\]\\\/\-]/g, "\\$&")
|
|
38
|
+
.replace(/\*/g, ".*");
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Convert a Bash permission glob to a regex.
|
|
42
|
+
*
|
|
43
|
+
* Two formats:
|
|
44
|
+
* - Colon: "tree:*" becomes /^tree(\s.*)?$/ (command with optional args)
|
|
45
|
+
* - Space: "sudo *" becomes /^sudo .*$/ (literal glob match)
|
|
46
|
+
*/
|
|
47
|
+
export function globToRegex(glob, caseInsensitive = false) {
|
|
48
|
+
let regexStr;
|
|
49
|
+
const colonIdx = glob.indexOf(":");
|
|
50
|
+
if (colonIdx !== -1) {
|
|
51
|
+
// Colon format: "command:argsGlob"
|
|
52
|
+
const command = glob.slice(0, colonIdx);
|
|
53
|
+
const argsGlob = glob.slice(colonIdx + 1);
|
|
54
|
+
const escapedCmd = escapeRegex(command);
|
|
55
|
+
const argsRegex = convertGlobPart(argsGlob);
|
|
56
|
+
// Match command alone OR command + space + args
|
|
57
|
+
regexStr = `^${escapedCmd}(\\s${argsRegex})?$`;
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
// Plain glob: "sudo *", "ls*", "* commit *"
|
|
61
|
+
regexStr = `^${convertGlobPart(glob)}$`;
|
|
62
|
+
}
|
|
63
|
+
return new RegExp(regexStr, caseInsensitive ? "i" : "");
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Convert a file path glob to a regex.
|
|
67
|
+
*
|
|
68
|
+
* Unlike `globToRegex` (which handles command patterns with colon and
|
|
69
|
+
* space semantics), this handles file path globs where:
|
|
70
|
+
* - `**` matches any number of path segments (including zero)
|
|
71
|
+
* - `*` matches anything except path separators
|
|
72
|
+
* - Paths are matched with forward slashes (callers normalize first)
|
|
73
|
+
*/
|
|
74
|
+
export function fileGlobToRegex(glob, caseInsensitive = false) {
|
|
75
|
+
let regexStr = "";
|
|
76
|
+
let i = 0;
|
|
77
|
+
while (i < glob.length) {
|
|
78
|
+
// Handle ** (globstar): match any number of directory segments
|
|
79
|
+
if (glob[i] === "*" && glob[i + 1] === "*") {
|
|
80
|
+
// **/ at the start or after a slash means "zero or more directories"
|
|
81
|
+
if (i + 2 < glob.length && glob[i + 2] === "/") {
|
|
82
|
+
regexStr += "(.*/)?";
|
|
83
|
+
i += 3; // skip "*" "*" "/"
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
// Trailing ** matches everything
|
|
87
|
+
regexStr += ".*";
|
|
88
|
+
i += 2;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
else if (glob[i] === "*") {
|
|
92
|
+
// Single * matches anything except /
|
|
93
|
+
regexStr += "[^/]*";
|
|
94
|
+
i++;
|
|
95
|
+
}
|
|
96
|
+
else if (glob[i] === "?") {
|
|
97
|
+
regexStr += "[^/]";
|
|
98
|
+
i++;
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
// Escape regex-special characters
|
|
102
|
+
regexStr += glob[i].replace(/[.+^${}()|[\]\\\/\-]/g, "\\$&");
|
|
103
|
+
i++;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return new RegExp(`^${regexStr}$`, caseInsensitive ? "i" : "");
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Check if a command matches any Bash pattern in the list.
|
|
110
|
+
* Returns the matching pattern string, or null.
|
|
111
|
+
*/
|
|
112
|
+
export function matchesAnyPattern(command, patterns, caseInsensitive = false) {
|
|
113
|
+
for (const pattern of patterns) {
|
|
114
|
+
const glob = parseBashPattern(pattern);
|
|
115
|
+
if (!glob)
|
|
116
|
+
continue;
|
|
117
|
+
if (globToRegex(glob, caseInsensitive).test(command))
|
|
118
|
+
return pattern;
|
|
119
|
+
}
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
// ==============================================================================
|
|
123
|
+
// Chained Command Splitting
|
|
124
|
+
// ==============================================================================
|
|
125
|
+
/**
|
|
126
|
+
* Split a shell command on chain operators (&&, ||, ;, |) while
|
|
127
|
+
* respecting single/double quotes and backticks.
|
|
128
|
+
*
|
|
129
|
+
* "echo hello && sudo rm -rf /" → ["echo hello", "sudo rm -rf /"]
|
|
130
|
+
*
|
|
131
|
+
* This prevents bypassing deny patterns by prepending innocent commands.
|
|
132
|
+
*/
|
|
133
|
+
export function splitChainedCommands(command) {
|
|
134
|
+
const parts = [];
|
|
135
|
+
let current = "";
|
|
136
|
+
let inSingle = false;
|
|
137
|
+
let inDouble = false;
|
|
138
|
+
let inBacktick = false;
|
|
139
|
+
for (let i = 0; i < command.length; i++) {
|
|
140
|
+
const ch = command[i];
|
|
141
|
+
const prev = i > 0 ? command[i - 1] : "";
|
|
142
|
+
if (ch === "'" && !inDouble && !inBacktick && prev !== "\\") {
|
|
143
|
+
inSingle = !inSingle;
|
|
144
|
+
current += ch;
|
|
145
|
+
}
|
|
146
|
+
else if (ch === '"' && !inSingle && !inBacktick && prev !== "\\") {
|
|
147
|
+
inDouble = !inDouble;
|
|
148
|
+
current += ch;
|
|
149
|
+
}
|
|
150
|
+
else if (ch === "`" && !inSingle && !inDouble && prev !== "\\") {
|
|
151
|
+
inBacktick = !inBacktick;
|
|
152
|
+
current += ch;
|
|
153
|
+
}
|
|
154
|
+
else if (!inSingle && !inDouble && !inBacktick) {
|
|
155
|
+
if (ch === ";") {
|
|
156
|
+
parts.push(current.trim());
|
|
157
|
+
current = "";
|
|
158
|
+
}
|
|
159
|
+
else if (ch === "|" && command[i + 1] === "|") {
|
|
160
|
+
parts.push(current.trim());
|
|
161
|
+
current = "";
|
|
162
|
+
i++; // skip second |
|
|
163
|
+
}
|
|
164
|
+
else if (ch === "&" && command[i + 1] === "&") {
|
|
165
|
+
parts.push(current.trim());
|
|
166
|
+
current = "";
|
|
167
|
+
i++; // skip second &
|
|
168
|
+
}
|
|
169
|
+
else if (ch === "|") {
|
|
170
|
+
// Single pipe — left side is a command too
|
|
171
|
+
parts.push(current.trim());
|
|
172
|
+
current = "";
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
current += ch;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
current += ch;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
if (current.trim())
|
|
183
|
+
parts.push(current.trim());
|
|
184
|
+
return parts.filter((p) => p.length > 0);
|
|
185
|
+
}
|
|
186
|
+
// ==============================================================================
|
|
187
|
+
// Settings Reader
|
|
188
|
+
// ==============================================================================
|
|
189
|
+
/** Read one settings file and return a SecurityPolicy with only Bash patterns. */
|
|
190
|
+
function readSingleSettings(path) {
|
|
191
|
+
let raw;
|
|
192
|
+
try {
|
|
193
|
+
raw = readFileSync(path, "utf-8");
|
|
194
|
+
}
|
|
195
|
+
catch {
|
|
196
|
+
return null;
|
|
197
|
+
}
|
|
198
|
+
let parsed;
|
|
199
|
+
try {
|
|
200
|
+
parsed = JSON.parse(raw);
|
|
201
|
+
}
|
|
202
|
+
catch {
|
|
203
|
+
return null;
|
|
204
|
+
}
|
|
205
|
+
const perms = parsed?.permissions;
|
|
206
|
+
if (!perms || typeof perms !== "object")
|
|
207
|
+
return null;
|
|
208
|
+
const filterBash = (arr) => {
|
|
209
|
+
if (!Array.isArray(arr))
|
|
210
|
+
return [];
|
|
211
|
+
return arr.filter((p) => typeof p === "string" && parseBashPattern(p) !== null);
|
|
212
|
+
};
|
|
213
|
+
return {
|
|
214
|
+
allow: filterBash(perms.allow),
|
|
215
|
+
deny: filterBash(perms.deny),
|
|
216
|
+
ask: filterBash(perms.ask),
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Read Bash permission policies from up to 3 settings files.
|
|
221
|
+
*
|
|
222
|
+
* Returns policies in precedence order (most local first):
|
|
223
|
+
* 1. .claude/settings.local.json (project-local)
|
|
224
|
+
* 2. .claude/settings.json (project-shared)
|
|
225
|
+
* 3. ~/.claude/settings.json (global)
|
|
226
|
+
*
|
|
227
|
+
* Missing or invalid files are silently skipped.
|
|
228
|
+
*/
|
|
229
|
+
export function readBashPolicies(projectDir, globalSettingsPath) {
|
|
230
|
+
const policies = [];
|
|
231
|
+
if (projectDir) {
|
|
232
|
+
const localPath = resolve(projectDir, ".claude", "settings.local.json");
|
|
233
|
+
const localPolicy = readSingleSettings(localPath);
|
|
234
|
+
if (localPolicy)
|
|
235
|
+
policies.push(localPolicy);
|
|
236
|
+
const sharedPath = resolve(projectDir, ".claude", "settings.json");
|
|
237
|
+
const sharedPolicy = readSingleSettings(sharedPath);
|
|
238
|
+
if (sharedPolicy)
|
|
239
|
+
policies.push(sharedPolicy);
|
|
240
|
+
}
|
|
241
|
+
const globalPath = globalSettingsPath ?? resolve(homedir(), ".claude", "settings.json");
|
|
242
|
+
const globalPolicy = readSingleSettings(globalPath);
|
|
243
|
+
if (globalPolicy)
|
|
244
|
+
policies.push(globalPolicy);
|
|
245
|
+
return policies;
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Read deny patterns for a specific tool from settings files.
|
|
249
|
+
*
|
|
250
|
+
* Reads the same 3-tier settings as `readBashPolicies`, but extracts
|
|
251
|
+
* only deny globs for the given tool. Used for Read and Grep enforcement
|
|
252
|
+
* — checks if file paths should be blocked by deny patterns.
|
|
253
|
+
*
|
|
254
|
+
* Returns an array of arrays (one per settings file, in precedence order).
|
|
255
|
+
* Each inner array contains the extracted glob strings.
|
|
256
|
+
*/
|
|
257
|
+
export function readToolDenyPatterns(toolName, projectDir, globalSettingsPath) {
|
|
258
|
+
const result = [];
|
|
259
|
+
const extractGlobs = (path) => {
|
|
260
|
+
let raw;
|
|
261
|
+
try {
|
|
262
|
+
raw = readFileSync(path, "utf-8");
|
|
263
|
+
}
|
|
264
|
+
catch {
|
|
265
|
+
return null;
|
|
266
|
+
}
|
|
267
|
+
let parsed;
|
|
268
|
+
try {
|
|
269
|
+
parsed = JSON.parse(raw);
|
|
270
|
+
}
|
|
271
|
+
catch {
|
|
272
|
+
return null;
|
|
273
|
+
}
|
|
274
|
+
const deny = parsed?.permissions?.deny;
|
|
275
|
+
if (!Array.isArray(deny))
|
|
276
|
+
return [];
|
|
277
|
+
const globs = [];
|
|
278
|
+
for (const entry of deny) {
|
|
279
|
+
if (typeof entry !== "string")
|
|
280
|
+
continue;
|
|
281
|
+
const tp = parseToolPattern(entry);
|
|
282
|
+
if (tp && tp.tool === toolName) {
|
|
283
|
+
globs.push(tp.glob);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
return globs;
|
|
287
|
+
};
|
|
288
|
+
if (projectDir) {
|
|
289
|
+
const localGlobs = extractGlobs(resolve(projectDir, ".claude", "settings.local.json"));
|
|
290
|
+
if (localGlobs !== null)
|
|
291
|
+
result.push(localGlobs);
|
|
292
|
+
const sharedGlobs = extractGlobs(resolve(projectDir, ".claude", "settings.json"));
|
|
293
|
+
if (sharedGlobs !== null)
|
|
294
|
+
result.push(sharedGlobs);
|
|
295
|
+
}
|
|
296
|
+
const globalPath = globalSettingsPath ?? resolve(homedir(), ".claude", "settings.json");
|
|
297
|
+
const globalGlobs = extractGlobs(globalPath);
|
|
298
|
+
if (globalGlobs !== null)
|
|
299
|
+
result.push(globalGlobs);
|
|
300
|
+
return result;
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Evaluate a command against policies in precedence order.
|
|
304
|
+
*
|
|
305
|
+
* Splits chained commands (&&, ||, ;, |) and checks each segment
|
|
306
|
+
* against deny patterns — prevents bypassing deny by prepending
|
|
307
|
+
* innocent commands like "echo ok && sudo rm -rf /".
|
|
308
|
+
*
|
|
309
|
+
* Within each policy: deny > ask > allow (most restrictive wins).
|
|
310
|
+
* First definitive match across policies wins.
|
|
311
|
+
* Default (no match in any policy): "ask".
|
|
312
|
+
*/
|
|
313
|
+
export function evaluateCommand(command, policies, caseInsensitive = process.platform === "win32") {
|
|
314
|
+
// Check each segment of chained commands against deny patterns
|
|
315
|
+
const segments = splitChainedCommands(command);
|
|
316
|
+
for (const segment of segments) {
|
|
317
|
+
for (const policy of policies) {
|
|
318
|
+
const denyMatch = matchesAnyPattern(segment, policy.deny, caseInsensitive);
|
|
319
|
+
if (denyMatch)
|
|
320
|
+
return { decision: "deny", matchedPattern: denyMatch };
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
// Check ask/allow against the full command (original behavior)
|
|
324
|
+
for (const policy of policies) {
|
|
325
|
+
const askMatch = matchesAnyPattern(command, policy.ask, caseInsensitive);
|
|
326
|
+
if (askMatch)
|
|
327
|
+
return { decision: "ask", matchedPattern: askMatch };
|
|
328
|
+
const allowMatch = matchesAnyPattern(command, policy.allow, caseInsensitive);
|
|
329
|
+
if (allowMatch)
|
|
330
|
+
return { decision: "allow", matchedPattern: allowMatch };
|
|
331
|
+
}
|
|
332
|
+
return { decision: "ask" };
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Server-side variant: only enforce deny patterns.
|
|
336
|
+
*
|
|
337
|
+
* The server has no UI for "ask" prompts, so allow/ask patterns are
|
|
338
|
+
* irrelevant. Returns "deny" if any deny pattern matches, otherwise "allow".
|
|
339
|
+
*
|
|
340
|
+
* Also splits chained commands to prevent bypass.
|
|
341
|
+
*/
|
|
342
|
+
export function evaluateCommandDenyOnly(command, policies, caseInsensitive = process.platform === "win32") {
|
|
343
|
+
const segments = splitChainedCommands(command);
|
|
344
|
+
for (const segment of segments) {
|
|
345
|
+
for (const policy of policies) {
|
|
346
|
+
const denyMatch = matchesAnyPattern(segment, policy.deny, caseInsensitive);
|
|
347
|
+
if (denyMatch)
|
|
348
|
+
return { decision: "deny", matchedPattern: denyMatch };
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
return { decision: "allow" };
|
|
352
|
+
}
|
|
353
|
+
// ==============================================================================
|
|
354
|
+
// File Path Evaluation
|
|
355
|
+
// ==============================================================================
|
|
356
|
+
/**
|
|
357
|
+
* Check if a file path should be denied based on deny globs.
|
|
358
|
+
*
|
|
359
|
+
* Normalizes backslashes to forward slashes before matching so that
|
|
360
|
+
* Windows paths work with Unix-style glob patterns.
|
|
361
|
+
*/
|
|
362
|
+
export function evaluateFilePath(filePath, denyGlobs, caseInsensitive = process.platform === "win32") {
|
|
363
|
+
// Normalize backslashes to forward slashes for cross-platform matching
|
|
364
|
+
const normalized = filePath.replace(/\\/g, "/");
|
|
365
|
+
for (const globs of denyGlobs) {
|
|
366
|
+
for (const glob of globs) {
|
|
367
|
+
if (fileGlobToRegex(glob, caseInsensitive).test(normalized)) {
|
|
368
|
+
return { denied: true, matchedPattern: glob };
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
return { denied: false };
|
|
373
|
+
}
|
|
374
|
+
// ==============================================================================
|
|
375
|
+
// Shell-Escape Scanner
|
|
376
|
+
// ==============================================================================
|
|
377
|
+
// Regex patterns that detect shell-escape calls in non-shell languages.
|
|
378
|
+
// Each pattern uses capture groups so that the embedded command string
|
|
379
|
+
// can be extracted from the last non-quote group.
|
|
380
|
+
//
|
|
381
|
+
// NOTE: These regexes contain literal strings like "execSync" — they are
|
|
382
|
+
// patterns for *detecting* shell escapes in user code, not actual usage.
|
|
383
|
+
const SHELL_ESCAPE_PATTERNS = {
|
|
384
|
+
python: [
|
|
385
|
+
/os\.system\(\s*(['"])(.*?)\1\s*\)/g,
|
|
386
|
+
/subprocess\.(?:run|call|Popen|check_output|check_call)\(\s*(['"])(.*?)\1/g,
|
|
387
|
+
],
|
|
388
|
+
javascript: [
|
|
389
|
+
/exec(?:Sync|File|FileSync)?\(\s*(['"`])(.*?)\1/g,
|
|
390
|
+
/spawn(?:Sync)?\(\s*(['"`])(.*?)\1/g,
|
|
391
|
+
],
|
|
392
|
+
typescript: [
|
|
393
|
+
/exec(?:Sync|File|FileSync)?\(\s*(['"`])(.*?)\1/g,
|
|
394
|
+
/spawn(?:Sync)?\(\s*(['"`])(.*?)\1/g,
|
|
395
|
+
],
|
|
396
|
+
ruby: [
|
|
397
|
+
/system\(\s*(['"])(.*?)\1/g,
|
|
398
|
+
/`(.*?)`/g,
|
|
399
|
+
],
|
|
400
|
+
go: [
|
|
401
|
+
/exec\.Command\(\s*(['"`])(.*?)\1/g,
|
|
402
|
+
],
|
|
403
|
+
php: [
|
|
404
|
+
/shell_exec\(\s*(['"`])(.*?)\1/g,
|
|
405
|
+
/(?:^|[^.])exec\(\s*(['"`])(.*?)\1/g,
|
|
406
|
+
/(?:^|[^.])system\(\s*(['"`])(.*?)\1/g,
|
|
407
|
+
/passthru\(\s*(['"`])(.*?)\1/g,
|
|
408
|
+
/proc_open\(\s*(['"`])(.*?)\1/g,
|
|
409
|
+
],
|
|
410
|
+
rust: [
|
|
411
|
+
/Command::new\(\s*(['"`])(.*?)\1/g,
|
|
412
|
+
],
|
|
413
|
+
};
|
|
414
|
+
/**
|
|
415
|
+
* Extract all string elements from a Python subprocess list call.
|
|
416
|
+
*
|
|
417
|
+
* subprocess.run(["rm", "-rf", "/"]) → "rm -rf /"
|
|
418
|
+
*
|
|
419
|
+
* This catches the list-of-strings form that the single-string regex misses.
|
|
420
|
+
*/
|
|
421
|
+
function extractPythonSubprocessListArgs(code) {
|
|
422
|
+
const commands = [];
|
|
423
|
+
const pattern = /subprocess\.(?:run|call|Popen|check_output|check_call)\(\s*\[([^\]]+)\]/g;
|
|
424
|
+
let match;
|
|
425
|
+
while ((match = pattern.exec(code)) !== null) {
|
|
426
|
+
const listContent = match[1];
|
|
427
|
+
const args = [...listContent.matchAll(/(['"])(.*?)\1/g)].map((m) => m[2]);
|
|
428
|
+
if (args.length > 0) {
|
|
429
|
+
commands.push(args.join(" "));
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
return commands;
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* Scan non-shell code for shell-escape calls and extract the embedded
|
|
436
|
+
* command strings.
|
|
437
|
+
*
|
|
438
|
+
* Returns an array of command strings found in the code. For unknown
|
|
439
|
+
* languages or code without shell-escape calls, returns an empty array.
|
|
440
|
+
*/
|
|
441
|
+
export function extractShellCommands(code, language) {
|
|
442
|
+
const patterns = SHELL_ESCAPE_PATTERNS[language];
|
|
443
|
+
if (!patterns && language !== "python")
|
|
444
|
+
return [];
|
|
445
|
+
const commands = [];
|
|
446
|
+
if (patterns) {
|
|
447
|
+
for (const pattern of patterns) {
|
|
448
|
+
// Reset lastIndex since we reuse the global regex
|
|
449
|
+
pattern.lastIndex = 0;
|
|
450
|
+
let match;
|
|
451
|
+
while ((match = pattern.exec(code)) !== null) {
|
|
452
|
+
// The command string is in the last capture group that isn't the
|
|
453
|
+
// quote delimiter. For patterns with 2 groups (quote + content),
|
|
454
|
+
// it's group 2. For Ruby backticks with 1 group, it's group 1.
|
|
455
|
+
const command = match[match.length - 1];
|
|
456
|
+
if (command)
|
|
457
|
+
commands.push(command);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
// Python: also extract subprocess list-form args
|
|
462
|
+
if (language === "python") {
|
|
463
|
+
commands.push(...extractPythonSubprocessListArgs(code));
|
|
464
|
+
}
|
|
465
|
+
return commands;
|
|
466
|
+
}
|