@amaster.ai/dsh-policy 0.1.41 → 0.1.43
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/README.md +5 -2
- package/lib/index.js +6 -1
- package/lib/rule.js +12 -4
- package/lib/shell.js +37 -2
- package/lib/types/rule.d.ts +3 -1
- package/lib/types/shell.d.ts +7 -0
- package/package.json +20 -19
package/README.md
CHANGED
|
@@ -73,9 +73,12 @@ Plugin-level fields: `enabled` (master switch) and `commandKeys` (argument keys
|
|
|
73
73
|
### Matching semantics
|
|
74
74
|
|
|
75
75
|
- **Every condition on a rule must hold** for the rule to match (AND). A rule with no conditions beyond `tool` matches every call of that tool.
|
|
76
|
-
- **Shell commands are checked segment by segment.** Compound commands (`a && b | c`, newlines, background `&`) are split, quote-aware, and the executable command word is normalized through quotes, escapes and line continuations. Bash `$( )`, backtick and `<()` / `>()` substitutions are extracted as their own segments; here-doc bodies are data, except for real substitutions in unquoted bodies. PowerShell backticks are escapes, not Bash-style substitutions.
|
|
76
|
+
- **Shell commands are checked segment by segment.** Compound commands (`a && b | c`, newlines, background `&`) are split, quote-aware, and the executable command word is normalized through quotes, escapes and line continuations. Fd redirections such as `2>&1` and `&>file` stay inside their segment. Bash `$( )`, backtick and `<()` / `>()` substitutions are extracted as their own segments; here-doc bodies are data, except for real substitutions in unquoted bodies. PowerShell backticks are escapes, not Bash-style substitutions.
|
|
77
|
+
- **Bash grouping is stripped before matching.** Subshell parens, `{ … }`, `!`, `time`, and the `if`/`then`/`else`/`while`/`do` keywords are removed, so `(npm i)` and `if x; then npm i; fi` check `npm i`. `deny` and `ask` rules also match the segment without leading `NAME=value` assignments and with the command path reduced to its basename (`FOO=1 /usr/bin/npm i` matches `npm`). `allow` rules match the command as written, so they never cover a changed environment or another binary. Every unquoted paren splits, so an array assignment such as `arr=(a b)` or a `case` pattern becomes an extra segment; such extra segments can add a deny or ask match but never an allow.
|
|
78
|
+
- **Wrappers are not unwrapped.** `env`, `sudo`, `command`, `exec`, `xargs`, `eval`, `sh -c` and similar launchers are the segment's command word. Deny or ask those launchers explicitly when a deny rule must hold.
|
|
77
79
|
- **Priority resolves competition within one segment**; across segments the aggregation is conservative: any segment's `deny` denies the whole call, then any `ask` escalates, and `allow` requires every segment decided allow. A broad deny is still overridable by a specific allow because both compete on the same segment (`bun run lint` at 300 vs `bun run` at 200).
|
|
78
80
|
- **No rule matches → the call passes through** to the rest of the `tools/pre-execute` chain (`next()`), so dsh's own gates and other plugins keep their say. Invalid regexes are config errors and fail the plugin load; a runtime evaluation failure is logged with the `[dsh-policy]` prefix and denied (fail-closed) without breaking the agent loop.
|
|
81
|
+
- **Denials carry a routable identity**: `info: { name: 'PolicyError', code: 'POLICY_DENIED' }`, or `POLICY_EVALUATION_FAILED` for a failed evaluation. Telemetry and clients can tell policy denials from tool failures.
|
|
79
82
|
|
|
80
83
|
### What the gate covers
|
|
81
84
|
|
|
@@ -111,7 +114,7 @@ A policy plugin is advisory gating, not containment: deny rules keep a well-beha
|
|
|
111
114
|
|
|
112
115
|
| @amaster.ai/dsh-policy | dsh | cordis |
|
|
113
116
|
| --- | --- | --- |
|
|
114
|
-
| 0.1.x | `>=0.1.5-rc.1 || >=0.1.6-alpha.1 || >=0.1.7-alpha.1 || >=0.2.0-rc.1 || >=0.2.1-rc.1 || >=0.2.2-rc.1` (tested `0.2.2-rc.
|
|
117
|
+
| 0.1.x | `>=0.1.5-rc.1 || >=0.1.6-alpha.1 || >=0.1.7-alpha.1 || >=0.2.0-rc.1 || >=0.2.1-rc.1 || >=0.2.2-rc.1` (tested `0.2.2-rc.2`) | `^4.0.1` (tested `4.1.0`) |
|
|
115
118
|
|
|
116
119
|
Peer dependency: `@deepseek-ai/dsh-tools` (the `tools/pre-execute` gate). The approval seam (`@deepseek-ai/dsh-user-approval`) is optional and only involved in `ask` decisions.
|
|
117
120
|
|
package/lib/index.js
CHANGED
|
@@ -67,6 +67,7 @@ export function apply(ctx, config) {
|
|
|
67
67
|
return {
|
|
68
68
|
kind: 'deny',
|
|
69
69
|
reason: hit.rule.message ?? `denied by policy: ${describeRule(hit.rule)}`,
|
|
70
|
+
info: { name: 'PolicyError', code: 'POLICY_DENIED' },
|
|
70
71
|
};
|
|
71
72
|
case 'ask':
|
|
72
73
|
return { kind: 'ask', reason: hit.rule.message };
|
|
@@ -75,7 +76,11 @@ export function apply(ctx, config) {
|
|
|
75
76
|
catch (error) {
|
|
76
77
|
// Keep the agent loop alive without turning a broken policy into an allow.
|
|
77
78
|
console.error('[dsh-policy] evaluation failed:', error);
|
|
78
|
-
return {
|
|
79
|
+
return {
|
|
80
|
+
kind: 'deny',
|
|
81
|
+
reason: 'policy evaluation failed',
|
|
82
|
+
info: { name: 'PolicyError', code: 'POLICY_EVALUATION_FAILED' },
|
|
83
|
+
};
|
|
79
84
|
}
|
|
80
85
|
});
|
|
81
86
|
}
|
package/lib/rule.js
CHANGED
|
@@ -10,14 +10,16 @@
|
|
|
10
10
|
* - `commandRegex` is anchored at the segment start (Gemini anchors it at the
|
|
11
11
|
* command start) — use `.*` to match mid-segment.
|
|
12
12
|
* - Shell commands are checked segment by segment (see shell.ts): a compound
|
|
13
|
-
* command cannot launder a denied command behind an allowed one.
|
|
13
|
+
* command cannot launder a denied command behind an allowed one. Deny/ask
|
|
14
|
+
* rules also match the segment without leading assignments and with the
|
|
15
|
+
* command path reduced to its basename; allow rules match it as written.
|
|
14
16
|
* - Priority resolves rules competing for the SAME segment; ties break
|
|
15
17
|
* fail-closed (deny > ask > allow). Across segments deny wins outright,
|
|
16
18
|
* then ask; allow only when EVERY segment decided allow.
|
|
17
19
|
* - No matching rule at all → `undefined` → the plugin delegates to the rest
|
|
18
20
|
* of the `tools/pre-execute` chain (dsh's own gates keep their say).
|
|
19
21
|
*/
|
|
20
|
-
import { matchesPrefix, normalizeCommandWord, splitSegments } from './shell.js';
|
|
22
|
+
import { bareCommand, matchesPrefix, normalizeCommandWord, splitSegments, } from './shell.js';
|
|
21
23
|
/** Fail-closed tie-break within one priority tier. */
|
|
22
24
|
const SEVERITY = { allow: 0, ask: 1, deny: 2 };
|
|
23
25
|
const prefer = (a, b) => {
|
|
@@ -106,8 +108,14 @@ function matchesSegment(rule, segment, dialect) {
|
|
|
106
108
|
// No command string on this call: command conditions cannot be satisfied.
|
|
107
109
|
if (segment === undefined)
|
|
108
110
|
return !hasCommandCondition;
|
|
109
|
-
|
|
110
|
-
anyMatch(rule.commandRes, (re) => re.test(normalizeCommandWord(
|
|
111
|
+
const matches = (form) => anyMatch(rule.commandPrefixes, (prefix) => matchesPrefix(form, prefix, dialect)) &&
|
|
112
|
+
anyMatch(rule.commandRes, (re) => re.test(normalizeCommandWord(form, dialect)));
|
|
113
|
+
if (matches(segment))
|
|
114
|
+
return true;
|
|
115
|
+
return (hasCommandCondition &&
|
|
116
|
+
rule.decision !== 'allow' &&
|
|
117
|
+
dialect === 'bash' &&
|
|
118
|
+
matches(bareCommand(segment)));
|
|
111
119
|
}
|
|
112
120
|
export function evaluate(rules, toolName, args, commandKeys) {
|
|
113
121
|
const candidates = rules.filter((rule) => rule.tools.includes('*') || rule.tools.includes(toolName));
|
package/lib/shell.js
CHANGED
|
@@ -75,6 +75,33 @@ export function matchesPrefix(segment, prefix, dialect = 'bash') {
|
|
|
75
75
|
const trimmed = normalizeCommandWord(segment, dialect).trimStart();
|
|
76
76
|
return trimmed.startsWith(prefix) && isWordBoundary(trimmed[prefix.length]);
|
|
77
77
|
}
|
|
78
|
+
const ASSIGNMENT = /^[A-Za-z_][A-Za-z0-9_]*=(?:'[^']*'|"(?:[^"\\]|\\.)*"|\\.|[^\s'"\\])*\s+/;
|
|
79
|
+
/**
|
|
80
|
+
* The Bash segment without leading `NAME=value` assignments and with an
|
|
81
|
+
* absolute or relative command path reduced to its basename
|
|
82
|
+
* (`FOO=1 /usr/bin/npm i` → `npm i`). Only deny/ask rules match this form,
|
|
83
|
+
* so an allow rule never covers an altered environment or another binary.
|
|
84
|
+
*/
|
|
85
|
+
export function bareCommand(segment) {
|
|
86
|
+
let rest = segment.trimStart();
|
|
87
|
+
for (let match = ASSIGNMENT.exec(rest); match; match = ASSIGNMENT.exec(rest)) {
|
|
88
|
+
rest = rest.slice(match[0].length);
|
|
89
|
+
}
|
|
90
|
+
const normalized = normalizeCommandWord(rest);
|
|
91
|
+
const word = /^\S*/.exec(normalized)?.[0] ?? '';
|
|
92
|
+
return normalized.slice(word.lastIndexOf('/') + 1);
|
|
93
|
+
}
|
|
94
|
+
// Grouping and compound-command keywords before the command word, e.g. `{ npm i`, `then npm i`.
|
|
95
|
+
const LEADING_SYNTAX = /^(?:[{!]|if|then|elif|else|while|until|do|time)(?:\s+|$)/;
|
|
96
|
+
// A segment that only closes a group or compound command runs nothing.
|
|
97
|
+
const CLOSER = /^(?:\}|fi|done|esac)$/;
|
|
98
|
+
function stripLeadingSyntax(segment) {
|
|
99
|
+
let rest = segment;
|
|
100
|
+
for (let match = LEADING_SYNTAX.exec(rest); match; match = LEADING_SYNTAX.exec(rest)) {
|
|
101
|
+
rest = rest.slice(match[0].length);
|
|
102
|
+
}
|
|
103
|
+
return CLOSER.test(rest) ? '' : rest;
|
|
104
|
+
}
|
|
78
105
|
/** Read a `$(`...`)` body starting just after the opening paren. */
|
|
79
106
|
function readParenSubshell(text, start) {
|
|
80
107
|
let depth = 1;
|
|
@@ -222,8 +249,9 @@ export function splitSegments(command, dialect = 'bash') {
|
|
|
222
249
|
const segments = [];
|
|
223
250
|
let buf = '';
|
|
224
251
|
let quote;
|
|
252
|
+
let escaped = -1;
|
|
225
253
|
const push = () => {
|
|
226
|
-
const trimmed = buf.trim();
|
|
254
|
+
const trimmed = dialect === 'bash' ? stripLeadingSyntax(buf.trim()) : buf.trim();
|
|
227
255
|
if (trimmed !== '')
|
|
228
256
|
segments.push(trimmed);
|
|
229
257
|
buf = '';
|
|
@@ -296,10 +324,17 @@ export function splitSegments(command, dialect = 'bash') {
|
|
|
296
324
|
}
|
|
297
325
|
if (dialect === 'bash' && ch === '\\' && i + 1 < command.length) {
|
|
298
326
|
buf += ch + command[i + 1];
|
|
327
|
+
escaped = i + 1;
|
|
299
328
|
i += 2;
|
|
300
329
|
continue;
|
|
301
330
|
}
|
|
302
|
-
|
|
331
|
+
// `2>&1`, `<&3` and `&>file` are redirections, not background separators; `\>&` is not.
|
|
332
|
+
const redirect = ch === '&' &&
|
|
333
|
+
(((command[i - 1] === '>' || command[i - 1] === '<') && escaped !== i - 1) ||
|
|
334
|
+
command[i + 1] === '>');
|
|
335
|
+
// Unquoted Bash parens delimit subshells, so `(npm i)` checks `npm i`.
|
|
336
|
+
const paren = dialect === 'bash' && (ch === '(' || ch === ')');
|
|
337
|
+
if (paren || (!redirect && (ch === ';' || ch === '\n' || ch === '|' || ch === '&'))) {
|
|
303
338
|
push();
|
|
304
339
|
i += command[i + 1] === ch && (ch === '|' || ch === '&') ? 2 : 1;
|
|
305
340
|
continue;
|
package/lib/types/rule.d.ts
CHANGED
|
@@ -10,7 +10,9 @@
|
|
|
10
10
|
* - `commandRegex` is anchored at the segment start (Gemini anchors it at the
|
|
11
11
|
* command start) — use `.*` to match mid-segment.
|
|
12
12
|
* - Shell commands are checked segment by segment (see shell.ts): a compound
|
|
13
|
-
* command cannot launder a denied command behind an allowed one.
|
|
13
|
+
* command cannot launder a denied command behind an allowed one. Deny/ask
|
|
14
|
+
* rules also match the segment without leading assignments and with the
|
|
15
|
+
* command path reduced to its basename; allow rules match it as written.
|
|
14
16
|
* - Priority resolves rules competing for the SAME segment; ties break
|
|
15
17
|
* fail-closed (deny > ask > allow). Across segments deny wins outright,
|
|
16
18
|
* then ask; allow only when EVERY segment decided allow.
|
package/lib/types/shell.d.ts
CHANGED
|
@@ -16,6 +16,13 @@ export declare function normalizeCommandWord(segment: string, dialect?: ShellDia
|
|
|
16
16
|
* `npm install` and `npm` itself, never `npmx` or `npm-cache`.
|
|
17
17
|
*/
|
|
18
18
|
export declare function matchesPrefix(segment: string, prefix: string, dialect?: ShellDialect): boolean;
|
|
19
|
+
/**
|
|
20
|
+
* The Bash segment without leading `NAME=value` assignments and with an
|
|
21
|
+
* absolute or relative command path reduced to its basename
|
|
22
|
+
* (`FOO=1 /usr/bin/npm i` → `npm i`). Only deny/ask rules match this form,
|
|
23
|
+
* so an allow rule never covers an altered environment or another binary.
|
|
24
|
+
*/
|
|
25
|
+
export declare function bareCommand(segment: string): string;
|
|
19
26
|
/**
|
|
20
27
|
* Split a compound command into the segments a shell would run: pipelines,
|
|
21
28
|
* `&&`/`||`/`;`/newline chains, background `&`, plus the interiors of
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@amaster.ai/dsh-policy",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.43",
|
|
4
4
|
"description": "Declarative tool-call policy for DeepSeek Harness: config-driven allow/deny/ask rules (tool name, args pattern, shell command prefix/regex, priority) on the tools/pre-execute gate — Gemini CLI-style policy files expressed as plain plugin config.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -40,27 +40,28 @@
|
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"@deepseek-ai/cordis": "4.1.0",
|
|
43
|
-
"@deepseek-ai/dsh-
|
|
44
|
-
"@deepseek-ai/dsh-
|
|
45
|
-
"@deepseek-ai/dsh-
|
|
46
|
-
"@deepseek-ai/dsh-
|
|
47
|
-
"@deepseek-ai/dsh-
|
|
48
|
-
"@deepseek-ai/dsh-
|
|
49
|
-
"@deepseek-ai/dsh-
|
|
50
|
-
"@deepseek-ai/dsh-sandbox
|
|
51
|
-
"@deepseek-ai/dsh-
|
|
52
|
-
"@deepseek-ai/dsh-
|
|
53
|
-
"@deepseek-ai/dsh-
|
|
54
|
-
"@deepseek-ai/dsh-shell
|
|
55
|
-
"@deepseek-ai/dsh-
|
|
56
|
-
"@deepseek-ai/dsh-
|
|
57
|
-
"@deepseek-ai/dsh-
|
|
58
|
-
"@deepseek-ai/dsh-
|
|
59
|
-
"@deepseek-ai/dsh-
|
|
43
|
+
"@deepseek-ai/dsh-error": "0.2.2-rc.2",
|
|
44
|
+
"@deepseek-ai/dsh-agent": "0.2.2-rc.2",
|
|
45
|
+
"@deepseek-ai/dsh-home-paths": "0.2.2-rc.2",
|
|
46
|
+
"@deepseek-ai/dsh-http-proxy": "0.2.2-rc.2",
|
|
47
|
+
"@deepseek-ai/dsh-jobs": "0.2.2-rc.2",
|
|
48
|
+
"@deepseek-ai/dsh-llm": "0.2.2-rc.2",
|
|
49
|
+
"@deepseek-ai/dsh-ptc-runtime": "0.2.2-rc.2",
|
|
50
|
+
"@deepseek-ai/dsh-sandbox": "0.2.2-rc.2",
|
|
51
|
+
"@deepseek-ai/dsh-sandbox-policy": "0.2.2-rc.2",
|
|
52
|
+
"@deepseek-ai/dsh-scope": "0.2.2-rc.2",
|
|
53
|
+
"@deepseek-ai/dsh-session-projection": "0.2.2-rc.2",
|
|
54
|
+
"@deepseek-ai/dsh-shell": "0.2.2-rc.2",
|
|
55
|
+
"@deepseek-ai/dsh-shell-env": "0.2.2-rc.2",
|
|
56
|
+
"@deepseek-ai/dsh-subprocess": "0.2.2-rc.2",
|
|
57
|
+
"@deepseek-ai/dsh-system-prompt": "0.2.2-rc.2",
|
|
58
|
+
"@deepseek-ai/dsh-tool-bash": "0.2.2-rc.2",
|
|
59
|
+
"@deepseek-ai/dsh-tools": "0.2.2-rc.2",
|
|
60
|
+
"@deepseek-ai/dsh-user-approval": "0.2.2-rc.2",
|
|
60
61
|
"@types/node": "^22.0.0",
|
|
61
62
|
"typescript": "^5.6.3",
|
|
62
63
|
"vitest": "^4.1.11",
|
|
63
|
-
"@deepseek-ai/dsh-tool-executor-native": "0.2.2-rc.
|
|
64
|
+
"@deepseek-ai/dsh-tool-executor-native": "0.2.2-rc.2"
|
|
64
65
|
},
|
|
65
66
|
"engines": {
|
|
66
67
|
"node": "^20.19 || ^22.12 || >=24.0"
|