@venizia/vulcan 0.4.10-2 → 0.4.10-21
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 +30 -23
- package/base/checks/wiki-source-links.ts +17 -1
- package/base/claude/events.json +1 -11
- package/base/claude/settings.json +5 -76
- package/base/extensions/vulcan-guards.ts +81 -18
- package/base/hooks/guard-git.ts +95 -19
- package/base/hooks/guard-outside.ts +163 -43
- package/base/hooks/guard-writes.ts +15 -36
- package/base/hooks/session-start.ts +1 -1
- package/base/make/vulcan.mk +8 -5
- package/base/packs/venizia.md +16 -0
- package/base/references/delegation-brief.md +3 -5
- package/base/references/testing.md +1 -9
- package/base/rules.md +33 -38
- package/base/skills/build-a-feature/SKILL.md +4 -4
- package/base/skills/docs-update/SKILL.md +19 -8
- package/base/skills/fix-a-bug/SKILL.md +4 -2
- package/base/skills/plan-a-change/SKILL.md +1 -1
- package/base/skills/research/SKILL.md +1 -1
- package/base/skills/retro/SKILL.md +2 -2
- package/base/skills/review-a-change/SKILL.md +1 -1
- package/base/skills/visualize-work/SKILL.md +3 -3
- package/base/skills/visualize-work/references/spec.md +6 -3
- package/base/templates/AGENTS.md +9 -7
- package/base/templates/board.html +10 -110
- package/package.json +3 -3
- package/src/agents/atlas-block.ts +8 -31
- package/src/agents/codex-hooks.ts +1 -1
- package/src/agents/generated-block.ts +52 -0
- package/src/agents/hooks-json.ts +22 -3
- package/src/agents/index.ts +2 -0
- package/src/agents/render.ts +15 -2
- package/src/agents/resolve.ts +1 -0
- package/src/agents/rules-block.ts +92 -0
- package/src/board/command.ts +92 -30
- package/src/board/common/constants.ts +26 -0
- package/src/board/common/types.ts +10 -3
- package/src/board/fold.ts +1 -1
- package/src/board/github.ts +1 -2
- package/src/board/gitlab.ts +436 -0
- package/src/board/index.ts +1 -0
- package/src/board/logo.ts +49 -0
- package/src/board/open-file.ts +110 -0
- package/src/board/page.ts +57 -5
- package/src/board/schema.ts +4 -1
- package/src/board/server.ts +87 -3
- package/src/board/settings.ts +21 -24
- package/src/commands/changelog.command.ts +40 -0
- package/src/commands/clean.command.ts +6 -1
- package/src/commands/help.command.ts +2 -63
- package/src/commands/index.ts +6 -1
- package/src/commands/migrate.command.ts +71 -0
- package/src/commands/profile.command.ts +1 -1
- package/src/commands/runners.command.ts +43 -0
- package/src/commands/sync.command.ts +8 -4
- package/src/commands/table.ts +362 -0
- package/src/commit/check.ts +1 -0
- package/src/commit/common/constants.ts +4 -0
- package/src/commit/hook.ts +112 -8
- package/src/common/constants.ts +11 -9
- package/src/common/env-variables.ts +41 -0
- package/src/common/index.ts +1 -0
- package/src/common/layout-folder.ts +3 -4
- package/src/common/record.ts +5 -0
- package/src/config/common/constants.ts +28 -22
- package/src/config/loader.ts +22 -0
- package/src/config/schema.ts +330 -146
- package/src/create/add-app.ts +7 -2
- package/src/create/add-lib.ts +2 -4
- package/src/create/common/constants.ts +5 -3
- package/src/create/preset.ts +1 -0
- package/src/create/scratch.ts +2 -1
- package/src/doctor/checks.ts +277 -448
- package/src/doctor/common/constants.ts +27 -19
- package/src/doctor/private-values.ts +4 -1
- package/src/doctor/runner.ts +5 -12
- package/src/doctor/schema.ts +4 -0
- package/src/events/common/actor-names.ts +12 -0
- package/src/events/common/constants.ts +65 -38
- package/src/events/common/index.ts +1 -0
- package/src/events/hook.ts +5 -56
- package/src/events/text.ts +41 -6
- package/src/hooks/command-view.ts +80 -19
- package/src/hooks/common/constants.ts +26 -17
- package/src/hooks/common/types.ts +13 -24
- package/src/hooks/decision.ts +30 -12
- package/src/hooks/git-queries.ts +113 -0
- package/src/hooks/guard-git.ts +76 -24
- package/src/hooks/guard-outside.ts +334 -311
- package/src/hooks/guard-runner.ts +206 -0
- package/src/hooks/guard-writes.ts +87 -184
- package/src/hooks/hook-input.ts +57 -0
- package/src/hooks/index.ts +3 -6
- package/src/hooks/link-makers.ts +315 -0
- package/src/hooks/package-runners.ts +268 -0
- package/src/hooks/project-guards.ts +81 -44
- package/src/hooks/project-tmp.ts +39 -4
- package/src/hooks/runner-scan.ts +257 -0
- package/src/hooks/script-runners.ts +400 -0
- package/src/hooks/session-digest.ts +27 -117
- package/src/hooks/shell-folders.ts +760 -0
- package/src/hooks/shell-writes.ts +646 -0
- package/src/hooks/shell.ts +1421 -194
- package/src/hooks/subagent-card.ts +2 -2
- package/src/hooks/write-commands.ts +898 -0
- package/src/init/scaffold.ts +2 -3
- package/src/local/auto-clean.ts +2 -1
- package/src/local/browser-socket.ts +33 -0
- package/src/local/clean.ts +37 -6
- package/src/local/constants.ts +15 -1
- package/src/local/index.ts +1 -0
- package/src/local/scan.ts +41 -10
- package/src/manifest/render.ts +84 -30
- package/src/owner/constants.ts +4 -5
- package/src/owner/index.ts +0 -2
- package/src/owner/profile.ts +1 -0
- package/src/owner/schema.ts +51 -27
- package/src/owner/survey.ts +0 -3
- package/src/owner/view.ts +0 -9
- package/src/router.ts +8 -132
- package/src/rules/base.ts +12 -7
- package/src/rules/index.ts +1 -0
- package/src/rules/packs.ts +44 -0
- package/src/runners/check.ts +130 -0
- package/src/skills/frontmatter.ts +1 -0
- package/src/sync/agents-block.ts +104 -0
- package/src/sync/base-rules.ts +3 -3
- package/src/sync/claude-launch.ts +12 -12
- package/src/sync/common/constants.ts +0 -2
- package/src/sync/index.ts +1 -1
- package/src/sync/local-choice.ts +32 -3
- package/src/sync/planner.ts +2 -15
- package/src/sync/settings.ts +0 -17
- package/src/theme/index.ts +1 -0
- package/src/theme/tokens.ts +410 -0
- package/src/ui/prompt.ts +1 -0
- package/src/visualize/architecture.ts +504 -0
- package/src/visualize/assets.ts +41 -30
- package/src/visualize/compare-render.ts +22 -24
- package/src/visualize/constants.ts +27 -0
- package/src/visualize/diagrams.ts +13 -121
- package/src/visualize/kinds-render.ts +2 -2
- package/src/visualize/kinds-schema.ts +49 -0
- package/src/visualize/labels-vi.ts +25 -0
- package/src/visualize/labels.ts +2 -0
- package/src/visualize/layout.ts +415 -66
- package/src/visualize/page.ts +82 -15
- package/src/visualize/render.ts +2 -1
- package/src/visualize/schema.ts +29 -1
- package/src/visualize/sections.ts +78 -9
- package/src/visualize/theme.ts +49 -18
- package/src/wiki/anchors.ts +1 -0
- package/src/wiki/command.ts +10 -3
- package/src/wiki/source-links.ts +57 -18
- package/base/ci-global.json +0 -13
- package/base/claude/remote-hosts.json +0 -15
- package/base/hooks/guard-manifest.ts +0 -27
- package/base/hooks/guard-package.ts +0 -30
- package/base/hooks/guard-remote-hosts.ts +0 -70
- package/base/hooks/guard-scripting.ts +0 -32
- package/base/hooks/post-tool.ts +0 -37
- package/base/hooks/prompt-submit.ts +0 -54
- package/base/hooks/stop.ts +0 -40
- package/base/skills/build-a-feature/evals/evals.json +0 -29
- package/base/skills/docs-update/evals/evals.json +0 -23
- package/base/skills/docs-writing/evals/evals.json +0 -17
- package/base/skills/fix-a-bug/evals/evals.json +0 -17
- package/base/skills/knowledge-sync/SKILL.md +0 -81
- package/base/skills/knowledge-sync/knowledge-sync.workflow.js +0 -291
- package/base/skills/knowledge-sync/references/workflow.md +0 -44
- package/base/skills/plan-a-change/evals/evals.json +0 -47
- package/base/skills/report-back/evals/evals.json +0 -23
- package/base/skills/research/evals/evals.json +0 -17
- package/base/skills/retro/evals/evals.json +0 -17
- package/base/skills/review-a-change/evals/evals.json +0 -17
- package/base/skills/ship/evals/evals.json +0 -23
- package/base/skills/visualize-work/evals/evals.json +0 -17
- package/base/templates/ci/github-actions.yml +0 -75
- package/base/templates/ci/gitlab-ci.yml +0 -52
- package/base/templates/trusted-hosts.local.example +0 -11
- package/src/ci/affected.ts +0 -176
- package/src/ci/command.ts +0 -166
- package/src/ci/common/constants.ts +0 -76
- package/src/ci/common/index.ts +0 -2
- package/src/ci/common/types.ts +0 -37
- package/src/ci/git.ts +0 -72
- package/src/ci/global.ts +0 -48
- package/src/ci/graph.ts +0 -43
- package/src/ci/index.ts +0 -7
- package/src/ci/workspace.ts +0 -119
- package/src/commands/ci.command.ts +0 -4
- package/src/hooks/guard-manifest.ts +0 -109
- package/src/hooks/guard-package.ts +0 -226
- package/src/hooks/guard-scripting.ts +0 -36
- package/src/hooks/host-pattern.ts +0 -17
- package/src/hooks/owner-reply.ts +0 -162
- package/src/hooks/post-tool.ts +0 -138
- package/src/hooks/remote-hosts.ts +0 -1013
- package/src/hooks/trusted-scripts.ts +0 -212
- package/src/owner/shape.ts +0 -496
- package/src/owner/violations.ts +0 -332
- package/src/signals/claims.ts +0 -73
- package/src/signals/classify.ts +0 -137
- package/src/signals/constants.ts +0 -88
- package/src/signals/index.ts +0 -5
- package/src/signals/loop.ts +0 -48
- package/src/signals/state.ts +0 -271
- package/src/sync/atlas-block.ts +0 -56
|
@@ -1,1013 +0,0 @@
|
|
|
1
|
-
import { HookConstants } from './common/index.ts';
|
|
2
|
-
import { HostPatterns } from './host-pattern.ts';
|
|
3
|
-
import type { IWriteHit } from './common/index.ts';
|
|
4
|
-
import { CommandView } from './command-view.ts';
|
|
5
|
-
import { HookDecision } from './decision.ts';
|
|
6
|
-
import { ShellParser, ShellPrograms } from './shell.ts';
|
|
7
|
-
|
|
8
|
-
// -----------------------------------------------------------------------------
|
|
9
|
-
// The remote-host guard: a Bash command that reaches a host the project has not listed asks first.
|
|
10
|
-
//
|
|
11
|
-
// Hosts are read from the command, then compared whole: exact name, or a pattern whose only wildcard is `*`
|
|
12
|
-
// (it stays inside host characters). A name is never searched for inside the command text, a user, a path
|
|
13
|
-
// or an argument - that substring match is how a trusted name once let another host through. What the guard
|
|
14
|
-
// cannot read (a substitution, a jump through a command, an unknown client) asks.
|
|
15
|
-
// -----------------------------------------------------------------------------
|
|
16
|
-
|
|
17
|
-
interface IRemoteHostsPolicy {
|
|
18
|
-
/** Hosts any command may reach. */
|
|
19
|
-
trusted: string[];
|
|
20
|
-
/** Hosts that only the strict read-only forms may reach. */
|
|
21
|
-
readOnly: string[];
|
|
22
|
-
/** Where the personal list lives, for the message. */
|
|
23
|
-
localFile?: string;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/** What the personal list holds: trusted hosts, read-only hosts, and the numbers of the lines that were skipped. */
|
|
27
|
-
interface IPersonalHosts {
|
|
28
|
-
trusted: string[];
|
|
29
|
-
readOnly: string[];
|
|
30
|
-
skipped: number[];
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const READONLY_PREFIX = 'readonly:';
|
|
34
|
-
|
|
35
|
-
interface ISegment {
|
|
36
|
-
words: string[];
|
|
37
|
-
/** `$(...)` or a backtick ran inside it: what it reaches is decided at run time. */
|
|
38
|
-
substitution: boolean;
|
|
39
|
-
/** A `<` or `>` redirect: the form is not a bare read. */
|
|
40
|
-
redirect: boolean;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
interface IFinding {
|
|
44
|
-
hosts: string[];
|
|
45
|
-
/** Why the target cannot be read; `undefined` when every host is known. */
|
|
46
|
-
unreadable?: string;
|
|
47
|
-
/** The whole form is one of the strict read-only forms. */
|
|
48
|
-
strict: boolean;
|
|
49
|
-
redirect: boolean;
|
|
50
|
-
/** The target is not a remote login: only a host the project listed matters, any other is left alone. */
|
|
51
|
-
listedOnly?: boolean;
|
|
52
|
-
/** The target is the current ambient one (a kubeconfig context): read on a host, it is that host's own. */
|
|
53
|
-
ambient?: boolean;
|
|
54
|
-
/** An ssh's remote command: it runs on the far side, and may hop on. */
|
|
55
|
-
remote?: string[];
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// --- host matching ---
|
|
59
|
-
|
|
60
|
-
const WILDCARD = '*';
|
|
61
|
-
const PATTERN_CHARACTERS = /^[a-z0-9._*-]+$/;
|
|
62
|
-
|
|
63
|
-
/** `${VAR}` as `$VAR`: the same unexpanded token. */
|
|
64
|
-
const normalizeVariable = (opts: { host: string }): string =>
|
|
65
|
-
opts.host.replace(/^\$\{(\w+)\}$/, '$$$1');
|
|
66
|
-
|
|
67
|
-
const SEPARATORS = new Set([';', '&', '|', '\n', '(', ')']);
|
|
68
|
-
|
|
69
|
-
const REDIRECTS = new Set(['<', '>']);
|
|
70
|
-
|
|
71
|
-
/** Shell words, split into the commands they form. Quotes are removed; a `#` word starts a comment. */
|
|
72
|
-
const tokenize = (opts: { command: string }): ISegment[] => {
|
|
73
|
-
const segments: ISegment[] = [];
|
|
74
|
-
let words: string[] = [];
|
|
75
|
-
let current = '';
|
|
76
|
-
let started = false;
|
|
77
|
-
let quote: string | undefined;
|
|
78
|
-
let substitution = false;
|
|
79
|
-
let redirect = false;
|
|
80
|
-
const text = opts.command;
|
|
81
|
-
|
|
82
|
-
const endWord = (): void => {
|
|
83
|
-
if (started) {
|
|
84
|
-
words.push(current);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
current = '';
|
|
88
|
-
started = false;
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
const endSegment = (): void => {
|
|
92
|
-
endWord();
|
|
93
|
-
|
|
94
|
-
if (words.length > 0) {
|
|
95
|
-
segments.push({ words, substitution, redirect });
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
words = [];
|
|
99
|
-
substitution = false;
|
|
100
|
-
redirect = false;
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
for (let index = 0; index < text.length; index++) {
|
|
104
|
-
const char = text[index];
|
|
105
|
-
const next = text[index + 1];
|
|
106
|
-
|
|
107
|
-
if (quote === "'") {
|
|
108
|
-
if (char === "'") {
|
|
109
|
-
quote = undefined;
|
|
110
|
-
} else {
|
|
111
|
-
current += char;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
continue;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
if (quote === '"') {
|
|
118
|
-
if (char === '"') {
|
|
119
|
-
quote = undefined;
|
|
120
|
-
} else if (char === '\\' && next !== undefined && '"\\$`'.includes(next)) {
|
|
121
|
-
current += next;
|
|
122
|
-
index++;
|
|
123
|
-
} else {
|
|
124
|
-
substitution ||= char === '`' || (char === '$' && next === '(');
|
|
125
|
-
current += char;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
continue;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
if (char === '\\' && next !== undefined) {
|
|
132
|
-
if (next !== '\n') {
|
|
133
|
-
current += next;
|
|
134
|
-
started = true;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
index++;
|
|
138
|
-
} else if (char === "'" || char === '"') {
|
|
139
|
-
quote = char;
|
|
140
|
-
started = true;
|
|
141
|
-
} else if (char === '`' || (char === '$' && next === '(')) {
|
|
142
|
-
substitution = true;
|
|
143
|
-
current += char;
|
|
144
|
-
started = true;
|
|
145
|
-
} else if (char === '#' && !started) {
|
|
146
|
-
while (index < text.length && text[index] !== '\n') {
|
|
147
|
-
index++;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
endSegment();
|
|
151
|
-
} else if (char === ' ' || char === '\t' || char === '\r') {
|
|
152
|
-
endWord();
|
|
153
|
-
} else if (SEPARATORS.has(char)) {
|
|
154
|
-
endSegment();
|
|
155
|
-
} else if (REDIRECTS.has(char)) {
|
|
156
|
-
redirect = true;
|
|
157
|
-
endWord();
|
|
158
|
-
|
|
159
|
-
// Input from a file or a string is kept as a word, so a shell fed that way is seen reading it.
|
|
160
|
-
if (char === '<') {
|
|
161
|
-
words.push(char);
|
|
162
|
-
}
|
|
163
|
-
} else {
|
|
164
|
-
current += char;
|
|
165
|
-
started = true;
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
endSegment();
|
|
170
|
-
|
|
171
|
-
return segments;
|
|
172
|
-
};
|
|
173
|
-
|
|
174
|
-
const basenameOf = (opts: { word: string }): string =>
|
|
175
|
-
(opts.word.split('/').pop() ?? opts.word).toLowerCase();
|
|
176
|
-
|
|
177
|
-
/** The host of `[user@]host`, `ssh://user@host:port/` or `[v6]`; lowercase, no user, no port in a URL. */
|
|
178
|
-
const hostOf = (opts: { value: string }): string => {
|
|
179
|
-
const url = /^(?:ssh|scp|sftp|rsync):\/\/(?:[^@/]*@)?(\[[^\]]+\]|[^/:]+)/i.exec(opts.value);
|
|
180
|
-
const bare = url?.[1] ?? opts.value.slice(opts.value.lastIndexOf('@') + 1);
|
|
181
|
-
|
|
182
|
-
return bare
|
|
183
|
-
.replace(/^\[|\]$/g, '')
|
|
184
|
-
.replace(/\.$/, '')
|
|
185
|
-
.toLowerCase();
|
|
186
|
-
};
|
|
187
|
-
|
|
188
|
-
/** The host of `scheme://[user@]host[:port]/path` or `[user@]host[:port]/path`; `undefined` for a socket or nothing. */
|
|
189
|
-
const authorityOf = (opts: { value: string }): string | undefined => {
|
|
190
|
-
const rest = /[a-z][a-z0-9+.-]*:\/\/(.*)$/i.exec(opts.value)?.[1] ?? opts.value;
|
|
191
|
-
const host = /^(?:[^@/?#]*@)?(\[[^\]]+\]|[^/:?#\s]+)/.exec(rest)?.[1];
|
|
192
|
-
|
|
193
|
-
return host === undefined
|
|
194
|
-
? undefined
|
|
195
|
-
: host
|
|
196
|
-
.replace(/^\[|\]$/g, '')
|
|
197
|
-
.replace(/\.$/, '')
|
|
198
|
-
.toLowerCase();
|
|
199
|
-
};
|
|
200
|
-
|
|
201
|
-
const ENVIRONMENT_ASSIGNMENT = /^[A-Za-z_][A-Za-z0-9_]*=/;
|
|
202
|
-
|
|
203
|
-
/** Words that open a command inside a loop, a condition or a group: the command is what follows. */
|
|
204
|
-
const SHELL_KEYWORDS = new Set([
|
|
205
|
-
'do',
|
|
206
|
-
'then',
|
|
207
|
-
'else',
|
|
208
|
-
'elif',
|
|
209
|
-
'if',
|
|
210
|
-
'while',
|
|
211
|
-
'until',
|
|
212
|
-
'!',
|
|
213
|
-
'{',
|
|
214
|
-
'coproc',
|
|
215
|
-
]);
|
|
216
|
-
|
|
217
|
-
const HTTP_CLIENTS = new Set(['curl', 'wget', 'http', 'https', 'httpie', 'xh', 'xhs']);
|
|
218
|
-
|
|
219
|
-
/** Programs that run the rest of their line: the names and the options each takes a value for are in `hooks/shell.ts`; `command` is handled apart below. */
|
|
220
|
-
const WRAPPERS = new Set(ShellPrograms.WRAPPER_NAMES.filter((name) => name !== 'command'));
|
|
221
|
-
|
|
222
|
-
const SHELLS = new Set([...ShellPrograms.SHELL_NAMES, 'ksh', 'ash']);
|
|
223
|
-
|
|
224
|
-
/** Look a command up or describe it; running none of the words that follow. */
|
|
225
|
-
const LOOKUPS = new Set(['man', 'which', 'type', 'whereis', 'apropos', 'whatis', 'echo', 'printf']);
|
|
226
|
-
|
|
227
|
-
const SSH_ARGUMENT_LETTERS: Record<string, string> = {
|
|
228
|
-
ssh: 'BbcDEeFIiJLlmOopQRSWw',
|
|
229
|
-
sftp: 'BbcDFiJlOoPRSsX',
|
|
230
|
-
scp: 'cDFiJlOoPSX',
|
|
231
|
-
mosh: 'p',
|
|
232
|
-
'ssh-copy-id': 'iopt',
|
|
233
|
-
'ssh-keyscan': 'ptT',
|
|
234
|
-
};
|
|
235
|
-
|
|
236
|
-
const NETWORK_CLIENTS = new Set(['nc', 'ncat', 'netcat', 'telnet']);
|
|
237
|
-
|
|
238
|
-
const NETWORK_ARGUMENT_LETTERS = 'pswiIOTxXPZ';
|
|
239
|
-
|
|
240
|
-
const KUBERNETES_CLIENTS = new Set(['kubectl', 'helm', 'k9s', 'oc']);
|
|
241
|
-
|
|
242
|
-
const KUBERNETES_TARGET_OPTIONS = [
|
|
243
|
-
'--context',
|
|
244
|
-
'--kube-context',
|
|
245
|
-
'--cluster',
|
|
246
|
-
'--server',
|
|
247
|
-
'--kube-apiserver',
|
|
248
|
-
'-s',
|
|
249
|
-
];
|
|
250
|
-
|
|
251
|
-
/** Clients whose target this guard does not parse: they always ask. */
|
|
252
|
-
const UNPARSED_CLIENTS =
|
|
253
|
-
/^(?:autossh|pssh|pscp|pslurp|prsync|parallel-ssh|parallel-scp|pdsh|clush|dbclient|plink|putty|socat|rsh|rcp|rlogin|ftp|lftp|tftp|cssh|tssh|ansible(?:-[\w-]+)?)$/;
|
|
254
|
-
|
|
255
|
-
const SENSITIVE_ASSIGNMENT =
|
|
256
|
-
/(?:^|[\s;&|(])(?:export\s+)?(?:git_ssh(?:_command)?|rsync_rsh|docker_host|docker_context|kubeconfig)=|core\.sshcommand/i;
|
|
257
|
-
|
|
258
|
-
const URL_HOST = /(?:ssh|scp|sftp|rsync):\/\/(?:[^@\s/'"]*@)?(\[[^\]]+\]|[^\s/:'"]+)/gi;
|
|
259
|
-
|
|
260
|
-
const DEVICE_HOST = /\/dev\/(?:tcp|udp)\/([^/\s'"]+)\//gi;
|
|
261
|
-
|
|
262
|
-
const UNSAFE_OPTION_KEYS = new Set(['proxycommand', 'localcommand', 'knownhostscommand']);
|
|
263
|
-
|
|
264
|
-
const LABEL = '\\w[\\w-]*';
|
|
265
|
-
|
|
266
|
-
const REMOTE_PATH = '/[\\w./-]*';
|
|
267
|
-
|
|
268
|
-
const LOCAL_PATH = '(?:\\./)?\\w[\\w-]*(?:[./][\\w-]+)*/?';
|
|
269
|
-
|
|
270
|
-
const UNIT = '[\\w@][\\w.@-]*';
|
|
271
|
-
|
|
272
|
-
const READ_ONLY_REMOTE = new RegExp(
|
|
273
|
-
`^(?:${[
|
|
274
|
-
'uptime',
|
|
275
|
-
'hostname',
|
|
276
|
-
'df -h',
|
|
277
|
-
'free -m',
|
|
278
|
-
'docker ps(?: -a)?',
|
|
279
|
-
'docker compose ps',
|
|
280
|
-
`ls(?: -[a-zA-Z]+)? ${REMOTE_PATH}`,
|
|
281
|
-
`cat ${REMOTE_PATH}`,
|
|
282
|
-
`tail -n \\d+ ${REMOTE_PATH}`,
|
|
283
|
-
`systemctl status ${UNIT}(?: --no-pager)?`,
|
|
284
|
-
`journalctl -u ${UNIT} -n \\d+ --no-pager`,
|
|
285
|
-
`docker logs --tail \\d+ ${UNIT}`,
|
|
286
|
-
].join('|')})$`,
|
|
287
|
-
);
|
|
288
|
-
|
|
289
|
-
const COPY_SOURCE = new RegExp(`^(?:${LABEL}(?:\\.${LABEL})*@)?[^@\\s/:]+:${REMOTE_PATH}$`);
|
|
290
|
-
|
|
291
|
-
const COPY_TARGET = new RegExp(`^${LOCAL_PATH}$`);
|
|
292
|
-
|
|
293
|
-
const SCP_SAFE_FLAGS = /^-[rpqC]+$/;
|
|
294
|
-
|
|
295
|
-
const RSYNC_SAFE_FLAGS = /^-[avzrhPt]+$/;
|
|
296
|
-
|
|
297
|
-
const KUBECTL_READ_VERBS = new Set(['get', 'describe', 'logs', 'top']);
|
|
298
|
-
|
|
299
|
-
const KUBECTL_SAFE_WORD = /^[\w./=:,@-]+$/;
|
|
300
|
-
|
|
301
|
-
const unreadable = (opts: { why: string }): IFinding => ({
|
|
302
|
-
hosts: [],
|
|
303
|
-
unreadable: opts.why,
|
|
304
|
-
strict: false,
|
|
305
|
-
redirect: false,
|
|
306
|
-
});
|
|
307
|
-
|
|
308
|
-
/** What one ssh-family option means for where the connection goes. */
|
|
309
|
-
const optionEffect = (opts: { letter: string; value: string }): IOptionEffect => {
|
|
310
|
-
const { letter, value } = opts;
|
|
311
|
-
|
|
312
|
-
if (letter === 'J') {
|
|
313
|
-
return { hosts: value.split(',').map((jump) => hostOf({ value: jump.replace(/:\d+$/, '') })) };
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
if (letter === 'F' || letter === 'S') {
|
|
317
|
-
return { hosts: [], unreadable: `-${letter} ${value} can redirect the connection` };
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
if (letter === 'W') {
|
|
321
|
-
return { hosts: [hostOf({ value: value.replace(/:\d+$/, '') })] };
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
if (letter !== 'o') {
|
|
325
|
-
return { hosts: [] };
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
const [key = '', ...rest] = value.split(/[=\s]+/);
|
|
329
|
-
const setting = rest.join('=').trim();
|
|
330
|
-
|
|
331
|
-
if (UNSAFE_OPTION_KEYS.has(key.toLowerCase())) {
|
|
332
|
-
return { hosts: [], unreadable: `-o ${key} runs a command` };
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
if (key.toLowerCase() === 'proxyjump' && setting.toLowerCase() !== 'none') {
|
|
336
|
-
return {
|
|
337
|
-
hosts: setting.split(',').map((jump) => hostOf({ value: jump.replace(/:\d+$/, '') })),
|
|
338
|
-
};
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
return { hosts: key.toLowerCase() === 'hostname' ? [hostOf({ value: setting })] : [] };
|
|
342
|
-
};
|
|
343
|
-
|
|
344
|
-
/** `ssh`, `sftp`, `mosh`, `ssh-copy-id`, `ssh-keyscan`: options, then the destination, then a remote command. */
|
|
345
|
-
const analyzeSsh = (opts: { program: string; words: string[] }): IFinding => {
|
|
346
|
-
const letters = SSH_ARGUMENT_LETTERS[opts.program] ?? '';
|
|
347
|
-
const hosts: string[] = [];
|
|
348
|
-
const positionals: string[] = [];
|
|
349
|
-
let why: string | undefined;
|
|
350
|
-
let sawOption = false;
|
|
351
|
-
let command: string[] = [];
|
|
352
|
-
const keyscan = opts.program === 'ssh-keyscan';
|
|
353
|
-
|
|
354
|
-
for (let index = 1; index < opts.words.length; index++) {
|
|
355
|
-
const word = opts.words[index] ?? '';
|
|
356
|
-
|
|
357
|
-
if (word === '--') {
|
|
358
|
-
const rest = opts.words.slice(index + 1);
|
|
359
|
-
|
|
360
|
-
if (positionals.length === 0 || keyscan) {
|
|
361
|
-
positionals.push(...rest.slice(0, 1));
|
|
362
|
-
command = keyscan ? [] : rest.slice(1);
|
|
363
|
-
} else {
|
|
364
|
-
command = rest;
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
break;
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
if (word.startsWith('--') && opts.program === 'mosh') {
|
|
371
|
-
sawOption = true;
|
|
372
|
-
why ??= /^--(?:ssh|server)\b/.test(word) ? `${word.split('=')[0]} runs a command` : undefined;
|
|
373
|
-
continue;
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
if (word.startsWith('-') && word.length > 1) {
|
|
377
|
-
sawOption = true;
|
|
378
|
-
|
|
379
|
-
for (let position = 1; position < word.length; position++) {
|
|
380
|
-
const letter = word[position] ?? '';
|
|
381
|
-
|
|
382
|
-
if (keyscan && letter === 'f') {
|
|
383
|
-
why ??= '-f reads the hosts from a file';
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
if (!letters.includes(letter)) {
|
|
387
|
-
continue;
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
const attached = word.slice(position + 1);
|
|
391
|
-
const value = attached !== '' ? attached : (opts.words[++index] ?? '');
|
|
392
|
-
const effect = optionEffect({ letter, value });
|
|
393
|
-
|
|
394
|
-
hosts.push(...effect.hosts);
|
|
395
|
-
why ??= effect.unreadable;
|
|
396
|
-
break;
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
continue;
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
if (positionals.length === 0 || keyscan) {
|
|
403
|
-
positionals.push(word);
|
|
404
|
-
continue;
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
command = opts.words.slice(index);
|
|
408
|
-
break;
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
if (positionals.length === 0) {
|
|
412
|
-
return unreadable({ why: `${opts.program} names no destination` });
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
hosts.push(...positionals.map((positional) => hostOf({ value: positional })));
|
|
416
|
-
|
|
417
|
-
return {
|
|
418
|
-
hosts,
|
|
419
|
-
...(why === undefined ? {} : { unreadable: why }),
|
|
420
|
-
strict:
|
|
421
|
-
opts.program === 'ssh' &&
|
|
422
|
-
!sawOption &&
|
|
423
|
-
command.length > 0 &&
|
|
424
|
-
READ_ONLY_REMOTE.test(command.join(' ')),
|
|
425
|
-
redirect: false,
|
|
426
|
-
...(opts.program === 'ssh' && command.length > 0 ? { remote: command } : {}),
|
|
427
|
-
};
|
|
428
|
-
};
|
|
429
|
-
|
|
430
|
-
/** `user@host:path`, `host::module` or a `scheme://` URL: the host, else `undefined` for a local path. */
|
|
431
|
-
const copySpecHost = (opts: { word: string }): string | undefined => {
|
|
432
|
-
const { word } = opts;
|
|
433
|
-
|
|
434
|
-
if (word.startsWith('-') || /^(?:\/|\.{1,2}\/|~)/.test(word)) {
|
|
435
|
-
return undefined;
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
if (/^(?:ssh|scp|sftp|rsync):\/\//i.test(word)) {
|
|
439
|
-
return hostOf({ value: word });
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
const spec = /^(?:[^@\s/:]+@)?(\[[0-9a-f:.]+\]|[^@\s/:]+):/i.exec(word);
|
|
443
|
-
|
|
444
|
-
return spec?.[1] === undefined ? undefined : hostOf({ value: spec[1] });
|
|
445
|
-
};
|
|
446
|
-
|
|
447
|
-
/** `-e`/`--rsh` that is a plain `ssh` (with a port or key) leaves the host as typed; anything else may add a hop. */
|
|
448
|
-
const PLAIN_RSH = /^ssh(?: -[pi] \S+)*$/;
|
|
449
|
-
|
|
450
|
-
/** `scp`, `rsync`, `sshfs`: every `host:path` is a host; options may add a jump or run a command. */
|
|
451
|
-
const analyzeCopy = (opts: { program: string; words: string[] }): IFinding => {
|
|
452
|
-
const hosts: string[] = [];
|
|
453
|
-
const positionals: string[] = [];
|
|
454
|
-
const flags: string[] = [];
|
|
455
|
-
let why: string | undefined;
|
|
456
|
-
const scp = opts.program === 'scp' || opts.program === 'sshfs';
|
|
457
|
-
const letters = SSH_ARGUMENT_LETTERS.scp ?? '';
|
|
458
|
-
|
|
459
|
-
for (let index = 1; index < opts.words.length; index++) {
|
|
460
|
-
const word = opts.words[index] ?? '';
|
|
461
|
-
|
|
462
|
-
if (word.startsWith('--')) {
|
|
463
|
-
const rsh = /^--rsh(?:=(.*))?$/.exec(word);
|
|
464
|
-
|
|
465
|
-
if (rsh !== null) {
|
|
466
|
-
const value = rsh[1] ?? opts.words[++index] ?? '';
|
|
467
|
-
|
|
468
|
-
why ??= PLAIN_RSH.test(value)
|
|
469
|
-
? undefined
|
|
470
|
-
: `--rsh "${value}" can add a hop or run a command`;
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
flags.push(word);
|
|
474
|
-
continue;
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
if (word.startsWith('-') && word.length > 1) {
|
|
478
|
-
flags.push(word);
|
|
479
|
-
|
|
480
|
-
for (let position = 1; position < word.length; position++) {
|
|
481
|
-
const letter = word[position] ?? '';
|
|
482
|
-
const takesArgument = scp ? letters.includes(letter) : letter === 'e';
|
|
483
|
-
|
|
484
|
-
if (!takesArgument) {
|
|
485
|
-
continue;
|
|
486
|
-
}
|
|
487
|
-
|
|
488
|
-
const attached = word.slice(position + 1);
|
|
489
|
-
const value = attached !== '' ? attached : (opts.words[++index] ?? '');
|
|
490
|
-
|
|
491
|
-
if (letter === 'e') {
|
|
492
|
-
why ??= PLAIN_RSH.test(value)
|
|
493
|
-
? undefined
|
|
494
|
-
: `-e "${value}" can add a hop or run a command`;
|
|
495
|
-
} else {
|
|
496
|
-
const effect = optionEffect({ letter, value });
|
|
497
|
-
|
|
498
|
-
hosts.push(...effect.hosts);
|
|
499
|
-
why ??= effect.unreadable;
|
|
500
|
-
}
|
|
501
|
-
|
|
502
|
-
break;
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
continue;
|
|
506
|
-
}
|
|
507
|
-
|
|
508
|
-
positionals.push(word);
|
|
509
|
-
}
|
|
510
|
-
|
|
511
|
-
const specs = positionals.map((positional) => copySpecHost({ word: positional }));
|
|
512
|
-
|
|
513
|
-
hosts.push(...specs.flatMap((spec) => (spec === undefined ? [] : [spec])));
|
|
514
|
-
|
|
515
|
-
const safe = flags.every((flag) => (scp ? SCP_SAFE_FLAGS : RSYNC_SAFE_FLAGS).test(flag));
|
|
516
|
-
const [source = '', target = ''] = positionals;
|
|
517
|
-
|
|
518
|
-
return {
|
|
519
|
-
hosts,
|
|
520
|
-
...(why === undefined ? {} : { unreadable: why }),
|
|
521
|
-
strict:
|
|
522
|
-
safe &&
|
|
523
|
-
positionals.length === 2 &&
|
|
524
|
-
COPY_SOURCE.test(source) &&
|
|
525
|
-
specs[0] !== undefined &&
|
|
526
|
-
specs[1] === undefined &&
|
|
527
|
-
COPY_TARGET.test(target),
|
|
528
|
-
redirect: false,
|
|
529
|
-
};
|
|
530
|
-
};
|
|
531
|
-
|
|
532
|
-
/** The value of `--name value` / `--name=value`, wherever it stands. */
|
|
533
|
-
const optionValues = (opts: { words: string[]; names: string[] }): string[] =>
|
|
534
|
-
opts.words.flatMap((word, index) => {
|
|
535
|
-
const name = opts.names.find(
|
|
536
|
-
(candidate) => word === candidate || word.startsWith(`${candidate}=`),
|
|
537
|
-
);
|
|
538
|
-
|
|
539
|
-
if (name === undefined) {
|
|
540
|
-
return [];
|
|
541
|
-
}
|
|
542
|
-
|
|
543
|
-
return [word === name ? (opts.words[index + 1] ?? '') : word.slice(name.length + 1)];
|
|
544
|
-
});
|
|
545
|
-
|
|
546
|
-
const analyzeKubernetes = (opts: { program: string; words: string[] }): IFinding => {
|
|
547
|
-
if (opts.words.some((word) => word === '--kubeconfig' || word.startsWith('--kubeconfig='))) {
|
|
548
|
-
return unreadable({ why: '--kubeconfig points at contexts this guard cannot read' });
|
|
549
|
-
}
|
|
550
|
-
|
|
551
|
-
const targets = optionValues({ words: opts.words, names: KUBERNETES_TARGET_OPTIONS });
|
|
552
|
-
|
|
553
|
-
if (targets.length === 0) {
|
|
554
|
-
return {
|
|
555
|
-
...unreadable({ why: `${opts.program} uses the current kubeconfig context` }),
|
|
556
|
-
ambient: true,
|
|
557
|
-
};
|
|
558
|
-
}
|
|
559
|
-
|
|
560
|
-
const hosts = targets.map((target) =>
|
|
561
|
-
target.includes('://') ? new URL(target).hostname : target.toLowerCase(),
|
|
562
|
-
);
|
|
563
|
-
const contexts = optionValues({ words: opts.words, names: ['--context'] });
|
|
564
|
-
const rest: string[] = [];
|
|
565
|
-
|
|
566
|
-
for (let index = 1; index < opts.words.length; index++) {
|
|
567
|
-
const word = opts.words[index] ?? '';
|
|
568
|
-
|
|
569
|
-
if (word === '--context') {
|
|
570
|
-
index++;
|
|
571
|
-
} else if (!word.startsWith('--context=')) {
|
|
572
|
-
rest.push(word);
|
|
573
|
-
}
|
|
574
|
-
}
|
|
575
|
-
|
|
576
|
-
const [verb = ''] = rest;
|
|
577
|
-
|
|
578
|
-
return {
|
|
579
|
-
hosts,
|
|
580
|
-
strict:
|
|
581
|
-
opts.program === 'kubectl' &&
|
|
582
|
-
contexts.length === 1 &&
|
|
583
|
-
targets.length === 1 &&
|
|
584
|
-
KUBECTL_READ_VERBS.has(verb) &&
|
|
585
|
-
rest.every((word) => KUBECTL_SAFE_WORD.test(word)),
|
|
586
|
-
redirect: false,
|
|
587
|
-
};
|
|
588
|
-
};
|
|
589
|
-
|
|
590
|
-
const analyzeNetwork = (opts: { words: string[] }): IFinding => {
|
|
591
|
-
const positionals: string[] = [];
|
|
592
|
-
|
|
593
|
-
for (let index = 1; index < opts.words.length; index++) {
|
|
594
|
-
const word = opts.words[index] ?? '';
|
|
595
|
-
|
|
596
|
-
if (word.startsWith('-') && word.length > 1) {
|
|
597
|
-
const last = word[word.length - 1] ?? '';
|
|
598
|
-
|
|
599
|
-
if (NETWORK_ARGUMENT_LETTERS.includes(last)) {
|
|
600
|
-
index++;
|
|
601
|
-
}
|
|
602
|
-
|
|
603
|
-
continue;
|
|
604
|
-
}
|
|
605
|
-
|
|
606
|
-
positionals.push(word);
|
|
607
|
-
}
|
|
608
|
-
|
|
609
|
-
return positionals.length === 0
|
|
610
|
-
? unreadable({ why: 'no host to connect to' })
|
|
611
|
-
: { hosts: [hostOf({ value: positionals[0] ?? '' })], strict: false, redirect: false };
|
|
612
|
-
};
|
|
613
|
-
|
|
614
|
-
/** `curl`, `wget`, `http`: a URL or bare address names a host; only a listed one matters. */
|
|
615
|
-
const analyzeHttp = (opts: { words: string[] }): IFinding => {
|
|
616
|
-
const hosts = opts.words.slice(1).flatMap((word) => {
|
|
617
|
-
const candidate = /\s/.test(word) ? /[a-z][a-z0-9+.-]*:\/\/\S*/i.exec(word)?.[0] : word;
|
|
618
|
-
|
|
619
|
-
if (candidate === undefined || (candidate.startsWith('-') && !candidate.includes('://'))) {
|
|
620
|
-
return [];
|
|
621
|
-
}
|
|
622
|
-
|
|
623
|
-
const host = authorityOf({ value: candidate });
|
|
624
|
-
|
|
625
|
-
return host === undefined ? [] : [host];
|
|
626
|
-
});
|
|
627
|
-
|
|
628
|
-
return { hosts, strict: false, redirect: false, listedOnly: true };
|
|
629
|
-
};
|
|
630
|
-
|
|
631
|
-
/** `-H`/`--host` name the daemon; a context or `docker context` resolves elsewhere and cannot be read. */
|
|
632
|
-
const analyzeDocker = (opts: { words: string[] }): IFinding => {
|
|
633
|
-
const hosts: string[] = [];
|
|
634
|
-
|
|
635
|
-
for (let index = 1; index < opts.words.length; index++) {
|
|
636
|
-
const word = opts.words[index] ?? '';
|
|
637
|
-
|
|
638
|
-
if (/^(?:-c|--context)(?:=|$)/.test(word) || word === 'context') {
|
|
639
|
-
return unreadable({ why: 'docker is pointed at another host or context' });
|
|
640
|
-
}
|
|
641
|
-
|
|
642
|
-
const long = /^--host(?:=(.*))?$/.exec(word);
|
|
643
|
-
const short = /^-H(.*)$/.exec(word);
|
|
644
|
-
const given = long ?? short;
|
|
645
|
-
|
|
646
|
-
if (given === null) {
|
|
647
|
-
continue;
|
|
648
|
-
}
|
|
649
|
-
|
|
650
|
-
const value = given[1] ?? '';
|
|
651
|
-
const host = authorityOf({ value: value === '' ? (opts.words[++index] ?? '') : value });
|
|
652
|
-
|
|
653
|
-
if (host !== undefined) {
|
|
654
|
-
hosts.push(host);
|
|
655
|
-
}
|
|
656
|
-
}
|
|
657
|
-
|
|
658
|
-
return { hosts, strict: false, redirect: false };
|
|
659
|
-
};
|
|
660
|
-
|
|
661
|
-
/** Whether a shell with no `-c` takes its commands from its input: no script file, or `-s`. */
|
|
662
|
-
const readsItsInput = (opts: { words: string[] }): boolean => {
|
|
663
|
-
if (opts.words.includes('<')) {
|
|
664
|
-
return true;
|
|
665
|
-
}
|
|
666
|
-
|
|
667
|
-
for (const word of opts.words.slice(1)) {
|
|
668
|
-
if (!word.startsWith('-')) {
|
|
669
|
-
return false;
|
|
670
|
-
}
|
|
671
|
-
|
|
672
|
-
if (/^-[a-z]*s[a-z]*$/.test(word)) {
|
|
673
|
-
return true;
|
|
674
|
-
}
|
|
675
|
-
}
|
|
676
|
-
|
|
677
|
-
return true;
|
|
678
|
-
};
|
|
679
|
-
|
|
680
|
-
const MAXIMUM_DEPTH = 4;
|
|
681
|
-
|
|
682
|
-
/** Skips a wrapper's own options (and the values its options take); the words after are its command. */
|
|
683
|
-
const unwrap = (opts: { program: string; words: string[] }): string[] => {
|
|
684
|
-
let index = 1;
|
|
685
|
-
|
|
686
|
-
while (index < opts.words.length) {
|
|
687
|
-
const word = opts.words[index] ?? '';
|
|
688
|
-
|
|
689
|
-
if (word.startsWith('-') && word.length > 1) {
|
|
690
|
-
index += ShellParser.optionTakesNextWord({ wrapper: opts.program, word }) ? 2 : 1;
|
|
691
|
-
} else if (
|
|
692
|
-
ENVIRONMENT_ASSIGNMENT.test(word) ||
|
|
693
|
-
(/^\d[\w.]*$/.test(word) && opts.program !== 'sshpass')
|
|
694
|
-
) {
|
|
695
|
-
index++;
|
|
696
|
-
} else {
|
|
697
|
-
break;
|
|
698
|
-
}
|
|
699
|
-
}
|
|
700
|
-
|
|
701
|
-
return opts.words.slice(index);
|
|
702
|
-
};
|
|
703
|
-
|
|
704
|
-
/** The findings of one command's words, following wrappers, `sh -c`, `eval` and `find -exec`. */
|
|
705
|
-
const analyzeWords = (opts: {
|
|
706
|
-
words: string[];
|
|
707
|
-
depth: number;
|
|
708
|
-
/** Reads a command string (`sh -c`, `eval`): the whole analysis again, one level deeper. */
|
|
709
|
-
recurse: (opts: { command: string; depth: number }) => IFinding[];
|
|
710
|
-
}): IFinding[] => {
|
|
711
|
-
const words = [...opts.words];
|
|
712
|
-
|
|
713
|
-
while (
|
|
714
|
-
words[0] !== undefined &&
|
|
715
|
-
(ENVIRONMENT_ASSIGNMENT.test(words[0]) || SHELL_KEYWORDS.has(words[0]))
|
|
716
|
-
) {
|
|
717
|
-
words.shift();
|
|
718
|
-
}
|
|
719
|
-
|
|
720
|
-
const program = basenameOf({ word: words[0] ?? '' });
|
|
721
|
-
|
|
722
|
-
// `rg -n "ssh|scp" docs/`: the arguments of a search or a print are data.
|
|
723
|
-
if (
|
|
724
|
-
CommandView.isDataOnly({
|
|
725
|
-
command: { words, redirects: [], assignments: [] },
|
|
726
|
-
withMessages: false,
|
|
727
|
-
})
|
|
728
|
-
) {
|
|
729
|
-
return [];
|
|
730
|
-
}
|
|
731
|
-
|
|
732
|
-
if (program === '' || LOOKUPS.has(program) || opts.depth > MAXIMUM_DEPTH) {
|
|
733
|
-
return opts.depth > MAXIMUM_DEPTH ? [unreadable({ why: 'the command nests too deeply' })] : [];
|
|
734
|
-
}
|
|
735
|
-
|
|
736
|
-
if (program === 'command' && words.some((word) => word === '-v' || word === '-V')) {
|
|
737
|
-
return [];
|
|
738
|
-
}
|
|
739
|
-
|
|
740
|
-
if (program === 'env') {
|
|
741
|
-
// `env -S "ssh host"` carries a whole command line in one word: the command it makes is read as one.
|
|
742
|
-
const splits = ShellParser.programOf({
|
|
743
|
-
command: { words, redirects: [], assignments: [] },
|
|
744
|
-
}).splitCommands;
|
|
745
|
-
|
|
746
|
-
if (splits.length > 0) {
|
|
747
|
-
return splits.flatMap((splitWords) =>
|
|
748
|
-
analyzeWords({ words: splitWords, depth: opts.depth + 1, recurse: opts.recurse }),
|
|
749
|
-
);
|
|
750
|
-
}
|
|
751
|
-
}
|
|
752
|
-
|
|
753
|
-
if (WRAPPERS.has(program) || program === 'command' || program === 'builtin') {
|
|
754
|
-
return analyzeWords({
|
|
755
|
-
words: unwrap({ program, words }),
|
|
756
|
-
depth: opts.depth + 1,
|
|
757
|
-
recurse: opts.recurse,
|
|
758
|
-
});
|
|
759
|
-
}
|
|
760
|
-
|
|
761
|
-
if (program === 'eval') {
|
|
762
|
-
return opts.recurse({
|
|
763
|
-
command: words.slice(1).join(' '),
|
|
764
|
-
depth: opts.depth + 1,
|
|
765
|
-
});
|
|
766
|
-
}
|
|
767
|
-
|
|
768
|
-
if (SHELLS.has(program)) {
|
|
769
|
-
const flag = words.findIndex((word, index) => index > 0 && /^-[a-z]*c[a-z]*$/.test(word));
|
|
770
|
-
|
|
771
|
-
if (flag !== -1) {
|
|
772
|
-
return opts.recurse({
|
|
773
|
-
command: words[flag + 1] ?? '',
|
|
774
|
-
depth: opts.depth + 1,
|
|
775
|
-
});
|
|
776
|
-
}
|
|
777
|
-
|
|
778
|
-
return readsItsInput({ words })
|
|
779
|
-
? [unreadable({ why: `${program} runs commands it reads from its input` })]
|
|
780
|
-
: [];
|
|
781
|
-
}
|
|
782
|
-
|
|
783
|
-
if (program === 'find') {
|
|
784
|
-
const start = words.findIndex((word) => ['-exec', '-execdir', '-ok', '-okdir'].includes(word));
|
|
785
|
-
|
|
786
|
-
if (start === -1) {
|
|
787
|
-
return [];
|
|
788
|
-
}
|
|
789
|
-
|
|
790
|
-
const tail = words.slice(start + 1);
|
|
791
|
-
const end = tail.findIndex((word) => word === ';' || word === '+');
|
|
792
|
-
|
|
793
|
-
return analyzeWords({
|
|
794
|
-
words: end === -1 ? tail : tail.slice(0, end),
|
|
795
|
-
depth: opts.depth + 1,
|
|
796
|
-
recurse: opts.recurse,
|
|
797
|
-
});
|
|
798
|
-
}
|
|
799
|
-
|
|
800
|
-
if (program === 'docker') {
|
|
801
|
-
return [analyzeDocker({ words })];
|
|
802
|
-
}
|
|
803
|
-
|
|
804
|
-
if (HTTP_CLIENTS.has(program)) {
|
|
805
|
-
return [analyzeHttp({ words })];
|
|
806
|
-
}
|
|
807
|
-
|
|
808
|
-
if (program in SSH_ARGUMENT_LETTERS && program !== 'scp') {
|
|
809
|
-
const finding = analyzeSsh({ program, words });
|
|
810
|
-
const nested =
|
|
811
|
-
finding.remote === undefined
|
|
812
|
-
? []
|
|
813
|
-
: opts
|
|
814
|
-
.recurse({ command: finding.remote.join(' '), depth: opts.depth + 1 })
|
|
815
|
-
.filter((inner) => inner.ambient !== true);
|
|
816
|
-
|
|
817
|
-
return [finding, ...nested];
|
|
818
|
-
}
|
|
819
|
-
|
|
820
|
-
if (program === 'scp' || program === 'rsync' || program === 'sshfs') {
|
|
821
|
-
return [analyzeCopy({ program, words })];
|
|
822
|
-
}
|
|
823
|
-
|
|
824
|
-
if (KUBERNETES_CLIENTS.has(program)) {
|
|
825
|
-
return [analyzeKubernetes({ program, words })];
|
|
826
|
-
}
|
|
827
|
-
|
|
828
|
-
if (NETWORK_CLIENTS.has(program)) {
|
|
829
|
-
return [analyzeNetwork({ words })];
|
|
830
|
-
}
|
|
831
|
-
|
|
832
|
-
return UNPARSED_CLIENTS.test(program)
|
|
833
|
-
? [unreadable({ why: `the target of ${program} is not parsed` })]
|
|
834
|
-
: [];
|
|
835
|
-
};
|
|
836
|
-
|
|
837
|
-
const analyzeCommand = (opts: { command: string; depth: number }): IFinding[] =>
|
|
838
|
-
// A heredoc body that is data (`cat > f <<EOF`) is not a command; one a shell reads stays in the text.
|
|
839
|
-
tokenize({ command: ShellParser.splitHeredocs({ command: opts.command }).text }).flatMap(
|
|
840
|
-
(segment) => {
|
|
841
|
-
const findings = analyzeWords({
|
|
842
|
-
words: segment.words,
|
|
843
|
-
depth: opts.depth,
|
|
844
|
-
recurse: analyzeCommand,
|
|
845
|
-
});
|
|
846
|
-
|
|
847
|
-
return findings.map((finding): IFinding =>
|
|
848
|
-
segment.substitution && (finding.hosts.length > 0 || finding.unreadable !== undefined)
|
|
849
|
-
? {
|
|
850
|
-
...finding,
|
|
851
|
-
unreadable: finding.unreadable ?? 'a substitution runs inside it',
|
|
852
|
-
redirect: finding.redirect || segment.redirect,
|
|
853
|
-
}
|
|
854
|
-
: { ...finding, redirect: finding.redirect || segment.redirect },
|
|
855
|
-
);
|
|
856
|
-
},
|
|
857
|
-
);
|
|
858
|
-
|
|
859
|
-
const trustedMessage = (opts: { host: string; policy: IRemoteHostsPolicy }): string =>
|
|
860
|
-
`reaching '${opts.host}', which is not in remoteHosts.trusted (list it there${opts.policy.localFile === undefined ? '' : `, or in ${opts.policy.localFile} for yourself`}); a remoteHosts.readOnly host passes only the strict read-only forms`;
|
|
861
|
-
|
|
862
|
-
const readOnlyMessage = (opts: { host: string }): string =>
|
|
863
|
-
`'${opts.host}' is a remoteHosts.readOnly host: only a strict read-only form passes (uptime, df -h, cat <path>, scp/rsync from it, kubectl get|describe|logs|top), and this command is not one`;
|
|
864
|
-
|
|
865
|
-
export class RemoteHosts {
|
|
866
|
-
/** Whether `host` is one of `entries`: whole-name equal, or a `*` pattern. Case-insensitive. */
|
|
867
|
-
static matches(opts: { host: string; entries: string[] }): boolean {
|
|
868
|
-
const host = normalizeVariable({ host: opts.host.toLowerCase() });
|
|
869
|
-
|
|
870
|
-
return opts.entries.some((entry) => {
|
|
871
|
-
const wanted = normalizeVariable({ host: entry.toLowerCase() });
|
|
872
|
-
|
|
873
|
-
if (!wanted.includes(WILDCARD)) {
|
|
874
|
-
return host === wanted;
|
|
875
|
-
}
|
|
876
|
-
|
|
877
|
-
if (
|
|
878
|
-
!PATTERN_CHARACTERS.test(wanted) ||
|
|
879
|
-
wanted.replaceAll(WILDCARD, '').replaceAll('.', '') === ''
|
|
880
|
-
) {
|
|
881
|
-
return false;
|
|
882
|
-
}
|
|
883
|
-
|
|
884
|
-
const pattern = wanted
|
|
885
|
-
.split(WILDCARD)
|
|
886
|
-
.map((part) => part.replace(/[.*+?^${}()|[\]\\-]/g, '\\$&'))
|
|
887
|
-
.join('[a-z0-9._-]*');
|
|
888
|
-
|
|
889
|
-
return new RegExp(`^${pattern}$`).test(host);
|
|
890
|
-
});
|
|
891
|
-
}
|
|
892
|
-
|
|
893
|
-
/**
|
|
894
|
-
* The personal list: one host per line, `#` starts a comment, `readonly: <host>` is a read-only host. An entry
|
|
895
|
-
* that is not a host name or a `*` pattern (a lone `*`, a user, a port, a path) is skipped, never trusted, and its
|
|
896
|
-
* line number is returned in `skipped`.
|
|
897
|
-
*/
|
|
898
|
-
static parsePersonalList(opts: { text: string }): IPersonalHosts {
|
|
899
|
-
const personal: IPersonalHosts = { trusted: [], readOnly: [], skipped: [] };
|
|
900
|
-
|
|
901
|
-
opts.text.split('\n').forEach((raw, index) => {
|
|
902
|
-
const line = raw.replace(/#.*$/, '').trim();
|
|
903
|
-
|
|
904
|
-
if (line === '') {
|
|
905
|
-
return;
|
|
906
|
-
}
|
|
907
|
-
|
|
908
|
-
const isReadOnly = line.startsWith(READONLY_PREFIX);
|
|
909
|
-
const value = isReadOnly ? line.slice(READONLY_PREFIX.length).trim() : line;
|
|
910
|
-
|
|
911
|
-
if (!HostPatterns.isValid({ value })) {
|
|
912
|
-
personal.skipped.push(index + 1);
|
|
913
|
-
|
|
914
|
-
return;
|
|
915
|
-
}
|
|
916
|
-
|
|
917
|
-
(isReadOnly ? personal.readOnly : personal.trusted).push(value);
|
|
918
|
-
});
|
|
919
|
-
|
|
920
|
-
return personal;
|
|
921
|
-
}
|
|
922
|
-
|
|
923
|
-
/** Why each host this command reaches may not be reached without asking; empty means nothing to ask. */
|
|
924
|
-
static inspectCommand(opts: { command: string; policy: IRemoteHostsPolicy }): IWriteHit[] {
|
|
925
|
-
const { command, policy } = opts;
|
|
926
|
-
const findings = analyzeCommand({ command, depth: 0 });
|
|
927
|
-
const hits: IWriteHit[] = [];
|
|
928
|
-
const asked = new Set<string>();
|
|
929
|
-
|
|
930
|
-
const ask = (message: string): void => {
|
|
931
|
-
if (!asked.has(message)) {
|
|
932
|
-
asked.add(message);
|
|
933
|
-
hits.push({ rule: 'remote-hosts', message });
|
|
934
|
-
}
|
|
935
|
-
};
|
|
936
|
-
|
|
937
|
-
for (const finding of findings) {
|
|
938
|
-
if (finding.unreadable !== undefined) {
|
|
939
|
-
ask(
|
|
940
|
-
`the target of this command cannot be read (${finding.unreadable}), so any host could be reached`,
|
|
941
|
-
);
|
|
942
|
-
continue;
|
|
943
|
-
}
|
|
944
|
-
|
|
945
|
-
for (const host of finding.hosts) {
|
|
946
|
-
if (RemoteHosts.matches({ host, entries: policy.trusted })) {
|
|
947
|
-
continue;
|
|
948
|
-
}
|
|
949
|
-
|
|
950
|
-
if (RemoteHosts.matches({ host, entries: policy.readOnly })) {
|
|
951
|
-
if (!finding.strict || finding.redirect) {
|
|
952
|
-
ask(readOnlyMessage({ host }));
|
|
953
|
-
}
|
|
954
|
-
|
|
955
|
-
continue;
|
|
956
|
-
}
|
|
957
|
-
|
|
958
|
-
if (finding.listedOnly !== true) {
|
|
959
|
-
ask(trustedMessage({ host, policy }));
|
|
960
|
-
}
|
|
961
|
-
}
|
|
962
|
-
}
|
|
963
|
-
|
|
964
|
-
const devices = [...command.matchAll(DEVICE_HOST)].map((match) =>
|
|
965
|
-
hostOf({ value: match[1] ?? '' }),
|
|
966
|
-
);
|
|
967
|
-
const urls = [...command.matchAll(URL_HOST)].map((match) => hostOf({ value: match[1] ?? '' }));
|
|
968
|
-
|
|
969
|
-
for (const host of [...devices, ...urls]) {
|
|
970
|
-
if (!RemoteHosts.matches({ host, entries: policy.trusted })) {
|
|
971
|
-
ask(trustedMessage({ host, policy }));
|
|
972
|
-
}
|
|
973
|
-
}
|
|
974
|
-
|
|
975
|
-
if (SENSITIVE_ASSIGNMENT.test(command)) {
|
|
976
|
-
ask(
|
|
977
|
-
'the target of this command cannot be read (an environment variable or git option redirects ssh, rsync, docker or kubectl)',
|
|
978
|
-
);
|
|
979
|
-
}
|
|
980
|
-
|
|
981
|
-
return hits;
|
|
982
|
-
}
|
|
983
|
-
|
|
984
|
-
static buildDecision(opts: { hits: IWriteHit[] }) {
|
|
985
|
-
return HookDecision.forHost({
|
|
986
|
-
output: {
|
|
987
|
-
hookSpecificOutput: {
|
|
988
|
-
hookEventName: HookConstants.PRE_TOOL_USE,
|
|
989
|
-
permissionDecision: HookConstants.GUARD_DECISION,
|
|
990
|
-
permissionDecisionReason: [
|
|
991
|
-
'This command may reach a remote host the project has not listed:',
|
|
992
|
-
...opts.hits.map((hit) => ` - ${hit.message}`),
|
|
993
|
-
'',
|
|
994
|
-
'Read on the host, never edit, restart or copy onto it. Produce the command and hand it over.',
|
|
995
|
-
].join('\n'),
|
|
996
|
-
},
|
|
997
|
-
},
|
|
998
|
-
});
|
|
999
|
-
}
|
|
1000
|
-
}
|
|
1001
|
-
|
|
1002
|
-
// --- reading a command ---
|
|
1003
|
-
|
|
1004
|
-
// --- the strict read-only forms ---
|
|
1005
|
-
|
|
1006
|
-
// --- per-client analysis ---
|
|
1007
|
-
|
|
1008
|
-
interface IOptionEffect {
|
|
1009
|
-
hosts: string[];
|
|
1010
|
-
unreadable?: string;
|
|
1011
|
-
}
|
|
1012
|
-
|
|
1013
|
-
// --- the decision ---
|