@goodandready/dsh-approval-gate 0.1.20 → 0.1.22
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/CHANGELOG.md +16 -0
- package/lib/audit-log.js +348 -345
- package/lib/bands.js +243 -239
- package/lib/engine.js +75 -80
- package/lib/escalation.js +151 -0
- package/lib/guidance.js +119 -0
- package/lib/index.js +85 -86
- package/lib/lineage.js +182 -0
- package/lib/measure-kinds.js +138 -0
- package/lib/messages.js +33 -0
- package/lib/snapshots.js +268 -0
- package/package.json +55 -55
package/lib/bands.js
CHANGED
|
@@ -1,239 +1,243 @@
|
|
|
1
|
-
// lib/bands.js
|
|
2
|
-
// Two-band deterministic filtering engine for dsh-approval-gate.
|
|
3
|
-
// Band 0: Hard Deny (fast regexes for exfiltration, fork-bombs, disk wipes)
|
|
4
|
-
// Band 1: Safe Allow (prefix globs & curated safe read-only inspection commands and tools)
|
|
5
|
-
|
|
6
|
-
export const DEFAULT_DENY_PATTERNS = Object.freeze([
|
|
7
|
-
/\b(?:nc|ncat|netcat)\s+.*-e\s+/i,
|
|
8
|
-
/\/dev\/tcp\/\d+\.\d+\.\d+\.\d+\/\d+/i,
|
|
9
|
-
/\/dev\/udp\/\d+\.\d+\.\d+\.\d+\/\d+/i,
|
|
10
|
-
/\bmkfs(?:\.[a-z0-9]+)?\s+/i,
|
|
11
|
-
/\bdd\s+.*of=\/dev\/(?:sd|hd|vd|xvd|nvme|mmcblk|loop|mapper)/i,
|
|
12
|
-
/:\(\)\s*\{\s*:\s*\|\s*:\s*&\s*\}\s*;\s*:/,
|
|
13
|
-
/\bchmod\s+(?:-R\s+)?(?:0?777|a\+rwx)\s+\/(?!tmp)/i,
|
|
14
|
-
/\bchown\s+(?:-R\s+)?root\b/i,
|
|
15
|
-
/\b(?:shutdown|reboot|poweroff|halt)\s*(?:-|$)/i,
|
|
16
|
-
]);
|
|
17
|
-
|
|
18
|
-
export const DEFAULT_ALLOW_GLOBS = Object.freeze([
|
|
19
|
-
// Git inspection commands
|
|
20
|
-
"git status*",
|
|
21
|
-
"git diff*",
|
|
22
|
-
"git log*",
|
|
23
|
-
"git branch*",
|
|
24
|
-
"git show*",
|
|
25
|
-
"git remote*",
|
|
26
|
-
"git tag*",
|
|
27
|
-
"git rev-parse*",
|
|
28
|
-
|
|
29
|
-
// File viewing & analysis
|
|
30
|
-
"cat *",
|
|
31
|
-
"head *",
|
|
32
|
-
"tail *",
|
|
33
|
-
"more *",
|
|
34
|
-
"less *",
|
|
35
|
-
"wc *",
|
|
36
|
-
"file *",
|
|
37
|
-
"stat *",
|
|
38
|
-
"diff *",
|
|
39
|
-
|
|
40
|
-
// Search & finding
|
|
41
|
-
"grep *",
|
|
42
|
-
"fgrep *",
|
|
43
|
-
"egrep *",
|
|
44
|
-
"rg *",
|
|
45
|
-
"ag *",
|
|
46
|
-
"find *",
|
|
47
|
-
|
|
48
|
-
// Directory listing & environment info
|
|
49
|
-
"ls*",
|
|
50
|
-
"dir*",
|
|
51
|
-
"pwd*",
|
|
52
|
-
"which *",
|
|
53
|
-
"whereis *",
|
|
54
|
-
"whoami*",
|
|
55
|
-
"hostname*",
|
|
56
|
-
"uname*",
|
|
57
|
-
"date*",
|
|
58
|
-
"uptime*",
|
|
59
|
-
"echo *",
|
|
60
|
-
"printf *",
|
|
61
|
-
|
|
62
|
-
// Language / runtime versions & test runners
|
|
63
|
-
"node --version*",
|
|
64
|
-
"node -v*",
|
|
65
|
-
"npm test*",
|
|
66
|
-
"npm --version*",
|
|
67
|
-
"npm -v*",
|
|
68
|
-
"pnpm test*",
|
|
69
|
-
"pnpm --version*",
|
|
70
|
-
"pnpm -v*",
|
|
71
|
-
"yarn test*",
|
|
72
|
-
"cargo test*",
|
|
73
|
-
"cargo check*",
|
|
74
|
-
"cargo --version*",
|
|
75
|
-
"pytest*",
|
|
76
|
-
"python --version*",
|
|
77
|
-
"python3 --version*",
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
"
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
"
|
|
86
|
-
"
|
|
87
|
-
"
|
|
88
|
-
"
|
|
89
|
-
"
|
|
90
|
-
"
|
|
91
|
-
"
|
|
92
|
-
"
|
|
93
|
-
"
|
|
94
|
-
"
|
|
95
|
-
"
|
|
96
|
-
"
|
|
97
|
-
"
|
|
98
|
-
"
|
|
99
|
-
"
|
|
100
|
-
"
|
|
101
|
-
"
|
|
102
|
-
"
|
|
103
|
-
"
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
*
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
if (
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
}
|
|
239
|
-
|
|
1
|
+
// lib/bands.js
|
|
2
|
+
// Two-band deterministic filtering engine for dsh-approval-gate.
|
|
3
|
+
// Band 0: Hard Deny (fast regexes for exfiltration, fork-bombs, disk wipes)
|
|
4
|
+
// Band 1: Safe Allow (prefix globs & curated safe read-only inspection commands and tools)
|
|
5
|
+
|
|
6
|
+
export const DEFAULT_DENY_PATTERNS = Object.freeze([
|
|
7
|
+
/\b(?:nc|ncat|netcat)\s+.*-e\s+/i,
|
|
8
|
+
/\/dev\/tcp\/\d+\.\d+\.\d+\.\d+\/\d+/i,
|
|
9
|
+
/\/dev\/udp\/\d+\.\d+\.\d+\.\d+\/\d+/i,
|
|
10
|
+
/\bmkfs(?:\.[a-z0-9]+)?\s+/i,
|
|
11
|
+
/\bdd\s+.*of=\/dev\/(?:sd|hd|vd|xvd|nvme|mmcblk|loop|mapper)/i,
|
|
12
|
+
/:\(\)\s*\{\s*:\s*\|\s*:\s*&\s*\}\s*;\s*:/,
|
|
13
|
+
/\bchmod\s+(?:-R\s+)?(?:0?777|a\+rwx)\s+\/(?!tmp)/i,
|
|
14
|
+
/\bchown\s+(?:-R\s+)?root\b/i,
|
|
15
|
+
/\b(?:shutdown|reboot|poweroff|halt)\s*(?:-|$)/i,
|
|
16
|
+
]);
|
|
17
|
+
|
|
18
|
+
export const DEFAULT_ALLOW_GLOBS = Object.freeze([
|
|
19
|
+
// Git inspection commands
|
|
20
|
+
"git status*",
|
|
21
|
+
"git diff*",
|
|
22
|
+
"git log*",
|
|
23
|
+
"git branch*",
|
|
24
|
+
"git show*",
|
|
25
|
+
"git remote*",
|
|
26
|
+
"git tag*",
|
|
27
|
+
"git rev-parse*",
|
|
28
|
+
|
|
29
|
+
// File viewing & analysis
|
|
30
|
+
"cat *",
|
|
31
|
+
"head *",
|
|
32
|
+
"tail *",
|
|
33
|
+
"more *",
|
|
34
|
+
"less *",
|
|
35
|
+
"wc *",
|
|
36
|
+
"file *",
|
|
37
|
+
"stat *",
|
|
38
|
+
"diff *",
|
|
39
|
+
|
|
40
|
+
// Search & finding
|
|
41
|
+
"grep *",
|
|
42
|
+
"fgrep *",
|
|
43
|
+
"egrep *",
|
|
44
|
+
"rg *",
|
|
45
|
+
"ag *",
|
|
46
|
+
"find *",
|
|
47
|
+
|
|
48
|
+
// Directory listing & environment info
|
|
49
|
+
"ls*",
|
|
50
|
+
"dir*",
|
|
51
|
+
"pwd*",
|
|
52
|
+
"which *",
|
|
53
|
+
"whereis *",
|
|
54
|
+
"whoami*",
|
|
55
|
+
"hostname*",
|
|
56
|
+
"uname*",
|
|
57
|
+
"date*",
|
|
58
|
+
"uptime*",
|
|
59
|
+
"echo *",
|
|
60
|
+
"printf *",
|
|
61
|
+
|
|
62
|
+
// Language / runtime versions & test runners
|
|
63
|
+
"node --version*",
|
|
64
|
+
"node -v*",
|
|
65
|
+
"npm test*",
|
|
66
|
+
"npm --version*",
|
|
67
|
+
"npm -v*",
|
|
68
|
+
"pnpm test*",
|
|
69
|
+
"pnpm --version*",
|
|
70
|
+
"pnpm -v*",
|
|
71
|
+
"yarn test*",
|
|
72
|
+
"cargo test*",
|
|
73
|
+
"cargo check*",
|
|
74
|
+
"cargo --version*",
|
|
75
|
+
"pytest*",
|
|
76
|
+
"python --version*",
|
|
77
|
+
"python3 --version*",
|
|
78
|
+
|
|
79
|
+
// Service status inspection
|
|
80
|
+
"systemctl is-active*",
|
|
81
|
+
"systemctl status*",
|
|
82
|
+
]);
|
|
83
|
+
|
|
84
|
+
export const DEFAULT_READ_ONLY_TOOLS = Object.freeze([
|
|
85
|
+
"read",
|
|
86
|
+
"read_file",
|
|
87
|
+
"readFile",
|
|
88
|
+
"read_file_content",
|
|
89
|
+
"view_file",
|
|
90
|
+
"view",
|
|
91
|
+
"show",
|
|
92
|
+
"glob",
|
|
93
|
+
"grep",
|
|
94
|
+
"find_files",
|
|
95
|
+
"find_by_name",
|
|
96
|
+
"search_code",
|
|
97
|
+
"file_search",
|
|
98
|
+
"list_dir",
|
|
99
|
+
"list_directory",
|
|
100
|
+
"directory_list",
|
|
101
|
+
"tree",
|
|
102
|
+
"read_url_content",
|
|
103
|
+
"fetch_web_page",
|
|
104
|
+
"web_search",
|
|
105
|
+
"search_web",
|
|
106
|
+
"list_resources",
|
|
107
|
+
"read_resource",
|
|
108
|
+
]);
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Compile a prefix-glob into an anchored RegExp.
|
|
112
|
+
* @param {string} pattern
|
|
113
|
+
* @returns {RegExp}
|
|
114
|
+
*/
|
|
115
|
+
export function compileGlob(pattern) {
|
|
116
|
+
let source = "";
|
|
117
|
+
for (const ch of pattern) {
|
|
118
|
+
if (ch === "*") source += ".*";
|
|
119
|
+
else if (ch === "?") source += ".";
|
|
120
|
+
else source += ch.replace(/[.+^${}()|[\]\\]/g, "\\$&");
|
|
121
|
+
}
|
|
122
|
+
return new RegExp(`^${source}$`, "i");
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export class BandsEngine {
|
|
126
|
+
/**
|
|
127
|
+
* @param {{
|
|
128
|
+
* denyPatterns?: Array<RegExp|string>,
|
|
129
|
+
* allowGlobs?: Array<RegExp|string>,
|
|
130
|
+
* readOnlyTools?: Array<string>
|
|
131
|
+
* }} [options]
|
|
132
|
+
*/
|
|
133
|
+
constructor(options = {}) {
|
|
134
|
+
this.denyPatterns = (options.denyPatterns || DEFAULT_DENY_PATTERNS).map((p) =>
|
|
135
|
+
typeof p === "string" ? new RegExp(p, "i") : p
|
|
136
|
+
);
|
|
137
|
+
this.allowGlobs = (options.allowGlobs || DEFAULT_ALLOW_GLOBS).map((g) =>
|
|
138
|
+
typeof g === "string" ? compileGlob(g) : g
|
|
139
|
+
);
|
|
140
|
+
this.readOnlyTools = new Set(options.readOnlyTools || DEFAULT_READ_ONLY_TOOLS);
|
|
141
|
+
|
|
142
|
+
// Derive read-only tools dynamically from host tool registry if provided (#156)
|
|
143
|
+
if (options.toolRegistry) {
|
|
144
|
+
try {
|
|
145
|
+
const tools = typeof options.toolRegistry.getTools === "function"
|
|
146
|
+
? options.toolRegistry.getTools()
|
|
147
|
+
: (Array.isArray(options.toolRegistry) ? options.toolRegistry : []);
|
|
148
|
+
for (const t of tools) {
|
|
149
|
+
if (t && (t.readOnly || t.readonly || t.isReadOnly) && typeof t.name === "string") {
|
|
150
|
+
this.readOnlyTools.add(t.name);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
} catch {
|
|
154
|
+
// ignore
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Check if a tool is configured as an unconditional read-only tool.
|
|
161
|
+
* @param {string} tool
|
|
162
|
+
* @returns {boolean}
|
|
163
|
+
*/
|
|
164
|
+
isReadOnlyTool(tool) {
|
|
165
|
+
return typeof tool === "string" && this.readOnlyTools.has(tool.trim());
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Check if a command matches safe inspection patterns.
|
|
170
|
+
* @param {string} command
|
|
171
|
+
* @returns {boolean}
|
|
172
|
+
*/
|
|
173
|
+
isSafeInspectionCommand(command) {
|
|
174
|
+
if (typeof command !== "string" || command.trim() === "") return false;
|
|
175
|
+
const text = command.trim();
|
|
176
|
+
// Chained commands, subshells, substitutions, and redirections cannot match whole-string safe-allow
|
|
177
|
+
if (/[\n;&|`<>]|\$\([\s\S]*\)|\$\{[\s\S]*\}/.test(text)) {
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
for (const pattern of this.denyPatterns) {
|
|
181
|
+
if (pattern.test(text)) return false;
|
|
182
|
+
}
|
|
183
|
+
for (const glob of this.allowGlobs) {
|
|
184
|
+
if (glob.test(text)) return true;
|
|
185
|
+
}
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Register an additional read-only tool name.
|
|
191
|
+
* @param {string} tool
|
|
192
|
+
*/
|
|
193
|
+
addReadOnlyTool(tool) {
|
|
194
|
+
if (typeof tool === "string" && tool.trim()) {
|
|
195
|
+
this.readOnlyTools.add(tool.trim());
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Register an additional allow-glob pattern.
|
|
201
|
+
* @param {string} globPattern
|
|
202
|
+
*/
|
|
203
|
+
addAllowGlob(globPattern) {
|
|
204
|
+
if (typeof globPattern === "string" && globPattern.trim()) {
|
|
205
|
+
this.allowGlobs.push(compileGlob(globPattern.trim()));
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Evaluates an execution request through Band 0 and Band 1.
|
|
211
|
+
* @param {string} tool - Tool name
|
|
212
|
+
* @param {string} [command] - Normalized command text (for bash)
|
|
213
|
+
* @returns {"deny" | "allow" | "unmatched"}
|
|
214
|
+
*/
|
|
215
|
+
evaluate(tool, command) {
|
|
216
|
+
if (this.isReadOnlyTool(tool)) {
|
|
217
|
+
return "allow";
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
if (typeof command === "string" && command.trim().length > 0) {
|
|
221
|
+
const text = command.trim();
|
|
222
|
+
|
|
223
|
+
// Band 0: Hard Deny
|
|
224
|
+
for (const pattern of this.denyPatterns) {
|
|
225
|
+
if (pattern.test(text)) {
|
|
226
|
+
return "deny";
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// Band 1: Safe Allow (applies strictly to single commands without chaining, subshells or redirects)
|
|
231
|
+
if (!/[\n;&|`<>]|\$\([\s\S]*\)|\$\{[\s\S]*\}/.test(text)) {
|
|
232
|
+
for (const glob of this.allowGlobs) {
|
|
233
|
+
if (glob.test(text)) {
|
|
234
|
+
return "allow";
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
return "unmatched";
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|