@goodandready/dsh-approval-gate 0.1.19 → 0.1.21

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/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
- export const DEFAULT_READ_ONLY_TOOLS = Object.freeze([
81
- "read",
82
- "read_file",
83
- "readFile",
84
- "read_file_content",
85
- "view_file",
86
- "view",
87
- "show",
88
- "glob",
89
- "grep",
90
- "find_files",
91
- "find_by_name",
92
- "search_code",
93
- "file_search",
94
- "list_dir",
95
- "list_directory",
96
- "directory_list",
97
- "tree",
98
- "read_url_content",
99
- "fetch_web_page",
100
- "web_search",
101
- "search_web",
102
- "list_resources",
103
- "read_resource",
104
- ]);
105
-
106
- /**
107
- * Compile a prefix-glob into an anchored RegExp.
108
- * @param {string} pattern
109
- * @returns {RegExp}
110
- */
111
- export function compileGlob(pattern) {
112
- let source = "";
113
- for (const ch of pattern) {
114
- if (ch === "*") source += ".*";
115
- else if (ch === "?") source += ".";
116
- else source += ch.replace(/[.+^${}()|[\]\\]/g, "\\$&");
117
- }
118
- return new RegExp(`^${source}$`, "i");
119
- }
120
-
121
- export class BandsEngine {
122
- /**
123
- * @param {{
124
- * denyPatterns?: Array<RegExp|string>,
125
- * allowGlobs?: Array<RegExp|string>,
126
- * readOnlyTools?: Array<string>
127
- * }} [options]
128
- */
129
- constructor(options = {}) {
130
- this.denyPatterns = (options.denyPatterns || DEFAULT_DENY_PATTERNS).map((p) =>
131
- typeof p === "string" ? new RegExp(p, "i") : p
132
- );
133
- this.allowGlobs = (options.allowGlobs || DEFAULT_ALLOW_GLOBS).map((g) =>
134
- typeof g === "string" ? compileGlob(g) : g
135
- );
136
- this.readOnlyTools = new Set(options.readOnlyTools || DEFAULT_READ_ONLY_TOOLS);
137
-
138
- // Derive read-only tools dynamically from host tool registry if provided (#156)
139
- if (options.toolRegistry) {
140
- try {
141
- const tools = typeof options.toolRegistry.getTools === "function"
142
- ? options.toolRegistry.getTools()
143
- : (Array.isArray(options.toolRegistry) ? options.toolRegistry : []);
144
- for (const t of tools) {
145
- if (t && (t.readOnly || t.readonly || t.isReadOnly) && typeof t.name === "string") {
146
- this.readOnlyTools.add(t.name);
147
- }
148
- }
149
- } catch {
150
- // ignore
151
- }
152
- }
153
- }
154
-
155
- /**
156
- * Check if a tool is configured as an unconditional read-only tool.
157
- * @param {string} tool
158
- * @returns {boolean}
159
- */
160
- isReadOnlyTool(tool) {
161
- return typeof tool === "string" && this.readOnlyTools.has(tool.trim());
162
- }
163
-
164
- /**
165
- * Check if a command matches safe inspection patterns.
166
- * @param {string} command
167
- * @returns {boolean}
168
- */
169
- isSafeInspectionCommand(command) {
170
- if (typeof command !== "string" || command.trim() === "") return false;
171
- const text = command.trim();
172
- // Chained commands, subshells, substitutions, and redirections cannot match whole-string safe-allow
173
- if (/[\n;&|`<>]|\$\([\s\S]*\)|\$\{[\s\S]*\}/.test(text)) {
174
- return false;
175
- }
176
- for (const pattern of this.denyPatterns) {
177
- if (pattern.test(text)) return false;
178
- }
179
- for (const glob of this.allowGlobs) {
180
- if (glob.test(text)) return true;
181
- }
182
- return false;
183
- }
184
-
185
- /**
186
- * Register an additional read-only tool name.
187
- * @param {string} tool
188
- */
189
- addReadOnlyTool(tool) {
190
- if (typeof tool === "string" && tool.trim()) {
191
- this.readOnlyTools.add(tool.trim());
192
- }
193
- }
194
-
195
- /**
196
- * Register an additional allow-glob pattern.
197
- * @param {string} globPattern
198
- */
199
- addAllowGlob(globPattern) {
200
- if (typeof globPattern === "string" && globPattern.trim()) {
201
- this.allowGlobs.push(compileGlob(globPattern.trim()));
202
- }
203
- }
204
-
205
- /**
206
- * Evaluates an execution request through Band 0 and Band 1.
207
- * @param {string} tool - Tool name
208
- * @param {string} [command] - Normalized command text (for bash)
209
- * @returns {"deny" | "allow" | "unmatched"}
210
- */
211
- evaluate(tool, command) {
212
- if (this.isReadOnlyTool(tool)) {
213
- return "allow";
214
- }
215
-
216
- if (typeof command === "string" && command.trim().length > 0) {
217
- const text = command.trim();
218
-
219
- // Band 0: Hard Deny
220
- for (const pattern of this.denyPatterns) {
221
- if (pattern.test(text)) {
222
- return "deny";
223
- }
224
- }
225
-
226
- // Band 1: Safe Allow (applies strictly to single commands without chaining, subshells or redirects)
227
- if (!/[\n;&|`<>]|\$\([\s\S]*\)|\$\{[\s\S]*\}/.test(text)) {
228
- for (const glob of this.allowGlobs) {
229
- if (glob.test(text)) {
230
- return "allow";
231
- }
232
- }
233
- }
234
- }
235
-
236
- return "unmatched";
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
+
package/lib/compat.js ADDED
@@ -0,0 +1,124 @@
1
+ // lib/compat.js
2
+ // Host compatibility checks and polyfills across DeepSeek Harness minor versions (#106).
3
+
4
+ /**
5
+ * Detects available runtime capabilities and services in the Cordis host context (#106).
6
+ *
7
+ * @param {any} ctx - Cordis / DSH host context
8
+ * @returns {{
9
+ * hasContext: boolean,
10
+ * hasToolsService: boolean,
11
+ * hasPreExecuteHook: boolean,
12
+ * hasProvideApi: boolean,
13
+ * hasLocaleService: boolean,
14
+ * harnessVersion: string | null
15
+ * }}
16
+ */
17
+ export function detectHarnessCapabilities(ctx) {
18
+ if (!ctx || typeof ctx !== "object") {
19
+ return {
20
+ hasContext: false,
21
+ hasToolsService: false,
22
+ hasPreExecuteHook: false,
23
+ hasProvideApi: false,
24
+ hasLocaleService: false,
25
+ harnessVersion: null,
26
+ };
27
+ }
28
+
29
+ const getService = (name) => {
30
+ try {
31
+ if (typeof ctx.get === "function") return ctx.get(name);
32
+ } catch {
33
+ // ignore
34
+ }
35
+ return ctx[name];
36
+ };
37
+
38
+ const tools = getService("tools");
39
+ const locale = getService("locale");
40
+
41
+ const harnessVersion =
42
+ (typeof ctx.version === "string" && ctx.version) ||
43
+ (typeof process.env.DSH_VERSION === "string" && process.env.DSH_VERSION) ||
44
+ null;
45
+
46
+ return {
47
+ hasContext: true,
48
+ hasToolsService: Boolean(tools),
49
+ hasPreExecuteHook: typeof ctx.on === "function",
50
+ hasProvideApi: typeof ctx.provide === "function",
51
+ hasLocaleService: Boolean(locale && typeof locale.register === "function"),
52
+ harnessVersion,
53
+ };
54
+ }
55
+
56
+ /**
57
+ * Asserts host compatibility and identifies missing optional or required features (#106).
58
+ *
59
+ * @param {any} ctx
60
+ * @param {{ requireTools?: boolean }} [options]
61
+ * @returns {{ compatible: boolean, issues: string[], capabilities: ReturnType<typeof detectHarnessCapabilities> }}
62
+ */
63
+ export function assertHarnessCompatibility(ctx, options = {}) {
64
+ const caps = detectHarnessCapabilities(ctx);
65
+ const issues = [];
66
+
67
+ if (!caps.hasContext) {
68
+ issues.push("Invalid or missing Cordis context object");
69
+ }
70
+
71
+ if (options.requireTools && !caps.hasToolsService) {
72
+ issues.push("Missing tools service in host context");
73
+ }
74
+
75
+ return {
76
+ compatible: issues.length === 0,
77
+ issues,
78
+ capabilities: caps,
79
+ };
80
+ }
81
+
82
+ /**
83
+ * Safely provides a service to the host context with backward-compatible polyfill (#106).
84
+ *
85
+ * @param {any} ctx
86
+ * @param {string} serviceName
87
+ * @param {any} serviceInstance
88
+ * @returns {boolean} True if successfully registered
89
+ */
90
+ export function provideServicePolyfill(ctx, serviceName, serviceInstance) {
91
+ if (!ctx || typeof ctx !== "object" || !serviceName) return false;
92
+
93
+ if (typeof ctx.provide === "function") {
94
+ try {
95
+ ctx.provide(serviceName, serviceInstance);
96
+ return true;
97
+ } catch {
98
+ // fallback to property assignment below
99
+ }
100
+ }
101
+
102
+ ctx[serviceName] = serviceInstance;
103
+ return true;
104
+ }
105
+
106
+ /**
107
+ * Safely attaches an event hook to the host context with error boundary (#106).
108
+ *
109
+ * @param {any} ctx
110
+ * @param {string} event
111
+ * @param {(...args: any[]) => any} handler
112
+ * @returns {(() => void) | null} Unsubscribe dispose function or null
113
+ */
114
+ export function registerHookPolyfill(ctx, event, handler) {
115
+ if (!ctx || typeof ctx.on !== "function" || typeof handler !== "function") {
116
+ return null;
117
+ }
118
+
119
+ try {
120
+ return ctx.on(event, handler);
121
+ } catch {
122
+ return null;
123
+ }
124
+ }