@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,52 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { pickMostRestrictive } from "#src/handlers/gates/candidate-check";
|
|
4
|
+
|
|
5
|
+
import { makeGateCheckResult } from "#test/helpers/gate-fixtures";
|
|
6
|
+
|
|
7
|
+
describe("pickMostRestrictive", () => {
|
|
8
|
+
it("returns undefined for an empty list", () => {
|
|
9
|
+
expect(pickMostRestrictive([])).toBeUndefined();
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it("returns the single result for a one-element list", () => {
|
|
13
|
+
const only = makeGateCheckResult({ state: "allow" });
|
|
14
|
+
expect(pickMostRestrictive([only])).toBe(only);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("prefers deny over ask and allow regardless of position", () => {
|
|
18
|
+
const allow = makeGateCheckResult({ state: "allow", matchedPattern: "a" });
|
|
19
|
+
const ask = makeGateCheckResult({ state: "ask", matchedPattern: "b" });
|
|
20
|
+
const deny = makeGateCheckResult({ state: "deny", matchedPattern: "c" });
|
|
21
|
+
expect(pickMostRestrictive([allow, ask, deny])).toBe(deny);
|
|
22
|
+
expect(pickMostRestrictive([deny, ask, allow])).toBe(deny);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("prefers ask over allow when no deny is present", () => {
|
|
26
|
+
const allow = makeGateCheckResult({ state: "allow" });
|
|
27
|
+
const ask = makeGateCheckResult({ state: "ask" });
|
|
28
|
+
expect(pickMostRestrictive([allow, ask])).toBe(ask);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("keeps the first deny on ties", () => {
|
|
32
|
+
const deny1 = makeGateCheckResult({
|
|
33
|
+
state: "deny",
|
|
34
|
+
matchedPattern: "first",
|
|
35
|
+
});
|
|
36
|
+
const deny2 = makeGateCheckResult({
|
|
37
|
+
state: "deny",
|
|
38
|
+
matchedPattern: "second",
|
|
39
|
+
});
|
|
40
|
+
expect(pickMostRestrictive([deny1, deny2])).toBe(deny1);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("keeps the first ask on ties when no deny is present", () => {
|
|
44
|
+
const allow = makeGateCheckResult({ state: "allow" });
|
|
45
|
+
const ask1 = makeGateCheckResult({ state: "ask", matchedPattern: "first" });
|
|
46
|
+
const ask2 = makeGateCheckResult({
|
|
47
|
+
state: "ask",
|
|
48
|
+
matchedPattern: "second",
|
|
49
|
+
});
|
|
50
|
+
expect(pickMostRestrictive([allow, ask1, ask2])).toBe(ask1);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { describe, expect, test } from "vitest";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
formatBashExternalDirectoryAskPrompt,
|
|
5
|
+
formatExternalDirectoryAskPrompt,
|
|
6
|
+
} from "#src/handlers/gates/external-directory-messages";
|
|
7
|
+
|
|
8
|
+
// Denial message functions (formatExternalDirectoryDenyReason,
|
|
9
|
+
// formatExternalDirectoryUserDeniedReason, formatExternalDirectoryHardStopHint,
|
|
10
|
+
// formatBashExternalDirectoryDenyReason) have moved to denial-messages.ts.
|
|
11
|
+
// Their behavior is tested in denial-messages.test.ts.
|
|
12
|
+
|
|
13
|
+
describe("formatExternalDirectoryAskPrompt", () => {
|
|
14
|
+
test("uses 'Current agent' when no agent name provided", () => {
|
|
15
|
+
const result = formatExternalDirectoryAskPrompt(
|
|
16
|
+
"read",
|
|
17
|
+
"/etc/passwd",
|
|
18
|
+
"/projects/my-app",
|
|
19
|
+
);
|
|
20
|
+
expect(result).toContain("Current agent");
|
|
21
|
+
expect(result).toContain("read");
|
|
22
|
+
expect(result).toContain("/etc/passwd");
|
|
23
|
+
expect(result).toContain("/projects/my-app");
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
test("uses agent name when provided", () => {
|
|
27
|
+
const result = formatExternalDirectoryAskPrompt(
|
|
28
|
+
"write",
|
|
29
|
+
"/tmp/out.txt",
|
|
30
|
+
"/projects/my-app",
|
|
31
|
+
"my-agent",
|
|
32
|
+
);
|
|
33
|
+
expect(result).toContain("Agent 'my-agent'");
|
|
34
|
+
expect(result).toContain("write");
|
|
35
|
+
expect(result).toContain("/tmp/out.txt");
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
describe("formatBashExternalDirectoryAskPrompt", () => {
|
|
40
|
+
test("includes command, paths, cwd, and agent name", () => {
|
|
41
|
+
const result = formatBashExternalDirectoryAskPrompt(
|
|
42
|
+
"cat /etc/passwd",
|
|
43
|
+
["/etc/passwd"],
|
|
44
|
+
"/projects/my-app",
|
|
45
|
+
"my-agent",
|
|
46
|
+
);
|
|
47
|
+
expect(result).toContain("Agent 'my-agent'");
|
|
48
|
+
expect(result).toContain("cat /etc/passwd");
|
|
49
|
+
expect(result).toContain("/etc/passwd");
|
|
50
|
+
expect(result).toContain("/projects/my-app");
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test("uses 'Current agent' when no agent name provided", () => {
|
|
54
|
+
const result = formatBashExternalDirectoryAskPrompt(
|
|
55
|
+
"ls /tmp",
|
|
56
|
+
["/tmp"],
|
|
57
|
+
"/projects/my-app",
|
|
58
|
+
);
|
|
59
|
+
expect(result).toContain("Current agent");
|
|
60
|
+
});
|
|
61
|
+
});
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
GateBypass,
|
|
5
|
+
GateDescriptor,
|
|
6
|
+
} from "#src/handlers/gates/descriptor";
|
|
7
|
+
import { isGateBypass, isGateDescriptor } from "#src/handlers/gates/descriptor";
|
|
8
|
+
import { describeExternalDirectoryGate } from "#src/handlers/gates/external-directory";
|
|
9
|
+
import type { ToolCallContext } from "#src/handlers/gates/types";
|
|
10
|
+
import type { ScopedPermissionResolver } from "#src/permission-resolver";
|
|
11
|
+
import type { ToolAccessExtractorLookup } from "#src/tool-access-extractor-registry";
|
|
12
|
+
import { makeResolver } from "#test/helpers/gate-fixtures";
|
|
13
|
+
import { makeCheckResult } from "#test/helpers/handler-fixtures";
|
|
14
|
+
|
|
15
|
+
// ── helpers ───────────────────────────��────────────────────────────��───────
|
|
16
|
+
|
|
17
|
+
function makeTcc(overrides: Partial<ToolCallContext> = {}): ToolCallContext {
|
|
18
|
+
return {
|
|
19
|
+
toolName: "read",
|
|
20
|
+
agentName: null,
|
|
21
|
+
input: { path: "/outside/project/file.ts" },
|
|
22
|
+
toolCallId: "tc-1",
|
|
23
|
+
cwd: "/test/project",
|
|
24
|
+
...overrides,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Default resolver for descriptor-shape tests that do not assert the resolved
|
|
29
|
+
// state: returns `ask` for the external_directory surface so a descriptor is
|
|
30
|
+
// produced. Tests that assert the typed+resolved matching pass an explicit
|
|
31
|
+
// resolver to `describeExternalDirectoryGate` directly.
|
|
32
|
+
function gateUnderTest(
|
|
33
|
+
tcc: ToolCallContext,
|
|
34
|
+
infraDirs: string[],
|
|
35
|
+
extractors?: ToolAccessExtractorLookup,
|
|
36
|
+
resolver: ScopedPermissionResolver = makeResolver(
|
|
37
|
+
makeCheckResult({ state: "ask", toolName: "external_directory" }),
|
|
38
|
+
),
|
|
39
|
+
) {
|
|
40
|
+
return describeExternalDirectoryGate(tcc, infraDirs, resolver, extractors);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// ── tests ────────────────────��────────────────────────────────────��────────
|
|
44
|
+
|
|
45
|
+
describe("describeExternalDirectoryGate", () => {
|
|
46
|
+
it("returns null when no CWD", () => {
|
|
47
|
+
const result = gateUnderTest(makeTcc({ cwd: undefined }), ["/test/agent"]);
|
|
48
|
+
expect(result).toBeNull();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("returns null when tool is not path-bearing", () => {
|
|
52
|
+
const result = gateUnderTest(
|
|
53
|
+
makeTcc({ toolName: "bash", input: { command: "ls" } }),
|
|
54
|
+
["/test/agent"],
|
|
55
|
+
);
|
|
56
|
+
expect(result).toBeNull();
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("returns null when path is inside CWD", () => {
|
|
60
|
+
const result = gateUnderTest(
|
|
61
|
+
makeTcc({ input: { path: "/test/project/src/index.ts" } }),
|
|
62
|
+
["/test/agent"],
|
|
63
|
+
);
|
|
64
|
+
expect(result).toBeNull();
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
// ── Pi infrastructure read bypass ─────────────────���────────────────────
|
|
68
|
+
|
|
69
|
+
it("returns GateBypass for read targeting an infra dir", () => {
|
|
70
|
+
const result = gateUnderTest(
|
|
71
|
+
makeTcc({
|
|
72
|
+
toolName: "read",
|
|
73
|
+
input: { path: "/test/agent/git/some-package/SKILL.md" },
|
|
74
|
+
}),
|
|
75
|
+
["/test/agent", "/test/agent/git"],
|
|
76
|
+
);
|
|
77
|
+
expect(result).not.toBeNull();
|
|
78
|
+
expect(isGateBypass(result)).toBe(true);
|
|
79
|
+
const bypass = result as GateBypass;
|
|
80
|
+
expect(bypass.action).toBe("allow");
|
|
81
|
+
expect(bypass.decision).toMatchObject({
|
|
82
|
+
resolution: "infrastructure_auto_allowed",
|
|
83
|
+
result: "allow",
|
|
84
|
+
});
|
|
85
|
+
expect(bypass.log).toMatchObject({
|
|
86
|
+
event: "permission_request.infrastructure_auto_allowed",
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("returns GateBypass respecting custom infraDirs", () => {
|
|
91
|
+
const result = gateUnderTest(
|
|
92
|
+
makeTcc({
|
|
93
|
+
toolName: "read",
|
|
94
|
+
input: { path: "/custom/infra/SKILL.md" },
|
|
95
|
+
}),
|
|
96
|
+
["/custom/infra"],
|
|
97
|
+
);
|
|
98
|
+
expect(isGateBypass(result)).toBe(true);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("does NOT bypass for write tools targeting infra dirs", () => {
|
|
102
|
+
const result = gateUnderTest(
|
|
103
|
+
makeTcc({
|
|
104
|
+
toolName: "write",
|
|
105
|
+
input: { path: "/test/agent/git/some-file.ts", content: "x" },
|
|
106
|
+
}),
|
|
107
|
+
["/test/agent", "/test/agent/git"],
|
|
108
|
+
);
|
|
109
|
+
// Should be a GateDescriptor (needs permission check), not a bypass
|
|
110
|
+
expect(result).not.toBeNull();
|
|
111
|
+
expect(isGateDescriptor(result)).toBe(true);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
// ── GateDescriptor for external paths ─────────────────────────────────��
|
|
115
|
+
|
|
116
|
+
it("returns GateDescriptor with surface 'external_directory'", () => {
|
|
117
|
+
const result = gateUnderTest(makeTcc(), ["/test/agent"]);
|
|
118
|
+
expect(isGateDescriptor(result)).toBe(true);
|
|
119
|
+
const desc = result as GateDescriptor;
|
|
120
|
+
expect(desc.surface).toBe("external_directory");
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("decision value is the external path", () => {
|
|
124
|
+
const result = gateUnderTest(
|
|
125
|
+
makeTcc({ input: { path: "/outside/project/file.ts" } }),
|
|
126
|
+
["/test/agent"],
|
|
127
|
+
) as GateDescriptor;
|
|
128
|
+
expect(result.decision.value).toBe("/outside/project/file.ts");
|
|
129
|
+
expect(result.decision.surface).toBe("external_directory");
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("carries a precomputed preCheck and an empty input (matching is done by the gate)", () => {
|
|
133
|
+
const result = gateUnderTest(
|
|
134
|
+
makeTcc({ input: { path: "/outside/project/file.ts" } }),
|
|
135
|
+
["/test/agent"],
|
|
136
|
+
) as GateDescriptor;
|
|
137
|
+
expect(result.input).toEqual({});
|
|
138
|
+
expect(result.preCheck).toBeDefined();
|
|
139
|
+
expect(result.preCheck?.state).toBe("ask");
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("resolves the typed and symlink-resolved aliases on the external_directory surface (#418)", () => {
|
|
143
|
+
const resolver = makeResolver(
|
|
144
|
+
makeCheckResult({ state: "ask", toolName: "external_directory" }),
|
|
145
|
+
);
|
|
146
|
+
gateUnderTest(
|
|
147
|
+
makeTcc({ input: { path: "/outside/project/file.ts" } }),
|
|
148
|
+
["/test/agent"],
|
|
149
|
+
undefined,
|
|
150
|
+
resolver,
|
|
151
|
+
);
|
|
152
|
+
expect(resolver.resolvePathPolicy).toHaveBeenCalledWith(
|
|
153
|
+
["/outside/project/file.ts"],
|
|
154
|
+
undefined,
|
|
155
|
+
"external_directory",
|
|
156
|
+
);
|
|
157
|
+
expect(resolver.resolve).not.toHaveBeenCalled();
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it("sessionApproval uses deriveApprovalPattern", () => {
|
|
161
|
+
const result = gateUnderTest(
|
|
162
|
+
makeTcc({ input: { path: "/outside/project/file.ts" } }),
|
|
163
|
+
["/test/agent"],
|
|
164
|
+
) as GateDescriptor;
|
|
165
|
+
expect(result.sessionApproval).toBeDefined();
|
|
166
|
+
expect(result.sessionApproval?.surface).toBe("external_directory");
|
|
167
|
+
expect(result.sessionApproval?.representativePattern).toBeDefined();
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it("denialContext contains the external path and cwd", () => {
|
|
171
|
+
const result = gateUnderTest(
|
|
172
|
+
makeTcc({ input: { path: "/outside/project/file.ts" } }),
|
|
173
|
+
["/test/agent"],
|
|
174
|
+
) as GateDescriptor;
|
|
175
|
+
expect(result.denialContext).toMatchObject({
|
|
176
|
+
kind: "external_directory",
|
|
177
|
+
toolName: "read",
|
|
178
|
+
pathValue: "/outside/project/file.ts",
|
|
179
|
+
cwd: "/test/project",
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it("promptDetails includes path and tool_call source", () => {
|
|
184
|
+
const result = gateUnderTest(
|
|
185
|
+
makeTcc({ toolName: "read", agentName: "agent-1", toolCallId: "tc-5" }),
|
|
186
|
+
["/test/agent"],
|
|
187
|
+
) as GateDescriptor;
|
|
188
|
+
expect(result.promptDetails).toMatchObject({
|
|
189
|
+
source: "tool_call",
|
|
190
|
+
agentName: "agent-1",
|
|
191
|
+
toolCallId: "tc-5",
|
|
192
|
+
toolName: "read",
|
|
193
|
+
path: "/outside/project/file.ts",
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it("logContext includes path and message", () => {
|
|
198
|
+
const result = gateUnderTest(makeTcc(), ["/test/agent"]) as GateDescriptor;
|
|
199
|
+
expect(result.logContext).toMatchObject({
|
|
200
|
+
source: "tool_call",
|
|
201
|
+
path: "/outside/project/file.ts",
|
|
202
|
+
});
|
|
203
|
+
expect(result.logContext.message).toBeDefined();
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
// Extension and MCP tools are now external-directory gated (#352) ───────────
|
|
208
|
+
|
|
209
|
+
describe("describeExternalDirectoryGate — extension and MCP tools (#352)", () => {
|
|
210
|
+
it("gates an extension tool with an external input.path", () => {
|
|
211
|
+
const result = gateUnderTest(
|
|
212
|
+
makeTcc({
|
|
213
|
+
toolName: "my-ext",
|
|
214
|
+
input: { path: "/outside/project/file.ts" },
|
|
215
|
+
}),
|
|
216
|
+
["/test/agent"],
|
|
217
|
+
);
|
|
218
|
+
expect(isGateDescriptor(result)).toBe(true);
|
|
219
|
+
expect((result as GateDescriptor).surface).toBe("external_directory");
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
it("gates an MCP tool with an external arguments.path", () => {
|
|
223
|
+
const result = gateUnderTest(
|
|
224
|
+
makeTcc({
|
|
225
|
+
toolName: "mcp",
|
|
226
|
+
input: { arguments: { path: "/outside/project/file.ts" } },
|
|
227
|
+
}),
|
|
228
|
+
["/test/agent"],
|
|
229
|
+
);
|
|
230
|
+
expect(isGateDescriptor(result)).toBe(true);
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
it("uses a registered extractor's external path for a custom-shaped tool", () => {
|
|
234
|
+
const extractors = {
|
|
235
|
+
get: (name: string) =>
|
|
236
|
+
name === "ffgrep"
|
|
237
|
+
? (input: Record<string, unknown>) =>
|
|
238
|
+
typeof input.target === "string" ? input.target : undefined
|
|
239
|
+
: undefined,
|
|
240
|
+
};
|
|
241
|
+
const result = gateUnderTest(
|
|
242
|
+
makeTcc({ toolName: "ffgrep", input: { target: "/outside/project/x" } }),
|
|
243
|
+
["/test/agent"],
|
|
244
|
+
extractors,
|
|
245
|
+
);
|
|
246
|
+
expect(isGateDescriptor(result)).toBe(true);
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
it("returns null for an extension tool whose path is inside cwd", () => {
|
|
250
|
+
const result = gateUnderTest(
|
|
251
|
+
makeTcc({
|
|
252
|
+
toolName: "my-ext",
|
|
253
|
+
input: { path: "/test/project/src/x.ts" },
|
|
254
|
+
}),
|
|
255
|
+
["/test/agent"],
|
|
256
|
+
);
|
|
257
|
+
expect(result).toBeNull();
|
|
258
|
+
});
|
|
259
|
+
});
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
buildDecisionEvent,
|
|
5
|
+
deriveDecisionValue,
|
|
6
|
+
deriveResolution,
|
|
7
|
+
} from "#src/handlers/gates/helpers";
|
|
8
|
+
import type { PermissionCheckResult } from "#src/types";
|
|
9
|
+
|
|
10
|
+
describe("deriveDecisionValue", () => {
|
|
11
|
+
it("returns command for bash", () => {
|
|
12
|
+
expect(deriveDecisionValue("bash", { command: "git status" })).toBe(
|
|
13
|
+
"git status",
|
|
14
|
+
);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("falls back to toolName when bash has no command", () => {
|
|
18
|
+
expect(deriveDecisionValue("bash", {})).toBe("bash");
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("returns target for mcp", () => {
|
|
22
|
+
expect(deriveDecisionValue("mcp", { target: "exa:search" })).toBe(
|
|
23
|
+
"exa:search",
|
|
24
|
+
);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("falls back to toolName when mcp has no target", () => {
|
|
28
|
+
expect(deriveDecisionValue("mcp", {})).toBe("mcp");
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("returns toolName for non-path-bearing tools", () => {
|
|
32
|
+
expect(deriveDecisionValue("my_extension_tool", {})).toBe(
|
|
33
|
+
"my_extension_tool",
|
|
34
|
+
);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("returns path for path-bearing tools when path is provided", () => {
|
|
38
|
+
expect(deriveDecisionValue("read", {}, "/project/src/main.ts")).toBe(
|
|
39
|
+
"/project/src/main.ts",
|
|
40
|
+
);
|
|
41
|
+
expect(deriveDecisionValue("write", {}, "src/.env")).toBe("src/.env");
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("falls back to toolName for path-bearing tools when path is missing", () => {
|
|
45
|
+
expect(deriveDecisionValue("read", {})).toBe("read");
|
|
46
|
+
expect(deriveDecisionValue("write", {}, undefined)).toBe("write");
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe("deriveResolution", () => {
|
|
51
|
+
it("returns policy_allow for allow state", () => {
|
|
52
|
+
expect(deriveResolution("allow", "allow", false, true)).toBe(
|
|
53
|
+
"policy_allow",
|
|
54
|
+
);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("returns policy_deny for deny state", () => {
|
|
58
|
+
expect(deriveResolution("deny", "block", false, true)).toBe("policy_deny");
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("returns user_approved for ask + allow without session", () => {
|
|
62
|
+
expect(deriveResolution("ask", "allow", false, true)).toBe("user_approved");
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("returns user_approved_for_session for ask + allow with session", () => {
|
|
66
|
+
expect(deriveResolution("ask", "allow", true, true)).toBe(
|
|
67
|
+
"user_approved_for_session",
|
|
68
|
+
);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("returns auto_approved when autoApproved flag is set", () => {
|
|
72
|
+
expect(deriveResolution("ask", "allow", false, true, true)).toBe(
|
|
73
|
+
"auto_approved",
|
|
74
|
+
);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("returns user_approved_for_project for project persistent approval", () => {
|
|
78
|
+
expect(deriveResolution("ask", "allow", false, true, false, "project")).toBe(
|
|
79
|
+
"user_approved_for_project",
|
|
80
|
+
);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("returns user_approved_globally for global persistent approval", () => {
|
|
84
|
+
expect(deriveResolution("ask", "allow", false, true, false, "global")).toBe(
|
|
85
|
+
"user_approved_globally",
|
|
86
|
+
);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("returns user_denied for ask + block with canConfirm", () => {
|
|
90
|
+
expect(deriveResolution("ask", "block", false, true)).toBe("user_denied");
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("returns confirmation_unavailable for ask + block without canConfirm", () => {
|
|
94
|
+
expect(deriveResolution("ask", "block", false, false)).toBe(
|
|
95
|
+
"confirmation_unavailable",
|
|
96
|
+
);
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
describe("buildDecisionEvent", () => {
|
|
101
|
+
function makeCheck(
|
|
102
|
+
overrides: Partial<PermissionCheckResult> = {},
|
|
103
|
+
): PermissionCheckResult {
|
|
104
|
+
return {
|
|
105
|
+
state: "allow",
|
|
106
|
+
toolName: "read",
|
|
107
|
+
source: "tool",
|
|
108
|
+
origin: "builtin",
|
|
109
|
+
matchedPattern: "*",
|
|
110
|
+
...overrides,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
it("builds a decision event with all fields populated", () => {
|
|
115
|
+
const event = buildDecisionEvent(
|
|
116
|
+
{ surface: "read", value: "read" },
|
|
117
|
+
makeCheck({ origin: "global", matchedPattern: "read" }),
|
|
118
|
+
"test-agent",
|
|
119
|
+
"allow",
|
|
120
|
+
"policy_allow",
|
|
121
|
+
);
|
|
122
|
+
expect(event).toEqual({
|
|
123
|
+
surface: "read",
|
|
124
|
+
value: "read",
|
|
125
|
+
result: "allow",
|
|
126
|
+
resolution: "policy_allow",
|
|
127
|
+
origin: "global",
|
|
128
|
+
agentName: "test-agent",
|
|
129
|
+
matchedPattern: "read",
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("normalises undefined origin to null", () => {
|
|
134
|
+
const event = buildDecisionEvent(
|
|
135
|
+
{ surface: "bash", value: "git status" },
|
|
136
|
+
makeCheck({ origin: undefined }),
|
|
137
|
+
null,
|
|
138
|
+
"allow",
|
|
139
|
+
"user_approved",
|
|
140
|
+
);
|
|
141
|
+
expect(event.origin).toBeNull();
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("normalises null agentName to null", () => {
|
|
145
|
+
const event = buildDecisionEvent(
|
|
146
|
+
{ surface: "read", value: "read" },
|
|
147
|
+
makeCheck(),
|
|
148
|
+
null,
|
|
149
|
+
"deny",
|
|
150
|
+
"policy_deny",
|
|
151
|
+
);
|
|
152
|
+
expect(event.agentName).toBeNull();
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it("normalises undefined matchedPattern to null", () => {
|
|
156
|
+
const event = buildDecisionEvent(
|
|
157
|
+
{ surface: "read", value: "read" },
|
|
158
|
+
makeCheck({ matchedPattern: undefined }),
|
|
159
|
+
null,
|
|
160
|
+
"deny",
|
|
161
|
+
"policy_deny",
|
|
162
|
+
);
|
|
163
|
+
expect(event.matchedPattern).toBeNull();
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it("passes result and resolution through", () => {
|
|
167
|
+
const event = buildDecisionEvent(
|
|
168
|
+
{ surface: "bash", value: "rm -rf /" },
|
|
169
|
+
makeCheck(),
|
|
170
|
+
null,
|
|
171
|
+
"deny",
|
|
172
|
+
"user_denied",
|
|
173
|
+
);
|
|
174
|
+
expect(event.result).toBe("deny");
|
|
175
|
+
expect(event.resolution).toBe("user_denied");
|
|
176
|
+
});
|
|
177
|
+
});
|