@peer2peer/codex-bridge 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -5
- package/dist/bridge.d.ts +48 -5
- package/dist/bridge.js +140 -38
- package/dist/hook.d.ts +11 -0
- package/dist/hook.js +107 -9
- package/dist/lib.d.ts +73 -9
- package/dist/lib.js +750 -47
- package/dist/p2p.d.ts +43 -2
- package/dist/p2p.js +30 -2
- package/dist/server.d.ts +1 -1
- package/dist/server.js +5 -1
- package/dist/tools.d.ts +15 -0
- package/dist/tools.js +76 -6
- package/dist/trusted.d.ts +13 -0
- package/dist/trusted.js +40 -0
- package/package.json +1 -1
package/dist/lib.js
CHANGED
|
@@ -124,6 +124,11 @@ export function resolveTarget(to, peers, ownSid) {
|
|
|
124
124
|
return { kind: "peer", to: `${only.own} [${only.ref}]`, peer: only };
|
|
125
125
|
if (hits.length > 1)
|
|
126
126
|
return { kind: "ambiguous", candidates: hits };
|
|
127
|
+
// A label alone names a listed session or nothing: never the user's bare address, which the CLI
|
|
128
|
+
// would send to another of their sessions (or, for this session's own label, another device of yours).
|
|
129
|
+
if (parsed.label !== undefined && parsed.ref === undefined) {
|
|
130
|
+
return { kind: "refused", code: "no-label", reason: `No session of ${inertMax(parsed.user, 80)} labelled "${inertMax(parsed.label, 40)}" is listed right now: p2p_list_peers shows the live ones.` };
|
|
131
|
+
}
|
|
127
132
|
return { kind: "cli", to: parsed.ref ? `${parsed.user} [${parsed.ref}]` : parsed.user };
|
|
128
133
|
}
|
|
129
134
|
/**
|
|
@@ -131,11 +136,30 @@ export function resolveTarget(to, peers, ownSid) {
|
|
|
131
136
|
* label and ref) when it came from one, else the user's address alone.
|
|
132
137
|
*/
|
|
133
138
|
export function eventAddress(event, peers) {
|
|
139
|
+
// One of this user's own sessions: own/sid name the session it went INTO (this one), never the sender.
|
|
140
|
+
// The sending session when the CLI knows it (this device), else no session at all.
|
|
141
|
+
if (event.from.self === true) {
|
|
142
|
+
const ref = typeof event.fromRef === "string" && REF_RE.test(event.fromRef) ? event.fromRef : undefined;
|
|
143
|
+
if (ref === undefined)
|
|
144
|
+
return `${sessionAddress({ address: event.from.address })} (another of your sessions)`;
|
|
145
|
+
const known = peers.find((p) => p.own === event.from.userId && p.ref === ref);
|
|
146
|
+
return sessionAddress({ address: event.from.address, ref, ...(known ? { label: known.label } : {}) });
|
|
147
|
+
}
|
|
134
148
|
if (event.own !== event.from.userId)
|
|
135
149
|
return sessionAddress({ address: event.from.address });
|
|
136
150
|
const known = peers.find((p) => p.own === event.own && p.sid === event.sid);
|
|
137
151
|
return sessionAddress({ address: event.from.address, ref: event.ref, ...(known ? { label: known.label } : {}) });
|
|
138
152
|
}
|
|
153
|
+
/**
|
|
154
|
+
* Where a delivery notice to a received message goes: a peer's own session by its ref, else the peer;
|
|
155
|
+
* one of this user's own sessions only by the sending session's ref (this device's record), else
|
|
156
|
+
* nowhere ("": never this session's own address, never the bare address).
|
|
157
|
+
*/
|
|
158
|
+
export function replyToOf(event) {
|
|
159
|
+
if (event.from.self === true)
|
|
160
|
+
return typeof event.fromRef === "string" && REF_RE.test(event.fromRef) ? `${event.from.userId} [${event.fromRef}]` : "";
|
|
161
|
+
return event.own === event.from.userId ? `${event.from.userId} [${event.ref}]` : event.from.userId;
|
|
162
|
+
}
|
|
139
163
|
const ASKING_APPROVALS = new Set(["untrusted", "on-request", "on-failure"]);
|
|
140
164
|
const ASKING_PERMISSION_MODES = new Set(["default", "plan"]);
|
|
141
165
|
/**
|
|
@@ -232,10 +256,12 @@ export function capHeld(list) {
|
|
|
232
256
|
const kept = [];
|
|
233
257
|
for (let i = list.length - 1; i >= 0; i--) {
|
|
234
258
|
const item = list[i];
|
|
235
|
-
|
|
259
|
+
// Your own sessions each get their own bucket (`bucket`), not one shared by all of them.
|
|
260
|
+
const key = item.bucket ?? item.from.userId;
|
|
261
|
+
const n = counts.get(key) ?? 0;
|
|
236
262
|
if (n >= HELD_PER_SENDER || kept.length >= HELD_TOTAL)
|
|
237
263
|
continue;
|
|
238
|
-
counts.set(
|
|
264
|
+
counts.set(key, n + 1);
|
|
239
265
|
kept.push(item);
|
|
240
266
|
}
|
|
241
267
|
return kept.reverse();
|
|
@@ -392,7 +418,8 @@ export const MUTATING_TOOLS = new Set([
|
|
|
392
418
|
"p2p_release",
|
|
393
419
|
]);
|
|
394
420
|
/** `p2p` verbs that change who reaches this user, or touch its keys (the mod's guard list). */
|
|
395
|
-
|
|
421
|
+
// `history`: the person's messages (and `history --clear` deletes them); a session's own come through p2p_history.
|
|
422
|
+
const MUTATING_ONE = new Set(["join", "accept", "invite", "backup", "setup", "init", "remove", "unfriend", "block", "petname", "link", "release", "history"]);
|
|
396
423
|
const MUTATING_TWO = new Set([
|
|
397
424
|
"held approve",
|
|
398
425
|
"held decline",
|
|
@@ -418,6 +445,11 @@ const MUTATING_TWO = new Set([
|
|
|
418
445
|
"project share",
|
|
419
446
|
"project private",
|
|
420
447
|
]);
|
|
448
|
+
/**
|
|
449
|
+
* Verbs refused only as the subcommand itself (common words in message text): `receipt` reports for a
|
|
450
|
+
* session (its front end's job), `own-agents` decides whether your agent sessions skip the hold (yours).
|
|
451
|
+
*/
|
|
452
|
+
const MUTATING_FIRST = new Set(["receipt", "own-agents"]);
|
|
421
453
|
/** Shell words of one simple command (shellTokens: linear, quote-aware, adjacent parts joined). */
|
|
422
454
|
function shellWords(segment) {
|
|
423
455
|
return shellTokens(segment);
|
|
@@ -472,6 +504,8 @@ export function mutatingP2PVerb(command, binNames = [], depth = 0) {
|
|
|
472
504
|
if (MUTATING_TWO.has(two))
|
|
473
505
|
return two;
|
|
474
506
|
}
|
|
507
|
+
if (MUTATING_FIRST.has(plain[0] ?? ""))
|
|
508
|
+
return plain[0];
|
|
475
509
|
for (const w of after)
|
|
476
510
|
if (MUTATING_ONE.has(w))
|
|
477
511
|
return w;
|
|
@@ -638,6 +672,24 @@ export function screenMessage(o) {
|
|
|
638
672
|
// The bridge's settings or trusted files sit where a project could plant them: nothing goes out by itself.
|
|
639
673
|
if (o.refused)
|
|
640
674
|
return { deliver: false, reason: "auto", why: `held: ${o.refused}` };
|
|
675
|
+
// One of your own agent sessions (a peer may have told it what to send): held like a peer's where a
|
|
676
|
+
// peer's would be, so an injected instruction can't drive this session unattended. P2P_AUTO_DELIVER doesn't apply.
|
|
677
|
+
if (o.ownAgent === true) {
|
|
678
|
+
if (o.mode === "asking")
|
|
679
|
+
return { deliver: true };
|
|
680
|
+
if (o.mode === "auto") {
|
|
681
|
+
return o.elsewhere
|
|
682
|
+
? { deliver: false, reason: "auto", why: "another device of yours sent it, which can't say whether your user typed it, and this Codex session runs actions without asking (`p2p own-agents deliver --devices all-other` in your user's own terminal lets those through)" }
|
|
683
|
+
: { deliver: false, reason: "auto", why: "an agent session of yours sent it, and this Codex session runs actions without asking you (`p2p own-agents deliver` in your own terminal skips this)" };
|
|
684
|
+
}
|
|
685
|
+
return {
|
|
686
|
+
deliver: false,
|
|
687
|
+
reason: "unknown",
|
|
688
|
+
why: o.elsewhere
|
|
689
|
+
? "another device of yours sent it, which can't say whether your user typed it, and this Codex session's approval mode isn't confirmed yet"
|
|
690
|
+
: "an agent session of yours sent it, and this Codex session's approval mode isn't confirmed yet",
|
|
691
|
+
};
|
|
692
|
+
}
|
|
641
693
|
if (o.fromSelf || isAutoDeliver(o.autoDeliver, o.fromUserId) || o.mode === "asking")
|
|
642
694
|
return { deliver: true };
|
|
643
695
|
if (o.mode === "auto")
|
|
@@ -704,16 +756,23 @@ export const LITERAL = '\uE000';
|
|
|
704
756
|
* parts join into one word (`''x1` and `"x"1` are `x1`), backslashes escape, `$'…'` is
|
|
705
757
|
* ANSI-C quoted, and `;`, `|`, `&`, `(`, `)`, `<`, `>` and newlines separate words. Expansions
|
|
706
758
|
* (`$X`, `$(…)`, backticks) stay as text. Linear in the length, with no cap on the number of words
|
|
707
|
-
* (a cap would let padding hide what follows it). Best effort: no aliases, no globbing.
|
|
759
|
+
* (a cap would let padding hide what follows it). Best effort: no aliases, no globbing. With
|
|
760
|
+
* `starts`: the index of each word that begins a simple command (the first word, and the first
|
|
761
|
+
* after an unquoted `;`, `|`, `&`, `(`, `)` or newline).
|
|
708
762
|
*/
|
|
709
|
-
export function shellTokens(command, markLiteral = false) {
|
|
763
|
+
export function shellTokens(command, markLiteral = false, substs, starts) {
|
|
710
764
|
const out = [];
|
|
711
765
|
let cur = '';
|
|
712
766
|
let has = false;
|
|
713
767
|
let q = '';
|
|
768
|
+
let fresh = true;
|
|
714
769
|
const push = () => {
|
|
715
|
-
if (has)
|
|
770
|
+
if (has) {
|
|
771
|
+
if (fresh)
|
|
772
|
+
starts?.add(out.length);
|
|
773
|
+
fresh = false;
|
|
716
774
|
out.push(cur);
|
|
775
|
+
}
|
|
717
776
|
cur = '';
|
|
718
777
|
has = false;
|
|
719
778
|
};
|
|
@@ -736,6 +795,17 @@ export function shellTokens(command, markLiteral = false) {
|
|
|
736
795
|
cur += markLiteral && (c === '$' || c === '`') ? LITERAL : c;
|
|
737
796
|
continue;
|
|
738
797
|
}
|
|
798
|
+
// With `substs`: a `$(…)` or backtick command stays one placeholder in its word (SUBST), its text
|
|
799
|
+
// collected for shellPaths to read (unquoted and in double quotes, where the shell runs it).
|
|
800
|
+
if (substs && ((c === '$' && command[i + 1] === '(') || c === '`')) {
|
|
801
|
+
const open = c === '`' ? 1 : 2;
|
|
802
|
+
const end = c === '`' ? closingTick(command, i + 1) : closingParen(command, i + 2);
|
|
803
|
+
substs.push(command.slice(i + open, end));
|
|
804
|
+
cur += SUBST;
|
|
805
|
+
has = true;
|
|
806
|
+
i = end;
|
|
807
|
+
continue;
|
|
808
|
+
}
|
|
739
809
|
if (q === '"') {
|
|
740
810
|
if (c === '\\' && i + 1 < command.length && '"\\$`'.includes(command[i + 1]))
|
|
741
811
|
cur += command[++i];
|
|
@@ -767,6 +837,8 @@ export function shellTokens(command, markLiteral = false) {
|
|
|
767
837
|
}
|
|
768
838
|
if (c === ' ' || c === '\t' || c === '\n' || c === '\r' || ';|&()<>'.includes(c)) {
|
|
769
839
|
push();
|
|
840
|
+
if (c === '\n' || ';|&()'.includes(c))
|
|
841
|
+
fresh = true;
|
|
770
842
|
continue;
|
|
771
843
|
}
|
|
772
844
|
cur += c;
|
|
@@ -775,6 +847,40 @@ export function shellTokens(command, markLiteral = false) {
|
|
|
775
847
|
push();
|
|
776
848
|
return out;
|
|
777
849
|
}
|
|
850
|
+
/** The index of the `)` that closes a `$(` whose text starts at `j` (quotes and escapes skipped), or the end. Linear. */
|
|
851
|
+
function closingParen(s, j) {
|
|
852
|
+
let depth = 1;
|
|
853
|
+
for (let k = j; k < s.length; k++) {
|
|
854
|
+
const c = s[k];
|
|
855
|
+
if (c === '\\')
|
|
856
|
+
k++;
|
|
857
|
+
else if (c === "'") {
|
|
858
|
+
const e = s.indexOf("'", k + 1);
|
|
859
|
+
k = e < 0 ? s.length : e;
|
|
860
|
+
}
|
|
861
|
+
else if (c === '"') {
|
|
862
|
+
let e = k + 1;
|
|
863
|
+
while (e < s.length && s[e] !== '"')
|
|
864
|
+
e += s[e] === '\\' ? 2 : 1;
|
|
865
|
+
k = e;
|
|
866
|
+
}
|
|
867
|
+
else if (c === '(')
|
|
868
|
+
depth++;
|
|
869
|
+
else if (c === ')' && --depth === 0)
|
|
870
|
+
return k;
|
|
871
|
+
}
|
|
872
|
+
return s.length;
|
|
873
|
+
}
|
|
874
|
+
/** The index of the backtick that closes one whose text starts at `j`, or the end. */
|
|
875
|
+
function closingTick(s, j) {
|
|
876
|
+
for (let k = j; k < s.length; k++) {
|
|
877
|
+
if (s[k] === '\\')
|
|
878
|
+
k++;
|
|
879
|
+
else if (s[k] === '`')
|
|
880
|
+
return k;
|
|
881
|
+
}
|
|
882
|
+
return s.length;
|
|
883
|
+
}
|
|
778
884
|
/** `p` made absolute against `cwd` (POSIX or Windows separators), `.` and `..` resolved. Linear: no regex over the input. */
|
|
779
885
|
export function resolveLike(cwd, p) {
|
|
780
886
|
const isSep = (c) => c === '/' || c === '\\';
|
|
@@ -825,46 +931,558 @@ export function resolveLike(cwd, p) {
|
|
|
825
931
|
}
|
|
826
932
|
/** At most this many folders one command may name; past it the session is private (`over`). */
|
|
827
933
|
export const MAX_COMMAND_PATHS = 256;
|
|
934
|
+
/** Stands for a `$(…)` or backtick substitution in a word (shellTokens with `substs`): an expansion whose value is unknown. */
|
|
935
|
+
const SUBST = '\uE001';
|
|
936
|
+
/** Device files a command names by habit (`2>/dev/null`): never a folder of the session's. */
|
|
937
|
+
const DEVICES = new Set(['/dev/null', '/dev/zero', '/dev/random', '/dev/urandom', '/dev/tty', '/dev/stdin', '/dev/stdout', '/dev/stderr', '/dev/full']);
|
|
938
|
+
const isDevice = (p) => DEVICES.has(p) || /^\/dev\/fd\/\d+$/.test(p);
|
|
939
|
+
/** The files Claude Code runs ahead of each Bash command (CLAUDE_ENV_FILE, its session-env and shell-snapshots folders): they can set any variable. */
|
|
940
|
+
const ENV_FILES = /CLAUDE_ENV_FILE|session-env|shell-snapshots/i;
|
|
941
|
+
/** Characters after which a word starts a command, for evalLike. */
|
|
942
|
+
const EVAL_BOUNDARY = ';&|(){}`\n!\'"';
|
|
943
|
+
/** Words after which the next word starts a command, for evalLike. */
|
|
944
|
+
const EVAL_KEYWORDS = new Set(['then', 'do', 'else', 'elif', 'exec', 'command', 'builtin', 'xargs']);
|
|
945
|
+
const isSpace = (c) => c !== undefined && /\s/.test(c);
|
|
946
|
+
const isWordChar = (c) => c !== undefined && /\w/.test(c);
|
|
828
947
|
/**
|
|
829
|
-
*
|
|
948
|
+
* A command that may set variables in ways no word shows: eval, source or `.` at the start, after
|
|
949
|
+
* `;`, `&`, `|`, a bracket, a quote, a newline or `!`, or after then/do/else/elif/exec/command/
|
|
950
|
+
* builtin/xargs, each followed by a space. One pass over the text, so it stays linear.
|
|
951
|
+
*/
|
|
952
|
+
function evalLike(s) {
|
|
953
|
+
let boundary = true;
|
|
954
|
+
for (let i = 0; i < s.length;) {
|
|
955
|
+
const c = s[i];
|
|
956
|
+
if (EVAL_BOUNDARY.includes(c)) {
|
|
957
|
+
boundary = true;
|
|
958
|
+
i++;
|
|
959
|
+
continue;
|
|
960
|
+
}
|
|
961
|
+
if (isSpace(c)) {
|
|
962
|
+
i++;
|
|
963
|
+
continue;
|
|
964
|
+
}
|
|
965
|
+
if (boundary)
|
|
966
|
+
for (const w of ['eval', 'source', '.'])
|
|
967
|
+
if (s.startsWith(w, i) && isSpace(s[i + w.length]))
|
|
968
|
+
return true;
|
|
969
|
+
if (isWordChar(c)) {
|
|
970
|
+
let j = i;
|
|
971
|
+
while (j < s.length && isWordChar(s[j]))
|
|
972
|
+
j++;
|
|
973
|
+
boundary = EVAL_KEYWORDS.has(s.slice(i, j));
|
|
974
|
+
i = j;
|
|
975
|
+
continue;
|
|
976
|
+
}
|
|
977
|
+
boundary = false;
|
|
978
|
+
i++;
|
|
979
|
+
}
|
|
980
|
+
return false;
|
|
981
|
+
}
|
|
982
|
+
/** Whether `c` holds a brace expansion with a choice: a `{`, then `,` or `..` with no brace between, then `}`. Linear. */
|
|
983
|
+
function braceChoice(c) {
|
|
984
|
+
let open = false;
|
|
985
|
+
let choice = false;
|
|
986
|
+
for (let i = 0; i < c.length; i++) {
|
|
987
|
+
const ch = c[i];
|
|
988
|
+
if (ch === '{') {
|
|
989
|
+
open = true;
|
|
990
|
+
choice = false;
|
|
991
|
+
}
|
|
992
|
+
else if (ch === '}') {
|
|
993
|
+
if (open && choice)
|
|
994
|
+
return true;
|
|
995
|
+
open = false;
|
|
996
|
+
}
|
|
997
|
+
else if (open && (ch === ',' || (ch === '.' && c[i + 1] === '.')))
|
|
998
|
+
choice = true;
|
|
999
|
+
}
|
|
1000
|
+
return false;
|
|
1001
|
+
}
|
|
1002
|
+
/** A path component that names a repository marker or could match one (`.git`, `.p2p-private`, `.g*`, `[.]git`, `{.git,x}`). */
|
|
1003
|
+
const namesMarker = (c) => c === '.git' || c === '.p2p-private' || ((c.startsWith('.') || c.startsWith('[')) && /[*?[]/.test(c)) || braceChoice(c);
|
|
1004
|
+
/** A command word's name: its last path segment, lower-cased, `.exe` and a version (python3.12) dropped. */
|
|
1005
|
+
function commandName(w) {
|
|
1006
|
+
let base = (w.split(/[\\/]/).pop() ?? '').toLowerCase();
|
|
1007
|
+
if (base.endsWith('.exe'))
|
|
1008
|
+
base = base.slice(0, -4);
|
|
1009
|
+
let end = base.length;
|
|
1010
|
+
while (end > 0 && '0123456789.'.includes(base[end - 1]))
|
|
1011
|
+
end--;
|
|
1012
|
+
return { base, bare: base.slice(0, end) };
|
|
1013
|
+
}
|
|
1014
|
+
const CODE_SHELLS = new Set(['sh', 'bash', 'zsh', 'dash', 'ksh', 'mksh', 'fish']);
|
|
1015
|
+
const SED = new Set(['sed', 'gsed']);
|
|
1016
|
+
const JQ = new Set(['jq', 'gojq']);
|
|
1017
|
+
const AWK = new Set(['awk', 'gawk', 'mawk', 'nawk']);
|
|
1018
|
+
// Shapes a program has and a path almost never does. A word in a program's place that lacks one stays a path.
|
|
1019
|
+
const sedShape = (w) => w !== undefined && /^[\s\d$,!~+;]*[sy]([^\w\s\\])(?:(?!\1)[^\\]|\\.)*\1(?:(?!\1)[^\\]|\\.)*\1/s.test(w);
|
|
1020
|
+
const jqShape = (w) => w !== undefined && (/^\s*(\.(?!\.?[\\/])|[[{("@$])/.test(w) || w.includes('|'));
|
|
1021
|
+
const awkShape = (w) => w !== undefined && (/[{}]/.test(w) || /^\s*(BEGIN|END)\b/.test(w));
|
|
1022
|
+
const codeShape = (w) => w !== undefined && (/[\s;(){}'"=]/.test(w) || sedShape(w));
|
|
1023
|
+
/** Shell words that come before a simple command's command word (`if x`, `then x`, `! x`, `{ x`). */
|
|
1024
|
+
const KEYWORDS = new Set(['if', 'then', 'do', 'else', 'elif', 'while', 'until', '!', '{', '}', 'fi', 'done', 'esac', 'time', 'coproc']);
|
|
1025
|
+
/** Commands that run the next word as the command, and their options that take a value. */
|
|
1026
|
+
const RUNNERS = {
|
|
1027
|
+
sudo: ['-u', '-g', '-p', '-C', '-D', '-h', '-r', '-t', '-T', '-U', '--user', '--group', '--prompt', '--chdir', '--close-from', '--host', '--role', '--type', '--command-timeout', '--other-user'],
|
|
1028
|
+
doas: ['-u', '-C'],
|
|
1029
|
+
env: ['-u', '--unset', '-C', '--chdir'],
|
|
1030
|
+
exec: ['-a'],
|
|
1031
|
+
time: ['-f', '-o', '--format', '--output'],
|
|
1032
|
+
nohup: [],
|
|
1033
|
+
command: [],
|
|
1034
|
+
builtin: [],
|
|
1035
|
+
nice: ['-n', '--adjustment'],
|
|
1036
|
+
timeout: ['-s', '--signal', '-k', '--kill-after'],
|
|
1037
|
+
xargs: ['-I', '-L', '-n', '-P', '-s', '-d', '-E', '-a', '--arg-file', '--delimiter', '--max-args', '--max-procs', '--max-lines', '--replace', '--eof', '--max-chars'],
|
|
1038
|
+
stdbuf: ['-i', '-o', '-e', '--input', '--output', '--error'],
|
|
1039
|
+
unbuffer: [],
|
|
1040
|
+
ionice: ['-c', '-n', '-p', '--class', '--classdata', '--pid'],
|
|
1041
|
+
caffeinate: ['-t', '-w'],
|
|
1042
|
+
noglob: [],
|
|
1043
|
+
nocorrect: [],
|
|
1044
|
+
};
|
|
1045
|
+
/**
|
|
1046
|
+
* The simple commands of a command line, as `[first, end)` word ranges (shellTokens `starts`).
|
|
1047
|
+
*/
|
|
1048
|
+
function segmentsOf(count, starts) {
|
|
1049
|
+
const at = [...starts].filter(k => k < count).sort((x, y) => x - y);
|
|
1050
|
+
if (count > 0 && at[0] !== 0)
|
|
1051
|
+
at.unshift(0);
|
|
1052
|
+
return at.map((k, n) => [k, at[n + 1] ?? count]);
|
|
1053
|
+
}
|
|
1054
|
+
/**
|
|
1055
|
+
* The command word of the simple command `words[s..e)`: past keywords (`then`, `!`), assignments
|
|
1056
|
+
* (`X=1`) and runners with their options (`sudo -u x`, `env -i`, `timeout 5`, `xargs -0`), or -1
|
|
1057
|
+
* when there is none or it can't be told (`env -S '…'`). `dirs`: the folders a runner moves to
|
|
1058
|
+
* first (`env -C d`, `sudo -D d`).
|
|
1059
|
+
*/
|
|
1060
|
+
function commandAt(words, s, e) {
|
|
1061
|
+
const dirs = [];
|
|
1062
|
+
let i = s;
|
|
1063
|
+
for (let guard = 0; guard < 64 && i < e; guard++) {
|
|
1064
|
+
const w = words[i];
|
|
1065
|
+
if (KEYWORDS.has(w) || /^[A-Za-z_]\w*=/.test(w)) {
|
|
1066
|
+
i++;
|
|
1067
|
+
continue;
|
|
1068
|
+
}
|
|
1069
|
+
const { base } = commandName(w);
|
|
1070
|
+
const opts = Object.prototype.hasOwnProperty.call(RUNNERS, base) ? RUNNERS[base] : undefined;
|
|
1071
|
+
if (opts === undefined)
|
|
1072
|
+
break;
|
|
1073
|
+
i++;
|
|
1074
|
+
while (i < e && words[i].startsWith('-') && words[i] !== '-') {
|
|
1075
|
+
const opt = words[i];
|
|
1076
|
+
i++;
|
|
1077
|
+
if (opt === '--')
|
|
1078
|
+
break;
|
|
1079
|
+
if (base === 'env' && (opt === '-S' || opt === '--split-string' || opt.startsWith('--split-string=') || /^-[A-Za-z]*S/.test(opt)))
|
|
1080
|
+
return { at: -1, dirs };
|
|
1081
|
+
const eq = opt.indexOf('=');
|
|
1082
|
+
const name = eq > 0 ? opt.slice(0, eq) : opt;
|
|
1083
|
+
const chdir = ((base === 'env' || base === 'doas') && name === '-C') || name === '--chdir' || (base === 'sudo' && name === '-D');
|
|
1084
|
+
if (eq > 0) {
|
|
1085
|
+
if (chdir)
|
|
1086
|
+
dirs.push(opt.slice(eq + 1));
|
|
1087
|
+
}
|
|
1088
|
+
else if (opts.includes(opt)) {
|
|
1089
|
+
if (chdir && i < e)
|
|
1090
|
+
dirs.push(words[i]);
|
|
1091
|
+
i++;
|
|
1092
|
+
}
|
|
1093
|
+
else if (base === 'env' && /^-C./.test(opt))
|
|
1094
|
+
dirs.push(opt.slice(2));
|
|
1095
|
+
}
|
|
1096
|
+
if (base === 'timeout' && /^\d/.test(words[i] ?? ''))
|
|
1097
|
+
i++;
|
|
1098
|
+
}
|
|
1099
|
+
return { at: i < e ? i : -1, dirs };
|
|
1100
|
+
}
|
|
1101
|
+
/**
|
|
1102
|
+
* The words that are a program, not a path: the script of sed (`s#a/b#c/d#`), jq (`.a/b`), awk
|
|
1103
|
+
* and perl -e, and the code of `python -c`, `node -e`, `sh -c` and the like. Only for the command
|
|
1104
|
+
* word of a simple command (`segs`: commandAt), in a program's place (after the options, with
|
|
1105
|
+
* -e/-f/--from-file read as they are), within that simple command, and only with a program's
|
|
1106
|
+
* shape: anything else stays a path, since a path missed binds nothing. A word named sed or jq
|
|
1107
|
+
* anywhere else (`cat jq x/../k`) is an argument like any other. Their absolute paths are still
|
|
1108
|
+
* read (shellPaths: embedded).
|
|
1109
|
+
*/
|
|
1110
|
+
function programWords(words, segs) {
|
|
1111
|
+
const out = new Set();
|
|
1112
|
+
const mark = (j, ok) => {
|
|
1113
|
+
if (j < end && ok(words[j]))
|
|
1114
|
+
out.add(j);
|
|
1115
|
+
};
|
|
1116
|
+
let end = 0;
|
|
1117
|
+
for (const [s, e] of segs) {
|
|
1118
|
+
const i = commandAt(words, s, e).at;
|
|
1119
|
+
if (i < 0)
|
|
1120
|
+
continue;
|
|
1121
|
+
end = e;
|
|
1122
|
+
const { base, bare } = commandName(words[i]);
|
|
1123
|
+
if (SED.has(base)) {
|
|
1124
|
+
let script = false;
|
|
1125
|
+
for (let j = i + 1; j < e; j++) {
|
|
1126
|
+
const w = words[j];
|
|
1127
|
+
if (w === '--') {
|
|
1128
|
+
if (!script)
|
|
1129
|
+
mark(j + 1, sedShape);
|
|
1130
|
+
break;
|
|
1131
|
+
}
|
|
1132
|
+
if (w === '-e' || w === '--expression') {
|
|
1133
|
+
mark(j + 1, sedShape);
|
|
1134
|
+
script = true;
|
|
1135
|
+
j++;
|
|
1136
|
+
}
|
|
1137
|
+
else if (w.startsWith('--expression=')) {
|
|
1138
|
+
if (sedShape(w.slice(13)))
|
|
1139
|
+
out.add(j);
|
|
1140
|
+
script = true;
|
|
1141
|
+
}
|
|
1142
|
+
else if (w === '-f' || w === '--file') {
|
|
1143
|
+
script = true;
|
|
1144
|
+
j++;
|
|
1145
|
+
}
|
|
1146
|
+
else if (w.startsWith('--file='))
|
|
1147
|
+
script = true;
|
|
1148
|
+
else if (w === '-l' || w === '--line-length')
|
|
1149
|
+
j++;
|
|
1150
|
+
else if (/^-[A-Za-z]+$/.test(w)) {
|
|
1151
|
+
if (w.endsWith('e')) {
|
|
1152
|
+
mark(j + 1, sedShape);
|
|
1153
|
+
script = true;
|
|
1154
|
+
j++;
|
|
1155
|
+
}
|
|
1156
|
+
else if (w.endsWith('f')) {
|
|
1157
|
+
script = true;
|
|
1158
|
+
j++;
|
|
1159
|
+
}
|
|
1160
|
+
else if (w.endsWith('l'))
|
|
1161
|
+
j++;
|
|
1162
|
+
}
|
|
1163
|
+
else if (w.startsWith('-') && w.length > 1)
|
|
1164
|
+
continue;
|
|
1165
|
+
else {
|
|
1166
|
+
if (!script)
|
|
1167
|
+
mark(j, sedShape);
|
|
1168
|
+
break;
|
|
1169
|
+
}
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1172
|
+
else if (JQ.has(base)) {
|
|
1173
|
+
let script = false;
|
|
1174
|
+
for (let j = i + 1; j < e; j++) {
|
|
1175
|
+
const w = words[j];
|
|
1176
|
+
if (w === '--') {
|
|
1177
|
+
if (!script)
|
|
1178
|
+
mark(j + 1, jqShape);
|
|
1179
|
+
break;
|
|
1180
|
+
}
|
|
1181
|
+
if (w === '-f' || w === '--from-file') {
|
|
1182
|
+
script = true;
|
|
1183
|
+
j++;
|
|
1184
|
+
}
|
|
1185
|
+
else if (w === '--arg' || w === '--argjson' || w === '--slurpfile' || w === '--rawfile')
|
|
1186
|
+
j += 2;
|
|
1187
|
+
else if (w === '--indent' || w === '-L' || w === '--library-path')
|
|
1188
|
+
j++;
|
|
1189
|
+
else if (w.startsWith('-') && w.length > 1)
|
|
1190
|
+
continue;
|
|
1191
|
+
else {
|
|
1192
|
+
if (!script)
|
|
1193
|
+
mark(j, jqShape);
|
|
1194
|
+
break;
|
|
1195
|
+
}
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
else if (AWK.has(base)) {
|
|
1199
|
+
let script = false;
|
|
1200
|
+
for (let j = i + 1; j < e; j++) {
|
|
1201
|
+
const w = words[j];
|
|
1202
|
+
if (w === '--') {
|
|
1203
|
+
if (!script)
|
|
1204
|
+
mark(j + 1, awkShape);
|
|
1205
|
+
break;
|
|
1206
|
+
}
|
|
1207
|
+
if (w === '-f' || w === '-E' || w === '--file' || w === '--exec') {
|
|
1208
|
+
script = true;
|
|
1209
|
+
j++;
|
|
1210
|
+
}
|
|
1211
|
+
else if (w === '-e' || w === '--source') {
|
|
1212
|
+
mark(j + 1, awkShape);
|
|
1213
|
+
script = true;
|
|
1214
|
+
j++;
|
|
1215
|
+
}
|
|
1216
|
+
else if (w.startsWith('--source=')) {
|
|
1217
|
+
if (awkShape(w.slice(9)))
|
|
1218
|
+
out.add(j);
|
|
1219
|
+
script = true;
|
|
1220
|
+
}
|
|
1221
|
+
else if (/^-[Fvil]$/.test(w) || w === '--field-separator' || w === '--assign' || w === '--include' || w === '--load')
|
|
1222
|
+
j++;
|
|
1223
|
+
else if (w.startsWith('-') && w.length > 1) {
|
|
1224
|
+
if (/^-[A-Za-z]*f$/.test(w)) {
|
|
1225
|
+
script = true;
|
|
1226
|
+
j++;
|
|
1227
|
+
}
|
|
1228
|
+
}
|
|
1229
|
+
else {
|
|
1230
|
+
if (!script)
|
|
1231
|
+
mark(j, awkShape);
|
|
1232
|
+
break;
|
|
1233
|
+
}
|
|
1234
|
+
}
|
|
1235
|
+
}
|
|
1236
|
+
else if (INTERPRETERS.has(base) || INTERPRETERS.has(bare) || CODE_SHELLS.has(base)) {
|
|
1237
|
+
// The word after a code flag (-c, -e, --eval; a cluster like -ne, -lc): code, when it looks like code.
|
|
1238
|
+
for (let j = i + 1; j < Math.min(e, i + 6); j++) {
|
|
1239
|
+
const w = words[j];
|
|
1240
|
+
if (CODE_FLAGS.has(w) || /^-[A-Za-z]*[ceE]$/.test(w)) {
|
|
1241
|
+
mark(j + 1, codeShape);
|
|
1242
|
+
break;
|
|
1243
|
+
}
|
|
1244
|
+
if (!w.startsWith('-'))
|
|
1245
|
+
break;
|
|
1246
|
+
}
|
|
1247
|
+
}
|
|
1248
|
+
}
|
|
1249
|
+
return out;
|
|
1250
|
+
}
|
|
1251
|
+
/**
|
|
1252
|
+
* The absolute, `~/`, `./` and `../` paths written inside a program or a quoted phrase
|
|
1253
|
+
* (`open("/x/y")`, `sed 'r /x/y'`, `sh -c 'cat ../x/y'`): each begins after a space, a quote, `(`,
|
|
1254
|
+
* `<`, `>`, `=` or `:`, never at the word's start (a sed or awk address there is a pattern). Linear.
|
|
1255
|
+
*/
|
|
1256
|
+
function embedded(t) {
|
|
1257
|
+
const out = [];
|
|
1258
|
+
const starts = ' \t\n\r"\'`(<>=:';
|
|
1259
|
+
const ends = ' \t\n\r"\'`;|&()<>,' + LITERAL + SUBST;
|
|
1260
|
+
for (let k = 1; k < t.length; k++) {
|
|
1261
|
+
const c = t[k];
|
|
1262
|
+
const tilde = c === '~' && t[k + 1] === '/';
|
|
1263
|
+
const dot = c === '.' && (t[k + 1] === '/' || (t[k + 1] === '.' && t[k + 2] === '/'));
|
|
1264
|
+
if ((c !== '/' && !tilde && !dot) || !starts.includes(t[k - 1]))
|
|
1265
|
+
continue;
|
|
1266
|
+
const first = t[tilde ? k + 2 : dot ? k + (t[k + 1] === '/' ? 2 : 3) : k + 1];
|
|
1267
|
+
if (first === undefined || !/[\w.~@%+-]/.test(first))
|
|
1268
|
+
continue;
|
|
1269
|
+
let e = k;
|
|
1270
|
+
while (e < t.length && !ends.includes(t[e]))
|
|
1271
|
+
e++;
|
|
1272
|
+
out.push(t.slice(k, e));
|
|
1273
|
+
k = e;
|
|
1274
|
+
}
|
|
1275
|
+
return out;
|
|
1276
|
+
}
|
|
1277
|
+
/** Whether `raw` uses the variable `name` other than by expanding it (`name=…`, `export name`, `read name`, `for name in`). */
|
|
1278
|
+
function mentions(raw, name) {
|
|
1279
|
+
for (let k = raw.indexOf(name); k >= 0; k = raw.indexOf(name, k + 1)) {
|
|
1280
|
+
const before = raw[k - 1];
|
|
1281
|
+
const after = raw[k + name.length];
|
|
1282
|
+
if ((before !== undefined && /\w/.test(before)) || (after !== undefined && /\w/.test(after)))
|
|
1283
|
+
continue;
|
|
1284
|
+
if (before === '$' || (before === '{' && raw[k - 2] === '$'))
|
|
1285
|
+
continue;
|
|
1286
|
+
return true;
|
|
1287
|
+
}
|
|
1288
|
+
return false;
|
|
1289
|
+
}
|
|
1290
|
+
/** Commands whose `-C <dir>` (or `--directory`) runs them in another folder. */
|
|
1291
|
+
const DIR_FLAG_COMMANDS = new Set(['git', 'make', 'gmake', 'bmake', 'tar', 'gtar', 'bsdtar', 'ninja']);
|
|
1292
|
+
/** A path with a `..` segment. */
|
|
1293
|
+
const DOTDOT = /(^|[\\/])\.\.([\\/]|$)/;
|
|
1294
|
+
/**
|
|
1295
|
+
* The paths a shell command names: absolute, `~` and relative paths (anything with a separator,
|
|
830
1296
|
* or starting with `.` or `~`), an option's value (`--file=/x`) too, each resolved (relative ones
|
|
831
|
-
* against `cwd
|
|
832
|
-
*
|
|
833
|
-
*
|
|
1297
|
+
* against `cwd` and every folder the command moves to) and the absolute paths written inside a
|
|
1298
|
+
* program or quoted phrase. A path is passed as written (`/src/secret.repo` stays itself): the CLI
|
|
1299
|
+
* maps a file to its folder.
|
|
1300
|
+
*
|
|
1301
|
+
* The folders it moves to, read per simple command: `cd`/`pushd`/`chdir` (none: `$HOME`), `git -C`,
|
|
1302
|
+
* `make -C`, `tar -C`, `env -C`, `sudo -D` and the like. Each binds, and relative paths resolve
|
|
1303
|
+
* against each one (and against the ones before it). One it can't place (`cd -`, `cd $X`, `popd`
|
|
1304
|
+
* with no `pushd`) is `unresolved`.
|
|
1305
|
+
*
|
|
1306
|
+
* Variables expand only when the front end knows their value as the shell sees it: `$HOME`,
|
|
1307
|
+
* `$PWD`, and `ctx.vars` (the mod's `$CLAUDE_JOB_DIR`), and only when the command doesn't set
|
|
1308
|
+
* them itself (`HOME=…`, `export`, `read`, `cd` for `$PWD`, eval, source). Any other expansion
|
|
1309
|
+
* (`$X/y`, `$(…)/y`, backticks) in a path's first segment can't be resolved: the session is `over`
|
|
1310
|
+
* (private), `why` `unresolved`; further in, the folder before it binds. A bare `$X` or `$(…)`
|
|
1311
|
+
* names no path. A `$(…)` or backtick command is read for its own paths.
|
|
1312
|
+
*
|
|
1313
|
+
* A program's text (programWords: only for a simple command's command word) isn't a path, unless
|
|
1314
|
+
* it has a `..` segment or a shell expansion; what it would name otherwise is `maybe`.
|
|
1315
|
+
*
|
|
1316
|
+
* Device files (`/dev/null`, `/dev/fd/1`) are skipped. A command that could delete a repository's
|
|
1317
|
+
* `.git` or `.p2p-private` without naming it (find -delete, xargs, dotglob, inline code) also
|
|
1318
|
+
* names `<folder>/.git` for each folder, so a shared folder above never covers it afterwards
|
|
1319
|
+
* (projects.ts projectOf). Past MAX_COMMAND_PATHS: `over`, `why` `folders`. URLs are not paths.
|
|
1320
|
+
* `sticky`: the command touched the files Claude Code runs before each command (pass `untrusted`
|
|
1321
|
+
* from then on).
|
|
834
1322
|
*/
|
|
835
|
-
export function shellPaths(command, cwd, home) {
|
|
1323
|
+
export function shellPaths(command, cwd, home, ctx = {}, depth = 0) {
|
|
836
1324
|
const out = new Set();
|
|
837
|
-
|
|
1325
|
+
const maybe = new Set();
|
|
1326
|
+
const why = new Set();
|
|
838
1327
|
const isSep = (c) => c === '/' || c === '\\';
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
1328
|
+
const isAbs = (t) => t.startsWith('/') || /^[A-Za-z]:[\\/]/.test(t) || t.startsWith('\\\\');
|
|
1329
|
+
const sticky = ENV_FILES.test(command);
|
|
1330
|
+
const done = () => ({ paths: [...out], over: why.size > 0, ...(why.size > 0 ? { why: [...why] } : {}), ...(sticky ? { sticky: true } : {}), ...(maybe.size > 0 ? { maybe: [...maybe] } : {}) });
|
|
1331
|
+
if (depth > 8) {
|
|
1332
|
+
why.add('unresolved');
|
|
1333
|
+
return done();
|
|
1334
|
+
}
|
|
1335
|
+
const substs = [];
|
|
1336
|
+
const starts = new Set();
|
|
1337
|
+
const words = shellTokens(command, true, substs, starts);
|
|
1338
|
+
const segs = segmentsOf(words.length, starts);
|
|
1339
|
+
const changesDir = words.some(w => w === 'cd' || w === 'pushd' || w === 'popd' || w === 'chdir');
|
|
1340
|
+
// The variables known here, as this command's shell sees them.
|
|
1341
|
+
const known = new Map();
|
|
1342
|
+
const trusted = !ctx.untrusted && !sticky && !evalLike(command);
|
|
1343
|
+
for (const [name, val] of [['HOME', home], ['PWD', cwd], ...Object.entries(ctx.vars ?? {})]) {
|
|
1344
|
+
if (!trusted || !val || !/^[A-Za-z_]\w*$/.test(name) || /[\0\n]/.test(val) || !/^(\/|[A-Za-z]:[\\/]|\\\\)/.test(val) || mentions(command, name))
|
|
843
1345
|
continue;
|
|
844
|
-
//
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
1346
|
+
// After a cd, $PWD is where the command went: resolved as a relative path below (against each folder it cds to).
|
|
1347
|
+
if (name === 'PWD' && changesDir)
|
|
1348
|
+
continue;
|
|
1349
|
+
known.set(name, val);
|
|
1350
|
+
}
|
|
1351
|
+
// A word with the variables known here expanded.
|
|
1352
|
+
const expand = (word) => {
|
|
1353
|
+
let t = word;
|
|
1354
|
+
// A $PWD the command may have moved: the folder it is in, relative to each base.
|
|
1355
|
+
if (changesDir)
|
|
1356
|
+
for (const v of ['${PWD}', '$PWD'])
|
|
1357
|
+
if (t === v || t.startsWith(`${v}/`) || t.startsWith(`${v}\\`))
|
|
1358
|
+
t = `.${t.slice(v.length)}`;
|
|
1359
|
+
for (const [name, val] of known) {
|
|
1360
|
+
for (const v of [`\${${name}}`, `$${name}`]) {
|
|
1361
|
+
let k = t.indexOf(v);
|
|
1362
|
+
while (k >= 0) {
|
|
1363
|
+
const after = t[k + v.length];
|
|
1364
|
+
// `$HOMEDIR` is another variable: only the whole name.
|
|
1365
|
+
if (!v.startsWith('${') && after !== undefined && /\w/.test(after)) {
|
|
1366
|
+
k = t.indexOf(v, k + 1);
|
|
1367
|
+
continue;
|
|
1368
|
+
}
|
|
1369
|
+
t = `${t.slice(0, k)}${val}${t.slice(k + v.length)}`;
|
|
1370
|
+
k = t.indexOf(v, k + val.length);
|
|
1371
|
+
}
|
|
1372
|
+
}
|
|
1373
|
+
}
|
|
1374
|
+
return t;
|
|
1375
|
+
};
|
|
1376
|
+
// Folders a relative path may be in: the session's, and each one the command moves to, in order
|
|
1377
|
+
// (a relative move is relative to every folder before it).
|
|
1378
|
+
const bases = [cwd];
|
|
1379
|
+
const moved = [];
|
|
1380
|
+
const moveTo = (word) => {
|
|
1381
|
+
let t = expand(word);
|
|
1382
|
+
if (/[$`]/.test(t) || t.includes(SUBST))
|
|
1383
|
+
return false;
|
|
1384
|
+
t = t.split(LITERAL).join('$');
|
|
1385
|
+
if (t === '')
|
|
1386
|
+
return true;
|
|
1387
|
+
let ps;
|
|
1388
|
+
if (t === '~' || t.startsWith('~/') || t.startsWith('~\\')) {
|
|
1389
|
+
if (!home)
|
|
1390
|
+
return false;
|
|
1391
|
+
ps = [resolveLike(home, t.slice(2) || '.')];
|
|
1392
|
+
}
|
|
1393
|
+
else if (t.startsWith('~'))
|
|
1394
|
+
return false;
|
|
1395
|
+
else if (isAbs(t))
|
|
1396
|
+
ps = [resolveLike(cwd, t)];
|
|
1397
|
+
else
|
|
1398
|
+
ps = bases.map(b => resolveLike(b, t));
|
|
1399
|
+
for (const p of ps) {
|
|
1400
|
+
if (!bases.includes(p))
|
|
1401
|
+
bases.push(p);
|
|
1402
|
+
moved.push(p);
|
|
1403
|
+
}
|
|
1404
|
+
return true;
|
|
1405
|
+
};
|
|
1406
|
+
let pushed = false;
|
|
1407
|
+
for (const [s, e] of segs) {
|
|
1408
|
+
const c = commandAt(words, s, e);
|
|
1409
|
+
const targets = [...c.dirs];
|
|
1410
|
+
let unknown = false;
|
|
1411
|
+
if (c.at >= 0) {
|
|
1412
|
+
const name = commandName(words[c.at]).base;
|
|
1413
|
+
if (name === 'cd' || name === 'pushd' || name === 'chdir') {
|
|
1414
|
+
let j = c.at + 1;
|
|
1415
|
+
while (j < e && /^-[LPe@]+$/.test(words[j]))
|
|
1416
|
+
j++;
|
|
1417
|
+
if (words[j] === '--' && j < e)
|
|
1418
|
+
j++;
|
|
1419
|
+
const target = j < e ? words[j] : undefined;
|
|
1420
|
+
if (name === 'pushd')
|
|
1421
|
+
pushed = true;
|
|
1422
|
+
// A bare pushd swaps the top two folders of a stack this front end doesn't see; cd -, pushd +1 likewise.
|
|
1423
|
+
if (target === undefined) {
|
|
1424
|
+
if (name === 'pushd')
|
|
1425
|
+
unknown = true;
|
|
1426
|
+
else
|
|
1427
|
+
targets.push('~');
|
|
853
1428
|
}
|
|
854
|
-
if (
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
1429
|
+
else if (target === '-' || /^[+-]\d+$/.test(target))
|
|
1430
|
+
unknown = true;
|
|
1431
|
+
else
|
|
1432
|
+
targets.push(target);
|
|
1433
|
+
}
|
|
1434
|
+
else if (name === 'popd') {
|
|
1435
|
+
if (!pushed)
|
|
1436
|
+
unknown = true;
|
|
858
1437
|
}
|
|
1438
|
+
else if (DIR_FLAG_COMMANDS.has(name)) {
|
|
1439
|
+
for (let j = c.at + 1; j < e; j++) {
|
|
1440
|
+
const w = words[j];
|
|
1441
|
+
// git's own -C (and --git-dir, --work-tree) come before its subcommand.
|
|
1442
|
+
if (name === 'git' && !w.startsWith('-'))
|
|
1443
|
+
break;
|
|
1444
|
+
if (w === '-C' || w === '--directory' || (name === 'git' && (w === '--git-dir' || w === '--work-tree'))) {
|
|
1445
|
+
if (j + 1 < e)
|
|
1446
|
+
targets.push(words[++j]);
|
|
1447
|
+
}
|
|
1448
|
+
else if (w.startsWith('--directory=') || (name === 'git' && (w.startsWith('--git-dir=') || w.startsWith('--work-tree='))))
|
|
1449
|
+
targets.push(w.slice(w.indexOf('=') + 1));
|
|
1450
|
+
else if (name !== 'git' && /^-C./.test(w))
|
|
1451
|
+
targets.push(w.slice(2));
|
|
1452
|
+
else if (name === 'git' && (w === '-c' || w === '--namespace' || w === '--exec-path'))
|
|
1453
|
+
j++;
|
|
1454
|
+
}
|
|
1455
|
+
}
|
|
1456
|
+
}
|
|
1457
|
+
if (unknown)
|
|
1458
|
+
why.add('unresolved');
|
|
1459
|
+
for (const t of targets)
|
|
1460
|
+
if (!moveTo(t))
|
|
1461
|
+
why.add('unresolved');
|
|
1462
|
+
if (bases.length > 32) {
|
|
1463
|
+
why.add('folders');
|
|
1464
|
+
break;
|
|
859
1465
|
}
|
|
1466
|
+
}
|
|
1467
|
+
const add = (p) => {
|
|
1468
|
+
if (isDevice(p))
|
|
1469
|
+
return true;
|
|
1470
|
+
out.add(p);
|
|
1471
|
+
return out.size <= MAX_COMMAND_PATHS;
|
|
1472
|
+
};
|
|
1473
|
+
// One path. `inner`: one written inside a program, whose own `$x` and unknowns bind nothing (never
|
|
1474
|
+
// over). `maybe`: a program's own text, kept apart (ShellPaths.maybe) for the front end to check.
|
|
1475
|
+
const take = (word, mode) => {
|
|
1476
|
+
const inner = mode !== 'path';
|
|
1477
|
+
let t = expand(word);
|
|
860
1478
|
const pathLike = t.startsWith('~') || t.startsWith('.') || t.includes('/') || t.includes('\\');
|
|
861
1479
|
if (!pathLike)
|
|
862
|
-
|
|
1480
|
+
return true;
|
|
863
1481
|
// Any other expansion: where it sits decides. In the path's first segment, the location itself
|
|
864
1482
|
// is unknown: private. Further in, the folder before it is known: that one binds.
|
|
865
1483
|
let x = -1;
|
|
866
1484
|
for (let k = 0; k < t.length; k++) {
|
|
867
|
-
if (t[k] === '$' || t[k] === '`') {
|
|
1485
|
+
if (t[k] === '$' || t[k] === '`' || t[k] === SUBST || (inner && t[k] === LITERAL)) {
|
|
868
1486
|
x = k;
|
|
869
1487
|
break;
|
|
870
1488
|
}
|
|
@@ -875,31 +1493,116 @@ export function shellPaths(command, cwd, home) {
|
|
|
875
1493
|
if (isSep(t[k]))
|
|
876
1494
|
lastSep = k;
|
|
877
1495
|
if (lastSep <= 0) {
|
|
878
|
-
|
|
879
|
-
|
|
1496
|
+
if (!inner)
|
|
1497
|
+
why.add('unresolved');
|
|
1498
|
+
return true;
|
|
880
1499
|
}
|
|
881
1500
|
t = t.slice(0, lastSep);
|
|
882
1501
|
}
|
|
883
1502
|
t = t.split(LITERAL).join('$');
|
|
884
|
-
let
|
|
1503
|
+
let ps;
|
|
885
1504
|
if (t === '~' || t.startsWith('~/') || t.startsWith('~\\')) {
|
|
886
1505
|
if (!home) {
|
|
887
|
-
|
|
888
|
-
|
|
1506
|
+
if (!inner)
|
|
1507
|
+
why.add('unresolved');
|
|
1508
|
+
return true;
|
|
889
1509
|
}
|
|
890
|
-
|
|
1510
|
+
ps = [resolveLike(home, t.slice(2) || '.')];
|
|
1511
|
+
}
|
|
1512
|
+
else if (t.startsWith('~')) {
|
|
1513
|
+
// ~user, ~+, ~-: another folder this front end doesn't know.
|
|
1514
|
+
if (!inner)
|
|
1515
|
+
why.add('unresolved');
|
|
1516
|
+
return true;
|
|
891
1517
|
}
|
|
1518
|
+
else if (isAbs(t))
|
|
1519
|
+
ps = [resolveLike(cwd, t)];
|
|
892
1520
|
else
|
|
893
|
-
|
|
894
|
-
//
|
|
895
|
-
const
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
1521
|
+
ps = [...new Set(bases.map(b => resolveLike(b, t)))];
|
|
1522
|
+
// As written: a file, a folder or a pattern (the CLI finds a file's folder, by real path, through symlinks).
|
|
1523
|
+
for (const p of ps) {
|
|
1524
|
+
if (mode === 'maybe') {
|
|
1525
|
+
if (!isDevice(p))
|
|
1526
|
+
maybe.add(p);
|
|
1527
|
+
if (maybe.size > MAX_COMMAND_PATHS)
|
|
1528
|
+
return false;
|
|
1529
|
+
}
|
|
1530
|
+
else if (!add(p))
|
|
1531
|
+
return false;
|
|
1532
|
+
}
|
|
1533
|
+
return true;
|
|
1534
|
+
};
|
|
1535
|
+
const programs = programWords(words, segs);
|
|
1536
|
+
// A command that may remove a .git or .p2p-private without naming it.
|
|
1537
|
+
let mayUnmark = /dotglob|globdots|glob_dots|GLOBIGNORE/i.test(command) || /[*?\]@!+]\(/.test(command);
|
|
1538
|
+
for (let i = 0; i < words.length && !mayUnmark; i++) {
|
|
1539
|
+
const w = words[i];
|
|
1540
|
+
const { base, bare } = commandName(w);
|
|
1541
|
+
if (!/[\\/]/.test(w) && namesMarker(w))
|
|
1542
|
+
mayUnmark = true;
|
|
1543
|
+
else if (base === 'xargs')
|
|
1544
|
+
mayUnmark = true;
|
|
1545
|
+
else if (base === 'find' || base === 'gfind' || base === 'fd' || base === 'fdfind')
|
|
1546
|
+
mayUnmark = words.some(x => /^(-delete|-exec|-execdir|-ok|-okdir|-x|-X|--exec|--exec-batch)$/.test(x));
|
|
1547
|
+
else if (base === 'rsync')
|
|
1548
|
+
mayUnmark = words.some(x => x.startsWith('--del') || x === '--remove-source-files');
|
|
1549
|
+
else if (INTERPRETERS.has(base) || INTERPRETERS.has(bare) || CODE_SHELLS.has(base))
|
|
1550
|
+
mayUnmark = words.slice(i + 1, i + 6).some(x => CODE_FLAGS.has(x) || /^-[A-Za-z]*[ceE]$/.test(x));
|
|
1551
|
+
}
|
|
1552
|
+
for (let i = 0; i < words.length; i++) {
|
|
1553
|
+
const word = words[i];
|
|
1554
|
+
// An option's value (`--file=/x`), and an assignment's (`T=/x/y`: the folder it names binds, though `$T` later can't be read).
|
|
1555
|
+
const eq = word.startsWith('-') ? word.indexOf('=') : (/^[A-Za-z_]\w*=/.exec(word)?.[0].length ?? 0) - 1;
|
|
1556
|
+
const t = eq >= 0 ? word.slice(eq + 1) : word;
|
|
1557
|
+
if (t === '' || (word.startsWith('-') && eq < 0) || t.includes('\0'))
|
|
1558
|
+
continue;
|
|
1559
|
+
const program = programs.has(i);
|
|
1560
|
+
// A program's (or a quoted phrase's) own absolute paths.
|
|
1561
|
+
if (program || /\s/.test(t))
|
|
1562
|
+
for (const e of embedded(t))
|
|
1563
|
+
if (!e.includes('://') && !take(e, 'inner'))
|
|
1564
|
+
return capped();
|
|
1565
|
+
if (t.includes('://') || t.includes('\n'))
|
|
1566
|
+
continue;
|
|
1567
|
+
// A program's own text isn't a path, unless it holds an expansion of the shell's ("$X/y" in
|
|
1568
|
+
// double quotes) or a `..` segment (`jq .git/../../k`): those are read as paths. Otherwise it
|
|
1569
|
+
// is `maybe`: bound when such a path exists.
|
|
1570
|
+
if (program && !t.startsWith('/') && !t.startsWith('~') && !/[$`]/.test(t) && !DOTDOT.test(t)) {
|
|
1571
|
+
if (!take(t, 'maybe'))
|
|
1572
|
+
return capped();
|
|
1573
|
+
continue;
|
|
1574
|
+
}
|
|
1575
|
+
if (!take(t, 'path'))
|
|
1576
|
+
return capped();
|
|
1577
|
+
}
|
|
1578
|
+
for (const p of moved)
|
|
1579
|
+
if (!add(p))
|
|
1580
|
+
return capped();
|
|
1581
|
+
for (const s of substs) {
|
|
1582
|
+
const r = shellPaths(s, cwd, home, ctx, depth + 1);
|
|
1583
|
+
for (const p of r.paths)
|
|
1584
|
+
if (!add(p))
|
|
1585
|
+
return capped();
|
|
1586
|
+
for (const p of r.maybe ?? [])
|
|
1587
|
+
maybe.add(p);
|
|
1588
|
+
for (const w of r.why ?? [])
|
|
1589
|
+
why.add(w);
|
|
1590
|
+
}
|
|
1591
|
+
if (mayUnmark) {
|
|
1592
|
+
for (const p of [...out, ...bases]) {
|
|
1593
|
+
// The folder a pattern is in (`/x/old/*`: /x/old), whose .git it may match.
|
|
1594
|
+
let q = p;
|
|
1595
|
+
for (let k = Math.max(q.lastIndexOf('/'), q.lastIndexOf('\\')); k > 0 && /[*?[\]{}]/.test(q.slice(k + 1)); k = Math.max(q.lastIndexOf('/'), q.lastIndexOf('\\')))
|
|
1596
|
+
q = q.slice(0, k);
|
|
1597
|
+
if (!add(`${q}${q.includes('\\') && !q.includes('/') ? '\\' : '/'}.git`))
|
|
1598
|
+
return capped();
|
|
1599
|
+
}
|
|
1600
|
+
}
|
|
1601
|
+
return done();
|
|
1602
|
+
function capped() {
|
|
1603
|
+
why.add('folders');
|
|
1604
|
+
return { ...done(), paths: [...out].slice(0, MAX_COMMAND_PATHS), ...(maybe.size > 0 ? { maybe: [...maybe].slice(0, MAX_COMMAND_PATHS) } : {}) };
|
|
1605
|
+
}
|
|
903
1606
|
}
|
|
904
1607
|
/** Interpreters that run code given as an argument, and terminals that run a command under a pseudo-terminal. */
|
|
905
1608
|
const INTERPRETERS = new Set(['python', 'python2', 'python3', 'node', 'nodejs', 'perl', 'ruby', 'deno', 'bun', 'php', 'lua', 'osascript', 'tclsh', 'pwsh', 'powershell']);
|