@carrieros/cli-staging 0.0.0-stage → 0.4.1
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/README.md +6 -2
- package/dist/commands/attach.js +45 -0
- package/dist/commands/auth.js +60 -0
- package/dist/commands/btw.js +78 -0
- package/dist/commands/changes.js +167 -0
- package/dist/commands/chat.js +182 -0
- package/dist/commands/context.js +200 -0
- package/dist/commands/doctor.js +119 -0
- package/dist/commands/files.js +94 -0
- package/dist/commands/help.js +37 -0
- package/dist/commands/history.js +112 -0
- package/dist/commands/login.js +62 -0
- package/dist/commands/logout.js +26 -0
- package/dist/commands/memory.js +162 -0
- package/dist/commands/mode-model.js +64 -0
- package/dist/commands/models.js +28 -0
- package/dist/commands/modes.js +30 -0
- package/dist/commands/permissions.js +78 -0
- package/dist/commands/project.js +106 -0
- package/dist/commands/repo.js +146 -0
- package/dist/commands/repos.js +61 -0
- package/dist/commands/review.js +92 -0
- package/dist/commands/scope.js +155 -0
- package/dist/commands/search.js +92 -0
- package/dist/commands/sessions.js +282 -0
- package/dist/commands/settings.js +160 -0
- package/dist/commands/update.js +285 -0
- package/dist/commands/upload.js +223 -0
- package/dist/commands/usage.js +229 -0
- package/dist/commands/version.js +26 -0
- package/dist/commands/whoami.js +44 -0
- package/dist/ide/editor-context.js +275 -0
- package/dist/ide/ide-session.js +364 -0
- package/dist/ide/tool-host.js +109 -0
- package/dist/main.js +132 -0
- package/dist/services/api.js +441 -0
- package/dist/services/auth-origin.js +20 -0
- package/dist/services/auto-update.js +221 -0
- package/dist/services/chat-stream.js +168 -0
- package/dist/services/checkpoints.js +278 -0
- package/dist/services/client-tools.js +356 -0
- package/dist/services/code-intel.js +243 -0
- package/dist/services/code-symbols.js +94 -0
- package/dist/services/coding-workflow.js +554 -0
- package/dist/services/command-coordinator.js +273 -0
- package/dist/services/command-policy.js +434 -0
- package/dist/services/config.js +384 -0
- package/dist/services/container-runner.js +254 -0
- package/dist/services/container-supervisor.js +38 -0
- package/dist/services/context.js +416 -0
- package/dist/services/device-auth.js +288 -0
- package/dist/services/diff.js +196 -0
- package/dist/services/env-policy.js +108 -0
- package/dist/services/execution-platform.js +30 -0
- package/dist/services/file-detect.js +104 -0
- package/dist/services/file-resolver.js +199 -0
- package/dist/services/file-upload.js +126 -0
- package/dist/services/folder-key.js +22 -0
- package/dist/services/git-tools.js +429 -0
- package/dist/services/glob-match.js +164 -0
- package/dist/services/helper-bin.js +85 -0
- package/dist/services/ignore-rules.js +189 -0
- package/dist/services/instruction-resolver.js +54 -0
- package/dist/services/job-helper.js +328 -0
- package/dist/services/local-files.js +639 -0
- package/dist/services/local-tools.js +937 -0
- package/dist/services/login-flow.js +156 -0
- package/dist/services/meta.js +42 -0
- package/dist/services/mutation-coordinator.js +327 -0
- package/dist/services/mutation-groups.js +309 -0
- package/dist/services/mutation-recovery.js +159 -0
- package/dist/services/mutations.js +679 -0
- package/dist/services/open-url.js +68 -0
- package/dist/services/output.js +62 -0
- package/dist/services/packaged-update.js +403 -0
- package/dist/services/packaged.js +39 -0
- package/dist/services/permission-mode.js +78 -0
- package/dist/services/process-runner.js +412 -0
- package/dist/services/project-context.js +298 -0
- package/dist/services/redaction.js +201 -0
- package/dist/services/result-reconciliation.js +49 -0
- package/dist/services/secure-store.js +217 -0
- package/dist/services/session-resolver.js +115 -0
- package/dist/services/sia.js +51 -0
- package/dist/services/spinner.js +40 -0
- package/dist/services/supported-runtime.js +59 -0
- package/dist/services/task-context.js +212 -0
- package/dist/services/task-snapshot.js +38 -0
- package/dist/services/tool-result-outbox.js +195 -0
- package/dist/services/update.js +730 -0
- package/dist/services/workspace-registry.js +310 -0
- package/dist/services/workspace.js +580 -0
- package/dist/shell/shell-chat.js +1622 -0
- package/dist/shell/shell-commands.js +411 -0
- package/dist/shell/shell-prompt.js +110 -0
- package/dist/shell/shell-renderer.js +51 -0
- package/dist/shell/shell-router.js +37 -0
- package/dist/shell/shell.js +414 -0
- package/dist/shell/task-context-view.js +40 -0
- package/dist/staging.js +5 -0
- package/dist/types/index.js +6 -0
- package/dist/ui/agent-cards.js +111 -0
- package/dist/ui/agent-presentation.js +131 -0
- package/dist/ui/approval.js +111 -0
- package/dist/ui/banner.js +56 -0
- package/dist/ui/btw-overlay.js +230 -0
- package/dist/ui/change-review.js +334 -0
- package/dist/ui/chat-renderer.js +463 -0
- package/dist/ui/code-block.js +52 -0
- package/dist/ui/color-picker.js +100 -0
- package/dist/ui/colors.js +55 -0
- package/dist/ui/command-details.js +11 -0
- package/dist/ui/command-filter.js +92 -0
- package/dist/ui/command-item.js +6 -0
- package/dist/ui/command-palette.js +383 -0
- package/dist/ui/command-registry.js +128 -0
- package/dist/ui/command-renderer.js +55 -0
- package/dist/ui/command-review.js +187 -0
- package/dist/ui/composer-layout.js +74 -0
- package/dist/ui/conversation.js +450 -0
- package/dist/ui/interrupt.js +35 -0
- package/dist/ui/live.js +141 -0
- package/dist/ui/logo.js +58 -0
- package/dist/ui/markdown.js +205 -0
- package/dist/ui/message.js +21 -0
- package/dist/ui/screen-prompt.js +181 -0
- package/dist/ui/screen.js +297 -0
- package/dist/ui/select.js +284 -0
- package/dist/ui/sia-qa.js +213 -0
- package/dist/ui/spinner.js +23 -0
- package/dist/ui/sticky-stream.js +64 -0
- package/dist/ui/task-context-card.js +99 -0
- package/dist/ui/text-prompt.js +74 -0
- package/dist/ui/theme.js +80 -0
- package/dist/ui/trust.js +88 -0
- package/dist/ui/ui.js +101 -0
- package/package.json +33 -4
|
@@ -0,0 +1,434 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { isInside } from "./local-files.js";
|
|
4
|
+
import { MAX_TIMEOUT_MS, RunnerError, resolveCwd } from "./process-runner.js";
|
|
5
|
+
import { GIT_NO_HOOKS_PATH } from "./workspace.js";
|
|
6
|
+
export class PolicyError extends Error {
|
|
7
|
+
code;
|
|
8
|
+
constructor(code, message) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.code = code;
|
|
11
|
+
this.name = "PolicyError";
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
const deny = (message) => {
|
|
15
|
+
throw new PolicyError("COMMAND_DENIED", message);
|
|
16
|
+
};
|
|
17
|
+
const SHELL_OPERATOR = /[&|;<>`\r\n\0]|\$\(/;
|
|
18
|
+
/**
|
|
19
|
+
* The boundary, in the user's words, on every command that runs project code.
|
|
20
|
+
*
|
|
21
|
+
* Containment is real but narrow: the Job Object ends every process the command
|
|
22
|
+
* STARTS. It cannot govern work the command hands to something already running
|
|
23
|
+
* or to a Windows service — a document opened through a file association lands
|
|
24
|
+
* in an editor that was already open, a URL in the browser, a process created
|
|
25
|
+
* through WMI or the Task Scheduler is started by a service. None of those are
|
|
26
|
+
* the command's children, so none of them are in its job. R0 commands run no
|
|
27
|
+
* project code and do not get this note; R1 and R2 always do.
|
|
28
|
+
* See docs/SECURITY-local-execution.md.
|
|
29
|
+
*/
|
|
30
|
+
export const PROJECT_CODE_NOTE = "Project code runs as your Windows account and may open other applications — those are not contained by this command.";
|
|
31
|
+
const MIN_TIMEOUT_MS = 5_000;
|
|
32
|
+
const DEFAULT_TIMEOUT = { R0: 60_000, R1: 10 * 60_000, R2: 10 * 60_000 };
|
|
33
|
+
/** Named so the refusal says why, not just "not allowed". */
|
|
34
|
+
const KNOWN_DANGEROUS = {
|
|
35
|
+
cmd: "a shell",
|
|
36
|
+
powershell: "a shell",
|
|
37
|
+
pwsh: "a shell",
|
|
38
|
+
bash: "a shell",
|
|
39
|
+
sh: "a shell",
|
|
40
|
+
zsh: "a shell",
|
|
41
|
+
wsl: "a shell",
|
|
42
|
+
python: "an interpreter",
|
|
43
|
+
python3: "an interpreter",
|
|
44
|
+
py: "an interpreter",
|
|
45
|
+
ruby: "an interpreter",
|
|
46
|
+
perl: "an interpreter",
|
|
47
|
+
deno: "an interpreter",
|
|
48
|
+
npx: "a remote package runner",
|
|
49
|
+
curl: "a network tool",
|
|
50
|
+
wget: "a network tool",
|
|
51
|
+
"invoke-webrequest": "a network tool",
|
|
52
|
+
"invoke-restmethod": "a network tool",
|
|
53
|
+
"invoke-expression": "a shell command",
|
|
54
|
+
certutil: "a download tool",
|
|
55
|
+
bitsadmin: "a download tool",
|
|
56
|
+
rm: "a deletion command",
|
|
57
|
+
del: "a deletion command",
|
|
58
|
+
erase: "a deletion command",
|
|
59
|
+
rd: "a deletion command",
|
|
60
|
+
rmdir: "a deletion command",
|
|
61
|
+
format: "a system command",
|
|
62
|
+
reg: "a system command",
|
|
63
|
+
sc: "a system command",
|
|
64
|
+
schtasks: "a system command",
|
|
65
|
+
icacls: "a system command",
|
|
66
|
+
takeown: "a system command",
|
|
67
|
+
net: "a system command",
|
|
68
|
+
netsh: "a system command",
|
|
69
|
+
taskkill: "a system command",
|
|
70
|
+
msiexec: "an installer",
|
|
71
|
+
start: "a shell command",
|
|
72
|
+
explorer: "a launcher",
|
|
73
|
+
rundll32: "a launcher",
|
|
74
|
+
mshta: "a launcher",
|
|
75
|
+
cscript: "a script host",
|
|
76
|
+
wscript: "a script host"
|
|
77
|
+
};
|
|
78
|
+
// ── Shared checks ────────────────────────────────────────────────────────────
|
|
79
|
+
function asArgs(value) {
|
|
80
|
+
if (value === undefined)
|
|
81
|
+
return [];
|
|
82
|
+
if (!Array.isArray(value) || value.some((a) => typeof a !== "string")) {
|
|
83
|
+
throw new PolicyError("INVALID_COMMAND", "args must be an array of strings.");
|
|
84
|
+
}
|
|
85
|
+
if (value.length > 32)
|
|
86
|
+
throw new PolicyError("INVALID_COMMAND", "Too many arguments.");
|
|
87
|
+
for (const a of value) {
|
|
88
|
+
if (a.length > 1024)
|
|
89
|
+
throw new PolicyError("INVALID_COMMAND", "An argument is too long.");
|
|
90
|
+
if (SHELL_OPERATOR.test(a))
|
|
91
|
+
deny(`Shell syntax is not accepted: ${JSON.stringify(a.slice(0, 60))}`);
|
|
92
|
+
}
|
|
93
|
+
return value;
|
|
94
|
+
}
|
|
95
|
+
/** A positional argument that names a path must stay inside the project. */
|
|
96
|
+
function checkPathArg(arg, cwdAbs, root) {
|
|
97
|
+
if (/^[A-Za-z]:/.test(arg) || arg.startsWith("\\\\") || arg.startsWith("/")) {
|
|
98
|
+
const abs = path.resolve(arg);
|
|
99
|
+
if (!isInside(root, abs)) {
|
|
100
|
+
throw new PolicyError("OUTSIDE_WORKSPACE", `${arg} is outside the project.`);
|
|
101
|
+
}
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
if (!/[\\/]/.test(arg) && arg !== "..")
|
|
105
|
+
return; // a bare name or a revision
|
|
106
|
+
const abs = path.resolve(cwdAbs, arg);
|
|
107
|
+
if (!isInside(root, abs))
|
|
108
|
+
throw new PolicyError("OUTSIDE_WORKSPACE", `${arg} is outside the project.`);
|
|
109
|
+
try {
|
|
110
|
+
const real = fs.realpathSync.native(abs);
|
|
111
|
+
if (!isInside(fs.realpathSync.native(root), real)) {
|
|
112
|
+
throw new PolicyError("OUTSIDE_WORKSPACE", `${arg} links outside the project.`);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
catch (err) {
|
|
116
|
+
if (err instanceof PolicyError)
|
|
117
|
+
throw err;
|
|
118
|
+
/* does not exist (yet) — the lexical check above is what applies */
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
function timeoutFor(tier, requested) {
|
|
122
|
+
if (requested === undefined || requested === null)
|
|
123
|
+
return DEFAULT_TIMEOUT[tier];
|
|
124
|
+
if (typeof requested !== "number" || !Number.isInteger(requested)) {
|
|
125
|
+
throw new PolicyError("INVALID_COMMAND", "timeout_ms must be an integer.");
|
|
126
|
+
}
|
|
127
|
+
return Math.min(MAX_TIMEOUT_MS, Math.max(MIN_TIMEOUT_MS, requested));
|
|
128
|
+
}
|
|
129
|
+
// ── git ──────────────────────────────────────────────────────────────────────
|
|
130
|
+
/** Global hardening, placed before the subcommand. */
|
|
131
|
+
const GIT_GLOBAL = ["--no-pager", "-c", "core.fsmonitor=false", "-c", `core.hooksPath=${GIT_NO_HOOKS_PATH}`, "-c", "diff.external="];
|
|
132
|
+
/** Never allowed in any git invocation: each reads or writes outside the command's intent. */
|
|
133
|
+
const GIT_FORBIDDEN = /^(-c|-C|--git-dir|--work-tree|--exec-path|--namespace|--super-prefix|--config-env|--output|--ext-diff|--textconv|--no-index|--upload-pack|--receive-pack|--open-files-in-pager|-O)(=|$)/;
|
|
134
|
+
const GIT_FLAGS = {
|
|
135
|
+
status: /^(-s|--short|-b|--branch|--porcelain(=v[12])?|--long|-u(no|normal|all)?|--untracked-files(=(no|normal|all))?|--ignored(=(traditional|no|matching))?|--ignore-submodules(=(none|untracked|dirty|all))?|-z|--no-renames|--renames|--ahead-behind|--no-ahead-behind|-v|--verbose)$/,
|
|
136
|
+
diff: /^(--stat(=\d+(,\d+)?)?|--numstat|--shortstat|--dirstat|--name-only|--name-status|--summary|--cached|--staged|-U\d+|--unified=\d+|-w|--ignore-all-space|-b|--ignore-space-change|--ignore-blank-lines|--word-diff(=(color|plain|porcelain|none))?|--color=never|--no-color|-p|--patch|-s|--no-patch|--minimal|--patience|--histogram|--diff-filter=[ACDMRTUXB*a-z]+|-M\d*%?|--find-renames(=\d+%?)?|--relative(=.+)?|--check|--exit-code|--quiet|-R|--merge-base|--)$/,
|
|
137
|
+
log: /^(--oneline|-n\d*|-\d+|--max-count=\d+|--skip=\d+|--since=.+|--after=.+|--until=.+|--before=.+|--author=.+|--committer=.+|--grep=.+|-i|--regexp-ignore-case|--stat(=\d+)?|--numstat|--shortstat|--name-only|--name-status|-p|--patch|--graph|--decorate(=(short|full|no))?|--no-decorate|--all|--branches(=.+)?|--tags(=.+)?|--remotes(=.+)?|--format=.+|--pretty(=.+)?|--abbrev-commit|--no-abbrev-commit|--date=.+|--follow|--reverse|--first-parent|--no-merges|--merges|--color=never|--no-color|-U\d+|--)$/,
|
|
138
|
+
show: /^(--stat(=\d+)?|--numstat|--shortstat|--name-only|--name-status|--oneline|--format=.+|--pretty(=.+)?|-p|--patch|-s|--no-patch|--abbrev-commit|--date=.+|--color=never|--no-color|-U\d+|--)$/,
|
|
139
|
+
branch: /^(--list|-l|-a|--all|-r|--remotes|-v|-vv|--verbose|--show-current|--no-color|--color=never|--contains(=.+)?|--no-contains(=.+)?|--merged(=.+)?|--no-merged(=.+)?|--sort=.+|--format=.+|--points-at(=.+)?)$/
|
|
140
|
+
};
|
|
141
|
+
const GIT_WRITE_SUBCOMMANDS = new Set([
|
|
142
|
+
"commit", "push", "pull", "fetch", "reset", "checkout", "switch", "restore", "clean", "rebase", "merge",
|
|
143
|
+
"cherry-pick", "revert", "am", "apply", "stash", "tag", "config", "remote", "rm", "mv", "add", "init",
|
|
144
|
+
"clone", "submodule", "worktree", "gc", "prune", "filter-branch", "update-ref", "update-index", "notes",
|
|
145
|
+
"bisect", "lfs", "credential", "daemon", "archive", "bundle", "format-patch", "send-email", "instaweb"
|
|
146
|
+
]);
|
|
147
|
+
function evaluateGit(args, cwdAbs, root) {
|
|
148
|
+
const sub = args[0];
|
|
149
|
+
if (!sub)
|
|
150
|
+
deny("A git subcommand is required.");
|
|
151
|
+
if (sub.startsWith("-"))
|
|
152
|
+
deny("git options before the subcommand are not allowed.");
|
|
153
|
+
if (GIT_WRITE_SUBCOMMANDS.has(sub))
|
|
154
|
+
deny(`git ${sub} can change the repository or reach the network, and is not allowed.`);
|
|
155
|
+
const flags = GIT_FLAGS[sub];
|
|
156
|
+
if (!flags)
|
|
157
|
+
deny(`git ${sub} is not in the allowed read-only set (status, diff, log, show, branch --list).`);
|
|
158
|
+
const rest = args.slice(1);
|
|
159
|
+
let afterDashDash = false;
|
|
160
|
+
let positional = 0;
|
|
161
|
+
for (const a of rest) {
|
|
162
|
+
if (!afterDashDash && a === "--") {
|
|
163
|
+
afterDashDash = true;
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
if (!afterDashDash && a.startsWith("-")) {
|
|
167
|
+
if (GIT_FORBIDDEN.test(a))
|
|
168
|
+
deny(`git ${a} is not allowed.`);
|
|
169
|
+
if (!flags.test(a))
|
|
170
|
+
deny(`git ${sub} ${a} is not in the allowed set of options.`);
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
positional += 1;
|
|
174
|
+
checkPathArg(a, cwdAbs, root);
|
|
175
|
+
}
|
|
176
|
+
if (sub === "branch" && positional > 0 && !rest.some((a) => a === "--list" || a === "-l")) {
|
|
177
|
+
deny("git branch with a name creates a branch; only listing (--list) is allowed.");
|
|
178
|
+
}
|
|
179
|
+
const readsFiles = ["diff", "log", "show"].includes(sub);
|
|
180
|
+
return {
|
|
181
|
+
tier: "R0",
|
|
182
|
+
program: "git",
|
|
183
|
+
args,
|
|
184
|
+
effectiveArgs: [...GIT_GLOBAL, sub, ...(readsFiles ? ["--no-ext-diff", "--no-textconv"] : []), ...rest],
|
|
185
|
+
network: "none-expected",
|
|
186
|
+
notes: ["Read-only Git command."]
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
// ── node ─────────────────────────────────────────────────────────────────────
|
|
190
|
+
function evaluateNode(args, cwdAbs, root) {
|
|
191
|
+
if (args.length === 1 && (args[0] === "--version" || args[0] === "-v")) {
|
|
192
|
+
return { tier: "R0", program: "node", args, effectiveArgs: args, network: "none-expected", notes: ["Prints the Node.js version."] };
|
|
193
|
+
}
|
|
194
|
+
const [scriptArg, ...rest] = args;
|
|
195
|
+
if (!scriptArg)
|
|
196
|
+
deny("node needs a script file from this project.");
|
|
197
|
+
if (scriptArg.startsWith("-"))
|
|
198
|
+
deny(`node ${scriptArg} is not allowed — only running a script file from this project.`);
|
|
199
|
+
if (!/\.(c|m)?js$/i.test(scriptArg))
|
|
200
|
+
deny("node may only run a .js, .cjs or .mjs file.");
|
|
201
|
+
checkPathArg(scriptArg.includes("/") || scriptArg.includes("\\") ? scriptArg : `./${scriptArg}`, cwdAbs, root);
|
|
202
|
+
const abs = path.resolve(cwdAbs, scriptArg);
|
|
203
|
+
if (!fs.existsSync(abs) || !fs.statSync(abs).isFile()) {
|
|
204
|
+
throw new PolicyError("SCRIPT_NOT_FOUND", `${scriptArg} does not exist in this project.`);
|
|
205
|
+
}
|
|
206
|
+
for (const a of rest)
|
|
207
|
+
checkPathArg(a, cwdAbs, root);
|
|
208
|
+
return {
|
|
209
|
+
tier: "R1",
|
|
210
|
+
program: "node",
|
|
211
|
+
args,
|
|
212
|
+
effectiveArgs: args,
|
|
213
|
+
network: "project-code-may-use-network",
|
|
214
|
+
notes: ["Runs a script from this project — project code runs with your permissions."]
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
function nearestPackageJson(cwdAbs, root) {
|
|
218
|
+
let dir = cwdAbs;
|
|
219
|
+
for (;;) {
|
|
220
|
+
const file = path.join(dir, "package.json");
|
|
221
|
+
try {
|
|
222
|
+
if (fs.statSync(file).isFile() && fs.statSync(file).size < 1024 * 1024) {
|
|
223
|
+
return { file, data: JSON.parse(fs.readFileSync(file, "utf8")) };
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
catch {
|
|
227
|
+
/* none or unreadable here */
|
|
228
|
+
}
|
|
229
|
+
// The platform's own path comparison (case-insensitive only on Windows), as isInside.
|
|
230
|
+
if (path.relative(path.resolve(root), path.resolve(dir)) === "")
|
|
231
|
+
return null;
|
|
232
|
+
const parent = path.dirname(dir);
|
|
233
|
+
if (parent === dir || !isInside(root, parent))
|
|
234
|
+
return null;
|
|
235
|
+
dir = parent;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
function projectUses(manager, root, pkg) {
|
|
239
|
+
const lock = manager === "pnpm" ? "pnpm-lock.yaml" : "yarn.lock";
|
|
240
|
+
if (fs.existsSync(path.join(root, lock)))
|
|
241
|
+
return true;
|
|
242
|
+
return typeof pkg?.packageManager === "string" && pkg.packageManager.startsWith(`${manager}@`);
|
|
243
|
+
}
|
|
244
|
+
/** Scripts named for publishing or deploying are never run from here. */
|
|
245
|
+
const DENIED_SCRIPT_NAME = /(^|[:_-])(publish|prepublish|prepublishonly|postpublish|release|deploy|login|logout|adduser)($|[:_-])/i;
|
|
246
|
+
/** Script names that usually rewrite files. */
|
|
247
|
+
const WRITING_SCRIPT_NAME = /(^|[:_-])(format|fmt|fix|prettier|generate|gen|codegen|migrate|migration|seed|install|clean|bump|version|update|upgrade|snapshot)($|[:_-])/i;
|
|
248
|
+
/** Script text that pushes, publishes or deploys: refused. */
|
|
249
|
+
const DENIED_SCRIPT_TEXT = /\b(npm\s+publish|yarn\s+publish|pnpm\s+publish|git\s+push|git\s+commit|git\s+tag|gh\s+release|vercel\s+(--prod|deploy)|netlify\s+deploy|firebase\s+deploy|aws\s+s3\s+(cp|sync)|docker\s+push|twine\s+upload)\b/i;
|
|
250
|
+
/** Script text that writes files or installs: at least R2. */
|
|
251
|
+
const WRITING_SCRIPT_TEXT = /(--write\b|--fix\b|\brimraf\b|\brm\s+-|\bdel\s+\/|\bRemove-Item\b|\bmigrate\b|\bprisma\s+(db|migrate|generate)\b|\bnpm\s+(i|install|ci|update)\b|\byarn\s+(add|install)\b|\bpnpm\s+(add|install)\b|\bnpx\b|\bcurl\b|\bwget\b)/i;
|
|
252
|
+
function evaluatePackageManager(manager, args, cwdAbs, root) {
|
|
253
|
+
if (args.length === 1 && (args[0] === "--version" || args[0] === "-v")) {
|
|
254
|
+
return { tier: "R0", program: manager, args, effectiveArgs: args, network: "none-expected", notes: [`Prints the ${manager} version.`] };
|
|
255
|
+
}
|
|
256
|
+
const pkg = nearestPackageJson(cwdAbs, root);
|
|
257
|
+
if (manager !== "npm" && !projectUses(manager, root, pkg?.data)) {
|
|
258
|
+
deny(`This project does not use ${manager}.`);
|
|
259
|
+
}
|
|
260
|
+
const [verb, ...rest] = args;
|
|
261
|
+
const installVerbs = {
|
|
262
|
+
npm: ["install", "i", "ci", "update"],
|
|
263
|
+
pnpm: ["install", "i", "update"],
|
|
264
|
+
yarn: ["install", "upgrade"]
|
|
265
|
+
};
|
|
266
|
+
if (verb && installVerbs[manager].includes(verb)) {
|
|
267
|
+
if (rest.length > 0)
|
|
268
|
+
deny(`${manager} ${verb} with extra arguments is not allowed — only installing what the project already declares.`);
|
|
269
|
+
if (!pkg)
|
|
270
|
+
throw new PolicyError("SCRIPT_NOT_FOUND", "There is no package.json here.");
|
|
271
|
+
return {
|
|
272
|
+
tier: "R2",
|
|
273
|
+
program: manager,
|
|
274
|
+
args,
|
|
275
|
+
effectiveArgs: args,
|
|
276
|
+
network: "uses-network",
|
|
277
|
+
notes: [
|
|
278
|
+
"Changes files: node_modules and possibly the lockfile.",
|
|
279
|
+
"Uses the network to download packages, and installs can run package scripts.",
|
|
280
|
+
"/undo cannot restore what this command changes."
|
|
281
|
+
]
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
let scriptName;
|
|
285
|
+
if (verb === "test" || verb === "t") {
|
|
286
|
+
if (rest.length)
|
|
287
|
+
deny(`${manager} test with extra arguments is not allowed.`);
|
|
288
|
+
scriptName = "test";
|
|
289
|
+
}
|
|
290
|
+
else if (verb === "run" || verb === "run-script") {
|
|
291
|
+
if (rest.length !== 1)
|
|
292
|
+
deny(`${manager} ${verb} takes exactly one script name and no extra arguments.`);
|
|
293
|
+
scriptName = rest[0];
|
|
294
|
+
}
|
|
295
|
+
else {
|
|
296
|
+
deny(`${manager} ${verb ?? ""} is not allowed. Allowed: --version, test, run <script>, and install.`.replace(" ", " "));
|
|
297
|
+
}
|
|
298
|
+
if (!scriptName || !/^[A-Za-z0-9:_.-]{1,80}$/.test(scriptName))
|
|
299
|
+
deny("That is not a valid script name.");
|
|
300
|
+
if (!pkg)
|
|
301
|
+
throw new PolicyError("SCRIPT_NOT_FOUND", "There is no package.json here.");
|
|
302
|
+
const scripts = pkg.data.scripts ?? {};
|
|
303
|
+
const text = scripts[scriptName];
|
|
304
|
+
if (typeof text !== "string") {
|
|
305
|
+
throw new PolicyError("SCRIPT_NOT_FOUND", `package.json has no "${scriptName}" script.`);
|
|
306
|
+
}
|
|
307
|
+
const pre = typeof scripts[`pre${scriptName}`] === "string" ? scripts[`pre${scriptName}`] : undefined;
|
|
308
|
+
const post = typeof scripts[`post${scriptName}`] === "string" ? scripts[`post${scriptName}`] : undefined;
|
|
309
|
+
const allText = [pre, text, post].filter(Boolean).join("\n");
|
|
310
|
+
if (DENIED_SCRIPT_NAME.test(scriptName))
|
|
311
|
+
deny(`The "${scriptName}" script publishes or deploys, and is not run from here.`);
|
|
312
|
+
if (DENIED_SCRIPT_TEXT.test(allText))
|
|
313
|
+
deny(`The "${scriptName}" script pushes, publishes or deploys, and is not run from here.`);
|
|
314
|
+
const writes = WRITING_SCRIPT_NAME.test(scriptName) || WRITING_SCRIPT_TEXT.test(allText);
|
|
315
|
+
const tier = writes ? "R2" : "R1";
|
|
316
|
+
const notes = ["Runs project code with your permissions."];
|
|
317
|
+
if (writes)
|
|
318
|
+
notes.push("This script looks like it changes files.", "/undo cannot restore what this command changes.");
|
|
319
|
+
if (pre || post)
|
|
320
|
+
notes.push(`Also runs ${[pre && `pre${scriptName}`, post && `post${scriptName}`].filter(Boolean).join(" and ")}.`);
|
|
321
|
+
return {
|
|
322
|
+
tier,
|
|
323
|
+
program: manager,
|
|
324
|
+
args,
|
|
325
|
+
effectiveArgs: verb === "t" ? ["test"] : args,
|
|
326
|
+
network: writes ? "uses-network" : "project-code-may-use-network",
|
|
327
|
+
script: {
|
|
328
|
+
manager,
|
|
329
|
+
name: scriptName,
|
|
330
|
+
text,
|
|
331
|
+
...(pre ? { pre } : {}),
|
|
332
|
+
...(post ? { post } : {}),
|
|
333
|
+
packageJson: path.relative(root, pkg.file).split(path.sep).join("/") || "package.json"
|
|
334
|
+
},
|
|
335
|
+
notes
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
// ── Entry point ──────────────────────────────────────────────────────────────
|
|
339
|
+
/**
|
|
340
|
+
* Decide on one proposed command. Returns the decision for the approval prompt,
|
|
341
|
+
* or throws PolicyError — in which case the user is never asked.
|
|
342
|
+
*/
|
|
343
|
+
export function evaluateCommand(proposal, root) {
|
|
344
|
+
if (!proposal || typeof proposal !== "object" || Array.isArray(proposal)) {
|
|
345
|
+
throw new PolicyError("INVALID_COMMAND", "A command must be an object with program and args.");
|
|
346
|
+
}
|
|
347
|
+
for (const raw of ["command", "cmd", "shell", "script", "commandLine", "command_line"]) {
|
|
348
|
+
if (raw in proposal)
|
|
349
|
+
throw new PolicyError("INVALID_COMMAND", "Raw command strings are not accepted — use program and args.");
|
|
350
|
+
}
|
|
351
|
+
const { program } = proposal;
|
|
352
|
+
if (typeof program !== "string" || !program.trim())
|
|
353
|
+
throw new PolicyError("INVALID_COMMAND", "program is required.");
|
|
354
|
+
if (/\s/.test(program.trim())) {
|
|
355
|
+
throw new PolicyError("INVALID_COMMAND", "program must be a single name — put arguments in args.");
|
|
356
|
+
}
|
|
357
|
+
const args = asArgs(proposal.args);
|
|
358
|
+
const lower = program.trim().toLowerCase();
|
|
359
|
+
if (/[\\/]/.test(lower) || /^[a-z]:/.test(lower))
|
|
360
|
+
deny("program must be a bare name, not a path.");
|
|
361
|
+
const ext = path.extname(lower);
|
|
362
|
+
if (ext === ".cmd" || ext === ".bat" || ext === ".ps1" || ext === ".vbs" || ext === ".js" || ext === ".sh") {
|
|
363
|
+
deny(`${program} is a script file and cannot be run directly.`);
|
|
364
|
+
}
|
|
365
|
+
const name = ext === ".exe" ? lower.slice(0, -4) : lower;
|
|
366
|
+
if (KNOWN_DANGEROUS[name] || name.startsWith("invoke-")) {
|
|
367
|
+
deny(`${program} is ${KNOWN_DANGEROUS[name] ?? "a shell command"} and is never run from here.`);
|
|
368
|
+
}
|
|
369
|
+
let cwdAbsolute;
|
|
370
|
+
const cwdRaw = proposal.cwd === undefined || proposal.cwd === null || proposal.cwd === "" ? "." : proposal.cwd;
|
|
371
|
+
if (typeof cwdRaw !== "string")
|
|
372
|
+
throw new PolicyError("INVALID_COMMAND", "cwd must be a string.");
|
|
373
|
+
try {
|
|
374
|
+
cwdAbsolute = resolveCwd(cwdRaw, root);
|
|
375
|
+
}
|
|
376
|
+
catch (err) {
|
|
377
|
+
if (err instanceof RunnerError) {
|
|
378
|
+
throw new PolicyError(err.code === "OUTSIDE_WORKSPACE" ? "OUTSIDE_WORKSPACE" : "INVALID_COMMAND", err.message);
|
|
379
|
+
}
|
|
380
|
+
throw err;
|
|
381
|
+
}
|
|
382
|
+
// resolveCwd returns a real path; compare positional paths against the same root.
|
|
383
|
+
root = fs.realpathSync.native(root);
|
|
384
|
+
const cwdRelative = path.relative(fs.realpathSync.native(root), cwdAbsolute).split(path.sep).join("/") || ".";
|
|
385
|
+
let partial;
|
|
386
|
+
switch (name) {
|
|
387
|
+
case "git":
|
|
388
|
+
partial = evaluateGit(args, cwdAbsolute, root);
|
|
389
|
+
break;
|
|
390
|
+
case "node":
|
|
391
|
+
partial = evaluateNode(args, cwdAbsolute, root);
|
|
392
|
+
break;
|
|
393
|
+
case "npm":
|
|
394
|
+
case "pnpm":
|
|
395
|
+
case "yarn":
|
|
396
|
+
partial = evaluatePackageManager(name, args, cwdAbsolute, root);
|
|
397
|
+
break;
|
|
398
|
+
case "bun":
|
|
399
|
+
case "pytest":
|
|
400
|
+
case "go":
|
|
401
|
+
case "cargo": {
|
|
402
|
+
const version = args.length === 1 && args[0] === (name === "go" ? "version" : "--version");
|
|
403
|
+
const expected = name === "pytest" ? [] : name === "go" ? ["test", "./..."] : ["test"];
|
|
404
|
+
// No eval, arbitrary scripts, flags, install/publish or external paths.
|
|
405
|
+
if (!version && JSON.stringify(args) !== JSON.stringify(expected))
|
|
406
|
+
return deny(`${name} may only run its project tests or its version query.`);
|
|
407
|
+
const markers = name === "go" ? ["go.mod"] : name === "cargo" ? ["Cargo.toml"] : name === "bun" ? ["package.json"] : ["pyproject.toml", "pytest.ini", "setup.cfg", "setup.py"];
|
|
408
|
+
if (!version && !markers.some(file => {
|
|
409
|
+
// Same canonical form as root (realpathSync.native): 8.3, subst and mapped
|
|
410
|
+
// drives resolve alike, and a marker that links out still fails.
|
|
411
|
+
try {
|
|
412
|
+
const real = fs.realpathSync.native(path.join(cwdAbsolute, file));
|
|
413
|
+
return isInside(root, real) && fs.statSync(real).isFile();
|
|
414
|
+
}
|
|
415
|
+
catch {
|
|
416
|
+
return false;
|
|
417
|
+
}
|
|
418
|
+
}))
|
|
419
|
+
return deny(`${name} requires project configuration in the selected working directory.`);
|
|
420
|
+
partial = { tier: version ? "R0" : "R1", program: name, args, effectiveArgs: args,
|
|
421
|
+
network: version ? "none-expected" : "project-code-may-use-network",
|
|
422
|
+
notes: version ? [] : ["Runs project tests, including project-defined configuration and plugins."] };
|
|
423
|
+
break;
|
|
424
|
+
}
|
|
425
|
+
default:
|
|
426
|
+
return deny(`${program} is not in the allowed command set (git, node, npm, pnpm, yarn, bun, pytest, go, cargo).`);
|
|
427
|
+
}
|
|
428
|
+
const notes = partial.tier === "R0" ? partial.notes : [...partial.notes, PROJECT_CODE_NOTE];
|
|
429
|
+
return { ...partial, notes, cwdRelative, cwdAbsolute, timeoutMs: timeoutFor(partial.tier, proposal.timeout_ms) };
|
|
430
|
+
}
|
|
431
|
+
/** One line for display: `npm run build`. Arguments with spaces are quoted for reading only. */
|
|
432
|
+
export function displayCommand(program, args) {
|
|
433
|
+
return [program, ...args.map((a) => (/[\s"]/.test(a) || a === "" ? JSON.stringify(a) : a))].join(" ");
|
|
434
|
+
}
|