weavatrix 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +146 -0
- package/bin/weavatrix-mcp.mjs +6 -0
- package/package.json +50 -0
- package/skill/SKILL.md +56 -0
- package/src/analysis/coverage-reports.js +232 -0
- package/src/analysis/dead-check.js +136 -0
- package/src/analysis/dep-check.js +310 -0
- package/src/analysis/dep-rules.js +221 -0
- package/src/analysis/duplicates-worker.js +11 -0
- package/src/analysis/duplicates.compute.js +215 -0
- package/src/analysis/duplicates.js +15 -0
- package/src/analysis/duplicates.run.js +51 -0
- package/src/analysis/duplicates.tokenize.js +182 -0
- package/src/analysis/endpoints.js +156 -0
- package/src/analysis/findings.js +52 -0
- package/src/analysis/graph-analysis.aggregate.js +280 -0
- package/src/analysis/graph-analysis.js +7 -0
- package/src/analysis/graph-analysis.refs.js +146 -0
- package/src/analysis/graph-analysis.summaries.js +62 -0
- package/src/analysis/internal-audit.collect.js +117 -0
- package/src/analysis/internal-audit.js +9 -0
- package/src/analysis/internal-audit.reach.js +47 -0
- package/src/analysis/internal-audit.run.js +189 -0
- package/src/analysis/manifests.js +169 -0
- package/src/analysis/source-complexity.ast.js +97 -0
- package/src/analysis/source-complexity.constants.js +55 -0
- package/src/analysis/source-complexity.js +14 -0
- package/src/analysis/source-complexity.report.js +76 -0
- package/src/analysis/source-complexity.walk.js +174 -0
- package/src/build-graph.js +102 -0
- package/src/config.js +5 -0
- package/src/graph/build-worker.js +30 -0
- package/src/graph/builder/lang-csharp.js +40 -0
- package/src/graph/builder/lang-css.js +29 -0
- package/src/graph/builder/lang-go.js +53 -0
- package/src/graph/builder/lang-html.js +29 -0
- package/src/graph/builder/lang-java.js +29 -0
- package/src/graph/builder/lang-js.js +168 -0
- package/src/graph/builder/lang-python.js +78 -0
- package/src/graph/builder/lang-rust.js +45 -0
- package/src/graph/builder/spec-pkg.js +87 -0
- package/src/graph/graph-filter.js +44 -0
- package/src/graph/internal-builder.build.js +217 -0
- package/src/graph/internal-builder.js +12 -0
- package/src/graph/internal-builder.langs.js +86 -0
- package/src/graph/internal-builder.resolvers.js +116 -0
- package/src/graph/layout.js +35 -0
- package/src/infra/infra-items.js +255 -0
- package/src/infra/infra-registry.js +184 -0
- package/src/infra/infra.detect.js +119 -0
- package/src/infra/infra.js +38 -0
- package/src/infra/infra.match.js +102 -0
- package/src/infra/infra.scan.js +93 -0
- package/src/mcp/catalog.mjs +68 -0
- package/src/mcp/graph-context.mjs +276 -0
- package/src/mcp/tools-actions.mjs +132 -0
- package/src/mcp/tools-graph.mjs +274 -0
- package/src/mcp/tools-health.mjs +209 -0
- package/src/mcp/tools-impact.mjs +189 -0
- package/src/mcp-rg.mjs +51 -0
- package/src/mcp-server.mjs +171 -0
- package/src/mcp-source-tools.mjs +139 -0
- package/src/process.js +77 -0
- package/src/scan/discover.inventory.js +78 -0
- package/src/scan/discover.js +5 -0
- package/src/scan/discover.list.js +79 -0
- package/src/scan/discover.stack.js +227 -0
- package/src/scan/search.core.js +24 -0
- package/src/scan/search.git.js +102 -0
- package/src/scan/search.js +9 -0
- package/src/scan/search.node.js +83 -0
- package/src/scan/search.preview.js +49 -0
- package/src/scan/search.rg.js +145 -0
- package/src/security/advisory-store.js +177 -0
- package/src/security/installed.js +247 -0
- package/src/security/malware-file-heuristics.js +121 -0
- package/src/security/malware-heuristics.exclusions.js +134 -0
- package/src/security/malware-heuristics.js +15 -0
- package/src/security/malware-heuristics.roots.js +142 -0
- package/src/security/malware-heuristics.scan.js +87 -0
- package/src/security/malware-heuristics.sweep.js +122 -0
- package/src/security/malware-scoring.js +84 -0
- package/src/security/match.js +85 -0
- package/src/security/registry-sig.classify.js +136 -0
- package/src/security/registry-sig.js +18 -0
- package/src/security/registry-sig.rules.js +188 -0
- package/src/security/typosquat.js +72 -0
- package/src/util.js +27 -0
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { makeFinding } from "../analysis/findings.js";
|
|
2
|
+
import { MALWARE_ALLOWLIST } from "./registry-sig.js";
|
|
3
|
+
import { classifyTyposquat } from "./typosquat.js";
|
|
4
|
+
|
|
5
|
+
const STRONG_URL_KEYS = new Set(["exfil-url", "exfil-ip", "cloud-metadata-url", "network-url"]);
|
|
6
|
+
|
|
7
|
+
function addTyposquatCoSignals(byPkg) {
|
|
8
|
+
for (const pkg of [...byPkg.keys()]) {
|
|
9
|
+
if (!byPkg.get(pkg).some((s) => !s.weak)) continue;
|
|
10
|
+
const sq = classifyTyposquat(pkg);
|
|
11
|
+
if (!sq) continue;
|
|
12
|
+
byPkg.get(pkg).push({
|
|
13
|
+
key: "typosquat",
|
|
14
|
+
severity: "medium",
|
|
15
|
+
nearZeroFp: false,
|
|
16
|
+
what: `name resembles "${sq.nearest}" (edit distance ${sq.distance})`,
|
|
17
|
+
snippet: `${pkg} ~= ${sq.nearest}`,
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function visibleSignals(signals, allow) {
|
|
23
|
+
const kept = signals.filter((s) => !(allow && s.noisy));
|
|
24
|
+
if (!kept.length) return [];
|
|
25
|
+
const hasStrongerUrl = kept.some((s) => STRONG_URL_KEYS.has(s.key));
|
|
26
|
+
return hasStrongerUrl ? kept.filter((s) => s.key !== "hardcoded-url") : kept;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function escalatedSeverity(baseSeverity, smoking, multi, nearZeroFp) {
|
|
30
|
+
if (smoking && nearZeroFp) return "critical";
|
|
31
|
+
if ((smoking || multi) && (baseSeverity === "medium" || baseSeverity === "low")) return "high";
|
|
32
|
+
return baseSeverity;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function evidenceRows(signals) {
|
|
36
|
+
const seen = new Set();
|
|
37
|
+
const rows = [];
|
|
38
|
+
for (const s of signals) {
|
|
39
|
+
const row = {
|
|
40
|
+
file: String(s.file).replace(/\\/g, "/"),
|
|
41
|
+
line: s.line || 0,
|
|
42
|
+
snippet: s.snippet.slice(0, 200),
|
|
43
|
+
};
|
|
44
|
+
const key = `${row.file}\0${row.line}\0${row.snippet}`;
|
|
45
|
+
if (seen.has(key)) continue;
|
|
46
|
+
seen.add(key);
|
|
47
|
+
rows.push(row);
|
|
48
|
+
if (rows.length >= 6) break;
|
|
49
|
+
}
|
|
50
|
+
return rows;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function buildMalwareFindings({ byPkg, importedPkgs, contentRoots, scanMode, excludedSignals }) {
|
|
54
|
+
addTyposquatCoSignals(byPkg);
|
|
55
|
+
const findings = [];
|
|
56
|
+
for (const [pkg, signals] of byPkg) {
|
|
57
|
+
const visible = visibleSignals(signals, MALWARE_ALLOWLIST.has(pkg));
|
|
58
|
+
if (!visible.length) continue;
|
|
59
|
+
const keys = new Set(visible.map((s) => s.key));
|
|
60
|
+
const smoking = visible.some((s) => s.nearZeroFp);
|
|
61
|
+
const escalationKeys = new Set(visible.filter((s) => !s.weak).map((s) => s.key));
|
|
62
|
+
const multi = escalationKeys.size >= 2;
|
|
63
|
+
for (const key of keys) {
|
|
64
|
+
const ofKey = visible.filter((s) => s.key === key);
|
|
65
|
+
const base = ofKey[0];
|
|
66
|
+
const severity = escalatedSeverity(base.severity, smoking, multi, base.nearZeroFp);
|
|
67
|
+
findings.push(makeFinding({
|
|
68
|
+
category: "malware",
|
|
69
|
+
rule: key,
|
|
70
|
+
severity,
|
|
71
|
+
confidence: base.nearZeroFp ? "high" : multi ? "medium" : "low",
|
|
72
|
+
title: `${base.what}: ${pkg}`,
|
|
73
|
+
detail: `${base.what} in installed package "${pkg}"${multi ? ` - co-occurs with ${[...keys].filter((k) => k !== key).join(", ")} (escalated)` : ""}${importedPkgs.has(pkg) ? ". This package IS imported by the repo's own code." : ". Transitive install (still runs its install scripts)."} Heuristic pattern match - inspect the cited file before acting.`,
|
|
74
|
+
package: pkg,
|
|
75
|
+
file: base.file ? String(base.file).replace(/\\/g, "/") : "",
|
|
76
|
+
line: base.line || 0,
|
|
77
|
+
evidence: evidenceRows(ofKey),
|
|
78
|
+
source: "heuristic",
|
|
79
|
+
fixHint: smoking ? `treat as compromised: remove ${pkg}, reinstall from a clean lockfile, rotate reachable secrets` : `review the cited code in ${pkg}`,
|
|
80
|
+
}));
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return { findings, scanMode, packagesScanned: contentRoots.reduce((n, r) => n + (r.packages || 0), 0), excludedSignals };
|
|
84
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// Pure OSV advisory matcher — installed {ecosystem,name,version} × advisory affected[] → hits.
|
|
2
|
+
// From-scratch lite version comparator (semver-ish + PyPI epoch), NO deps. Exact `versions[]`
|
|
3
|
+
// membership is preferred (confidence high); range evaluation is the fallback (confidence medium) —
|
|
4
|
+
// per DEPS_SECURITY_PLAN P4: when unsure, label lower confidence rather than guessing "vulnerable".
|
|
5
|
+
export function parseVersion(v) {
|
|
6
|
+
let s = String(v || "").trim().replace(/^[v=]/, "");
|
|
7
|
+
let epoch = 0;
|
|
8
|
+
const em = s.match(/^(\d+)!(.*)$/); // PyPI epoch "1!2.0"
|
|
9
|
+
if (em) { epoch = Number(em[1]); s = em[2]; }
|
|
10
|
+
s = s.split("+")[0]; // build metadata never orders
|
|
11
|
+
const dash = s.indexOf("-");
|
|
12
|
+
const core = dash < 0 ? s : s.slice(0, dash);
|
|
13
|
+
const pre = dash < 0 ? [] : s.slice(dash + 1).split(".").filter(Boolean);
|
|
14
|
+
const nums = core.split(".").map((x) => { const n = parseInt(x, 10); return Number.isFinite(n) ? n : 0; });
|
|
15
|
+
return { epoch, nums, pre };
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function compareVersions(a, b) {
|
|
19
|
+
const pa = parseVersion(a), pb = parseVersion(b);
|
|
20
|
+
if (pa.epoch !== pb.epoch) return pa.epoch - pb.epoch;
|
|
21
|
+
for (let i = 0; i < Math.max(pa.nums.length, pb.nums.length); i++) {
|
|
22
|
+
const x = pa.nums[i] || 0, y = pb.nums[i] || 0;
|
|
23
|
+
if (x !== y) return x - y;
|
|
24
|
+
}
|
|
25
|
+
if (!pa.pre.length && pb.pre.length) return 1; // release > its pre-release
|
|
26
|
+
if (pa.pre.length && !pb.pre.length) return -1;
|
|
27
|
+
for (let i = 0; i < Math.max(pa.pre.length, pb.pre.length); i++) {
|
|
28
|
+
const x = pa.pre[i], y = pb.pre[i];
|
|
29
|
+
if (x === undefined) return -1;
|
|
30
|
+
if (y === undefined) return 1;
|
|
31
|
+
const nx = /^\d+$/.test(x), ny = /^\d+$/.test(y);
|
|
32
|
+
if (nx && ny) { if (Number(x) !== Number(y)) return Number(x) - Number(y); }
|
|
33
|
+
else if (nx !== ny) return nx ? -1 : 1; // numeric identifiers order before alphanumeric (semver)
|
|
34
|
+
else { const c = x < y ? -1 : x > y ? 1 : 0; if (c) return c; }
|
|
35
|
+
}
|
|
36
|
+
return 0;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// affected = one OSV `affected[]` entry (already filtered to this package): {versions?, ranges?}.
|
|
40
|
+
// Returns { hit, by: "versions"|"range"|"range-open", confidence } — GIT ranges are skipped (commit
|
|
41
|
+
// hashes aren't comparable to release versions).
|
|
42
|
+
export function isVersionAffected(version, affected = {}) {
|
|
43
|
+
if (Array.isArray(affected.versions) && affected.versions.length) {
|
|
44
|
+
const norm = String(version).replace(/^v/, "");
|
|
45
|
+
if (affected.versions.some((x) => String(x).replace(/^v/, "") === norm)) return { hit: true, by: "versions", confidence: "high" };
|
|
46
|
+
// NOT a short-circuit: OSV's enumerated versions[] is often incomplete while ranges[] stays
|
|
47
|
+
// authoritative (that's what the server-side querybatch matches on). Falling through to the
|
|
48
|
+
// ranges below is what stops "querybatch flagged it, but re-analysis shows 0 vulnerabilities".
|
|
49
|
+
}
|
|
50
|
+
for (const r of affected.ranges || []) {
|
|
51
|
+
if (!r || r.type === "GIT") continue;
|
|
52
|
+
let active = false;
|
|
53
|
+
for (const e of r.events || []) { // OSV spec: events are sorted
|
|
54
|
+
if (e.introduced !== undefined) {
|
|
55
|
+
active = e.introduced === "0" || compareVersions(version, e.introduced) >= 0;
|
|
56
|
+
} else if (e.fixed !== undefined) {
|
|
57
|
+
if (active && compareVersions(version, e.fixed) < 0) return { hit: true, by: "range", confidence: "medium" };
|
|
58
|
+
active = false;
|
|
59
|
+
} else if (e.last_affected !== undefined) {
|
|
60
|
+
if (active && compareVersions(version, e.last_affected) <= 0) return { hit: true, by: "range", confidence: "medium" };
|
|
61
|
+
active = false;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (active) return { hit: true, by: "range-open", confidence: "medium" }; // introduced, never fixed
|
|
65
|
+
}
|
|
66
|
+
return { hit: false };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// installed: [{ecosystem,name,version,...}]; queryFn(ecosystem, name) → normalized advisory rows
|
|
70
|
+
// [{id, kind: "vuln"|"malicious", severity, summary, url, affected}]. Dedup by (advisory id, package).
|
|
71
|
+
export function matchAdvisories(installed, queryFn) {
|
|
72
|
+
const hits = [];
|
|
73
|
+
const seen = new Set();
|
|
74
|
+
for (const pkg of installed || []) {
|
|
75
|
+
for (const adv of queryFn(pkg.ecosystem, pkg.name) || []) {
|
|
76
|
+
const key = `${adv.id}|${pkg.name}|${pkg.version}`;
|
|
77
|
+
if (seen.has(key)) continue;
|
|
78
|
+
const m = isVersionAffected(pkg.version, adv.affected || {});
|
|
79
|
+
if (!m.hit) continue;
|
|
80
|
+
seen.add(key);
|
|
81
|
+
hits.push({ pkg, adv, matchedBy: m.by, confidence: m.confidence });
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return hits;
|
|
85
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
// Pure classifiers for registry-sig.js: install-script/content/.pth/resolved-URL classification and
|
|
2
|
+
// the doc/benign-URL noise filters.
|
|
3
|
+
|
|
4
|
+
import { CONTENT_RULES } from "./registry-sig.rules.js";
|
|
5
|
+
|
|
6
|
+
// ---- install-script classification (package.json preinstall/install/postinstall) ----
|
|
7
|
+
const FETCH_RE = /\b(curl|wget|iwr|invoke-webrequest|certutil\s+-urlcache)\b|https?:\/\//i;
|
|
8
|
+
const EXEC_RE = /\|\s*(sh|bash|node|cmd|powershell|pwsh)\b|\b(node|python[0-9]?|perl|ruby|deno)\s+-e\s|python[0-9]?\s+-c\s|base64\s+(-d|--decode)|\beval\b|frombase64string|-enc(odedcommand)?\b|\biex\b|invoke-expression/i;
|
|
9
|
+
// routine native-build steps — never signals on their own
|
|
10
|
+
const BENIGN_ARG = "[\\w./:@=+,-]+";
|
|
11
|
+
const BENIGN_RE = new RegExp(`^(node-gyp\\s+rebuild|node-pre-gyp\\s+install(\\s+${BENIGN_ARG})*|prebuild-install(\\s+${BENIGN_ARG})*|node\\s+(scripts?|install|postinstall|lib)\\/[\\w./-]+\\.js(\\s+${BENIGN_ARG})*|electron-rebuild(\\s+${BENIGN_ARG})*|patch-package|husky(\\s+install)?|opencollective(\\s+${BENIGN_ARG})*|is-ci(\\s+${BENIGN_ARG})*)$`, "i");
|
|
12
|
+
const SHELL_CHAIN_RE = /&&|\|\||[;|`]|[$]\(|\r|\n/i;
|
|
13
|
+
function isBenignLifecycleScript(script) {
|
|
14
|
+
return !SHELL_CHAIN_RE.test(script) && BENIGN_RE.test(script);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// → [{key, severity, nearZeroFp, what, snippet}] for one package's scripts object
|
|
18
|
+
export function classifyInstallScript(scripts = {}) {
|
|
19
|
+
const signals = [];
|
|
20
|
+
for (const hook of ["preinstall", "install", "postinstall", "prepare"]) {
|
|
21
|
+
const s = String(scripts[hook] || "").trim();
|
|
22
|
+
if (!s || isBenignLifecycleScript(s)) continue;
|
|
23
|
+
const fetches = FETCH_RE.test(s);
|
|
24
|
+
const execs = EXEC_RE.test(s);
|
|
25
|
+
if (fetches && execs) {
|
|
26
|
+
signals.push({ key: "install-script-beacon", severity: "critical", nearZeroFp: true, what: `${hook} downloads AND executes (fetch+exec)`, snippet: s.slice(0, 200) });
|
|
27
|
+
} else if (fetches) {
|
|
28
|
+
signals.push({ key: "install-script-fetch", severity: "high", nearZeroFp: false, what: `${hook} reaches the network`, snippet: s.slice(0, 200) });
|
|
29
|
+
} else if (execs) {
|
|
30
|
+
signals.push({ key: "install-script-exec", severity: "medium", nearZeroFp: false, noisy: true, what: `${hook} uses eval/inline-node/base64`, snippet: s.slice(0, 200) });
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return signals;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// → signals[] for a chunk of file text (fallback scanning + rg-hit validation)
|
|
37
|
+
export function classifyContent(text) {
|
|
38
|
+
const t = String(text || "");
|
|
39
|
+
let out = [];
|
|
40
|
+
for (const r of CONTENT_RULES) if (r.re.test(t)) out.push(r);
|
|
41
|
+
const strongerUrl = out.some((r) => ["exfil-url", "exfil-ip", "cloud-metadata-url", "network-url"].includes(r.key));
|
|
42
|
+
const docOnlyUrl = /\b(see|readme|docs?|documentation)\b[\s\S]{0,80}https?:\/\//i.test(t) || /https?:\/\/[^'"`\s)\\]+\/docs\b/i.test(t);
|
|
43
|
+
if (strongerUrl || docOnlyUrl) out = out.filter((r) => r.key !== "hardcoded-url");
|
|
44
|
+
return out;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const PTH_SUSPICIOUS_IMPORT_RE = /^\s*import\s+.*\b(exec|eval|compile|__import__|os\.system|subprocess|socket|requests|urllib|httpx|base64|b64decode|marshal|zlib|ctypes)\b/i;
|
|
48
|
+
export function classifyPythonPth(text) {
|
|
49
|
+
const signals = [];
|
|
50
|
+
const lines = String(text || "").split(/\r?\n/);
|
|
51
|
+
lines.forEach((line, i) => {
|
|
52
|
+
if (!PTH_SUSPICIOUS_IMPORT_RE.test(line)) return;
|
|
53
|
+
signals.push({
|
|
54
|
+
key: "python-pth-startup-exec",
|
|
55
|
+
severity: "high",
|
|
56
|
+
nearZeroFp: false,
|
|
57
|
+
what: "Python .pth startup import with executable/network/decoder behavior",
|
|
58
|
+
snippet: line.trim().slice(0, 200),
|
|
59
|
+
line: i + 1,
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
return signals;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// ---- noise filters for the WEAK URL rules only (hardcoded-url, network-url). LICENSE files,
|
|
66
|
+
// comments and doc-links are where ~100% of their false positives live; the strong rules
|
|
67
|
+
// (miner/shell/exfil-url) still scan those files — a payload hidden in LICENSE stays caught. ----
|
|
68
|
+
export const NOISY_URL_KEYS = new Set(["hardcoded-url", "network-url"]);
|
|
69
|
+
|
|
70
|
+
const DOC_BASENAME_RE = /^((license|licence|notice|copying|copyright|authors|contributors|changelog|changes|history|readme|third[-_]?party[-_]?notices?|code[-_]?of[-_]?conduct)(\.(md|markdown|mdown|mkd|txt|rst|adoc|html?))?|licen[cs]es?[-_][\w.-]+|patents)$/i;
|
|
71
|
+
const DOC_EXT_RE = /\.(markdown|mdown|mkd|txt|rst|adoc|asciidoc|pod)$/i;
|
|
72
|
+
|
|
73
|
+
// prose/license file → URL evidence there is documentation, not behavior
|
|
74
|
+
export function isDocFile(file) {
|
|
75
|
+
const base = String(file || "").replace(/\\/g, "/").split("/").pop() || "";
|
|
76
|
+
return DOC_BASENAME_RE.test(base) || DOC_EXT_RE.test(base);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// comment openers BEFORE a URL ("// see", "* {@link", "# docs"); `//` not preceded by ':'/quote so
|
|
80
|
+
// protocol tails and '//'-in-string don't count; '#' not a hex color / shebang
|
|
81
|
+
const COMMENT_MARKER_RE = /(^|[^:'"`/])\/\/|\/\*|<!--|^\s*\*\s|\n\s*\*\s|(^|[\s({;,=])#(?!!|[0-9a-f]{3,8}\b)/i;
|
|
82
|
+
const DOC_WORD_PRE_RE = /\b(see|docs?|documentation|learn more|read more|more info(rmation)?|for details|refer(ence)? to|guide|manual|tutorial|homepage|website|link|visit|licen[cs]ed?|copyright|spec(ification)?|polyfill|based on|inspired by|ported from|wiki|report(ed)?\s+(at|to|bugs?|issues?)|file (an? )?(issue|bug)|available at|found at|thanks to)\b|@(see|link|license)\b|["']?\$schema["']?\s*:|\]\(\s*$/i;
|
|
83
|
+
const DOC_WORD_POST_RE = /^[):,'"`\]\s]{0,6}\b(spec(ification)?|docs?|documentation|standard|for (more|details)|page)\b/i;
|
|
84
|
+
// hosts that only serve standards/licenses/docs/registry pages — not attacker-controllable payload hosts
|
|
85
|
+
const BENIGN_URL_HOSTS = [
|
|
86
|
+
"w3.org", "ietf.org", "rfc-editor.org", "iana.org", "whatwg.org", "ecma-international.org", "tc39.es",
|
|
87
|
+
"unicode.org", "schema.org", "json-schema.org", "schemastore.org", "spdx.org", "semver.org",
|
|
88
|
+
"apache.org", "opensource.org", "gnu.org", "creativecommons.org", "choosealicense.com",
|
|
89
|
+
"mozilla.org", "nodejs.org", "npmjs.com", "npmjs.org", "yarnpkg.com", "python.org", "pypi.org",
|
|
90
|
+
"golang.org", "go.dev", "godoc.org", "readthedocs.io", "readthedocs.org", "editorconfig.org",
|
|
91
|
+
];
|
|
92
|
+
const DOC_URL_PATH_RE = /\/(docs?|documentation|wiki|issues?|pull|blob|tree|releases|licenses?|rfc\d*|help|manual|guide|spec|schemas?)([/#?.]|$)/i;
|
|
93
|
+
const URL_TOKEN_RE = /https?:\/\/[^\s'"`)\\<>]+/gi;
|
|
94
|
+
|
|
95
|
+
function isBenignUrl(url) {
|
|
96
|
+
try {
|
|
97
|
+
const u = new URL(url);
|
|
98
|
+
const host = u.host.replace(/^www\./, "").replace(/:\d+$/, "").toLowerCase();
|
|
99
|
+
if (BENIGN_URL_HOSTS.some((h) => host === h || host.endsWith(`.${h}`))) return true;
|
|
100
|
+
return DOC_URL_PATH_RE.test(u.pathname);
|
|
101
|
+
} catch { return false; }
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// snippet-level classifier: true when EVERY URL in the snippet sits in doc/comment context — a
|
|
105
|
+
// comment opener before the first URL comments out the rest of the line; otherwise each URL needs
|
|
106
|
+
// its own doc-word neighborhood or a benign standards/docs host. Conservative on purpose: one
|
|
107
|
+
// non-benign URL keeps the whole snippet as evidence.
|
|
108
|
+
export function isBenignUrlContext(text) {
|
|
109
|
+
const s = String(text || "");
|
|
110
|
+
const urls = [...s.matchAll(URL_TOKEN_RE)];
|
|
111
|
+
if (!urls.length) return false;
|
|
112
|
+
if (COMMENT_MARKER_RE.test(s.slice(Math.max(0, urls[0].index - 80), urls[0].index))) return true;
|
|
113
|
+
return urls.every((m) => {
|
|
114
|
+
const pre = s.slice(Math.max(0, m.index - 80), m.index);
|
|
115
|
+
const post = s.slice(m.index + m[0].length, m.index + m[0].length + 48);
|
|
116
|
+
return DOC_WORD_PRE_RE.test(pre) || DOC_WORD_POST_RE.test(post) || isBenignUrl(m[0]);
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// packages whose JOB is talking to cloud endpoints (IMDS auth, vault, STS) — mutes cloud-metadata-url
|
|
121
|
+
// and the weak hardcoded-url for them ONLY. hardcoded-url never escalates (weak) and is hidden whenever
|
|
122
|
+
// a stronger URL rule fires, so nothing real is lost; a trojaned version still trips every other rule.
|
|
123
|
+
const CLOUD_SDK_PKG_RE = /^(aws-sdk|gcp-metadata|google-auth-library|azure-identity|boto3|botocore|google-auth|msrestazure|cloud\.google\.com\/go(\/.*)?|github\.com\/aws\/aws-sdk-go(-v2)?(\/.*)?|github\.com\/[aA]zure\/azure-sdk-for-go(\/.*)?)$|^@(aws-sdk|azure|google-cloud)\//;
|
|
124
|
+
const CLOUD_SDK_MUTED_KEYS = new Set(["cloud-metadata-url"]);
|
|
125
|
+
export function isCloudSdkMetadataUse(pkg, ruleKey) {
|
|
126
|
+
return CLOUD_SDK_MUTED_KEYS.has(ruleKey) && CLOUD_SDK_PKG_RE.test(String(pkg || ""));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// package-lock `resolved` origin check: anything outside the big registries is worth a look.
|
|
130
|
+
const REGISTRY_RE = /^https:\/\/(registry\.npmjs\.org|registry\.yarnpkg\.com|registry\.npmmirror\.com)\//i;
|
|
131
|
+
export function classifyResolvedUrl(resolved) {
|
|
132
|
+
const r = String(resolved || "");
|
|
133
|
+
if (!r || REGISTRY_RE.test(r) || r.startsWith("file:")) return null; // file: = workspace/link, common
|
|
134
|
+
if (/^git\+|^git:|github\.com\//i.test(r)) return { key: "non-registry-source", severity: "medium", nearZeroFp: false, what: "installed from git, not the npm registry (no registry audit trail)", snippet: r.slice(0, 200) };
|
|
135
|
+
return { key: "non-registry-source", severity: "medium", nearZeroFp: false, what: "installed from a non-registry URL", snippet: r.slice(0, 200) };
|
|
136
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Curated malware-heuristic signature table (defensive scanning of INSTALLED dependencies) + pure
|
|
2
|
+
// classifiers. Same philosophy as infra-registry.js: deterministic patterns, near-zero-FP rules marked
|
|
3
|
+
// explicitly, and an allowlist that downgrades ONLY the noisy rules — NEVER the smoking-gun ones
|
|
4
|
+
// (miners, fetch+exec): supply-chain attacks trojan previously-good packages (event-stream lesson).
|
|
5
|
+
// DEPS_SECURITY_PLAN P5.
|
|
6
|
+
// Facade: signature tables live in registry-sig.rules.js, classifiers in registry-sig.classify.js.
|
|
7
|
+
|
|
8
|
+
export { MALWARE_ALLOWLIST, CONTENT_RULES } from "./registry-sig.rules.js";
|
|
9
|
+
export {
|
|
10
|
+
classifyInstallScript,
|
|
11
|
+
classifyContent,
|
|
12
|
+
classifyPythonPth,
|
|
13
|
+
NOISY_URL_KEYS,
|
|
14
|
+
isDocFile,
|
|
15
|
+
isBenignUrlContext,
|
|
16
|
+
isCloudSdkMetadataUse,
|
|
17
|
+
classifyResolvedUrl,
|
|
18
|
+
} from "./registry-sig.classify.js";
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
// Signature tables for registry-sig.js: the malware allowlist and the content-rule set.
|
|
2
|
+
|
|
3
|
+
// Packages with famously "suspicious-looking but legit" behavior: native builds, postinstall banners,
|
|
4
|
+
// heavily-minified bundles. Downgrades obfuscation-style signals only.
|
|
5
|
+
export const MALWARE_ALLOWLIST = new Set([
|
|
6
|
+
"esbuild", "sharp", "node-gyp", "husky", "playwright", "@playwright/test", "puppeteer", "electron",
|
|
7
|
+
"better-sqlite3", "bcrypt", "fsevents", "core-js", "styled-components", "cypress", "sentry-cli",
|
|
8
|
+
"@sentry/cli", "node-sass", "sqlite3", "grpc", "@grpc/grpc-js", "protobufjs", "swc", "@swc/core",
|
|
9
|
+
"workerd", "sass-embedded", "canvas", "re2", "cpu-features", "ssh2", "argon2", "tree-sitter",
|
|
10
|
+
]);
|
|
11
|
+
|
|
12
|
+
// One source of truth for beacon/exfil endpoints: exfil-url MATCHES them, the weaker URL rules
|
|
13
|
+
// EXCLUDE them (lookahead) so a single URL never counts as two distinct escalation keys.
|
|
14
|
+
// webhook.site / oast.* / canarytokens / dnslog.cn: Shai-Hulud-era exfil + OOB-callback services.
|
|
15
|
+
const EXFIL_HOSTS = "discord(app)?\\.com/api/webhooks|hooks\\.slack\\.com/services|api\\.telegram\\.org/bot|pastebin\\.com/raw|burpcollaborator|oastify\\.com|oast\\.(pro|live|fun|me|site|online)|interact\\.sh|pipedream\\.net|requestbin|webhook\\.site|canarytokens\\.(com|org)|dnslog\\.cn";
|
|
16
|
+
// external URL that is NOT an exfil endpoint (owned by exfil-url) and NOT a raw IP (owned by exfil-ip)
|
|
17
|
+
const PLAIN_EXTERNAL_URL = `https?://(?!(${EXFIL_HOSTS})|[0-9]{1,3}\\.|localhost\\b|127\\.|0\\.0\\.0\\.0|10\\.|192\\.168\\.|169\\.254\\.|172\\.(1[6-9]|2\\d|3[01])\\.)[^'"\`\\s)\\\\]+`;
|
|
18
|
+
|
|
19
|
+
// ---- content rules (scanned over node_modules file text; `pattern` is ripgrep-safe ERE, `re` is the
|
|
20
|
+
// JS validator used both to classify rg hits and by the no-rg fallback) ----
|
|
21
|
+
export const CONTENT_RULES = [
|
|
22
|
+
{
|
|
23
|
+
key: "crypto-miner",
|
|
24
|
+
severity: "critical",
|
|
25
|
+
nearZeroFp: true,
|
|
26
|
+
// mining-pool protocol + specific miner names / pool hosts / xmrig flags. Deliberately NO generic
|
|
27
|
+
// words ("miner", "hash") — those false-positive on legit crypto/hashing libs.
|
|
28
|
+
pattern: "stratum\\+(tcp|ssl)://|xmrig|cryptonight|randomx|coinhive|coinimp|cryptoloot|jsecoin|webminepool|minexmr|supportxmr|nanopool\\.org|nicehash\\.com|minergate|--donate-level|--coin\\s+monero",
|
|
29
|
+
re: /stratum\+(tcp|ssl):\/\/|xmrig|cryptonight|randomx|coinhive|coinimp|cryptoloot|jsecoin|webminepool|minexmr|supportxmr|nanopool\.org|nicehash\.com|minergate|--donate-level|--coin\s+monero/i,
|
|
30
|
+
what: "crypto-miner signature (mining pool protocol / miner name / xmrig flag)",
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
key: "reverse-shell",
|
|
34
|
+
severity: "critical",
|
|
35
|
+
nearZeroFp: true,
|
|
36
|
+
// canonical reverse/bind shells — a shell wired to a socket. Essentially never legitimate inside a
|
|
37
|
+
// published package: /dev/tcp redirection, netcat -e, interactive-shell redirect, mkfifo pipe shell.
|
|
38
|
+
pattern: "/dev/tcp/[0-9]|\\bnc(at)?\\s+-e\\b|\\b(ba)?sh\\s+-i\\b\\s*(2)?>&|mkfifo\\b.{0,60}(ba)?sh\\b|0<&196|socket\\.socket\\(.{0,40}(SOCK_STREAM).{0,80}(/bin/sh|exec)",
|
|
39
|
+
re: /\/dev\/tcp\/[0-9]|\bnc(at)?\s+-e\b|\b(ba)?sh\s+-i\b\s*(2)?>&|mkfifo\b[\s\S]{0,60}?\b(ba)?sh\b|0<&196|socket\.socket\([\s\S]{0,120}?(connect|SOCK_STREAM)[\s\S]{0,180}?(\/bin\/sh|subprocess|pty\.spawn|os\.dup2)/i,
|
|
40
|
+
what: "reverse/bind-shell pattern (interactive shell wired to a socket)",
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
key: "sensitive-file-read",
|
|
44
|
+
severity: "medium", // strong signal, but backup/dotfile tools touch these too → escalates via co-occurrence
|
|
45
|
+
nearZeroFp: false,
|
|
46
|
+
// rg pre-filter is the bare path; the JS re REQUIRES a read/copy/exfil verb on the same line — a
|
|
47
|
+
// credential PATH alone (docs, READMEs, comments) is not a read (that was the app-builder-lib /
|
|
48
|
+
// bun-types false positive). /etc/passwd + .netrc dropped (too common in prose).
|
|
49
|
+
pattern: "\\.ssh/(id_rsa|id_ed25519|id_dsa|authorized_keys)|\\.aws/credentials|\\.git-credentials|/etc/shadow|\\.docker/config\\.json|\\.kube/config",
|
|
50
|
+
re: /(readFileSync|readFile|createReadStream|openSync|\.read\(|copyFile|readlink|Get-Content|\bcat\s|\bscp\s|\bcurl\b[^\n]*-[TF])[\s\S]{0,80}?(\.ssh\/(id_(rsa|ed25519|dsa)|authorized_keys)|\.aws\/credentials|\.git-credentials|\/etc\/shadow|\.docker\/config\.json|\.kube\/config)/,
|
|
51
|
+
what: "reads a credential/secret file (SSH key, cloud creds) in a filesystem-read context",
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
key: "dynamic-require",
|
|
55
|
+
severity: "medium",
|
|
56
|
+
nearZeroFp: false,
|
|
57
|
+
noisy: true,
|
|
58
|
+
// require() of a DECODED/obfuscated name — NOT process.binding (legit legacy Node API in lodash/
|
|
59
|
+
// safer-buffer/jest, often only in a comment → was a false positive).
|
|
60
|
+
pattern: "require\\(\\s*(Buffer\\.from|atob)\\(|module\\.constructor\\._load|globalThis\\[['\"]require|require\\(\\s*_0x",
|
|
61
|
+
re: /require\(\s*(Buffer\.from|atob)\(|module\.constructor\._load|globalThis\[['"]require|require\(\s*_0x[a-f0-9]/,
|
|
62
|
+
what: "obfuscated module load (require of a base64/hex-decoded name)",
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
key: "exfil-url",
|
|
66
|
+
severity: "high",
|
|
67
|
+
nearZeroFp: false,
|
|
68
|
+
pattern: EXFIL_HOSTS,
|
|
69
|
+
re: new RegExp(EXFIL_HOSTS, "i"),
|
|
70
|
+
what: "beacon/exfiltration endpoint (webhook / paste / OOB-callback service)",
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
key: "exfil-ip",
|
|
74
|
+
severity: "medium", // raw-IP URLs have some legit uses (local tooling) — escalates via co-occurrence
|
|
75
|
+
nearZeroFp: false,
|
|
76
|
+
pattern: "https?://[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}",
|
|
77
|
+
re: /https?:\/\/(?!127\.|0\.0\.0\.0|10\.|192\.168\.|169\.254\.|172\.(1[6-9]|2\d|3[01])\.)[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/i, // /i: rg sweeps -i; URL schemes are case-insensitive (HTTP:// is a known evasion)
|
|
78
|
+
what: "hardcoded raw-IP URL (loopback/private/link-local ranges excluded)", // 169.254.169.254 = cloud IMDS (AWS/GCP/Azure SDK metadata) — benign, was a hot FP
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
key: "cloud-metadata-url",
|
|
82
|
+
severity: "medium",
|
|
83
|
+
nearZeroFp: false,
|
|
84
|
+
pattern: "169\\.254\\.169\\.254|metadata\\.google\\.internal|metadata\\.azure\\.com|latest/meta-data|metadata/identity/oauth2/token|https?://metadata/computeMetadata",
|
|
85
|
+
re: /169\.254\.169\.254|metadata\.google\.internal|metadata\.azure\.com|latest\/meta-data|metadata\/identity\/oauth2\/token|https?:\/\/metadata\/computeMetadata/i,
|
|
86
|
+
what: "cloud metadata service URL (credential-bearing endpoint; can be legit in cloud auth code)",
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
key: "network-url",
|
|
90
|
+
severity: "low",
|
|
91
|
+
nearZeroFp: false,
|
|
92
|
+
noisy: true,
|
|
93
|
+
pattern: "(fetch|axios\\.|XMLHttpRequest|sendBeacon|https?\\.request|request\\(|curl\\b|wget\\b).{0,220}https?://",
|
|
94
|
+
re: new RegExp(`\\b(fetch|axios\\.(get|post|put|patch|request)|XMLHttpRequest|sendBeacon|https?\\.request|http\\.request|request\\(|curl\\b|wget\\b)[\\s\\S]{0,220}?${PLAIN_EXTERNAL_URL}`, "i"),
|
|
95
|
+
what: "network call to hardcoded external URL",
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
key: "hardcoded-url",
|
|
99
|
+
severity: "low",
|
|
100
|
+
nearZeroFp: false,
|
|
101
|
+
noisy: true,
|
|
102
|
+
weak: true,
|
|
103
|
+
pattern: "https?://",
|
|
104
|
+
re: new RegExp(PLAIN_EXTERNAL_URL, "i"),
|
|
105
|
+
what: "hardcoded external URL in installed package source",
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
key: "env-exfil",
|
|
109
|
+
severity: "medium",
|
|
110
|
+
nearZeroFp: false,
|
|
111
|
+
pattern: "JSON\\.stringify\\(process\\.env\\)|Object\\.entries\\(process\\.env\\)|dict\\(os\\.environ\\)|os\\.environ\\.copy\\(\\)|os\\.Environ\\(\\)",
|
|
112
|
+
re: /JSON\.stringify\(process\.env\)|Object\.entries\(process\.env\)|dict\(os\.environ\)|os\.environ\.copy\(\)|os\.Environ\(\)/,
|
|
113
|
+
what: "whole-environment serialization (secrets harvesting favorite)",
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
key: "npm-token-or-publish",
|
|
117
|
+
severity: "high",
|
|
118
|
+
nearZeroFp: false,
|
|
119
|
+
pattern: "NPM_TOKEN|NODE_AUTH_TOKEN|\\.npmrc|npm\\s+(publish|token)|registry\\.npmjs\\.org",
|
|
120
|
+
re: /((NPM_TOKEN|NODE_AUTH_TOKEN|\.npmrc)[\s\S]{0,260}?(npm\s+(publish|token)|registry\.npmjs\.org)|((npm\s+(publish|token)|registry\.npmjs\.org)[\s\S]{0,260}?(NPM_TOKEN|NODE_AUTH_TOKEN|\.npmrc)))/i,
|
|
121
|
+
what: "npm registry token/publish behavior in an installed package",
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
key: "workflow-write",
|
|
125
|
+
severity: "high",
|
|
126
|
+
nearZeroFp: false,
|
|
127
|
+
pattern: "\\.github/workflows|workflow_dispatch",
|
|
128
|
+
re: /(writeFile(Sync)?|appendFile(Sync)?|copyFile(Sync)?|mkdir(Sync)?|createWriteStream|fs\.)[\s\S]{0,220}?\.github\/workflows|\.github\/workflows[\s\S]{0,220}?(writeFile(Sync)?|appendFile(Sync)?|copyFile(Sync)?|mkdir(Sync)?|createWriteStream|fs\.)/i,
|
|
129
|
+
what: "writes GitHub Actions workflow files from a dependency",
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
key: "python-obfuscated-exec",
|
|
133
|
+
severity: "high",
|
|
134
|
+
nearZeroFp: false,
|
|
135
|
+
pattern: "exec\\(|eval\\(|marshal\\.loads|base64\\.b64decode|zlib\\.decompress|codecs\\.decode",
|
|
136
|
+
re: /\b(exec|eval)\s*\(\s*(base64\.b64decode|b64decode|marshal\.loads|zlib\.decompress|codecs\.decode)\b|\bmarshal\.loads\s*\(|compile\s*\(\s*(base64\.b64decode|b64decode|zlib\.decompress|codecs\.decode)/i,
|
|
137
|
+
what: "Python decoded/marshaled payload execution",
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
key: "python-native-loader",
|
|
141
|
+
severity: "high",
|
|
142
|
+
nearZeroFp: false,
|
|
143
|
+
pattern: "ctypes|VirtualAlloc|mmap\\.PROT_EXEC|CFUNCTYPE|windll\\.kernel32",
|
|
144
|
+
re: /(ctypes|windll\.kernel32|CFUNCTYPE)[\s\S]{0,360}?(VirtualAlloc|mmap\.PROT_EXEC|memmove|CreateThread)|(VirtualAlloc|mmap\.PROT_EXEC|memmove|CreateThread)[\s\S]{0,360}?(ctypes|windll\.kernel32|CFUNCTYPE)/i,
|
|
145
|
+
what: "Python native-memory loader pattern",
|
|
146
|
+
},
|
|
147
|
+
{
|
|
148
|
+
key: "go-download-exec",
|
|
149
|
+
severity: "high",
|
|
150
|
+
nearZeroFp: false,
|
|
151
|
+
pattern: "exec\\.Command|http\\.Get|http\\.Client|os\\.StartProcess",
|
|
152
|
+
re: /(http\.(Get|Post|Client)|urlretrieve|io\.Copy)[\s\S]{0,420}?(exec\.Command|os\.StartProcess|syscall\.Exec)|(exec\.Command|os\.StartProcess|syscall\.Exec)[\s\S]{0,420}?(http\.(Get|Post|Client)|urlretrieve|io\.Copy)/i,
|
|
153
|
+
what: "download plus process execution in Go/Python package code",
|
|
154
|
+
},
|
|
155
|
+
{
|
|
156
|
+
key: "destructive-command",
|
|
157
|
+
severity: "critical",
|
|
158
|
+
nearZeroFp: true,
|
|
159
|
+
pattern: "rm\\s+-rf\\s+/|mkfs\\.|dd\\s+.*of=/dev/|Remove-Item\\s+.*-Recurse|del\\s+/[sq]",
|
|
160
|
+
re: /\brm\s+-rf\s+\/(?:\s|$)|\bmkfs\.[a-z0-9]+\b|\bdd\s+[^\n]*(of=\/dev\/(sd|hd|vd|nvme|disk)|if=\/dev\/zero)|Remove-Item\s+[^\n]*-Recurse|\bdel\s+\/[sq]\b/i,
|
|
161
|
+
what: "destructive filesystem command embedded in a dependency",
|
|
162
|
+
},
|
|
163
|
+
{
|
|
164
|
+
key: "crypto-clipper",
|
|
165
|
+
severity: "high",
|
|
166
|
+
nearZeroFp: false,
|
|
167
|
+
pattern: "clipboard|writeText|execCommand\\(['\"]copy|bc1[a-z0-9]{11}|0x[a-fA-F0-9]{40}",
|
|
168
|
+
re: /(clipboard|writeText|execCommand\(['"]copy|clipboardy)[\s\S]{0,520}?\b(0x[a-fA-F0-9]{40}|bc1[ac-hj-np-z02-9]{11,71}|[13][a-km-zA-HJ-NP-Z1-9]{25,34})\b|\b(0x[a-fA-F0-9]{40}|bc1[ac-hj-np-z02-9]{11,71}|[13][a-km-zA-HJ-NP-Z1-9]{25,34})\b[\s\S]{0,520}?(clipboard|writeText|execCommand\(['"]copy|clipboardy)/i,
|
|
169
|
+
what: "clipboard rewrite paired with a cryptocurrency wallet address",
|
|
170
|
+
},
|
|
171
|
+
{
|
|
172
|
+
key: "hidden-unicode",
|
|
173
|
+
severity: "medium",
|
|
174
|
+
nearZeroFp: false,
|
|
175
|
+
pattern: "[\\x{202A}-\\x{202E}\\x{2066}-\\x{2069}\\x{FE00}-\\x{FE0F}]",
|
|
176
|
+
re: new RegExp("\\u202A|\\u202B|\\u202C|\\u202D|\\u202E|\\u2066|\\u2067|\\u2068|\\u2069|\\uFE00|\\uFE01|\\uFE02|\\uFE03|\\uFE04|\\uFE05|\\uFE06|\\uFE07|\\uFE08|\\uFE09|\\uFE0A|\\uFE0B|\\uFE0C|\\uFE0D|\\uFE0E|\\uFE0F", "u"),
|
|
177
|
+
what: "hidden Unicode control/variation character in package source",
|
|
178
|
+
},
|
|
179
|
+
{
|
|
180
|
+
key: "obfuscated-code",
|
|
181
|
+
severity: "low", // minified/legit code look-alikes — only escalates with a second signal
|
|
182
|
+
nearZeroFp: false,
|
|
183
|
+
noisy: true,
|
|
184
|
+
pattern: "eval\\(atob\\(|eval\\(Buffer\\.from\\(|_0x[a-f0-9]{4}",
|
|
185
|
+
re: /eval\(atob\(|eval\(Buffer\.from\([^)]*base64|_0x[a-f0-9]{4,}/,
|
|
186
|
+
what: "obfuscation marker (eval-of-decoded payload / string-array obfuscator)",
|
|
187
|
+
},
|
|
188
|
+
];
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// Typosquat / name-confusion detection: is a package name a near-miss of a popular one (a classic
|
|
2
|
+
// supply-chain lure — crossenv↔cross-env, lodahs↔lodash)? Damerau-Levenshtein (handles transposition)
|
|
3
|
+
// against a bundled top-package list. Deliberately LOW-FP: many legit packages sit distance-1 from a
|
|
4
|
+
// popular one, so the caller GATES this — transitive deps only surface when a second malware signal
|
|
5
|
+
// co-fires (scanMalware), direct deps surface quietly at info level. DEPS_SECURITY_PLAN P6.
|
|
6
|
+
|
|
7
|
+
// Popular + historically-typosquatted npm names. Not exhaustive — the high-value lure targets.
|
|
8
|
+
export const TOP_PACKAGES = new Set([
|
|
9
|
+
"react", "react-dom", "lodash", "express", "axios", "chalk", "commander", "debug", "moment", "dayjs",
|
|
10
|
+
"webpack", "babel", "eslint", "prettier", "typescript", "jest", "mocha", "chai", "vue", "angular",
|
|
11
|
+
"next", "nuxt", "svelte", "vite", "rollup", "esbuild", "dotenv", "cors", "body-parser", "mongoose",
|
|
12
|
+
"mongodb", "mysql", "mysql2", "pg", "redis", "ioredis", "sequelize", "knex", "prisma", "socket.io",
|
|
13
|
+
"ws", "node-fetch", "got", "request", "superagent", "cross-env", "cross-spawn", "rimraf", "glob",
|
|
14
|
+
"fs-extra", "chokidar", "nodemon", "concurrently", "husky", "lint-staged", "uuid", "nanoid", "bcrypt",
|
|
15
|
+
"bcryptjs", "jsonwebtoken", "passport", "joi", "yup", "zod", "ajv", "classnames", "styled-components",
|
|
16
|
+
"tailwindcss", "postcss", "autoprefixer", "sass", "less", "react-router", "react-router-dom", "redux",
|
|
17
|
+
"react-redux", "@reduxjs/toolkit", "mobx", "recoil", "zustand", "formik", "react-hook-form", "immer",
|
|
18
|
+
"rxjs", "date-fns", "ramda", "underscore", "immutable", "yargs", "inquirer", "ora", "boxen", "chalk",
|
|
19
|
+
"colors", "winston", "pino", "morgan", "helmet", "compression", "cookie-parser", "express-session",
|
|
20
|
+
"multer", "sharp", "jimp", "puppeteer", "playwright", "cypress", "supertest", "sinon", "nock",
|
|
21
|
+
"ts-node", "tslib", "core-js", "regenerator-runtime", "@babel/core", "babel-loader", "css-loader",
|
|
22
|
+
"style-loader", "html-webpack-plugin", "terser", "browserify", "gulp", "grunt", "electron", "three",
|
|
23
|
+
"d3", "chart.js", "echarts", "leaflet", "mapbox-gl", "graphql", "apollo-server", "@apollo/client",
|
|
24
|
+
"protobufjs", "grpc", "kafkajs", "amqplib", "bull", "node-cron", "cheerio", "jsdom", "marked",
|
|
25
|
+
"markdown-it", "highlight.js", "prismjs", "qs", "querystring", "form-data", "semver", "minimist",
|
|
26
|
+
]);
|
|
27
|
+
|
|
28
|
+
// Legit close pairs to NOT flag (real distinct packages that happen to sit distance-1/2 apart).
|
|
29
|
+
const KNOWN_LEGIT = new Set([
|
|
30
|
+
"cross-spawn", "react-dom", "react-router", "bcryptjs", "mysql2", "colors", "underscore", "querystring",
|
|
31
|
+
"markdown-it", "babel-loader", "css-loader", "style-loader",
|
|
32
|
+
]);
|
|
33
|
+
|
|
34
|
+
const norm = (name) => String(name || "").toLowerCase().replace(/^@[^/]+\//, ""); // drop scope for the compare
|
|
35
|
+
|
|
36
|
+
// Damerau-Levenshtein (optimal string alignment) — includes adjacent transposition.
|
|
37
|
+
export function damerau(a, b) {
|
|
38
|
+
a = String(a); b = String(b);
|
|
39
|
+
const m = a.length, n = b.length;
|
|
40
|
+
if (!m) return n;
|
|
41
|
+
if (!n) return m;
|
|
42
|
+
const d = Array.from({ length: m + 1 }, () => new Array(n + 1).fill(0));
|
|
43
|
+
for (let i = 0; i <= m; i++) d[i][0] = i;
|
|
44
|
+
for (let j = 0; j <= n; j++) d[0][j] = j;
|
|
45
|
+
for (let i = 1; i <= m; i++) {
|
|
46
|
+
for (let j = 1; j <= n; j++) {
|
|
47
|
+
const cost = a[i - 1] === b[j - 1] ? 0 : 1;
|
|
48
|
+
d[i][j] = Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost);
|
|
49
|
+
if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + 1);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return d[m][n];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// → { nearest, distance } if `name` looks like a typosquat of a popular package, else null.
|
|
56
|
+
// Threshold: distance 1 always; distance 2 only for names ≥8 chars (short names collide too easily).
|
|
57
|
+
// Skip: exact popular names, known-legit pairs, names <4 chars.
|
|
58
|
+
export function classifyTyposquat(name) {
|
|
59
|
+
const raw = String(name || "");
|
|
60
|
+
const n = norm(raw);
|
|
61
|
+
if (n.length < 4 || TOP_PACKAGES.has(raw) || TOP_PACKAGES.has(n) || KNOWN_LEGIT.has(raw) || KNOWN_LEGIT.has(n)) return null;
|
|
62
|
+
let best = null;
|
|
63
|
+
for (const top of TOP_PACKAGES) {
|
|
64
|
+
const t = norm(top);
|
|
65
|
+
if (t === n) return null; // scope-only difference of a popular name → not a squat
|
|
66
|
+
const dist = damerau(n, t);
|
|
67
|
+
if (dist === 0) return null;
|
|
68
|
+
const limit = t.length >= 8 ? 2 : 1;
|
|
69
|
+
if (dist <= limit && (!best || dist < best.distance)) best = { nearest: top, distance: dist };
|
|
70
|
+
}
|
|
71
|
+
return best;
|
|
72
|
+
}
|
package/src/util.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// Shared fs/text helpers.
|
|
2
|
+
import { readFileSync, statSync } from "node:fs";
|
|
3
|
+
|
|
4
|
+
// Bounded, never-throwing file read shared by the source scanners (infra, endpoints): oversized
|
|
5
|
+
// files are skipped, not truncated — a partial read would produce misleading matches.
|
|
6
|
+
export const MAX_FILE_BYTES = 512 * 1024;
|
|
7
|
+
|
|
8
|
+
// Order-preserving dedupe by a derived key — first occurrence wins.
|
|
9
|
+
export function uniqueBy(list, keyFn) {
|
|
10
|
+
const seen = new Set();
|
|
11
|
+
return list.filter((item) => {
|
|
12
|
+
const k = keyFn(item);
|
|
13
|
+
if (seen.has(k)) return false;
|
|
14
|
+
seen.add(k);
|
|
15
|
+
return true;
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function safeRead(path) {
|
|
20
|
+
try {
|
|
21
|
+
const st = statSync(path);
|
|
22
|
+
if (!st.isFile() || st.size > MAX_FILE_BYTES) return "";
|
|
23
|
+
return readFileSync(path, "utf8");
|
|
24
|
+
} catch {
|
|
25
|
+
return "";
|
|
26
|
+
}
|
|
27
|
+
}
|