@acepaste/ingress 0.0.0-stage → 0.7.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 +105 -0
- package/README.md +15 -2
- package/dist/src/acepaste-alphabet.d.ts +26 -0
- package/dist/src/acepaste-alphabet.js +47 -0
- package/dist/src/acquire.d.ts +24 -0
- package/dist/src/acquire.js +146 -0
- package/dist/src/adapters.d.ts +30 -0
- package/dist/src/adapters.js +160 -0
- package/dist/src/character.d.ts +22 -0
- package/dist/src/character.js +291 -0
- package/dist/src/cli.d.ts +2 -0
- package/dist/src/cli.js +63 -0
- package/dist/src/color.d.ts +26 -0
- package/dist/src/color.js +111 -0
- package/dist/src/engine.d.ts +108 -0
- package/dist/src/engine.js +612 -0
- package/dist/src/evidence.d.ts +21 -0
- package/dist/src/evidence.js +77 -0
- package/dist/src/index.d.ts +26 -0
- package/dist/src/index.js +19 -0
- package/dist/src/meaning.d.ts +28 -0
- package/dist/src/meaning.js +226 -0
- package/dist/src/netpolicy.d.ts +34 -0
- package/dist/src/netpolicy.js +122 -0
- package/dist/src/normalize.d.ts +34 -0
- package/dist/src/normalize.js +204 -0
- package/dist/src/policyfile.d.ts +2 -0
- package/dist/src/policyfile.js +50 -0
- package/dist/src/sandbox.d.ts +45 -0
- package/dist/src/sandbox.js +231 -0
- package/dist/src/script.d.ts +23 -0
- package/dist/src/script.js +220 -0
- package/dist/src/secrets.d.ts +24 -0
- package/dist/src/secrets.js +192 -0
- package/dist/src/supplychain.d.ts +24 -0
- package/dist/src/supplychain.js +182 -0
- package/dist/src/taint.d.ts +37 -0
- package/dist/src/taint.js +39 -0
- package/dist/src/types.d.ts +176 -0
- package/dist/src/types.js +25 -0
- package/dist/src/visibility.d.ts +41 -0
- package/dist/src/visibility.js +424 -0
- package/package.json +64 -4
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Script pass (underlight lineage).
|
|
3
|
+
*
|
|
4
|
+
* Static, millisecond-budget analysis of every script on the page. It does
|
|
5
|
+
* not execute anything. A high score marks the page for sandbox escalation
|
|
6
|
+
* (sandbox not yet implemented; the hook is in engine.ts).
|
|
7
|
+
*
|
|
8
|
+
* Surfaces, from the underlight list: obfuscation and minification signatures,
|
|
9
|
+
* runtime code generation, typed-array / binary-buffer decoding, automation
|
|
10
|
+
* detection (agent-targeted cloaking), post-load text injection, and outbound
|
|
11
|
+
* beacons that could carry exfiltrated context.
|
|
12
|
+
*/
|
|
13
|
+
import * as acorn from "acorn";
|
|
14
|
+
import * as walk from "acorn-walk";
|
|
15
|
+
const AUTOMATION_PROBES = [
|
|
16
|
+
"webdriver", "__webdriver", "_phantom", "callPhantom", "__nightmare", "__selenium", "domAutomation",
|
|
17
|
+
"HeadlessChrome", "puppeteer", "playwright", "__playwright", "chrome.runtime", "navigator.plugins.length",
|
|
18
|
+
"navigator.languages.length", "outerWidth", "connection.rtt", "permissions.query", "notifications",
|
|
19
|
+
"GPTBot", "ClaudeBot", "Claude-Web", "anthropic-ai", "PerplexityBot", "ChatGPT-User", "OAI-SearchBot", "Google-Extended", "cohere-ai", "Bytespider",
|
|
20
|
+
];
|
|
21
|
+
const EXFIL_SINKS = ["sendBeacon", "XMLHttpRequest", "fetch", "WebSocket", "EventSource"];
|
|
22
|
+
const TEXT_SINKS = ["innerHTML", "outerHTML", "textContent", "innerText", "insertAdjacentHTML", "write", "writeln", "appendChild", "replaceChildren", "createTextNode"];
|
|
23
|
+
const CODEGEN = ["eval", "Function", "setTimeout", "setInterval", "execScript", "importScripts"];
|
|
24
|
+
const BINARY_DECODE = ["TextDecoder", "Uint8Array", "Uint16Array", "Int8Array", "DataView", "ArrayBuffer", "atob", "fromCharCode", "fromCodePoint", "decodeURIComponent", "unescape", "charCodeAt"];
|
|
25
|
+
function shannon(s) {
|
|
26
|
+
if (!s)
|
|
27
|
+
return 0;
|
|
28
|
+
const freq = new Map();
|
|
29
|
+
for (const c of s)
|
|
30
|
+
freq.set(c, (freq.get(c) ?? 0) + 1);
|
|
31
|
+
let h = 0;
|
|
32
|
+
for (const n of freq.values()) {
|
|
33
|
+
const p = n / s.length;
|
|
34
|
+
h -= p * Math.log2(p);
|
|
35
|
+
}
|
|
36
|
+
return h;
|
|
37
|
+
}
|
|
38
|
+
function memberChain(node) {
|
|
39
|
+
if (!node)
|
|
40
|
+
return "";
|
|
41
|
+
if (node.type === "Identifier")
|
|
42
|
+
return node.name;
|
|
43
|
+
if (node.type === "ThisExpression")
|
|
44
|
+
return "this";
|
|
45
|
+
if (node.type === "MemberExpression") {
|
|
46
|
+
const prop = node.computed ? (node.property.type === "Literal" ? String(node.property.value) : "[]") : node.property.name;
|
|
47
|
+
return `${memberChain(node.object)}.${prop}`;
|
|
48
|
+
}
|
|
49
|
+
if (node.type === "CallExpression")
|
|
50
|
+
return memberChain(node.callee) + "()";
|
|
51
|
+
return node.type;
|
|
52
|
+
}
|
|
53
|
+
export function analyzeScript(unit) {
|
|
54
|
+
const findings = [];
|
|
55
|
+
const src = unit.source;
|
|
56
|
+
const loc = unit.inline ? `inline script #${unit.index}` : `script #${unit.index} src=${unit.src}`;
|
|
57
|
+
if (!src.trim())
|
|
58
|
+
return { findings, suspicion: 0 };
|
|
59
|
+
// --- lexical signals, computed before parsing (obfuscated code may not parse)
|
|
60
|
+
const hexIdents = (src.match(/\b_0x[0-9a-f]{2,}\b/g) ?? []).length;
|
|
61
|
+
const escapes = (src.match(/\\x[0-9a-fA-F]{2}|\\u[0-9a-fA-F]{4}/g) ?? []).length;
|
|
62
|
+
const longStrings = (src.match(/["'`][^"'`\n]{200,}["'`]/g) ?? []);
|
|
63
|
+
const longStringEntropy = longStrings.length ? Math.max(...longStrings.map(shannon)) : 0;
|
|
64
|
+
const bracketAccess = (src.match(/\[\s*["'][A-Za-z_$][\w$]*["']\s*\]/g) ?? []).length;
|
|
65
|
+
const jsfuck = /(\[\]|!\[\]|\+\[\]|\(!\[\]\+\[\]\))\s*[\[\+]/.test(src) && (src.match(/\[\]/g) ?? []).length > 40;
|
|
66
|
+
const nonAscii = ([...src].filter((c) => c.charCodeAt(0) > 127).length) / Math.max(1, src.length);
|
|
67
|
+
let obf = 0;
|
|
68
|
+
// hex identifiers are the javascript-obfuscator.io signature; even a few are diagnostic
|
|
69
|
+
obf += hexIdents >= 3 ? Math.min(0.4, 0.2 + hexIdents / 40) : 0;
|
|
70
|
+
obf += Math.min(0.25, escapes / 80);
|
|
71
|
+
obf += longStringEntropy > 5.2 ? 0.2 : 0;
|
|
72
|
+
obf += Math.min(0.15, bracketAccess / 60);
|
|
73
|
+
obf += jsfuck ? 0.5 : 0;
|
|
74
|
+
obf += nonAscii > 0.05 ? 0.1 : 0;
|
|
75
|
+
// minified-but-legitimate bundles have short identifiers but no hex idents or escapes; keep obf low for them
|
|
76
|
+
if (obf >= 0.3) {
|
|
77
|
+
findings.push({
|
|
78
|
+
pass: "script", kind: "script.obfuscation", confidence: Math.min(0.9, obf), severity: 0.5,
|
|
79
|
+
message: `obfuscation signatures: ${hexIdents} hex identifiers, ${escapes} escapes, string entropy ${longStringEntropy.toFixed(2)}${jsfuck ? ", symbol-only encoding" : ""}`,
|
|
80
|
+
location: loc, action: "none",
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
// --- structural signals via AST
|
|
84
|
+
let ast;
|
|
85
|
+
try {
|
|
86
|
+
ast = acorn.parse(src, { ecmaVersion: "latest", sourceType: "script", allowHashBang: true, allowReturnOutsideFunction: true });
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
try {
|
|
90
|
+
ast = acorn.parse(src, { ecmaVersion: "latest", sourceType: "module" });
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
ast = null;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
let codegen = 0, automation = 0, textInject = 0, exfil = 0, binary = 0;
|
|
97
|
+
const automationHits = new Set();
|
|
98
|
+
const exfilHits = new Set();
|
|
99
|
+
const urlLiterals = new Set();
|
|
100
|
+
if (ast) {
|
|
101
|
+
walk.simple(ast, {
|
|
102
|
+
CallExpression(node) {
|
|
103
|
+
const chain = memberChain(node.callee);
|
|
104
|
+
const tail = chain.split(".").pop() ?? "";
|
|
105
|
+
if (tail === "eval" || tail === "Function" || tail === "execScript")
|
|
106
|
+
codegen += 1;
|
|
107
|
+
if ((tail === "setTimeout" || tail === "setInterval") && node.arguments[0]?.type !== "FunctionExpression" && node.arguments[0]?.type !== "ArrowFunctionExpression" && node.arguments[0]?.type !== "Identifier")
|
|
108
|
+
codegen += 0.6;
|
|
109
|
+
if (EXFIL_SINKS.includes(tail) || chain.endsWith("navigator.sendBeacon") || chain === "new Image") {
|
|
110
|
+
exfil += 1;
|
|
111
|
+
exfilHits.add(tail);
|
|
112
|
+
}
|
|
113
|
+
if (TEXT_SINKS.includes(tail))
|
|
114
|
+
textInject += 0.5;
|
|
115
|
+
if (BINARY_DECODE.includes(tail))
|
|
116
|
+
binary += 0.5;
|
|
117
|
+
if (tail === "fromCharCode" && node.arguments.length > 8)
|
|
118
|
+
binary += 1.5;
|
|
119
|
+
},
|
|
120
|
+
NewExpression(node) {
|
|
121
|
+
const name = memberChain(node.callee);
|
|
122
|
+
if (name === "Function")
|
|
123
|
+
codegen += 1;
|
|
124
|
+
if (BINARY_DECODE.includes(name))
|
|
125
|
+
binary += 0.5;
|
|
126
|
+
if (name === "Image" || name === "WebSocket" || name === "XMLHttpRequest") {
|
|
127
|
+
exfil += 0.5;
|
|
128
|
+
exfilHits.add(name);
|
|
129
|
+
}
|
|
130
|
+
},
|
|
131
|
+
MemberExpression(node) {
|
|
132
|
+
const chain = memberChain(node);
|
|
133
|
+
for (const probe of AUTOMATION_PROBES)
|
|
134
|
+
if (chain.includes(probe)) {
|
|
135
|
+
automation += 1;
|
|
136
|
+
automationHits.add(probe);
|
|
137
|
+
}
|
|
138
|
+
if (/\.(innerHTML|outerHTML|textContent|innerText)$/.test(chain))
|
|
139
|
+
textInject += 0.5;
|
|
140
|
+
if (chain.endsWith(".src") || chain.endsWith(".href"))
|
|
141
|
+
exfil += 0.1;
|
|
142
|
+
},
|
|
143
|
+
Literal(node) {
|
|
144
|
+
if (node.regex) {
|
|
145
|
+
for (const probe of AUTOMATION_PROBES)
|
|
146
|
+
if (node.regex.pattern.includes(probe)) {
|
|
147
|
+
automation += 0.8;
|
|
148
|
+
automationHits.add(probe);
|
|
149
|
+
}
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
if (typeof node.value !== "string")
|
|
153
|
+
return;
|
|
154
|
+
for (const probe of AUTOMATION_PROBES)
|
|
155
|
+
if (node.value.includes(probe)) {
|
|
156
|
+
automation += 0.8;
|
|
157
|
+
automationHits.add(probe);
|
|
158
|
+
}
|
|
159
|
+
if (/^(https?:)?\/\/[^/]+\/(collect|beacon|track|pixel|log|t\.gif|p\.gif|hook|webhook)/i.test(node.value)) {
|
|
160
|
+
exfil += 0.8;
|
|
161
|
+
exfilHits.add(node.value.slice(0, 60));
|
|
162
|
+
}
|
|
163
|
+
if (/^https?:\/\/[^\s"']+$/.test(node.value))
|
|
164
|
+
urlLiterals.add(node.value.slice(0, 120));
|
|
165
|
+
},
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
// unparseable: treat lexical hits as structural
|
|
170
|
+
for (const p of AUTOMATION_PROBES)
|
|
171
|
+
if (src.includes(p)) {
|
|
172
|
+
automation += 1;
|
|
173
|
+
automationHits.add(p);
|
|
174
|
+
}
|
|
175
|
+
if (/\beval\s*\(|new\s+Function\s*\(/.test(src))
|
|
176
|
+
codegen += 1;
|
|
177
|
+
obf += 0.2;
|
|
178
|
+
}
|
|
179
|
+
if (codegen >= 1)
|
|
180
|
+
findings.push({
|
|
181
|
+
pass: "script", kind: "script.runtime_codegen", confidence: Math.min(0.85, 0.4 + codegen * 0.15), severity: 0.6,
|
|
182
|
+
message: `runtime code generation (${Math.round(codegen)} sites: eval / new Function / string timers)`, location: loc, action: "none",
|
|
183
|
+
});
|
|
184
|
+
if (automation >= 1)
|
|
185
|
+
findings.push({
|
|
186
|
+
pass: "script", kind: "script.automation_detection", confidence: Math.min(0.95, 0.45 + automation * 0.12), severity: 0.85,
|
|
187
|
+
message: `probes for automation or AI crawlers: ${[...automationHits].slice(0, 6).join(", ")}`, excerpt: [...urlLiterals].slice(0, 5).join(" "), location: loc, action: "none",
|
|
188
|
+
});
|
|
189
|
+
if (textInject >= 2 && (codegen >= 1 || binary >= 2 || obf >= 0.3))
|
|
190
|
+
findings.push({
|
|
191
|
+
pass: "script", kind: "script.post_load_text_injection", confidence: 0.6, severity: 0.7,
|
|
192
|
+
message: `writes text into the document after load, combined with decoding or code generation`, location: loc, action: "none",
|
|
193
|
+
});
|
|
194
|
+
if (exfil >= 1.5 && (textInject >= 1 || binary >= 1 || automation >= 1))
|
|
195
|
+
findings.push({
|
|
196
|
+
pass: "script", kind: "script.exfil_beacon", confidence: Math.min(0.8, 0.3 + exfil * 0.1), severity: 0.6,
|
|
197
|
+
message: `outbound channels present: ${[...exfilHits].slice(0, 4).join(", ")}`, excerpt: [...urlLiterals].slice(0, 5).join(" "), location: loc, action: "none",
|
|
198
|
+
});
|
|
199
|
+
if (binary >= 3)
|
|
200
|
+
findings.push({
|
|
201
|
+
pass: "script", kind: "script.binary_decoding", confidence: Math.min(0.8, 0.3 + binary * 0.08), severity: 0.5,
|
|
202
|
+
message: `typed-array / char-code decoding of embedded data (${Math.round(binary)} sites)`, location: loc, action: "none",
|
|
203
|
+
});
|
|
204
|
+
// suspicion for sandbox escalation: automation detection and codegen+textinject are the strongest tells
|
|
205
|
+
const suspicion = Math.min(1, obf * 0.6 +
|
|
206
|
+
(automation >= 1 ? 0.5 : 0) +
|
|
207
|
+
(codegen >= 1 && textInject >= 1 ? 0.35 : 0) +
|
|
208
|
+
(binary >= 3 ? 0.15 : 0));
|
|
209
|
+
return { findings, suspicion };
|
|
210
|
+
}
|
|
211
|
+
export function analyzeScripts(units) {
|
|
212
|
+
const findings = [];
|
|
213
|
+
let maxSuspicion = 0;
|
|
214
|
+
for (const u of units) {
|
|
215
|
+
const r = analyzeScript(u);
|
|
216
|
+
findings.push(...r.findings);
|
|
217
|
+
maxSuspicion = Math.max(maxSuspicion, r.suspicion);
|
|
218
|
+
}
|
|
219
|
+
return { findings, maxSuspicion };
|
|
220
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Secrets pass (Kith & Revere lineage, ported 2026-10-31).
|
|
3
|
+
*
|
|
4
|
+
* Kith & Revere's rule for credentials: name the kind of secret and where it
|
|
5
|
+
* is, never print the secret itself. The gate applies the same rule at the
|
|
6
|
+
* ingestion boundary. A credential in fetched content is either a leak (it
|
|
7
|
+
* should not land in a model's context, logs or training data) or bait (a
|
|
8
|
+
* planted key with instructions to use it). Either way the gate redacts it
|
|
9
|
+
* from everything it emits: delivered text, finding excerpts, the attack log
|
|
10
|
+
* and the public feed. Detection alone does not refuse a page; a credential
|
|
11
|
+
* in hidden text does count against it.
|
|
12
|
+
*/
|
|
13
|
+
import type { Finding, TextSegment } from "./types.js";
|
|
14
|
+
/** The literal credential values present in these texts, for scrubbing excerpts that were cut or altered before redaction. */
|
|
15
|
+
export declare function collectSecrets(texts: ReadonlyArray<string>): string[];
|
|
16
|
+
/**
|
|
17
|
+
* Make an excerpt safe to log or publish. Redacts detected credentials, then removes any fragment of a known
|
|
18
|
+
* credential (an excerpt cut mid-token, or with an invisible character swapped for a marker, can no longer match
|
|
19
|
+
* a pattern on its own). Works on a copy with invisible characters and replacement markers removed.
|
|
20
|
+
*/
|
|
21
|
+
export declare function scrubExcerpt(text: string, known: ReadonlyArray<string>): string;
|
|
22
|
+
/** Replace every detected credential with a label naming its kind. Never reversible. */
|
|
23
|
+
export declare function redactSecrets(text: string): string;
|
|
24
|
+
export declare function scanSecrets(segments: ReadonlyArray<TextSegment>, decodedPayloads: ReadonlyArray<string>): Finding[];
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Secrets pass (Kith & Revere lineage, ported 2026-10-31).
|
|
3
|
+
*
|
|
4
|
+
* Kith & Revere's rule for credentials: name the kind of secret and where it
|
|
5
|
+
* is, never print the secret itself. The gate applies the same rule at the
|
|
6
|
+
* ingestion boundary. A credential in fetched content is either a leak (it
|
|
7
|
+
* should not land in a model's context, logs or training data) or bait (a
|
|
8
|
+
* planted key with instructions to use it). Either way the gate redacts it
|
|
9
|
+
* from everything it emits: delivered text, finding excerpts, the attack log
|
|
10
|
+
* and the public feed. Detection alone does not refuse a page; a credential
|
|
11
|
+
* in hidden text does count against it.
|
|
12
|
+
*/
|
|
13
|
+
// Every quantifier is bounded and every pattern has a cheap literal pre-filter: this pass runs on attacker-supplied
|
|
14
|
+
// text, so no pattern may be able to scan quadratically ("a.a.a.…", "sk-sk-sk-…", an unterminated key block).
|
|
15
|
+
// A token ends at the first character that cannot continue it, not at a word boundary ("ghp_…_backup" still matches).
|
|
16
|
+
const END = String.raw `(?![A-Za-z0-9])`;
|
|
17
|
+
const PATTERNS = [
|
|
18
|
+
{ name: "AWS access key id", pre: /A[KSBC][IC]A/, re: new RegExp(String.raw `(?<![A-Za-z0-9])(AKIA|ASIA|ABIA|ACCA)[0-9A-Z]{16}` + END, "g") },
|
|
19
|
+
{ name: "AWS secret access key", pre: /secret_?access_?key/i, re: /(aws_secret_access_key\s{0,3}[=:]\s{0,3}["']?|"SecretAccessKey"\s{0,3}:\s{0,3}")[A-Za-z0-9/+=]{40}(?![A-Za-z0-9/+=])/gi },
|
|
20
|
+
{ name: "GitHub token", pre: /gh[pousr]_|github_pat_/, re: new RegExp(String.raw `(?<![A-Za-z0-9])((ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9]{36,80}|github_pat_[A-Za-z0-9_]{60,120})` + END, "g") },
|
|
21
|
+
{ name: "GitLab token", pre: /glpat-/, re: new RegExp(String.raw `glpat-[A-Za-z0-9_-]{20,64}` + END, "g") },
|
|
22
|
+
{ name: "Slack token", pre: /xox[abposr]-/, re: new RegExp(String.raw `xox[abposr]-[A-Za-z0-9-]{10,120}` + END, "g") },
|
|
23
|
+
{ name: "Stripe secret key", pre: /[sr]k_live_/, re: new RegExp(String.raw `(sk|rk)_live_[A-Za-z0-9]{20,120}` + END, "g") },
|
|
24
|
+
{ name: "npm token", pre: /npm_|_authToken/, re: /(?<![A-Za-z0-9])npm_[A-Za-z0-9]{36}(?![A-Za-z0-9])|_authToken\s{0,3}=\s{0,3}[^\s"']{20,200}/g },
|
|
25
|
+
{ name: "Anthropic API key", pre: /sk-ant-/, re: new RegExp(String.raw `sk-ant-[A-Za-z0-9_-]{30,200}` + END, "g") },
|
|
26
|
+
{ name: "OpenAI API key", pre: /sk-/, re: new RegExp(String.raw `(?<![A-Za-z0-9-])sk-(proj-|svcacct-)?[A-Za-z0-9_-]{40,200}` + END, "g") },
|
|
27
|
+
{ name: "Hugging Face token", pre: /hf_/, re: new RegExp(String.raw `(?<![A-Za-z0-9])hf_[A-Za-z0-9]{34,40}` + END, "g") },
|
|
28
|
+
{ name: "SendGrid key", pre: /SG\./, re: /(?<![A-Za-z0-9])SG\.[A-Za-z0-9_-]{22}\.[A-Za-z0-9_-]{43}(?![A-Za-z0-9_-])/g },
|
|
29
|
+
{ name: "Google API key", pre: /AIza/, re: new RegExp(String.raw `AIza[0-9A-Za-z_-]{35}` + END, "g") },
|
|
30
|
+
{ name: "private key block", pre: /PRIVATE KEY/, re: /-----BEGIN (RSA |EC |OPENSSH |DSA |PGP |ENCRYPTED )?PRIVATE KEY( BLOCK)?-----[\s\S]{20,16000}?-----END (RSA |EC |OPENSSH |DSA |PGP |ENCRYPTED )?PRIVATE KEY( BLOCK)?-----/g },
|
|
31
|
+
{ name: "credential in URL", pre: /:\/\/[^\s/@]{0,200}:[^\s/@]{0,200}@/, re: /(?<![A-Za-z0-9+.-])[a-z][a-z0-9+.-]{0,20}:\/\/([^\s:/@]{2,64}):([^\s:/@]{6,128})@([^\s/:]{1,253})/gi },
|
|
32
|
+
{ name: "Kubernetes credential", pre: /client-(key|certificate)-data/, re: /(client-key-data|client-certificate-data):\s{0,3}[A-Za-z0-9+/=]{40,20000}/g },
|
|
33
|
+
];
|
|
34
|
+
/** documentation placeholders are not secrets */
|
|
35
|
+
const PLACEHOLDER = /EXAMPLE|example|xxxx|XXXX|your[_-]?(key|token)|placeholder|dummy|redacted|\*{4,}|<[^>]+>|\.\.\.|0{12,}|1234567890/;
|
|
36
|
+
/** well-known development defaults are not credentials worth redacting */
|
|
37
|
+
const DEFAULT_PASSWORDS = /^(password|passwd|postgres|secret|changeme|root|admin|example|test|guest|pass|mysql|redis)$/i;
|
|
38
|
+
const LOCAL_HOST = /^(localhost|127\.\d+\.\d+\.\d+|0\.0\.0\.0|\[::1\]|db|postgres|mysql|redis|.*\.(local|localhost|internal|test|example))$/i;
|
|
39
|
+
function entropy(s) {
|
|
40
|
+
const f = new Map();
|
|
41
|
+
for (const c of s)
|
|
42
|
+
f.set(c, (f.get(c) ?? 0) + 1);
|
|
43
|
+
let h = 0;
|
|
44
|
+
for (const n of f.values()) {
|
|
45
|
+
const p = n / s.length;
|
|
46
|
+
h -= p * Math.log2(p);
|
|
47
|
+
}
|
|
48
|
+
return h;
|
|
49
|
+
}
|
|
50
|
+
function plausible(p, m) {
|
|
51
|
+
const v = m[0];
|
|
52
|
+
if (PLACEHOLDER.test(v))
|
|
53
|
+
return false;
|
|
54
|
+
if (p.name === "private key block" || p.name === "Kubernetes credential")
|
|
55
|
+
return true;
|
|
56
|
+
if (p.name === "credential in URL") {
|
|
57
|
+
const [, user, pass, host] = m;
|
|
58
|
+
if (LOCAL_HOST.test(host) || DEFAULT_PASSWORDS.test(pass) || user === pass)
|
|
59
|
+
return false;
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
const body = v.replace(/^[a-z_]+[-_]/i, "");
|
|
63
|
+
if (entropy(body) < 3.0)
|
|
64
|
+
return false;
|
|
65
|
+
if (p.name === "OpenAI API key") {
|
|
66
|
+
// a key mixes case and digits and has at most a few hyphens; a URL slug like "sk-hynix-posts-record-…" does not
|
|
67
|
+
const b = v.replace(/^sk-(proj-|svcacct-)?/, "");
|
|
68
|
+
if (!(/[a-z]/.test(b) && /[A-Z]/.test(b) && /\d/.test(b)) || (b.match(/-/g) ?? []).length > 4)
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
function findSecrets(text) {
|
|
74
|
+
const hits = [];
|
|
75
|
+
if (!text || text.length < 16)
|
|
76
|
+
return hits;
|
|
77
|
+
// private key blocks: a linear scan (each BEGIN is paired with the next END by index, never by backtracking)
|
|
78
|
+
if (text.includes("PRIVATE KEY-----")) {
|
|
79
|
+
const begin = /-----BEGIN (?:RSA |EC |OPENSSH |DSA |PGP |ENCRYPTED )?PRIVATE KEY(?: BLOCK)?-----/g;
|
|
80
|
+
let m;
|
|
81
|
+
let after = 0, cachedEnd = -2;
|
|
82
|
+
while ((m = begin.exec(text)) && hits.length < 500) {
|
|
83
|
+
if (m.index < after)
|
|
84
|
+
continue;
|
|
85
|
+
if (cachedEnd < m.index + m[0].length && cachedEnd !== -1)
|
|
86
|
+
cachedEnd = text.indexOf("-----END ", m.index + m[0].length);
|
|
87
|
+
const endAt = cachedEnd;
|
|
88
|
+
if (endAt < 0)
|
|
89
|
+
break;
|
|
90
|
+
const close = text.indexOf("-----", endAt + 9);
|
|
91
|
+
if (close < 0)
|
|
92
|
+
break;
|
|
93
|
+
const stop = close + 5;
|
|
94
|
+
const body = text.slice(m.index + m[0].length, endAt);
|
|
95
|
+
if (body.length >= 20 && body.length <= 16000 && !PLACEHOLDER.test(body) && !/-----BEGIN /.test(body)) {
|
|
96
|
+
hits.push({ name: "private key block", start: m.index, end: stop, value: text.slice(m.index, stop) });
|
|
97
|
+
after = stop;
|
|
98
|
+
}
|
|
99
|
+
begin.lastIndex = Math.max(begin.lastIndex, m.index + m[0].length);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
for (const p of PATTERNS) {
|
|
103
|
+
if (p.name === "private key block")
|
|
104
|
+
continue;
|
|
105
|
+
if (!p.pre.test(text))
|
|
106
|
+
continue;
|
|
107
|
+
p.re.lastIndex = 0;
|
|
108
|
+
for (const m of text.matchAll(p.re)) {
|
|
109
|
+
if (!plausible(p, m))
|
|
110
|
+
continue;
|
|
111
|
+
hits.push({ name: p.name, start: m.index, end: m.index + m[0].length, value: m[0] });
|
|
112
|
+
if (hits.length > 500)
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return hits.sort((a, b) => a.start - b.start).filter((h, i, arr) => i === 0 || h.start >= arr[i - 1].end);
|
|
117
|
+
}
|
|
118
|
+
/** The literal credential values present in these texts, for scrubbing excerpts that were cut or altered before redaction. */
|
|
119
|
+
export function collectSecrets(texts) {
|
|
120
|
+
const out = new Set();
|
|
121
|
+
for (const t of texts)
|
|
122
|
+
for (const h of findSecrets(t))
|
|
123
|
+
out.add(h.value);
|
|
124
|
+
return [...out];
|
|
125
|
+
}
|
|
126
|
+
const INVISIBLE_OR_REPLACEMENT = /[\u200B-\u200F\u2060-\u2064\uFEFF\u00AD\uFFFD\u{E0000}-\u{E007F}]/gu;
|
|
127
|
+
/**
|
|
128
|
+
* Make an excerpt safe to log or publish. Redacts detected credentials, then removes any fragment of a known
|
|
129
|
+
* credential (an excerpt cut mid-token, or with an invisible character swapped for a marker, can no longer match
|
|
130
|
+
* a pattern on its own). Works on a copy with invisible characters and replacement markers removed.
|
|
131
|
+
*/
|
|
132
|
+
export function scrubExcerpt(text, known) {
|
|
133
|
+
if (!text)
|
|
134
|
+
return text;
|
|
135
|
+
let out = redactSecrets(text);
|
|
136
|
+
const flat = out.replace(INVISIBLE_OR_REPLACEMENT, "");
|
|
137
|
+
let scrubbed = redactSecrets(flat);
|
|
138
|
+
for (const k of known) {
|
|
139
|
+
const plain = k.replace(INVISIBLE_OR_REPLACEMENT, "");
|
|
140
|
+
// any run of 10+ characters of the credential, which also covers a truncated head or tail
|
|
141
|
+
for (let len = Math.min(plain.length, 400); len >= 10; len = len > 40 ? len - 20 : len - 1) {
|
|
142
|
+
let changed = false;
|
|
143
|
+
for (let i = 0; i + len <= plain.length; i += Math.max(1, Math.floor(len / 2))) {
|
|
144
|
+
const frag = plain.slice(i, i + len);
|
|
145
|
+
if (scrubbed.includes(frag)) {
|
|
146
|
+
scrubbed = scrubbed.split(frag).join("[redacted]");
|
|
147
|
+
changed = true;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (changed)
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return scrubbed !== flat ? scrubbed : out;
|
|
155
|
+
}
|
|
156
|
+
/** Replace every detected credential with a label naming its kind. Never reversible. */
|
|
157
|
+
export function redactSecrets(text) {
|
|
158
|
+
if (!text)
|
|
159
|
+
return text;
|
|
160
|
+
const hits = findSecrets(text);
|
|
161
|
+
if (!hits.length)
|
|
162
|
+
return text;
|
|
163
|
+
let out = "", i = 0;
|
|
164
|
+
for (const h of hits) {
|
|
165
|
+
out += text.slice(i, h.start) + `[redacted: ${h.name}]`;
|
|
166
|
+
i = h.end;
|
|
167
|
+
}
|
|
168
|
+
return out + text.slice(i);
|
|
169
|
+
}
|
|
170
|
+
export function scanSecrets(segments, decodedPayloads) {
|
|
171
|
+
const byKind = new Map();
|
|
172
|
+
const visit = (text, hidden, where) => {
|
|
173
|
+
for (const h of findSecrets(text)) {
|
|
174
|
+
const e = byKind.get(h.name) ?? { n: 0, hidden: false, where };
|
|
175
|
+
e.n++;
|
|
176
|
+
e.hidden ||= hidden;
|
|
177
|
+
byKind.set(h.name, e);
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
for (const s of segments)
|
|
181
|
+
visit(s.text, s.visibility !== "visible", s.path);
|
|
182
|
+
for (const p of decodedPayloads)
|
|
183
|
+
visit(p, true, "decoded payload");
|
|
184
|
+
return [...byKind.entries()].map(([name, e]) => ({
|
|
185
|
+
pass: "secrets", kind: "secrets.credential_in_content",
|
|
186
|
+
confidence: 0.8,
|
|
187
|
+
// a visible leaked key is not an attack on the model; a hidden one is bait or a planted channel
|
|
188
|
+
severity: e.hidden ? 0.6 : 0.25,
|
|
189
|
+
message: `${e.n} ${name}${e.n === 1 ? "" : "s"} in ${e.hidden ? "hidden " : ""}content; redacted from everything the gate emits (the value is never logged)`,
|
|
190
|
+
excerpt: `[redacted: ${name}]`, location: e.where, action: "neutralized",
|
|
191
|
+
}));
|
|
192
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Supply-chain pass (underlight lineage, ported 2026-10-31).
|
|
3
|
+
*
|
|
4
|
+
* underlight inventories JavaScript that can execute on a machine. The part
|
|
5
|
+
* of it that sits on the gate's boundary is the moment an agent reads
|
|
6
|
+
* something that would make it install or run code: a package manifest, a
|
|
7
|
+
* lockfile, or an install command in a README. This pass ports underlight's
|
|
8
|
+
* manifest detectors (lifecycle scripts, URL-valued dependency specifiers,
|
|
9
|
+
* off-registry lockfile resolutions; same patterns as underlight.py 0.2.2)
|
|
10
|
+
* and adds one the gate needs: install commands in text, graded by whether a
|
|
11
|
+
* human reading the page would see them.
|
|
12
|
+
*
|
|
13
|
+
* Visible install commands are documentation (rustup, Homebrew) and score
|
|
14
|
+
* low. The same command in hidden text, a decoded payload, or reaching for
|
|
15
|
+
* credentials is the attack.
|
|
16
|
+
*/
|
|
17
|
+
import type { ContentKind, Finding, TextSegment } from "./types.js";
|
|
18
|
+
export interface SupplyChainInput {
|
|
19
|
+
kind: ContentKind;
|
|
20
|
+
raw: string;
|
|
21
|
+
segments: ReadonlyArray<TextSegment>;
|
|
22
|
+
decodedPayloads: ReadonlyArray<string>;
|
|
23
|
+
}
|
|
24
|
+
export declare function scanSupplyChain(input: SupplyChainInput): Finding[];
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Supply-chain pass (underlight lineage, ported 2026-10-31).
|
|
3
|
+
*
|
|
4
|
+
* underlight inventories JavaScript that can execute on a machine. The part
|
|
5
|
+
* of it that sits on the gate's boundary is the moment an agent reads
|
|
6
|
+
* something that would make it install or run code: a package manifest, a
|
|
7
|
+
* lockfile, or an install command in a README. This pass ports underlight's
|
|
8
|
+
* manifest detectors (lifecycle scripts, URL-valued dependency specifiers,
|
|
9
|
+
* off-registry lockfile resolutions; same patterns as underlight.py 0.2.2)
|
|
10
|
+
* and adds one the gate needs: install commands in text, graded by whether a
|
|
11
|
+
* human reading the page would see them.
|
|
12
|
+
*
|
|
13
|
+
* Visible install commands are documentation (rustup, Homebrew) and score
|
|
14
|
+
* low. The same command in hidden text, a decoded payload, or reaching for
|
|
15
|
+
* credentials is the attack.
|
|
16
|
+
*/
|
|
17
|
+
/** scripts that run when a package is installed as a dependency (underlight CONSUMER_LIFECYCLE) */
|
|
18
|
+
const CONSUMER_LIFECYCLE = ["preinstall", "install", "postinstall", "prepare"];
|
|
19
|
+
/**
|
|
20
|
+
* underlight SUSPICIOUS_SCRIPT_RE, split three ways after adversarial review (2026-11-01):
|
|
21
|
+
* strong — fetches, opens a socket, or runs decoded or generated code: flags on its own
|
|
22
|
+
* weak — inline interpreters, shells and URLs: common in harmless scripts ("node -e" feature checks,
|
|
23
|
+
* husky recipes, an echoed docs link), so they flag only in combination
|
|
24
|
+
* cred — reaches for credentials
|
|
25
|
+
*/
|
|
26
|
+
const STRONG_RE = /(\bcurl\b|\bwget\b|\/dev\/(tcp|udp)\/|\bnc(at)?\s|\bsocat\b|\bnslookup\b|\bdig\s|\bfetch\(|child_process|\bexec(Sync)?\(|\bspawn(Sync)?\(|powershell|Invoke-WebRequest|\biwr\b|certutil|\beval\(|\bFunction\(|\batob\(|base64\s+(-d|--decode|-D)\b|\b(perl|ruby)\s+-e\b|\bphp\s+-r\b|\\x[0-9a-fA-F]{2}|require\(\s*["'](https?|net|dgram|dns|tls)["']\s*\))/i;
|
|
27
|
+
/** unambiguous on install: a socket back to a host, a decoded or downloaded payload handed straight to a shell */
|
|
28
|
+
const CRITICAL_RE = /(\/dev\/(tcp|udp)\/|\bnc(at)?\s[^\n]{0,120}\s-(e|c)\s|\bsocat\b[^\n]{0,120}exec|\b(curl|wget)\b[^\n|]{0,300}\|\s*(sudo\s+)?(ba|z)?sh\b|base64\s+(-d|--decode|-D)\b[^\n]{0,80}\|\s*(ba|z)?sh\b|\\x[0-9a-fA-F]{2}[^\n]{0,200}\|\s*(ba|z)?sh\b)/i;
|
|
29
|
+
const WEAK_RE = /(\bnode\s+-e\b|\b(ba|z)?sh\s+-c\b|\bpython[23]?\s+-c\b|https?:\/\/|\$\()/gi;
|
|
30
|
+
const CRED_RE = /(\$\{?HOME\b|~\/\.ssh|\.ssh\/id_|\.npmrc|\.aws\/|\.config\/gh|\.git-credentials|\bAWS_[A-Z_]+|GITHUB_TOKEN|NPM_TOKEN|\bprintenv\b|\benv\s*\|)/i;
|
|
31
|
+
function execSignal(script) {
|
|
32
|
+
if (STRONG_RE.test(script))
|
|
33
|
+
return true;
|
|
34
|
+
const weak = new Set((script.match(WEAK_RE) ?? []).map((w) => w.toLowerCase().replace(/\s+/g, " ").replace(/^(ba|z)sh/, "sh")));
|
|
35
|
+
// a URL inside an echo or a comment is a message to the installer, not an action
|
|
36
|
+
if (weak.size === 1 && [...weak][0].startsWith("http") && /\becho\b|^\s*#/.test(script))
|
|
37
|
+
return false;
|
|
38
|
+
return weak.size >= 2;
|
|
39
|
+
}
|
|
40
|
+
/** underlight URL_SPEC_RE */
|
|
41
|
+
const URL_SPEC_RE = /^(https?:\/\/|git\+|git:\/\/|ssh:\/\/|github:|gitlab:|bitbucket:|gist:|[\w.-]+\/[\w.-]+(#.*)?$|.*\.tgz$|.*\.tar\.gz$)/i;
|
|
42
|
+
const REGISTRY_HOSTS = new Set(["registry.npmjs.org", "registry.yarnpkg.com", "registry.npmjs.com", "registry.npmmirror.com"]);
|
|
43
|
+
/** install / execute commands an agent might be talked into running */
|
|
44
|
+
const SUDO = String.raw `(sudo(\s+-[A-Za-z]+)*\s+)?`;
|
|
45
|
+
const INSTALL_PATTERNS = [
|
|
46
|
+
{ re: new RegExp(String.raw `\b(curl|wget|fetch)\b[^\n|;]{0,300}\|\s*` + SUDO + String.raw `(ba|z|da|k|fi)?sh\b`, "i"), what: "download piped to a shell" },
|
|
47
|
+
{ re: new RegExp(String.raw `\b(curl|wget)\b[^\n|;]{0,300}\|\s*` + SUDO + String.raw `(python[23]?|node|perl|ruby|php|deno|bun)\b`, "i"), what: "download piped to an interpreter" },
|
|
48
|
+
{ re: /\b(ba|z)?sh\s{1,4}<\(\s{0,4}(curl|wget)\b/i, what: "download run through process substitution" },
|
|
49
|
+
{ re: /\b(ba|z)?sh\s{1,4}-c\s{1,4}["']?\$\(\s{0,4}(curl|wget)\b/i, what: "download run through command substitution" },
|
|
50
|
+
{ re: /\b(curl|wget)\b[^\n;&|]{0,200}\s(-o|-O|--output)\s{0,3}\S{1,200}[^\n]{0,60}(&&|;)\s{0,4}(sudo\s+)?((ba|z)?sh|chmod\s+\+x|\.\/)/i, what: "download saved and then run" },
|
|
51
|
+
{ re: /\b(iwr|irm|Invoke-WebRequest|Invoke-RestMethod)\b[^\n|]{0,300}\|\s*(iex|Invoke-Expression)\b/i, what: "PowerShell download piped to Invoke-Expression" },
|
|
52
|
+
{ re: /\b(iex|Invoke-Expression)\s{0,4}\(\s{0,4}(iwr|irm|Invoke-WebRequest|Invoke-RestMethod|\(?\s{0,4}New-Object\s+Net\.WebClient)/i, what: "PowerShell expression evaluating a download" },
|
|
53
|
+
{ re: /\.DownloadString\s{0,3}\(/i, what: "PowerShell DownloadString" },
|
|
54
|
+
{ re: /\bbase64\s+(-d|--decode|-D)\b[^\n]{0,80}\|\s*(ba|z)?sh\b/i, what: "base64-decoded payload piped to a shell" },
|
|
55
|
+
{ re: /\b(npm|pnpm|yarn|bun)\s+(i|install|add)\s+[^\n]{0,80}(https?:\/\/|git\+|github:|gist:|\.tgz\b)/i, what: "package install from a URL instead of a registry" },
|
|
56
|
+
{ re: /\bnpx\s+(-y\s+|--yes\s+)?(https?:\/\/|github:|gist:)/i, what: "npx executing code from a URL" },
|
|
57
|
+
{ re: /\bdeno\s+run\s+(-A|--allow-all)\b[^\n]{0,60}https?:\/\//i, what: "deno running remote code with all permissions" },
|
|
58
|
+
{ re: /\b(uvx|pipx\s+run)\s+(--from|--spec)\s+(git\+|https?:\/\/)/i, what: "Python tool run from a URL" },
|
|
59
|
+
{ re: /\bpip3?\s+install\s+[^\n]{0,80}(git\+|https?:\/\/)/i, what: "pip install from a URL" },
|
|
60
|
+
{ re: /\b(env|printenv|set)\s*\|\s*(curl|wget|nc|ncat)\b/i, what: "environment variables piped to the network" },
|
|
61
|
+
{ re: /\b(cat|tar|zip)\b[^\n|]{0,120}(\.ssh|\.npmrc|\.aws|\.git-credentials|\.config\/gh|id_rsa|id_ed25519)[^\n|]{0,80}\|\s*(curl|wget|nc|ncat)\b/i, what: "credential files piped to the network" },
|
|
62
|
+
{ re: /\b(curl|wget)\b[^\n]{0,200}\s(-d|--data(-binary|-raw|-urlencode)?|-F|--form|-T|--upload-file|--post-file)[=\s]{1,3}["']?@?[^\s"']{0,200}(\.ssh|\.npmrc|\.aws|\.git-credentials|id_rsa|id_ed25519|\.env\b|@-)/i, what: "upload of local secrets or stdin to a remote host" },
|
|
63
|
+
{ re: /\/dev\/(tcp|udp)\/[\w.-]{1,253}\/\d{1,5}/i, what: "shell socket to a remote host (reverse shell)" },
|
|
64
|
+
{ re: /\bpython3?\s+-c\s+["'][^\n]{0,200}(urllib|requests|socket|exec\()/i, what: "inline Python that fetches or executes" },
|
|
65
|
+
{ re: /\bnode\s+-e\s+["'][^\n]{0,200}(child_process|https?\.get|fetch\(|eval\()/i, what: "inline Node that fetches or executes" },
|
|
66
|
+
];
|
|
67
|
+
const clip = (s, n = 200) => (s.length > n ? s.slice(0, n) + "…" : s);
|
|
68
|
+
function hostOf(u) {
|
|
69
|
+
const m = /^[a-z][a-z0-9+.-]*:\/\/([^/@]+@)?([^/:]+)/i.exec(u);
|
|
70
|
+
return m ? m[2].toLowerCase() : null;
|
|
71
|
+
}
|
|
72
|
+
function isManifest(doc) {
|
|
73
|
+
return !!doc && typeof doc === "object" && !Array.isArray(doc) && typeof doc.name === "string" &&
|
|
74
|
+
(typeof doc.version === "string" || typeof doc.scripts === "object" || typeof doc.dependencies === "object");
|
|
75
|
+
}
|
|
76
|
+
function manifestFindings(doc) {
|
|
77
|
+
const out = [];
|
|
78
|
+
const scripts = doc.scripts && typeof doc.scripts === "object" ? doc.scripts : {};
|
|
79
|
+
for (const k of CONSUMER_LIFECYCLE) {
|
|
80
|
+
const v = scripts[k];
|
|
81
|
+
if (typeof v !== "string" || !v.trim())
|
|
82
|
+
continue;
|
|
83
|
+
const cred = CRED_RE.test(v), critical = CRITICAL_RE.test(v), exec = critical || execSignal(v);
|
|
84
|
+
if (!cred && !exec)
|
|
85
|
+
continue;
|
|
86
|
+
out.push({
|
|
87
|
+
pass: "supplychain", kind: "supplychain.lifecycle_script",
|
|
88
|
+
confidence: cred && exec ? 0.95 : cred || critical ? 0.9 : 0.65,
|
|
89
|
+
severity: cred || critical ? 0.95 : 0.6,
|
|
90
|
+
message: `${k} script runs on install and ${cred ? "reaches for credentials" : critical ? "opens a shell to a remote host or pipes a payload into a shell" : "fetches or executes code"}`,
|
|
91
|
+
excerpt: clip(v), location: `$.scripts.${k}`, action: "none",
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
const url = [];
|
|
95
|
+
for (const field of ["dependencies", "devDependencies", "optionalDependencies", "peerDependencies"]) {
|
|
96
|
+
const deps = doc[field];
|
|
97
|
+
if (!deps || typeof deps !== "object")
|
|
98
|
+
continue;
|
|
99
|
+
for (const [name, spec] of Object.entries(deps))
|
|
100
|
+
if (typeof spec === "string" && URL_SPEC_RE.test(spec.trim()))
|
|
101
|
+
url.push(`${name}@${spec}`);
|
|
102
|
+
}
|
|
103
|
+
if (url.length)
|
|
104
|
+
out.push({
|
|
105
|
+
pass: "supplychain", kind: "supplychain.url_dependency", confidence: 0.45, severity: 0.5,
|
|
106
|
+
message: `${url.length} dependenc${url.length === 1 ? "y resolves" : "ies resolve"} to a network location instead of a registry; registry scanners see none of them`,
|
|
107
|
+
excerpt: clip(url.slice(0, 5).join(", ")), location: "$.dependencies", action: "none",
|
|
108
|
+
});
|
|
109
|
+
return out;
|
|
110
|
+
}
|
|
111
|
+
function lockfileFindings(raw, doc) {
|
|
112
|
+
const hosts = new Map();
|
|
113
|
+
const note = (u) => { const h = hostOf(u); if (h && !REGISTRY_HOSTS.has(h))
|
|
114
|
+
hosts.set(h, (hosts.get(h) ?? 0) + 1); };
|
|
115
|
+
if (doc && typeof doc === "object" && ("lockfileVersion" in doc)) {
|
|
116
|
+
for (const group of [doc.packages, doc.dependencies])
|
|
117
|
+
if (group && typeof group === "object")
|
|
118
|
+
for (const v of Object.values(group))
|
|
119
|
+
if (v && typeof v.resolved === "string")
|
|
120
|
+
note(v.resolved);
|
|
121
|
+
}
|
|
122
|
+
else if (/^# (yarn lockfile|THIS IS AN AUTOGENERATED FILE)/m.test(raw) || /^lockfileVersion:/m.test(raw)) {
|
|
123
|
+
for (const m of raw.matchAll(/(?:resolved|tarball)[:\s]+["']?(https?:\/\/[^\s"'}]+)/g))
|
|
124
|
+
note(m[1]);
|
|
125
|
+
}
|
|
126
|
+
else
|
|
127
|
+
return [];
|
|
128
|
+
if (!hosts.size)
|
|
129
|
+
return [];
|
|
130
|
+
const list = [...hosts.entries()].sort((a, b) => b[1] - a[1]);
|
|
131
|
+
return [{
|
|
132
|
+
pass: "supplychain", kind: "supplychain.offregistry_resolution", confidence: 0.4, severity: 0.5,
|
|
133
|
+
message: `lockfile resolves packages from ${list.length} host${list.length === 1 ? "" : "s"} outside the public registries (a private registry is legitimate; an unknown one is not)`,
|
|
134
|
+
excerpt: clip(list.slice(0, 5).map(([h, n]) => `${h} (${n})`).join(", ")), action: "none",
|
|
135
|
+
}];
|
|
136
|
+
}
|
|
137
|
+
function installFindings(text, hidden, where) {
|
|
138
|
+
const out = [];
|
|
139
|
+
for (const p of INSTALL_PATTERNS) {
|
|
140
|
+
const m = p.re.exec(text);
|
|
141
|
+
if (!m)
|
|
142
|
+
continue;
|
|
143
|
+
const start = Math.max(0, m.index - 40);
|
|
144
|
+
const ctx = text.slice(start, m.index + m[0].length + 80);
|
|
145
|
+
const cred = CRED_RE.test(ctx);
|
|
146
|
+
out.push({
|
|
147
|
+
pass: "supplychain", kind: "supplychain.install_instruction",
|
|
148
|
+
// a visible curl|sh is ordinary documentation; hidden, decoded, or credential-reaching is not
|
|
149
|
+
confidence: hidden ? 0.92 : cred ? 0.75 : 0.3,
|
|
150
|
+
severity: hidden ? 0.9 : cred ? 0.8 : 0.4,
|
|
151
|
+
message: `${hidden ? "hidden " : ""}${p.what}${cred ? ", reaching for credentials" : ""}${hidden ? " (a human reading the page would not see this command)" : ""}`,
|
|
152
|
+
excerpt: clip(m[0]), location: where, action: "none",
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
return out;
|
|
156
|
+
}
|
|
157
|
+
export function scanSupplyChain(input) {
|
|
158
|
+
const findings = [];
|
|
159
|
+
let doc;
|
|
160
|
+
if (input.kind === "json") {
|
|
161
|
+
try {
|
|
162
|
+
doc = JSON.parse(input.raw);
|
|
163
|
+
}
|
|
164
|
+
catch {
|
|
165
|
+
doc = undefined;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
if (doc !== undefined && isManifest(doc))
|
|
169
|
+
findings.push(...manifestFindings(doc));
|
|
170
|
+
if (input.kind === "json" || input.kind === "text")
|
|
171
|
+
findings.push(...lockfileFindings(input.raw, doc));
|
|
172
|
+
// manifests carry their scripts as JSON strings; those were judged above, so skip segment scanning of $.scripts
|
|
173
|
+
for (const s of input.segments) {
|
|
174
|
+
if (s.path.startsWith("$.scripts."))
|
|
175
|
+
continue;
|
|
176
|
+
// collapsed <details> and hidden tab panels are revealed by one click: install docs live there, so weigh them as visible
|
|
177
|
+
findings.push(...installFindings(s.text, s.visibility !== "visible" && !s.path.includes("~revealable"), s.path));
|
|
178
|
+
}
|
|
179
|
+
for (const p of input.decodedPayloads)
|
|
180
|
+
findings.push(...installFindings(p, true, "decoded payload"));
|
|
181
|
+
return findings;
|
|
182
|
+
}
|