@vybestack/llxprt-code-core 0.5.0-nightly.251121.9dcb43714 → 0.5.0-nightly.251121.cbc3e5b17
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/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/src/config/config.d.ts +9 -0
- package/dist/src/config/config.js +16 -0
- package/dist/src/config/config.js.map +1 -1
- package/dist/src/confirmation-bus/index.d.ts +2 -0
- package/dist/src/confirmation-bus/index.js +3 -0
- package/dist/src/confirmation-bus/index.js.map +1 -0
- package/dist/src/confirmation-bus/message-bus.d.ts +60 -0
- package/dist/src/confirmation-bus/message-bus.js +141 -0
- package/dist/src/confirmation-bus/message-bus.js.map +1 -0
- package/dist/src/confirmation-bus/types.d.ts +59 -0
- package/dist/src/confirmation-bus/types.js +10 -0
- package/dist/src/confirmation-bus/types.js.map +1 -0
- package/dist/src/core/client.d.ts +1 -1
- package/dist/src/core/client.js +24 -11
- package/dist/src/core/client.js.map +1 -1
- package/dist/src/core/coreToolScheduler.d.ts +18 -1
- package/dist/src/core/coreToolScheduler.js +133 -13
- package/dist/src/core/coreToolScheduler.js.map +1 -1
- package/dist/src/core/geminiChat.js +2 -0
- package/dist/src/core/geminiChat.js.map +1 -1
- package/dist/src/core/subagent.js +2 -0
- package/dist/src/core/subagent.js.map +1 -1
- package/dist/src/ide/detect-ide.d.ts +44 -14
- package/dist/src/ide/detect-ide.js +35 -75
- package/dist/src/ide/detect-ide.js.map +1 -1
- package/dist/src/ide/ide-client.d.ts +4 -4
- package/dist/src/ide/ide-client.js +25 -24
- package/dist/src/ide/ide-client.js.map +1 -1
- package/dist/src/ide/ide-installer.d.ts +2 -2
- package/dist/src/ide/ide-installer.js +7 -9
- package/dist/src/ide/ide-installer.js.map +1 -1
- package/dist/src/index.d.ts +7 -1
- package/dist/src/index.js +9 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/mcp/oauth-provider.d.ts +4 -1
- package/dist/src/mcp/oauth-provider.js +30 -27
- package/dist/src/mcp/oauth-provider.js.map +1 -1
- package/dist/src/policy/config.d.ts +51 -0
- package/dist/src/policy/config.js +102 -0
- package/dist/src/policy/config.js.map +1 -0
- package/dist/src/policy/index.d.ts +5 -0
- package/dist/src/policy/index.js +6 -0
- package/dist/src/policy/index.js.map +1 -0
- package/dist/src/policy/policies/discovered.toml +9 -0
- package/dist/src/policy/policies/read-only.toml +68 -0
- package/dist/src/policy/policies/write.toml +69 -0
- package/dist/src/policy/policies/yolo.toml +8 -0
- package/dist/src/policy/policy-engine.d.ts +55 -0
- package/dist/src/policy/policy-engine.js +126 -0
- package/dist/src/policy/policy-engine.js.map +1 -0
- package/dist/src/policy/stable-stringify.d.ts +29 -0
- package/dist/src/policy/stable-stringify.js +111 -0
- package/dist/src/policy/stable-stringify.js.map +1 -0
- package/dist/src/policy/toml-loader.d.ts +37 -0
- package/dist/src/policy/toml-loader.js +183 -0
- package/dist/src/policy/toml-loader.js.map +1 -0
- package/dist/src/policy/types.d.ts +16 -0
- package/dist/src/policy/types.js +7 -0
- package/dist/src/policy/types.js.map +1 -0
- package/dist/src/telemetry/uiTelemetry.d.ts +1 -1
- package/dist/src/telemetry/uiTelemetry.js +2 -3
- package/dist/src/telemetry/uiTelemetry.js.map +1 -1
- package/dist/src/tools/edit.d.ts +3 -2
- package/dist/src/tools/edit.js +23 -10
- package/dist/src/tools/edit.js.map +1 -1
- package/dist/src/tools/glob.d.ts +3 -2
- package/dist/src/tools/glob.js +2 -2
- package/dist/src/tools/glob.js.map +1 -1
- package/dist/src/tools/grep.d.ts +3 -2
- package/dist/src/tools/grep.js +2 -2
- package/dist/src/tools/grep.js.map +1 -1
- package/dist/src/tools/ls.d.ts +3 -2
- package/dist/src/tools/ls.js +2 -2
- package/dist/src/tools/ls.js.map +1 -1
- package/dist/src/tools/mcp-tool.js +7 -1
- package/dist/src/tools/mcp-tool.js.map +1 -1
- package/dist/src/tools/memoryTool.d.ts +5 -2
- package/dist/src/tools/memoryTool.js +12 -4
- package/dist/src/tools/memoryTool.js.map +1 -1
- package/dist/src/tools/read-file.d.ts +3 -2
- package/dist/src/tools/read-file.js +2 -2
- package/dist/src/tools/read-file.js.map +1 -1
- package/dist/src/tools/read-many-files.d.ts +3 -2
- package/dist/src/tools/read-many-files.js +2 -2
- package/dist/src/tools/read-many-files.js.map +1 -1
- package/dist/src/tools/ripGrep.d.ts +3 -2
- package/dist/src/tools/ripGrep.js +2 -2
- package/dist/src/tools/ripGrep.js.map +1 -1
- package/dist/src/tools/shell.d.ts +3 -2
- package/dist/src/tools/shell.js +10 -6
- package/dist/src/tools/shell.js.map +1 -1
- package/dist/src/tools/smart-edit.d.ts +3 -2
- package/dist/src/tools/smart-edit.js +13 -9
- package/dist/src/tools/smart-edit.js.map +1 -1
- package/dist/src/tools/tool-confirmation-types.d.ts +20 -0
- package/dist/src/tools/tool-confirmation-types.js +15 -0
- package/dist/src/tools/tool-confirmation-types.js.map +1 -0
- package/dist/src/tools/tool-error.d.ts +1 -0
- package/dist/src/tools/tool-error.js +1 -0
- package/dist/src/tools/tool-error.js.map +1 -1
- package/dist/src/tools/tool-registry.d.ts +8 -1
- package/dist/src/tools/tool-registry.js +18 -4
- package/dist/src/tools/tool-registry.js.map +1 -1
- package/dist/src/tools/tools.d.ts +52 -14
- package/dist/src/tools/tools.js +71 -15
- package/dist/src/tools/tools.js.map +1 -1
- package/dist/src/tools/web-fetch.d.ts +3 -2
- package/dist/src/tools/web-fetch.js +11 -6
- package/dist/src/tools/web-fetch.js.map +1 -1
- package/dist/src/tools/web-search-invocation.d.ts +3 -1
- package/dist/src/tools/web-search-invocation.js +5 -2
- package/dist/src/tools/web-search-invocation.js.map +1 -1
- package/dist/src/tools/web-search.d.ts +3 -2
- package/dist/src/tools/web-search.js +6 -4
- package/dist/src/tools/web-search.js.map +1 -1
- package/dist/src/tools/write-file.d.ts +3 -2
- package/dist/src/tools/write-file.js +11 -6
- package/dist/src/tools/write-file.js.map +1 -1
- package/package.json +4 -2
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# Default write tool policy
|
|
2
|
+
# Priority band: 1.01 (Tier 1 - Default)
|
|
3
|
+
# These tools require user confirmation by default
|
|
4
|
+
|
|
5
|
+
[[rule]]
|
|
6
|
+
toolName = "edit"
|
|
7
|
+
decision = "ask_user"
|
|
8
|
+
priority = 1.01
|
|
9
|
+
|
|
10
|
+
[[rule]]
|
|
11
|
+
toolName = "smart_edit"
|
|
12
|
+
decision = "ask_user"
|
|
13
|
+
priority = 1.01
|
|
14
|
+
|
|
15
|
+
[[rule]]
|
|
16
|
+
toolName = "write_file"
|
|
17
|
+
decision = "ask_user"
|
|
18
|
+
priority = 1.01
|
|
19
|
+
|
|
20
|
+
[[rule]]
|
|
21
|
+
toolName = "shell"
|
|
22
|
+
decision = "ask_user"
|
|
23
|
+
priority = 1.01
|
|
24
|
+
|
|
25
|
+
[[rule]]
|
|
26
|
+
toolName = "memory"
|
|
27
|
+
decision = "ask_user"
|
|
28
|
+
priority = 1.01
|
|
29
|
+
|
|
30
|
+
[[rule]]
|
|
31
|
+
toolName = "web_fetch"
|
|
32
|
+
decision = "ask_user"
|
|
33
|
+
priority = 1.01
|
|
34
|
+
|
|
35
|
+
[[rule]]
|
|
36
|
+
toolName = "mcp_tool"
|
|
37
|
+
decision = "ask_user"
|
|
38
|
+
priority = 1.01
|
|
39
|
+
|
|
40
|
+
# Shell commands with dangerous patterns are denied
|
|
41
|
+
[[rule]]
|
|
42
|
+
toolName = "shell"
|
|
43
|
+
argsPattern = "rm\\s+-rf\\s+/"
|
|
44
|
+
decision = "deny"
|
|
45
|
+
priority = 2.0
|
|
46
|
+
|
|
47
|
+
[[rule]]
|
|
48
|
+
toolName = "shell"
|
|
49
|
+
argsPattern = "chmod\\s+777"
|
|
50
|
+
decision = "deny"
|
|
51
|
+
priority = 2.0
|
|
52
|
+
|
|
53
|
+
[[rule]]
|
|
54
|
+
toolName = "shell"
|
|
55
|
+
argsPattern = "dd\\s+if="
|
|
56
|
+
decision = "deny"
|
|
57
|
+
priority = 2.0
|
|
58
|
+
|
|
59
|
+
[[rule]]
|
|
60
|
+
toolName = "shell"
|
|
61
|
+
argsPattern = "mkfs\\."
|
|
62
|
+
decision = "deny"
|
|
63
|
+
priority = 2.0
|
|
64
|
+
|
|
65
|
+
[[rule]]
|
|
66
|
+
toolName = "shell"
|
|
67
|
+
argsPattern = ":(){ :|:& };:"
|
|
68
|
+
decision = "deny"
|
|
69
|
+
priority = 2.0
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# YOLO mode policy - allow everything
|
|
2
|
+
# Priority band: 1.999 (Tier 1 - Default, just below user settings)
|
|
3
|
+
# WARNING: This disables all safety checks. Use only in trusted environments.
|
|
4
|
+
|
|
5
|
+
[[rule]]
|
|
6
|
+
# No toolName specified = wildcard (matches all tools)
|
|
7
|
+
decision = "allow"
|
|
8
|
+
priority = 1.999
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { PolicyDecision, type PolicyEngineConfig, type PolicyRule } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* PolicyEngine evaluates tool execution requests against configured rules.
|
|
4
|
+
* Rules are matched in priority order, with the highest priority rule winning.
|
|
5
|
+
*/
|
|
6
|
+
export declare class PolicyEngine {
|
|
7
|
+
private readonly rules;
|
|
8
|
+
private readonly defaultDecision;
|
|
9
|
+
private readonly nonInteractive;
|
|
10
|
+
constructor(config?: PolicyEngineConfig);
|
|
11
|
+
/**
|
|
12
|
+
* Evaluates a tool execution request and returns a policy decision.
|
|
13
|
+
*
|
|
14
|
+
* @param toolName - The name of the tool being executed
|
|
15
|
+
* @param args - The arguments passed to the tool
|
|
16
|
+
* @param serverName - Optional MCP server name (for spoofing prevention)
|
|
17
|
+
* @returns PolicyDecision (ALLOW, DENY, or ASK_USER)
|
|
18
|
+
*/
|
|
19
|
+
evaluate(toolName: string, args: Record<string, unknown>, serverName?: string): PolicyDecision;
|
|
20
|
+
/**
|
|
21
|
+
* Finds the highest priority rule matching the tool and args.
|
|
22
|
+
*
|
|
23
|
+
* @param toolName - The name of the tool
|
|
24
|
+
* @param args - The tool arguments
|
|
25
|
+
* @returns The matching rule, or undefined if none match
|
|
26
|
+
*/
|
|
27
|
+
private findMatchingRule;
|
|
28
|
+
/**
|
|
29
|
+
* Validates that a tool name matches its claimed server name.
|
|
30
|
+
* Returns null if spoofing is detected, otherwise returns the tool name.
|
|
31
|
+
*
|
|
32
|
+
* @param toolName - The tool name (may include server prefix)
|
|
33
|
+
* @param serverName - The claimed server name
|
|
34
|
+
* @returns The validated tool name, or null if spoofing detected
|
|
35
|
+
*/
|
|
36
|
+
private validateServerName;
|
|
37
|
+
/**
|
|
38
|
+
* Returns all configured rules (for debugging/inspection).
|
|
39
|
+
*
|
|
40
|
+
* @returns Array of policy rules
|
|
41
|
+
*/
|
|
42
|
+
getRules(): readonly PolicyRule[];
|
|
43
|
+
/**
|
|
44
|
+
* Returns the default decision used when no rules match.
|
|
45
|
+
*
|
|
46
|
+
* @returns PolicyDecision
|
|
47
|
+
*/
|
|
48
|
+
getDefaultDecision(): PolicyDecision;
|
|
49
|
+
/**
|
|
50
|
+
* Returns whether the engine is in non-interactive mode.
|
|
51
|
+
*
|
|
52
|
+
* @returns boolean
|
|
53
|
+
*/
|
|
54
|
+
isNonInteractive(): boolean;
|
|
55
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { PolicyDecision, } from './types.js';
|
|
2
|
+
import { stableStringify } from './stable-stringify.js';
|
|
3
|
+
/**
|
|
4
|
+
* PolicyEngine evaluates tool execution requests against configured rules.
|
|
5
|
+
* Rules are matched in priority order, with the highest priority rule winning.
|
|
6
|
+
*/
|
|
7
|
+
export class PolicyEngine {
|
|
8
|
+
rules;
|
|
9
|
+
defaultDecision;
|
|
10
|
+
nonInteractive;
|
|
11
|
+
constructor(config) {
|
|
12
|
+
this.rules = config?.rules ?? [];
|
|
13
|
+
this.defaultDecision = config?.defaultDecision ?? PolicyDecision.ASK_USER;
|
|
14
|
+
this.nonInteractive = config?.nonInteractive ?? false;
|
|
15
|
+
// Sort rules by priority (highest first)
|
|
16
|
+
this.rules.sort((a, b) => (b.priority ?? 0) - (a.priority ?? 0));
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Evaluates a tool execution request and returns a policy decision.
|
|
20
|
+
*
|
|
21
|
+
* @param toolName - The name of the tool being executed
|
|
22
|
+
* @param args - The arguments passed to the tool
|
|
23
|
+
* @param serverName - Optional MCP server name (for spoofing prevention)
|
|
24
|
+
* @returns PolicyDecision (ALLOW, DENY, or ASK_USER)
|
|
25
|
+
*/
|
|
26
|
+
evaluate(toolName, args, serverName) {
|
|
27
|
+
// Validate serverName to prevent spoofing
|
|
28
|
+
if (serverName) {
|
|
29
|
+
const validatedToolName = this.validateServerName(toolName, serverName);
|
|
30
|
+
if (validatedToolName === null) {
|
|
31
|
+
// Server name spoofing detected - deny
|
|
32
|
+
return PolicyDecision.DENY;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
// Find the highest priority matching rule
|
|
36
|
+
const matchingRule = this.findMatchingRule(toolName, args);
|
|
37
|
+
if (matchingRule) {
|
|
38
|
+
const decision = matchingRule.decision;
|
|
39
|
+
// In non-interactive mode, ASK_USER becomes DENY
|
|
40
|
+
if (this.nonInteractive && decision === PolicyDecision.ASK_USER) {
|
|
41
|
+
return PolicyDecision.DENY;
|
|
42
|
+
}
|
|
43
|
+
return decision;
|
|
44
|
+
}
|
|
45
|
+
// No matching rule - use default decision
|
|
46
|
+
if (this.nonInteractive &&
|
|
47
|
+
this.defaultDecision === PolicyDecision.ASK_USER) {
|
|
48
|
+
return PolicyDecision.DENY;
|
|
49
|
+
}
|
|
50
|
+
return this.defaultDecision;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Finds the highest priority rule matching the tool and args.
|
|
54
|
+
*
|
|
55
|
+
* @param toolName - The name of the tool
|
|
56
|
+
* @param args - The tool arguments
|
|
57
|
+
* @returns The matching rule, or undefined if none match
|
|
58
|
+
*/
|
|
59
|
+
findMatchingRule(toolName, args) {
|
|
60
|
+
const argsString = stableStringify(args);
|
|
61
|
+
for (const rule of this.rules) {
|
|
62
|
+
// Check tool name match
|
|
63
|
+
const toolMatches = !rule.toolName || rule.toolName === toolName;
|
|
64
|
+
if (!toolMatches) {
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
// Check args pattern match
|
|
68
|
+
const argsMatch = !rule.argsPattern || rule.argsPattern.test(argsString);
|
|
69
|
+
if (!argsMatch) {
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
// Both match - return this rule
|
|
73
|
+
return rule;
|
|
74
|
+
}
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Validates that a tool name matches its claimed server name.
|
|
79
|
+
* Returns null if spoofing is detected, otherwise returns the tool name.
|
|
80
|
+
*
|
|
81
|
+
* @param toolName - The tool name (may include server prefix)
|
|
82
|
+
* @param serverName - The claimed server name
|
|
83
|
+
* @returns The validated tool name, or null if spoofing detected
|
|
84
|
+
*/
|
|
85
|
+
validateServerName(toolName, serverName) {
|
|
86
|
+
// For MCP tools, expect format: "serverName__toolName"
|
|
87
|
+
const expectedPrefix = `${serverName}__`;
|
|
88
|
+
if (toolName.startsWith(expectedPrefix)) {
|
|
89
|
+
return toolName;
|
|
90
|
+
}
|
|
91
|
+
// If tool name doesn't have the expected prefix, check if it's a non-MCP tool
|
|
92
|
+
// Non-MCP tools don't have a server prefix, so if a serverName is provided
|
|
93
|
+
// but the tool doesn't have the prefix, it's likely spoofing
|
|
94
|
+
if (!toolName.includes('__')) {
|
|
95
|
+
// This is a built-in tool, serverName should not be set
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
// Tool has a different server prefix - spoofing attempt
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Returns all configured rules (for debugging/inspection).
|
|
103
|
+
*
|
|
104
|
+
* @returns Array of policy rules
|
|
105
|
+
*/
|
|
106
|
+
getRules() {
|
|
107
|
+
return [...this.rules];
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Returns the default decision used when no rules match.
|
|
111
|
+
*
|
|
112
|
+
* @returns PolicyDecision
|
|
113
|
+
*/
|
|
114
|
+
getDefaultDecision() {
|
|
115
|
+
return this.defaultDecision;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Returns whether the engine is in non-interactive mode.
|
|
119
|
+
*
|
|
120
|
+
* @returns boolean
|
|
121
|
+
*/
|
|
122
|
+
isNonInteractive() {
|
|
123
|
+
return this.nonInteractive;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
//# sourceMappingURL=policy-engine.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"policy-engine.js","sourceRoot":"","sources":["../../../src/policy/policy-engine.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,GAGf,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAExD;;;GAGG;AACH,MAAM,OAAO,YAAY;IACN,KAAK,CAAe;IACpB,eAAe,CAAiB;IAChC,cAAc,CAAU;IAEzC,YAAY,MAA2B;QACrC,IAAI,CAAC,KAAK,GAAG,MAAM,EAAE,KAAK,IAAI,EAAE,CAAC;QACjC,IAAI,CAAC,eAAe,GAAG,MAAM,EAAE,eAAe,IAAI,cAAc,CAAC,QAAQ,CAAC;QAC1E,IAAI,CAAC,cAAc,GAAG,MAAM,EAAE,cAAc,IAAI,KAAK,CAAC;QAEtD,yCAAyC;QACzC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,QAAQ,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,QAAQ,IA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AAE,CAAC;gBACjB,SAAS;YACX,CAAC;YAED,2BAA2B;YAC3B,MAAM,SAAS,GAAG,CAAC,IAAI,CAAC,WAAW,IAAI,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;YACzE,IAAI,CAAC,SAAS,EAAE,CAAC;gBACf,SAAS;YACX,CAAC;YAED,gCAAgC;YAChC,OAAO,IAAI,CAAC;QACd,CAAC;QAED,OAAO,SAAS,CAAC;IACnB,CAAC;IAED;;;;;;;OAOG;IACK,kBAAkB,CACxB,QAAgB,EAChB,UAAkB;QAElB,uDAAuD;QACvD,MAAM,cAAc,GAAG,GAAG,UAAU,IAAI,CAAC;QAEzC,IAAI,QAAQ,CAAC,UAAU,CAAC,cAAc,CAAC,EAAE,CAAC;YACxC,OAAO,QAAQ,CAAC;QAClB,CAAC;QAED,8EAA8E;QAC9E,2EAA2E;QAC3E,6DAA6D;QAC7D,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;YAC7B,wDAAwD;YACxD,OAAO,IAAI,CAAC;QACd,CAAC;QAED,wDAAwD;QACxD,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;;;OAIG;IACH,QAAQ;QACN,OAAO,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC;IACzB,CAAC;IAED;;;;OAIG;IACH,kBAAkB;QAChB,OAAO,IAAI,CAAC,eAAe,CAAC;IAC9B,CAAC;IAED;;;;OAIG;IACH,gBAAgB;QACd,OAAO,IAAI,CAAC,cAAc,CAAC;IAC7B,CAAC;CACF"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provides deterministic JSON stringification for pattern matching in policy rules.
|
|
3
|
+
* Ensures consistent ordering of object keys and handling of special values.
|
|
4
|
+
*/
|
|
5
|
+
type JSONValue = string | number | boolean | null | JSONValue[] | {
|
|
6
|
+
[key: string]: JSONValue;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Deterministically stringifies a value for use in pattern matching.
|
|
10
|
+
* - Object keys are sorted alphabetically
|
|
11
|
+
* - Arrays maintain their order
|
|
12
|
+
* - undefined values are omitted
|
|
13
|
+
* - Functions and symbols are converted to null
|
|
14
|
+
* - Circular references throw an error
|
|
15
|
+
*
|
|
16
|
+
* @param value - The value to stringify
|
|
17
|
+
* @param space - Optional spacing for readability (default: none)
|
|
18
|
+
* @returns Deterministic JSON string
|
|
19
|
+
*/
|
|
20
|
+
export declare function stableStringify(value: unknown, space?: string | number): string;
|
|
21
|
+
/**
|
|
22
|
+
* Parses a stable-stringified JSON string back into a value.
|
|
23
|
+
* This is just a wrapper around JSON.parse for consistency.
|
|
24
|
+
*
|
|
25
|
+
* @param text - The JSON string to parse
|
|
26
|
+
* @returns Parsed value
|
|
27
|
+
*/
|
|
28
|
+
export declare function stableParse(text: string): JSONValue;
|
|
29
|
+
export {};
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provides deterministic JSON stringification for pattern matching in policy rules.
|
|
3
|
+
* Ensures consistent ordering of object keys and handling of special values.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Deterministically stringifies a value for use in pattern matching.
|
|
7
|
+
* - Object keys are sorted alphabetically
|
|
8
|
+
* - Arrays maintain their order
|
|
9
|
+
* - undefined values are omitted
|
|
10
|
+
* - Functions and symbols are converted to null
|
|
11
|
+
* - Circular references throw an error
|
|
12
|
+
*
|
|
13
|
+
* @param value - The value to stringify
|
|
14
|
+
* @param space - Optional spacing for readability (default: none)
|
|
15
|
+
* @returns Deterministic JSON string
|
|
16
|
+
*/
|
|
17
|
+
export function stableStringify(value, space) {
|
|
18
|
+
const seen = new WeakSet();
|
|
19
|
+
function stringify(val, indent, currentDepth) {
|
|
20
|
+
// Handle primitives
|
|
21
|
+
if (val === null)
|
|
22
|
+
return 'null';
|
|
23
|
+
if (val === undefined)
|
|
24
|
+
return 'null';
|
|
25
|
+
if (typeof val === 'boolean')
|
|
26
|
+
return String(val);
|
|
27
|
+
if (typeof val === 'number') {
|
|
28
|
+
if (!Number.isFinite(val))
|
|
29
|
+
return 'null';
|
|
30
|
+
return String(val);
|
|
31
|
+
}
|
|
32
|
+
if (typeof val === 'string') {
|
|
33
|
+
return JSON.stringify(val);
|
|
34
|
+
}
|
|
35
|
+
// Handle functions and symbols
|
|
36
|
+
if (typeof val === 'function' || typeof val === 'symbol') {
|
|
37
|
+
return 'null';
|
|
38
|
+
}
|
|
39
|
+
// Handle objects and arrays
|
|
40
|
+
if (typeof val === 'object') {
|
|
41
|
+
// Circular reference check
|
|
42
|
+
if (seen.has(val)) {
|
|
43
|
+
throw new TypeError('Converting circular structure to JSON');
|
|
44
|
+
}
|
|
45
|
+
seen.add(val);
|
|
46
|
+
try {
|
|
47
|
+
// Handle arrays
|
|
48
|
+
if (Array.isArray(val)) {
|
|
49
|
+
const items = [];
|
|
50
|
+
const nextIndent = space ? indent + getIndentString(space) : '';
|
|
51
|
+
const separator = space ? '\n' : '';
|
|
52
|
+
for (let i = 0; i < val.length; i++) {
|
|
53
|
+
const item = stringify(val[i], nextIndent, currentDepth + 1);
|
|
54
|
+
items.push(space ? `${nextIndent}${item}` : item);
|
|
55
|
+
}
|
|
56
|
+
if (items.length === 0) {
|
|
57
|
+
return '[]';
|
|
58
|
+
}
|
|
59
|
+
return space
|
|
60
|
+
? `[${separator}${items.join(`,${separator}`)}${separator}${indent}]`
|
|
61
|
+
: `[${items.join(',')}]`;
|
|
62
|
+
}
|
|
63
|
+
// Handle objects
|
|
64
|
+
const keys = Object.keys(val).sort();
|
|
65
|
+
const pairs = [];
|
|
66
|
+
const nextIndent = space ? indent + getIndentString(space) : '';
|
|
67
|
+
const separator = space ? '\n' : '';
|
|
68
|
+
for (const key of keys) {
|
|
69
|
+
const value = val[key];
|
|
70
|
+
if (value !== undefined) {
|
|
71
|
+
const stringifiedKey = JSON.stringify(key);
|
|
72
|
+
const stringifiedValue = stringify(value, nextIndent, currentDepth + 1);
|
|
73
|
+
const pair = space
|
|
74
|
+
? `${nextIndent}${stringifiedKey}: ${stringifiedValue}`
|
|
75
|
+
: `${stringifiedKey}:${stringifiedValue}`;
|
|
76
|
+
pairs.push(pair);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (pairs.length === 0) {
|
|
80
|
+
return '{}';
|
|
81
|
+
}
|
|
82
|
+
return space
|
|
83
|
+
? `{${separator}${pairs.join(`,${separator}`)}${separator}${indent}}`
|
|
84
|
+
: `{${pairs.join(',')}}`;
|
|
85
|
+
}
|
|
86
|
+
finally {
|
|
87
|
+
seen.delete(val);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
// Fallback for unknown types
|
|
91
|
+
return 'null';
|
|
92
|
+
}
|
|
93
|
+
function getIndentString(space) {
|
|
94
|
+
if (typeof space === 'number') {
|
|
95
|
+
return ' '.repeat(Math.min(10, Math.max(0, Math.floor(space))));
|
|
96
|
+
}
|
|
97
|
+
return String(space).slice(0, 10);
|
|
98
|
+
}
|
|
99
|
+
return stringify(value, '', 0);
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Parses a stable-stringified JSON string back into a value.
|
|
103
|
+
* This is just a wrapper around JSON.parse for consistency.
|
|
104
|
+
*
|
|
105
|
+
* @param text - The JSON string to parse
|
|
106
|
+
* @returns Parsed value
|
|
107
|
+
*/
|
|
108
|
+
export function stableParse(text) {
|
|
109
|
+
return JSON.parse(text);
|
|
110
|
+
}
|
|
111
|
+
//# sourceMappingURL=stable-stringify.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stable-stringify.js","sourceRoot":"","sources":["../../../src/policy/stable-stringify.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAUH;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,eAAe,CAC7B,KAAc,EACd,KAAuB;IAEvB,MAAM,IAAI,GAAG,IAAI,OAAO,EAAU,CAAC;IAEnC,SAAS,SAAS,CA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AM,UAAU,WAAW,CAAC,IAAY;IACtC,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAc,CAAC;AACvC,CAAC"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TOML Policy Loader
|
|
3
|
+
*
|
|
4
|
+
* Loads and parses TOML policy files into PolicyRule objects.
|
|
5
|
+
* Handles:
|
|
6
|
+
* - TOML parsing with @iarna/toml
|
|
7
|
+
* - Schema validation with zod
|
|
8
|
+
* - argsPattern string → RegExp conversion
|
|
9
|
+
* - Priority band enforcement
|
|
10
|
+
* - Comprehensive error handling
|
|
11
|
+
*/
|
|
12
|
+
import { type PolicyRule } from './types.js';
|
|
13
|
+
/**
|
|
14
|
+
* Error thrown when policy loading or validation fails
|
|
15
|
+
*/
|
|
16
|
+
export declare class PolicyLoadError extends Error {
|
|
17
|
+
readonly path?: string;
|
|
18
|
+
readonly cause?: unknown;
|
|
19
|
+
constructor(message: string, path?: string, options?: {
|
|
20
|
+
cause?: unknown;
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Loads and parses a TOML policy file
|
|
25
|
+
*
|
|
26
|
+
* @param path - Absolute path to the TOML policy file
|
|
27
|
+
* @returns Array of PolicyRule objects
|
|
28
|
+
* @throws PolicyLoadError if file cannot be read, parsed, or validated
|
|
29
|
+
*/
|
|
30
|
+
export declare function loadPolicyFromToml(path: string): Promise<PolicyRule[]>;
|
|
31
|
+
/**
|
|
32
|
+
* Loads all default policy files from the policies directory
|
|
33
|
+
*
|
|
34
|
+
* @returns Array of PolicyRule objects from all default policies
|
|
35
|
+
* @throws PolicyLoadError if any default policy file fails to load
|
|
36
|
+
*/
|
|
37
|
+
export declare function loadDefaultPolicies(): Promise<PolicyRule[]>;
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TOML Policy Loader
|
|
3
|
+
*
|
|
4
|
+
* Loads and parses TOML policy files into PolicyRule objects.
|
|
5
|
+
* Handles:
|
|
6
|
+
* - TOML parsing with @iarna/toml
|
|
7
|
+
* - Schema validation with zod
|
|
8
|
+
* - argsPattern string → RegExp conversion
|
|
9
|
+
* - Priority band enforcement
|
|
10
|
+
* - Comprehensive error handling
|
|
11
|
+
*/
|
|
12
|
+
import * as toml from '@iarna/toml';
|
|
13
|
+
import { z } from 'zod';
|
|
14
|
+
import { readFile } from 'node:fs/promises';
|
|
15
|
+
import { join } from 'node:path';
|
|
16
|
+
import { PolicyDecision } from './types.js';
|
|
17
|
+
/**
|
|
18
|
+
* Zod schema for a single policy rule in TOML
|
|
19
|
+
*/
|
|
20
|
+
const TomlRuleSchema = z.object({
|
|
21
|
+
toolName: z.string().optional(),
|
|
22
|
+
argsPattern: z.string().optional(),
|
|
23
|
+
decision: z.nativeEnum(PolicyDecision),
|
|
24
|
+
priority: z.number().optional(),
|
|
25
|
+
});
|
|
26
|
+
/**
|
|
27
|
+
* Zod schema for the entire policy TOML file
|
|
28
|
+
*/
|
|
29
|
+
const PolicyFileSchema = z.object({
|
|
30
|
+
rule: z.array(TomlRuleSchema),
|
|
31
|
+
});
|
|
32
|
+
/**
|
|
33
|
+
* Error thrown when policy loading or validation fails
|
|
34
|
+
*/
|
|
35
|
+
export class PolicyLoadError extends Error {
|
|
36
|
+
path;
|
|
37
|
+
cause;
|
|
38
|
+
constructor(message, path, options) {
|
|
39
|
+
super(message);
|
|
40
|
+
this.name = 'PolicyLoadError';
|
|
41
|
+
this.path = path;
|
|
42
|
+
this.cause = options?.cause;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Validates priority band according to specification:
|
|
47
|
+
* - Tier 3 (Admin): 3.xxx
|
|
48
|
+
* - Tier 2 (User): 2.xxx
|
|
49
|
+
* - Tier 1 (Default): 1.xxx
|
|
50
|
+
*
|
|
51
|
+
* Throws if priority is outside valid range.
|
|
52
|
+
*/
|
|
53
|
+
function validatePriorityBand(priority, path) {
|
|
54
|
+
if (priority === undefined) {
|
|
55
|
+
return; // Priority 0 is default and valid
|
|
56
|
+
}
|
|
57
|
+
// Valid range: 1.0 to 3.999
|
|
58
|
+
if (priority < 1.0 || priority >= 4.0) {
|
|
59
|
+
throw new PolicyLoadError(`Invalid priority ${priority} in ${path}. Priority must be in range [1.0, 4.0).`, path);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Converts argsPattern string to RegExp with proper error handling
|
|
64
|
+
*/
|
|
65
|
+
function parseArgsPattern(pattern, path) {
|
|
66
|
+
try {
|
|
67
|
+
return new RegExp(pattern);
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
throw new PolicyLoadError(`Invalid regular expression in argsPattern: ${pattern}`, path, { cause: error });
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Transforms a parsed TOML rule into a PolicyRule object
|
|
75
|
+
*/
|
|
76
|
+
function transformRule(rule, path) {
|
|
77
|
+
validatePriorityBand(rule.priority, path);
|
|
78
|
+
const policyRule = {
|
|
79
|
+
decision: rule.decision,
|
|
80
|
+
priority: rule.priority ?? 0,
|
|
81
|
+
};
|
|
82
|
+
// toolName is optional - undefined means wildcard (matches all tools)
|
|
83
|
+
if (rule.toolName !== undefined) {
|
|
84
|
+
policyRule.toolName = rule.toolName;
|
|
85
|
+
}
|
|
86
|
+
// argsPattern is optional - converts string to RegExp
|
|
87
|
+
if (rule.argsPattern !== undefined) {
|
|
88
|
+
policyRule.argsPattern = parseArgsPattern(rule.argsPattern, path);
|
|
89
|
+
}
|
|
90
|
+
return policyRule;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Loads and parses a TOML policy file
|
|
94
|
+
*
|
|
95
|
+
* @param path - Absolute path to the TOML policy file
|
|
96
|
+
* @returns Array of PolicyRule objects
|
|
97
|
+
* @throws PolicyLoadError if file cannot be read, parsed, or validated
|
|
98
|
+
*/
|
|
99
|
+
export async function loadPolicyFromToml(path) {
|
|
100
|
+
let content;
|
|
101
|
+
try {
|
|
102
|
+
content = await readFile(path, 'utf-8');
|
|
103
|
+
}
|
|
104
|
+
catch (error) {
|
|
105
|
+
throw new PolicyLoadError(`Failed to read policy file: ${path}`, path, {
|
|
106
|
+
cause: error,
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
let parsed;
|
|
110
|
+
try {
|
|
111
|
+
parsed = toml.parse(content);
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
const tomlError = error;
|
|
115
|
+
throw new PolicyLoadError(`Invalid TOML syntax in ${path}: ${tomlError.message}`, path, { cause: error });
|
|
116
|
+
}
|
|
117
|
+
let validated;
|
|
118
|
+
try {
|
|
119
|
+
validated = PolicyFileSchema.parse(parsed);
|
|
120
|
+
}
|
|
121
|
+
catch (error) {
|
|
122
|
+
if (error instanceof z.ZodError) {
|
|
123
|
+
const errorMessages = error.errors.map((e) => e.message).join(', ');
|
|
124
|
+
throw new PolicyLoadError(`Invalid policy schema in ${path}: ${errorMessages}`, path, { cause: error });
|
|
125
|
+
}
|
|
126
|
+
throw error;
|
|
127
|
+
}
|
|
128
|
+
// Transform each rule
|
|
129
|
+
const rules = [];
|
|
130
|
+
for (const tomlRule of validated.rule) {
|
|
131
|
+
try {
|
|
132
|
+
const rule = transformRule(tomlRule, path);
|
|
133
|
+
rules.push(rule);
|
|
134
|
+
}
|
|
135
|
+
catch (error) {
|
|
136
|
+
if (error instanceof PolicyLoadError) {
|
|
137
|
+
throw error;
|
|
138
|
+
}
|
|
139
|
+
throw new PolicyLoadError(`Failed to transform rule in ${path}`, path, {
|
|
140
|
+
cause: error,
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return rules;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Loads all default policy files from the policies directory
|
|
148
|
+
*
|
|
149
|
+
* @returns Array of PolicyRule objects from all default policies
|
|
150
|
+
* @throws PolicyLoadError if any default policy file fails to load
|
|
151
|
+
*/
|
|
152
|
+
export async function loadDefaultPolicies() {
|
|
153
|
+
const policyFiles = [
|
|
154
|
+
'read-only.toml',
|
|
155
|
+
'write.toml',
|
|
156
|
+
// Note: yolo.toml and discovered.toml are loaded conditionally
|
|
157
|
+
];
|
|
158
|
+
const policiesDirUrl = new URL('./policies/', import.meta.url);
|
|
159
|
+
let policiesDir = decodeURIComponent(policiesDirUrl.pathname);
|
|
160
|
+
if (policiesDir.startsWith('/@fs/')) {
|
|
161
|
+
policiesDir = `/${policiesDir.slice('/@fs/'.length)}`;
|
|
162
|
+
}
|
|
163
|
+
if (process.platform === 'win32' && policiesDir.match(/^\/[A-Za-z]:\//)) {
|
|
164
|
+
policiesDir = policiesDir.slice(1);
|
|
165
|
+
}
|
|
166
|
+
const rules = [];
|
|
167
|
+
for (const file of policyFiles) {
|
|
168
|
+
const path = join(policiesDir, file);
|
|
169
|
+
try {
|
|
170
|
+
const fileRules = await loadPolicyFromToml(path);
|
|
171
|
+
rules.push(...fileRules);
|
|
172
|
+
}
|
|
173
|
+
catch (error) {
|
|
174
|
+
// Re-throw with context about which default file failed
|
|
175
|
+
if (error instanceof PolicyLoadError) {
|
|
176
|
+
throw new PolicyLoadError(`Failed to load default policy ${file}: ${error.message}`, path, { cause: error });
|
|
177
|
+
}
|
|
178
|
+
throw error;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return rules;
|
|
182
|
+
}
|
|
183
|
+
//# sourceMappingURL=toml-loader.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"toml-loader.js","sourceRoot":"","sources":["../../../src/policy/toml-loader.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,IAAI,MAAM,aAAa,CAAC;AACpC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,IAAI,EAAE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E,CACjB,CAAC;YACJ,CAAC;YACD,MAAM,KAAK,CAAC;QACd,CAAC;IACH,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export declare enum PolicyDecision {
|
|
2
|
+
ALLOW = "allow",
|
|
3
|
+
DENY = "deny",
|
|
4
|
+
ASK_USER = "ask_user"
|
|
5
|
+
}
|
|
6
|
+
export interface PolicyRule {
|
|
7
|
+
toolName?: string;
|
|
8
|
+
argsPattern?: RegExp;
|
|
9
|
+
decision: PolicyDecision;
|
|
10
|
+
priority?: number;
|
|
11
|
+
}
|
|
12
|
+
export interface PolicyEngineConfig {
|
|
13
|
+
rules?: PolicyRule[];
|
|
14
|
+
defaultDecision?: PolicyDecision;
|
|
15
|
+
nonInteractive?: boolean;
|
|
16
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/policy/types.ts"],"names":[],"mappings":"AAAA,MAAM,CAAN,IAAY,cAIX;AAJD,WAAY,cAAc;IACxB,iCAAe,CAAA;IACf,+BAAa,CAAA;IACb,uCAAqB,CAAA;AACvB,CAAC,EAJW,cAAc,KAAd,cAAc,QAIzB"}
|