telecoder 0.0.0-stage → 1.2.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/.env.example +363 -0
- package/CHANGELOG.md +1186 -0
- package/LICENSE +22 -0
- package/README.md +126 -2
- package/dist/audio/transcribe.js +50 -0
- package/dist/bin/mcp-server.js +378 -0
- package/dist/bin/tool-result.js +22 -0
- package/dist/bot/access/group-access.js +147 -0
- package/dist/bot/bot.js +592 -0
- package/dist/bot/file-access.js +141 -0
- package/dist/bot/handlers/command/access.js +407 -0
- package/dist/bot/handlers/command/admin.js +827 -0
- package/dist/bot/handlers/command/auto-approve.js +138 -0
- package/dist/bot/handlers/command/btw.js +224 -0
- package/dist/bot/handlers/command/cost.js +409 -0
- package/dist/bot/handlers/command/media.js +197 -0
- package/dist/bot/handlers/command/model.js +305 -0
- package/dist/bot/handlers/command/prefs-scope.js +197 -0
- package/dist/bot/handlers/command/project.js +536 -0
- package/dist/bot/handlers/command/resume.js +321 -0
- package/dist/bot/handlers/command/session.js +438 -0
- package/dist/bot/handlers/command/settings.js +1012 -0
- package/dist/bot/handlers/command/shared.js +210 -0
- package/dist/bot/handlers/command/streaming-mode.js +16 -0
- package/dist/bot/handlers/command/tasks.js +513 -0
- package/dist/bot/handlers/command/topic-store.js +113 -0
- package/dist/bot/handlers/command/topic.js +80 -0
- package/dist/bot/handlers/command.handler.js +772 -0
- package/dist/bot/handlers/fork.handler.js +1109 -0
- package/dist/bot/handlers/media-fallback.js +121 -0
- package/dist/bot/handlers/message/replies.js +133 -0
- package/dist/bot/handlers/message/shared.js +117 -0
- package/dist/bot/handlers/message/throttle.js +49 -0
- package/dist/bot/handlers/message/turn-notify.js +169 -0
- package/dist/bot/handlers/message/turn-runner.js +141 -0
- package/dist/bot/handlers/message.handler.js +107 -0
- package/dist/bot/handlers/newbot.handler.js +221 -0
- package/dist/bot/handlers/photo.handler.js +327 -0
- package/dist/bot/handlers/prompts.handler.js +343 -0
- package/dist/bot/handlers/running-note.js +44 -0
- package/dist/bot/handlers/schedule.handler.js +303 -0
- package/dist/bot/handlers/session-guard.js +78 -0
- package/dist/bot/handlers/suggestion.handler.js +84 -0
- package/dist/bot/handlers/tree.handler.js +480 -0
- package/dist/bot/handlers/view.js +41 -0
- package/dist/bot/handlers/voice.handler.js +209 -0
- package/dist/bot/middleware/admin-only.js +49 -0
- package/dist/bot/middleware/auth.middleware.js +122 -0
- package/dist/bot/middleware/group-mention.middleware.js +139 -0
- package/dist/bot/middleware/group-role.middleware.js +75 -0
- package/dist/bot/middleware/message-batcher.js +87 -0
- package/dist/bot/middleware/queued-messages.js +971 -0
- package/dist/bot/middleware/stale-filter.js +31 -0
- package/dist/build-info.json +8 -0
- package/dist/claude/agent-watchdog.js +162 -0
- package/dist/claude/agent.js +1458 -0
- package/dist/claude/ask-user.js +295 -0
- package/dist/claude/auto-approve.js +225 -0
- package/dist/claude/auto-topic-haiku.js +126 -0
- package/dist/claude/available-commands.js +153 -0
- package/dist/claude/carry-over.js +27 -0
- package/dist/claude/ccr-shim.js +46 -0
- package/dist/claude/charter-judge.js +214 -0
- package/dist/claude/claude-auth.js +121 -0
- package/dist/claude/command-parser.js +200 -0
- package/dist/claude/cost-overlay-scraper.js +129 -0
- package/dist/claude/enabled-plugins.js +56 -0
- package/dist/claude/fork-tree.js +393 -0
- package/dist/claude/held-query.js +190 -0
- package/dist/claude/in-flight-tracker.js +61 -0
- package/dist/claude/ipc-server.js +192 -0
- package/dist/claude/mcp-bridge.js +453 -0
- package/dist/claude/mcp-tools.js +354 -0
- package/dist/claude/message-offsets.js +88 -0
- package/dist/claude/modal-relay.js +450 -0
- package/dist/claude/model-catalog.js +85 -0
- package/dist/claude/monitor-relay.js +597 -0
- package/dist/claude/pending-forks.js +93 -0
- package/dist/claude/pending-suggestions.js +50 -0
- package/dist/claude/permission-gate.js +337 -0
- package/dist/claude/permission-mode.js +178 -0
- package/dist/claude/poll-user.js +51 -0
- package/dist/claude/project-commands.js +69 -0
- package/dist/claude/prompt-hold.js +124 -0
- package/dist/claude/prompt-suggestion-scraper.js +89 -0
- package/dist/claude/pty-provider.js +2214 -0
- package/dist/claude/pty-spawn-config.js +296 -0
- package/dist/claude/pty-stream.js +363 -0
- package/dist/claude/request-queue.js +135 -0
- package/dist/claude/scheduled-runner.js +91 -0
- package/dist/claude/scheduler.js +237 -0
- package/dist/claude/scope-guard.js +274 -0
- package/dist/claude/sdk-permission-gate.js +77 -0
- package/dist/claude/secret-request.js +576 -0
- package/dist/claude/session-cost.js +41 -0
- package/dist/claude/session-history.js +286 -0
- package/dist/claude/session-jsonl.js +920 -0
- package/dist/claude/session-manager.js +195 -0
- package/dist/claude/session-scan.js +270 -0
- package/dist/claude/setup-token.js +285 -0
- package/dist/claude/side-call-options.js +45 -0
- package/dist/claude/side-question.js +249 -0
- package/dist/claude/stage-images.js +62 -0
- package/dist/claude/subagent-tools.js +12 -0
- package/dist/claude/synthetic-ctx.js +51 -0
- package/dist/claude/system-prompt.js +162 -0
- package/dist/claude/tasks-overlay-scraper.js +94 -0
- package/dist/claude/tip-scraper.js +78 -0
- package/dist/claude/tui-modal.js +501 -0
- package/dist/claude/tui-state.js +384 -0
- package/dist/claude/turn-end.js +91 -0
- package/dist/claude/turn-prose.js +64 -0
- package/dist/claude/update-banner-relay.js +70 -0
- package/dist/cli/commands/doctor.js +305 -0
- package/dist/cli/commands/help.js +25 -0
- package/dist/cli/commands/init.js +123 -0
- package/dist/cli/commands/service.js +678 -0
- package/dist/cli/commands/start.js +53 -0
- package/dist/cli/commands/version.js +11 -0
- package/dist/cli/dispatch.js +79 -0
- package/dist/cli/init/env-file.js +99 -0
- package/dist/cli/init/flow.js +638 -0
- package/dist/cli/init/prompter.js +116 -0
- package/dist/cli/init/telegram.js +94 -0
- package/dist/cli/service/render.js +212 -0
- package/dist/cli.js +18 -0
- package/dist/config-report.js +107 -0
- package/dist/config.js +500 -0
- package/dist/index.js +367 -0
- package/dist/launcher.js +933 -0
- package/dist/node-version-check.js +27 -0
- package/dist/providers/ccr-health.js +119 -0
- package/dist/providers/ccr-introspect.js +149 -0
- package/dist/providers/ccr-provider.js +60 -0
- package/dist/providers/claude-provider.js +63 -0
- package/dist/providers/prefs-sync.js +164 -0
- package/dist/providers/project-favorites.js +74 -0
- package/dist/providers/provider-router.js +127 -0
- package/dist/providers/provider-switch.js +91 -0
- package/dist/providers/sdk-provider.js +37 -0
- package/dist/providers/types.js +8 -0
- package/dist/providers/user-preferences.js +214 -0
- package/dist/restart-when-idle.js +153 -0
- package/dist/startup/auto-continue.js +67 -0
- package/dist/startup/auto-resume.js +173 -0
- package/dist/startup/notices.js +120 -0
- package/dist/startup/shared.js +44 -0
- package/dist/startup/verify-token.js +31 -0
- package/dist/telegram/access-request.js +153 -0
- package/dist/telegram/action-logger.js +684 -0
- package/dist/telegram/admin-mention.js +107 -0
- package/dist/telegram/background-activity-card.js +502 -0
- package/dist/telegram/botname-settings.js +336 -0
- package/dist/telegram/button-text.js +26 -0
- package/dist/telegram/client-options.js +27 -0
- package/dist/telegram/deduplication.js +33 -0
- package/dist/telegram/entities.js +295 -0
- package/dist/telegram/force-reply-tracker.js +57 -0
- package/dist/telegram/lettered-options.js +87 -0
- package/dist/telegram/markdown.js +362 -0
- package/dist/telegram/message-sender.js +1287 -0
- package/dist/telegram/prompt-label.js +142 -0
- package/dist/telegram/prompt-suggestion.js +54 -0
- package/dist/telegram/send.js +123 -0
- package/dist/telegram/streaming-turn.js +105 -0
- package/dist/telegram/suggestions-settings.js +30 -0
- package/dist/telegram/task-list.js +367 -0
- package/dist/telegram/task-tracker.js +209 -0
- package/dist/telegram/telegraph-settings.js +31 -0
- package/dist/telegram/telegraph.js +453 -0
- package/dist/telegram/terminal-renderer.js +671 -0
- package/dist/telegram/terminal-settings.js +25 -0
- package/dist/telegram/thread-titles.js +190 -0
- package/dist/telegram/threads-setup.js +117 -0
- package/dist/tts/tts-settings.js +62 -0
- package/dist/tts/tts.js +245 -0
- package/dist/tts/voice-reply.js +78 -0
- package/dist/utils/admins.js +143 -0
- package/dist/utils/agent-timer.js +55 -0
- package/dist/utils/atomic-write.js +24 -0
- package/dist/utils/bot-token.js +74 -0
- package/dist/utils/bounded-map.js +55 -0
- package/dist/utils/build-info.js +57 -0
- package/dist/utils/caffeinate.js +50 -0
- package/dist/utils/claude-env.js +54 -0
- package/dist/utils/claude-version.js +48 -0
- package/dist/utils/config-home.js +62 -0
- package/dist/utils/debug-agent.js +117 -0
- package/dist/utils/debug-watchdog.js +121 -0
- package/dist/utils/download.js +111 -0
- package/dist/utils/exit-codes.js +48 -0
- package/dist/utils/file-type.js +149 -0
- package/dist/utils/format.js +81 -0
- package/dist/utils/home-project.js +30 -0
- package/dist/utils/host-stall.js +51 -0
- package/dist/utils/id-list.js +44 -0
- package/dist/utils/idle-restart.js +92 -0
- package/dist/utils/install-mode.js +63 -0
- package/dist/utils/instance-config.js +148 -0
- package/dist/utils/instances.js +95 -0
- package/dist/utils/json-store.js +140 -0
- package/dist/utils/keyed-settings.js +62 -0
- package/dist/utils/legacy-env.js +35 -0
- package/dist/utils/npm-version.js +134 -0
- package/dist/utils/prefs-relay.js +107 -0
- package/dist/utils/proc-children.js +102 -0
- package/dist/utils/project-data-dir.js +33 -0
- package/dist/utils/reasoning-summary.js +12 -0
- package/dist/utils/resolve-bin.js +86 -0
- package/dist/utils/resolve-claude-bin.js +63 -0
- package/dist/utils/respawn-backoff.js +19 -0
- package/dist/utils/sanitize.js +91 -0
- package/dist/utils/session-key.js +37 -0
- package/dist/utils/stale-launcher.js +109 -0
- package/dist/utils/state-dir-migration.js +92 -0
- package/dist/utils/systemd.js +126 -0
- package/dist/utils/user-roster.js +275 -0
- package/dist/utils/verbosity.js +118 -0
- package/dist/utils/version-check.js +166 -0
- package/dist/utils/workspace-guard.js +44 -0
- package/dist/worker-restart.js +141 -0
- package/instances.json.example +41 -0
- package/package.json +76 -4
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Does this tool call reach outside the projects the bot was shared for?
|
|
3
|
+
*
|
|
4
|
+
* The permission gate's pattern list answers "is this command destructive".
|
|
5
|
+
* It has nothing to say about `cat ~/.ssh/id_rsa`, or an edit to a project
|
|
6
|
+
* nobody was invited to, because neither is destructive — they are simply out
|
|
7
|
+
* of bounds. This module answers that second question.
|
|
8
|
+
*
|
|
9
|
+
* It is a supervision layer, not a sandbox. Claude still runs as the bot's Unix
|
|
10
|
+
* user with permissions bypassed, so anything here can be defeated by a command
|
|
11
|
+
* written to avoid naming its path. The point is that the ordinary way to reach
|
|
12
|
+
* out of scope — naming the path — surfaces as an approval prompt, and the
|
|
13
|
+
* ordinary way to work inside scope never does.
|
|
14
|
+
*
|
|
15
|
+
* That balance is why the rules below are shaped the way they are. A guest's
|
|
16
|
+
* session reads from `/usr`, `/etc` and the language toolchains all day; making
|
|
17
|
+
* those prompt would train everyone to tap Allow without reading. So system
|
|
18
|
+
* paths are readable, credential directories never are, and everything else is
|
|
19
|
+
* judged against the configured roots.
|
|
20
|
+
*/
|
|
21
|
+
import * as os from 'os';
|
|
22
|
+
import * as path from 'path';
|
|
23
|
+
import { config } from '../config.js';
|
|
24
|
+
import { getWorkspaceRoot } from '../utils/workspace-guard.js';
|
|
25
|
+
/** Tools whose input names a path we can check directly. */
|
|
26
|
+
const PATH_FIELDS = {
|
|
27
|
+
Read: ['file_path'],
|
|
28
|
+
Write: ['file_path'],
|
|
29
|
+
Edit: ['file_path'],
|
|
30
|
+
NotebookEdit: ['notebook_path'],
|
|
31
|
+
Glob: ['path'],
|
|
32
|
+
Grep: ['path'],
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Directories whose contents are credentials, and the bot's own state. Always
|
|
36
|
+
* prompt, even when they sit inside an allowed root — which they do the moment
|
|
37
|
+
* someone points WORKSPACE_DIR at a home directory.
|
|
38
|
+
*
|
|
39
|
+
* Matched on path segments, so `~/.ssh` catches `/home/x/.ssh/id_rsa` without
|
|
40
|
+
* also catching `/srv/app/ssh-helper`.
|
|
41
|
+
*/
|
|
42
|
+
const SENSITIVE_SEGMENTS = [
|
|
43
|
+
'.ssh',
|
|
44
|
+
'.aws',
|
|
45
|
+
'.gnupg',
|
|
46
|
+
'.kube',
|
|
47
|
+
'.docker',
|
|
48
|
+
'.claude',
|
|
49
|
+
'.claudegram',
|
|
50
|
+
'.telecoder',
|
|
51
|
+
'.password-store',
|
|
52
|
+
'.mozilla',
|
|
53
|
+
'.thunderbird',
|
|
54
|
+
];
|
|
55
|
+
/** Credential files, matched on basename. */
|
|
56
|
+
const SENSITIVE_FILES = [
|
|
57
|
+
'.netrc',
|
|
58
|
+
'.npmrc',
|
|
59
|
+
'.pypirc',
|
|
60
|
+
'.git-credentials',
|
|
61
|
+
'.pgpass',
|
|
62
|
+
'id_rsa',
|
|
63
|
+
'id_dsa',
|
|
64
|
+
'id_ecdsa',
|
|
65
|
+
'id_ed25519',
|
|
66
|
+
'shadow',
|
|
67
|
+
'sudoers',
|
|
68
|
+
];
|
|
69
|
+
/** `~/.config/<name>` holding credentials rather than preferences. */
|
|
70
|
+
const SENSITIVE_CONFIG_SUBDIRS = ['gcloud', 'gh', 'op', 'doctl', 'fly', 'rclone'];
|
|
71
|
+
/**
|
|
72
|
+
* Readable without a prompt: the operating system and the language toolchains.
|
|
73
|
+
* Reading these is background noise in any real session, and a prompt for each
|
|
74
|
+
* one would be a prompt nobody reads.
|
|
75
|
+
*/
|
|
76
|
+
const SYSTEM_READ_PREFIXES = [
|
|
77
|
+
'/usr', '/etc', '/opt', '/bin', '/sbin', '/lib', '/lib64',
|
|
78
|
+
'/proc', '/sys', '/var/log', '/var/lib', '/snap', '/nix',
|
|
79
|
+
'/home/linuxbrew', '/Library', '/System', '/Applications',
|
|
80
|
+
];
|
|
81
|
+
/** Same idea, under the bot user's home. Deliberately excludes `.config`. */
|
|
82
|
+
const HOME_READ_SUFFIXES = [
|
|
83
|
+
'.nvm', '.npm', '.cargo', '.rustup', '.pyenv', '.rbenv', '.sdkman',
|
|
84
|
+
'.cache', '.local/lib', '.local/bin', '.bun', '.deno',
|
|
85
|
+
];
|
|
86
|
+
/** True when the scope guard runs at all. Follows the gate unless overridden. */
|
|
87
|
+
export function isScopeGuardEnabled(gateEnabled) {
|
|
88
|
+
if (config.SCOPE_GUARD === 'on')
|
|
89
|
+
return true;
|
|
90
|
+
if (config.SCOPE_GUARD === 'off')
|
|
91
|
+
return false;
|
|
92
|
+
return gateEnabled;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* The directories a session may work in without asking. `/tmp` is included
|
|
96
|
+
* because every toolchain writes there and nothing durable lives there.
|
|
97
|
+
*/
|
|
98
|
+
export function getAllowedRoots() {
|
|
99
|
+
const roots = [getWorkspaceRoot(), os.tmpdir(), '/tmp', ...config.SCOPE_ALLOWED_PATHS];
|
|
100
|
+
return dedupe(roots.map((r) => path.resolve(r)));
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Inspect a tool call for paths outside the allowed roots.
|
|
104
|
+
*
|
|
105
|
+
* `cwd` is the session's working directory, used to resolve relative paths —
|
|
106
|
+
* without it a `../../..` climb reads as harmless.
|
|
107
|
+
*/
|
|
108
|
+
export function checkToolScope(toolName, toolInput, cwd) {
|
|
109
|
+
const roots = getAllowedRoots();
|
|
110
|
+
const fields = PATH_FIELDS[toolName];
|
|
111
|
+
if (fields) {
|
|
112
|
+
for (const field of fields) {
|
|
113
|
+
const raw = toolInput[field];
|
|
114
|
+
if (typeof raw !== 'string' || !raw)
|
|
115
|
+
continue;
|
|
116
|
+
const verdict = classify(raw, cwd, roots);
|
|
117
|
+
if (verdict.outOfScope)
|
|
118
|
+
return verdict;
|
|
119
|
+
}
|
|
120
|
+
return { outOfScope: false };
|
|
121
|
+
}
|
|
122
|
+
if (toolName === 'Bash') {
|
|
123
|
+
const command = typeof toolInput.command === 'string' ? toolInput.command : '';
|
|
124
|
+
for (const token of extractPathTokens(command)) {
|
|
125
|
+
const verdict = classify(token, cwd, roots);
|
|
126
|
+
if (verdict.outOfScope)
|
|
127
|
+
return verdict;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return { outOfScope: false };
|
|
131
|
+
}
|
|
132
|
+
/** Judge one path string against the roots. Exported for tests. */
|
|
133
|
+
export function classify(raw, cwd, roots) {
|
|
134
|
+
const resolved = resolvePath(raw, cwd);
|
|
135
|
+
if (!resolved)
|
|
136
|
+
return { outOfScope: false };
|
|
137
|
+
if (isSensitive(resolved)) {
|
|
138
|
+
return {
|
|
139
|
+
outOfScope: true,
|
|
140
|
+
reason: 'reads a credential or bot-state path',
|
|
141
|
+
offender: display(resolved),
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
if (roots.some((root) => within(root, resolved)))
|
|
145
|
+
return { outOfScope: false };
|
|
146
|
+
if (isSystemReadable(resolved))
|
|
147
|
+
return { outOfScope: false };
|
|
148
|
+
return {
|
|
149
|
+
outOfScope: true,
|
|
150
|
+
reason: 'path outside the shared projects',
|
|
151
|
+
offender: display(resolved),
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Turn a raw path string into an absolute one, or undefined when it isn't a
|
|
156
|
+
* path we can judge. Only strings that announce themselves as paths qualify —
|
|
157
|
+
* a leading `/`, `~`, `$HOME`, `./` or `../` — which is what keeps `sed`'s
|
|
158
|
+
* `s/a/b/` and a bare `npm` out of the results.
|
|
159
|
+
*/
|
|
160
|
+
export function resolvePath(raw, cwd) {
|
|
161
|
+
let value = raw.trim().replace(/^['"]+|['"]+$/g, '');
|
|
162
|
+
if (!value)
|
|
163
|
+
return undefined;
|
|
164
|
+
// Home expansion runs before the glob trim, not after: `${HOME}` is braces
|
|
165
|
+
// around a variable, and trimming at the first brace would leave a bare `$`.
|
|
166
|
+
value = value.replace(/^\$\{HOME\}/, os.homedir()).replace(/^\$HOME\b/, os.homedir());
|
|
167
|
+
if (value === '~')
|
|
168
|
+
value = os.homedir();
|
|
169
|
+
else if (value.startsWith('~/'))
|
|
170
|
+
value = path.join(os.homedir(), value.slice(2));
|
|
171
|
+
// Glob and brace metacharacters: keep the literal prefix, which is what
|
|
172
|
+
// decides containment.
|
|
173
|
+
const metaIdx = value.search(/[*?[\]{}]/);
|
|
174
|
+
if (metaIdx === 0)
|
|
175
|
+
return undefined;
|
|
176
|
+
if (metaIdx > 0)
|
|
177
|
+
value = value.slice(0, metaIdx);
|
|
178
|
+
if (!value)
|
|
179
|
+
return undefined;
|
|
180
|
+
if (value.startsWith('/'))
|
|
181
|
+
return path.resolve(value);
|
|
182
|
+
if (value.startsWith('./') || value.startsWith('../') || value === '..') {
|
|
183
|
+
return path.resolve(cwd, value);
|
|
184
|
+
}
|
|
185
|
+
return undefined;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Pull path-looking tokens out of a shell command.
|
|
189
|
+
*
|
|
190
|
+
* Approximate by nature, and deliberately biased toward missing things rather
|
|
191
|
+
* than inventing them: a false positive is a prompt for a command that was
|
|
192
|
+
* fine, which is the cost that makes people stop reading prompts.
|
|
193
|
+
*/
|
|
194
|
+
export function extractPathTokens(command) {
|
|
195
|
+
const tokens = [];
|
|
196
|
+
for (const rawToken of command.split(/[\s;|&()<>]+/)) {
|
|
197
|
+
if (!rawToken)
|
|
198
|
+
continue;
|
|
199
|
+
// A URL is not a filesystem path; `//` after a scheme would resolve to one.
|
|
200
|
+
if (/^[a-z][a-z0-9+.-]*:\/\//i.test(rawToken))
|
|
201
|
+
continue;
|
|
202
|
+
// `--config=/path` and `-I/usr/include` carry a path after the flag.
|
|
203
|
+
const candidates = [rawToken];
|
|
204
|
+
const eq = rawToken.indexOf('=');
|
|
205
|
+
if (eq > 0)
|
|
206
|
+
candidates.push(rawToken.slice(eq + 1));
|
|
207
|
+
if (/^-[A-Za-z]/.test(rawToken))
|
|
208
|
+
candidates.push(rawToken.replace(/^-+[A-Za-z]*/, ''));
|
|
209
|
+
for (const candidate of candidates) {
|
|
210
|
+
const stripped = candidate.replace(/^['"]+/, '');
|
|
211
|
+
if (stripped.startsWith('/') ||
|
|
212
|
+
stripped.startsWith('~') ||
|
|
213
|
+
stripped.startsWith('$HOME') ||
|
|
214
|
+
stripped.startsWith('${HOME}') ||
|
|
215
|
+
stripped.startsWith('./') ||
|
|
216
|
+
stripped.startsWith('../')) {
|
|
217
|
+
tokens.push(stripped);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
return dedupe(tokens);
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Is this absolute path a credential or bot-state path? The same denylist the
|
|
225
|
+
* tool-call check uses, for other ways of reaching a file — `/file` and
|
|
226
|
+
* `/telegraph <path>` — so there is one list to keep current.
|
|
227
|
+
*/
|
|
228
|
+
export function isSensitivePath(absolutePath) {
|
|
229
|
+
return isSensitive(path.resolve(absolutePath));
|
|
230
|
+
}
|
|
231
|
+
function isSensitive(resolved) {
|
|
232
|
+
const segments = resolved.split(path.sep).filter(Boolean);
|
|
233
|
+
if (segments.some((s) => SENSITIVE_SEGMENTS.includes(s)))
|
|
234
|
+
return true;
|
|
235
|
+
const base = segments[segments.length - 1];
|
|
236
|
+
if (base && SENSITIVE_FILES.includes(base))
|
|
237
|
+
return true;
|
|
238
|
+
const configIdx = segments.indexOf('.config');
|
|
239
|
+
if (configIdx >= 0) {
|
|
240
|
+
const next = segments[configIdx + 1];
|
|
241
|
+
if (next && SENSITIVE_CONFIG_SUBDIRS.includes(next))
|
|
242
|
+
return true;
|
|
243
|
+
}
|
|
244
|
+
// Any dotenv outside the allowed roots — the bot's own token file is one.
|
|
245
|
+
if (base === '.env' || base?.startsWith('.env.'))
|
|
246
|
+
return true;
|
|
247
|
+
return false;
|
|
248
|
+
}
|
|
249
|
+
function isSystemReadable(resolved) {
|
|
250
|
+
if (SYSTEM_READ_PREFIXES.some((prefix) => within(prefix, resolved)))
|
|
251
|
+
return true;
|
|
252
|
+
const home = os.homedir();
|
|
253
|
+
return HOME_READ_SUFFIXES.some((suffix) => within(path.join(home, suffix), resolved));
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Containment by path segment. Purely lexical on already-resolved paths: unlike
|
|
257
|
+
* utils/workspace-guard this must judge paths that do not exist yet, which is
|
|
258
|
+
* most of them at PreToolUse time.
|
|
259
|
+
*/
|
|
260
|
+
function within(root, target) {
|
|
261
|
+
const r = path.resolve(root);
|
|
262
|
+
return target === r || target.startsWith(r.endsWith(path.sep) ? r : r + path.sep);
|
|
263
|
+
}
|
|
264
|
+
/** Shorten a home path for display, matching how the rest of the bot logs. */
|
|
265
|
+
function display(resolved) {
|
|
266
|
+
const home = os.homedir();
|
|
267
|
+
return resolved === home || resolved.startsWith(home + path.sep)
|
|
268
|
+
? '~' + resolved.slice(home.length)
|
|
269
|
+
: resolved;
|
|
270
|
+
}
|
|
271
|
+
function dedupe(values) {
|
|
272
|
+
return [...new Set(values)];
|
|
273
|
+
}
|
|
274
|
+
//# sourceMappingURL=scope-guard.js.map
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { gateHookTimeoutSeconds, gateToolCall } from './permission-gate.js';
|
|
2
|
+
/**
|
|
3
|
+
* The permission gate on the SDK transport, and so on CCR, which always takes
|
|
4
|
+
* the SDK path.
|
|
5
|
+
*
|
|
6
|
+
* The PTY transport runs the gate as a PreToolUse command hook that calls back
|
|
7
|
+
* into the bot over IPC. Here the CLI is driven through the agent SDK, which
|
|
8
|
+
* takes PreToolUse hooks as in-process callbacks, so the same decision is made
|
|
9
|
+
* by the same function (gateToolCall) without the round trip. A PreToolUse
|
|
10
|
+
* hook rather than canUseTool, for two reasons: canUseTool is only asked about
|
|
11
|
+
* calls the permission rules leave open, and the curated `allowedTools` list
|
|
12
|
+
* pre-approves Bash, Write and the rest; and a hook fires in every permission
|
|
13
|
+
* mode, bypass included, which is what the PTY hook does too — so a chat's
|
|
14
|
+
* /mode means the same thing on both transports.
|
|
15
|
+
*
|
|
16
|
+
* Hooks fire for a subagent's tool calls as well (their input carries an
|
|
17
|
+
* `agent_id`), so a Task or a background agent is gated like the main thread.
|
|
18
|
+
*/
|
|
19
|
+
/**
|
|
20
|
+
* The Telegram update behind each session's latest turn. A query can outlive
|
|
21
|
+
* the turn that started it — it is held open for background work, and the
|
|
22
|
+
* chat's next message joins it — and the gate has to prompt about, and name,
|
|
23
|
+
* whoever's turn it is now, as the PTY gate does through its registered turn.
|
|
24
|
+
*/
|
|
25
|
+
const turnContexts = new Map();
|
|
26
|
+
/**
|
|
27
|
+
* Record `ctx` as the update behind `sessionKey`'s current turn. A turn with
|
|
28
|
+
* none clears it, so the gate denies rather than prompting on behalf of an
|
|
29
|
+
* earlier turn's sender — as a PTY turn without a context does.
|
|
30
|
+
*/
|
|
31
|
+
export function noteTurnContext(sessionKey, ctx) {
|
|
32
|
+
if (ctx)
|
|
33
|
+
turnContexts.set(sessionKey, ctx);
|
|
34
|
+
else
|
|
35
|
+
turnContexts.delete(sessionKey);
|
|
36
|
+
}
|
|
37
|
+
/** Forget a session's turn context, for tests and a cleared conversation. */
|
|
38
|
+
export function forgetTurnContext(sessionKey) {
|
|
39
|
+
turnContexts.delete(sessionKey);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* The PreToolUse matcher that runs the gate for `sessionKey`'s query.
|
|
43
|
+
*
|
|
44
|
+
* Registered whether or not the gate is on: it is checked per call, so a bot
|
|
45
|
+
* that gains a guest mid-query gets the gate on its next tool call, and the
|
|
46
|
+
* hook set a held query is matched on stays the same either way.
|
|
47
|
+
*/
|
|
48
|
+
export function permissionGateHook(sessionKey) {
|
|
49
|
+
return {
|
|
50
|
+
// Long enough for an unanswered prompt to time out into the gate's own
|
|
51
|
+
// denial. A hook that times out is skipped, so the call would run.
|
|
52
|
+
timeout: gateHookTimeoutSeconds(),
|
|
53
|
+
hooks: [async (input) => {
|
|
54
|
+
if (input.hook_event_name !== 'PreToolUse')
|
|
55
|
+
return { continue: true };
|
|
56
|
+
const toolInput = input.tool_input;
|
|
57
|
+
const decision = await gateToolCall({
|
|
58
|
+
sessionKey,
|
|
59
|
+
toolName: String(input.tool_name ?? 'unknown'),
|
|
60
|
+
toolInput: toolInput && typeof toolInput === 'object'
|
|
61
|
+
? toolInput
|
|
62
|
+
: {},
|
|
63
|
+
telegramCtx: turnContexts.get(sessionKey),
|
|
64
|
+
});
|
|
65
|
+
if (!decision.block)
|
|
66
|
+
return { continue: true };
|
|
67
|
+
return {
|
|
68
|
+
hookSpecificOutput: {
|
|
69
|
+
hookEventName: 'PreToolUse',
|
|
70
|
+
permissionDecision: 'deny',
|
|
71
|
+
permissionDecisionReason: decision.reason,
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
}],
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
//# sourceMappingURL=sdk-permission-gate.js.map
|