@kirkchinese/harness-forge 0.0.0-stage → 0.5.0
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/LICENSE +21 -0
- package/README.md +46 -2
- package/assets/icon.svg +12 -0
- package/cordis.patch.yml +28 -0
- package/docs/DETAIL.zh.md +47 -0
- package/lib/agents/presets.d.ts +98 -0
- package/lib/agents/presets.js +292 -0
- package/lib/agents/profiles.d.ts +58 -0
- package/lib/agents/profiles.js +279 -0
- package/lib/agents/row.d.ts +17 -0
- package/lib/agents/row.js +148 -0
- package/lib/client/paths.d.ts +12 -0
- package/lib/client/paths.js +68 -0
- package/lib/client/styles.d.ts +8 -0
- package/lib/client/styles.js +89 -0
- package/lib/client/typert.d.ts +25 -0
- package/lib/client/typert.js +158 -0
- package/lib/client.js +23823 -0
- package/lib/commands/definition.d.ts +40 -0
- package/lib/commands/definition.js +113 -0
- package/lib/commands/output-styles.d.ts +42 -0
- package/lib/commands/output-styles.js +152 -0
- package/lib/commands/row.d.ts +9 -0
- package/lib/commands/row.js +157 -0
- package/lib/context/instructions.d.ts +32 -0
- package/lib/context/instructions.js +88 -0
- package/lib/context/row.d.ts +16 -0
- package/lib/context/row.js +17 -0
- package/lib/dsh/commands.d.ts +30 -0
- package/lib/dsh/commands.js +61 -0
- package/lib/dsh/host.d.ts +110 -0
- package/lib/dsh/host.js +217 -0
- package/lib/dsh/model-route.d.ts +21 -0
- package/lib/dsh/model-route.js +37 -0
- package/lib/dsh/session-records.d.ts +78 -0
- package/lib/dsh/session-records.js +36 -0
- package/lib/dsh/subagents.d.ts +46 -0
- package/lib/dsh/subagents.js +14 -0
- package/lib/forge/contract.d.ts +271 -0
- package/lib/forge/contract.js +45 -0
- package/lib/forge/follow.d.ts +30 -0
- package/lib/forge/follow.js +73 -0
- package/lib/forge/ledger.d.ts +206 -0
- package/lib/forge/ledger.js +1005 -0
- package/lib/forge/live-mounts.d.ts +45 -0
- package/lib/forge/live-mounts.js +78 -0
- package/lib/forge/npm.d.ts +31 -0
- package/lib/forge/npm.js +60 -0
- package/lib/forge/projects.d.ts +97 -0
- package/lib/forge/projects.js +343 -0
- package/lib/forge/receipts.d.ts +5 -0
- package/lib/forge/receipts.js +31 -0
- package/lib/forge/remote/archive.d.ts +65 -0
- package/lib/forge/remote/archive.js +565 -0
- package/lib/forge/remote/code-parts.d.ts +18 -0
- package/lib/forge/remote/code-parts.js +76 -0
- package/lib/forge/remote/fetch.d.ts +265 -0
- package/lib/forge/remote/fetch.js +1133 -0
- package/lib/forge/remote/service.d.ts +256 -0
- package/lib/forge/remote/service.js +868 -0
- package/lib/forge/row.d.ts +32 -0
- package/lib/forge/row.js +59 -0
- package/lib/forge/service.d.ts +115 -0
- package/lib/forge/service.js +290 -0
- package/lib/forge/settings.d.ts +43 -0
- package/lib/forge/settings.js +122 -0
- package/lib/forge/state.d.ts +129 -0
- package/lib/forge/state.js +266 -0
- package/lib/forge/trust.d.ts +48 -0
- package/lib/forge/trust.js +107 -0
- package/lib/forge/turns.d.ts +13 -0
- package/lib/forge/turns.js +56 -0
- package/lib/hooks/broker/engine.d.ts +112 -0
- package/lib/hooks/broker/engine.js +722 -0
- package/lib/hooks/broker/helpers.d.ts +10 -0
- package/lib/hooks/broker/helpers.js +22 -0
- package/lib/hooks/broker/lifecycle.d.ts +65 -0
- package/lib/hooks/broker/lifecycle.js +342 -0
- package/lib/hooks/broker/session-events.d.ts +27 -0
- package/lib/hooks/broker/session-events.js +205 -0
- package/lib/hooks/broker/tools.d.ts +52 -0
- package/lib/hooks/broker/tools.js +424 -0
- package/lib/hooks/broker/types.d.ts +264 -0
- package/lib/hooks/broker/types.js +1 -0
- package/lib/hooks/broker.d.ts +80 -0
- package/lib/hooks/broker.js +95 -0
- package/lib/hooks/codex-user.d.ts +5 -0
- package/lib/hooks/codex-user.js +109 -0
- package/lib/hooks/condition.d.ts +87 -0
- package/lib/hooks/condition.js +602 -0
- package/lib/hooks/config.d.ts +85 -0
- package/lib/hooks/config.js +154 -0
- package/lib/hooks/glob.d.ts +41 -0
- package/lib/hooks/glob.js +114 -0
- package/lib/hooks/matcher.d.ts +20 -0
- package/lib/hooks/matcher.js +39 -0
- package/lib/hooks/notice.d.ts +17 -0
- package/lib/hooks/notice.js +39 -0
- package/lib/hooks/output.d.ts +56 -0
- package/lib/hooks/output.js +214 -0
- package/lib/hooks/permissions.d.ts +19 -0
- package/lib/hooks/permissions.js +202 -0
- package/lib/hooks/powershell-aliases.d.ts +19 -0
- package/lib/hooks/powershell-aliases.js +58 -0
- package/lib/hooks/process.d.ts +64 -0
- package/lib/hooks/process.js +289 -0
- package/lib/hooks/row.d.ts +17 -0
- package/lib/hooks/row.js +96 -0
- package/lib/hooks/runner.d.ts +98 -0
- package/lib/hooks/runner.js +322 -0
- package/lib/hooks/shell-commands.d.ts +28 -0
- package/lib/hooks/shell-commands.js +241 -0
- package/lib/hooks/shell.d.ts +44 -0
- package/lib/hooks/shell.js +129 -0
- package/lib/hooks/transcript.d.ts +28 -0
- package/lib/hooks/transcript.js +141 -0
- package/lib/hooks/vocabulary.d.ts +57 -0
- package/lib/hooks/vocabulary.js +366 -0
- package/lib/index.d.ts +31 -0
- package/lib/index.js +287 -0
- package/lib/mcp/auth.d.ts +35 -0
- package/lib/mcp/auth.js +178 -0
- package/lib/mcp/catalog.d.ts +39 -0
- package/lib/mcp/catalog.js +69 -0
- package/lib/mcp/mount.d.ts +113 -0
- package/lib/mcp/mount.js +317 -0
- package/lib/mcp/policy.d.ts +42 -0
- package/lib/mcp/policy.js +43 -0
- package/lib/mcp/remote.d.ts +45 -0
- package/lib/mcp/remote.js +133 -0
- package/lib/mcp/row.d.ts +9 -0
- package/lib/mcp/row.js +225 -0
- package/lib/mods/bundler.d.ts +38 -0
- package/lib/mods/bundler.js +157 -0
- package/lib/mods/chain.d.ts +103 -0
- package/lib/mods/chain.js +436 -0
- package/lib/mods/claude.d.ts +27 -0
- package/lib/mods/claude.js +56 -0
- package/lib/mods/engine.d.ts +160 -0
- package/lib/mods/engine.js +701 -0
- package/lib/mods/expect.d.ts +8 -0
- package/lib/mods/expect.js +248 -0
- package/lib/mods/host.d.ts +105 -0
- package/lib/mods/host.js +462 -0
- package/lib/mods/loader.d.ts +53 -0
- package/lib/mods/loader.js +231 -0
- package/lib/mods/ops.d.ts +13 -0
- package/lib/mods/ops.js +117 -0
- package/lib/mods/row.d.ts +9 -0
- package/lib/mods/row.js +77 -0
- package/lib/mods/runtime.d.ts +60 -0
- package/lib/mods/runtime.js +133 -0
- package/lib/mods/service/commands.d.ts +17 -0
- package/lib/mods/service/commands.js +121 -0
- package/lib/mods/service/drawings.d.ts +36 -0
- package/lib/mods/service/drawings.js +64 -0
- package/lib/mods/service/sessions.d.ts +63 -0
- package/lib/mods/service/sessions.js +345 -0
- package/lib/mods/service/tools.d.ts +32 -0
- package/lib/mods/service/tools.js +253 -0
- package/lib/mods/service/turns.d.ts +33 -0
- package/lib/mods/service/turns.js +136 -0
- package/lib/mods/service/types.d.ts +151 -0
- package/lib/mods/service/types.js +1 -0
- package/lib/mods/service.d.ts +72 -0
- package/lib/mods/service.js +104 -0
- package/lib/mods/surfaces.d.ts +93 -0
- package/lib/mods/surfaces.js +309 -0
- package/lib/mods/values.d.ts +30 -0
- package/lib/mods/values.js +95 -0
- package/lib/opencode/bun.d.ts +40 -0
- package/lib/opencode/bun.js +168 -0
- package/lib/opencode/convert.d.ts +53 -0
- package/lib/opencode/convert.js +255 -0
- package/lib/opencode/row.d.ts +22 -0
- package/lib/opencode/row.js +104 -0
- package/lib/opencode/runtime/core.d.ts +74 -0
- package/lib/opencode/runtime/core.js +249 -0
- package/lib/opencode/runtime/events.d.ts +80 -0
- package/lib/opencode/runtime/events.js +281 -0
- package/lib/opencode/runtime/mount.d.ts +27 -0
- package/lib/opencode/runtime/mount.js +283 -0
- package/lib/opencode/runtime/server.d.ts +27 -0
- package/lib/opencode/runtime/server.js +354 -0
- package/lib/opencode/runtime/types.d.ts +109 -0
- package/lib/opencode/runtime/types.js +1 -0
- package/lib/opencode/runtime.d.ts +55 -0
- package/lib/opencode/runtime.js +79 -0
- package/lib/porting/adapter-paths.d.ts +13 -0
- package/lib/porting/adapter-paths.js +120 -0
- package/lib/porting/port-gap.d.ts +36 -0
- package/lib/porting/port-gap.js +158 -0
- package/lib/porting/port-sessions.d.ts +41 -0
- package/lib/porting/port-sessions.js +98 -0
- package/lib/porting/prompt.d.ts +23 -0
- package/lib/porting/prompt.js +51 -0
- package/lib/porting/row.d.ts +16 -0
- package/lib/porting/row.js +27 -0
- package/lib/porting/verify-port.d.ts +46 -0
- package/lib/porting/verify-port.js +82 -0
- package/lib/presets/fs-search.d.ts +25 -0
- package/lib/presets/fs-search.js +17 -0
- package/lib/presets/tool-filter.d.ts +35 -0
- package/lib/presets/tool-filter.js +91 -0
- package/lib/sessions/chat-import.d.ts +48 -0
- package/lib/sessions/chat-import.js +44 -0
- package/lib/sessions/claude.d.ts +35 -0
- package/lib/sessions/claude.js +104 -0
- package/lib/sessions/codex-discover.d.ts +34 -0
- package/lib/sessions/codex-discover.js +205 -0
- package/lib/sessions/codex-meta.d.ts +63 -0
- package/lib/sessions/codex-meta.js +148 -0
- package/lib/sessions/codex-text.d.ts +9 -0
- package/lib/sessions/codex-text.js +44 -0
- package/lib/sessions/codex.d.ts +42 -0
- package/lib/sessions/codex.js +270 -0
- package/lib/sessions/opencode.d.ts +38 -0
- package/lib/sessions/opencode.js +339 -0
- package/lib/sessions/persist.d.ts +162 -0
- package/lib/sessions/persist.js +377 -0
- package/lib/sessions/row.d.ts +15 -0
- package/lib/sessions/row.js +101 -0
- package/lib/sessions/turns.d.ts +31 -0
- package/lib/sessions/turns.js +63 -0
- package/lib/sessions/zstd.d.ts +15 -0
- package/lib/sessions/zstd.js +152 -0
- package/lib/shared/invocation.d.ts +169 -0
- package/lib/shared/invocation.js +325 -0
- package/lib/shared/render.d.ts +101 -0
- package/lib/shared/render.js +1353 -0
- package/lib/shared/util.d.ts +21 -0
- package/lib/shared/util.js +32 -0
- package/lib/skills/bundled.d.ts +3 -0
- package/lib/skills/bundled.js +42 -0
- package/lib/skills/provider.d.ts +74 -0
- package/lib/skills/provider.js +346 -0
- package/lib/skills/row.d.ts +22 -0
- package/lib/skills/row.js +204 -0
- package/lib/workbench/adapter-coverage.d.ts +65 -0
- package/lib/workbench/adapter-coverage.js +141 -0
- package/lib/workbench/model-tools.d.ts +10 -0
- package/lib/workbench/model-tools.js +122 -0
- package/lib/workbench/overview.d.ts +153 -0
- package/lib/workbench/overview.js +246 -0
- package/lib/workbench/remote.d.ts +161 -0
- package/lib/workbench/remote.js +561 -0
- package/lib/workbench/row.d.ts +15 -0
- package/lib/workbench/row.js +16 -0
- package/lib/workbench/settings.d.ts +19 -0
- package/lib/workbench/settings.js +60 -0
- package/lib/workbench/workbench.d.ts +130 -0
- package/lib/workbench/workbench.js +361 -0
- package/locale/agents/en.json +6 -0
- package/locale/agents/zh.json +6 -0
- package/locale/commands/en.json +6 -0
- package/locale/commands/zh.json +6 -0
- package/locale/context/en.json +6 -0
- package/locale/context/zh.json +6 -0
- package/locale/en.json +6 -0
- package/locale/hooks/en.json +6 -0
- package/locale/hooks/zh.json +6 -0
- package/locale/mcp/en.json +6 -0
- package/locale/mcp/zh.json +6 -0
- package/locale/mods/en.json +6 -0
- package/locale/mods/zh.json +6 -0
- package/locale/opencode/en.json +6 -0
- package/locale/opencode/zh.json +6 -0
- package/locale/porter/en.json +6 -0
- package/locale/porter/zh.json +6 -0
- package/locale/sessions/en.json +6 -0
- package/locale/sessions/zh.json +6 -0
- package/locale/skills/en.json +6 -0
- package/locale/skills/zh.json +6 -0
- package/locale/workbench/en.json +6 -0
- package/locale/workbench/zh.json +6 -0
- package/locale/zh.json +7 -0
- package/package.json +247 -3
- package/runtime/opencode-host.mjs +270 -0
- package/skills/harness-forge/SKILL.md +47 -0
|
@@ -0,0 +1,602 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A handler's `if` field: one permission rule (`Bash(git *)`, `Edit(src/**)`,
|
|
3
|
+
* `WebFetch(domain:example.com)`, `mcp__server__tool`, a bare tool name)
|
|
4
|
+
* matched against the tool name and its input together.
|
|
5
|
+
*
|
|
6
|
+
* - Shell rules match each subcommand (after stripping `VAR=value`), commands
|
|
7
|
+
* inside `$(…)` and backticks; when the input cannot be read, or a
|
|
8
|
+
* subcommand expands something at run time where it could still become
|
|
9
|
+
* what the rule names, the hook runs.
|
|
10
|
+
* - File rules follow gitignore semantics, anchored as Claude Code anchors
|
|
11
|
+
* them (see `pathRuleMatches`): a pattern without `/` matches the file name
|
|
12
|
+
* at any depth under the working directory, `./x` only `<cwd>/x`, `src/**` only under `<cwd>/src`
|
|
13
|
+
* in an allow rule, `//abs` an absolute path, `~/x` the home directory and
|
|
14
|
+
* `/x` the directory of the settings source.
|
|
15
|
+
* `Edit` rules cover every file-writing tool and `Read` rules every reader.
|
|
16
|
+
* @module @kirkchinese/harness-forge
|
|
17
|
+
*/
|
|
18
|
+
import { lstatSync, readlinkSync, realpathSync } from 'node:fs';
|
|
19
|
+
import { homedir } from 'node:os';
|
|
20
|
+
import { isAbsolute, posix, resolve, win32 } from 'node:path';
|
|
21
|
+
import { linksReachNewShare, reachesNewShare, SYMLINK_HOPS } from '@kirkchinese/harness-forge-core';
|
|
22
|
+
import { Glob, wildcard } from './glob.js';
|
|
23
|
+
import { powershellCommand } from './powershell-aliases.js';
|
|
24
|
+
import { shellCommands } from './shell-commands.js';
|
|
25
|
+
import { isShellTool } from './vocabulary.js';
|
|
26
|
+
const WRITE_TOOLS = new Set(['Edit', 'Write', 'MultiEdit', 'NotebookEdit', 'apply_patch']);
|
|
27
|
+
const READ_TOOLS = new Set(['Read', 'Glob', 'Grep', 'NotebookRead']);
|
|
28
|
+
/** Whether a call is a file call that `Read(...)`/`Edit(...)` rules match by path. */
|
|
29
|
+
export function isFileCall(tool) {
|
|
30
|
+
const path = tool.input['file_path'] ?? tool.input['path'];
|
|
31
|
+
return typeof path === 'string' && tool.aliases.some(alias => WRITE_TOOLS.has(alias) || READ_TOOLS.has(alias));
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Whether `rule` selects this tool call.
|
|
35
|
+
* @param cwd - the session's working directory, for relative file rules.
|
|
36
|
+
* @param unknown - the answer when a wildcard or what it is matched against is
|
|
37
|
+
* too large to match within budget: true (the default) for a hook's `if` (the
|
|
38
|
+
* hook runs) and for deny and ask rules, false for allow rules.
|
|
39
|
+
* @param use - how a permission rule is used, for file rules (see `RuleUse`).
|
|
40
|
+
*/
|
|
41
|
+
export function conditionMatches(rule, tool, cwd, unknown = true, use = {}) {
|
|
42
|
+
const match = /^\s*([A-Za-z0-9_\-:.*]+?)\s*(?:\((.*)\))?\s*$/su.exec(rule);
|
|
43
|
+
if (match === null)
|
|
44
|
+
return false;
|
|
45
|
+
const name = match[1] ?? '';
|
|
46
|
+
const specifier = match[2];
|
|
47
|
+
if (name.startsWith('mcp__'))
|
|
48
|
+
return mcpRuleMatches(name, specifier, tool.name);
|
|
49
|
+
if (!ruleNamesTool(name, tool))
|
|
50
|
+
return false;
|
|
51
|
+
if (specifier === undefined || specifier === '' || specifier === '*')
|
|
52
|
+
return true;
|
|
53
|
+
if (isShellTool(tool.dshName))
|
|
54
|
+
return shellRuleMatches(specifier, String(tool.input['command'] ?? ''), unknown, shellOf(tool));
|
|
55
|
+
if (name === 'WebFetch' && specifier.startsWith('domain:'))
|
|
56
|
+
return domainMatches(specifier.slice('domain:'.length), String(tool.input['url'] ?? ''), unknown);
|
|
57
|
+
const path = tool.input['file_path'] ?? tool.input['path'];
|
|
58
|
+
if (typeof path === 'string' && (WRITE_TOOLS.has(name) || READ_TOOLS.has(name)))
|
|
59
|
+
return fileRuleMatches(specifier, path, cwd, unknown, use);
|
|
60
|
+
const subject = tool.input['subagent_type'] ?? tool.input['skill'] ?? tool.input['query'] ?? tool.input['url'] ?? tool.input['pattern'] ?? '';
|
|
61
|
+
return glob(specifier, true).match(String(subject)) ?? unknown;
|
|
62
|
+
}
|
|
63
|
+
function ruleNamesTool(name, tool) {
|
|
64
|
+
if (tool.aliases.includes(name))
|
|
65
|
+
return true;
|
|
66
|
+
if (name === 'Edit')
|
|
67
|
+
return tool.aliases.some(alias => WRITE_TOOLS.has(alias));
|
|
68
|
+
if (name === 'Read')
|
|
69
|
+
return tool.aliases.some(alias => READ_TOOLS.has(alias));
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
function mcpRuleMatches(name, specifier, toolName) {
|
|
73
|
+
if (specifier !== undefined && specifier !== '' && specifier !== '*')
|
|
74
|
+
return false;
|
|
75
|
+
if (name === toolName)
|
|
76
|
+
return true;
|
|
77
|
+
const server = name.endsWith('__*') ? name.slice(0, -1) : `${name}__`;
|
|
78
|
+
return name.split('__').length === 2 || name.endsWith('__*') ? toolName.startsWith(server) : false;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Shell rule: any subcommand matches; unreadable input runs the hook. The rule
|
|
82
|
+
* and each subcommand are compared as the words the shell reads (quotes and
|
|
83
|
+
* escapes removed, blanks collapsed). A subcommand with a word the shell
|
|
84
|
+
* would expand, or whose syntax the word reader does not read, matches with
|
|
85
|
+
* `unknown` when its words before that one agree with the rule's words.
|
|
86
|
+
* Only the argv is compared: redirections (`> out`, `2>&1`, `<<< x`) are set
|
|
87
|
+
* apart on both sides, so `git status > out` is `git status` to a rule. What a
|
|
88
|
+
* redirection reads or writes is checked against file rules instead (see
|
|
89
|
+
* `redirectFiles` and `ruleVerdict`).
|
|
90
|
+
*/
|
|
91
|
+
function shellRuleMatches(specifier, command, unknown, shell) {
|
|
92
|
+
const parsed = shellCommands(command, shell);
|
|
93
|
+
if (parsed === undefined)
|
|
94
|
+
return true;
|
|
95
|
+
// PowerShell: the command word on both sides as its cmdlet, through every alias for a restricting answer and
|
|
96
|
+
// only this platform's for an approving one (see `powershellCommand`).
|
|
97
|
+
const canonical = (word) => shell === 'powershell' ? powershellCommand(word, unknown ? 'all' : 'here') : word;
|
|
98
|
+
const rule = shellRule(specifier, shell, canonical);
|
|
99
|
+
if (rule === undefined)
|
|
100
|
+
return unknown;
|
|
101
|
+
return parsed.commands.some((sub) => {
|
|
102
|
+
const read = shellWords(sub, false, shell)?.words;
|
|
103
|
+
if (read === undefined)
|
|
104
|
+
return unknown;
|
|
105
|
+
const words = read.map((word, index) => index === 0 && !word.dynamic ? { ...word, text: canonical(word.text) } : word);
|
|
106
|
+
const dynamicAt = words.findIndex(word => word.dynamic);
|
|
107
|
+
if (dynamicAt === -1)
|
|
108
|
+
return rule.pattern.match(words.map(word => word.text).join(WORD_BREAK)) ?? unknown;
|
|
109
|
+
return unknown && couldExpandInto(rule, words, dynamicAt);
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
/** What stands between two words when a rule is matched against a command's words: no word holds it (one that does is dynamic). */
|
|
113
|
+
const WORD_BREAK = '\u0000';
|
|
114
|
+
/**
|
|
115
|
+
* `git *` matches `git` and `git <anything>` when that `*` is the rule's only
|
|
116
|
+
* wildcard; `prefix:*` is the legacy prefix form; any other `*` stands for
|
|
117
|
+
* any text, across words. Undefined when the rule's quotes do not close.
|
|
118
|
+
*/
|
|
119
|
+
function shellRule(specifier, shell, canonical) {
|
|
120
|
+
const trimmed = specifier.trim();
|
|
121
|
+
if (Glob.tooLong(trimmed))
|
|
122
|
+
return { words: [], prefix: false, pattern: Glob.refused() };
|
|
123
|
+
const prefix = trimmed.endsWith(':*');
|
|
124
|
+
const read = shellWords(prefix ? trimmed.slice(0, -2) : trimmed, true, shell);
|
|
125
|
+
if (read === undefined)
|
|
126
|
+
return undefined;
|
|
127
|
+
// The command word, unless a wildcard stands in it, read as the command it names.
|
|
128
|
+
const words = read.words.map((word, index) => index === 0 && !/[*?]/u.test(word.text) ? canonical(word.text) : word.text);
|
|
129
|
+
const wildcards = words.filter(word => /[*?]/u.test(word)).length;
|
|
130
|
+
const trailingStar = !prefix && words.at(-1) === '*' && wildcards === 1;
|
|
131
|
+
const pattern = new Glob();
|
|
132
|
+
(trailingStar ? words.slice(0, -1) : words).forEach((word, index) => {
|
|
133
|
+
if (index > 0)
|
|
134
|
+
pattern.literal(WORD_BREAK);
|
|
135
|
+
for (const char of word) {
|
|
136
|
+
if (char === '*')
|
|
137
|
+
pattern.many();
|
|
138
|
+
else if (char === '?')
|
|
139
|
+
pattern.one();
|
|
140
|
+
else
|
|
141
|
+
pattern.literal(char);
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
if (prefix || trailingStar)
|
|
145
|
+
pattern.optional(rest => rest.literal(WORD_BREAK).many());
|
|
146
|
+
return { words, prefix, pattern };
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Whether a command with a run-time expansion could become one the rule
|
|
150
|
+
* names: its static words agree with the rule's, position by position, up to
|
|
151
|
+
* the first expanding word or the first rule word with a wildcard. The
|
|
152
|
+
* expansion may be empty, so `cat secret.txt $X` could be `cat secret.txt`.
|
|
153
|
+
*/
|
|
154
|
+
function couldExpandInto(rule, words, dynamicAt) {
|
|
155
|
+
for (let index = 0; index < dynamicAt; index++) {
|
|
156
|
+
const ruleWord = rule.words[index];
|
|
157
|
+
if (ruleWord === undefined)
|
|
158
|
+
return rule.prefix;
|
|
159
|
+
if (/[*?]/u.test(ruleWord))
|
|
160
|
+
return true;
|
|
161
|
+
if (words[index]?.text !== ruleWord)
|
|
162
|
+
return false;
|
|
163
|
+
}
|
|
164
|
+
return true;
|
|
165
|
+
}
|
|
166
|
+
/** The null device of bash (also Git Bash, which maps it on Windows), and of PowerShell where it is a device (not on Windows). */
|
|
167
|
+
const DEV_NULL = '/dev/null';
|
|
168
|
+
/** PowerShell's null target (https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_redirection). */
|
|
169
|
+
const POWERSHELL_NULL = /^\$null$/iu;
|
|
170
|
+
/** Windows' null device, reserved in every folder whichever shell (bash, PowerShell, cmd) names it. */
|
|
171
|
+
const WINDOWS_NUL = /^nul$/iu;
|
|
172
|
+
/** Whether a redirection target is the null device of `shell` on this platform: no file behind it. */
|
|
173
|
+
function nullDevice(target, shell) {
|
|
174
|
+
const windows = process.platform === 'win32';
|
|
175
|
+
if (windows && WINDOWS_NUL.test(target.text))
|
|
176
|
+
return true;
|
|
177
|
+
// Only the bare variable: quoted, `'$null'` names a file of that name.
|
|
178
|
+
if (shell === 'powershell')
|
|
179
|
+
return (!target.quoted && POWERSHELL_NULL.test(target.text)) || (!windows && target.text === DEV_NULL);
|
|
180
|
+
return target.text === DEV_NULL;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* The files the redirections of one simple command of `shell` read or write;
|
|
184
|
+
* undefined when its words cannot be read. Here-documents, here-strings,
|
|
185
|
+
* descriptor copies and closes (`2>&1`, `<&3`, `>&-`) and the shell's null
|
|
186
|
+
* device (see `nullDevice`) name no file.
|
|
187
|
+
*/
|
|
188
|
+
export function redirectFiles(command, shell) {
|
|
189
|
+
const read = shellWords(command, false, shell);
|
|
190
|
+
if (read === undefined)
|
|
191
|
+
return undefined;
|
|
192
|
+
return read.redirects.flatMap(({ operator, target }) => {
|
|
193
|
+
// Without its descriptor or stream (`2`, PowerShell's `*`).
|
|
194
|
+
const op = operator.replace(/^(?:\d+|\*)/u, '');
|
|
195
|
+
if (op.startsWith('<<') || nullDevice(target, shell))
|
|
196
|
+
return [];
|
|
197
|
+
if ((op === '<&' || op === '>&') && /^(?:\d+-?|-)$/u.test(target.text))
|
|
198
|
+
return [];
|
|
199
|
+
const reads = op === '<' || op === '<>' || op === '<&';
|
|
200
|
+
const writes = op !== '<' && op !== '<&';
|
|
201
|
+
return [{ path: target.text, reads, writes, plain: !target.dynamic && !target.text.startsWith('~') && !/[*?[]/u.test(target.text) }];
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* A redirection operator of bash (https://www.gnu.org/software/bash/manual/bash.html#Redirections),
|
|
206
|
+
* longest first: `&>>`/`&>` (stdout and stderr), here-strings `<<<`, here-documents `<<-`/`<<`,
|
|
207
|
+
* `<>` (read and write), descriptor copies `<&`/`>&`, `>>`, `>|` (despite noclobber), `<` and `>`.
|
|
208
|
+
*/
|
|
209
|
+
const REDIRECT_OPERATOR = /&>>|&>|<<<|<<-|<<|<>|<&|>>|>\||>&|<|>/uy;
|
|
210
|
+
/**
|
|
211
|
+
* PowerShell's redirection operators (https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_redirection),
|
|
212
|
+
* after an optional stream (see {@link POWERSHELL_STREAM}): `>>`, `>&` (a merge, `2>&1`), `>`, and `<`, which
|
|
213
|
+
* PowerShell 7 reserves and refuses to run; read as an input redirection, it can only make a rule match more.
|
|
214
|
+
*/
|
|
215
|
+
const POWERSHELL_REDIRECT_OPERATOR = />>|>&|>|</uy;
|
|
216
|
+
/** The streams a PowerShell redirection names: success (1) to information (6), or `*` for all. */
|
|
217
|
+
const POWERSHELL_STREAM = /^(?:[1-6]|\*)$/u;
|
|
218
|
+
/** A bash redirection's descriptor number. */
|
|
219
|
+
const BASH_DESCRIPTOR = /^\d+$/u;
|
|
220
|
+
/** The shell that reads a shell call's command: PowerShell for DSH's `pwsh` tool, bash otherwise. */
|
|
221
|
+
export function shellOf(tool) {
|
|
222
|
+
return tool.dshName === 'pwsh' ? 'powershell' : 'bash';
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* The words of a command or a rule, in one pass over the text: blanks
|
|
226
|
+
* separate words; single quotes, double quotes and backslashes quote and are
|
|
227
|
+
* removed. In a command (not a `rule`) an unquoted `#` at a word's start
|
|
228
|
+
* begins a comment, and a word is dynamic when it holds `$` or a backtick,
|
|
229
|
+
* unquoted glob, brace or parenthesis characters, or {@link WORD_BREAK}.
|
|
230
|
+
* An unquoted redirection, with the descriptor number before it and the
|
|
231
|
+
* word after it, is set apart in `redirects` (a process substitution `<(…)`
|
|
232
|
+
* stays a dynamic word). Undefined when a quote does not close or a
|
|
233
|
+
* redirection has no word.
|
|
234
|
+
*/
|
|
235
|
+
function shellWords(text, rule, shell) {
|
|
236
|
+
const operators = shell === 'powershell' ? POWERSHELL_REDIRECT_OPERATOR : REDIRECT_OPERATOR;
|
|
237
|
+
const descriptors = shell === 'powershell' ? POWERSHELL_STREAM : BASH_DESCRIPTOR;
|
|
238
|
+
// PowerShell matches without case (https://code.claude.com/docs/en/permissions#powershell).
|
|
239
|
+
const fold = (word) => shell === 'powershell' ? word.toLowerCase() : word;
|
|
240
|
+
const words = [];
|
|
241
|
+
const redirects = [];
|
|
242
|
+
let current = '';
|
|
243
|
+
let started = false;
|
|
244
|
+
let dynamic = false;
|
|
245
|
+
// The current word was written with a quote or an escape: it cannot be a descriptor number.
|
|
246
|
+
let quoted = false;
|
|
247
|
+
// The operator whose target the next word is.
|
|
248
|
+
let pending;
|
|
249
|
+
const end = () => {
|
|
250
|
+
if (started) {
|
|
251
|
+
const word = { text: current, dynamic: dynamic || current.includes(WORD_BREAK), quoted };
|
|
252
|
+
if (pending !== undefined)
|
|
253
|
+
redirects.push({ operator: pending, target: word });
|
|
254
|
+
else
|
|
255
|
+
words.push({ ...word, text: fold(word.text) });
|
|
256
|
+
pending = undefined;
|
|
257
|
+
}
|
|
258
|
+
current = '';
|
|
259
|
+
started = false;
|
|
260
|
+
dynamic = false;
|
|
261
|
+
quoted = false;
|
|
262
|
+
};
|
|
263
|
+
for (let index = 0; index < text.length; index++) {
|
|
264
|
+
const char = text[index] ?? '';
|
|
265
|
+
if (char === ' ' || char === '\t' || char === '\n' || char === '\r') {
|
|
266
|
+
end();
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
if (char === '#' && !started && !rule)
|
|
270
|
+
break;
|
|
271
|
+
operators.lastIndex = index;
|
|
272
|
+
const opens = char === '<' || char === '>' || (shell === 'bash' && char === '&');
|
|
273
|
+
const operator = opens && text[index + 1] !== '(' ? operators.exec(text)?.[0] : undefined;
|
|
274
|
+
if (operator !== undefined && pending === undefined) {
|
|
275
|
+
// `2>` (PowerShell also `*>`) names a descriptor; any other word before the operator is a word of its own.
|
|
276
|
+
const descriptor = started && !quoted && descriptors.test(current) ? current : '';
|
|
277
|
+
if (descriptor !== '') {
|
|
278
|
+
// The descriptor is no word: what it set (PowerShell's `*` reads as a glob) goes with it.
|
|
279
|
+
current = '';
|
|
280
|
+
started = false;
|
|
281
|
+
dynamic = false;
|
|
282
|
+
}
|
|
283
|
+
else
|
|
284
|
+
end();
|
|
285
|
+
pending = descriptor + operator;
|
|
286
|
+
index += operator.length - 1;
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
started = true;
|
|
290
|
+
if (char === '\\') {
|
|
291
|
+
quoted = true;
|
|
292
|
+
const next = text[index + 1];
|
|
293
|
+
if (next === undefined)
|
|
294
|
+
dynamic = true;
|
|
295
|
+
// A backslash-newline joins two lines and stands for nothing.
|
|
296
|
+
else if (next !== '\n')
|
|
297
|
+
current += next;
|
|
298
|
+
index++;
|
|
299
|
+
}
|
|
300
|
+
else if (char === "'") {
|
|
301
|
+
quoted = true;
|
|
302
|
+
const close = text.indexOf("'", index + 1);
|
|
303
|
+
if (close === -1)
|
|
304
|
+
return undefined;
|
|
305
|
+
current += text.slice(index + 1, close);
|
|
306
|
+
index = close;
|
|
307
|
+
}
|
|
308
|
+
else if (char === '"') {
|
|
309
|
+
quoted = true;
|
|
310
|
+
let close = index + 1;
|
|
311
|
+
for (; close < text.length && text[close] !== '"'; close++) {
|
|
312
|
+
const inner = text[close] ?? '';
|
|
313
|
+
const next = text[close + 1] ?? '';
|
|
314
|
+
if (inner === '\\' && (next === '$' || next === '`' || next === '"' || next === '\\' || next === '\n')) {
|
|
315
|
+
if (next !== '\n')
|
|
316
|
+
current += next;
|
|
317
|
+
close++;
|
|
318
|
+
}
|
|
319
|
+
else {
|
|
320
|
+
if (!rule && (inner === '$' || inner === '`'))
|
|
321
|
+
dynamic = true;
|
|
322
|
+
current += inner;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
if (close >= text.length)
|
|
326
|
+
return undefined;
|
|
327
|
+
index = close;
|
|
328
|
+
}
|
|
329
|
+
else {
|
|
330
|
+
if (!rule && '$`*?[{()'.includes(char))
|
|
331
|
+
dynamic = true;
|
|
332
|
+
current += char;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
end();
|
|
336
|
+
return pending === undefined ? { words, redirects } : undefined;
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* `domain:example.com` is that host only, `*.example.com` any subdomain at
|
|
340
|
+
* any depth but not the domain itself, `*` every host; any other `*` stays
|
|
341
|
+
* between two dots. Case and a trailing dot are ignored.
|
|
342
|
+
*/
|
|
343
|
+
function domainMatches(domain, url, unknown) {
|
|
344
|
+
try {
|
|
345
|
+
const host = new URL(url).hostname.toLowerCase().replace(/\.$/u, '');
|
|
346
|
+
const wanted = domain.trim().toLowerCase().replace(/\.$/u, '');
|
|
347
|
+
if (wanted === '*')
|
|
348
|
+
return true;
|
|
349
|
+
if (wanted.startsWith('*.'))
|
|
350
|
+
return host.endsWith(wanted.slice(1));
|
|
351
|
+
return wildcard(wanted, char => char !== '.').match(host) ?? unknown;
|
|
352
|
+
}
|
|
353
|
+
catch {
|
|
354
|
+
return false;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* Whether a gitignore-style pattern, relative to the working directory,
|
|
359
|
+
* matches a path as spelled (skill `paths`): no link is followed.
|
|
360
|
+
*/
|
|
361
|
+
export function pathPatternMatches(pattern, path, cwd) {
|
|
362
|
+
return pathRuleMatches(pattern, path, cwd, false, {}, spelled);
|
|
363
|
+
}
|
|
364
|
+
/** A path as spelled. */
|
|
365
|
+
const spelled = (path) => path;
|
|
366
|
+
const SYSTEM = {
|
|
367
|
+
get platform() { return process.platform; },
|
|
368
|
+
lstat: lstatSync,
|
|
369
|
+
readlink: readlinkSync,
|
|
370
|
+
realpath: path => realpathSync.native(path),
|
|
371
|
+
};
|
|
372
|
+
/**
|
|
373
|
+
* The real path of an absolute path, or undefined when it cannot be known (a
|
|
374
|
+
* link loop, more than {@link SYMLINK_HOPS} links, a link or folder that
|
|
375
|
+
* cannot be read, or, on Windows, a path or link that leads to a network share
|
|
376
|
+
* or device `base` is not on). What exists is resolved by the system; a part
|
|
377
|
+
* that does not exist yet is followed link by link (`readlink`), so a dangling
|
|
378
|
+
* link leads where a write through it would create its file. The one resolver
|
|
379
|
+
* file rules and redirection targets use; nothing else follows links.
|
|
380
|
+
*
|
|
381
|
+
* On Windows, opening, resolving or reading a link on `\\server\share` makes
|
|
382
|
+
* the system connect to that server and sign in with the person's credentials,
|
|
383
|
+
* and `realpath` follows links by itself. So before any call that could follow
|
|
384
|
+
* one, `linksReachNewShare` walks the path with `lstat` and `readlink` only;
|
|
385
|
+
* realpath runs only for a path that stays local (it is what expands a short
|
|
386
|
+
* name such as `RUNNER~1`).
|
|
387
|
+
* @param base - the working directory: a share it is on is not new.
|
|
388
|
+
*/
|
|
389
|
+
export function realPathOf(path, base, system = SYSTEM) {
|
|
390
|
+
return resolveReal(path, base, system, { hops: 0 });
|
|
391
|
+
}
|
|
392
|
+
function resolveReal(path, base, system, followed) {
|
|
393
|
+
if (linksReachNewShare(path, base, system.platform, system))
|
|
394
|
+
return undefined;
|
|
395
|
+
const paths = system.platform === 'win32' ? win32 : posix;
|
|
396
|
+
try {
|
|
397
|
+
return system.realpath(path);
|
|
398
|
+
}
|
|
399
|
+
catch (error) {
|
|
400
|
+
// Only a missing part is followed by hand; a loop or an unreadable folder stays unknown.
|
|
401
|
+
if (error.code !== 'ENOENT')
|
|
402
|
+
return undefined;
|
|
403
|
+
}
|
|
404
|
+
const parent = paths.dirname(path);
|
|
405
|
+
if (parent === path)
|
|
406
|
+
return undefined;
|
|
407
|
+
const folder = resolveReal(parent, base, system, followed);
|
|
408
|
+
if (folder === undefined)
|
|
409
|
+
return undefined;
|
|
410
|
+
const candidate = paths.join(folder, paths.basename(path));
|
|
411
|
+
try {
|
|
412
|
+
if (!system.lstat(candidate).isSymbolicLink())
|
|
413
|
+
return undefined;
|
|
414
|
+
}
|
|
415
|
+
catch (error) {
|
|
416
|
+
// Nothing there: a write creates the file here.
|
|
417
|
+
return error.code === 'ENOENT' ? candidate : undefined;
|
|
418
|
+
}
|
|
419
|
+
if (++followed.hops > SYMLINK_HOPS)
|
|
420
|
+
return undefined;
|
|
421
|
+
let target;
|
|
422
|
+
try {
|
|
423
|
+
target = system.readlink(candidate);
|
|
424
|
+
}
|
|
425
|
+
catch {
|
|
426
|
+
return undefined;
|
|
427
|
+
}
|
|
428
|
+
return resolveReal(paths.resolve(folder, target), base, system, followed);
|
|
429
|
+
}
|
|
430
|
+
/**
|
|
431
|
+
* A `Read(...)`/`Edit(...)` rule, checked on the path as spelled and on its
|
|
432
|
+
* real path (anchors resolved the same way), or on the one `use.path` names:
|
|
433
|
+
* a restricting answer (`unknown` true: deny, ask, a hook's `if`) matches
|
|
434
|
+
* when either does, so a link cannot hide a denied file; an approving one
|
|
435
|
+
* (allow) only when both do, so a link inside an allowed folder cannot reach
|
|
436
|
+
* a file outside it (`ruleVerdict` lets two allow rules cover the two paths).
|
|
437
|
+
*/
|
|
438
|
+
function fileRuleMatches(specifier, path, cwd, unknown, use) {
|
|
439
|
+
const real = (absolute) => realPathOf(absolute, cwd);
|
|
440
|
+
if (use.path !== undefined)
|
|
441
|
+
return pathRuleMatches(specifier, path, cwd, unknown, use, use.path === 'real' ? real : spelled);
|
|
442
|
+
const asSpelled = pathRuleMatches(specifier, path, cwd, unknown, use, spelled);
|
|
443
|
+
if (asSpelled === unknown)
|
|
444
|
+
return asSpelled;
|
|
445
|
+
// A path whose real form cannot be known matches a restricting rule and no approving one.
|
|
446
|
+
return pathRuleMatches(specifier, path, cwd, unknown, use, real);
|
|
447
|
+
}
|
|
448
|
+
/**
|
|
449
|
+
* A file rule, anchored as Claude Code anchors it
|
|
450
|
+
* (https://code.claude.com/docs/en/permissions#how-path-patterns-match):
|
|
451
|
+
* `//path` at the filesystem root, `~/path` at the home directory, `/path` at
|
|
452
|
+
* `root` (the directory of the settings source; the working directory when
|
|
453
|
+
* there is none), `path` and `./path` at the working directory. A rule
|
|
454
|
+
* matches only files under its anchor. The docs apply gitignore semantics
|
|
455
|
+
* (https://code.claude.com/docs/en/permissions#how-deep-a-pattern-matches):
|
|
456
|
+
* a bare pattern without a `/` ("bare filenames ... match at any depth")
|
|
457
|
+
* matches at any depth below its anchor, and so does a bare single-directory
|
|
458
|
+
* `dir/**` in a deny or ask rule (`restricts`); any other slash anchors, the
|
|
459
|
+
* leading one of `./x` too (https://git-scm.com/docs/gitignore#_pattern_format:
|
|
460
|
+
* a separator at the beginning or middle makes the pattern relative to its
|
|
461
|
+
* base), so `./x` is `<cwd>/x` only. In a deny or ask rule a pattern that
|
|
462
|
+
* matches a directory also matches everything in it, as in gitignore.
|
|
463
|
+
*/
|
|
464
|
+
function pathRuleMatches(specifier, path, cwd, unknown, { root, restricts = false }, locate) {
|
|
465
|
+
const located = locate(isAbsolute(path) ? resolve(path) : resolve(cwd, path));
|
|
466
|
+
// Where a path or an anchor cannot be known, the answer is `unknown`: a restricting rule matches, an approving one does not.
|
|
467
|
+
if (located === undefined)
|
|
468
|
+
return unknown;
|
|
469
|
+
const target = rulePath(located);
|
|
470
|
+
let unresolved = false;
|
|
471
|
+
const anchor = (directory) => {
|
|
472
|
+
const real = locate(resolve(directory));
|
|
473
|
+
if (real === undefined)
|
|
474
|
+
unresolved = true;
|
|
475
|
+
return rulePath(real ?? directory);
|
|
476
|
+
};
|
|
477
|
+
let pattern = specifier.trim();
|
|
478
|
+
let base;
|
|
479
|
+
// A bare relative pattern (no leading `./`, `/`, `~/` or `//`): the only form that may match below its anchor.
|
|
480
|
+
let bare = false;
|
|
481
|
+
if (pattern.startsWith('//')) {
|
|
482
|
+
base = '/';
|
|
483
|
+
pattern = pattern.slice(2);
|
|
484
|
+
}
|
|
485
|
+
else if (pattern.startsWith('~/')) {
|
|
486
|
+
base = anchor(homedir());
|
|
487
|
+
pattern = pattern.slice(2);
|
|
488
|
+
}
|
|
489
|
+
else if (pattern.startsWith('/')) {
|
|
490
|
+
base = anchor(root ?? cwd);
|
|
491
|
+
pattern = pattern.slice(1);
|
|
492
|
+
}
|
|
493
|
+
else if (pattern.startsWith('./')) {
|
|
494
|
+
// `./x` contains a slash, so gitignore anchors it: `<cwd>/x` only, never `<cwd>/sub/x`.
|
|
495
|
+
base = anchor(cwd);
|
|
496
|
+
pattern = pattern.slice(2);
|
|
497
|
+
}
|
|
498
|
+
else {
|
|
499
|
+
base = anchor(cwd);
|
|
500
|
+
bare = true;
|
|
501
|
+
}
|
|
502
|
+
if (process.platform === 'win32')
|
|
503
|
+
pattern = pattern.toLowerCase();
|
|
504
|
+
pattern = pattern.replace(/\/+$/u, '');
|
|
505
|
+
// An anchored rule's literal folders (before the last part or the first wildcard) are resolved as the path is, so
|
|
506
|
+
// `//etc/hosts` also covers macOS's `/private/etc/hosts`, and a Windows short name its long one.
|
|
507
|
+
if (!bare && locate !== spelled) {
|
|
508
|
+
const parts = pattern.split('/');
|
|
509
|
+
const literal = parts.findIndex(part => /[*?[]/u.test(part));
|
|
510
|
+
const folders = Math.min(literal === -1 ? parts.length - 1 : literal, parts.length - 1);
|
|
511
|
+
const prefix = nativePath(`${base.endsWith('/') ? base : `${base}/`}${parts.slice(0, folders).join('/')}`);
|
|
512
|
+
// A rule's folders on a share stay as written: nothing local resolves onto one (such a path is unknown), and
|
|
513
|
+
// resolving them would connect to it.
|
|
514
|
+
if (folders > 0 && !reachesNewShare(prefix, cwd)) {
|
|
515
|
+
const real = locate(prefix);
|
|
516
|
+
if (real === undefined)
|
|
517
|
+
unresolved = true;
|
|
518
|
+
else {
|
|
519
|
+
base = rulePath(real);
|
|
520
|
+
pattern = parts.slice(folders).join('/');
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
if (unresolved)
|
|
525
|
+
return unknown;
|
|
526
|
+
const rel = below(base, target);
|
|
527
|
+
if (rel === undefined)
|
|
528
|
+
return false;
|
|
529
|
+
const anyDepth = bare && (!pattern.includes('/') || (restricts && /^[^/]+\/\*\*$/u.test(pattern)));
|
|
530
|
+
const compiled = glob(anyDepth ? `**/${pattern}` : pattern, false);
|
|
531
|
+
// The path and, for a deny or ask rule, each directory above it.
|
|
532
|
+
const candidates = restricts ? [...[...rel.matchAll(/\//gu)].map(slash => rel.slice(0, slash.index)), rel] : [rel];
|
|
533
|
+
return candidates.some(candidate => compiled.match(candidate) ?? unknown);
|
|
534
|
+
}
|
|
535
|
+
/** A path in the form rules match (on Windows see `windowsRulePath`). */
|
|
536
|
+
function rulePath(path) {
|
|
537
|
+
return process.platform === 'win32' ? windowsRulePath(path) : path;
|
|
538
|
+
}
|
|
539
|
+
/** A path in rule form back to one the system resolves (on Windows see `windowsNativePath`). */
|
|
540
|
+
function nativePath(path) {
|
|
541
|
+
return process.platform === 'win32' ? windowsNativePath(path) : path;
|
|
542
|
+
}
|
|
543
|
+
/** Windows' long-path prefixes: `\\?\UNC\server\share` and `\\?\C:\…`. */
|
|
544
|
+
const WINDOWS_LONG_UNC = /^\\\\\?\\UNC\\/iu;
|
|
545
|
+
const WINDOWS_LONG = /^\\\\\?\\/u;
|
|
546
|
+
/**
|
|
547
|
+
* A Windows path in the form rules match, as Claude Code writes them
|
|
548
|
+
* (https://code.claude.com/docs/en/permissions#where-a-pattern-anchors: "`C:\Users\alice` becomes `/c/Users/alice`"):
|
|
549
|
+
* either separator becomes `/`, the long-path prefix goes, a drive `C:` becomes `/c` and a share `\\server` stays
|
|
550
|
+
* `//server`. The whole path is compared without case, as Windows' file systems compare names by default, so the
|
|
551
|
+
* rule, the working directory and every path, spelled or real, go through this one function.
|
|
552
|
+
*/
|
|
553
|
+
export function windowsRulePath(path) {
|
|
554
|
+
return path.replace(WINDOWS_LONG_UNC, '\\\\').replace(WINDOWS_LONG, '').replace(/\\/gu, '/').toLowerCase().replace(/^([a-z]):/u, '/$1');
|
|
555
|
+
}
|
|
556
|
+
/** A rule-form Windows path (`/c/users/x`, `//server/share/x`) as Windows names it (`c:\users\x`). */
|
|
557
|
+
export function windowsNativePath(path) {
|
|
558
|
+
const drive = /^\/([a-z])(?:\/(.*))?$/u.exec(path);
|
|
559
|
+
if (drive !== null)
|
|
560
|
+
return `${drive[1] ?? ''}:\\${(drive[2] ?? '').replace(/\//gu, '\\')}`;
|
|
561
|
+
return path.replace(/\//gu, '\\');
|
|
562
|
+
}
|
|
563
|
+
/** `target` relative to `base` (both from `rulePath`), or undefined when it lies outside. */
|
|
564
|
+
function below(base, target) {
|
|
565
|
+
if (target === base)
|
|
566
|
+
return '';
|
|
567
|
+
const prefix = base.endsWith('/') ? base : `${base}/`;
|
|
568
|
+
return target.startsWith(prefix) ? target.slice(prefix.length) : undefined;
|
|
569
|
+
}
|
|
570
|
+
/** A glob. In shell rules `*` spans anything; in path rules it stays within a segment. */
|
|
571
|
+
function glob(pattern, shell) {
|
|
572
|
+
if (Glob.tooLong(pattern))
|
|
573
|
+
return Glob.refused();
|
|
574
|
+
const segment = (char) => shell || char !== '/';
|
|
575
|
+
const result = new Glob();
|
|
576
|
+
const chars = [...pattern];
|
|
577
|
+
for (let index = 0; index < chars.length; index++) {
|
|
578
|
+
const char = chars[index] ?? '';
|
|
579
|
+
if (char === '*') {
|
|
580
|
+
if (chars[index + 1] === '*') {
|
|
581
|
+
index++;
|
|
582
|
+
if (chars[index + 1] === '/') {
|
|
583
|
+
index++;
|
|
584
|
+
result.optional(dirs => dirs.many().literal('/'));
|
|
585
|
+
}
|
|
586
|
+
else {
|
|
587
|
+
result.many();
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
else {
|
|
591
|
+
result.many(segment);
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
else if (char === '?') {
|
|
595
|
+
result.one(segment);
|
|
596
|
+
}
|
|
597
|
+
else {
|
|
598
|
+
result.literal(char);
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
return result;
|
|
602
|
+
}
|