@node9/policy-engine 2.45.0 → 2.47.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +286 -190
- package/dist/index.mjs +286 -190
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -258,9 +258,6 @@ __export(src_exports, {
|
|
|
258
258
|
});
|
|
259
259
|
module.exports = __toCommonJS(src_exports);
|
|
260
260
|
|
|
261
|
-
// src/shell/derived-writes.ts
|
|
262
|
-
var import_mvdan_sh3 = __toESM(require("mvdan-sh"));
|
|
263
|
-
|
|
264
261
|
// src/files/protected-access.ts
|
|
265
262
|
var import_picomatch2 = __toESM(require("picomatch"));
|
|
266
263
|
|
|
@@ -3556,7 +3553,121 @@ function resolveCheckMap(settings) {
|
|
|
3556
3553
|
var import_picomatch = __toESM(require("picomatch"));
|
|
3557
3554
|
|
|
3558
3555
|
// src/shell/index.ts
|
|
3556
|
+
var import_mvdan_sh2 = __toESM(require("mvdan-sh"));
|
|
3557
|
+
|
|
3558
|
+
// src/shell/plain-tree.ts
|
|
3559
3559
|
var import_mvdan_sh = __toESM(require("mvdan-sh"));
|
|
3560
|
+
var { syntax } = import_mvdan_sh.default;
|
|
3561
|
+
var KINDS = {
|
|
3562
|
+
File: { nodes: ["Stmts"] },
|
|
3563
|
+
Stmt: { nodes: ["Cmd", "Redirs"], scalars: ["Negated", "Background", "Coprocess"] },
|
|
3564
|
+
CallExpr: { nodes: ["Assigns", "Args"] },
|
|
3565
|
+
Assign: { nodes: ["Name", "Value", "Index", "Array"], scalars: ["Append", "Naked"] },
|
|
3566
|
+
Word: { nodes: ["Parts"] },
|
|
3567
|
+
Lit: { scalars: ["Value"] },
|
|
3568
|
+
SglQuoted: { scalars: ["Value", "Dollar"] },
|
|
3569
|
+
DblQuoted: { nodes: ["Parts"], scalars: ["Dollar"] },
|
|
3570
|
+
ParamExp: {
|
|
3571
|
+
nodes: ["Param", "Index", "Slice", "Repl", "Exp"],
|
|
3572
|
+
scalars: ["Short", "Excl", "Length", "Width"]
|
|
3573
|
+
},
|
|
3574
|
+
CmdSubst: { nodes: ["Stmts"], scalars: ["Backquotes", "TempFile", "ReplyVar"] },
|
|
3575
|
+
ArithmExp: { nodes: ["X"], scalars: ["Bracket", "Unsigned"] },
|
|
3576
|
+
ArithmCmd: { nodes: ["X"], scalars: ["Unsigned"] },
|
|
3577
|
+
ProcSubst: { nodes: ["Stmts"], scalars: ["Op"] },
|
|
3578
|
+
ExtGlob: { nodes: ["Pattern"], scalars: ["Op"] },
|
|
3579
|
+
BraceExp: { nodes: ["Elems"], scalars: ["Sequence"] },
|
|
3580
|
+
// only with SplitBraces, unused here
|
|
3581
|
+
BinaryCmd: { nodes: ["X", "Y"], scalars: ["Op"] },
|
|
3582
|
+
BinaryArithm: { nodes: ["X", "Y"], scalars: ["Op"] },
|
|
3583
|
+
UnaryArithm: { nodes: ["X"], scalars: ["Op", "Post"] },
|
|
3584
|
+
ParenArithm: { nodes: ["X"] },
|
|
3585
|
+
BinaryTest: { nodes: ["X", "Y"], scalars: ["Op"] },
|
|
3586
|
+
UnaryTest: { nodes: ["X"], scalars: ["Op"] },
|
|
3587
|
+
ParenTest: { nodes: ["X"] },
|
|
3588
|
+
Redirect: { nodes: ["N", "Word", "Hdoc"], scalars: ["Op"] },
|
|
3589
|
+
Block: { nodes: ["Stmts"] },
|
|
3590
|
+
Subshell: { nodes: ["Stmts"] },
|
|
3591
|
+
IfClause: { nodes: ["Cond", "Then", "Else"] },
|
|
3592
|
+
WhileClause: { nodes: ["Cond", "Do"], scalars: ["Until"] },
|
|
3593
|
+
ForClause: { nodes: ["Loop", "Do"], scalars: ["Select"] },
|
|
3594
|
+
WordIter: { nodes: ["Name", "Items"] },
|
|
3595
|
+
CStyleLoop: { nodes: ["Init", "Cond", "Post"] },
|
|
3596
|
+
CaseClause: { nodes: ["Word", "Items"] },
|
|
3597
|
+
CaseItem: { nodes: ["Patterns", "Stmts"], scalars: ["Op"] },
|
|
3598
|
+
FuncDecl: { nodes: ["Name", "Body"], scalars: ["RsrvWord"] },
|
|
3599
|
+
DeclClause: { nodes: ["Variant", "Args"], notWalked: ["Variant"] },
|
|
3600
|
+
LetClause: { nodes: ["Exprs"] },
|
|
3601
|
+
TimeClause: { nodes: ["Stmt"], scalars: ["PosixFormat"] },
|
|
3602
|
+
CoprocClause: { nodes: ["Name", "Stmt"] },
|
|
3603
|
+
TestClause: { nodes: ["X"] },
|
|
3604
|
+
TestDecl: { nodes: ["Description", "Body"] },
|
|
3605
|
+
ArrayExpr: { nodes: ["Elems"] },
|
|
3606
|
+
ArrayElem: { nodes: ["Index", "Value"] },
|
|
3607
|
+
Comment: { scalars: ["Text"] }
|
|
3608
|
+
};
|
|
3609
|
+
var STRUCTS = {
|
|
3610
|
+
"ParamExp.Slice": { nodes: ["Offset", "Length"], notWalked: ["Offset", "Length"] },
|
|
3611
|
+
"ParamExp.Repl": { nodes: ["Orig", "With"], scalars: ["All"] },
|
|
3612
|
+
"ParamExp.Exp": { nodes: ["Word"], scalars: ["Op"] }
|
|
3613
|
+
};
|
|
3614
|
+
var unknownKinds = /* @__PURE__ */ new Set();
|
|
3615
|
+
function isGoNode(v) {
|
|
3616
|
+
return v !== null && typeof v === "object" && typeof v.Pos === "function";
|
|
3617
|
+
}
|
|
3618
|
+
function toPlainTree(goNode) {
|
|
3619
|
+
const kind = syntax.NodeType(goNode);
|
|
3620
|
+
const out = { kind, pos: goNode.Pos().Offset(), end: goNode.End().Offset() };
|
|
3621
|
+
const spec = KINDS[kind];
|
|
3622
|
+
if (!spec) {
|
|
3623
|
+
unknownKinds.add(kind);
|
|
3624
|
+
return out;
|
|
3625
|
+
}
|
|
3626
|
+
convertFields(goNode, spec, out, kind);
|
|
3627
|
+
return out;
|
|
3628
|
+
}
|
|
3629
|
+
function convertFields(goNode, spec, out, kind) {
|
|
3630
|
+
for (const f of spec.scalars ?? []) out[f] = goNode[f];
|
|
3631
|
+
for (const f of spec.nodes ?? []) {
|
|
3632
|
+
const v = goNode[f];
|
|
3633
|
+
if (v === void 0 || v === null) {
|
|
3634
|
+
out[f] = null;
|
|
3635
|
+
} else if (Array.isArray(v)) {
|
|
3636
|
+
out[f] = v.map((c) => isGoNode(c) ? toPlainTree(c) : c);
|
|
3637
|
+
} else if (isGoNode(v)) {
|
|
3638
|
+
out[f] = toPlainTree(v);
|
|
3639
|
+
} else if (typeof v === "object" && STRUCTS[`${kind}.${f}`]) {
|
|
3640
|
+
const sub = { struct: `${kind}.${f}` };
|
|
3641
|
+
convertFields(v, STRUCTS[`${kind}.${f}`], sub, `${kind}.${f}`);
|
|
3642
|
+
out[f] = sub;
|
|
3643
|
+
} else {
|
|
3644
|
+
out[f] = v;
|
|
3645
|
+
}
|
|
3646
|
+
}
|
|
3647
|
+
}
|
|
3648
|
+
function childrenOf(n) {
|
|
3649
|
+
const spec = n.kind ? KINDS[n.kind] : STRUCTS[n.struct];
|
|
3650
|
+
if (!spec?.nodes) return [];
|
|
3651
|
+
const out = [];
|
|
3652
|
+
for (const f of spec.nodes) {
|
|
3653
|
+
if (spec.notWalked?.includes(f)) continue;
|
|
3654
|
+
const v = n[f];
|
|
3655
|
+
if (!v) continue;
|
|
3656
|
+
if (Array.isArray(v)) {
|
|
3657
|
+
for (const c of v) if (c) out.push(c);
|
|
3658
|
+
} else if (v.kind) {
|
|
3659
|
+
out.push(v);
|
|
3660
|
+
} else if (v.struct) {
|
|
3661
|
+
out.push(...childrenOf(v));
|
|
3662
|
+
}
|
|
3663
|
+
}
|
|
3664
|
+
return out;
|
|
3665
|
+
}
|
|
3666
|
+
function walk(root, fn) {
|
|
3667
|
+
if (!root) return;
|
|
3668
|
+
if (!fn(root)) return;
|
|
3669
|
+
for (const c of childrenOf(root)) walk(c, fn);
|
|
3670
|
+
}
|
|
3560
3671
|
|
|
3561
3672
|
// src/dlp/normalize.ts
|
|
3562
3673
|
var INVISIBLE_RE = /[͏----︀-️]|[\u{E0000}-\u{E007F}]/gu;
|
|
@@ -4003,8 +4114,8 @@ function judgeInlineCode(language, code, isProtected) {
|
|
|
4003
4114
|
}
|
|
4004
4115
|
|
|
4005
4116
|
// src/shell/index.ts
|
|
4006
|
-
var { syntax } =
|
|
4007
|
-
var sharedParser =
|
|
4117
|
+
var { syntax: syntax2 } = import_mvdan_sh2.default;
|
|
4118
|
+
var sharedParser = syntax2.NewParser();
|
|
4008
4119
|
var MESSAGE_FLAGS = /* @__PURE__ */ new Set([
|
|
4009
4120
|
"-m",
|
|
4010
4121
|
"--message",
|
|
@@ -4037,7 +4148,7 @@ var DOWNLOAD_CMDS = /* @__PURE__ */ new Set([
|
|
|
4037
4148
|
]);
|
|
4038
4149
|
function isCatHeredocOrLit(part) {
|
|
4039
4150
|
if (!part) return false;
|
|
4040
|
-
const t =
|
|
4151
|
+
const t = part.kind;
|
|
4041
4152
|
if (t === "Lit") return true;
|
|
4042
4153
|
if (t !== "CmdSubst") return false;
|
|
4043
4154
|
const stmts = part.Stmts || [];
|
|
@@ -4047,15 +4158,15 @@ function isCatHeredocOrLit(part) {
|
|
|
4047
4158
|
const hasHeredoc = redirs.some((r) => r && r.Hdoc);
|
|
4048
4159
|
if (!hasHeredoc) return false;
|
|
4049
4160
|
const cmd = stmt.Cmd;
|
|
4050
|
-
if (!cmd ||
|
|
4161
|
+
if (!cmd || cmd.kind !== "CallExpr") return false;
|
|
4051
4162
|
const firstArg = cmd.Args?.[0]?.Parts || [];
|
|
4052
|
-
if (firstArg.length !== 1 ||
|
|
4163
|
+
if (firstArg.length !== 1 || firstArg[0].kind !== "Lit") return false;
|
|
4053
4164
|
return (firstArg[0].Value || "").toLowerCase() === "cat";
|
|
4054
4165
|
}
|
|
4055
4166
|
var NORMALIZE_CACHE_MAX = 5e3;
|
|
4056
4167
|
var normalizeCache = /* @__PURE__ */ new Map();
|
|
4057
4168
|
var codeBodiesCache = /* @__PURE__ */ new Map();
|
|
4058
|
-
var AST_CACHE_MAX =
|
|
4169
|
+
var AST_CACHE_MAX = 256;
|
|
4059
4170
|
var astCache = /* @__PURE__ */ new Map();
|
|
4060
4171
|
var PARSE_FAIL = /* @__PURE__ */ Symbol("parse-fail");
|
|
4061
4172
|
function parseShared(command) {
|
|
@@ -4067,7 +4178,7 @@ function parseShared(command) {
|
|
|
4067
4178
|
}
|
|
4068
4179
|
let parsed;
|
|
4069
4180
|
try {
|
|
4070
|
-
parsed = sharedParser.Parse(command, "cmd");
|
|
4181
|
+
parsed = toPlainTree(sharedParser.Parse(command, "cmd"));
|
|
4071
4182
|
} catch {
|
|
4072
4183
|
parsed = PARSE_FAIL;
|
|
4073
4184
|
}
|
|
@@ -4132,10 +4243,10 @@ function fileAccessViewImpl(command) {
|
|
|
4132
4243
|
const at = (byteOffset) => toCharIndex === null ? byteOffset : toCharIndex(byteOffset);
|
|
4133
4244
|
const edits = [];
|
|
4134
4245
|
try {
|
|
4135
|
-
|
|
4246
|
+
walk(f, (node) => {
|
|
4136
4247
|
if (!node) return false;
|
|
4137
4248
|
const n = node;
|
|
4138
|
-
if (
|
|
4249
|
+
if (n.kind !== "CallExpr") return true;
|
|
4139
4250
|
const args = n.Args || [];
|
|
4140
4251
|
const words = args.map((a) => resolveWordLiteral(a));
|
|
4141
4252
|
const head = unwrapCommandHead(words);
|
|
@@ -4147,8 +4258,8 @@ function fileAccessViewImpl(command) {
|
|
|
4147
4258
|
if (!PROGRAM_DIR.test(word)) continue;
|
|
4148
4259
|
const name = word.split(/[\\/]/).pop() ?? "";
|
|
4149
4260
|
if (!name) continue;
|
|
4150
|
-
const s = at(args[i].
|
|
4151
|
-
const e = at(args[i].
|
|
4261
|
+
const s = at(args[i].pos);
|
|
4262
|
+
const e = at(args[i].end);
|
|
4152
4263
|
if (s < 0 || e < 0) continue;
|
|
4153
4264
|
edits.push([s, e, name]);
|
|
4154
4265
|
}
|
|
@@ -4177,10 +4288,10 @@ function literalPayloadsOf(command, depth) {
|
|
|
4177
4288
|
if (f === PARSE_FAIL) return [];
|
|
4178
4289
|
const out = [];
|
|
4179
4290
|
try {
|
|
4180
|
-
|
|
4291
|
+
walk(f, (node) => {
|
|
4181
4292
|
if (!node || out.length >= MAX_READING_PAYLOADS) return false;
|
|
4182
4293
|
const n = node;
|
|
4183
|
-
if (
|
|
4294
|
+
if (n.kind !== "CallExpr") return true;
|
|
4184
4295
|
const words = (n.Args || []).map((a) => resolveWordLiteral(a));
|
|
4185
4296
|
if (words.length === 0) return true;
|
|
4186
4297
|
const payload = literalShellPayload(words, baseWord(words[0]));
|
|
@@ -4200,23 +4311,23 @@ function foldCommandWords(reading) {
|
|
|
4200
4311
|
const at = (byteOffset) => toCharIndex === null ? byteOffset : toCharIndex(byteOffset);
|
|
4201
4312
|
const edits = [];
|
|
4202
4313
|
try {
|
|
4203
|
-
|
|
4314
|
+
walk(f, (node) => {
|
|
4204
4315
|
if (!node) return false;
|
|
4205
4316
|
const n = node;
|
|
4206
|
-
if (
|
|
4317
|
+
if (n.kind !== "CallExpr") return true;
|
|
4207
4318
|
const args = n.Args || [];
|
|
4208
4319
|
const words = args.map((a) => resolveWordLiteral(a));
|
|
4209
4320
|
const folded = words.map((w) => w === null ? null : foldHomoglyphs(w));
|
|
4210
4321
|
const head = unwrapCommandHead(folded);
|
|
4211
4322
|
for (let i = 0; i <= head && i < args.length; i++) {
|
|
4212
4323
|
const parts = args[i].Parts || [];
|
|
4213
|
-
if (parts.length !== 1 ||
|
|
4324
|
+
if (parts.length !== 1 || parts[0].kind !== "Lit") continue;
|
|
4214
4325
|
const value = parts[0].Value ?? "";
|
|
4215
4326
|
const word = folded[i];
|
|
4216
4327
|
if (word === null || word === value || !/[^\x00-\x7f]/.test(value)) continue;
|
|
4217
4328
|
if (!/^[A-Za-z0-9._/-]+$/.test(word)) continue;
|
|
4218
|
-
const s = at(args[i].
|
|
4219
|
-
const e = at(args[i].
|
|
4329
|
+
const s = at(args[i].pos);
|
|
4330
|
+
const e = at(args[i].end);
|
|
4220
4331
|
if (s < 0 || e < 0) continue;
|
|
4221
4332
|
edits.push([s, e, word]);
|
|
4222
4333
|
}
|
|
@@ -4284,10 +4395,10 @@ function interpreterHeredocSpans(f, at) {
|
|
|
4284
4395
|
const stmts = f?.Stmts || [];
|
|
4285
4396
|
const candidates = [];
|
|
4286
4397
|
let unsafe = false;
|
|
4287
|
-
|
|
4398
|
+
walk(f, (node) => {
|
|
4288
4399
|
if (!node || unsafe) return false;
|
|
4289
4400
|
const n = node;
|
|
4290
|
-
const t =
|
|
4401
|
+
const t = n.kind;
|
|
4291
4402
|
if (t === "FuncDecl") {
|
|
4292
4403
|
unsafe = true;
|
|
4293
4404
|
return false;
|
|
@@ -4307,7 +4418,7 @@ function interpreterHeredocSpans(f, at) {
|
|
|
4307
4418
|
});
|
|
4308
4419
|
if (unsafe) return [];
|
|
4309
4420
|
for (const st of stmts) {
|
|
4310
|
-
if (!st?.Cmd ||
|
|
4421
|
+
if (!st?.Cmd || st.Cmd.kind !== "CallExpr") continue;
|
|
4311
4422
|
const words = (st.Cmd.Args || []).map((a) => resolveWordLiteral(a));
|
|
4312
4423
|
const name = (words[0] ?? "").toLowerCase();
|
|
4313
4424
|
if (!STDIN_CODE_INTERPRETERS.test(name)) continue;
|
|
@@ -4322,8 +4433,8 @@ function interpreterHeredocSpans(f, at) {
|
|
|
4322
4433
|
if (stdin.length !== 1 || others > 0) continue;
|
|
4323
4434
|
const r = stdin[0];
|
|
4324
4435
|
if (heredocText(r.Hdoc) === null) continue;
|
|
4325
|
-
const hs = at(r.Hdoc.
|
|
4326
|
-
const he = at(r.Hdoc.
|
|
4436
|
+
const hs = at(r.Hdoc.pos);
|
|
4437
|
+
const he = at(r.Hdoc.end);
|
|
4327
4438
|
if (hs < 0 || he < 0 || he <= hs) continue;
|
|
4328
4439
|
spans.push([hs, he]);
|
|
4329
4440
|
}
|
|
@@ -4343,24 +4454,24 @@ function normalizeCommandForPolicyImpl(command, codeBodies) {
|
|
|
4343
4454
|
strips.push([hs, he]);
|
|
4344
4455
|
msgSpans.add(`${hs}:${he}`);
|
|
4345
4456
|
}
|
|
4346
|
-
|
|
4457
|
+
walk(f, (node) => {
|
|
4347
4458
|
if (!node) return false;
|
|
4348
4459
|
const n = node;
|
|
4349
|
-
if (
|
|
4460
|
+
if (n.kind !== "CallExpr") return true;
|
|
4350
4461
|
const args = n.Args || [];
|
|
4351
4462
|
for (let i = 0; i < args.length - 1; i++) {
|
|
4352
4463
|
const argParts = args[i].Parts || [];
|
|
4353
|
-
if (argParts.length !== 1 ||
|
|
4464
|
+
if (argParts.length !== 1 || argParts[0].kind !== "Lit") continue;
|
|
4354
4465
|
const flagVal = argParts[0].Value || "";
|
|
4355
4466
|
if (!MESSAGE_FLAGS.has(flagVal.toLowerCase())) continue;
|
|
4356
4467
|
const next = args[i + 1];
|
|
4357
4468
|
const nextParts = next.Parts || [];
|
|
4358
4469
|
if (nextParts.length !== 1) continue;
|
|
4359
4470
|
const quotedNode = nextParts[0];
|
|
4360
|
-
const nt =
|
|
4471
|
+
const nt = quotedNode.kind;
|
|
4361
4472
|
const markStrip = () => {
|
|
4362
|
-
const s = at(next.
|
|
4363
|
-
const e = at(next.
|
|
4473
|
+
const s = at(next.pos);
|
|
4474
|
+
const e = at(next.end);
|
|
4364
4475
|
if (s < 0 || e < 0) return;
|
|
4365
4476
|
strips.push([s, e]);
|
|
4366
4477
|
msgSpans.add(`${s}:${e}`);
|
|
@@ -4369,7 +4480,7 @@ function normalizeCommandForPolicyImpl(command, codeBodies) {
|
|
|
4369
4480
|
markStrip();
|
|
4370
4481
|
} else if (nt === "DblQuoted") {
|
|
4371
4482
|
const innerParts = quotedNode.Parts || [];
|
|
4372
|
-
const allLit = innerParts.length === 0 || innerParts.every((p) =>
|
|
4483
|
+
const allLit = innerParts.length === 0 || innerParts.every((p) => p.kind === "Lit");
|
|
4373
4484
|
if (allLit) {
|
|
4374
4485
|
markStrip();
|
|
4375
4486
|
} else if (innerParts.every((p) => isCatHeredocOrLit(p))) {
|
|
@@ -4378,8 +4489,8 @@ function normalizeCommandForPolicyImpl(command, codeBodies) {
|
|
|
4378
4489
|
}
|
|
4379
4490
|
}
|
|
4380
4491
|
for (const arg of args) {
|
|
4381
|
-
const s = at(arg.
|
|
4382
|
-
const e = at(arg.
|
|
4492
|
+
const s = at(arg.pos);
|
|
4493
|
+
const e = at(arg.end);
|
|
4383
4494
|
if (s < 0 || e < 0) continue;
|
|
4384
4495
|
if (msgSpans.has(`${s}:${e}`)) continue;
|
|
4385
4496
|
const resolved = resolveWordLiteral(arg);
|
|
@@ -4419,10 +4530,10 @@ function scanArgsForDynamicExec(args, startIdx) {
|
|
|
4419
4530
|
let hasParamExp = false;
|
|
4420
4531
|
let hasCurl = false;
|
|
4421
4532
|
for (let i = startIdx; i < args.length; i++) {
|
|
4422
|
-
|
|
4533
|
+
walk(args[i], (inner) => {
|
|
4423
4534
|
if (!inner) return false;
|
|
4424
4535
|
const inn = inner;
|
|
4425
|
-
const it =
|
|
4536
|
+
const it = inn.kind;
|
|
4426
4537
|
if (it === "CmdSubst") hasCmdSubst = true;
|
|
4427
4538
|
if (it === "ParamExp") hasParamExp = true;
|
|
4428
4539
|
if (it === "Lit" && DOWNLOAD_CMDS.has(inn.Value?.toLowerCase())) hasCurl = true;
|
|
@@ -4435,15 +4546,16 @@ function scanArgsForDynamicExec(args, startIdx) {
|
|
|
4435
4546
|
}
|
|
4436
4547
|
function detectDangerousShellExec(command) {
|
|
4437
4548
|
try {
|
|
4438
|
-
const f =
|
|
4549
|
+
const f = parseShared(command);
|
|
4550
|
+
if (f === PARSE_FAIL) return null;
|
|
4439
4551
|
let result = null;
|
|
4440
4552
|
const assigned = assignedNames(f);
|
|
4441
|
-
|
|
4553
|
+
walk(f, (node) => {
|
|
4442
4554
|
if (!node || result === "block") return false;
|
|
4443
4555
|
const n = node;
|
|
4444
|
-
if (
|
|
4556
|
+
if (n.kind === "BinaryCmd" && n.Y && !listOps().has(n.Op)) {
|
|
4445
4557
|
const call = n.Y.Cmd;
|
|
4446
|
-
if (call &&
|
|
4558
|
+
if (call && call.kind === "CallExpr") {
|
|
4447
4559
|
const words = (call.Args || []).map((a) => resolveWordLiteral(a));
|
|
4448
4560
|
const head2 = unwrapCommandHead(words);
|
|
4449
4561
|
const name = (words[head2] ?? "").split("/").pop() ?? "";
|
|
@@ -4456,7 +4568,7 @@ function detectDangerousShellExec(command) {
|
|
|
4456
4568
|
result = "review";
|
|
4457
4569
|
}
|
|
4458
4570
|
}
|
|
4459
|
-
if (
|
|
4571
|
+
if (n.kind !== "CallExpr") return true;
|
|
4460
4572
|
const args = n.Args || [];
|
|
4461
4573
|
if (args.length === 0) return true;
|
|
4462
4574
|
const headWords = args.map(
|
|
@@ -4467,7 +4579,7 @@ function detectDangerousShellExec(command) {
|
|
|
4467
4579
|
if (!lookup && (opaqueExecutable(args[0], assigned) || opaqueExecutable(args[head], assigned)))
|
|
4468
4580
|
result = "review";
|
|
4469
4581
|
const firstParts = args[0].Parts || [];
|
|
4470
|
-
if (firstParts.length !== 1 ||
|
|
4582
|
+
if (firstParts.length !== 1 || firstParts[0].kind !== "Lit") return true;
|
|
4471
4583
|
const cmdName = firstParts[0].Value?.toLowerCase() ?? "";
|
|
4472
4584
|
if (cmdName === "eval") {
|
|
4473
4585
|
const v = scanArgsForDynamicExec(args, 1);
|
|
@@ -4475,7 +4587,7 @@ function detectDangerousShellExec(command) {
|
|
|
4475
4587
|
} else if (SHELL_INTERPRETERS.has(cmdName)) {
|
|
4476
4588
|
for (let i = 1; i < args.length - 1; i++) {
|
|
4477
4589
|
const flagParts = args[i].Parts || [];
|
|
4478
|
-
if (flagParts.length !== 1 ||
|
|
4590
|
+
if (flagParts.length !== 1 || flagParts[0].kind !== "Lit" || flagParts[0].Value !== "-c")
|
|
4479
4591
|
continue;
|
|
4480
4592
|
const v = scanArgsForDynamicExec(args, i + 1);
|
|
4481
4593
|
if (v === "block" || v === "review" && result === null) result = v;
|
|
@@ -4500,8 +4612,7 @@ function isDefaultExpansion(part) {
|
|
|
4500
4612
|
}
|
|
4501
4613
|
function lookupSubstitution(part) {
|
|
4502
4614
|
const stmts = part.Stmts ?? [];
|
|
4503
|
-
if (stmts.length !== 1 || !stmts[0].Cmd ||
|
|
4504
|
-
return null;
|
|
4615
|
+
if (stmts.length !== 1 || !stmts[0].Cmd || stmts[0].Cmd.kind !== "CallExpr") return null;
|
|
4505
4616
|
const words = (stmts[0].Cmd.Args ?? []).map((a) => resolveWordLiteral(a));
|
|
4506
4617
|
if (words.some((w) => w === null)) return null;
|
|
4507
4618
|
if (words.length === 2 && words[0] === "which") return words[1];
|
|
@@ -4523,9 +4634,9 @@ var VARIABLE_READERS = /* @__PURE__ */ new Set([
|
|
|
4523
4634
|
]);
|
|
4524
4635
|
function assignedNames(f) {
|
|
4525
4636
|
const names = /* @__PURE__ */ new Set();
|
|
4526
|
-
|
|
4637
|
+
walk(f, (node) => {
|
|
4527
4638
|
if (!node) return false;
|
|
4528
|
-
const kind =
|
|
4639
|
+
const kind = node.kind;
|
|
4529
4640
|
if (kind === "Assign" && node.Name?.Value) names.add(node.Name.Value);
|
|
4530
4641
|
if (kind === "ForClause" && node.Loop?.Name?.Value) names.add(node.Loop.Name.Value);
|
|
4531
4642
|
if (kind === "CallExpr") {
|
|
@@ -4540,7 +4651,7 @@ function assignedNames(f) {
|
|
|
4540
4651
|
}
|
|
4541
4652
|
function executableParts(word, assigned, quoted = false) {
|
|
4542
4653
|
return (word?.Parts ?? []).flatMap((part) => {
|
|
4543
|
-
const kind =
|
|
4654
|
+
const kind = part.kind;
|
|
4544
4655
|
if (kind === "DblQuoted") return executableParts(part, assigned, true);
|
|
4545
4656
|
if (kind === "Lit") return [{ kind: "Lit", value: part.Value ?? "", quoted }];
|
|
4546
4657
|
if (kind === "SglQuoted")
|
|
@@ -5264,10 +5375,10 @@ function chmodHasOpenPermMode(command) {
|
|
|
5264
5375
|
if (f === PARSE_FAIL) return false;
|
|
5265
5376
|
let found = false;
|
|
5266
5377
|
try {
|
|
5267
|
-
|
|
5378
|
+
walk(f, (node) => {
|
|
5268
5379
|
if (!node || found) return false;
|
|
5269
5380
|
const n = node;
|
|
5270
|
-
if (
|
|
5381
|
+
if (n.kind !== "CallExpr") return true;
|
|
5271
5382
|
const words = (n.Args || []).map((a) => resolveWordLiteral(a));
|
|
5272
5383
|
if (words.length === 0) return true;
|
|
5273
5384
|
const name = (words[0] ?? "").toLowerCase();
|
|
@@ -5344,11 +5455,11 @@ function redirStdinOps() {
|
|
|
5344
5455
|
}
|
|
5345
5456
|
function deriveBinaryOp(sample) {
|
|
5346
5457
|
try {
|
|
5347
|
-
const f = sharedParser.Parse(sample, "cmd");
|
|
5458
|
+
const f = toPlainTree(sharedParser.Parse(sample, "cmd"));
|
|
5348
5459
|
let op = -1;
|
|
5349
|
-
|
|
5460
|
+
walk(f, (node) => {
|
|
5350
5461
|
const n = node;
|
|
5351
|
-
if (n &&
|
|
5462
|
+
if (n && n.kind === "BinaryCmd" && op < 0) op = n.Op;
|
|
5352
5463
|
return true;
|
|
5353
5464
|
});
|
|
5354
5465
|
return op;
|
|
@@ -5444,7 +5555,7 @@ function heredocText(word) {
|
|
|
5444
5555
|
const parts = word?.Parts ?? [];
|
|
5445
5556
|
let out = "";
|
|
5446
5557
|
for (const p of parts) {
|
|
5447
|
-
if (
|
|
5558
|
+
if (p.kind !== "Lit") return null;
|
|
5448
5559
|
out += p.Value ?? "";
|
|
5449
5560
|
}
|
|
5450
5561
|
return out;
|
|
@@ -5480,7 +5591,7 @@ function attachedCode(interp, flag) {
|
|
|
5480
5591
|
}
|
|
5481
5592
|
function inlineExecCall(stmt, pipeFed) {
|
|
5482
5593
|
const cmd = stmt?.Cmd;
|
|
5483
|
-
if (!cmd ||
|
|
5594
|
+
if (!cmd || cmd.kind !== "CallExpr") return null;
|
|
5484
5595
|
const words = (cmd.Args || []).map((a) => resolveWordLiteral(a));
|
|
5485
5596
|
if (words.length === 0) return null;
|
|
5486
5597
|
const headIdx = unwrapCommandHead(words);
|
|
@@ -5563,10 +5674,10 @@ function shellPayloadNeedsReview(payload) {
|
|
|
5563
5674
|
let review = false;
|
|
5564
5675
|
payloadNesting++;
|
|
5565
5676
|
try {
|
|
5566
|
-
|
|
5677
|
+
walk(f, (node) => {
|
|
5567
5678
|
if (!node || review) return false;
|
|
5568
5679
|
const n = node;
|
|
5569
|
-
if (
|
|
5680
|
+
if (n.kind !== "CallExpr") return true;
|
|
5570
5681
|
const words = (n.Args || []).map((a) => resolveWordLiteral(a));
|
|
5571
5682
|
if (words.length === 0) return true;
|
|
5572
5683
|
const h = unwrapCommandHead(words);
|
|
@@ -5601,10 +5712,10 @@ function inlineExecCalls(command) {
|
|
|
5601
5712
|
if (f === PARSE_FAIL) return PARSE_FAIL;
|
|
5602
5713
|
const calls = [];
|
|
5603
5714
|
try {
|
|
5604
|
-
|
|
5715
|
+
walk(f, (node) => {
|
|
5605
5716
|
if (!node || calls.length >= MAX_INLINE_CALLS) return false;
|
|
5606
5717
|
const n = node;
|
|
5607
|
-
const t =
|
|
5718
|
+
const t = n.kind;
|
|
5608
5719
|
if (t === "BinaryCmd" && n.Y && !listOps().has(n.Op)) {
|
|
5609
5720
|
const call = inlineExecCall(n.Y, true);
|
|
5610
5721
|
if (call) calls.push(call);
|
|
@@ -5749,7 +5860,7 @@ function shellScriptOperands(command) {
|
|
|
5749
5860
|
walkWithCalls(f, (node) => {
|
|
5750
5861
|
if (!node) return false;
|
|
5751
5862
|
const n = node;
|
|
5752
|
-
if (
|
|
5863
|
+
if (n.kind !== "CallExpr") return true;
|
|
5753
5864
|
const callArgs = n.Args || [];
|
|
5754
5865
|
if (callArgs.length === 0) return true;
|
|
5755
5866
|
const words = callArgs.map((a) => resolveWordLiteral(a));
|
|
@@ -5785,7 +5896,7 @@ function shellScriptOperands(command) {
|
|
|
5785
5896
|
}
|
|
5786
5897
|
function windowsDrivePathSpelling(word) {
|
|
5787
5898
|
const parts = word?.Parts || [];
|
|
5788
|
-
if (parts.length === 0 || !parts.every((p) =>
|
|
5899
|
+
if (parts.length === 0 || !parts.every((p) => p.kind === "Lit")) return null;
|
|
5789
5900
|
const raw = parts.map((p) => p.Value ?? "").join("");
|
|
5790
5901
|
return /^[A-Za-z]:\\[^\s'"$`]*$/.test(raw) ? raw : null;
|
|
5791
5902
|
}
|
|
@@ -5990,8 +6101,8 @@ function recordTopLevelAssignments(f) {
|
|
|
5990
6101
|
}
|
|
5991
6102
|
function probeBinOp(src) {
|
|
5992
6103
|
try {
|
|
5993
|
-
const cmd =
|
|
5994
|
-
if (cmd &&
|
|
6104
|
+
const cmd = toPlainTree(syntax2.NewParser().Parse(src, "probe"))?.Stmts?.[0]?.Cmd;
|
|
6105
|
+
if (cmd && cmd.kind === "BinaryCmd") return cmd.Op;
|
|
5995
6106
|
} catch {
|
|
5996
6107
|
}
|
|
5997
6108
|
return null;
|
|
@@ -6001,7 +6112,7 @@ var OR_OP = probeBinOp("a || b");
|
|
|
6001
6112
|
var AND_OR_OPS = new Set([AND_OP, OR_OP].filter((o) => o !== null));
|
|
6002
6113
|
function recordTopLevelStmt(stmt) {
|
|
6003
6114
|
if (!stmt || !stmt.Cmd) return false;
|
|
6004
|
-
const t =
|
|
6115
|
+
const t = stmt.Cmd.kind;
|
|
6005
6116
|
if (t === "BinaryCmd") {
|
|
6006
6117
|
if (!AND_OR_OPS.has(stmt.Cmd.Op)) return false;
|
|
6007
6118
|
if (!recordTopLevelStmt(stmt.Cmd.X)) return false;
|
|
@@ -6011,7 +6122,7 @@ function recordTopLevelStmt(stmt) {
|
|
|
6011
6122
|
if (t !== "CallExpr" && t !== "DeclClause") return false;
|
|
6012
6123
|
let at = 0;
|
|
6013
6124
|
try {
|
|
6014
|
-
at = stmt.
|
|
6125
|
+
at = stmt.pos;
|
|
6015
6126
|
} catch {
|
|
6016
6127
|
at = 0;
|
|
6017
6128
|
}
|
|
@@ -6022,14 +6133,14 @@ function recordTopLevelStmt(stmt) {
|
|
|
6022
6133
|
}
|
|
6023
6134
|
function recordAssignments(n, at) {
|
|
6024
6135
|
if (!assignmentTable) return;
|
|
6025
|
-
const t =
|
|
6136
|
+
const t = n.kind;
|
|
6026
6137
|
let assigns = [];
|
|
6027
6138
|
if (t === "CallExpr") {
|
|
6028
6139
|
if ((n.Args || []).length > 0) return;
|
|
6029
6140
|
assigns = n.Assigns || [];
|
|
6030
6141
|
} else if (t === "DeclClause") {
|
|
6031
6142
|
if (!ASSIGNMENT_HEADS.has(n.Variant?.Value ?? "")) return;
|
|
6032
|
-
assigns = (n.Args || []).filter((a) =>
|
|
6143
|
+
assigns = (n.Args || []).filter((a) => a.kind === "Assign");
|
|
6033
6144
|
} else return;
|
|
6034
6145
|
for (const a of assigns) {
|
|
6035
6146
|
const name = a?.Name?.Value;
|
|
@@ -6038,13 +6149,13 @@ function recordAssignments(n, at) {
|
|
|
6038
6149
|
}
|
|
6039
6150
|
}
|
|
6040
6151
|
function resolveTrivialSubst(part) {
|
|
6041
|
-
if (
|
|
6152
|
+
if (part.kind !== "CmdSubst") return void 0;
|
|
6042
6153
|
const stmts = part.Stmts || [];
|
|
6043
6154
|
if (stmts.length !== 1) return void 0;
|
|
6044
6155
|
const st = stmts[0];
|
|
6045
6156
|
if ((st.Redirs || []).length > 0 || st.Negated || st.Background) return void 0;
|
|
6046
6157
|
const cmd = st.Cmd;
|
|
6047
|
-
if (!cmd ||
|
|
6158
|
+
if (!cmd || cmd.kind !== "CallExpr") return void 0;
|
|
6048
6159
|
if ((cmd.Assigns || []).length > 0) return void 0;
|
|
6049
6160
|
const words = (cmd.Args || []).map((a) => resolveWordLiteral(a));
|
|
6050
6161
|
if (words.length === 0 || words.some((w) => w === null)) return void 0;
|
|
@@ -6069,7 +6180,7 @@ function recordedExpansion(name) {
|
|
|
6069
6180
|
return rec.value;
|
|
6070
6181
|
}
|
|
6071
6182
|
function isPlainParam(p) {
|
|
6072
|
-
if (
|
|
6183
|
+
if (p.kind !== "ParamExp") return false;
|
|
6073
6184
|
return !(p.Excl || p.Length || p.Width || p.Index || p.Slice || p.Repl || p.Exp);
|
|
6074
6185
|
}
|
|
6075
6186
|
function expandPlainParam(p) {
|
|
@@ -6085,9 +6196,9 @@ var callDepth = 0;
|
|
|
6085
6196
|
var callWalks = 0;
|
|
6086
6197
|
function functionBodies(f) {
|
|
6087
6198
|
const out = /* @__PURE__ */ new Map();
|
|
6088
|
-
|
|
6199
|
+
walk(f, (node) => {
|
|
6089
6200
|
const n = node;
|
|
6090
|
-
if (n &&
|
|
6201
|
+
if (n && n.kind === "FuncDecl" && n.Name?.Value && n.Body) {
|
|
6091
6202
|
const list = out.get(n.Name.Value) ?? [];
|
|
6092
6203
|
list.push(n.Body);
|
|
6093
6204
|
out.set(n.Name.Value, list);
|
|
@@ -6098,10 +6209,10 @@ function functionBodies(f) {
|
|
|
6098
6209
|
}
|
|
6099
6210
|
function rebindsPositionals(body) {
|
|
6100
6211
|
let found = false;
|
|
6101
|
-
|
|
6212
|
+
walk(body, (node) => {
|
|
6102
6213
|
const n = node;
|
|
6103
6214
|
if (found || !n) return false;
|
|
6104
|
-
if (
|
|
6215
|
+
if (n.kind === "CallExpr") {
|
|
6105
6216
|
const words = (n.Args || []).map((a) => resolveWordLiteral(a));
|
|
6106
6217
|
if (words[0] === "shift") found = true;
|
|
6107
6218
|
else if (words[0] === "set" && words.slice(1).some((w) => w === null || w === "--" || !/^[-+]/.test(w)))
|
|
@@ -6121,7 +6232,7 @@ function tableBindings() {
|
|
|
6121
6232
|
function walkWithCalls(f, visit) {
|
|
6122
6233
|
const bodies = functionBodies(f);
|
|
6123
6234
|
if (bodies.size === 0) {
|
|
6124
|
-
|
|
6235
|
+
walk(f, visit);
|
|
6125
6236
|
return;
|
|
6126
6237
|
}
|
|
6127
6238
|
const seenCalls = /* @__PURE__ */ new Set();
|
|
@@ -6155,8 +6266,8 @@ function walkWithCalls(f, visit) {
|
|
|
6155
6266
|
currentStmtOffset = Number.MAX_SAFE_INTEGER;
|
|
6156
6267
|
callDepth++;
|
|
6157
6268
|
try {
|
|
6158
|
-
if (body.Cmd &&
|
|
6159
|
-
|
|
6269
|
+
if (body.Cmd && body.Cmd.kind === "Block") recordTopLevelAssignments(body.Cmd);
|
|
6270
|
+
walk(body, step);
|
|
6160
6271
|
} finally {
|
|
6161
6272
|
callDepth--;
|
|
6162
6273
|
assignmentTable = outerTable;
|
|
@@ -6167,10 +6278,10 @@ function walkWithCalls(f, visit) {
|
|
|
6167
6278
|
const step = (node) => {
|
|
6168
6279
|
if (!node) return false;
|
|
6169
6280
|
const go = visit(node);
|
|
6170
|
-
if (go !== false &&
|
|
6281
|
+
if (go !== false && node.kind === "CallExpr") inline(node);
|
|
6171
6282
|
return go;
|
|
6172
6283
|
};
|
|
6173
|
-
|
|
6284
|
+
walk(f, step);
|
|
6174
6285
|
}
|
|
6175
6286
|
function withAssignments(f, body) {
|
|
6176
6287
|
const outerTable = assignmentTable;
|
|
@@ -6195,7 +6306,7 @@ function walkCommandScope(command, visit) {
|
|
|
6195
6306
|
if (f === PARSE_FAIL) return "parse-fail";
|
|
6196
6307
|
withAssignments(f, () => {
|
|
6197
6308
|
walkWithCalls(f, (node) => {
|
|
6198
|
-
if (node &&
|
|
6309
|
+
if (node && node.kind === "Stmt") trackStatement(node);
|
|
6199
6310
|
return visit(node);
|
|
6200
6311
|
});
|
|
6201
6312
|
});
|
|
@@ -6203,7 +6314,7 @@ function walkCommandScope(command, visit) {
|
|
|
6203
6314
|
}
|
|
6204
6315
|
function trackStatement(n) {
|
|
6205
6316
|
try {
|
|
6206
|
-
currentStmtOffset = n.
|
|
6317
|
+
currentStmtOffset = n.pos;
|
|
6207
6318
|
} catch {
|
|
6208
6319
|
currentStmtOffset = Number.MAX_SAFE_INTEGER;
|
|
6209
6320
|
}
|
|
@@ -6219,11 +6330,11 @@ function resolveWordLiteral(w) {
|
|
|
6219
6330
|
return s;
|
|
6220
6331
|
}
|
|
6221
6332
|
function resolveWordPrefix(w) {
|
|
6222
|
-
const
|
|
6333
|
+
const walk3 = (parts) => {
|
|
6223
6334
|
let s = "";
|
|
6224
6335
|
for (const p of parts) {
|
|
6225
6336
|
if (p && typeof p === "object" && Array.isArray(p.Parts) && p.Dollar !== void 0) {
|
|
6226
|
-
const inner =
|
|
6337
|
+
const inner = walk3(p.Parts);
|
|
6227
6338
|
s += inner.prefix;
|
|
6228
6339
|
if (inner.dynamic) return { prefix: s, dynamic: true };
|
|
6229
6340
|
continue;
|
|
@@ -6234,10 +6345,10 @@ function resolveWordPrefix(w) {
|
|
|
6234
6345
|
}
|
|
6235
6346
|
return { prefix: s, dynamic: false };
|
|
6236
6347
|
};
|
|
6237
|
-
return
|
|
6348
|
+
return walk3(w?.Parts || []);
|
|
6238
6349
|
}
|
|
6239
6350
|
function resolvePart(p, inQuotes) {
|
|
6240
|
-
const t =
|
|
6351
|
+
const t = p.kind;
|
|
6241
6352
|
if (t === "Lit") {
|
|
6242
6353
|
const raw = p.Value ?? "";
|
|
6243
6354
|
if (!inQuotes) return raw.replace(/\\(.)/g, "$1");
|
|
@@ -6333,7 +6444,7 @@ function extractShellDestinations(command) {
|
|
|
6333
6444
|
() => walkWithCalls(f, (node) => {
|
|
6334
6445
|
if (!node) return false;
|
|
6335
6446
|
const n = node;
|
|
6336
|
-
if (
|
|
6447
|
+
if (n.kind === "Stmt") {
|
|
6337
6448
|
trackStatement(n);
|
|
6338
6449
|
for (const raw of redirectWords(n)) {
|
|
6339
6450
|
const host2 = devNetRedirectHost(raw);
|
|
@@ -6345,7 +6456,7 @@ function extractShellDestinations(command) {
|
|
|
6345
6456
|
out.push({ host: host2, binary, raw });
|
|
6346
6457
|
}
|
|
6347
6458
|
}
|
|
6348
|
-
if (
|
|
6459
|
+
if (n.kind !== "CallExpr") return true;
|
|
6349
6460
|
const callArgs = n.Args || [];
|
|
6350
6461
|
if (callArgs.length === 0) return true;
|
|
6351
6462
|
const words = callArgs.map((a) => resolveWordLiteral(a));
|
|
@@ -6380,7 +6491,7 @@ function extractShellDestTokens(command) {
|
|
|
6380
6491
|
() => walkWithCalls(f, (node) => {
|
|
6381
6492
|
if (!node) return false;
|
|
6382
6493
|
const n = node;
|
|
6383
|
-
if (
|
|
6494
|
+
if (n.kind === "Stmt") {
|
|
6384
6495
|
trackStatement(n);
|
|
6385
6496
|
for (const raw of redirectWords(n)) {
|
|
6386
6497
|
const host2 = devNetRedirectHost(raw);
|
|
@@ -6392,7 +6503,7 @@ function extractShellDestTokens(command) {
|
|
|
6392
6503
|
out.push({ token: host2, binary });
|
|
6393
6504
|
}
|
|
6394
6505
|
}
|
|
6395
|
-
if (
|
|
6506
|
+
if (n.kind !== "CallExpr") return true;
|
|
6396
6507
|
const callArgs = n.Args || [];
|
|
6397
6508
|
if (callArgs.length === 0) return true;
|
|
6398
6509
|
const words = callArgs.map((a) => resolveWordLiteral(a));
|
|
@@ -6463,11 +6574,11 @@ function isWaivableCleanupTarget(p) {
|
|
|
6463
6574
|
}
|
|
6464
6575
|
function deriveRedirOp(sample) {
|
|
6465
6576
|
try {
|
|
6466
|
-
const f = sharedParser.Parse(sample, "cmd");
|
|
6577
|
+
const f = toPlainTree(sharedParser.Parse(sample, "cmd"));
|
|
6467
6578
|
let op = -1;
|
|
6468
|
-
|
|
6579
|
+
walk(f, (node) => {
|
|
6469
6580
|
const n = node;
|
|
6470
|
-
if (n &&
|
|
6581
|
+
if (n && n.kind === "Redirect" && op < 0) op = n.Op;
|
|
6471
6582
|
return true;
|
|
6472
6583
|
});
|
|
6473
6584
|
return op;
|
|
@@ -6488,7 +6599,7 @@ function collectSameCommandCreations(f) {
|
|
|
6488
6599
|
try {
|
|
6489
6600
|
const stmts = Array.isArray(f?.Stmts) ? f.Stmts : [];
|
|
6490
6601
|
for (const stmt of stmts) {
|
|
6491
|
-
if (!stmt || !stmt.Cmd ||
|
|
6602
|
+
if (!stmt || !stmt.Cmd || stmt.Cmd.kind !== "CallExpr") continue;
|
|
6492
6603
|
const redirs = stmt.Redirs || [];
|
|
6493
6604
|
if (!redirs.some((r) => r && REDIR_HEREDOC_OPS.has(r.Op))) continue;
|
|
6494
6605
|
for (const r of redirs) {
|
|
@@ -6512,10 +6623,10 @@ function isRmCreatedInCommandCleanup(command) {
|
|
|
6512
6623
|
let sawRm = false;
|
|
6513
6624
|
let ok = true;
|
|
6514
6625
|
try {
|
|
6515
|
-
|
|
6626
|
+
walk(f, (node) => {
|
|
6516
6627
|
if (!node || !ok) return false;
|
|
6517
6628
|
const n = node;
|
|
6518
|
-
if (
|
|
6629
|
+
if (n.kind !== "CallExpr") return true;
|
|
6519
6630
|
const args = n.Args || [];
|
|
6520
6631
|
const name = (resolveWordLiteral(args[0]) ?? "").toLowerCase();
|
|
6521
6632
|
if (name !== "rm") return true;
|
|
@@ -6565,10 +6676,10 @@ function analyzeFsOperationImpl(command, depth = 0) {
|
|
|
6565
6676
|
walkWithCalls(f, (node) => {
|
|
6566
6677
|
if (!node || result?.verdict === "block") return false;
|
|
6567
6678
|
const n = node;
|
|
6568
|
-
const nodeType =
|
|
6679
|
+
const nodeType = n.kind;
|
|
6569
6680
|
if (nodeType === "Stmt") {
|
|
6570
6681
|
try {
|
|
6571
|
-
currentStmtOffset = n.
|
|
6682
|
+
currentStmtOffset = n.pos;
|
|
6572
6683
|
} catch {
|
|
6573
6684
|
currentStmtOffset = Number.MAX_SAFE_INTEGER;
|
|
6574
6685
|
}
|
|
@@ -7111,11 +7222,12 @@ function analyzeShellCommand(command) {
|
|
|
7111
7222
|
if (lower2.startsWith("-")) allTokens.push(lower2.replace(/^-+/, ""));
|
|
7112
7223
|
};
|
|
7113
7224
|
try {
|
|
7114
|
-
const f =
|
|
7115
|
-
|
|
7225
|
+
const f = parseShared(command);
|
|
7226
|
+
if (f === PARSE_FAIL) throw new Error("parse failed");
|
|
7227
|
+
walk(f, (node) => {
|
|
7116
7228
|
if (!node) return false;
|
|
7117
7229
|
const n = node;
|
|
7118
|
-
if (
|
|
7230
|
+
if (n.kind !== "CallExpr") return true;
|
|
7119
7231
|
const wordValues2 = (n.Args || []).map((arg) => {
|
|
7120
7232
|
return (arg.Parts || []).map((p) => (p.Value ?? "").replace(/\\(.)/g, "$1")).join("");
|
|
7121
7233
|
}).filter((s) => s.length > 0);
|
|
@@ -7414,8 +7526,8 @@ function shellRuleArgs(tool, args, ruleTool, inspection = {}) {
|
|
|
7414
7526
|
}
|
|
7415
7527
|
|
|
7416
7528
|
// src/shell/file-access.ts
|
|
7417
|
-
var
|
|
7418
|
-
var { syntax:
|
|
7529
|
+
var import_mvdan_sh3 = __toESM(require("mvdan-sh"));
|
|
7530
|
+
var { syntax: syntax3 } = import_mvdan_sh3.default;
|
|
7419
7531
|
var MAX_ALTERNATIVES = 64;
|
|
7420
7532
|
var MAX_CALL_DEPTH2 = 4;
|
|
7421
7533
|
var MAX_CALL_WALKS2 = 256;
|
|
@@ -7432,7 +7544,7 @@ function runnablePrefix(command) {
|
|
|
7432
7544
|
let text = command;
|
|
7433
7545
|
for (let attempt = 0; attempt < 32; attempt++) {
|
|
7434
7546
|
try {
|
|
7435
|
-
return
|
|
7547
|
+
return toPlainTree(syntax3.NewParser().Parse(text, ""));
|
|
7436
7548
|
} catch (error) {
|
|
7437
7549
|
const message = String(typeof error?.Error === "function" ? error.Error() : error?.message);
|
|
7438
7550
|
const line = Number(/^(\d+):/.exec(message)?.[1]);
|
|
@@ -7577,7 +7689,7 @@ function wordValues(word, state) {
|
|
|
7577
7689
|
out2 = next.slice(0, MAX_ALTERNATIVES);
|
|
7578
7690
|
};
|
|
7579
7691
|
for (const node of nodes) {
|
|
7580
|
-
const kind =
|
|
7692
|
+
const kind = node.kind;
|
|
7581
7693
|
let pieces;
|
|
7582
7694
|
if (kind === "ParamExp") pieces = paramValues(node, quoted);
|
|
7583
7695
|
else if (kind === "DblQuoted") {
|
|
@@ -7645,9 +7757,9 @@ function collectShellFileAccesses(command, context = {}) {
|
|
|
7645
7757
|
}
|
|
7646
7758
|
function substitutions(word, state, depth) {
|
|
7647
7759
|
if (!word) return;
|
|
7648
|
-
|
|
7760
|
+
walk(word, (node) => {
|
|
7649
7761
|
if (!node) return false;
|
|
7650
|
-
if (["CmdSubst", "ProcSubst"].includes(
|
|
7762
|
+
if (["CmdSubst", "ProcSubst"].includes(node.kind)) {
|
|
7651
7763
|
statements(node.Stmts ?? [], clone(state), depth + 1);
|
|
7652
7764
|
return false;
|
|
7653
7765
|
}
|
|
@@ -8071,7 +8183,7 @@ function collectShellFileAccesses(command, context = {}) {
|
|
|
8071
8183
|
}
|
|
8072
8184
|
function readsScriptFromStdin(stmt) {
|
|
8073
8185
|
const cmd = stmt?.Cmd;
|
|
8074
|
-
if (!cmd ||
|
|
8186
|
+
if (!cmd || cmd.kind !== "CallExpr") return false;
|
|
8075
8187
|
const words = (cmd.Args ?? []).map((a) => resolveWordLiteral(a));
|
|
8076
8188
|
const head = unwrapCommandHead(words);
|
|
8077
8189
|
return SHELL_INTERPRETERS.has(base(words[head])) && shellConsumesPipe(words.slice(head + 1));
|
|
@@ -8085,7 +8197,7 @@ function collectShellFileAccesses(command, context = {}) {
|
|
|
8085
8197
|
}
|
|
8086
8198
|
function producedText(stmt) {
|
|
8087
8199
|
const cmd = stmt?.Cmd;
|
|
8088
|
-
if (!cmd ||
|
|
8200
|
+
if (!cmd || cmd.kind !== "CallExpr") return null;
|
|
8089
8201
|
const words = (cmd.Args ?? []).map((a) => resolveWordLiteral(a));
|
|
8090
8202
|
if (words.some((w) => w === null)) return null;
|
|
8091
8203
|
const head = unwrapCommandHead(words);
|
|
@@ -8104,7 +8216,7 @@ function collectShellFileAccesses(command, context = {}) {
|
|
|
8104
8216
|
if (!stmt || depth > MAX_DEPTH) return;
|
|
8105
8217
|
state.onSuccess = void 0;
|
|
8106
8218
|
if (stmt.Background) state = clone(state);
|
|
8107
|
-
const headWord = stmt.Cmd &&
|
|
8219
|
+
const headWord = stmt.Cmd && stmt.Cmd.kind === "CallExpr" ? base(resolveWordLiteral(stmt.Cmd.Args?.[0])) : "";
|
|
8108
8220
|
const sends = SENDING_STDIN.has(headWord);
|
|
8109
8221
|
for (const redir of stmt.Redirs ?? []) {
|
|
8110
8222
|
substitutions(redir.Word, state, depth);
|
|
@@ -8120,7 +8232,7 @@ function collectShellFileAccesses(command, context = {}) {
|
|
|
8120
8232
|
const cmd = stmt.Cmd;
|
|
8121
8233
|
if (!cmd) return;
|
|
8122
8234
|
stdinScript(stmt, state, depth);
|
|
8123
|
-
switch (
|
|
8235
|
+
switch (cmd.kind) {
|
|
8124
8236
|
case "CallExpr":
|
|
8125
8237
|
call(cmd, state, depth);
|
|
8126
8238
|
break;
|
|
@@ -8134,7 +8246,7 @@ function collectShellFileAccesses(command, context = {}) {
|
|
|
8134
8246
|
for (; ; ) {
|
|
8135
8247
|
segments2.unshift({ op: node.Op, part: node.Y });
|
|
8136
8248
|
const left = node.X;
|
|
8137
|
-
if (left?.Cmd &&
|
|
8249
|
+
if (left?.Cmd && left.Cmd.kind === "BinaryCmd" && !left.Redirs?.length && !left.Negated && !left.Background)
|
|
8138
8250
|
node = left.Cmd;
|
|
8139
8251
|
else {
|
|
8140
8252
|
segments2.unshift({ op: node.Op, part: left });
|
|
@@ -8409,7 +8521,6 @@ function evaluateProtectedFileAccess(accesses, context, settings) {
|
|
|
8409
8521
|
}
|
|
8410
8522
|
|
|
8411
8523
|
// src/shell/derived-writes.ts
|
|
8412
|
-
var { syntax: syntax3 } = import_mvdan_sh3.default;
|
|
8413
8524
|
var SEQUENCE_OPS = (() => {
|
|
8414
8525
|
const ops = /* @__PURE__ */ new Set();
|
|
8415
8526
|
for (const text of ["x && y", "x || y"]) {
|
|
@@ -8448,13 +8559,13 @@ function pipelineTexts(command) {
|
|
|
8448
8559
|
const visit = (stmt, depth) => {
|
|
8449
8560
|
if (!stmt || depth > 64) return;
|
|
8450
8561
|
const cmd = stmt.Cmd;
|
|
8451
|
-
if (cmd &&
|
|
8562
|
+
if (cmd && cmd.kind === "BinaryCmd" && SEQUENCE_OPS.has(cmd.Op)) {
|
|
8452
8563
|
visit(cmd.X, depth + 1);
|
|
8453
8564
|
visit(cmd.Y, depth + 1);
|
|
8454
8565
|
return;
|
|
8455
8566
|
}
|
|
8456
|
-
const s = at(stmt.
|
|
8457
|
-
const e = at(stmt.
|
|
8567
|
+
const s = at(stmt.pos);
|
|
8568
|
+
const e = at(stmt.end);
|
|
8458
8569
|
const text = s >= 0 && e > s ? command.slice(s, e).replace(/[\s;&]+$/, "") : "";
|
|
8459
8570
|
if (text) out.push(text);
|
|
8460
8571
|
};
|
|
@@ -12794,8 +12905,6 @@ function writtenTexts(args) {
|
|
|
12794
12905
|
}
|
|
12795
12906
|
|
|
12796
12907
|
// src/shell/write-source.ts
|
|
12797
|
-
var import_mvdan_sh4 = __toESM(require("mvdan-sh"));
|
|
12798
|
-
var { syntax: syntax4 } = import_mvdan_sh4.default;
|
|
12799
12908
|
var RDR_IN = (() => {
|
|
12800
12909
|
try {
|
|
12801
12910
|
return parseShared("x <y").Stmts[0].Redirs[0].Op;
|
|
@@ -12830,11 +12939,10 @@ function shellWriteUnseen(command, target) {
|
|
|
12830
12939
|
let unseen = false;
|
|
12831
12940
|
let seen = false;
|
|
12832
12941
|
try {
|
|
12833
|
-
|
|
12942
|
+
walk(f, (node) => {
|
|
12834
12943
|
if (!node || unseen) return false;
|
|
12835
12944
|
const n = node;
|
|
12836
|
-
if (
|
|
12837
|
-
return true;
|
|
12945
|
+
if (n.kind !== "Stmt" || !n.Cmd || n.Cmd.kind !== "CallExpr") return true;
|
|
12838
12946
|
const words = (n.Cmd.Args || []).map(
|
|
12839
12947
|
(a) => resolveWordLiteral(a)
|
|
12840
12948
|
);
|
|
@@ -12864,8 +12972,6 @@ function shellWriteUnseen(command, target) {
|
|
|
12864
12972
|
}
|
|
12865
12973
|
|
|
12866
12974
|
// src/shell/git.ts
|
|
12867
|
-
var import_mvdan_sh5 = __toESM(require("mvdan-sh"));
|
|
12868
|
-
var { syntax: syntax5 } = import_mvdan_sh5.default;
|
|
12869
12975
|
var GIT_PRESCREEN_RE = /git|husky/i;
|
|
12870
12976
|
var MAX_PAYLOAD_DEPTH2 = 8;
|
|
12871
12977
|
var MAX_ALIAS_DEPTH = 5;
|
|
@@ -12891,11 +12997,11 @@ function analyzeGit(command) {
|
|
|
12891
12997
|
}
|
|
12892
12998
|
function gitInvocations(command) {
|
|
12893
12999
|
const out = [];
|
|
12894
|
-
const
|
|
13000
|
+
const walk3 = new GitWalk(
|
|
12895
13001
|
(inv) => out.push(inv),
|
|
12896
13002
|
() => void 0
|
|
12897
13003
|
);
|
|
12898
|
-
|
|
13004
|
+
walk3.run(command, 0);
|
|
12899
13005
|
return out;
|
|
12900
13006
|
}
|
|
12901
13007
|
function analyzeGitImpl(command) {
|
|
@@ -12907,8 +13013,8 @@ function analyzeGitImpl(command) {
|
|
|
12907
13013
|
seen.add(key);
|
|
12908
13014
|
findings.push(f);
|
|
12909
13015
|
};
|
|
12910
|
-
const
|
|
12911
|
-
const parsed =
|
|
13016
|
+
const walk3 = new GitWalk((inv) => classify(inv).forEach(found), found);
|
|
13017
|
+
const parsed = walk3.run(command, 0);
|
|
12912
13018
|
return { parsed, findings };
|
|
12913
13019
|
}
|
|
12914
13020
|
var PAYLOAD_SHELLS = /* @__PURE__ */ new Set(["sh", "bash", "zsh", "dash", "ksh", "eval"]);
|
|
@@ -12974,7 +13080,7 @@ var GitWalk = class {
|
|
|
12974
13080
|
visit(node, depth) {
|
|
12975
13081
|
if (!node) return false;
|
|
12976
13082
|
const n = node;
|
|
12977
|
-
const type =
|
|
13083
|
+
const type = n.kind;
|
|
12978
13084
|
if (type === "Stmt") {
|
|
12979
13085
|
this.visitHeredocShell(n, depth);
|
|
12980
13086
|
return true;
|
|
@@ -12982,7 +13088,7 @@ var GitWalk = class {
|
|
|
12982
13088
|
if (type === "ForClause") {
|
|
12983
13089
|
const loop = n.Loop;
|
|
12984
13090
|
const name = loop?.Name?.Value;
|
|
12985
|
-
if (name &&
|
|
13091
|
+
if (name && loop.kind === "WordIter") {
|
|
12986
13092
|
const items = (loop.Items || []).map((w) => resolveWordLiteral(w));
|
|
12987
13093
|
const literal = items.length > 0 && items.every((v) => v !== null);
|
|
12988
13094
|
this.loopVars.set(name, literal ? items.slice(0, MAX_LOOP_ITEMS) : null);
|
|
@@ -13049,7 +13155,7 @@ var GitWalk = class {
|
|
|
13049
13155
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
13050
13156
|
visitHeredocShell(stmt, depth) {
|
|
13051
13157
|
const cmd = stmt.Cmd;
|
|
13052
|
-
if (!cmd ||
|
|
13158
|
+
if (!cmd || cmd.kind !== "CallExpr" || depth >= MAX_PAYLOAD_DEPTH2) return;
|
|
13053
13159
|
const redirs = stmt.Redirs || [];
|
|
13054
13160
|
const hdoc = redirs.find((r) => r?.Hdoc && (r.N == null || r.N.Value === "0"));
|
|
13055
13161
|
if (!hdoc) return;
|
|
@@ -13199,8 +13305,8 @@ var GitWalk = class {
|
|
|
13199
13305
|
var POSITIONAL2 = "\0positional";
|
|
13200
13306
|
function isPositionalParam(word) {
|
|
13201
13307
|
let parts = word?.Parts || [];
|
|
13202
|
-
if (parts.length === 1 &&
|
|
13203
|
-
if (parts.length !== 1 ||
|
|
13308
|
+
if (parts.length === 1 && parts[0].kind === "DblQuoted") parts = parts[0].Parts || [];
|
|
13309
|
+
if (parts.length !== 1 || parts[0].kind !== "ParamExp") return false;
|
|
13204
13310
|
return /^(\d+|@|\*)$/.test(parts[0].Param?.Value ?? "");
|
|
13205
13311
|
}
|
|
13206
13312
|
function boundPositionals() {
|
|
@@ -13214,11 +13320,11 @@ function boundPositionals() {
|
|
|
13214
13320
|
function plainParamName(word) {
|
|
13215
13321
|
let parts = word?.Parts || [];
|
|
13216
13322
|
let quoted = false;
|
|
13217
|
-
if (parts.length === 1 &&
|
|
13323
|
+
if (parts.length === 1 && parts[0].kind === "DblQuoted") {
|
|
13218
13324
|
parts = parts[0].Parts || [];
|
|
13219
13325
|
quoted = true;
|
|
13220
13326
|
}
|
|
13221
|
-
if (parts.length !== 1 ||
|
|
13327
|
+
if (parts.length !== 1 || parts[0].kind !== "ParamExp") return null;
|
|
13222
13328
|
const p = parts[0];
|
|
13223
13329
|
if (p.Excl || p.Length || p.Width || p.Index || p.Slice || p.Repl || p.Exp) return null;
|
|
13224
13330
|
const name = p.Param?.Value;
|
|
@@ -13243,7 +13349,7 @@ function resolveGitWords(word) {
|
|
|
13243
13349
|
}
|
|
13244
13350
|
const parts = word?.Parts || [];
|
|
13245
13351
|
const expansion = parts.some((p) => {
|
|
13246
|
-
const t =
|
|
13352
|
+
const t = p.kind;
|
|
13247
13353
|
return t === "ParamExp" || t === "CmdSubst";
|
|
13248
13354
|
});
|
|
13249
13355
|
return expansion ? splitFields(value) : [value];
|
|
@@ -13754,14 +13860,14 @@ function hookCommands(text) {
|
|
|
13754
13860
|
const root = parsed?.hooks;
|
|
13755
13861
|
if (!root || typeof root !== "object") return null;
|
|
13756
13862
|
const out = [];
|
|
13757
|
-
const
|
|
13863
|
+
const walk3 = (v, depth) => {
|
|
13758
13864
|
if (depth > 8 || !v || typeof v !== "object") return;
|
|
13759
13865
|
for (const [k, x] of Object.entries(v)) {
|
|
13760
13866
|
if (k === "command" && typeof x === "string") out.push(x);
|
|
13761
|
-
else
|
|
13867
|
+
else walk3(x, depth + 1);
|
|
13762
13868
|
}
|
|
13763
13869
|
};
|
|
13764
|
-
|
|
13870
|
+
walk3(root, 0);
|
|
13765
13871
|
return out;
|
|
13766
13872
|
}
|
|
13767
13873
|
function hooksDisarmed(text) {
|
|
@@ -13923,8 +14029,6 @@ function evaluateAgentFileWrite(args, accesses, context, settings, shell, hooks
|
|
|
13923
14029
|
}
|
|
13924
14030
|
|
|
13925
14031
|
// src/shell/outbound.ts
|
|
13926
|
-
var import_mvdan_sh6 = __toESM(require("mvdan-sh"));
|
|
13927
|
-
var { syntax: syntax6 } = import_mvdan_sh6.default;
|
|
13928
14032
|
var SHELLS = /* @__PURE__ */ new Set([...SHELL_INTERPRETERS, "mksh", "ash", "csh", "tcsh"]);
|
|
13929
14033
|
var NETCATS = /* @__PURE__ */ new Set(["nc", "ncat", "netcat", "nc.traditional", "nc.openbsd"]);
|
|
13930
14034
|
var NET_CLIENTS = /* @__PURE__ */ new Set([...NETCATS, "telnet", "socat"]);
|
|
@@ -13964,9 +14068,9 @@ function isDupInOp(op) {
|
|
|
13964
14068
|
if (dupInOp === null) {
|
|
13965
14069
|
let found = -1;
|
|
13966
14070
|
try {
|
|
13967
|
-
|
|
14071
|
+
walk(parseShared("cat <&3"), (node) => {
|
|
13968
14072
|
const n = node;
|
|
13969
|
-
if (found === -1 && n &&
|
|
14073
|
+
if (found === -1 && n && n.kind === "Stmt" && n.Redirs?.[0]) {
|
|
13970
14074
|
found = n.Redirs[0].Op;
|
|
13971
14075
|
}
|
|
13972
14076
|
return true;
|
|
@@ -13987,10 +14091,10 @@ function analyzeOutbound(command) {
|
|
|
13987
14091
|
return hit;
|
|
13988
14092
|
}
|
|
13989
14093
|
const findings = [];
|
|
13990
|
-
const
|
|
13991
|
-
const parsed =
|
|
14094
|
+
const walk3 = new OutboundWalk(findings);
|
|
14095
|
+
const parsed = walk3.run(command, 0);
|
|
13992
14096
|
const envOut = findings.find((f) => f.checkId === "data.env-exfil");
|
|
13993
|
-
if (envOut) envOut.destinations =
|
|
14097
|
+
if (envOut) envOut.destinations = walk3.envDestinations();
|
|
13994
14098
|
const computed = { parsed, findings };
|
|
13995
14099
|
if (cache2.size >= CACHE_MAX2) {
|
|
13996
14100
|
const oldest = cache2.keys().next().value;
|
|
@@ -14008,7 +14112,7 @@ function wordShape(word) {
|
|
|
14008
14112
|
const parts = word?.Parts || [];
|
|
14009
14113
|
let out = "";
|
|
14010
14114
|
for (const p of parts) {
|
|
14011
|
-
const t =
|
|
14115
|
+
const t = p.kind;
|
|
14012
14116
|
if (t === "Lit" || t === "SglQuoted") out += p.Value ?? "";
|
|
14013
14117
|
else if (t === "DblQuoted") out += wordShape(p);
|
|
14014
14118
|
else out += "X";
|
|
@@ -14164,7 +14268,7 @@ var OutboundWalk = class {
|
|
|
14164
14268
|
const f = parseShared(command);
|
|
14165
14269
|
if (!f || typeof f !== "object" || !f.Stmts) return false;
|
|
14166
14270
|
try {
|
|
14167
|
-
|
|
14271
|
+
walk(f, (node) => this.visit(node, depth));
|
|
14168
14272
|
} catch {
|
|
14169
14273
|
return false;
|
|
14170
14274
|
}
|
|
@@ -14203,7 +14307,7 @@ var OutboundWalk = class {
|
|
|
14203
14307
|
}
|
|
14204
14308
|
visit(n, depth) {
|
|
14205
14309
|
if (!n) return false;
|
|
14206
|
-
const type =
|
|
14310
|
+
const type = n.kind;
|
|
14207
14311
|
if (type === "Stmt") {
|
|
14208
14312
|
this.visitStmt(n, depth);
|
|
14209
14313
|
return true;
|
|
@@ -14223,7 +14327,7 @@ var OutboundWalk = class {
|
|
|
14223
14327
|
const redirs = redirectWords(stmt);
|
|
14224
14328
|
const toSocket = redirs.some((w) => w !== null && DEV_NET_RE.test(w));
|
|
14225
14329
|
if (!cmd) return;
|
|
14226
|
-
const type =
|
|
14330
|
+
const type = cmd.kind;
|
|
14227
14331
|
if (type !== "CallExpr" || wordsOf(cmd).length > 0) {
|
|
14228
14332
|
if (this.readsNetFd(stmt) && runsCommandsIn(stmt)) {
|
|
14229
14333
|
this.reverseShell("commands are read from a network socket opened with exec");
|
|
@@ -14309,7 +14413,7 @@ var OutboundWalk = class {
|
|
|
14309
14413
|
const stages = [];
|
|
14310
14414
|
const flatten = (s) => {
|
|
14311
14415
|
const c = s?.Cmd;
|
|
14312
|
-
if (c &&
|
|
14416
|
+
if (c && c.kind === "BinaryCmd" && isPipeOp(c.Op)) {
|
|
14313
14417
|
flatten(c.X);
|
|
14314
14418
|
flatten(c.Y);
|
|
14315
14419
|
} else stages.push(s);
|
|
@@ -14331,8 +14435,8 @@ var OutboundWalk = class {
|
|
|
14331
14435
|
sinks.set(i, socketHosts(st));
|
|
14332
14436
|
}
|
|
14333
14437
|
if (!c) return;
|
|
14334
|
-
if (
|
|
14335
|
-
if (
|
|
14438
|
+
if (c.kind === "DeclClause" && declDumpsEnv(c) && dump < 0) dump = i;
|
|
14439
|
+
if (c.kind !== "CallExpr") {
|
|
14336
14440
|
if (runsCommandsIn(st)) shell = i;
|
|
14337
14441
|
return;
|
|
14338
14442
|
}
|
|
@@ -14373,10 +14477,10 @@ var OutboundWalk = class {
|
|
|
14373
14477
|
/** A word (or assignment value) whose text includes an env dump's output. */
|
|
14374
14478
|
carriesEnv(word) {
|
|
14375
14479
|
let hit = false;
|
|
14376
|
-
|
|
14480
|
+
walk(word, (node) => {
|
|
14377
14481
|
if (hit || !node) return false;
|
|
14378
14482
|
const n = node;
|
|
14379
|
-
const t =
|
|
14483
|
+
const t = n.kind;
|
|
14380
14484
|
if (t === "ParamExp" && n.Param?.Value && this.envVars.has(n.Param.Value)) {
|
|
14381
14485
|
hit = true;
|
|
14382
14486
|
return false;
|
|
@@ -14393,11 +14497,11 @@ var OutboundWalk = class {
|
|
|
14393
14497
|
stmtsDumpEnv(stmts) {
|
|
14394
14498
|
let hit = false;
|
|
14395
14499
|
for (const s of stmts) {
|
|
14396
|
-
|
|
14500
|
+
walk(s, (node) => {
|
|
14397
14501
|
if (hit || !node) return false;
|
|
14398
14502
|
const n = node;
|
|
14399
|
-
const t =
|
|
14400
|
-
if (t === "Stmt" && n.Cmd &&
|
|
14503
|
+
const t = n.kind;
|
|
14504
|
+
if (t === "Stmt" && n.Cmd && n.Cmd.kind === "CallExpr") {
|
|
14401
14505
|
const words = wordsOf(n.Cmd);
|
|
14402
14506
|
if (words.length > 0 && dumpsEnv(words, n)) hit = true;
|
|
14403
14507
|
}
|
|
@@ -14416,16 +14520,16 @@ var OutboundWalk = class {
|
|
|
14416
14520
|
};
|
|
14417
14521
|
function paramName(word) {
|
|
14418
14522
|
let parts = word?.Parts || [];
|
|
14419
|
-
if (parts.length === 1 &&
|
|
14420
|
-
if (parts.length !== 1 ||
|
|
14523
|
+
if (parts.length === 1 && parts[0].kind === "DblQuoted") parts = parts[0].Parts || [];
|
|
14524
|
+
if (parts.length !== 1 || parts[0].kind !== "ParamExp") return null;
|
|
14421
14525
|
return parts[0].Param?.Value ?? null;
|
|
14422
14526
|
}
|
|
14423
14527
|
function refersTo(word, names) {
|
|
14424
14528
|
let hit = false;
|
|
14425
|
-
|
|
14529
|
+
walk(word, (node) => {
|
|
14426
14530
|
if (hit || !node) return false;
|
|
14427
14531
|
const n = node;
|
|
14428
|
-
if (
|
|
14532
|
+
if (n.kind === "ParamExp" && names.has(n.Param?.Value)) hit = true;
|
|
14429
14533
|
return !hit;
|
|
14430
14534
|
});
|
|
14431
14535
|
return hit;
|
|
@@ -14433,10 +14537,10 @@ function refersTo(word, names) {
|
|
|
14433
14537
|
function runsCommandsIn(stmt) {
|
|
14434
14538
|
const readVars = /* @__PURE__ */ new Set();
|
|
14435
14539
|
let hit = false;
|
|
14436
|
-
|
|
14540
|
+
walk(stmt, (node) => {
|
|
14437
14541
|
if (hit || !node) return false;
|
|
14438
14542
|
const n = node;
|
|
14439
|
-
if (
|
|
14543
|
+
if (n.kind !== "CallExpr") return true;
|
|
14440
14544
|
const words = wordsOf(n);
|
|
14441
14545
|
if (words.length === 0) return true;
|
|
14442
14546
|
const { head, at } = headOf(words);
|
|
@@ -14457,12 +14561,7 @@ function runsCommandsIn(stmt) {
|
|
|
14457
14561
|
return hit;
|
|
14458
14562
|
}
|
|
14459
14563
|
|
|
14460
|
-
// src/shell/package-registry.ts
|
|
14461
|
-
var import_mvdan_sh8 = __toESM(require("mvdan-sh"));
|
|
14462
|
-
|
|
14463
14564
|
// src/shell/package-install.ts
|
|
14464
|
-
var import_mvdan_sh7 = __toESM(require("mvdan-sh"));
|
|
14465
|
-
var { syntax: syntax7 } = import_mvdan_sh7.default;
|
|
14466
14565
|
var NPM_INSTALL_VERBS = /* @__PURE__ */ new Set([
|
|
14467
14566
|
"install",
|
|
14468
14567
|
"i",
|
|
@@ -15401,7 +15500,7 @@ function localContexts(file) {
|
|
|
15401
15500
|
if (depth > 64) return null;
|
|
15402
15501
|
if (!stmt || stmt.Background || stmt.Negated || stmt.Redirs?.length) return null;
|
|
15403
15502
|
const cmd = stmt.Cmd;
|
|
15404
|
-
const kind =
|
|
15503
|
+
const kind = cmd.kind;
|
|
15405
15504
|
if (kind === "BinaryCmd" && cmd.Op === andOp) {
|
|
15406
15505
|
const next = visit(cmd.X, steps, depth + 1);
|
|
15407
15506
|
return next === null ? null : visit(cmd.Y, next, depth + 1);
|
|
@@ -15417,7 +15516,7 @@ function localContexts(file) {
|
|
|
15417
15516
|
)
|
|
15418
15517
|
))
|
|
15419
15518
|
return null;
|
|
15420
|
-
contexts.set(cmd.
|
|
15519
|
+
contexts.set(cmd.pos, steps);
|
|
15421
15520
|
return null;
|
|
15422
15521
|
}
|
|
15423
15522
|
if (file.Stmts?.length === 1) visit(file.Stmts[0], []);
|
|
@@ -15428,12 +15527,12 @@ function collect(command, out, depth) {
|
|
|
15428
15527
|
const f = parseShared(command);
|
|
15429
15528
|
if (typeof f === "symbol") return;
|
|
15430
15529
|
const contexts = depth === 0 ? localContexts(f) : /* @__PURE__ */ new Map();
|
|
15431
|
-
|
|
15432
|
-
if (node &&
|
|
15530
|
+
walk(f, (node) => {
|
|
15531
|
+
if (node && node.kind === "CallExpr") {
|
|
15433
15532
|
const words = (node.Args || []).map((a) => resolveWordLiteral(a));
|
|
15434
15533
|
const start = out.length;
|
|
15435
15534
|
if (words.length > 0) fromCall(words, out, depth);
|
|
15436
|
-
const steps = contexts.get(node.
|
|
15535
|
+
const steps = contexts.get(node.pos);
|
|
15437
15536
|
for (let i = start; i < out.length; i++) {
|
|
15438
15537
|
if (["npx", "bunx", "npm exec"].includes(out[i].manager)) {
|
|
15439
15538
|
if (!steps) out[i].localCwd = null;
|
|
@@ -15457,7 +15556,6 @@ function extractPackageInstalls(command) {
|
|
|
15457
15556
|
}
|
|
15458
15557
|
|
|
15459
15558
|
// src/shell/package-registry.ts
|
|
15460
|
-
var { syntax: syntax8 } = import_mvdan_sh8.default;
|
|
15461
15559
|
var NPM_FAMILY = /* @__PURE__ */ new Set(["npm", "pnpm", "yarn", "bun", "npx", "pnpx", "bunx"]);
|
|
15462
15560
|
var PIP_FAMILY = /* @__PURE__ */ new Set(["pip", "pip3", "uv", "uvx", "pipx", "pipenv"]);
|
|
15463
15561
|
var NPM_FETCH_VERBS = /* @__PURE__ */ new Set([
|
|
@@ -15548,7 +15646,7 @@ function collect2(command, out, depth) {
|
|
|
15548
15646
|
try {
|
|
15549
15647
|
walkCommandScope(command, (node) => {
|
|
15550
15648
|
const n = node;
|
|
15551
|
-
if (!n ||
|
|
15649
|
+
if (!n || n.kind !== "CallExpr") return true;
|
|
15552
15650
|
const words = (n.Args || []).map((a) => resolveWordLiteral(a));
|
|
15553
15651
|
if (words.length === 0) return true;
|
|
15554
15652
|
const prefix = (n.Assigns || []).filter((a) => a?.Name?.Value).map(
|
|
@@ -17335,12 +17433,12 @@ function extractContentDestinations(text) {
|
|
|
17335
17433
|
function extractArgDestinations(args) {
|
|
17336
17434
|
const out = /* @__PURE__ */ new Set();
|
|
17337
17435
|
try {
|
|
17338
|
-
|
|
17436
|
+
walk2(args, void 0, out, 0);
|
|
17339
17437
|
} catch {
|
|
17340
17438
|
}
|
|
17341
17439
|
return [...out];
|
|
17342
17440
|
}
|
|
17343
|
-
function
|
|
17441
|
+
function walk2(value, key, out, depth) {
|
|
17344
17442
|
if (out.size >= MAX_DESTINATIONS || depth > 12) return;
|
|
17345
17443
|
if (typeof value === "string") {
|
|
17346
17444
|
const s = value.length > MAX_SCAN_CHARS ? value.slice(0, MAX_SCAN_CHARS) : value;
|
|
@@ -17359,12 +17457,12 @@ function walk(value, key, out, depth) {
|
|
|
17359
17457
|
return;
|
|
17360
17458
|
}
|
|
17361
17459
|
if (Array.isArray(value)) {
|
|
17362
|
-
for (const v of value)
|
|
17460
|
+
for (const v of value) walk2(v, key, out, depth + 1);
|
|
17363
17461
|
return;
|
|
17364
17462
|
}
|
|
17365
17463
|
if (value && typeof value === "object") {
|
|
17366
17464
|
for (const [k, v] of Object.entries(value))
|
|
17367
|
-
|
|
17465
|
+
walk2(v, k, out, depth + 1);
|
|
17368
17466
|
}
|
|
17369
17467
|
}
|
|
17370
17468
|
function destinationDomain(dest) {
|
|
@@ -17875,8 +17973,6 @@ function maskLeaf(value, key, depth) {
|
|
|
17875
17973
|
}
|
|
17876
17974
|
|
|
17877
17975
|
// src/exposure/shell.ts
|
|
17878
|
-
var import_mvdan_sh9 = __toESM(require("mvdan-sh"));
|
|
17879
|
-
var { syntax: syntax9 } = import_mvdan_sh9.default;
|
|
17880
17976
|
var EMPTY = {
|
|
17881
17977
|
fetches: false,
|
|
17882
17978
|
sends: false,
|
|
@@ -18290,9 +18386,9 @@ function classifyCall(head, args, prefixes, env, state, out) {
|
|
|
18290
18386
|
function walkCalls(command, out, depth, state) {
|
|
18291
18387
|
const f = parseShared(command);
|
|
18292
18388
|
if (typeof f === "symbol" || !f) return;
|
|
18293
|
-
|
|
18389
|
+
walk(f, (node) => {
|
|
18294
18390
|
if (!node) return false;
|
|
18295
|
-
const type =
|
|
18391
|
+
const type = node.kind;
|
|
18296
18392
|
if (type === "DeclClause") {
|
|
18297
18393
|
for (const a of node.Args || []) if (a?.Name?.Value) state.env.add(a.Name.Value);
|
|
18298
18394
|
return true;
|
|
@@ -18870,7 +18966,7 @@ function matchCanaryArgs(args, values) {
|
|
|
18870
18966
|
if (values.length === 0) return null;
|
|
18871
18967
|
const needles = prepare(values);
|
|
18872
18968
|
if (needles.length === 0) return null;
|
|
18873
|
-
const
|
|
18969
|
+
const walk3 = (v, depth, fieldPath) => {
|
|
18874
18970
|
if (depth > MAX_DEPTH4) return null;
|
|
18875
18971
|
if (typeof v === "string") {
|
|
18876
18972
|
const hit = matchPrepared(v, needles);
|
|
@@ -18879,31 +18975,31 @@ function matchCanaryArgs(args, values) {
|
|
|
18879
18975
|
const trimmed = v.trim();
|
|
18880
18976
|
if (trimmed.startsWith("{") || trimmed.startsWith("[")) {
|
|
18881
18977
|
try {
|
|
18882
|
-
return
|
|
18978
|
+
return walk3(JSON.parse(v), depth + 1, fieldPath);
|
|
18883
18979
|
} catch {
|
|
18884
18980
|
}
|
|
18885
18981
|
}
|
|
18886
18982
|
}
|
|
18887
18983
|
return null;
|
|
18888
18984
|
}
|
|
18889
|
-
if (typeof v === "number" && Number.isFinite(v)) return
|
|
18985
|
+
if (typeof v === "number" && Number.isFinite(v)) return walk3(String(v), depth, fieldPath);
|
|
18890
18986
|
if (Array.isArray(v)) {
|
|
18891
18987
|
for (let i = 0; i < v.length; i++) {
|
|
18892
|
-
const h =
|
|
18988
|
+
const h = walk3(v[i], depth + 1, `${fieldPath}[${i}]`);
|
|
18893
18989
|
if (h) return h;
|
|
18894
18990
|
}
|
|
18895
18991
|
return null;
|
|
18896
18992
|
}
|
|
18897
18993
|
if (v && typeof v === "object") {
|
|
18898
18994
|
for (const [k, child] of Object.entries(v)) {
|
|
18899
|
-
const h =
|
|
18995
|
+
const h = walk3(child, depth + 1, fieldPath ? `${fieldPath}.${k}` : k);
|
|
18900
18996
|
if (h) return h;
|
|
18901
18997
|
}
|
|
18902
18998
|
}
|
|
18903
18999
|
return null;
|
|
18904
19000
|
};
|
|
18905
19001
|
try {
|
|
18906
|
-
return
|
|
19002
|
+
return walk3(args, 0, "");
|
|
18907
19003
|
} catch (e) {
|
|
18908
19004
|
console.error(
|
|
18909
19005
|
"[node9 engine] canary args walk failed:",
|
|
@@ -18916,7 +19012,7 @@ function matchCanaryArgs(args, values) {
|
|
|
18916
19012
|
// src/scan/canonical.ts
|
|
18917
19013
|
var LONG_OUTPUT_THRESHOLD_BYTES = 100 * 1024;
|
|
18918
19014
|
var CANONICAL_EXTRACTOR_VERSION = "canonical-v35";
|
|
18919
|
-
var CANONICAL_EXTRACTOR_HASH = "
|
|
19015
|
+
var CANONICAL_EXTRACTOR_HASH = "dfb2d85278a146ad";
|
|
18920
19016
|
var DEDUPE_PREVIEW_LEN = 120;
|
|
18921
19017
|
function extractCanonicalFindings(call, ctx) {
|
|
18922
19018
|
const out = [];
|