@loophubs/agent-guard 0.1.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 +91 -0
- package/bin/agent-guard +48 -0
- package/package.json +26 -0
- package/src/argv.ts +196 -0
- package/src/comments.ts +16 -0
- package/src/core.ts +44 -0
- package/src/cwd.ts +5 -0
- package/src/frontend.ts +240 -0
- package/src/guard.ts +41 -0
- package/src/links.ts +59 -0
- package/src/moves.ts +28 -0
- package/src/mvdan-sh.d.ts +92 -0
- package/src/paths.ts +149 -0
- package/src/pipeline.ts +26 -0
- package/src/reasons.ts +19 -0
- package/src/record.ts +51 -0
- package/src/rules/appdata.ts +82 -0
- package/src/rules/credentials.ts +193 -0
- package/src/rules/workflow.ts +38 -0
- package/src/search-roles.ts +76 -0
- package/src/words.ts +62 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 agent-guard contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# agent-guard
|
|
2
|
+
|
|
3
|
+
`agent-guard` is a macOS pre-tool hook for Claude Code, Codex, and Pi that blocks risky filesystem scans and credential reads. It helps avoid agent commands that traverse `~/Library` and trigger macOS App Data prompts recorded as `SystemPolicyAppDataDetailed`.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
Install globally with npm or Bun (Bun 1.4 or newer must be available at runtime):
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install --global @loophubs/agent-guard
|
|
11
|
+
# or
|
|
12
|
+
bun add --global @loophubs/agent-guard
|
|
13
|
+
command -v agent-guard
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Use the absolute path printed by the last command in the examples below.
|
|
17
|
+
|
|
18
|
+
## Register a runtime
|
|
19
|
+
|
|
20
|
+
### Claude Code
|
|
21
|
+
|
|
22
|
+
Add this `PreToolUse` hook to `~/.claude/settings.json`, replacing the command path. It covers Claude Code's Bash and file-reading tools; [Claude Code's hook settings](https://code.claude.com/docs/en/hooks#hook-locations) describe the user-level location.
|
|
23
|
+
|
|
24
|
+
```json
|
|
25
|
+
{
|
|
26
|
+
"hooks": {
|
|
27
|
+
"PreToolUse": [{
|
|
28
|
+
"matcher": "Bash|Read|Edit|Write|Grep",
|
|
29
|
+
"hooks": [{ "type": "command", "command": "/absolute/path/to/agent-guard --runtime claude", "timeout": 5 }]
|
|
30
|
+
}]
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### Codex
|
|
36
|
+
|
|
37
|
+
Add this to `~/.codex/config.toml`, replacing the command path. [OpenAI's hook documentation](https://learn.chatgpt.com/docs/hooks) specifies `PreToolUse`, the Bash matcher, and `/hooks` trust review; open `/hooks` in Codex and trust the new hook before testing it.
|
|
38
|
+
|
|
39
|
+
```toml
|
|
40
|
+
[[hooks.PreToolUse]]
|
|
41
|
+
matcher = "^Bash$"
|
|
42
|
+
|
|
43
|
+
[[hooks.PreToolUse.hooks]]
|
|
44
|
+
type = "command"
|
|
45
|
+
command = "/absolute/path/to/agent-guard --runtime codex"
|
|
46
|
+
timeout = 5
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Pi
|
|
50
|
+
|
|
51
|
+
Create `~/.pi/agent/extensions/agent-guard.ts` with this adapter, replacing the command path. [Pi's extension API](https://github.com/earendil-works/pi/blob/main/packages/coding-agent/docs/extensions.md) loads files in that directory and lets `tool_call` block a tool before execution.
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
import { spawnSync } from "node:child_process";
|
|
55
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
56
|
+
|
|
57
|
+
const guard = "/absolute/path/to/agent-guard";
|
|
58
|
+
|
|
59
|
+
export default function (pi: ExtensionAPI) {
|
|
60
|
+
pi.on("tool_call", (event, ctx) => {
|
|
61
|
+
if (!["bash", "read", "edit", "write", "grep"].includes(event.toolName)) return;
|
|
62
|
+
const input = event.input as Record<string, unknown>;
|
|
63
|
+
const toolInput = event.toolName === "bash" ? input : {
|
|
64
|
+
...input,
|
|
65
|
+
file_path: input.path,
|
|
66
|
+
};
|
|
67
|
+
const result = spawnSync(guard, ["--runtime", "pi"], {
|
|
68
|
+
cwd: ctx.cwd,
|
|
69
|
+
input: JSON.stringify({ tool_name: event.toolName, tool_input: toolInput, cwd: ctx.cwd }),
|
|
70
|
+
encoding: "utf8",
|
|
71
|
+
timeout: 4500,
|
|
72
|
+
});
|
|
73
|
+
if (result.status === 0) return;
|
|
74
|
+
return { block: true, reason: result.stderr?.trim() || result.error?.message || "agent-guard failed" };
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Verify it blocks
|
|
80
|
+
|
|
81
|
+
From a project directory, ask the configured agent to run `du -sh ~/Library`. It should report the guard's denial **without running `du`**. You can check the package entry directly without reading `~/Library`:
|
|
82
|
+
|
|
83
|
+
```sh
|
|
84
|
+
printf '%s\n' '{"tool_name":"Bash","tool_input":{"command":"du -sh ~/Library"}}' | agent-guard --runtime claude
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
The direct check should exit 2 and print a `DENIED:` reason on stderr. A direct check confirms the package works; the agent check confirms that runtime loaded its hook.
|
|
88
|
+
|
|
89
|
+
## Limits
|
|
90
|
+
|
|
91
|
+
The guard checks supported tool calls and recognizable shell commands; it does not sandbox the agent or cover every custom tool, dynamic shell expansion, or process outside a registered hook. Codex's example covers Bash calls, while the Pi adapter covers Bash, read, edit, write, and grep; other Pi tools need their own adapter handling. A broken or timed-out package entry denies a checked call, but a hook disabled or skipped by its runtime cannot inspect that call.
|
package/bin/agent-guard
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
#!/bin/zsh -f
|
|
2
|
+
# PreToolUse entry for the agent guard:
|
|
3
|
+
# agent-guard --runtime claude|codex|pi < event.json
|
|
4
|
+
# Runs src/guard.ts from the package directory and passes its denial through
|
|
5
|
+
# with exit 2. Exit 0 only means no objection; it never approves a call. Every
|
|
6
|
+
# other outcome denies too, because Claude lets a hook's other failures and
|
|
7
|
+
# timeouts through, Codex is assumed to, and nothing else stands between an
|
|
8
|
+
# agent and ~/Library.
|
|
9
|
+
emulate zsh
|
|
10
|
+
setopt no_bg_nice
|
|
11
|
+
zmodload zsh/zselect
|
|
12
|
+
|
|
13
|
+
fail() {
|
|
14
|
+
print -u2 -- "The agent guard could not complete its check ($1), so this call is blocked. Inspect the guard installation, repair the reported failure, then retry the call."
|
|
15
|
+
exit 2
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
pkg=${0:A:h:h}
|
|
19
|
+
bun=$(command -v bun) || fail 'bun missing'
|
|
20
|
+
guard=$pkg/src/guard.ts
|
|
21
|
+
[[ -r $guard ]] || fail 'guard.ts missing'
|
|
22
|
+
event=$(/bin/cat)
|
|
23
|
+
# bun reads bunfig.toml, tsconfig.json and .env from its working directory,
|
|
24
|
+
# which is the agent's project, so a project file could preload code into the
|
|
25
|
+
# guard or remap its parser. The guard runs from the package directory, with
|
|
26
|
+
# the hook's directory passed as --cwd.
|
|
27
|
+
hook_cwd=$PWD
|
|
28
|
+
cd -q $pkg || fail 'package directory unreadable'
|
|
29
|
+
|
|
30
|
+
# The deadline sits below every runtime's hook timeout, so an overrun denies
|
|
31
|
+
# here instead of timing out as a pass. Reading the coprocess under the same
|
|
32
|
+
# deadline keeps anything the guard started from holding this hook open.
|
|
33
|
+
coproc { print -r -- "$event" | exec $bun $guard "$@" --cwd $hook_cwd 2>&1 }
|
|
34
|
+
pid=$!
|
|
35
|
+
{ zselect -t 300; kill -KILL $pid } >/dev/null 2>&1 &!
|
|
36
|
+
watchdog=$!
|
|
37
|
+
read -r -t 3 -p reason
|
|
38
|
+
wait $pid
|
|
39
|
+
rc=$?
|
|
40
|
+
kill -KILL $watchdog 2>/dev/null
|
|
41
|
+
case $rc in
|
|
42
|
+
(0) [[ -n $reason ]] && print -r -- "$reason"; exit 0 ;;
|
|
43
|
+
(2) ;;
|
|
44
|
+
(*) fail 'guard failed' ;;
|
|
45
|
+
esac
|
|
46
|
+
[[ -n $reason ]] || fail 'guard gave no reason'
|
|
47
|
+
print -u2 -r -- "$reason"
|
|
48
|
+
exit 2
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@loophubs/agent-guard",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Blocks filesystem scans and credential reads that macOS records as TCC App Data access, and points the agent at a safe alternative.",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"engines": {
|
|
8
|
+
"bun": ">=1.4"
|
|
9
|
+
},
|
|
10
|
+
"bin": {
|
|
11
|
+
"agent-guard": "bin/agent-guard"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"bin",
|
|
15
|
+
"src"
|
|
16
|
+
],
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"mvdan-sh": "0.10.1"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"typescript": "6.0.3"
|
|
22
|
+
},
|
|
23
|
+
"scripts": {
|
|
24
|
+
"test": "bunx tsc --noEmit --strict --target es2022 --module esnext --moduleResolution bundler --allowImportingTsExtensions tests/*.ts tests/fixtures/*.ts && bun test tests/"
|
|
25
|
+
}
|
|
26
|
+
}
|
package/src/argv.ts
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { basename, resolve } from "node:path";
|
|
2
|
+
import { expandHome } from "./paths.ts";
|
|
3
|
+
import type { Command, Word } from "./record.ts";
|
|
4
|
+
import { searchRoles } from "./search-roles.ts";
|
|
5
|
+
|
|
6
|
+
const shellPrograms = new Set(["sh", "bash", "zsh", "dash", "ksh"]);
|
|
7
|
+
const codePrograms = new Set(["python", "python3", "node", "bun", "deno", "ruby", "perl", "php", "osascript", "lua"]);
|
|
8
|
+
const zshBuiltins = new Set(["echo", "printf", "print", "export", "typeset", "declare", "set", "command", "eval", "source", "."]);
|
|
9
|
+
const shellCodeFlag = /^-[a-z]*c[a-z]*$/;
|
|
10
|
+
|
|
11
|
+
export function resolveCommand(cmd: Command, home: string): { children: string[]; code: string[] } {
|
|
12
|
+
const w = cmd.argv;
|
|
13
|
+
const children: string[] = [];
|
|
14
|
+
const code: string[] = [];
|
|
15
|
+
let i = 0;
|
|
16
|
+
let shell = true;
|
|
17
|
+
let lastPrecommand = "";
|
|
18
|
+
// Bash reads an assignment after nocorrect as an argument; zsh does not.
|
|
19
|
+
const assignment = (word: Word) => word.role === "assign" || /^[A-Za-z_][A-Za-z0-9_]*=/.test(word.raw);
|
|
20
|
+
while (i < w.length && (assignment(w[i]) || w[i].raw === "nocorrect")) w[i++].role = "precommand";
|
|
21
|
+
while (i < w.length && ["builtin", "-", "noglob"].includes(w[i].text)) {
|
|
22
|
+
lastPrecommand = w[i].text;
|
|
23
|
+
w[i++].role = "precommand";
|
|
24
|
+
}
|
|
25
|
+
// builtin runs nothing when the next word is not a builtin.
|
|
26
|
+
if (lastPrecommand === "builtin" && !zshBuiltins.has(w[i]?.text ?? "")) i = w.length;
|
|
27
|
+
wrappers: while (i < w.length) {
|
|
28
|
+
const name = basename(w[i].text);
|
|
29
|
+
switch (name) {
|
|
30
|
+
case "command":
|
|
31
|
+
if (w[i].text !== "command") break wrappers;
|
|
32
|
+
w[i++].role = "precommand";
|
|
33
|
+
while (["-p", "--"].includes(w[i]?.text)) i++;
|
|
34
|
+
// command -v only looks the name up.
|
|
35
|
+
if (["-v", "-V"].includes(w[i]?.text)) i = w.length;
|
|
36
|
+
break;
|
|
37
|
+
case "exec":
|
|
38
|
+
if (!shell || w[i].text !== "exec") break wrappers;
|
|
39
|
+
w[i++].role = "precommand";
|
|
40
|
+
while (["-c", "-l"].includes(w[i]?.text)) i++;
|
|
41
|
+
if (w[i]?.text === "-a") i += 2;
|
|
42
|
+
break;
|
|
43
|
+
case "nohup":
|
|
44
|
+
w[i++].role = "precommand";
|
|
45
|
+
if (w[i]?.text === "--") i++;
|
|
46
|
+
break;
|
|
47
|
+
case "timeout":
|
|
48
|
+
w[i++].role = "precommand";
|
|
49
|
+
while (w[i]?.text.startsWith("-")) {
|
|
50
|
+
if (["-s", "--signal", "-k", "--kill-after"].includes(w[i].text)) i++;
|
|
51
|
+
i++;
|
|
52
|
+
}
|
|
53
|
+
i++; // duration
|
|
54
|
+
break;
|
|
55
|
+
case "nice":
|
|
56
|
+
w[i++].role = "precommand";
|
|
57
|
+
if (w[i]?.text === "-n" || w[i]?.text === "--adjustment") i += 2;
|
|
58
|
+
else if (/^(-n|--adjustment=|-\d)/.test(w[i]?.text ?? "")) i++;
|
|
59
|
+
break;
|
|
60
|
+
case "stdbuf":
|
|
61
|
+
w[i++].role = "precommand";
|
|
62
|
+
while (/^-[ioe]/.test(w[i]?.text ?? "")) {
|
|
63
|
+
if (/^-[ioe]$/.test(w[i].text)) i++;
|
|
64
|
+
i++;
|
|
65
|
+
}
|
|
66
|
+
break;
|
|
67
|
+
case "caffeinate":
|
|
68
|
+
w[i++].role = "precommand";
|
|
69
|
+
while (/^-[disumtw]$/.test(w[i]?.text ?? "")) {
|
|
70
|
+
if (w[i].text === "-t" || w[i].text === "-w") i++;
|
|
71
|
+
i++;
|
|
72
|
+
}
|
|
73
|
+
break;
|
|
74
|
+
case "time":
|
|
75
|
+
w[i++].role = "precommand";
|
|
76
|
+
while (w[i]?.text.startsWith("-")) {
|
|
77
|
+
if (["-f", "-o", "--format", "--output"].includes(w[i].text)) i++;
|
|
78
|
+
i++;
|
|
79
|
+
}
|
|
80
|
+
break;
|
|
81
|
+
case "xargs":
|
|
82
|
+
w[i++].role = "precommand";
|
|
83
|
+
while (w[i]?.text.startsWith("-")) {
|
|
84
|
+
if (["-a", "-d", "-E", "-I", "-L", "-n", "-P", "-s", "--arg-file", "--delimiter", "--replace", "--max-args"].includes(w[i].text)) i++;
|
|
85
|
+
i++;
|
|
86
|
+
}
|
|
87
|
+
break;
|
|
88
|
+
case "env":
|
|
89
|
+
w[i++].role = "precommand";
|
|
90
|
+
while (i < w.length) {
|
|
91
|
+
const arg = w[i].text;
|
|
92
|
+
if (arg === "-C") {
|
|
93
|
+
if (w[i + 1]) {
|
|
94
|
+
cmd.cwd = resolve(cmd.cwd, w[i + 1].text);
|
|
95
|
+
w[i + 1].role = "precommand";
|
|
96
|
+
}
|
|
97
|
+
i += 2;
|
|
98
|
+
} else if (["-u", "-P"].includes(arg)) i += 2;
|
|
99
|
+
else if (arg === "-S") {
|
|
100
|
+
// The split string is a command line of its own.
|
|
101
|
+
children.push(w[i + 1]?.text ?? "");
|
|
102
|
+
cmd.wrappers.push("env-S");
|
|
103
|
+
i = w.length;
|
|
104
|
+
} else if (arg.startsWith("-") || arg.includes("=")) i++;
|
|
105
|
+
else break;
|
|
106
|
+
}
|
|
107
|
+
break;
|
|
108
|
+
case "repeat":
|
|
109
|
+
// zsh's repeat COUNT runs the rest; bash parses it as a plain command.
|
|
110
|
+
if (!shell || w[i].text !== "repeat") break wrappers;
|
|
111
|
+
w[i++].role = "precommand";
|
|
112
|
+
i++;
|
|
113
|
+
continue;
|
|
114
|
+
case "envchain":
|
|
115
|
+
w[i++].role = "precommand";
|
|
116
|
+
// --set, --list and --unset manage the store and run nothing.
|
|
117
|
+
if (w[i]?.text.startsWith("-")) i = w.length;
|
|
118
|
+
if (w[i]) w[i].role = "namespace";
|
|
119
|
+
i++;
|
|
120
|
+
break;
|
|
121
|
+
default:
|
|
122
|
+
break wrappers;
|
|
123
|
+
}
|
|
124
|
+
cmd.wrappers.push(name);
|
|
125
|
+
shell = false;
|
|
126
|
+
}
|
|
127
|
+
cmd.shell = shell;
|
|
128
|
+
if (i >= w.length) return { children, code };
|
|
129
|
+
cmd.program = i;
|
|
130
|
+
w[i].role = "program";
|
|
131
|
+
if (w[i].text.startsWith("=") && w[i].text.length > 1) w[i].text = w[i].text.slice(1);
|
|
132
|
+
const name = basename(w[i].text);
|
|
133
|
+
const rest = w.slice(i + 1);
|
|
134
|
+
if (["rg", "grep", "ag", "ack"].includes(name)) {
|
|
135
|
+
searchRoles(cmd, rest, name);
|
|
136
|
+
} else if (name === "fd") {
|
|
137
|
+
let pattern = true;
|
|
138
|
+
for (let n = 0; n < rest.length; n++) {
|
|
139
|
+
const arg = rest[n].text;
|
|
140
|
+
if (["-x", "-X", "--exec", "--exec-batch"].includes(arg)) {
|
|
141
|
+
rest[n].role = "option";
|
|
142
|
+
children.push(rest.slice(n + 1).map((word) => word.raw).join(" "));
|
|
143
|
+
break;
|
|
144
|
+
} else if ((/^(--search-path|--base-directory)(=|$)/.test(arg) || arg === "-C") && (arg.includes("=") || rest[n + 1])) {
|
|
145
|
+
const separate = !arg.includes("=");
|
|
146
|
+
const base = arg === "-C" || arg.startsWith("--base-directory");
|
|
147
|
+
if (separate) rest[n++].role = "option";
|
|
148
|
+
const path = rest[n];
|
|
149
|
+
path.role = "path";
|
|
150
|
+
path.value = separate ? path.text : arg.slice(arg.indexOf("=") + 1);
|
|
151
|
+
if (base) {
|
|
152
|
+
path.value = expandHome(path.value, home);
|
|
153
|
+
cmd.cwd = resolve(cmd.cwd, path.value);
|
|
154
|
+
}
|
|
155
|
+
} else if (["-E", "-e", "-t", "-d", "--exclude", "--extension", "--type", "--max-depth"].includes(arg)) {
|
|
156
|
+
rest[n].role = "option";
|
|
157
|
+
if (rest[n + 1]) rest[++n].role = "optarg";
|
|
158
|
+
} else if (arg.startsWith("-")) rest[n].role = "option";
|
|
159
|
+
else if (pattern) (rest[n].role = "pattern"), (pattern = false);
|
|
160
|
+
else rest[n].role = "path";
|
|
161
|
+
}
|
|
162
|
+
} else if (name === "du") {
|
|
163
|
+
for (let n = 0; n < rest.length; n++) {
|
|
164
|
+
const arg = rest[n].text;
|
|
165
|
+
if (["-d", "-I", "-B", "-t", "--max-depth", "--exclude", "--block-size", "--threshold"].includes(arg) || /^-[^-]+[dIBt]$/.test(arg)) {
|
|
166
|
+
rest[n].role = "option";
|
|
167
|
+
if (rest[n + 1]) rest[++n].role = "optarg";
|
|
168
|
+
} else if (arg.startsWith("-")) rest[n].role = "option";
|
|
169
|
+
}
|
|
170
|
+
} else if (shellPrograms.has(name)) {
|
|
171
|
+
const flag = rest.findIndex((a) => shellCodeFlag.test(a.text));
|
|
172
|
+
if (flag >= 0) {
|
|
173
|
+
if (rest[flag + 1]) rest[flag + 1].role = "code";
|
|
174
|
+
children.push(rest[flag + 1]?.text ?? "");
|
|
175
|
+
}
|
|
176
|
+
} else if (name === "eval" && (shell || cmd.wrappers.includes("command"))) {
|
|
177
|
+
children.push(rest.map((a) => a.text).join(" "));
|
|
178
|
+
} else if (codePrograms.has(name)) {
|
|
179
|
+
const flag = rest.findIndex((a) => /^(-[ceE]|--eval)$/.test(a.text));
|
|
180
|
+
if (flag >= 0) {
|
|
181
|
+
if (rest[flag + 1]) rest[flag + 1].role = "code";
|
|
182
|
+
code.push(rest[flag + 1]?.text ?? "");
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return { children, code };
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export function stdinKind(cmd: Command): "shell" | "code" | undefined {
|
|
189
|
+
const program = cmd.argv[cmd.program];
|
|
190
|
+
if (!program) return undefined;
|
|
191
|
+
const name = basename(program.text);
|
|
192
|
+
const args = cmd.argv.slice(cmd.program + 1);
|
|
193
|
+
if (shellPrograms.has(name) && !args.some((a) => shellCodeFlag.test(a.text))) return "shell";
|
|
194
|
+
if (codePrograms.has(name)) return "code";
|
|
195
|
+
return undefined;
|
|
196
|
+
}
|
package/src/comments.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// mvdan/sh 3.5 starts a comment at a # right after a quote or an expansion,
|
|
2
|
+
// where bash and zsh keep the # in the word and run the rest of the line.
|
|
3
|
+
import sh from "mvdan-sh";
|
|
4
|
+
import type { Comment, File } from "mvdan-sh";
|
|
5
|
+
|
|
6
|
+
export function misreadComment(file: File, bytes: Buffer): boolean {
|
|
7
|
+
let misread = false;
|
|
8
|
+
sh.syntax.Walk(file, (node) => {
|
|
9
|
+
if (node && sh.syntax.NodeType(node) === "Comment") {
|
|
10
|
+
const at = (node as Comment).Hash.Offset();
|
|
11
|
+
misread ||= at > 0 && !" \t\n;&|()<>".includes(String.fromCharCode(bytes[at - 1]));
|
|
12
|
+
}
|
|
13
|
+
return !misread;
|
|
14
|
+
});
|
|
15
|
+
return misread;
|
|
16
|
+
}
|
package/src/core.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { parseScript } from "./frontend.ts";
|
|
2
|
+
import { linkedRequest } from "./links.ts";
|
|
3
|
+
import { absPath } from "./paths.ts";
|
|
4
|
+
import { reasons } from "./reasons.ts";
|
|
5
|
+
import type { Request, Runtime, Tool } from "./record.ts";
|
|
6
|
+
import { appdataRules } from "./rules/appdata.ts";
|
|
7
|
+
import { credentialFilesystemRules, credentialRules } from "./rules/credentials.ts";
|
|
8
|
+
import { claudeWorkflowRules, codexWorkflowRules } from "./rules/workflow.ts";
|
|
9
|
+
|
|
10
|
+
export function buildRequest(runtime: Runtime, tool: Tool, cwd: string, input: string, glob: string, home: string): Request {
|
|
11
|
+
const req: Request = { runtime, tool, home, cwd: absPath(cwd || "/", "/", home), operation: "", target: "", searchRoot: "", glob, commands: [], uninspectable: [] };
|
|
12
|
+
if (tool === "bash") {
|
|
13
|
+
if (input) Object.assign(req, parseScript(input, req.cwd, home));
|
|
14
|
+
} else if (tool === "grep") {
|
|
15
|
+
req.operation = "search";
|
|
16
|
+
req.searchRoot = absPath(input || req.cwd, req.cwd, home);
|
|
17
|
+
} else if (input) {
|
|
18
|
+
req.operation = tool === "read" ? "read" : "write";
|
|
19
|
+
req.target = absPath(input, req.cwd, home);
|
|
20
|
+
}
|
|
21
|
+
return req;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function evaluate(req: Request): string | undefined {
|
|
25
|
+
const denials = [
|
|
26
|
+
...appdataRules(req),
|
|
27
|
+
...credentialRules(req),
|
|
28
|
+
];
|
|
29
|
+
// Deny lexically protected paths before asking the filesystem about links.
|
|
30
|
+
if (!denials.length) {
|
|
31
|
+
try {
|
|
32
|
+
const linked = linkedRequest(req);
|
|
33
|
+
if (linked) denials.push(...appdataRules(linked), ...credentialRules(linked));
|
|
34
|
+
} catch {
|
|
35
|
+
denials.push(reasons.symlink);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (!denials.length) denials.push(...credentialFilesystemRules(req));
|
|
39
|
+
return denials[0];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function suggestions(req: Request): string[] {
|
|
43
|
+
return req.runtime === "claude" ? claudeWorkflowRules(req) : req.runtime === "codex" ? codexWorkflowRules(req) : [];
|
|
44
|
+
}
|
package/src/cwd.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// Keep speculative directories bounded without losing broad home roots.
|
|
2
|
+
export function boundedDirectories(current: string, candidates: string[], home: string): string[] {
|
|
3
|
+
const distinct = [...new Set(candidates)].filter((path) => path !== current);
|
|
4
|
+
return distinct.length > 16 ? [home, `${home}/Library`] : distinct;
|
|
5
|
+
}
|