seance-cli 0.0.0-stage → 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 +40 -2
- package/dist/commands/init.d.ts +29 -0
- package/dist/commands/init.js +310 -0
- package/dist/commands/init.js.map +1 -0
- package/dist/commands/mcp.d.ts +19 -0
- package/dist/commands/mcp.js +38 -0
- package/dist/commands/mcp.js.map +1 -0
- package/dist/commands/quiz.d.ts +23 -0
- package/dist/commands/quiz.js +187 -0
- package/dist/commands/quiz.js.map +1 -0
- package/dist/commands/review.d.ts +4 -0
- package/dist/commands/review.js +115 -0
- package/dist/commands/review.js.map +1 -0
- package/dist/commands/sync.d.ts +17 -0
- package/dist/commands/sync.js +367 -0
- package/dist/commands/sync.js.map +1 -0
- package/dist/git.d.ts +33 -0
- package/dist/git.js +103 -0
- package/dist/git.js.map +1 -0
- package/dist/hosted-api.d.ts +24 -0
- package/dist/hosted-api.js +85 -0
- package/dist/hosted-api.js.map +1 -0
- package/dist/hosted-consent.d.ts +8 -0
- package/dist/hosted-consent.js +37 -0
- package/dist/hosted-consent.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +108 -0
- package/dist/index.js.map +1 -0
- package/dist/init-fs.d.ts +51 -0
- package/dist/init-fs.js +220 -0
- package/dist/init-fs.js.map +1 -0
- package/dist/init-questions.d.ts +21 -0
- package/dist/init-questions.js +161 -0
- package/dist/init-questions.js.map +1 -0
- package/dist/mcp-handlers.d.ts +22 -0
- package/dist/mcp-handlers.js +185 -0
- package/dist/mcp-handlers.js.map +1 -0
- package/dist/mcp-server.d.ts +4 -0
- package/dist/mcp-server.js +39 -0
- package/dist/mcp-server.js.map +1 -0
- package/dist/prompter.d.ts +29 -0
- package/dist/prompter.js +42 -0
- package/dist/prompter.js.map +1 -0
- package/dist/quiz-backend.d.ts +40 -0
- package/dist/quiz-backend.js +116 -0
- package/dist/quiz-backend.js.map +1 -0
- package/dist/quiz-input.d.ts +12 -0
- package/dist/quiz-input.js +52 -0
- package/dist/quiz-input.js.map +1 -0
- package/dist/quiz-io.d.ts +30 -0
- package/dist/quiz-io.js +83 -0
- package/dist/quiz-io.js.map +1 -0
- package/dist/quiz-ui.d.ts +18 -0
- package/dist/quiz-ui.js +51 -0
- package/dist/quiz-ui.js.map +1 -0
- package/node_modules/@seance/ai/LICENSE +21 -0
- package/node_modules/@seance/ai/dist/chain.d.ts +31 -0
- package/node_modules/@seance/ai/dist/chain.js +114 -0
- package/node_modules/@seance/ai/dist/chain.js.map +1 -0
- package/node_modules/@seance/ai/dist/grade.d.ts +14 -0
- package/node_modules/@seance/ai/dist/grade.js +44 -0
- package/node_modules/@seance/ai/dist/grade.js.map +1 -0
- package/node_modules/@seance/ai/dist/hosted-config.d.ts +9 -0
- package/node_modules/@seance/ai/dist/hosted-config.js +27 -0
- package/node_modules/@seance/ai/dist/hosted-config.js.map +1 -0
- package/node_modules/@seance/ai/dist/index.d.ts +9 -0
- package/node_modules/@seance/ai/dist/index.js +9 -0
- package/node_modules/@seance/ai/dist/index.js.map +1 -0
- package/node_modules/@seance/ai/dist/providers.d.ts +54 -0
- package/node_modules/@seance/ai/dist/providers.js +149 -0
- package/node_modules/@seance/ai/dist/providers.js.map +1 -0
- package/node_modules/@seance/ai/dist/quiz.d.ts +26 -0
- package/node_modules/@seance/ai/dist/quiz.js +41 -0
- package/node_modules/@seance/ai/dist/quiz.js.map +1 -0
- package/node_modules/@seance/ai/package.json +25 -0
- package/node_modules/@seance/core/LICENSE +21 -0
- package/node_modules/@seance/core/dist/ai-check.d.ts +58 -0
- package/node_modules/@seance/core/dist/ai-check.js +460 -0
- package/node_modules/@seance/core/dist/ai-check.js.map +1 -0
- package/node_modules/@seance/core/dist/ai-prompts.d.ts +29 -0
- package/node_modules/@seance/core/dist/ai-prompts.js +221 -0
- package/node_modules/@seance/core/dist/ai-prompts.js.map +1 -0
- package/node_modules/@seance/core/dist/ai-schemas.d.ts +155 -0
- package/node_modules/@seance/core/dist/ai-schemas.js +92 -0
- package/node_modules/@seance/core/dist/ai-schemas.js.map +1 -0
- package/node_modules/@seance/core/dist/api-client.d.ts +26 -0
- package/node_modules/@seance/core/dist/api-client.js +71 -0
- package/node_modules/@seance/core/dist/api-client.js.map +1 -0
- package/node_modules/@seance/core/dist/api-errors.d.ts +37 -0
- package/node_modules/@seance/core/dist/api-errors.js +66 -0
- package/node_modules/@seance/core/dist/api-errors.js.map +1 -0
- package/node_modules/@seance/core/dist/api-schemas.d.ts +284 -0
- package/node_modules/@seance/core/dist/api-schemas.js +67 -0
- package/node_modules/@seance/core/dist/api-schemas.js.map +1 -0
- package/node_modules/@seance/core/dist/decision-log.d.ts +73 -0
- package/node_modules/@seance/core/dist/decision-log.js +239 -0
- package/node_modules/@seance/core/dist/decision-log.js.map +1 -0
- package/node_modules/@seance/core/dist/dependency-guard.d.ts +20 -0
- package/node_modules/@seance/core/dist/dependency-guard.js +732 -0
- package/node_modules/@seance/core/dist/dependency-guard.js.map +1 -0
- package/node_modules/@seance/core/dist/gate.d.ts +11 -0
- package/node_modules/@seance/core/dist/gate.js +26 -0
- package/node_modules/@seance/core/dist/gate.js.map +1 -0
- package/node_modules/@seance/core/dist/git-parse.d.ts +11 -0
- package/node_modules/@seance/core/dist/git-parse.js +23 -0
- package/node_modules/@seance/core/dist/git-parse.js.map +1 -0
- package/node_modules/@seance/core/dist/index.d.ts +65 -0
- package/node_modules/@seance/core/dist/index.js +47 -0
- package/node_modules/@seance/core/dist/index.js.map +1 -0
- package/node_modules/@seance/core/dist/init-checklist.d.ts +153 -0
- package/node_modules/@seance/core/dist/init-checklist.js +200 -0
- package/node_modules/@seance/core/dist/init-checklist.js.map +1 -0
- package/node_modules/@seance/core/dist/init-detect.d.ts +9 -0
- package/node_modules/@seance/core/dist/init-detect.js +84 -0
- package/node_modules/@seance/core/dist/init-detect.js.map +1 -0
- package/node_modules/@seance/core/dist/init-templates.d.ts +36 -0
- package/node_modules/@seance/core/dist/init-templates.js +257 -0
- package/node_modules/@seance/core/dist/init-templates.js.map +1 -0
- package/node_modules/@seance/core/dist/map-basics.d.ts +13 -0
- package/node_modules/@seance/core/dist/map-basics.js +19 -0
- package/node_modules/@seance/core/dist/map-basics.js.map +1 -0
- package/node_modules/@seance/core/dist/map-summary.d.ts +5 -0
- package/node_modules/@seance/core/dist/map-summary.js +50 -0
- package/node_modules/@seance/core/dist/map-summary.js.map +1 -0
- package/node_modules/@seance/core/dist/markers.d.ts +51 -0
- package/node_modules/@seance/core/dist/markers.js +84 -0
- package/node_modules/@seance/core/dist/markers.js.map +1 -0
- package/node_modules/@seance/core/dist/mcp-quiz.d.ts +94 -0
- package/node_modules/@seance/core/dist/mcp-quiz.js +130 -0
- package/node_modules/@seance/core/dist/mcp-quiz.js.map +1 -0
- package/node_modules/@seance/core/dist/mcp-tools.d.ts +82 -0
- package/node_modules/@seance/core/dist/mcp-tools.js +136 -0
- package/node_modules/@seance/core/dist/mcp-tools.js.map +1 -0
- package/node_modules/@seance/core/dist/mcp-views.d.ts +21 -0
- package/node_modules/@seance/core/dist/mcp-views.js +83 -0
- package/node_modules/@seance/core/dist/mcp-views.js.map +1 -0
- package/node_modules/@seance/core/dist/quiz-assembly.d.ts +21 -0
- package/node_modules/@seance/core/dist/quiz-assembly.js +43 -0
- package/node_modules/@seance/core/dist/quiz-assembly.js.map +1 -0
- package/node_modules/@seance/core/dist/quiz-cards.d.ts +183 -0
- package/node_modules/@seance/core/dist/quiz-cards.js +215 -0
- package/node_modules/@seance/core/dist/quiz-cards.js.map +1 -0
- package/node_modules/@seance/core/dist/quiz-session.d.ts +67 -0
- package/node_modules/@seance/core/dist/quiz-session.js +141 -0
- package/node_modules/@seance/core/dist/quiz-session.js.map +1 -0
- package/node_modules/@seance/core/dist/quiz-subjects.d.ts +13 -0
- package/node_modules/@seance/core/dist/quiz-subjects.js +63 -0
- package/node_modules/@seance/core/dist/quiz-subjects.js.map +1 -0
- package/node_modules/@seance/core/dist/rate-limit.d.ts +33 -0
- package/node_modules/@seance/core/dist/rate-limit.js +114 -0
- package/node_modules/@seance/core/dist/rate-limit.js.map +1 -0
- package/node_modules/@seance/core/dist/scoring.d.ts +48 -0
- package/node_modules/@seance/core/dist/scoring.js +95 -0
- package/node_modules/@seance/core/dist/scoring.js.map +1 -0
- package/node_modules/@seance/core/dist/seance-config.d.ts +177 -0
- package/node_modules/@seance/core/dist/seance-config.js +43 -0
- package/node_modules/@seance/core/dist/seance-config.js.map +1 -0
- package/node_modules/@seance/core/dist/seance-state.d.ts +125 -0
- package/node_modules/@seance/core/dist/seance-state.js +61 -0
- package/node_modules/@seance/core/dist/seance-state.js.map +1 -0
- package/node_modules/@seance/core/dist/sm2.d.ts +18 -0
- package/node_modules/@seance/core/dist/sm2.js +87 -0
- package/node_modules/@seance/core/dist/sm2.js.map +1 -0
- package/node_modules/@seance/core/dist/stored-schemas.d.ts +29 -0
- package/node_modules/@seance/core/dist/stored-schemas.js +29 -0
- package/node_modules/@seance/core/dist/stored-schemas.js.map +1 -0
- package/node_modules/@seance/core/dist/structural-questions.d.ts +31 -0
- package/node_modules/@seance/core/dist/structural-questions.js +575 -0
- package/node_modules/@seance/core/dist/structural-questions.js.map +1 -0
- package/node_modules/@seance/core/dist/structure-map-schema.d.ts +84 -0
- package/node_modules/@seance/core/dist/structure-map-schema.js +73 -0
- package/node_modules/@seance/core/dist/structure-map-schema.js.map +1 -0
- package/node_modules/@seance/core/dist/structure-map.d.ts +78 -0
- package/node_modules/@seance/core/dist/structure-map.js +750 -0
- package/node_modules/@seance/core/dist/structure-map.js.map +1 -0
- package/node_modules/@seance/core/dist/sync-changes.d.ts +38 -0
- package/node_modules/@seance/core/dist/sync-changes.js +89 -0
- package/node_modules/@seance/core/dist/sync-changes.js.map +1 -0
- package/node_modules/@seance/core/dist/sync-render.d.ts +28 -0
- package/node_modules/@seance/core/dist/sync-render.js +113 -0
- package/node_modules/@seance/core/dist/sync-render.js.map +1 -0
- package/node_modules/@seance/core/dist/terminal-text.d.ts +8 -0
- package/node_modules/@seance/core/dist/terminal-text.js +33 -0
- package/node_modules/@seance/core/dist/terminal-text.js.map +1 -0
- package/node_modules/@seance/core/dist/write-plan.d.ts +51 -0
- package/node_modules/@seance/core/dist/write-plan.js +173 -0
- package/node_modules/@seance/core/dist/write-plan.js.map +1 -0
- package/node_modules/@seance/core/package.json +26 -0
- package/package.json +42 -6
|
@@ -0,0 +1,732 @@
|
|
|
1
|
+
// The dependency guard: decides whether a shell command adds packages the user has not approved.
|
|
2
|
+
// `classifyShellCommand` is the typed version; `DEPENDENCY_GUARD_SCRIPT` is the same logic as a plain Node
|
|
3
|
+
// script that `seance init` writes into the project, so the hook runs without Seance installed. Pure: no file access.
|
|
4
|
+
// The message is built from two constants that the script shares, so the function and the script cannot drift apart.
|
|
5
|
+
const MESSAGE_START = "Seance dependency guard blocked this command because it adds: ";
|
|
6
|
+
const MESSAGE_END = ". Ask the user first: say what each package does, why it is needed, its weekly downloads and when its version was published (avoid versions from the last 7 days). If the user agrees, they run the command in their own terminal or add the names to dependencyGuard.allow in .seance/config.json. Do not try another way to install it.";
|
|
7
|
+
/** The text the guard writes to stderr when it blocks (the model reads it). */
|
|
8
|
+
export function guardMessage(packages) {
|
|
9
|
+
return MESSAGE_START + packages.join(", ") + MESSAGE_END;
|
|
10
|
+
}
|
|
11
|
+
/** Every start of `word` that is at least `from` characters long: `prefixes("link", 3)` is `lin`, `link`. */
|
|
12
|
+
function prefixes(word, from) {
|
|
13
|
+
const starts = [];
|
|
14
|
+
for (let length = from; length <= word.length; length++)
|
|
15
|
+
starts.push(word.slice(0, length));
|
|
16
|
+
return starts;
|
|
17
|
+
}
|
|
18
|
+
// Sub-commands that add packages, per package manager. Anything else (run, ci, test, remove...) is not an install.
|
|
19
|
+
// npm accepts any unambiguous start of a command name, so the lists hold every name npm 11.0.0 resolves to the command
|
|
20
|
+
// (found with npm's own `deref`; requirements open question 49). `npm cit x` is blocked although `npm ci` ignores names.
|
|
21
|
+
// Edit both: this table is duplicated in DEPENDENCY_GUARD_SCRIPT.
|
|
22
|
+
const INSTALL_COMMANDS = {
|
|
23
|
+
npm: [
|
|
24
|
+
"install", "i", "in", "ins", "inst", "insta", "instal", "isnt", "isnta", "isntal", "isntall", "add",
|
|
25
|
+
...prefixes("link", 3), "ln",
|
|
26
|
+
...prefixes("install-test", 9), "it",
|
|
27
|
+
...prefixes("install-ci-test", 10), ...prefixes("clean-install-test", 14), "cit", "sit", "si",
|
|
28
|
+
],
|
|
29
|
+
pnpm: ["add", "install", "i"],
|
|
30
|
+
yarn: ["add"],
|
|
31
|
+
bun: ["add", "a", "install", "i"],
|
|
32
|
+
};
|
|
33
|
+
// Flags whose next token is their value, not a package (`--omit dev`), per package manager.
|
|
34
|
+
// npm: checked in the npm 10.9.2 config definitions. pnpm: checked in `pnpm add/install --help` of pnpm 10.29.3
|
|
35
|
+
// (`-F` and `--registry` come from the earlier list). yarn and bun: only the names Vedant gave, not checked locally.
|
|
36
|
+
// `-w` and `--workspace` take a value only for npm; for pnpm `-w` means "workspace root" and takes none.
|
|
37
|
+
// A flag that is missing here makes the command block (its value is read as a package), never pass.
|
|
38
|
+
const VALUE_FLAGS = {
|
|
39
|
+
npm: [
|
|
40
|
+
"-w", "--workspace", "--prefix", "-C", "--registry", "--tag", "--cache", "--omit", "--include", "--loglevel", "--install-strategy",
|
|
41
|
+
"--before", "--otp", "--userconfig", "--save-prefix", "--fetch-retries", "--fetch-retry-mintimeout", "--fetch-retry-maxtimeout", "--fetch-timeout",
|
|
42
|
+
],
|
|
43
|
+
pnpm: [
|
|
44
|
+
"-C", "--dir", "--filter", "-F", "--filter-prod", "--reporter", "--loglevel", "--store-dir", "--virtual-store-dir", "--modules-dir",
|
|
45
|
+
"--lockfile-dir", "--global-dir", "--hoist-pattern", "--public-hoist-pattern", "--network-concurrency", "--child-concurrency",
|
|
46
|
+
"--changed-files-ignore-pattern", "--test-pattern", "--package-import-method", "--registry",
|
|
47
|
+
],
|
|
48
|
+
yarn: ["--cwd"],
|
|
49
|
+
bun: ["--cwd", "--backend", "--registry"],
|
|
50
|
+
};
|
|
51
|
+
// pnpm words that run the next sub-command in every workspace package: `pnpm m add x` is `pnpm add x` (open question 49).
|
|
52
|
+
const PNPM_RECURSIVE = ["recursive", "multi", "m"];
|
|
53
|
+
// Words that can stand before the program of a command.
|
|
54
|
+
const KEYWORDS = ["!", "if", "then", "elif", "else", "do", "while", "until"];
|
|
55
|
+
const WRAPPERS = ["command", "time", "nohup", "exec", "builtin", "noglob"];
|
|
56
|
+
const SUDO_VALUE_FLAGS = [
|
|
57
|
+
"-u", "-g", "-h", "-p", "-C", "-D", "-R", "-T", "-r", "-t", "-U",
|
|
58
|
+
"--user", "--group", "--host", "--prompt", "--close-from", "--chdir", "--chroot", "--command-timeout", "--role", "--type", "--other-user",
|
|
59
|
+
];
|
|
60
|
+
const ENV_VALUE_FLAGS = ["-u", "-C", "-S", "--unset", "--chdir", "--split-string"];
|
|
61
|
+
const RUNNERS = {
|
|
62
|
+
sudo: { valueFlags: SUDO_VALUE_FLAGS, words: 0 },
|
|
63
|
+
env: { valueFlags: ENV_VALUE_FLAGS, words: 0 },
|
|
64
|
+
nice: { valueFlags: ["-n", "--adjustment"], words: 0 },
|
|
65
|
+
timeout: { valueFlags: ["-k", "-s", "--kill-after", "--signal"], words: 1 },
|
|
66
|
+
stdbuf: { valueFlags: ["-i", "-o", "-e", "--input", "--output", "--error"], words: 0 },
|
|
67
|
+
ionice: { valueFlags: ["-c", "-n", "-p", "-P", "-u", "--class", "--classdata", "--pid", "--pgid", "--uid"], words: 0 },
|
|
68
|
+
};
|
|
69
|
+
const SHELLS = ["posix", "powershell"];
|
|
70
|
+
// Characters that end a segment when they are outside quotes (`&`, `(` and the backtick are handled in splitSegments).
|
|
71
|
+
const SEGMENT_ENDS = ["|", ";", "\n", "\r", ")", "{", "}"];
|
|
72
|
+
// Posix reading: a backslash outside quotes escapes only these (a backslash before other characters stays: C:\nodejs\npm.cmd).
|
|
73
|
+
const POSIX_ESCAPABLE = " \t'\"\\;|&(){}<>$`";
|
|
74
|
+
const POSIX_DOUBLE_QUOTE_ESCAPABLE = '"\\$`';
|
|
75
|
+
const ENV_ASSIGNMENT = /^[A-Za-z_][A-Za-z0-9_]*=/;
|
|
76
|
+
const REDIRECTION = /^([0-9]*[<>]|&>)/; // `>log`, `2>`, `>>out`, `2>&1`, `&>log`
|
|
77
|
+
const REDIRECTION_OPERATOR_ONLY = /^(?:[0-9]*(?:>>?&?|<{1,3}&?)|&>>?)$/; // the target is the next token
|
|
78
|
+
const LOCAL_PATH = /^(\.{1,2}([\\/]|$)|\/|~[\\/]|[A-Za-z]:[\\/]|file:)/; // nothing is downloaded
|
|
79
|
+
const URL_SCHEME = /^[A-Za-z][A-Za-z0-9+.-]*:/;
|
|
80
|
+
const REGISTRY_VERSION = /^[A-Za-z0-9.*^~<>=|+_ -]*$/;
|
|
81
|
+
const SCOPED_NAME = /^@[^/@]+\/[^/@]+$/;
|
|
82
|
+
/**
|
|
83
|
+
* The length of the line break (`\n` = 1, `\r\n` = 2) that starts at `index`, or 0 when there is none.
|
|
84
|
+
* PowerShell also counts a lone `\r` as a line break (1); the posix reading does not (open question 50).
|
|
85
|
+
*/
|
|
86
|
+
function lineBreakLength(command, index, posix) {
|
|
87
|
+
if (command.charAt(index) === "\n")
|
|
88
|
+
return 1;
|
|
89
|
+
if (command.startsWith("\r\n", index))
|
|
90
|
+
return 2;
|
|
91
|
+
return !posix && command.charAt(index) === "\r" ? 1 : 0;
|
|
92
|
+
}
|
|
93
|
+
/** The index of the next `\n` or `\r` at or after `from`, or the length of the text when there is none (where a comment ends). */
|
|
94
|
+
function endOfLine(command, from) {
|
|
95
|
+
let end = from;
|
|
96
|
+
while (end < command.length && command.charAt(end) !== "\n" && command.charAt(end) !== "\r")
|
|
97
|
+
end++;
|
|
98
|
+
return end;
|
|
99
|
+
}
|
|
100
|
+
/** True when `char` is one of the characters in `set` (an empty `char`, the end of the text, is never in it). */
|
|
101
|
+
function isOneOf(set, char) {
|
|
102
|
+
return char !== "" && set.includes(char);
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Cuts a command into segments of tokens, reading it the way `shell` would (open question 48).
|
|
106
|
+
* Outside quotes `&&`, `||`, `;`, `|`, `&`, line breaks, `(`, `)`, `{`, `}` end a segment (so `$(npm i x)` is checked), and so
|
|
107
|
+
* does the backtick in the posix reading; an `&` that is part of a redirection (`2>&1`, `&>log`) does not.
|
|
108
|
+
* Whitespace outside quotes ends a token; quote characters are dropped. Escapes and line continuations (design, step 1):
|
|
109
|
+
* posix: `\<LF>` joins the word; `\"`, `\'`, `\;`, `\ ` and so on are plain characters; a lone backtick ends the segment.
|
|
110
|
+
* powershell: backtick + `<LF>` separates two words but keeps the segment; backtick + any character is that character;
|
|
111
|
+
* a `\` before a line break is dropped and the line break ends the segment; other `\` are ordinary;
|
|
112
|
+
* a lone `\r` is a line break too.
|
|
113
|
+
* Comments (open question 50, both readings): a `#` at the start of a word runs to the next `\n` or `\r`, which still ends
|
|
114
|
+
* the segment; a `#` inside a word is ordinary. PowerShell only: `<#` at the start of a word runs through the next `#>`.
|
|
115
|
+
* Posix only: `$'...'` is an ANSI-C string where `\'` and `\\` are escapes and the other escapes are kept as written.
|
|
116
|
+
*
|
|
117
|
+
* Edit both: this logic is duplicated in DEPENDENCY_GUARD_SCRIPT, P10 checks they agree.
|
|
118
|
+
*/
|
|
119
|
+
function splitSegments(command, shell) {
|
|
120
|
+
const posix = shell === "posix";
|
|
121
|
+
const segments = [];
|
|
122
|
+
let tokens = [];
|
|
123
|
+
let token = "";
|
|
124
|
+
let quote = ""; // "", "'", '"' or (posix only) "$'"
|
|
125
|
+
let quotedWord = false; // a quote opened in the current word, so a `#` after it (`''#x`) is not the start of a word
|
|
126
|
+
const endToken = () => {
|
|
127
|
+
if (token !== "")
|
|
128
|
+
tokens.push(token);
|
|
129
|
+
token = "";
|
|
130
|
+
quotedWord = false;
|
|
131
|
+
};
|
|
132
|
+
const endSegment = () => {
|
|
133
|
+
endToken();
|
|
134
|
+
if (tokens.length > 0)
|
|
135
|
+
segments.push(tokens);
|
|
136
|
+
tokens = [];
|
|
137
|
+
};
|
|
138
|
+
for (let i = 0; i < command.length; i++) {
|
|
139
|
+
const char = command.charAt(i);
|
|
140
|
+
const next = command.charAt(i + 1);
|
|
141
|
+
const lineBreak = lineBreakLength(command, i + 1, posix); // a line break right after this character
|
|
142
|
+
const wordStart = token === "" && !quotedWord;
|
|
143
|
+
if (quote === "'") {
|
|
144
|
+
if (char === "'")
|
|
145
|
+
quote = "";
|
|
146
|
+
else
|
|
147
|
+
token += char;
|
|
148
|
+
}
|
|
149
|
+
else if (quote === "$'") {
|
|
150
|
+
if (char === "'")
|
|
151
|
+
quote = "";
|
|
152
|
+
else if (char === "\\" && (next === "'" || next === "\\"))
|
|
153
|
+
token += command.charAt(++i); // the two escapes that decide where the string ends
|
|
154
|
+
else
|
|
155
|
+
token += char;
|
|
156
|
+
}
|
|
157
|
+
else if (quote === '"') {
|
|
158
|
+
if (char === '"')
|
|
159
|
+
quote = "";
|
|
160
|
+
else if (char === (posix ? "\\" : "`") && lineBreak > 0)
|
|
161
|
+
i += lineBreak; // continuation inside quotes: dropped
|
|
162
|
+
else if (posix && char === "\\" && isOneOf(POSIX_DOUBLE_QUOTE_ESCAPABLE, next))
|
|
163
|
+
token += command.charAt(++i);
|
|
164
|
+
else if (!posix && char === "`" && next !== "")
|
|
165
|
+
token += command.charAt(++i);
|
|
166
|
+
else
|
|
167
|
+
token += char;
|
|
168
|
+
}
|
|
169
|
+
else if (char === "#" && wordStart) {
|
|
170
|
+
i = endOfLine(command, i) - 1; // a comment: the line break is read next and ends the segment
|
|
171
|
+
}
|
|
172
|
+
else if (!posix && char === "<" && next === "#" && wordStart) {
|
|
173
|
+
const close = command.indexOf("#>", i + 2); // a PowerShell block comment, also over several lines
|
|
174
|
+
i = close === -1 ? command.length : close + 1;
|
|
175
|
+
}
|
|
176
|
+
else if (posix && char === "$" && next === "'") {
|
|
177
|
+
quote = "$'"; // an ANSI-C string; the `$` is not part of the word
|
|
178
|
+
quotedWord = true;
|
|
179
|
+
i++;
|
|
180
|
+
}
|
|
181
|
+
else if (char === "'" || char === '"') {
|
|
182
|
+
quote = char;
|
|
183
|
+
quotedWord = true;
|
|
184
|
+
}
|
|
185
|
+
else if (posix && char === "\\" && lineBreak > 0) {
|
|
186
|
+
i += lineBreak; // `np\<LF>m` is `npm`: the word goes on
|
|
187
|
+
}
|
|
188
|
+
else if (!posix && char === "`" && lineBreak > 0) {
|
|
189
|
+
endToken(); // line continuation: two words, the segment goes on
|
|
190
|
+
i += lineBreak;
|
|
191
|
+
}
|
|
192
|
+
else if (!posix && char === "\\" && lineBreak > 0) {
|
|
193
|
+
// A bash-style continuation read by PowerShell: drop the backslash, the line break ends the segment next.
|
|
194
|
+
}
|
|
195
|
+
else if (posix && char === "\\" && isOneOf(POSIX_ESCAPABLE, next)) {
|
|
196
|
+
token += command.charAt(++i); // the escaped character is part of the word and does nothing else
|
|
197
|
+
}
|
|
198
|
+
else if (char === "`") {
|
|
199
|
+
if (posix)
|
|
200
|
+
endSegment(); // command substitution
|
|
201
|
+
else if (next !== "")
|
|
202
|
+
token += command.charAt(++i); // PowerShell escape; a trailing backtick is dropped
|
|
203
|
+
}
|
|
204
|
+
else if (char === "&") {
|
|
205
|
+
// `&` ends a segment, except inside a redirection: `2>&1` and `>&2` (before is > or <), `&>log` and `&>>log` (after is >).
|
|
206
|
+
const before = command.charAt(i - 1);
|
|
207
|
+
if (before === ">" || before === "<" || next === ">")
|
|
208
|
+
token += char;
|
|
209
|
+
else
|
|
210
|
+
endSegment();
|
|
211
|
+
}
|
|
212
|
+
else if (char === "(") {
|
|
213
|
+
if (token.endsWith("$"))
|
|
214
|
+
token = token.slice(0, -1); // `$(`
|
|
215
|
+
endSegment();
|
|
216
|
+
}
|
|
217
|
+
else if (SEGMENT_ENDS.includes(char)) {
|
|
218
|
+
endSegment();
|
|
219
|
+
}
|
|
220
|
+
else if (/\s/.test(char)) {
|
|
221
|
+
endToken();
|
|
222
|
+
}
|
|
223
|
+
else {
|
|
224
|
+
token += char;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
endSegment();
|
|
228
|
+
return segments;
|
|
229
|
+
}
|
|
230
|
+
/** The program name of a token: last path part, lower case, without `.cmd`, `.exe` or `.ps1` (`C:\nodejs\NPM.cmd` -> `npm`). */
|
|
231
|
+
function programName(token) {
|
|
232
|
+
const parts = token.split(/[\\/]/);
|
|
233
|
+
return (parts[parts.length - 1] ?? "").toLowerCase().replace(/\.(cmd|exe|ps1)$/, "");
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* True when this flag is followed by its value as a separate token. A short cluster like `-Eu` is read like getopt:
|
|
237
|
+
* with `-u` a value flag, `-Eu bob` takes `bob` (the value flag is the last letter), `-uroot` does not (`root` is the
|
|
238
|
+
* rest of the same token), and `--user bob` takes `bob` while `--user=bob` is one token and does not.
|
|
239
|
+
*/
|
|
240
|
+
function takesNextToken(flag, valueFlags) {
|
|
241
|
+
if (flag.startsWith("--"))
|
|
242
|
+
return valueFlags.includes(flag);
|
|
243
|
+
for (let k = 1; k < flag.length; k++) {
|
|
244
|
+
if (valueFlags.includes(`-${flag.charAt(k)}`))
|
|
245
|
+
return k === flag.length - 1; // else the value is the rest of the token
|
|
246
|
+
}
|
|
247
|
+
return false;
|
|
248
|
+
}
|
|
249
|
+
/** The index after the flags that start at `from`; a flag in `valueFlags` also skips its value. */
|
|
250
|
+
function skipFlags(tokens, from, valueFlags) {
|
|
251
|
+
let i = from;
|
|
252
|
+
while (i < tokens.length) {
|
|
253
|
+
const token = tokens[i] ?? "";
|
|
254
|
+
if (!token.startsWith("-") || token === "-")
|
|
255
|
+
break;
|
|
256
|
+
i += takesNextToken(token, valueFlags) ? 2 : 1;
|
|
257
|
+
}
|
|
258
|
+
return i;
|
|
259
|
+
}
|
|
260
|
+
/** The index of the program in a segment: skips assignments, `!`, shell keywords, wrappers and the runners of RUNNERS with their flags. */
|
|
261
|
+
function programIndex(tokens) {
|
|
262
|
+
let i = 0;
|
|
263
|
+
while (i < tokens.length) {
|
|
264
|
+
const token = tokens[i] ?? "";
|
|
265
|
+
const name = programName(token);
|
|
266
|
+
// hasOwn: a program called "constructor" must not find something on Object.prototype.
|
|
267
|
+
const runner = Object.hasOwn(RUNNERS, name) ? RUNNERS[name] : undefined;
|
|
268
|
+
if (ENV_ASSIGNMENT.test(token) || KEYWORDS.includes(token))
|
|
269
|
+
i += 1;
|
|
270
|
+
else if (WRAPPERS.includes(name))
|
|
271
|
+
i = skipFlags(tokens, i + 1, []);
|
|
272
|
+
else if (runner !== undefined)
|
|
273
|
+
i = skipFlags(tokens, i + 1, runner.valueFlags) + runner.words;
|
|
274
|
+
else
|
|
275
|
+
break;
|
|
276
|
+
}
|
|
277
|
+
return i;
|
|
278
|
+
}
|
|
279
|
+
/** Local path: undefined (nothing downloaded). Registry spec: its name. Anything else: the whole spec, always blocked. */
|
|
280
|
+
function classifySpec(spec) {
|
|
281
|
+
if (LOCAL_PATH.test(spec))
|
|
282
|
+
return undefined;
|
|
283
|
+
const remote = { label: spec, always: true };
|
|
284
|
+
if (URL_SCHEME.test(spec))
|
|
285
|
+
return remote;
|
|
286
|
+
const at = spec.indexOf("@", spec.startsWith("@") ? 1 : 0);
|
|
287
|
+
const name = at === -1 ? spec : spec.slice(0, at);
|
|
288
|
+
const version = at === -1 ? "" : spec.slice(at + 1);
|
|
289
|
+
if (version.startsWith("file:"))
|
|
290
|
+
return undefined;
|
|
291
|
+
if (!REGISTRY_VERSION.test(version))
|
|
292
|
+
return remote;
|
|
293
|
+
const nameOk = name.startsWith("@") ? SCOPED_NAME.test(name) : !name.includes("/");
|
|
294
|
+
return nameOk ? { label: name, always: false } : remote;
|
|
295
|
+
}
|
|
296
|
+
/** The packages one segment installs; empty for anything that is not a package-adding command. */
|
|
297
|
+
function installedEntries(tokens) {
|
|
298
|
+
const start = programIndex(tokens);
|
|
299
|
+
const first = tokens[start];
|
|
300
|
+
if (first === undefined)
|
|
301
|
+
return [];
|
|
302
|
+
const program = programName(first);
|
|
303
|
+
// hasOwn: a program called "constructor" or "__proto__" must not find something on Object.prototype.
|
|
304
|
+
if (!Object.hasOwn(INSTALL_COMMANDS, program))
|
|
305
|
+
return [];
|
|
306
|
+
const verbs = INSTALL_COMMANDS[program] ?? [];
|
|
307
|
+
const valueFlags = VALUE_FLAGS[program] ?? [];
|
|
308
|
+
const positional = [];
|
|
309
|
+
for (let i = start + 1; i < tokens.length; i++) {
|
|
310
|
+
const token = tokens[i] ?? "";
|
|
311
|
+
if (REDIRECTION.test(token)) {
|
|
312
|
+
if (REDIRECTION_OPERATOR_ONLY.test(token))
|
|
313
|
+
i += 1; // skip the file name too
|
|
314
|
+
}
|
|
315
|
+
else if (token.startsWith("-")) {
|
|
316
|
+
if (valueFlags.includes(token))
|
|
317
|
+
i += 1;
|
|
318
|
+
}
|
|
319
|
+
else {
|
|
320
|
+
positional.push(token);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
let at = 0;
|
|
324
|
+
if (program === "yarn") {
|
|
325
|
+
while (positional[at] === "global")
|
|
326
|
+
at += 1;
|
|
327
|
+
if (positional[at] === "workspace")
|
|
328
|
+
at += 2; // `yarn workspace <name> add x`
|
|
329
|
+
}
|
|
330
|
+
if (program === "pnpm") {
|
|
331
|
+
while (PNPM_RECURSIVE.includes(positional[at] ?? ""))
|
|
332
|
+
at += 1; // `pnpm recursive add x`, `pnpm m add x`
|
|
333
|
+
}
|
|
334
|
+
const subcommand = positional[at];
|
|
335
|
+
let specsFrom = at + 1;
|
|
336
|
+
if (program === "yarn" && subcommand === "workspaces" && positional[at + 1] === "foreach") {
|
|
337
|
+
// `yarn workspaces foreach -A add x`: the first `add` after `foreach` is the sub-command. A flag value before it
|
|
338
|
+
// (`--include web`) is just another word, so it cannot hide the `add`.
|
|
339
|
+
const addAt = positional.indexOf("add", at + 2);
|
|
340
|
+
if (addAt === -1)
|
|
341
|
+
return [];
|
|
342
|
+
specsFrom = addAt + 1;
|
|
343
|
+
}
|
|
344
|
+
else if (subcommand === undefined || !verbs.includes(subcommand)) {
|
|
345
|
+
return [];
|
|
346
|
+
}
|
|
347
|
+
const entries = [];
|
|
348
|
+
for (const spec of positional.slice(specsFrom)) {
|
|
349
|
+
const entry = classifySpec(spec);
|
|
350
|
+
if (entry !== undefined)
|
|
351
|
+
entries.push(entry);
|
|
352
|
+
}
|
|
353
|
+
return entries;
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* Decides whether a shell command may run. The command is read as a POSIX shell and as PowerShell would read it, and
|
|
357
|
+
* blocked when either reading blocks (open question 48). It blocks when any segment adds a registry package whose name is not in
|
|
358
|
+
* `allow`, or any package that is not a registry spec (a URL, `owner/repo`, `zod@npm:evil`), which `allow` cannot approve.
|
|
359
|
+
* Installs from the lockfile or from a local path (no package named) and every other command are allowed. Never throws.
|
|
360
|
+
*/
|
|
361
|
+
export function classifyShellCommand(command, allow) {
|
|
362
|
+
const blocked = [];
|
|
363
|
+
for (const shell of SHELLS) {
|
|
364
|
+
for (const segment of splitSegments(command, shell)) {
|
|
365
|
+
for (const entry of installedEntries(segment)) {
|
|
366
|
+
if ((entry.always || !allow.includes(entry.label)) && !blocked.includes(entry.label))
|
|
367
|
+
blocked.push(entry.label);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
return blocked.length === 0 ? { verdict: "allow", packages: [] } : { verdict: "block", packages: blocked };
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* The guard as a plain Node ESM script (only `node:` imports), written to `.kiro/seance/dependency-guard.mjs`.
|
|
375
|
+
* Same rules as `classifyShellCommand`; the differential test (P10) runs the real script against the function.
|
|
376
|
+
* Written with String.raw so backslashes reach the file untouched. Keep backticks and dollar-brace out of it:
|
|
377
|
+
* the backtick is written as the escape x60 inside a string, and the script never has a dollar sign before a brace.
|
|
378
|
+
*/
|
|
379
|
+
export const DEPENDENCY_GUARD_SCRIPT = String.raw `// Seance dependency guard, written by "seance init".
|
|
380
|
+
// Kiro runs this before every shell command (hook: .kiro/hooks/seance-dependency-guard.json).
|
|
381
|
+
// It blocks commands that add packages not listed in dependencyGuard.allow of .seance/config.json.
|
|
382
|
+
// Safe to delete together with that hook file. It never exits with code 1: on any problem the command runs.
|
|
383
|
+
import { readFileSync, statSync } from "node:fs";
|
|
384
|
+
import { join } from "node:path";
|
|
385
|
+
|
|
386
|
+
const MAX_STDIN_BYTES = 100 * 1024;
|
|
387
|
+
const MAX_CONFIG_BYTES = 256 * 1024;
|
|
388
|
+
// Every start of word that is at least from characters long: prefixes("link", 3) is lin, link.
|
|
389
|
+
function prefixes(word, from) {
|
|
390
|
+
const starts = [];
|
|
391
|
+
for (let length = from; length <= word.length; length++) starts.push(word.slice(0, length));
|
|
392
|
+
return starts;
|
|
393
|
+
}
|
|
394
|
+
// Edit both: this table is duplicated from INSTALL_COMMANDS in @seance/core (dependency-guard.ts); P10 checks they agree.
|
|
395
|
+
const INSTALL_COMMANDS = {
|
|
396
|
+
npm: [
|
|
397
|
+
"install", "i", "in", "ins", "inst", "insta", "instal", "isnt", "isnta", "isntal", "isntall", "add",
|
|
398
|
+
...prefixes("link", 3), "ln",
|
|
399
|
+
...prefixes("install-test", 9), "it",
|
|
400
|
+
...prefixes("install-ci-test", 10), ...prefixes("clean-install-test", 14), "cit", "sit", "si",
|
|
401
|
+
],
|
|
402
|
+
pnpm: ["add", "install", "i"],
|
|
403
|
+
yarn: ["add"],
|
|
404
|
+
bun: ["add", "a", "install", "i"],
|
|
405
|
+
};
|
|
406
|
+
const VALUE_FLAGS = {
|
|
407
|
+
npm: [
|
|
408
|
+
"-w", "--workspace", "--prefix", "-C", "--registry", "--tag", "--cache", "--omit", "--include", "--loglevel", "--install-strategy",
|
|
409
|
+
"--before", "--otp", "--userconfig", "--save-prefix", "--fetch-retries", "--fetch-retry-mintimeout", "--fetch-retry-maxtimeout", "--fetch-timeout",
|
|
410
|
+
],
|
|
411
|
+
pnpm: [
|
|
412
|
+
"-C", "--dir", "--filter", "-F", "--filter-prod", "--reporter", "--loglevel", "--store-dir", "--virtual-store-dir", "--modules-dir",
|
|
413
|
+
"--lockfile-dir", "--global-dir", "--hoist-pattern", "--public-hoist-pattern", "--network-concurrency", "--child-concurrency",
|
|
414
|
+
"--changed-files-ignore-pattern", "--test-pattern", "--package-import-method", "--registry",
|
|
415
|
+
],
|
|
416
|
+
yarn: ["--cwd"],
|
|
417
|
+
bun: ["--cwd", "--backend", "--registry"],
|
|
418
|
+
};
|
|
419
|
+
const PNPM_RECURSIVE = ["recursive", "multi", "m"];
|
|
420
|
+
const KEYWORDS = ["!", "if", "then", "elif", "else", "do", "while", "until"];
|
|
421
|
+
const WRAPPERS = ["command", "time", "nohup", "exec", "builtin", "noglob"];
|
|
422
|
+
const SUDO_VALUE_FLAGS = [
|
|
423
|
+
"-u", "-g", "-h", "-p", "-C", "-D", "-R", "-T", "-r", "-t", "-U",
|
|
424
|
+
"--user", "--group", "--host", "--prompt", "--close-from", "--chdir", "--chroot", "--command-timeout", "--role", "--type", "--other-user",
|
|
425
|
+
];
|
|
426
|
+
const ENV_VALUE_FLAGS = ["-u", "-C", "-S", "--unset", "--chdir", "--split-string"];
|
|
427
|
+
const RUNNERS = {
|
|
428
|
+
sudo: { valueFlags: SUDO_VALUE_FLAGS, words: 0 },
|
|
429
|
+
env: { valueFlags: ENV_VALUE_FLAGS, words: 0 },
|
|
430
|
+
nice: { valueFlags: ["-n", "--adjustment"], words: 0 },
|
|
431
|
+
timeout: { valueFlags: ["-k", "-s", "--kill-after", "--signal"], words: 1 },
|
|
432
|
+
stdbuf: { valueFlags: ["-i", "-o", "-e", "--input", "--output", "--error"], words: 0 },
|
|
433
|
+
ionice: { valueFlags: ["-c", "-n", "-p", "-P", "-u", "--class", "--classdata", "--pid", "--pgid", "--uid"], words: 0 },
|
|
434
|
+
};
|
|
435
|
+
const SHELLS = ["posix", "powershell"];
|
|
436
|
+
const SEGMENT_ENDS = ["|", ";", "\n", "\r", ")", "{", "}"];
|
|
437
|
+
const POSIX_ESCAPABLE = " \t'\"\\;|&(){}<>$\x60";
|
|
438
|
+
const POSIX_DOUBLE_QUOTE_ESCAPABLE = '"\\$\x60';
|
|
439
|
+
const ENV_ASSIGNMENT = /^[A-Za-z_][A-Za-z0-9_]*=/;
|
|
440
|
+
const REDIRECTION = /^([0-9]*[<>]|&>)/;
|
|
441
|
+
const REDIRECTION_OPERATOR_ONLY = /^(?:[0-9]*(?:>>?&?|<{1,3}&?)|&>>?)$/;
|
|
442
|
+
const LOCAL_PATH = /^(\.{1,2}([\\/]|$)|\/|~[\\/]|[A-Za-z]:[\\/]|file:)/;
|
|
443
|
+
const URL_SCHEME = /^[A-Za-z][A-Za-z0-9+.-]*:/;
|
|
444
|
+
const REGISTRY_VERSION = /^[A-Za-z0-9.*^~<>=|+_ -]*$/;
|
|
445
|
+
const SCOPED_NAME = /^@[^/@]+\/[^/@]+$/;
|
|
446
|
+
const MESSAGE_START = ${JSON.stringify(MESSAGE_START)};
|
|
447
|
+
const MESSAGE_END = ${JSON.stringify(MESSAGE_END)};
|
|
448
|
+
|
|
449
|
+
// Reads stdin until it ends. Gives null when it is too big or takes longer than 3 seconds.
|
|
450
|
+
function readStdin() {
|
|
451
|
+
return new Promise((resolve) => {
|
|
452
|
+
const chunks = [];
|
|
453
|
+
let size = 0;
|
|
454
|
+
let finished = false;
|
|
455
|
+
const finish = (value) => {
|
|
456
|
+
if (finished) return;
|
|
457
|
+
finished = true;
|
|
458
|
+
clearTimeout(timer);
|
|
459
|
+
process.stdin.destroy();
|
|
460
|
+
resolve(value);
|
|
461
|
+
};
|
|
462
|
+
const timer = setTimeout(() => finish(null), 3000);
|
|
463
|
+
process.stdin.on("data", (chunk) => {
|
|
464
|
+
size += chunk.length;
|
|
465
|
+
if (size > MAX_STDIN_BYTES) finish(null);
|
|
466
|
+
else chunks.push(chunk);
|
|
467
|
+
});
|
|
468
|
+
process.stdin.on("end", () => finish(Buffer.concat(chunks).toString("utf8")));
|
|
469
|
+
process.stdin.on("error", () => finish(null));
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
// The allow list from .seance/config.json in the folder the hook runs in (the project root). Empty when anything is off.
|
|
474
|
+
function readAllow() {
|
|
475
|
+
try {
|
|
476
|
+
const file = join(process.cwd(), ".seance", "config.json");
|
|
477
|
+
if (statSync(file).size > MAX_CONFIG_BYTES) return [];
|
|
478
|
+
const allow = JSON.parse(readFileSync(file, "utf8")).dependencyGuard.allow;
|
|
479
|
+
return Array.isArray(allow) && allow.every((name) => typeof name === "string") ? allow : [];
|
|
480
|
+
} catch {
|
|
481
|
+
return [];
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
// The length of the line break (LF = 1, CR LF = 2) that starts at index, or 0 when there is none.
|
|
486
|
+
// PowerShell also counts a lone CR as a line break (1); the posix reading does not.
|
|
487
|
+
function lineBreakLength(command, index, posix) {
|
|
488
|
+
if (command.charAt(index) === "\n") return 1;
|
|
489
|
+
if (command.startsWith("\r\n", index)) return 2;
|
|
490
|
+
return !posix && command.charAt(index) === "\r" ? 1 : 0;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
// The index of the next LF or CR at or after from, or the length of the text when there is none (where a comment ends).
|
|
494
|
+
function endOfLine(command, from) {
|
|
495
|
+
let end = from;
|
|
496
|
+
while (end < command.length && command.charAt(end) !== "\n" && command.charAt(end) !== "\r") end++;
|
|
497
|
+
return end;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
// True when char is one of the characters in set (an empty char, the end of the text, never is).
|
|
501
|
+
function isOneOf(set, char) {
|
|
502
|
+
return char !== "" && set.includes(char);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
// Edit both: this logic is duplicated from splitSegments in @seance/core (dependency-guard.ts); P10 checks they agree.
|
|
506
|
+
// Reads the command the way a POSIX shell or the way PowerShell would; the command is blocked when either reading blocks.
|
|
507
|
+
// A hash at the start of a word is a comment up to the next LF or CR (which still ends the segment); PowerShell also has
|
|
508
|
+
// block comments that start with less-than and hash; the posix reading has ANSI-C strings (dollar plus single quote).
|
|
509
|
+
function splitSegments(command, shell) {
|
|
510
|
+
const posix = shell === "posix";
|
|
511
|
+
const segments = [];
|
|
512
|
+
let tokens = [];
|
|
513
|
+
let token = "";
|
|
514
|
+
let quote = ""; // "", a single quote, a double quote or (posix only) dollar plus single quote
|
|
515
|
+
let quotedWord = false; // a quote opened in the current word, so a hash after it is not the start of a word
|
|
516
|
+
const endToken = () => {
|
|
517
|
+
if (token !== "") tokens.push(token);
|
|
518
|
+
token = "";
|
|
519
|
+
quotedWord = false;
|
|
520
|
+
};
|
|
521
|
+
const endSegment = () => {
|
|
522
|
+
endToken();
|
|
523
|
+
if (tokens.length > 0) segments.push(tokens);
|
|
524
|
+
tokens = [];
|
|
525
|
+
};
|
|
526
|
+
for (let i = 0; i < command.length; i++) {
|
|
527
|
+
const char = command.charAt(i);
|
|
528
|
+
const next = command.charAt(i + 1);
|
|
529
|
+
const lineBreak = lineBreakLength(command, i + 1, posix);
|
|
530
|
+
const wordStart = token === "" && !quotedWord;
|
|
531
|
+
if (quote === "'") {
|
|
532
|
+
if (char === "'") quote = "";
|
|
533
|
+
else token += char;
|
|
534
|
+
} else if (quote === "$'") {
|
|
535
|
+
if (char === "'") quote = "";
|
|
536
|
+
else if (char === "\\" && (next === "'" || next === "\\")) token += command.charAt(++i);
|
|
537
|
+
else token += char;
|
|
538
|
+
} else if (quote === '"') {
|
|
539
|
+
if (char === '"') quote = "";
|
|
540
|
+
else if (char === (posix ? "\\" : "\x60") && lineBreak > 0) i += lineBreak;
|
|
541
|
+
else if (posix && char === "\\" && isOneOf(POSIX_DOUBLE_QUOTE_ESCAPABLE, next)) token += command.charAt(++i);
|
|
542
|
+
else if (!posix && char === "\x60" && next !== "") token += command.charAt(++i);
|
|
543
|
+
else token += char;
|
|
544
|
+
} else if (char === "#" && wordStart) {
|
|
545
|
+
i = endOfLine(command, i) - 1;
|
|
546
|
+
} else if (!posix && char === "<" && next === "#" && wordStart) {
|
|
547
|
+
const close = command.indexOf("#>", i + 2);
|
|
548
|
+
i = close === -1 ? command.length : close + 1;
|
|
549
|
+
} else if (posix && char === "$" && next === "'") {
|
|
550
|
+
quote = "$'";
|
|
551
|
+
quotedWord = true;
|
|
552
|
+
i++;
|
|
553
|
+
} else if (char === "'" || char === '"') {
|
|
554
|
+
quote = char;
|
|
555
|
+
quotedWord = true;
|
|
556
|
+
} else if (posix && char === "\\" && lineBreak > 0) {
|
|
557
|
+
i += lineBreak;
|
|
558
|
+
} else if (!posix && char === "\x60" && lineBreak > 0) {
|
|
559
|
+
endToken();
|
|
560
|
+
i += lineBreak;
|
|
561
|
+
} else if (!posix && char === "\\" && lineBreak > 0) {
|
|
562
|
+
// A bash-style continuation read by PowerShell: drop the backslash, the line break ends the segment next.
|
|
563
|
+
} else if (posix && char === "\\" && isOneOf(POSIX_ESCAPABLE, next)) {
|
|
564
|
+
token += command.charAt(++i);
|
|
565
|
+
} else if (char === "\x60") {
|
|
566
|
+
if (posix) endSegment();
|
|
567
|
+
else if (next !== "") token += command.charAt(++i);
|
|
568
|
+
} else if (char === "&") {
|
|
569
|
+
// An ampersand ends a segment, except inside a redirection: 2>&1 and >&2 (before is > or <), &>log (after is >).
|
|
570
|
+
const before = command.charAt(i - 1);
|
|
571
|
+
if (before === ">" || before === "<" || next === ">") token += char;
|
|
572
|
+
else endSegment();
|
|
573
|
+
} else if (char === "(") {
|
|
574
|
+
if (token.endsWith("$")) token = token.slice(0, -1);
|
|
575
|
+
endSegment();
|
|
576
|
+
} else if (SEGMENT_ENDS.includes(char)) {
|
|
577
|
+
endSegment();
|
|
578
|
+
} else if (/\s/.test(char)) {
|
|
579
|
+
endToken();
|
|
580
|
+
} else {
|
|
581
|
+
token += char;
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
endSegment();
|
|
585
|
+
return segments;
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
function programName(token) {
|
|
589
|
+
const parts = token.split(/[\\/]/);
|
|
590
|
+
return (parts[parts.length - 1] || "").toLowerCase().replace(/\.(cmd|exe|ps1)$/, "");
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
// With -u a value flag: "-Eu bob" takes bob (the value flag is the last letter), "-uroot" does not (root is the rest
|
|
594
|
+
// of the same token), "--user bob" takes bob and "--user=bob" is one token.
|
|
595
|
+
function takesNextToken(flag, valueFlags) {
|
|
596
|
+
if (flag.startsWith("--")) return valueFlags.includes(flag);
|
|
597
|
+
for (let k = 1; k < flag.length; k++) {
|
|
598
|
+
if (valueFlags.includes("-" + flag.charAt(k))) return k === flag.length - 1;
|
|
599
|
+
}
|
|
600
|
+
return false;
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
function skipFlags(tokens, from, valueFlags) {
|
|
604
|
+
let i = from;
|
|
605
|
+
while (i < tokens.length) {
|
|
606
|
+
const token = tokens[i];
|
|
607
|
+
if (!token.startsWith("-") || token === "-") break;
|
|
608
|
+
i += takesNextToken(token, valueFlags) ? 2 : 1;
|
|
609
|
+
}
|
|
610
|
+
return i;
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
function programIndex(tokens) {
|
|
614
|
+
let i = 0;
|
|
615
|
+
while (i < tokens.length) {
|
|
616
|
+
const token = tokens[i];
|
|
617
|
+
const name = programName(token);
|
|
618
|
+
const runner = Object.hasOwn(RUNNERS, name) ? RUNNERS[name] : undefined;
|
|
619
|
+
if (ENV_ASSIGNMENT.test(token) || KEYWORDS.includes(token)) i += 1;
|
|
620
|
+
else if (WRAPPERS.includes(name)) i = skipFlags(tokens, i + 1, []);
|
|
621
|
+
else if (runner !== undefined) i = skipFlags(tokens, i + 1, runner.valueFlags) + runner.words;
|
|
622
|
+
else break;
|
|
623
|
+
}
|
|
624
|
+
return i;
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
// A local path gives undefined; a registry spec gives its name; anything else gives the whole spec, always blocked.
|
|
628
|
+
function classifySpec(spec) {
|
|
629
|
+
if (LOCAL_PATH.test(spec)) return undefined;
|
|
630
|
+
const remote = { label: spec, always: true };
|
|
631
|
+
if (URL_SCHEME.test(spec)) return remote;
|
|
632
|
+
const at = spec.indexOf("@", spec.startsWith("@") ? 1 : 0);
|
|
633
|
+
const name = at === -1 ? spec : spec.slice(0, at);
|
|
634
|
+
const version = at === -1 ? "" : spec.slice(at + 1);
|
|
635
|
+
if (version.startsWith("file:")) return undefined;
|
|
636
|
+
if (!REGISTRY_VERSION.test(version)) return remote;
|
|
637
|
+
const nameOk = name.startsWith("@") ? SCOPED_NAME.test(name) : !name.includes("/");
|
|
638
|
+
return nameOk ? { label: name, always: false } : remote;
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
function installedEntries(tokens) {
|
|
642
|
+
const start = programIndex(tokens);
|
|
643
|
+
if (start >= tokens.length) return [];
|
|
644
|
+
const program = programName(tokens[start]);
|
|
645
|
+
if (!Object.hasOwn(INSTALL_COMMANDS, program)) return [];
|
|
646
|
+
const verbs = INSTALL_COMMANDS[program];
|
|
647
|
+
const valueFlags = VALUE_FLAGS[program];
|
|
648
|
+
|
|
649
|
+
const positional = [];
|
|
650
|
+
for (let i = start + 1; i < tokens.length; i++) {
|
|
651
|
+
const token = tokens[i];
|
|
652
|
+
if (REDIRECTION.test(token)) {
|
|
653
|
+
if (REDIRECTION_OPERATOR_ONLY.test(token)) i += 1;
|
|
654
|
+
} else if (token.startsWith("-")) {
|
|
655
|
+
if (valueFlags.includes(token)) i += 1;
|
|
656
|
+
} else {
|
|
657
|
+
positional.push(token);
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
let at = 0;
|
|
661
|
+
if (program === "yarn") {
|
|
662
|
+
while (positional[at] === "global") at += 1;
|
|
663
|
+
if (positional[at] === "workspace") at += 2;
|
|
664
|
+
}
|
|
665
|
+
if (program === "pnpm") {
|
|
666
|
+
while (PNPM_RECURSIVE.includes(positional[at] || "")) at += 1;
|
|
667
|
+
}
|
|
668
|
+
const subcommand = positional[at];
|
|
669
|
+
let specsFrom = at + 1;
|
|
670
|
+
if (program === "yarn" && subcommand === "workspaces" && positional[at + 1] === "foreach") {
|
|
671
|
+
const addAt = positional.indexOf("add", at + 2);
|
|
672
|
+
if (addAt === -1) return [];
|
|
673
|
+
specsFrom = addAt + 1;
|
|
674
|
+
} else if (subcommand === undefined || !verbs.includes(subcommand)) {
|
|
675
|
+
return [];
|
|
676
|
+
}
|
|
677
|
+
const entries = [];
|
|
678
|
+
for (const spec of positional.slice(specsFrom)) {
|
|
679
|
+
const entry = classifySpec(spec);
|
|
680
|
+
if (entry !== undefined) entries.push(entry);
|
|
681
|
+
}
|
|
682
|
+
return entries;
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
function blockedNames(command, allow) {
|
|
686
|
+
const blocked = [];
|
|
687
|
+
for (const shell of SHELLS) {
|
|
688
|
+
for (const segment of splitSegments(command, shell)) {
|
|
689
|
+
for (const entry of installedEntries(segment)) {
|
|
690
|
+
if ((entry.always || !allow.includes(entry.label)) && !blocked.includes(entry.label)) blocked.push(entry.label);
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
return blocked;
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
// The command string of a hook event (current and older payload spelling), or undefined.
|
|
698
|
+
function commandOf(event) {
|
|
699
|
+
if (typeof event !== "object" || event === null) return undefined;
|
|
700
|
+
for (const key of ["tool_input", "toolArgs"]) {
|
|
701
|
+
const input = event[key];
|
|
702
|
+
if (typeof input === "object" && input !== null && typeof input.command === "string") return input.command;
|
|
703
|
+
}
|
|
704
|
+
return undefined;
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
async function main() {
|
|
708
|
+
const text = await readStdin();
|
|
709
|
+
if (text === null) return 0;
|
|
710
|
+
let command;
|
|
711
|
+
try {
|
|
712
|
+
command = commandOf(JSON.parse(text));
|
|
713
|
+
} catch {
|
|
714
|
+
return 0;
|
|
715
|
+
}
|
|
716
|
+
if (command === undefined) return 0;
|
|
717
|
+
const blocked = blockedNames(command, readAllow());
|
|
718
|
+
if (blocked.length === 0) return 0;
|
|
719
|
+
process.stderr.write(MESSAGE_START + blocked.join(", ") + MESSAGE_END);
|
|
720
|
+
return 2;
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
main().then(
|
|
724
|
+
(code) => {
|
|
725
|
+
process.exitCode = code;
|
|
726
|
+
},
|
|
727
|
+
() => {
|
|
728
|
+
process.exitCode = 0;
|
|
729
|
+
},
|
|
730
|
+
);
|
|
731
|
+
`;
|
|
732
|
+
//# sourceMappingURL=dependency-guard.js.map
|