@clear-capabilities/agentic-security-scanner 0.118.2 → 0.119.2
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/CHANGELOG.md +274 -0
- package/bin/agentic-security.js +78 -1
- package/dist/178.index.js +1 -1
- package/dist/384.index.js +1 -1
- package/dist/415.index.js +90 -0
- package/dist/444.index.js +89 -0
- package/dist/503.index.js +288 -0
- package/dist/637.index.js +15 -1
- package/dist/838.index.js +1 -1
- package/dist/985.index.js +5 -1
- package/dist/agentic-security.mjs +18 -18
- package/dist/agentic-security.mjs.sha256 +1 -1
- package/package.json +13 -8
- package/src/dataflow/CLAUDE.md +1 -1
- package/src/dataflow/catalog.js +50 -11
- package/src/dataflow/engine.js +76 -1
- package/src/dataflow/implicit-flow.js +4 -2
- package/src/dataflow/index.js +9 -4
- package/src/dataflow/proof-gate.js +5 -0
- package/src/dataflow/summaries.js +19 -1
- package/src/engine.js +95 -7
- package/src/ir/parser-go.js +8 -0
- package/src/mcp/tools.js +6 -2
- package/src/posture/fix-history.js +24 -0
- package/src/posture/provenance.js +39 -0
- package/src/posture/sca-verdict.js +130 -0
- package/src/posture/secret-history.js +72 -0
- package/src/pr-delta.js +13 -0
- package/src/report/index.js +50 -0
- package/src/sast/agent-untrusted-flow.js +107 -0
- package/src/sast/api-authz.js +71 -0
- package/src/sast/cross-service.js +98 -0
- package/src/sast/event-entrypoint.js +90 -0
- package/src/sast/iac-terraform.js +107 -0
- package/src/sast/rbac-consistency.js +113 -0
- package/src/sast/wrong-context-sanitizer.js +55 -0
- package/src/sca/image-packages.js +74 -0
- package/src/sca/import-reachability.js +271 -0
- package/src/sca/install-script-analysis.js +82 -0
- package/bin/.agentic-security/findings.json +0 -1907
- package/bin/.agentic-security/last-scan.json +0 -1907
- package/bin/.agentic-security/last-scan.json.sig +0 -1
- package/bin/.agentic-security/scan-history.json +0 -166
- package/bin/.agentic-security/streak.json +0 -20
- package/src/.agentic-security/dpia.md +0 -26
- package/src/.agentic-security/findings.json +0 -117732
- package/src/.agentic-security/last-scan.json +0 -117732
- package/src/.agentic-security/last-scan.json.sig +0 -1
- package/src/.agentic-security/pqc-migration-plan.json +0 -80
- package/src/.agentic-security/pqc-migration-plan.md +0 -32
- package/src/.agentic-security/sbom-history/0775d96ba4cf814520d02760cfb3010760c7684f.json +0 -6
- package/src/.agentic-security/sbom-history/29300441d3460832137a8e51a92b74068cad2f15.json +0 -6
- package/src/.agentic-security/sbom-history/3852ad2aa0e50951bed0c930c29d1861aa13d048.json +0 -6
- package/src/.agentic-security/sbom-history/3d96e2daec97e3226c9679bac6e64a89ca10bfe9.json +0 -6
- package/src/.agentic-security/sbom-history/5f0b136622fce09b469a1a6137c04d5b18652ea4.json +0 -6
- package/src/.agentic-security/sbom-history/64e30595d447b62344d0d9200760269df2095332.json +0 -6
- package/src/.agentic-security/sbom-history/71f3241902c9e7437764e3caa046ba8a3bcdd32a.json +0 -6
- package/src/.agentic-security/sbom-history/99c372cdc33ecc9dde92960b4ffadf7775860e61.json +0 -6
- package/src/.agentic-security/sbom-history/9c93b15e8cf3ff2ddb8cdae92371b82e178b463c.json +0 -6
- package/src/.agentic-security/sbom-history/a3544abf1863d71776b61302ca74d9ead2b26a9f.json +0 -6
- package/src/.agentic-security/sbom-history/dca15abdec43c508aa342db86dc5db8d7f01fbbe.json +0 -6
- package/src/.agentic-security/scan-history.json +0 -12946
- package/src/.agentic-security/streak.json +0 -21
- package/src/.agentic-security/threat-model.json +0 -3702
- package/src/.agentic-security/threat-model.md +0 -118
- package/src/dataflow/.agentic-security/dpia.md +0 -26
- package/src/dataflow/.agentic-security/findings.json +0 -6086
- package/src/dataflow/.agentic-security/last-scan.json +0 -6086
- package/src/dataflow/.agentic-security/last-scan.json.sig +0 -1
- package/src/dataflow/.agentic-security/sbom-history/15d7c4d20f9d968ebec29445c9e9b3fc6f74e6a1.json +0 -6
- package/src/dataflow/.agentic-security/sbom-history/3852ad2aa0e50951bed0c930c29d1861aa13d048.json +0 -6
- package/src/dataflow/.agentic-security/sbom-history/73166872888d9ddcbe98645af818ecc7d8917b07.json +0 -6
- package/src/dataflow/.agentic-security/sbom-history/8869fe64035bec0152d92ece961e9369d8637076.json +0 -6
- package/src/dataflow/.agentic-security/sbom-history/9c93b15e8cf3ff2ddb8cdae92371b82e178b463c.json +0 -6
- package/src/dataflow/.agentic-security/sbom-history/b7ac2dada4a9216ccfe8969fc57f988c83f46975.json +0 -6
- package/src/dataflow/.agentic-security/scan-history.json +0 -250
- package/src/dataflow/.agentic-security/streak.json +0 -21
- package/src/dataflow/.agentic-security/threat-model.json +0 -161
- package/src/dataflow/.agentic-security/threat-model.md +0 -43
- package/src/ir/.agentic-security/dpia.md +0 -26
- package/src/ir/.agentic-security/exploit-bundles.json +0 -77
- package/src/ir/.agentic-security/findings.json +0 -4011
- package/src/ir/.agentic-security/last-scan.json +0 -4011
- package/src/ir/.agentic-security/last-scan.json.sig +0 -1
- package/src/ir/.agentic-security/sbom-history/73166872888d9ddcbe98645af818ecc7d8917b07.json +0 -6
- package/src/ir/.agentic-security/sbom-history/b5f468a158f6a145aedd8680841c106b9809f16b.json +0 -6
- package/src/ir/.agentic-security/scan-history.json +0 -193
- package/src/ir/.agentic-security/streak.json +0 -20
- package/src/ir/.agentic-security/threat-model.json +0 -143
- package/src/ir/.agentic-security/threat-model.md +0 -39
- package/src/mcp/.agentic-security/findings.json +0 -8632
- package/src/mcp/.agentic-security/last-scan.json +0 -8632
- package/src/mcp/.agentic-security/last-scan.json.sig +0 -1
- package/src/mcp/.agentic-security/scan-history.json +0 -331
- package/src/mcp/.agentic-security/streak.json +0 -20
- package/src/posture/.agentic-security/dpia.md +0 -26
- package/src/posture/.agentic-security/findings.json +0 -77181
- package/src/posture/.agentic-security/last-scan.json +0 -77181
- package/src/posture/.agentic-security/last-scan.json.sig +0 -1
- package/src/posture/.agentic-security/pqc-migration-plan.json +0 -65
- package/src/posture/.agentic-security/pqc-migration-plan.md +0 -30
- package/src/posture/.agentic-security/sbom-history/0724c133d2943c87555695b5b2adcfcfc3373a2d.json +0 -6
- package/src/posture/.agentic-security/sbom-history/7d45b5e03804aac084b4a2b4dc8c6f10107d2005.json +0 -6
- package/src/posture/.agentic-security/sbom-history/99c372cdc33ecc9dde92960b4ffadf7775860e61.json +0 -6
- package/src/posture/.agentic-security/sbom-history/b7ac2dada4a9216ccfe8969fc57f988c83f46975.json +0 -6
- package/src/posture/.agentic-security/scan-history.json +0 -8904
- package/src/posture/.agentic-security/streak.json +0 -21
- package/src/posture/.agentic-security/threat-model.json +0 -2038
- package/src/posture/.agentic-security/threat-model.md +0 -73
- package/src/posture/compliance-frameworks/.agentic-security/findings.json +0 -80
- package/src/posture/compliance-frameworks/.agentic-security/last-scan.json +0 -80
- package/src/posture/compliance-frameworks/.agentic-security/last-scan.json.sig +0 -1
- package/src/posture/compliance-frameworks/.agentic-security/scan-history.json +0 -90
- package/src/posture/compliance-frameworks/.agentic-security/streak.json +0 -22
- package/src/report/.agentic-security/dpia.md +0 -26
- package/src/report/.agentic-security/sbom-history/7d45b5e03804aac084b4a2b4dc8c6f10107d2005.json +0 -6
- package/src/report/.agentic-security/sbom-history/f94be839aa6a1011f53da016416a561698b8f338.json +0 -6
- package/src/report/.agentic-security/threat-model.json +0 -7
- package/src/report/.agentic-security/threat-model.md +0 -22
- package/src/sast/.agentic-security/dpia.md +0 -26
- package/src/sast/.agentic-security/findings.json +0 -6154
- package/src/sast/.agentic-security/last-scan.json +0 -6154
- package/src/sast/.agentic-security/last-scan.json.sig +0 -1
- package/src/sast/.agentic-security/pqc-migration-plan.json +0 -65
- package/src/sast/.agentic-security/pqc-migration-plan.md +0 -30
- package/src/sast/.agentic-security/sbom-history/043a7b401c68f5a298c83d4c42e94f118ff2a89a.json +0 -6
- package/src/sast/.agentic-security/sbom-history/0775d96ba4cf814520d02760cfb3010760c7684f.json +0 -6
- package/src/sast/.agentic-security/sbom-history/20d77ad7363a82777d506dedbbb1ecfed52f094a.json +0 -6
- package/src/sast/.agentic-security/sbom-history/29300441d3460832137a8e51a92b74068cad2f15.json +0 -6
- package/src/sast/.agentic-security/sbom-history/3852ad2aa0e50951bed0c930c29d1861aa13d048.json +0 -6
- package/src/sast/.agentic-security/sbom-history/3c6b3205f04e26f7654943d465281d00e0fa395e.json +0 -6
- package/src/sast/.agentic-security/sbom-history/3d96e2daec97e3226c9679bac6e64a89ca10bfe9.json +0 -6
- package/src/sast/.agentic-security/sbom-history/5282502e090013219eb006f7e02d8c983bc29c8e.json +0 -6
- package/src/sast/.agentic-security/sbom-history/5f0b136622fce09b469a1a6137c04d5b18652ea4.json +0 -6
- package/src/sast/.agentic-security/sbom-history/64e30595d447b62344d0d9200760269df2095332.json +0 -6
- package/src/sast/.agentic-security/sbom-history/71f3241902c9e7437764e3caa046ba8a3bcdd32a.json +0 -6
- package/src/sast/.agentic-security/sbom-history/73514b485d4770f3c5ee57d87338bccd1c61d8b7.json +0 -6
- package/src/sast/.agentic-security/sbom-history/96fa584fc3134512237d58aac63d6f3778b0cbcb.json +0 -6
- package/src/sast/.agentic-security/sbom-history/99c372cdc33ecc9dde92960b4ffadf7775860e61.json +0 -6
- package/src/sast/.agentic-security/sbom-history/a3544abf1863d71776b61302ca74d9ead2b26a9f.json +0 -6
- package/src/sast/.agentic-security/sbom-history/a4a34ab8b3d758de2adc8c3dc3e42d4cb0db00a3.json +0 -6
- package/src/sast/.agentic-security/sbom-history/ac236ec3602cfc8fb133f389ec3687e409084673.json +0 -6
- package/src/sast/.agentic-security/sbom-history/b57b3e1292f1f9f0de6c5a34a436e330c78008a3.json +0 -6
- package/src/sast/.agentic-security/sbom-history/dca15abdec43c508aa342db86dc5db8d7f01fbbe.json +0 -6
- package/src/sast/.agentic-security/sbom-history/dcbfd4935922dd165093c08a1003c338eed937c5.json +0 -6
- package/src/sast/.agentic-security/sbom-history/fa017f725615b0293814344c8ef7dfeb06775ca0.json +0 -6
- package/src/sast/.agentic-security/scan-history.json +0 -941
- package/src/sast/.agentic-security/streak.json +0 -22
- package/src/sast/.agentic-security/threat-model.json +0 -816
- package/src/sast/.agentic-security/threat-model.md +0 -91
- package/src/sca/.agentic-security/findings.json +0 -7460
- package/src/sca/.agentic-security/last-scan.json +0 -7460
- package/src/sca/.agentic-security/last-scan.json.sig +0 -1
- package/src/sca/.agentic-security/scan-history.json +0 -113
- package/src/sca/.agentic-security/streak.json +0 -21
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
// R7 — import-aware SCA function-reachability (JS/TS + Python).
|
|
2
|
+
//
|
|
3
|
+
// The default reachability pass (engine.js#markUsedVulnFunctions) is a regex
|
|
4
|
+
// over `pkg.fn` / bare function names. It has two documented failure modes:
|
|
5
|
+
// - FALSE NEGATIVE on aliased / namespace imports:
|
|
6
|
+
// import { merge as deepMerge } from 'lodash'; deepMerge(a, b) // missed
|
|
7
|
+
// import * as _ from 'lodash'; _.merge(a, b) // member ok
|
|
8
|
+
// - FALSE POSITIVE on coincidental names: a bare `parse(` in a file that never
|
|
9
|
+
// imports the vulnerable package is matched anyway.
|
|
10
|
+
//
|
|
11
|
+
// This module parses the import statements of each JS/TS and Python file into an
|
|
12
|
+
// alias map, then finds call sites of a package's vulnerable functions resolved
|
|
13
|
+
// THROUGH that map and GATED to files that actually import the package. It does
|
|
14
|
+
// not decide `functionReachable` itself — it augments `vulnerableFunctionCallSites`
|
|
15
|
+
// so the existing route-reachability classifier (which distinguishes
|
|
16
|
+
// reachable / unreachable / unknown) sees the calls the regex missed.
|
|
17
|
+
//
|
|
18
|
+
// Other ecosystems keep the regex pass (this module is a no-op for them).
|
|
19
|
+
|
|
20
|
+
import { blankComments } from '../sast/_comment-strip.js';
|
|
21
|
+
|
|
22
|
+
const JS_RE = /\.(?:m|c)?[jt]sx?$/i;
|
|
23
|
+
const PY_RE = /\.py$/i;
|
|
24
|
+
|
|
25
|
+
export function languageOfFile(file) {
|
|
26
|
+
if (JS_RE.test(file)) return 'js';
|
|
27
|
+
if (PY_RE.test(file)) return 'py';
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Normalize an import source to a package base name.
|
|
32
|
+
// '@scope/pkg/sub' -> '@scope/pkg' 'pkg/sub' -> 'pkg' (npm)
|
|
33
|
+
// 'pkg.sub.mod' -> 'pkg' (python)
|
|
34
|
+
function pkgBase(source, lang) {
|
|
35
|
+
let s = String(source || '').trim();
|
|
36
|
+
if (lang === 'py') return s.split('.')[0];
|
|
37
|
+
if (s.startsWith('@')) {
|
|
38
|
+
const parts = s.split('/');
|
|
39
|
+
return parts.slice(0, 2).join('/');
|
|
40
|
+
}
|
|
41
|
+
return s.split('/')[0];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Loose package-name equivalence. npm is case-sensitive; PyPI dist names differ
|
|
45
|
+
// from import names by case and -/_ (PyYAML→yaml is NOT covered — that needs a
|
|
46
|
+
// dist→import table; documented limitation).
|
|
47
|
+
export function pkgMatches(depName, importedPkg, lang) {
|
|
48
|
+
if (!depName || !importedPkg) return false;
|
|
49
|
+
if (depName === importedPkg) return true;
|
|
50
|
+
if (lang === 'py') {
|
|
51
|
+
const norm = (x) => x.toLowerCase().replace(/[-_.]/g, '');
|
|
52
|
+
return norm(depName) === norm(importedPkg);
|
|
53
|
+
}
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Split a named-imports clause body ("a, b as c, default as d") into pairs.
|
|
58
|
+
function parseNamedClause(body) {
|
|
59
|
+
const out = [];
|
|
60
|
+
for (const raw of body.split(',')) {
|
|
61
|
+
const t = raw.trim();
|
|
62
|
+
if (!t) continue;
|
|
63
|
+
const m = t.match(/^([A-Za-z_$][\w$]*)\s+as\s+([A-Za-z_$][\w$]*)$/);
|
|
64
|
+
if (m) out.push({ imported: m[1], local: m[2] });
|
|
65
|
+
else if (/^[A-Za-z_$][\w$]*$/.test(t)) out.push({ imported: t, local: t });
|
|
66
|
+
}
|
|
67
|
+
return out;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Parse JS/TS imports into:
|
|
72
|
+
* named: Map<localName, { pkg, imported }>
|
|
73
|
+
* ns: Map<localName, pkg> (namespace/default whole-module binding)
|
|
74
|
+
* packages: Set<pkg>
|
|
75
|
+
* Handles ESM (named / default / namespace) and CJS require (destructure / whole).
|
|
76
|
+
*/
|
|
77
|
+
export function extractJsImports(content) {
|
|
78
|
+
const code = blankComments(content);
|
|
79
|
+
const named = new Map();
|
|
80
|
+
const ns = new Map();
|
|
81
|
+
const packages = new Set();
|
|
82
|
+
const add = (pkg) => { if (pkg) packages.add(pkg); };
|
|
83
|
+
|
|
84
|
+
// ESM: import ... from 'pkg'
|
|
85
|
+
const esm = /\bimport\s+([^;'"]+?)\s+from\s*['"]([^'"]+)['"]/g;
|
|
86
|
+
let m;
|
|
87
|
+
while ((m = esm.exec(code))) {
|
|
88
|
+
const clause = m[1].trim();
|
|
89
|
+
const pkg = pkgBase(m[2], 'js');
|
|
90
|
+
add(pkg);
|
|
91
|
+
// namespace: * as ns
|
|
92
|
+
const nsM = clause.match(/\*\s+as\s+([A-Za-z_$][\w$]*)/);
|
|
93
|
+
if (nsM) ns.set(nsM[1], pkg);
|
|
94
|
+
// named: { ... }
|
|
95
|
+
const braceM = clause.match(/\{([^}]*)\}/);
|
|
96
|
+
if (braceM) for (const p of parseNamedClause(braceM[1])) named.set(p.local, { pkg, imported: p.imported });
|
|
97
|
+
// default: leading bare identifier (before any { or *)
|
|
98
|
+
const defM = clause.match(/^([A-Za-z_$][\w$]*)\s*(?:,|$)/);
|
|
99
|
+
if (defM && !nsM) ns.set(defM[1], pkg);
|
|
100
|
+
}
|
|
101
|
+
// CJS: const X = require('pkg') / const { a, b: c } = require('pkg')
|
|
102
|
+
const cjs = /(?:const|let|var)\s+(\{[^}]*\}|[A-Za-z_$][\w$]*)\s*=\s*require\s*\(\s*['"]([^'"]+)['"]\s*\)/g;
|
|
103
|
+
while ((m = cjs.exec(code))) {
|
|
104
|
+
const bind = m[1].trim();
|
|
105
|
+
const pkg = pkgBase(m[2], 'js');
|
|
106
|
+
add(pkg);
|
|
107
|
+
if (bind.startsWith('{')) {
|
|
108
|
+
// require destructure uses `orig: local`
|
|
109
|
+
for (const raw of bind.slice(1, -1).split(',')) {
|
|
110
|
+
const t = raw.trim(); if (!t) continue;
|
|
111
|
+
const mm = t.match(/^([A-Za-z_$][\w$]*)\s*:\s*([A-Za-z_$][\w$]*)$/);
|
|
112
|
+
if (mm) named.set(mm[2], { pkg, imported: mm[1] });
|
|
113
|
+
else if (/^[A-Za-z_$][\w$]*$/.test(t)) named.set(t, { pkg, imported: t });
|
|
114
|
+
}
|
|
115
|
+
} else {
|
|
116
|
+
ns.set(bind, pkg);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return { named, ns, packages };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Parse Python imports. Same return shape as extractJsImports.
|
|
124
|
+
* from pkg import a, b as c -> named: a->{pkg,a}, c->{pkg,b}
|
|
125
|
+
* from pkg.sub import a -> named: a->{pkg,a} (pkg base)
|
|
126
|
+
* import pkg / import pkg as p -> ns: pkg->pkg / p->pkg
|
|
127
|
+
*/
|
|
128
|
+
export function extractPyImports(content) {
|
|
129
|
+
const code = blankComments(content);
|
|
130
|
+
const named = new Map();
|
|
131
|
+
const ns = new Map();
|
|
132
|
+
const packages = new Set();
|
|
133
|
+
|
|
134
|
+
const from = /^[ \t]*from\s+([A-Za-z_][\w.]*)\s+import\s+(.+)$/gm;
|
|
135
|
+
let m;
|
|
136
|
+
while ((m = from.exec(code))) {
|
|
137
|
+
const pkg = pkgBase(m[1], 'py');
|
|
138
|
+
packages.add(pkg);
|
|
139
|
+
let names = m[2].trim();
|
|
140
|
+
if (names === '*') continue;
|
|
141
|
+
names = names.replace(/[()]/g, '');
|
|
142
|
+
for (const p of parseNamedClause(names)) named.set(p.local, { pkg, imported: p.imported });
|
|
143
|
+
}
|
|
144
|
+
const imp = /^[ \t]*import\s+(.+)$/gm;
|
|
145
|
+
while ((m = imp.exec(code))) {
|
|
146
|
+
if (/^\s*import\s+/.test(m[0]) && /\bfrom\b/.test(m[0])) continue;
|
|
147
|
+
for (const raw of m[1].split(',')) {
|
|
148
|
+
const t = raw.trim(); if (!t) continue;
|
|
149
|
+
const mm = t.match(/^([A-Za-z_][\w.]*)(?:\s+as\s+([A-Za-z_]\w*))?$/);
|
|
150
|
+
if (!mm) continue;
|
|
151
|
+
const pkg = pkgBase(mm[1], 'py');
|
|
152
|
+
packages.add(pkg);
|
|
153
|
+
ns.set(mm[2] || mm[1].split('.')[0], pkg);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return { named, ns, packages };
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function extractImports(content, lang) {
|
|
160
|
+
return lang === 'py' ? extractPyImports(content) : extractJsImports(content);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Find call sites in `content` of `vulnFns` belonging to `pkg`, resolved through
|
|
165
|
+
* this file's import map. Returns [{ fn, line, via }]. Empty if the file does
|
|
166
|
+
* not import `pkg` (the precision gate).
|
|
167
|
+
*/
|
|
168
|
+
export function findImportAwareCallSites(content, lang, imports, pkg, vulnFns) {
|
|
169
|
+
if (!imports || !vulnFns || !vulnFns.length) return [];
|
|
170
|
+
const importsPkg = [...imports.packages].some((p) => pkgMatches(pkg, p, lang));
|
|
171
|
+
if (!importsPkg) return [];
|
|
172
|
+
const want = new Set(vulnFns);
|
|
173
|
+
const code = blankComments(content);
|
|
174
|
+
const lines = code.split('\n');
|
|
175
|
+
const sites = [];
|
|
176
|
+
const lineOf = (idx) => code.slice(0, idx).split('\n').length;
|
|
177
|
+
|
|
178
|
+
// 1) named/aliased imports: localName bound to {pkg, imported} where imported ∈ want
|
|
179
|
+
for (const [local, info] of imports.named) {
|
|
180
|
+
if (!pkgMatches(pkg, info.pkg, lang)) continue;
|
|
181
|
+
if (!want.has(info.imported)) continue;
|
|
182
|
+
const re = new RegExp(`\\b${local.replace(/[$]/g, '\\$&')}\\s*\\(`, 'g');
|
|
183
|
+
let mm;
|
|
184
|
+
while ((mm = re.exec(code))) sites.push({ fn: info.imported, line: lineOf(mm.index), via: local === info.imported ? 'named' : 'alias' });
|
|
185
|
+
}
|
|
186
|
+
// 2) namespace/default member access: ns.fn( where ns→pkg and fn ∈ want
|
|
187
|
+
for (const [local, p] of imports.ns) {
|
|
188
|
+
if (!pkgMatches(pkg, p, lang)) continue;
|
|
189
|
+
for (const fn of want) {
|
|
190
|
+
const sep = lang === 'py' ? '\\.' : '\\.';
|
|
191
|
+
const re = new RegExp(`\\b${local.replace(/[$]/g, '\\$&')}\\s*${sep}\\s*${fn.replace(/[^\w]/g, '\\$&')}\\s*\\(`, 'g');
|
|
192
|
+
let mm;
|
|
193
|
+
while ((mm = re.exec(code))) sites.push({ fn, line: lineOf(mm.index), via: 'namespace' });
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
// Dedup by fn+line.
|
|
197
|
+
const seen = new Set();
|
|
198
|
+
return sites.filter((s) => { const k = `${s.fn}:${s.line}`; if (seen.has(k)) return false; seen.add(k); return true; });
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Augment supplyChain findings (npm/pypi only) with import-aware call sites the
|
|
203
|
+
* regex pass missed, gated to files that import the package. Additive: it never
|
|
204
|
+
* removes existing sites, so it cannot regress the route-reachability classifier
|
|
205
|
+
* that runs next — it only gives it more (and more accurate) call sites.
|
|
206
|
+
*
|
|
207
|
+
* Mutates and returns `supplyChain`. Records `sc._importAwareCallSites` (count)
|
|
208
|
+
* for observability.
|
|
209
|
+
*/
|
|
210
|
+
export function augmentReachabilityViaImports(supplyChain, fileContents, vulnFnHints = {}) {
|
|
211
|
+
if (!Array.isArray(supplyChain)) return supplyChain;
|
|
212
|
+
// Collect the hardcoded/generated hint function names for a package. The
|
|
213
|
+
// hint map is keyed by `pkg` and by versioned `pkg@range` (and packages may
|
|
214
|
+
// be scoped `@scope/pkg`). The vulnerability decision already happened
|
|
215
|
+
// upstream; here we only need candidate function NAMES to locate call sites,
|
|
216
|
+
// so we gather version-agnostically.
|
|
217
|
+
const hintFnsFor = (name) => {
|
|
218
|
+
const out = [];
|
|
219
|
+
for (const [k, v] of Object.entries(vulnFnHints || {})) {
|
|
220
|
+
if (!Array.isArray(v)) continue;
|
|
221
|
+
const atIdx = k.lastIndexOf('@');
|
|
222
|
+
const base = atIdx > 0 ? k.slice(0, atIdx) : k; // >0 keeps a leading-@ scope intact
|
|
223
|
+
if (base === name) out.push(...v);
|
|
224
|
+
}
|
|
225
|
+
return out;
|
|
226
|
+
};
|
|
227
|
+
// Pre-parse imports per JS/PY file once.
|
|
228
|
+
const importsByFile = new Map();
|
|
229
|
+
for (const [file, content] of Object.entries(fileContents || {})) {
|
|
230
|
+
const lang = languageOfFile(file);
|
|
231
|
+
if (!lang) continue;
|
|
232
|
+
try { importsByFile.set(file, { lang, imports: extractImports(content, lang) }); } catch { /* skip unparseable */ }
|
|
233
|
+
}
|
|
234
|
+
if (!importsByFile.size) return supplyChain;
|
|
235
|
+
|
|
236
|
+
for (const sc of supplyChain) {
|
|
237
|
+
if (!sc || sc.type !== 'vulnerable_dep') continue;
|
|
238
|
+
if (sc.ecosystem !== 'npm' && sc.ecosystem !== 'pypi') continue;
|
|
239
|
+
const lang = sc.ecosystem === 'npm' ? 'js' : 'py';
|
|
240
|
+
const vulnFns = (sc.osvVulnFunctions || []).map((f) => {
|
|
241
|
+
const d = String(f).lastIndexOf('.'); return d > 0 ? String(f).slice(d + 1) : String(f);
|
|
242
|
+
}).concat(sc.usedVulnerableFunctions || []).concat(hintFnsFor(sc.name));
|
|
243
|
+
const uniqFns = [...new Set(vulnFns.filter(Boolean))];
|
|
244
|
+
if (!uniqFns.length) continue;
|
|
245
|
+
|
|
246
|
+
const existing = Array.isArray(sc.vulnerableFunctionCallSites) ? sc.vulnerableFunctionCallSites : [];
|
|
247
|
+
const seen = new Set(existing.map((s) => `${s.file}:${s.line}:${s.fn}`));
|
|
248
|
+
let added = 0;
|
|
249
|
+
for (const [file, { lang: fl, imports }] of importsByFile) {
|
|
250
|
+
if (fl !== lang) continue;
|
|
251
|
+
const content = fileContents[file];
|
|
252
|
+
const sites = findImportAwareCallSites(content, lang, imports, sc.name, uniqFns);
|
|
253
|
+
for (const s of sites) {
|
|
254
|
+
const key = `${file}:${s.line}:${s.fn}`;
|
|
255
|
+
if (seen.has(key)) continue;
|
|
256
|
+
seen.add(key);
|
|
257
|
+
existing.push({ pkg: sc.name, fn: s.fn, file, line: s.line, via: s.via, _r7: true });
|
|
258
|
+
added++;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
if (added > 0) {
|
|
262
|
+
sc.vulnerableFunctionCallSites = existing;
|
|
263
|
+
const usedSet = new Set(sc.usedVulnerableFunctions || []);
|
|
264
|
+
for (const s of existing) usedSet.add(s.fn);
|
|
265
|
+
sc.usedVulnerableFunctions = [...usedSet];
|
|
266
|
+
sc.noKnownCallSite = false;
|
|
267
|
+
sc._importAwareCallSites = (sc._importAwareCallSites || 0) + added;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
return supplyChain;
|
|
271
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
// R9 (PRD §5) — static behavioral analysis of package install hooks.
|
|
2
|
+
//
|
|
3
|
+
// The last several years of high-profile supply-chain attacks (event-stream,
|
|
4
|
+
// ua-parser-js, the xz backdoor's build step) ran their payload from a package
|
|
5
|
+
// LIFECYCLE script — preinstall/install/postinstall — that fetched and executed
|
|
6
|
+
// remote code, decoded an obfuscated blob, or exfiltrated environment secrets.
|
|
7
|
+
// Typosquat/dep-confusion (dep-confusion.js) catches the NAME; this catches the
|
|
8
|
+
// install-time BEHAVIOR, deterministically, on any package.json we can see.
|
|
9
|
+
//
|
|
10
|
+
// Precision-first: legitimate hooks (node-gyp rebuild, husky install,
|
|
11
|
+
// patch-package, electron-builder, "node ./scripts/x.js") do none of these, so
|
|
12
|
+
// they don't match. We only flag inline dangerous shapes.
|
|
13
|
+
|
|
14
|
+
const LIFECYCLE_HOOKS = ['preinstall', 'install', 'postinstall', 'prepare', 'prepublish', 'prepublishOnly'];
|
|
15
|
+
|
|
16
|
+
const DANGEROUS = [
|
|
17
|
+
{ id: 'download-exec', cwe: '829', sev: 'critical',
|
|
18
|
+
re: /\b(?:curl|wget|fetch)\b[^\n|&;]*?(?:https?:\/\/|\$\{?[A-Za-z_])[^\n|]*?[|&;]{1,2}\s*(?:sh|bash|zsh|node|python3?|ruby|perl)\b/i,
|
|
19
|
+
why: 'downloads and pipes remote content directly into an interpreter (remote code execution at install time)' },
|
|
20
|
+
{ id: 'base64-exec', cwe: '506', sev: 'critical',
|
|
21
|
+
re: /base64\s+(?:-d|--decode|-D)[^\n]*\|\s*(?:sh|bash|zsh|node|python3?)\b/i,
|
|
22
|
+
why: 'decodes a base64 blob and executes it (obfuscated payload)' },
|
|
23
|
+
{ id: 'eval-decode', cwe: '506', sev: 'critical',
|
|
24
|
+
re: /\beval\s*\(\s*(?:atob|Buffer\.from|decodeURIComponent|require\(\s*['"]child_process)/i,
|
|
25
|
+
why: 'eval of a decoded/obfuscated string or child_process (obfuscated payload)' },
|
|
26
|
+
{ id: 'inline-node-eval', cwe: '94', sev: 'high',
|
|
27
|
+
// Only when the inline program does something dangerous — network I/O,
|
|
28
|
+
// process spawn, or eval/decode. A bare `node -e "const fs=require('fs')…"`
|
|
29
|
+
// (common in legitimate build hooks, incl. this repo's) must NOT match.
|
|
30
|
+
re: /\bnode\s+(?:--eval|-e)\b[^\n]*(?:require\(\s*['"](?:https?|http2|child_process|net|dns|tls)\b|https?:\/\/|\batob\b|Buffer\.from\s*\([^)]*base64|\beval\s*\()/i,
|
|
31
|
+
why: 'runs an inline node program that performs network I/O, spawns a process, or evals a decoded blob' },
|
|
32
|
+
{ id: 'env-exfil', cwe: '200', sev: 'high',
|
|
33
|
+
re: /(?:process\.env|printenv|env\b|\$[A-Z_]{3,})[^\n]*(?:curl|wget|nc\b|netcat|https?:\/\/)[^\n]*(?:\||>|<)/i,
|
|
34
|
+
why: 'reads environment variables and pipes them to a network command (credential exfiltration)' },
|
|
35
|
+
{ id: 'cred-read', cwe: '522', sev: 'high',
|
|
36
|
+
re: /(?:\.npmrc|id_rsa|\.aws\/credentials|\.ssh\/|\.netrc|\.docker\/config)/i,
|
|
37
|
+
why: 'reads credential/secret files from an install hook' },
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
// Obfuscation heuristic: a long hex/base64 blob embedded in the hook string.
|
|
41
|
+
const OBFUSCATION = /(?:\\x[0-9a-f]{2}){12,}|[A-Za-z0-9+/]{120,}={0,2}/;
|
|
42
|
+
|
|
43
|
+
export function scanInstallScripts(fp, raw) {
|
|
44
|
+
if (typeof raw !== 'string' || !raw) return [];
|
|
45
|
+
const base = fp.split('/').pop();
|
|
46
|
+
if (base !== 'package.json') return [];
|
|
47
|
+
let pkg;
|
|
48
|
+
try { pkg = JSON.parse(raw); } catch { return []; }
|
|
49
|
+
const scripts = pkg && pkg.scripts;
|
|
50
|
+
if (!scripts || typeof scripts !== 'object') return [];
|
|
51
|
+
const lines = raw.split('\n');
|
|
52
|
+
const findings = [];
|
|
53
|
+
const seen = new Set();
|
|
54
|
+
for (const hook of LIFECYCLE_HOOKS) {
|
|
55
|
+
const cmd = scripts[hook];
|
|
56
|
+
if (typeof cmd !== 'string' || !cmd) continue;
|
|
57
|
+
// Locate the hook's line for attribution.
|
|
58
|
+
const li = lines.findIndex(l => new RegExp(`["']${hook}["']\\s*:`).test(l));
|
|
59
|
+
const line = li >= 0 ? li + 1 : 1;
|
|
60
|
+
const checks = [...DANGEROUS];
|
|
61
|
+
if (OBFUSCATION.test(cmd)) checks.push({ id: 'obfuscated-blob', cwe: '506', sev: 'high', re: /.*/, why: 'embeds a long obfuscated (hex/base64) blob' });
|
|
62
|
+
for (const d of checks) {
|
|
63
|
+
if (!d.re.test(cmd)) continue;
|
|
64
|
+
const key = `${line}:${d.id}`;
|
|
65
|
+
if (seen.has(key)) continue;
|
|
66
|
+
seen.add(key);
|
|
67
|
+
findings.push({
|
|
68
|
+
id: `install-script:${fp}:${hook}:${d.id}`,
|
|
69
|
+
severity: d.sev,
|
|
70
|
+
file: fp,
|
|
71
|
+
line,
|
|
72
|
+
vuln: `Malicious-looking install hook — \`${hook}\` ${d.id}`,
|
|
73
|
+
cwe: d.cwe,
|
|
74
|
+
family: 'malicious-install-script',
|
|
75
|
+
parser: 'SCA-INSTALL',
|
|
76
|
+
description: `The \`${hook}\` lifecycle script ${d.why}. Script: ${cmd.slice(0, 160)}`,
|
|
77
|
+
remediation: `Audit the \`${hook}\` script in ${base}. Lifecycle hooks should not fetch+exec remote code, decode/eval blobs, or read secrets. If this is a dependency's package.json, treat the package as suspicious and pin/replace it; install with --ignore-scripts where possible.`,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return findings;
|
|
82
|
+
}
|