@komarspn/pi-permission-system 16.0.2
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 +2234 -0
- package/LICENSE +21 -0
- package/README.md +158 -0
- package/config/config.example.json +39 -0
- package/package.json +82 -0
- package/schemas/permissions.schema.json +158 -0
- package/src/active-agent.ts +72 -0
- package/src/async-cache.ts +21 -0
- package/src/bash-arity.ts +210 -0
- package/src/builtin-tool-input-formatters.ts +82 -0
- package/src/canonicalize-path.ts +30 -0
- package/src/common.ts +121 -0
- package/src/config-loader.ts +432 -0
- package/src/config-modal.ts +259 -0
- package/src/config-paths.ts +47 -0
- package/src/config-reporter.ts +34 -0
- package/src/config-store.ts +222 -0
- package/src/decision-audit.ts +75 -0
- package/src/decision-reporter.ts +41 -0
- package/src/denial-messages.ts +232 -0
- package/src/expand-home.ts +28 -0
- package/src/extension-config.ts +79 -0
- package/src/extension-paths.ts +66 -0
- package/src/forwarded-permissions/io.ts +404 -0
- package/src/forwarded-permissions/permission-forwarder.ts +580 -0
- package/src/forwarding-manager.ts +74 -0
- package/src/gate-prompter.ts +12 -0
- package/src/handlers/before-agent-start.ts +94 -0
- package/src/handlers/gates/bash-command.ts +75 -0
- package/src/handlers/gates/bash-external-directory.ts +127 -0
- package/src/handlers/gates/bash-path-extractor.ts +15 -0
- package/src/handlers/gates/bash-path.ts +152 -0
- package/src/handlers/gates/bash-program.ts +1143 -0
- package/src/handlers/gates/bash-token-classification.ts +105 -0
- package/src/handlers/gates/candidate-check.ts +32 -0
- package/src/handlers/gates/descriptor.ts +81 -0
- package/src/handlers/gates/external-directory-messages.ts +20 -0
- package/src/handlers/gates/external-directory.ts +133 -0
- package/src/handlers/gates/helpers.ts +76 -0
- package/src/handlers/gates/path.ts +91 -0
- package/src/handlers/gates/runner.ts +186 -0
- package/src/handlers/gates/skill-input-gate-pipeline.ts +104 -0
- package/src/handlers/gates/skill-input.ts +46 -0
- package/src/handlers/gates/skill-read.ts +87 -0
- package/src/handlers/gates/tool-call-gate-pipeline.ts +129 -0
- package/src/handlers/gates/tool.ts +102 -0
- package/src/handlers/gates/types.ts +13 -0
- package/src/handlers/index.ts +3 -0
- package/src/handlers/lifecycle.ts +95 -0
- package/src/handlers/permission-gate-handler.ts +190 -0
- package/src/handlers/tool-call-boundary.ts +91 -0
- package/src/index.ts +225 -0
- package/src/input-normalizer.ts +157 -0
- package/src/logging.ts +113 -0
- package/src/mcp-targets.ts +170 -0
- package/src/node-modules-discovery.ts +76 -0
- package/src/normalize.ts +43 -0
- package/src/path-utils.ts +355 -0
- package/src/pattern-suggest.ts +132 -0
- package/src/permission-dialog.ts +138 -0
- package/src/permission-event-rpc.ts +223 -0
- package/src/permission-events.ts +266 -0
- package/src/permission-forwarding.ts +188 -0
- package/src/permission-gate.ts +94 -0
- package/src/permission-manager.ts +392 -0
- package/src/permission-merge.ts +32 -0
- package/src/permission-prompter.ts +142 -0
- package/src/permission-prompts.ts +93 -0
- package/src/permission-resolver.ts +109 -0
- package/src/permission-session.ts +189 -0
- package/src/permission-ui-prompt.ts +127 -0
- package/src/permissions-service.ts +63 -0
- package/src/persistent-approval-recorder.ts +139 -0
- package/src/policy-loader.ts +350 -0
- package/src/prompting-gateway.ts +104 -0
- package/src/rule.ts +188 -0
- package/src/scope-merge.ts +72 -0
- package/src/service-lifecycle.ts +49 -0
- package/src/service.ts +163 -0
- package/src/session-approval-recorder.ts +6 -0
- package/src/session-approval.ts +43 -0
- package/src/session-logger.ts +91 -0
- package/src/session-rules.ts +79 -0
- package/src/skill-prompt-sanitizer.ts +292 -0
- package/src/status.ts +35 -0
- package/src/subagent-context.ts +104 -0
- package/src/subagent-lifecycle-events.ts +72 -0
- package/src/subagent-registry.ts +105 -0
- package/src/synthesize.ts +92 -0
- package/src/system-prompt-sanitizer.ts +274 -0
- package/src/tool-access-extractor-registry.ts +68 -0
- package/src/tool-input-formatter-registry.ts +67 -0
- package/src/tool-input-preview.ts +34 -0
- package/src/tool-input-prompt-formatters.ts +63 -0
- package/src/tool-preview-formatter.ts +207 -0
- package/src/tool-registry.ts +148 -0
- package/src/types.ts +64 -0
- package/src/wildcard-matcher.ts +120 -0
- package/src/yolo-mode.ts +30 -0
- package/test/active-agent.test.ts +155 -0
- package/test/async-cache.test.ts +48 -0
- package/test/bash-arity.test.ts +144 -0
- package/test/bash-external-directory.test.ts +956 -0
- package/test/builtin-tool-input-formatters.test.ts +109 -0
- package/test/canonicalize-path.test.ts +93 -0
- package/test/common.test.ts +287 -0
- package/test/composition-root.test.ts +603 -0
- package/test/config-loader.test.ts +740 -0
- package/test/config-modal.test.ts +320 -0
- package/test/config-paths.test.ts +83 -0
- package/test/config-pipeline.test.ts +90 -0
- package/test/config-reporter.test.ts +147 -0
- package/test/config-store.test.ts +466 -0
- package/test/decision-audit.test.ts +72 -0
- package/test/decision-reporter.test.ts +112 -0
- package/test/denial-messages.test.ts +656 -0
- package/test/detect-permissive-bash-fallback.test.ts +56 -0
- package/test/expand-home.test.ts +93 -0
- package/test/extension-config.test.ts +129 -0
- package/test/extension-paths.test.ts +108 -0
- package/test/forwarded-permissions/io.test.ts +251 -0
- package/test/forwarding-manager.test.ts +194 -0
- package/test/handlers/before-agent-start.test.ts +317 -0
- package/test/handlers/external-directory-integration.test.ts +623 -0
- package/test/handlers/external-directory-session-dedup.test.ts +430 -0
- package/test/handlers/external-directory-symlink-acceptance.test.ts +149 -0
- package/test/handlers/gates/bash-command-metamorphic.test.ts +83 -0
- package/test/handlers/gates/bash-command.test.ts +191 -0
- package/test/handlers/gates/bash-external-directory.test.ts +269 -0
- package/test/handlers/gates/bash-path.test.ts +337 -0
- package/test/handlers/gates/bash-program.test.ts +410 -0
- package/test/handlers/gates/bash-token-classification.test.ts +241 -0
- package/test/handlers/gates/candidate-check.test.ts +52 -0
- package/test/handlers/gates/external-directory-messages.test.ts +61 -0
- package/test/handlers/gates/external-directory.test.ts +259 -0
- package/test/handlers/gates/helpers.test.ts +177 -0
- package/test/handlers/gates/path.test.ts +294 -0
- package/test/handlers/gates/runner.test.ts +447 -0
- package/test/handlers/gates/skill-input-gate-pipeline.test.ts +176 -0
- package/test/handlers/gates/skill-input.test.ts +131 -0
- package/test/handlers/gates/skill-read.test.ts +158 -0
- package/test/handlers/gates/tool-call-gate-pipeline.test.ts +252 -0
- package/test/handlers/gates/tool.test.ts +223 -0
- package/test/handlers/input-events.test.ts +168 -0
- package/test/handlers/input.test.ts +199 -0
- package/test/handlers/lifecycle.test.ts +221 -0
- package/test/handlers/tool-call-boundary.test.ts +145 -0
- package/test/handlers/tool-call-events.test.ts +277 -0
- package/test/handlers/tool-call.test.ts +395 -0
- package/test/handlers/validate-requested-tool.test.ts +92 -0
- package/test/helpers/gate-fixtures.ts +323 -0
- package/test/helpers/handler-fixtures.ts +335 -0
- package/test/helpers/make-fake-pi.ts +100 -0
- package/test/helpers/manager-harness.ts +112 -0
- package/test/helpers/session-fixtures.ts +204 -0
- package/test/input-normalizer.test.ts +367 -0
- package/test/logging.test.ts +51 -0
- package/test/mcp-targets.test.ts +233 -0
- package/test/node-modules-discovery.test.ts +97 -0
- package/test/normalize.test.ts +247 -0
- package/test/path-utils.test.ts +650 -0
- package/test/pattern-suggest.test.ts +248 -0
- package/test/permission-dialog.test.ts +241 -0
- package/test/permission-event-rpc.test.ts +541 -0
- package/test/permission-events.test.ts +402 -0
- package/test/permission-forwarder.test.ts +369 -0
- package/test/permission-forwarding.test.ts +315 -0
- package/test/permission-gate.test.ts +305 -0
- package/test/permission-manager-unified.test.ts +3368 -0
- package/test/permission-merge.test.ts +61 -0
- package/test/permission-prompter.test.ts +518 -0
- package/test/permission-prompts.test.ts +363 -0
- package/test/permission-resolver.test.ts +265 -0
- package/test/permission-session.test.ts +363 -0
- package/test/permission-ui-prompt.test.ts +146 -0
- package/test/permissions-service.test.ts +177 -0
- package/test/persistent-approval-recorder.test.ts +133 -0
- package/test/pi-infrastructure-read.test.ts +369 -0
- package/test/policy-loader.test.ts +561 -0
- package/test/prompting-gateway.test.ts +230 -0
- package/test/rule.test.ts +604 -0
- package/test/scope-merge.test.ts +116 -0
- package/test/service-lifecycle.test.ts +163 -0
- package/test/service.test.ts +308 -0
- package/test/session-approval.test.ts +75 -0
- package/test/session-logger.test.ts +200 -0
- package/test/session-rules.test.ts +304 -0
- package/test/session-start.test.ts +112 -0
- package/test/skill-prompt-sanitizer.test.ts +374 -0
- package/test/status.test.ts +10 -0
- package/test/subagent-context.test.ts +326 -0
- package/test/subagent-lifecycle-events.test.ts +132 -0
- package/test/subagent-registry.test.ts +145 -0
- package/test/synthesize.test.ts +300 -0
- package/test/system-prompt-sanitizer.test.ts +382 -0
- package/test/tool-access-extractor-registry.test.ts +77 -0
- package/test/tool-input-formatter-registry.test.ts +75 -0
- package/test/tool-input-preview.test.ts +129 -0
- package/test/tool-input-prompt-formatters.test.ts +115 -0
- package/test/tool-preview-formatter.test.ts +458 -0
- package/test/tool-registry.test.ts +197 -0
- package/test/wildcard-matcher.test.ts +424 -0
- package/test/yolo-mode.test.ts +188 -0
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import { afterEach, describe, expect, test, vi } from "vitest";
|
|
3
|
+
|
|
4
|
+
const mockHomedir = vi.hoisted(() => vi.fn(() => "/home/testuser"));
|
|
5
|
+
|
|
6
|
+
vi.mock("node:os", () => ({
|
|
7
|
+
homedir: mockHomedir,
|
|
8
|
+
default: { homedir: mockHomedir },
|
|
9
|
+
}));
|
|
10
|
+
|
|
11
|
+
const FAKE_HOME = "/home/testuser";
|
|
12
|
+
|
|
13
|
+
import {
|
|
14
|
+
compileWildcardPattern,
|
|
15
|
+
compileWildcardPatternEntries,
|
|
16
|
+
findCompiledWildcardMatch,
|
|
17
|
+
findCompiledWildcardMatchForNames,
|
|
18
|
+
wildcardMatch,
|
|
19
|
+
} from "#src/wildcard-matcher";
|
|
20
|
+
|
|
21
|
+
afterEach(() => {
|
|
22
|
+
mockHomedir.mockClear();
|
|
23
|
+
vi.restoreAllMocks();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe("compileWildcardPatternEntries", () => {
|
|
27
|
+
test("returns empty array for empty iterable", () => {
|
|
28
|
+
const result = compileWildcardPatternEntries([]);
|
|
29
|
+
expect(result).toEqual([]);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test("compiles a single exact pattern", () => {
|
|
33
|
+
const result = compileWildcardPatternEntries([["read", "allow"]]);
|
|
34
|
+
expect(result).toHaveLength(1);
|
|
35
|
+
expect(result[0].pattern).toBe("read");
|
|
36
|
+
expect(result[0].state).toBe("allow");
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test("compiles multiple patterns in order", () => {
|
|
40
|
+
const entries: [string, string][] = [
|
|
41
|
+
["read", "allow"],
|
|
42
|
+
["write", "deny"],
|
|
43
|
+
["bash *", "ask"],
|
|
44
|
+
];
|
|
45
|
+
const result = compileWildcardPatternEntries(entries);
|
|
46
|
+
expect(result).toHaveLength(3);
|
|
47
|
+
expect(result.map((r) => r.pattern)).toEqual(["read", "write", "bash *"]);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe("findCompiledWildcardMatch", () => {
|
|
52
|
+
test("returns null for empty patterns array", () => {
|
|
53
|
+
const result = findCompiledWildcardMatch([], "read");
|
|
54
|
+
expect(result).toBeNull();
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test("matches exact pattern", () => {
|
|
58
|
+
const patterns = compileWildcardPatternEntries([["read", "allow"]]);
|
|
59
|
+
const result = findCompiledWildcardMatch(patterns, "read");
|
|
60
|
+
expect(result).not.toBeNull();
|
|
61
|
+
expect(result?.state).toBe("allow");
|
|
62
|
+
expect(result?.matchedPattern).toBe("read");
|
|
63
|
+
expect(result?.matchedName).toBe("read");
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test("returns null when no pattern matches", () => {
|
|
67
|
+
const patterns = compileWildcardPatternEntries([["read", "allow"]]);
|
|
68
|
+
const result = findCompiledWildcardMatch(patterns, "write");
|
|
69
|
+
expect(result).toBeNull();
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test("matches glob * pattern", () => {
|
|
73
|
+
const patterns = compileWildcardPatternEntries([["git *", "allow"]]);
|
|
74
|
+
const result = findCompiledWildcardMatch(patterns, "git status");
|
|
75
|
+
expect(result).not.toBeNull();
|
|
76
|
+
expect(result?.state).toBe("allow");
|
|
77
|
+
expect(result?.matchedPattern).toBe("git *");
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test("glob * matches zero or more characters", () => {
|
|
81
|
+
const patterns = compileWildcardPatternEntries([["git*", "allow"]]);
|
|
82
|
+
expect(findCompiledWildcardMatch(patterns, "git")).not.toBeNull();
|
|
83
|
+
expect(findCompiledWildcardMatch(patterns, "git status")).not.toBeNull();
|
|
84
|
+
expect(findCompiledWildcardMatch(patterns, "npm install")).toBeNull();
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test("last-match-wins precedence: later pattern overrides earlier", () => {
|
|
88
|
+
const patterns = compileWildcardPatternEntries([
|
|
89
|
+
["git *", "allow"],
|
|
90
|
+
["git push *", "deny"],
|
|
91
|
+
]);
|
|
92
|
+
const result = findCompiledWildcardMatch(patterns, "git push origin main");
|
|
93
|
+
expect(result).not.toBeNull();
|
|
94
|
+
expect(result?.state).toBe("deny");
|
|
95
|
+
expect(result?.matchedPattern).toBe("git push *");
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test("last-match-wins: specific deny before broad allow matches the later one", () => {
|
|
99
|
+
const patterns = compileWildcardPatternEntries([
|
|
100
|
+
["*", "deny"],
|
|
101
|
+
["git status", "allow"],
|
|
102
|
+
]);
|
|
103
|
+
const result = findCompiledWildcardMatch(patterns, "git status");
|
|
104
|
+
expect(result).not.toBeNull();
|
|
105
|
+
expect(result?.state).toBe("allow");
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test("exact pattern does not match partial name", () => {
|
|
109
|
+
const patterns = compileWildcardPatternEntries([["read", "allow"]]);
|
|
110
|
+
expect(findCompiledWildcardMatch(patterns, "read ")).toBeNull();
|
|
111
|
+
expect(findCompiledWildcardMatch(patterns, "readonly")).toBeNull();
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test("regex special characters in pattern are escaped", () => {
|
|
115
|
+
const patterns = compileWildcardPatternEntries([
|
|
116
|
+
["tool.name", "allow"],
|
|
117
|
+
["tool+extra", "deny"],
|
|
118
|
+
]);
|
|
119
|
+
// "tool.name" should not match "toolXname" (dot is escaped)
|
|
120
|
+
expect(findCompiledWildcardMatch(patterns, "toolXname")).toBeNull();
|
|
121
|
+
// Exact match works
|
|
122
|
+
expect(findCompiledWildcardMatch(patterns, "tool.name")).not.toBeNull();
|
|
123
|
+
expect(findCompiledWildcardMatch(patterns, "tool+extra")).not.toBeNull();
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
describe("findCompiledWildcardMatchForNames", () => {
|
|
128
|
+
test("returns null for empty names array", () => {
|
|
129
|
+
const patterns = compileWildcardPatternEntries([["read", "allow"]]);
|
|
130
|
+
const result = findCompiledWildcardMatchForNames(patterns, []);
|
|
131
|
+
expect(result).toBeNull();
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
test("returns null when all names are whitespace", () => {
|
|
135
|
+
const patterns = compileWildcardPatternEntries([[" ", "allow"]]);
|
|
136
|
+
const result = findCompiledWildcardMatchForNames(patterns, [" ", "\t"]);
|
|
137
|
+
expect(result).toBeNull();
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
test("matches first name that has a pattern match", () => {
|
|
141
|
+
const patterns = compileWildcardPatternEntries([
|
|
142
|
+
["read", "allow"],
|
|
143
|
+
["write", "deny"],
|
|
144
|
+
]);
|
|
145
|
+
const result = findCompiledWildcardMatchForNames(patterns, [
|
|
146
|
+
"grep",
|
|
147
|
+
"write",
|
|
148
|
+
]);
|
|
149
|
+
expect(result).not.toBeNull();
|
|
150
|
+
expect(result?.matchedName).toBe("write");
|
|
151
|
+
expect(result?.state).toBe("deny");
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
test("trims whitespace from names before matching", () => {
|
|
155
|
+
const patterns = compileWildcardPatternEntries([["read", "allow"]]);
|
|
156
|
+
const result = findCompiledWildcardMatchForNames(patterns, [" read "]);
|
|
157
|
+
expect(result).not.toBeNull();
|
|
158
|
+
expect(result?.state).toBe("allow");
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
test("returns null when no name matches any pattern", () => {
|
|
162
|
+
const patterns = compileWildcardPatternEntries([["read", "allow"]]);
|
|
163
|
+
const result = findCompiledWildcardMatchForNames(patterns, [
|
|
164
|
+
"write",
|
|
165
|
+
"grep",
|
|
166
|
+
]);
|
|
167
|
+
expect(result).toBeNull();
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
test("multi-name lookup: returns match for first matching name in order", () => {
|
|
171
|
+
const patterns = compileWildcardPatternEntries([
|
|
172
|
+
["read", "allow"],
|
|
173
|
+
["write", "deny"],
|
|
174
|
+
]);
|
|
175
|
+
// "read" comes before "write" in names array, so "read" should match first
|
|
176
|
+
const result = findCompiledWildcardMatchForNames(patterns, [
|
|
177
|
+
"read",
|
|
178
|
+
"write",
|
|
179
|
+
]);
|
|
180
|
+
expect(result).not.toBeNull();
|
|
181
|
+
expect(result?.matchedName).toBe("read");
|
|
182
|
+
expect(result?.state).toBe("allow");
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
test("compileWildcardPattern produces correct pattern metadata", () => {
|
|
186
|
+
const compiled = compileWildcardPattern("bash *", "ask");
|
|
187
|
+
expect(compiled.pattern).toBe("bash *");
|
|
188
|
+
expect(compiled.state).toBe("ask");
|
|
189
|
+
expect(compiled.regex.test("bash ls -la")).toBe(true);
|
|
190
|
+
expect(compiled.regex.test("echo hello")).toBe(false);
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
describe("wildcardMatch", () => {
|
|
195
|
+
test("'*' pattern matches any value", () => {
|
|
196
|
+
expect(wildcardMatch("*", "anything")).toBe(true);
|
|
197
|
+
expect(wildcardMatch("*", "")).toBe(true);
|
|
198
|
+
expect(wildcardMatch("*", "bash")).toBe(true);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
test("'*' pattern matches values containing newlines", () => {
|
|
202
|
+
expect(wildcardMatch("*", "line1\nline2")).toBe(true);
|
|
203
|
+
expect(wildcardMatch("*", "a\nb\nc")).toBe(true);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
test("prefix-wildcard pattern matches value with embedded newlines", () => {
|
|
207
|
+
const command =
|
|
208
|
+
"node -e \"\nimport('x').then(() => {\n console.log('done');\n});\n\"";
|
|
209
|
+
expect(wildcardMatch("node *", command)).toBe(true);
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
test("compileWildcardPattern regex matches multiline string", () => {
|
|
213
|
+
const compiled = compileWildcardPattern("*", "allow");
|
|
214
|
+
expect(compiled.regex.test("a\nb")).toBe(true);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
test("exact pattern matches identical value", () => {
|
|
218
|
+
expect(wildcardMatch("read", "read")).toBe(true);
|
|
219
|
+
expect(wildcardMatch("external_directory", "external_directory")).toBe(
|
|
220
|
+
true,
|
|
221
|
+
);
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
test("exact pattern does not match a different value", () => {
|
|
225
|
+
expect(wildcardMatch("read", "write")).toBe(false);
|
|
226
|
+
expect(wildcardMatch("read", "readonly")).toBe(false);
|
|
227
|
+
expect(wildcardMatch("read", "read ")).toBe(false);
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
test("glob pattern matches with wildcard", () => {
|
|
231
|
+
expect(wildcardMatch("git *", "git status")).toBe(true);
|
|
232
|
+
expect(wildcardMatch("git *", "git push origin main")).toBe(true);
|
|
233
|
+
expect(wildcardMatch("git *", "npm install")).toBe(false);
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
test("glob with no trailing space matches longer string", () => {
|
|
237
|
+
expect(wildcardMatch("git*", "git")).toBe(true);
|
|
238
|
+
expect(wildcardMatch("git*", "git status")).toBe(true);
|
|
239
|
+
expect(wildcardMatch("git*", "npm")).toBe(false);
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
test("regex special characters in pattern are treated as literals", () => {
|
|
243
|
+
expect(wildcardMatch("tool.name", "tool.name")).toBe(true);
|
|
244
|
+
expect(wildcardMatch("tool.name", "toolXname")).toBe(false);
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
describe("trailing wildcard optionality", () => {
|
|
248
|
+
test("'git *' matches bare 'git' (trailing space+wildcard is optional)", () => {
|
|
249
|
+
expect(wildcardMatch("git *", "git")).toBe(true);
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
test("'git *' still matches 'git status' (existing behaviour preserved)", () => {
|
|
253
|
+
expect(wildcardMatch("git *", "git status")).toBe(true);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
test("'git *' still matches 'git status --short'", () => {
|
|
257
|
+
expect(wildcardMatch("git *", "git status --short")).toBe(true);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
test("'git *' does not match an unrelated command", () => {
|
|
261
|
+
expect(wildcardMatch("git *", "npm install")).toBe(false);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
test("'git status *' matches bare 'git status'", () => {
|
|
265
|
+
expect(wildcardMatch("git status *", "git status")).toBe(true);
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
test("'git status *' matches 'git status --short'", () => {
|
|
269
|
+
expect(wildcardMatch("git status *", "git status --short")).toBe(true);
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
test("non-trailing '*' is unaffected: 'g*t' does not match 'g' or 't'", () => {
|
|
273
|
+
expect(wildcardMatch("g*t", "g")).toBe(false);
|
|
274
|
+
expect(wildcardMatch("g*t", "t")).toBe(false);
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
test("non-trailing '*' still matches when content is present: 'g*t' matches 'git'", () => {
|
|
278
|
+
expect(wildcardMatch("g*t", "git")).toBe(true);
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
test("'git*' (no space) still matches bare 'git' — unchanged behaviour", () => {
|
|
282
|
+
expect(wildcardMatch("git*", "git")).toBe(true);
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
test("'*' alone still matches everything", () => {
|
|
286
|
+
expect(wildcardMatch("*", "git")).toBe(true);
|
|
287
|
+
expect(wildcardMatch("*", "")).toBe(true);
|
|
288
|
+
});
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
describe("match options (Windows path folding)", () => {
|
|
292
|
+
test("caseInsensitive matches a value differing only in case", () => {
|
|
293
|
+
expect(
|
|
294
|
+
wildcardMatch("C:\\Users\\Foo\\*", "c:\\users\\foo\\bar.md", {
|
|
295
|
+
caseInsensitive: true,
|
|
296
|
+
}),
|
|
297
|
+
).toBe(true);
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
test("case folding is off by default", () => {
|
|
301
|
+
expect(wildcardMatch("C:\\Users\\Foo\\*", "c:\\users\\foo\\bar.md")).toBe(
|
|
302
|
+
false,
|
|
303
|
+
);
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
test("windowsSeparators matches a backslash value against a forward-slash pattern", () => {
|
|
307
|
+
expect(
|
|
308
|
+
wildcardMatch("C:/Users/Foo/*", "C:\\Users\\Foo\\bar.md", {
|
|
309
|
+
windowsSeparators: true,
|
|
310
|
+
}),
|
|
311
|
+
).toBe(true);
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
test("separator normalization is off by default", () => {
|
|
315
|
+
expect(wildcardMatch("C:/Users/Foo/*", "C:\\Users\\Foo\\bar.md")).toBe(
|
|
316
|
+
false,
|
|
317
|
+
);
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
test("both options fold a mixed-case forward-slash pattern onto a lowercased backslash value", () => {
|
|
321
|
+
expect(
|
|
322
|
+
wildcardMatch(
|
|
323
|
+
"C:/Users/Foo/AppData/Roaming/*",
|
|
324
|
+
"c:\\users\\foo\\appdata\\roaming\\npm\\x.md",
|
|
325
|
+
{ caseInsensitive: true, windowsSeparators: true },
|
|
326
|
+
),
|
|
327
|
+
).toBe(true);
|
|
328
|
+
});
|
|
329
|
+
});
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
describe("? single-character wildcard", () => {
|
|
333
|
+
test("'?' matches exactly one character", () => {
|
|
334
|
+
expect(wildcardMatch("?", "a")).toBe(true);
|
|
335
|
+
expect(wildcardMatch("?", "Z")).toBe(true);
|
|
336
|
+
expect(wildcardMatch("?", "5")).toBe(true);
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
test("'?' does not match zero characters", () => {
|
|
340
|
+
expect(wildcardMatch("?", "")).toBe(false);
|
|
341
|
+
expect(wildcardMatch("a?", "a")).toBe(false);
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
test("'?' does not match two or more characters", () => {
|
|
345
|
+
expect(wildcardMatch("?", "ab")).toBe(false);
|
|
346
|
+
expect(wildcardMatch("?", "abc")).toBe(false);
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
test("multiple '?' match exactly that many characters", () => {
|
|
350
|
+
expect(wildcardMatch("f??", "foo")).toBe(true);
|
|
351
|
+
expect(wildcardMatch("f??", "fo")).toBe(false);
|
|
352
|
+
expect(wildcardMatch("f??", "fooo")).toBe(false);
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
test("'?' combined with '*'", () => {
|
|
356
|
+
// git + one char + anything
|
|
357
|
+
expect(wildcardMatch("git?*", "git status")).toBe(true);
|
|
358
|
+
// git + zero chars — '?' requires one
|
|
359
|
+
expect(wildcardMatch("git?*", "git")).toBe(false);
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
test("'?' matches path separators and special characters", () => {
|
|
363
|
+
expect(wildcardMatch("a?b", "a/b")).toBe(true);
|
|
364
|
+
expect(wildcardMatch("a?b", "a.b")).toBe(true);
|
|
365
|
+
expect(wildcardMatch("a?b", "a\nb")).toBe(true);
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
test("'?' in pattern matches literal '?' in value", () => {
|
|
369
|
+
expect(wildcardMatch("a?c", "a?c")).toBe(true);
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
test("'?' in bash-style patterns", () => {
|
|
373
|
+
expect(wildcardMatch("git statu?", "git status")).toBe(true);
|
|
374
|
+
expect(wildcardMatch("git statu?", "git statux")).toBe(true);
|
|
375
|
+
expect(wildcardMatch("git statu?", "git statu")).toBe(false);
|
|
376
|
+
});
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
describe("home path expansion in patterns", () => {
|
|
380
|
+
test("wildcardMatch expands ~ prefix in pattern before matching", () => {
|
|
381
|
+
const expandedPath = join(FAKE_HOME, "dev/project");
|
|
382
|
+
expect(wildcardMatch("~/dev/project", expandedPath)).toBe(true);
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
test("wildcardMatch expands ~/glob in pattern", () => {
|
|
386
|
+
const expandedFile = join(FAKE_HOME, "dev/project/file.ts");
|
|
387
|
+
expect(wildcardMatch("~/dev/*", expandedFile)).toBe(true);
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
test("wildcardMatch ~/glob does not match a different home directory", () => {
|
|
391
|
+
expect(wildcardMatch("~/dev/*", "/other/user/dev/file.ts")).toBe(false);
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
test("wildcardMatch expands $HOME prefix in pattern before matching", () => {
|
|
395
|
+
const expandedPath = join(FAKE_HOME, "dev/project");
|
|
396
|
+
expect(wildcardMatch("$HOME/dev/project", expandedPath)).toBe(true);
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
test("wildcardMatch expands $HOME/glob in pattern", () => {
|
|
400
|
+
const expandedFile = join(FAKE_HOME, "work/file.ts");
|
|
401
|
+
expect(wildcardMatch("$HOME/work/*", expandedFile)).toBe(true);
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
test("compileWildcardPattern retains original ~ pattern in .pattern field", () => {
|
|
405
|
+
const compiled = compileWildcardPattern("~/dev/*", "allow");
|
|
406
|
+
expect(compiled.pattern).toBe("~/dev/*");
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
test("compileWildcardPattern retains original $HOME pattern in .pattern field", () => {
|
|
410
|
+
const compiled = compileWildcardPattern("$HOME/dev/*", "allow");
|
|
411
|
+
expect(compiled.pattern).toBe("$HOME/dev/*");
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
test("compileWildcardPattern expanded regex matches the expanded path", () => {
|
|
415
|
+
const compiled = compileWildcardPattern("~/dev/*", "allow");
|
|
416
|
+
const expandedFile = join(FAKE_HOME, "dev/file.ts");
|
|
417
|
+
expect(compiled.regex.test(expandedFile)).toBe(true);
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
test("non-home pattern is unaffected", () => {
|
|
421
|
+
expect(wildcardMatch("/absolute/path/*", "/absolute/path/file")).toBe(true);
|
|
422
|
+
expect(wildcardMatch("/absolute/path/*", "/other/file")).toBe(false);
|
|
423
|
+
});
|
|
424
|
+
});
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import { afterEach, describe, expect, test, vi } from "vitest";
|
|
2
|
+
import type { PermissionSystemExtensionConfig } from "#src/extension-config";
|
|
3
|
+
import { DEFAULT_EXTENSION_CONFIG } from "#src/extension-config";
|
|
4
|
+
import { resolvePermissionForwardingTargetSessionId } from "#src/permission-forwarding";
|
|
5
|
+
import {
|
|
6
|
+
canResolveAskPermissionRequest,
|
|
7
|
+
shouldAutoApprovePermissionState,
|
|
8
|
+
} from "#src/yolo-mode";
|
|
9
|
+
|
|
10
|
+
afterEach(() => {
|
|
11
|
+
vi.restoreAllMocks();
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
function makeConfig(
|
|
15
|
+
yoloMode: boolean | undefined,
|
|
16
|
+
): PermissionSystemExtensionConfig {
|
|
17
|
+
return { yoloMode } as PermissionSystemExtensionConfig;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
describe("shouldAutoApprovePermissionState", () => {
|
|
21
|
+
test("returns true for 'ask' when yolo mode is on", () => {
|
|
22
|
+
expect(shouldAutoApprovePermissionState("ask", makeConfig(true))).toBe(
|
|
23
|
+
true,
|
|
24
|
+
);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test("returns false for 'ask' when yolo mode is off", () => {
|
|
28
|
+
expect(shouldAutoApprovePermissionState("ask", makeConfig(false))).toBe(
|
|
29
|
+
false,
|
|
30
|
+
);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test("returns false for 'ask' when yolo mode is undefined", () => {
|
|
34
|
+
expect(shouldAutoApprovePermissionState("ask", makeConfig(undefined))).toBe(
|
|
35
|
+
false,
|
|
36
|
+
);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test("returns false for 'allow' even when yolo mode is on", () => {
|
|
40
|
+
expect(shouldAutoApprovePermissionState("allow", makeConfig(true))).toBe(
|
|
41
|
+
false,
|
|
42
|
+
);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test("returns false for 'deny' even when yolo mode is on", () => {
|
|
46
|
+
expect(shouldAutoApprovePermissionState("deny", makeConfig(true))).toBe(
|
|
47
|
+
false,
|
|
48
|
+
);
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
describe("canResolveAskPermissionRequest", () => {
|
|
53
|
+
test("returns true when hasUI is true regardless of other flags", () => {
|
|
54
|
+
expect(
|
|
55
|
+
canResolveAskPermissionRequest({
|
|
56
|
+
config: makeConfig(false),
|
|
57
|
+
hasUI: true,
|
|
58
|
+
isSubagent: false,
|
|
59
|
+
}),
|
|
60
|
+
).toBe(true);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test("returns true when isSubagent is true regardless of other flags", () => {
|
|
64
|
+
expect(
|
|
65
|
+
canResolveAskPermissionRequest({
|
|
66
|
+
config: makeConfig(false),
|
|
67
|
+
hasUI: false,
|
|
68
|
+
isSubagent: true,
|
|
69
|
+
}),
|
|
70
|
+
).toBe(true);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test("returns true when yolo mode is on regardless of UI/subagent flags", () => {
|
|
74
|
+
expect(
|
|
75
|
+
canResolveAskPermissionRequest({
|
|
76
|
+
config: makeConfig(true),
|
|
77
|
+
hasUI: false,
|
|
78
|
+
isSubagent: false,
|
|
79
|
+
}),
|
|
80
|
+
).toBe(true);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test("returns false when no UI, not a subagent, and yolo mode is off", () => {
|
|
84
|
+
expect(
|
|
85
|
+
canResolveAskPermissionRequest({
|
|
86
|
+
config: makeConfig(false),
|
|
87
|
+
hasUI: false,
|
|
88
|
+
isSubagent: false,
|
|
89
|
+
}),
|
|
90
|
+
).toBe(false);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
test("returns false when no UI, not a subagent, and yolo mode is undefined", () => {
|
|
94
|
+
expect(
|
|
95
|
+
canResolveAskPermissionRequest({
|
|
96
|
+
config: makeConfig(undefined),
|
|
97
|
+
hasUI: false,
|
|
98
|
+
isSubagent: false,
|
|
99
|
+
}),
|
|
100
|
+
).toBe(false);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test("returns true when all three conditions are true", () => {
|
|
104
|
+
expect(
|
|
105
|
+
canResolveAskPermissionRequest({
|
|
106
|
+
config: makeConfig(true),
|
|
107
|
+
hasUI: true,
|
|
108
|
+
isSubagent: true,
|
|
109
|
+
}),
|
|
110
|
+
).toBe(true);
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
// ---------------------------------------------------------------------------
|
|
115
|
+
// Moved from permission-system.test.ts catch-all (#342)
|
|
116
|
+
// ---------------------------------------------------------------------------
|
|
117
|
+
|
|
118
|
+
test("Yolo mode only auto-approves ask-state permissions", () => {
|
|
119
|
+
expect(
|
|
120
|
+
shouldAutoApprovePermissionState("ask", DEFAULT_EXTENSION_CONFIG),
|
|
121
|
+
).toBe(false);
|
|
122
|
+
expect(
|
|
123
|
+
shouldAutoApprovePermissionState("ask", {
|
|
124
|
+
...DEFAULT_EXTENSION_CONFIG,
|
|
125
|
+
yoloMode: true,
|
|
126
|
+
}),
|
|
127
|
+
).toBe(true);
|
|
128
|
+
expect(
|
|
129
|
+
shouldAutoApprovePermissionState("deny", {
|
|
130
|
+
...DEFAULT_EXTENSION_CONFIG,
|
|
131
|
+
yoloMode: true,
|
|
132
|
+
}),
|
|
133
|
+
).toBe(false);
|
|
134
|
+
expect(
|
|
135
|
+
shouldAutoApprovePermissionState("allow", {
|
|
136
|
+
...DEFAULT_EXTENSION_CONFIG,
|
|
137
|
+
yoloMode: true,
|
|
138
|
+
}),
|
|
139
|
+
).toBe(false);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
test("Yolo mode resolves ask permissions without UI or delegation forwarding", () => {
|
|
143
|
+
expect(
|
|
144
|
+
canResolveAskPermissionRequest({
|
|
145
|
+
config: DEFAULT_EXTENSION_CONFIG,
|
|
146
|
+
hasUI: false,
|
|
147
|
+
isSubagent: false,
|
|
148
|
+
}),
|
|
149
|
+
).toBe(false);
|
|
150
|
+
expect(
|
|
151
|
+
canResolveAskPermissionRequest({
|
|
152
|
+
config: { ...DEFAULT_EXTENSION_CONFIG, yoloMode: true },
|
|
153
|
+
hasUI: false,
|
|
154
|
+
isSubagent: false,
|
|
155
|
+
}),
|
|
156
|
+
).toBe(true);
|
|
157
|
+
expect(
|
|
158
|
+
canResolveAskPermissionRequest({
|
|
159
|
+
config: DEFAULT_EXTENSION_CONFIG,
|
|
160
|
+
hasUI: false,
|
|
161
|
+
isSubagent: true,
|
|
162
|
+
}),
|
|
163
|
+
).toBe(true);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
test("Yolo mode bypasses delegated ask routing when no parent forwarding target is available", () => {
|
|
167
|
+
const targetSessionId = resolvePermissionForwardingTargetSessionId({
|
|
168
|
+
hasUI: false,
|
|
169
|
+
isSubagent: true,
|
|
170
|
+
currentSessionId: "child-session",
|
|
171
|
+
env: {},
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
expect(targetSessionId).toBe(null);
|
|
175
|
+
expect(
|
|
176
|
+
canResolveAskPermissionRequest({
|
|
177
|
+
config: { ...DEFAULT_EXTENSION_CONFIG, yoloMode: true },
|
|
178
|
+
hasUI: false,
|
|
179
|
+
isSubagent: true,
|
|
180
|
+
}),
|
|
181
|
+
).toBe(true);
|
|
182
|
+
expect(
|
|
183
|
+
shouldAutoApprovePermissionState("ask", {
|
|
184
|
+
...DEFAULT_EXTENSION_CONFIG,
|
|
185
|
+
yoloMode: true,
|
|
186
|
+
}),
|
|
187
|
+
).toBe(true);
|
|
188
|
+
});
|