tabsmith-lint 0.4.0 → 0.5.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/README.md +16 -2
- package/dist/cli.js +2 -0
- package/dist/rules/index.js +2 -1
- package/dist/rules/mv3.js +53 -19
- package/dist/rules/obfuscation.js +327 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -26,6 +26,7 @@ Chrome reviewers flag:
|
|
|
26
26
|
| Purple Potassium | Excessive / unused permissions | PERM001, PERM003, PERM004, PERM005 |
|
|
27
27
|
| Blue Argon | Remotely hosted code / latent remote loaders / string execution / forbidden CSP | MV3001, MV3005, MV3002, MV3004 |
|
|
28
28
|
| Yellow Magnesium | Functionality / broken packaging | FUNC001, FUNC002 |
|
|
29
|
+
| Red Titanium | Obfuscated code (minification is fine) | OBF001 |
|
|
29
30
|
| Harmful / unexpected behavior | DNR rules that strip sites' security headers | SEC001 |
|
|
30
31
|
|
|
31
32
|
```bash
|
|
@@ -83,7 +84,7 @@ exists, leave the severity at `fix`.
|
|
|
83
84
|
|
|
84
85
|
## Status
|
|
85
86
|
|
|
86
|
-
Chrome-only, directory input,
|
|
87
|
+
Chrome-only, directory input, 14 rules (PERM001-005, MV3001-005, FUNC001-002, SEC001, OBF001).
|
|
87
88
|
|
|
88
89
|
MV3001 (remotely hosted code, `reject`) covers HTML `<script src="https://…">`, remote
|
|
89
90
|
`import`/`import()`, remote `importScripts()`, a remote `src` set on a
|
|
@@ -97,7 +98,9 @@ computed URLs.
|
|
|
97
98
|
MV3005 (latent remote-code loader) covers what MV3001 deliberately leaves out: bundled
|
|
98
99
|
code that *could* load a remote script. A `<script>` element (or `importScripts`) given
|
|
99
100
|
a computed `src` is `fix` when the URL is built on a remote prefix
|
|
100
|
-
(`"https://cdn/" + v
|
|
101
|
+
(`"https://cdn/" + v`, including a prefix held in a variable bound only to that string,
|
|
102
|
+
`var base = "https://cdn/"; s.src = base + "x.js"`) or the same file also contains a
|
|
103
|
+
remote `.js`/`.mjs` URL literal;
|
|
101
104
|
the finding names the URL and, when a banner or `vendor/` path identifies it, the
|
|
102
105
|
bundled package. Other remote `.js` URL literals in shipped code are `info` (they never
|
|
103
106
|
change the verdict). Only `.js`/`.mjs` URLs count, so remote JSON/data endpoints are
|
|
@@ -110,6 +113,17 @@ that `remove`/`set` `Content-Security-Policy`(-Report-Only), `X-Frame-Options`,
|
|
|
110
113
|
`fix` when the condition isn't scoped to specific domains and the extension has broad
|
|
111
114
|
host access, otherwise `info`.
|
|
112
115
|
|
|
116
|
+
OBF001 (obfuscated code, Red Titanium, `fix`) looks for fingerprints that minifiers
|
|
117
|
+
never produce: `_0x`-style hexadecimal identifiers in quantity, javascript-obfuscator's
|
|
118
|
+
string-array rotator and hex-index lookups, Dean Edwards' `eval(function(p,a,c,k,e,d)…)`
|
|
119
|
+
packer, `eval`/`Function` run on a decoded string (`eval(atob(…))`,
|
|
120
|
+
`new Function(String.fromCharCode(…))`), and plain-text strings spelled with `\x`/`\u`
|
|
121
|
+
escapes. One finding per file, naming the bundled package from its license banner,
|
|
122
|
+
`node_modules/` or `vendor/` path, or the bundler's module comment. Measured precision:
|
|
123
|
+
0 findings on 43 popular npm browser packages (lodash, react-dom, vue, chart.js, three,
|
|
124
|
+
pdf.js, @babel/standalone, crypto libraries, ... — 12,041 script files), the 7 minified
|
|
125
|
+
release extensions and the Google samples; 37 of 38 obfuscated samples flagged.
|
|
126
|
+
|
|
113
127
|
Firefox/Edge, ZIP/CRX input, SARIF, and a GitHub Action are on the roadmap.
|
|
114
128
|
|
|
115
129
|
Two rules are intentionally conservative in v0.1 (trust is the product): **PERM001**
|
package/dist/cli.js
CHANGED
|
@@ -27,6 +27,8 @@ rejection risks before you submit:
|
|
|
27
27
|
import, importScripts, chrome.scripting remote files), latent remote-code loaders
|
|
28
28
|
in bundled code, string execution, forbidden extension-page CSP, Manifest V2
|
|
29
29
|
- declarativeNetRequest rules that strip security headers (CSP, X-Frame-Options, ...)
|
|
30
|
+
- obfuscated code ("Red Titanium": javascript-obfuscator output, packers, eval of
|
|
31
|
+
decoded strings), naming the bundled package it comes from; minified code is fine
|
|
30
32
|
- broken packaging: missing or wrong-case file references
|
|
31
33
|
|
|
32
34
|
${USAGE}
|
package/dist/rules/index.js
CHANGED
|
@@ -3,7 +3,8 @@ import { permissionRules } from "./permissions.js";
|
|
|
3
3
|
import { mv3Rules } from "./mv3.js";
|
|
4
4
|
import { fileRules } from "./files.js";
|
|
5
5
|
import { securityRules } from "./security.js";
|
|
6
|
-
|
|
6
|
+
import { obfuscationRules } from "./obfuscation.js";
|
|
7
|
+
export const allRules = [...permissionRules, ...mv3Rules, ...fileRules, ...securityRules, ...obfuscationRules];
|
|
7
8
|
/** Run every rule and return all findings, ordered reject → fix → info. */
|
|
8
9
|
export function runRules(model) {
|
|
9
10
|
const findings = allRules.flatMap((rule) => rule(model));
|
package/dist/rules/mv3.js
CHANGED
|
@@ -119,20 +119,31 @@ function patternNames(pattern, out = []) {
|
|
|
119
119
|
}
|
|
120
120
|
return out;
|
|
121
121
|
}
|
|
122
|
-
/** Names
|
|
123
|
-
|
|
124
|
-
|
|
122
|
+
/** Names whose EVERY binding in the file is a value accepted by `valueOf` (all
|
|
123
|
+
* bindings agreeing on that value). Any parameter, other initializer, reassignment,
|
|
124
|
+
* update or conflicting value with the same name drops it — reused/minified names
|
|
125
|
+
* are skipped, never guessed. */
|
|
126
|
+
function namesBoundOnlyTo(ast, valueOf) {
|
|
127
|
+
const bound = new Map();
|
|
125
128
|
const other = new Set();
|
|
129
|
+
const record = (name, v) => {
|
|
130
|
+
if (bound.has(name) && bound.get(name) !== v)
|
|
131
|
+
other.add(name);
|
|
132
|
+
else
|
|
133
|
+
bound.set(name, v);
|
|
134
|
+
};
|
|
126
135
|
walk(ast, (node) => {
|
|
127
136
|
if (node.type === "VariableDeclarator") {
|
|
128
|
-
|
|
129
|
-
|
|
137
|
+
const v = node.id?.type === "Identifier" && node.init ? valueOf(node.init) : null;
|
|
138
|
+
if (v !== null)
|
|
139
|
+
record(node.id.name, v);
|
|
130
140
|
else if (node.init)
|
|
131
141
|
patternNames(node.id).forEach((n) => other.add(n));
|
|
132
142
|
}
|
|
133
143
|
else if (node.type === "AssignmentExpression" && node.left?.type === "Identifier") {
|
|
134
|
-
|
|
135
|
-
|
|
144
|
+
const v = node.operator === "=" ? valueOf(node.right) : null;
|
|
145
|
+
if (v !== null)
|
|
146
|
+
record(node.left.name, v);
|
|
136
147
|
else
|
|
137
148
|
other.add(node.left.name);
|
|
138
149
|
}
|
|
@@ -165,8 +176,16 @@ function scriptElementNames(ast) {
|
|
|
165
176
|
}
|
|
166
177
|
});
|
|
167
178
|
for (const n of other)
|
|
168
|
-
|
|
169
|
-
return
|
|
179
|
+
bound.delete(n);
|
|
180
|
+
return bound;
|
|
181
|
+
}
|
|
182
|
+
/** Names bound ONLY to document.createElement("script") anywhere in the file. */
|
|
183
|
+
function scriptElementNames(ast) {
|
|
184
|
+
return new Set(namesBoundOnlyTo(ast, (init) => (isScriptCreate(init) ? true : null)).keys());
|
|
185
|
+
}
|
|
186
|
+
/** Names bound ONLY to one string literal (e.g. `var base = "https://cdn.example/"`). */
|
|
187
|
+
function constStringNames(ast) {
|
|
188
|
+
return namesBoundOnlyTo(ast, (init) => staticString(init));
|
|
170
189
|
}
|
|
171
190
|
/** Every place a `document.createElement("script")` element gets its `src` set,
|
|
172
191
|
* literal or not: `.src =`, `["src"] =`, `setAttribute("src", …)`,
|
|
@@ -378,14 +397,22 @@ function isGetUrlCall(node) {
|
|
|
378
397
|
c.object.property?.type === "Identifier" &&
|
|
379
398
|
(c.object.property.name === "runtime" || c.object.property.name === "extension"));
|
|
380
399
|
}
|
|
381
|
-
/** Leading static text of a string-building expression ("a" + x, `a${x}`).
|
|
382
|
-
|
|
400
|
+
/** Leading static text of a string-building expression ("a" + x, `a${x}`). With
|
|
401
|
+
* `consts`, a leading identifier bound only to one string literal resolves to it
|
|
402
|
+
* (`var base = "https://cdn/"; base + "x.js"`, `` `${base}x.js` ``). */
|
|
403
|
+
function staticPrefix(node, consts) {
|
|
383
404
|
if (node?.type === "StringLiteral")
|
|
384
405
|
return node.value;
|
|
385
|
-
if (node?.type === "
|
|
386
|
-
return node.
|
|
406
|
+
if (node?.type === "Identifier")
|
|
407
|
+
return consts?.get(node.name) ?? null;
|
|
408
|
+
if (node?.type === "TemplateLiteral") {
|
|
409
|
+
const head = node.quasis?.[0]?.value?.cooked ?? null;
|
|
410
|
+
if (head === "" && node.expressions?.length)
|
|
411
|
+
return staticPrefix(node.expressions[0], consts);
|
|
412
|
+
return head;
|
|
413
|
+
}
|
|
387
414
|
if (node?.type === "BinaryExpression" && node.operator === "+")
|
|
388
|
-
return staticPrefix(node.left);
|
|
415
|
+
return staticPrefix(node.left, consts);
|
|
389
416
|
return null;
|
|
390
417
|
}
|
|
391
418
|
const BANNER_RE = /^\s*(?:\/\*[!*]?|\/\/)\s*(?:@license\s+)?(@?[A-Za-z][\w./-]*)\s+v?(\d+\.\d+\.\d+[\w.-]*)/;
|
|
@@ -437,12 +464,18 @@ function mv3005(model) {
|
|
|
437
464
|
// One `fix` finding per file (first loader), other loader lines listed, to keep bundles readable.
|
|
438
465
|
let latent = false;
|
|
439
466
|
const hits = [];
|
|
467
|
+
const consts = sinks.length ? constStringNames(script.ast) : undefined;
|
|
468
|
+
const prefixes = new Set(); // remote prefixes already reported by a fix finding
|
|
440
469
|
for (const { node, value, how } of sinks) {
|
|
441
470
|
if (isGetUrlCall(value))
|
|
442
471
|
continue;
|
|
443
|
-
const prefix = staticPrefix(value);
|
|
472
|
+
const prefix = staticPrefix(value, consts);
|
|
444
473
|
if (isRemote(prefix)) {
|
|
445
|
-
|
|
474
|
+
prefixes.add(prefix);
|
|
475
|
+
const msg = value.type === "Identifier"
|
|
476
|
+
? `${how} loads a script from the variable ${value.name}, which only ever holds the remote URL (remote prefix) "${prefix}".`
|
|
477
|
+
: `${how} loads a script from a URL built at runtime on the remote prefix "${prefix}".`;
|
|
478
|
+
hits.push({ node, message: `${msg} Even if this path never runs, reviewers treat it as remotely hosted code.` });
|
|
446
479
|
}
|
|
447
480
|
else if (urls.length > 0) {
|
|
448
481
|
latent = true;
|
|
@@ -467,15 +500,16 @@ function mv3005(model) {
|
|
|
467
500
|
fixHint: "If the extension never needs this loader, strip or stub it at build time (or configure the package not to include it) and mention it in the submission notes. If it is needed, bundle the script and load the packaged copy via chrome.runtime.getURL(...). Fetching remote JSON/data is fine.",
|
|
468
501
|
});
|
|
469
502
|
}
|
|
470
|
-
|
|
503
|
+
const unexplained = urls.filter((u) => !prefixes.has(u));
|
|
504
|
+
if (!latent && unexplained.length > 0) {
|
|
471
505
|
findings.push({
|
|
472
506
|
ruleId: "MV3005",
|
|
473
507
|
severity: "info",
|
|
474
508
|
confidence: "low",
|
|
475
509
|
title: "Remote script URL in shipped code",
|
|
476
|
-
message: `Shipped code contains the remote script URL ${list(
|
|
510
|
+
message: `Shipped code contains the remote script URL ${list(unexplained)}. No loader was found for it, but reviewers search bundles for remote .js URLs; check that nothing can load it.${from}`,
|
|
477
511
|
file,
|
|
478
|
-
line: literals
|
|
512
|
+
line: literals.find((l) => !prefixes.has(l.url))?.line,
|
|
479
513
|
chromeViolationId: "Blue Argon",
|
|
480
514
|
fixHint: "Remove unused remote script URLs at build time, or explain them in the submission notes.",
|
|
481
515
|
});
|
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
import { walk } from "../parse/parse-script.js";
|
|
2
|
+
import { findLineByIndex } from "../engine/source-lines.js";
|
|
3
|
+
// OBF001 — obfuscated code (Chrome Web Store "Red Titanium"). Google's policy:
|
|
4
|
+
// minification is allowed, obfuscation is not ("character encoding", hidden
|
|
5
|
+
// functionality, packers). Minified bundles look nothing like obfuscator output,
|
|
6
|
+
// so OBF001 only fires on fingerprints that minifiers never produce:
|
|
7
|
+
// - `_0x`-style hexadecimal identifiers (javascript-obfuscator / obfuscator.io)
|
|
8
|
+
// used in quantity;
|
|
9
|
+
// - a string-array rotator: `arr.push(arr.shift())` inside a function that also
|
|
10
|
+
// runs the obfuscator's parseInt checksum or a `while (--n)` countdown;
|
|
11
|
+
// - string-array lookups: one function called over and over with a hex numeric
|
|
12
|
+
// literal (`_0x1a2b(0x1f4)`), the decoder call that replaces every string;
|
|
13
|
+
// - a Dean Edwards packer: `eval(function(p,a,c,k,e,d){…}(…))`;
|
|
14
|
+
// - eval / Function run on a decoded payload: `eval(atob(…))`,
|
|
15
|
+
// `new Function(String.fromCharCode(…))`, `eval(unescape(…))`;
|
|
16
|
+
// - plain-ASCII text hidden behind escapes: many string literals written as
|
|
17
|
+
// `"\x68\x65\x6c\x6c\x6f"` (minifiers only escape non-printable/non-ASCII).
|
|
18
|
+
// Thresholds were set on a negative set of 40+ popular minified npm libraries
|
|
19
|
+
// (lodash, react-dom, vue, chart.js, three, pdf.js, @babel/standalone, crypto
|
|
20
|
+
// libraries, ...) plus the minified release corpus, where OBF001 reports nothing.
|
|
21
|
+
// Severity is `fix`, not `reject`: the finding names what to replace and is a
|
|
22
|
+
// strong signal, but static fingerprints cannot prove intent. One finding per file.
|
|
23
|
+
export const OBF001_SEVERITY = "fix";
|
|
24
|
+
/** Minimum distinct `_0x…` identifiers for the identifier signal. */
|
|
25
|
+
export const HEX_IDENT_MIN = 10;
|
|
26
|
+
/** Minimum calls of a single function with a small hex index literal argument… */
|
|
27
|
+
export const HEX_LOOKUP_MIN = 25;
|
|
28
|
+
/** …with at least this many distinct index values. */
|
|
29
|
+
export const HEX_LOOKUP_DISTINCT_MIN = 15;
|
|
30
|
+
/** Minimum string literals that are plain ASCII text spelled with escapes. */
|
|
31
|
+
export const ESCAPED_ASCII_MIN = 20;
|
|
32
|
+
const HEX_IDENT_RE = /^_0x[0-9a-f]{3,}$/i;
|
|
33
|
+
const DECODERS = new Set(["atob", "unescape", "decodeURIComponent", "decodeURI", "escape"]);
|
|
34
|
+
function propName(member) {
|
|
35
|
+
if (member?.type !== "MemberExpression" && member?.type !== "OptionalMemberExpression")
|
|
36
|
+
return null;
|
|
37
|
+
if (!member.computed)
|
|
38
|
+
return member.property?.type === "Identifier" ? member.property.name : null;
|
|
39
|
+
return member.property?.type === "StringLiteral" ? member.property.value : null;
|
|
40
|
+
}
|
|
41
|
+
/** `A.push(A.shift())` / `A['push'](A['shift']())` with the same identifier A. */
|
|
42
|
+
function isPushShift(node) {
|
|
43
|
+
if (node?.type !== "CallExpression" || propName(node.callee) !== "push")
|
|
44
|
+
return false;
|
|
45
|
+
const obj = node.callee.object;
|
|
46
|
+
const arg = node.arguments?.[0];
|
|
47
|
+
if (obj?.type !== "Identifier" || node.arguments.length !== 1)
|
|
48
|
+
return false;
|
|
49
|
+
return (arg?.type === "CallExpression" &&
|
|
50
|
+
propName(arg.callee) === "shift" &&
|
|
51
|
+
arg.callee.object?.type === "Identifier" &&
|
|
52
|
+
arg.callee.object.name === obj.name &&
|
|
53
|
+
(arg.arguments?.length ?? 0) === 0);
|
|
54
|
+
}
|
|
55
|
+
function isFunctionNode(node) {
|
|
56
|
+
return node?.type === "FunctionDeclaration" || node?.type === "FunctionExpression" || node?.type === "ArrowFunctionExpression";
|
|
57
|
+
}
|
|
58
|
+
/** A function body that rotates an array AND runs a checksum (parseInt ×2) or a `while (--n)` countdown. */
|
|
59
|
+
function isRotatorFunction(fn) {
|
|
60
|
+
let pushShift = false;
|
|
61
|
+
let parseInts = 0;
|
|
62
|
+
let countdown = false;
|
|
63
|
+
walk(fn.body, (n) => {
|
|
64
|
+
if (isPushShift(n))
|
|
65
|
+
pushShift = true;
|
|
66
|
+
else if (n.type === "CallExpression" && n.callee?.type === "Identifier" && n.callee.name === "parseInt")
|
|
67
|
+
parseInts++;
|
|
68
|
+
else if (n.type === "WhileStatement" && n.test?.type === "UpdateExpression" && n.test.operator === "--")
|
|
69
|
+
countdown = true;
|
|
70
|
+
});
|
|
71
|
+
return pushShift && (parseInts >= 2 || countdown);
|
|
72
|
+
}
|
|
73
|
+
// String-array decoder call: `_0x1a2b(0x1f4)` / `_0x1a2b(0x1f4, "key")`. The index is
|
|
74
|
+
// a small, lower-case, unpadded hex literal (< 0x10000). Colour and hash constants
|
|
75
|
+
// (0xffb845, 0x428a2f98) are larger, so crypto/graphics code doesn't count.
|
|
76
|
+
const LOOKUP_HEX_RE = /^0x(?:0|[1-9a-f][0-9a-f]{0,3})$/;
|
|
77
|
+
function isHexLookup(node) {
|
|
78
|
+
if (node.callee?.type !== "Identifier")
|
|
79
|
+
return false;
|
|
80
|
+
const args = node.arguments ?? [];
|
|
81
|
+
if (args.length !== 1 && args.length !== 2)
|
|
82
|
+
return false;
|
|
83
|
+
if (args[0]?.type !== "NumericLiteral" || !LOOKUP_HEX_RE.test(args[0].extra?.raw ?? ""))
|
|
84
|
+
return false;
|
|
85
|
+
return args.length === 1 || ["StringLiteral", "NumericLiteral", "UnaryExpression"].includes(args[1]?.type);
|
|
86
|
+
}
|
|
87
|
+
/** `eval(function(p,a,c,k,e,d){…}(…))` — Dean Edwards' packer. */
|
|
88
|
+
function isPacker(node) {
|
|
89
|
+
if (node?.type !== "CallExpression" || node.callee?.type !== "Identifier" || node.callee.name !== "eval")
|
|
90
|
+
return false;
|
|
91
|
+
const inner = node.arguments?.[0];
|
|
92
|
+
if (inner?.type !== "CallExpression" || !isFunctionNode(inner.callee))
|
|
93
|
+
return false;
|
|
94
|
+
const names = (inner.callee.params ?? []).map((p) => (p.type === "Identifier" ? p.name : "?")).join(",");
|
|
95
|
+
return /^p,a,c,k,e,[a-z]$/.test(names);
|
|
96
|
+
}
|
|
97
|
+
/** A decoding call: atob(...), unescape(...), String.fromCharCode(...), possibly nested. */
|
|
98
|
+
function decoderName(node) {
|
|
99
|
+
if (node?.type !== "CallExpression")
|
|
100
|
+
return null;
|
|
101
|
+
const c = node.callee;
|
|
102
|
+
if (c?.type === "Identifier" && DECODERS.has(c.name))
|
|
103
|
+
return `${c.name}()`;
|
|
104
|
+
if ((c?.type === "MemberExpression") && propName(c) === "fromCharCode" &&
|
|
105
|
+
c.object?.type === "Identifier" && c.object.name === "String")
|
|
106
|
+
return "String.fromCharCode()";
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
/** eval(<decoded>), Function(<decoded>), new Function(<decoded>) → the decoder it wraps. */
|
|
110
|
+
function decodedEvalOf(node) {
|
|
111
|
+
const isCall = node?.type === "CallExpression" || node?.type === "NewExpression";
|
|
112
|
+
if (!isCall || node.callee?.type !== "Identifier")
|
|
113
|
+
return null;
|
|
114
|
+
const name = node.callee.name;
|
|
115
|
+
if (name !== "eval" && name !== "Function")
|
|
116
|
+
return null;
|
|
117
|
+
if (name === "eval" && node.type === "NewExpression")
|
|
118
|
+
return null;
|
|
119
|
+
// the code argument is the first for eval and the last for Function
|
|
120
|
+
const args = node.arguments ?? [];
|
|
121
|
+
const code = name === "eval" ? args[0] : args[args.length - 1];
|
|
122
|
+
const dec = decoderName(code);
|
|
123
|
+
return dec ? `${node.type === "NewExpression" ? "new " : ""}${name}(${dec})` : null;
|
|
124
|
+
}
|
|
125
|
+
const ESCAPE_RE = /\\x[0-9a-fA-F]{2}|\\u[0-9a-fA-F]{4}|\\u\{[0-9a-fA-F]+\}/g;
|
|
126
|
+
/** A string literal whose source spells printable ASCII text with escapes ("\x68\x69"). */
|
|
127
|
+
function isEscapedAscii(node) {
|
|
128
|
+
if (node?.type !== "StringLiteral")
|
|
129
|
+
return false;
|
|
130
|
+
const raw = node.extra?.raw;
|
|
131
|
+
const value = node.value;
|
|
132
|
+
if (!raw || value.length < 3)
|
|
133
|
+
return false;
|
|
134
|
+
if (!/^[\x20-\x7e]+$/.test(value) || !/[A-Za-z]/.test(value))
|
|
135
|
+
return false;
|
|
136
|
+
let escapedChars = 0;
|
|
137
|
+
let escapes = 0;
|
|
138
|
+
for (const m of raw.matchAll(ESCAPE_RE)) {
|
|
139
|
+
const ch = m[0].startsWith("\\x") ? parseInt(m[0].slice(2), 16) : parseInt(m[0].replace(/[\\u{}]/g, ""), 16);
|
|
140
|
+
if (ch >= 0x20 && ch <= 0x7e) {
|
|
141
|
+
escapes++;
|
|
142
|
+
escapedChars += m[0].length;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return escapes >= 3 && escapedChars >= (raw.length - 2) * 0.75;
|
|
146
|
+
}
|
|
147
|
+
/** Raw obfuscation measurements for one parsed file (exported for tests and FP probes). */
|
|
148
|
+
export function obfuscationSignals(ast) {
|
|
149
|
+
const hexIds = new Set();
|
|
150
|
+
const lookups = new Map();
|
|
151
|
+
let rotator = false;
|
|
152
|
+
let packer = false;
|
|
153
|
+
let decodedEval = null;
|
|
154
|
+
let escapedAscii = 0;
|
|
155
|
+
let firstIndex = null;
|
|
156
|
+
const mark = (n) => {
|
|
157
|
+
const i = n?.start;
|
|
158
|
+
if (typeof i === "number" && (firstIndex === null || i < firstIndex))
|
|
159
|
+
firstIndex = i;
|
|
160
|
+
};
|
|
161
|
+
let firstHexId = null;
|
|
162
|
+
let firstEscaped = null;
|
|
163
|
+
const firstLookup = new Map();
|
|
164
|
+
const functions = [];
|
|
165
|
+
const pushShifts = [];
|
|
166
|
+
walk(ast, (node) => {
|
|
167
|
+
switch (node.type) {
|
|
168
|
+
case "Identifier":
|
|
169
|
+
if (HEX_IDENT_RE.test(node.name)) {
|
|
170
|
+
hexIds.add(node.name);
|
|
171
|
+
if (!firstHexId)
|
|
172
|
+
firstHexId = node;
|
|
173
|
+
}
|
|
174
|
+
break;
|
|
175
|
+
case "StringLiteral":
|
|
176
|
+
if (isEscapedAscii(node)) {
|
|
177
|
+
escapedAscii++;
|
|
178
|
+
if (!firstEscaped)
|
|
179
|
+
firstEscaped = node;
|
|
180
|
+
}
|
|
181
|
+
break;
|
|
182
|
+
case "FunctionDeclaration":
|
|
183
|
+
case "FunctionExpression":
|
|
184
|
+
case "ArrowFunctionExpression":
|
|
185
|
+
functions.push(node);
|
|
186
|
+
break;
|
|
187
|
+
case "CallExpression":
|
|
188
|
+
case "NewExpression": {
|
|
189
|
+
if (isPushShift(node))
|
|
190
|
+
pushShifts.push(node.start);
|
|
191
|
+
if (node.type === "CallExpression" && isPacker(node)) {
|
|
192
|
+
packer = true;
|
|
193
|
+
mark(node);
|
|
194
|
+
}
|
|
195
|
+
const de = decodedEvalOf(node);
|
|
196
|
+
if (de && !decodedEval) {
|
|
197
|
+
decodedEval = de;
|
|
198
|
+
mark(node);
|
|
199
|
+
}
|
|
200
|
+
if (node.type === "CallExpression" && isHexLookup(node)) {
|
|
201
|
+
const name = node.callee.name;
|
|
202
|
+
const e = lookups.get(name) ?? { calls: 0, values: new Set() };
|
|
203
|
+
e.calls++;
|
|
204
|
+
e.values.add(node.arguments[0].value);
|
|
205
|
+
lookups.set(name, e);
|
|
206
|
+
if (!firstLookup.has(name))
|
|
207
|
+
firstLookup.set(name, node);
|
|
208
|
+
}
|
|
209
|
+
break;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
// Rotator check: only the INNERMOST function around each arr.push(arr.shift())
|
|
214
|
+
// (rare in real code; an outer bundle wrapper would contain unrelated parseInt calls).
|
|
215
|
+
for (const at of pushShifts) {
|
|
216
|
+
let inner = null;
|
|
217
|
+
for (const fn of functions)
|
|
218
|
+
if (fn.start < at && at < fn.end && (!inner || fn.start > inner.start))
|
|
219
|
+
inner = fn;
|
|
220
|
+
if (inner && isRotatorFunction(inner)) {
|
|
221
|
+
rotator = true;
|
|
222
|
+
mark(inner);
|
|
223
|
+
break;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
let hexLookupCalls = 0;
|
|
227
|
+
let hexLookupCallee = null;
|
|
228
|
+
for (const [name, e] of lookups) {
|
|
229
|
+
// a decoder is called with many DIFFERENT indices; constant tables repeat a few values
|
|
230
|
+
if (e.values.size >= HEX_LOOKUP_DISTINCT_MIN && e.calls > hexLookupCalls) {
|
|
231
|
+
hexLookupCalls = e.calls;
|
|
232
|
+
hexLookupCallee = name;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
if (hexIds.size >= HEX_IDENT_MIN)
|
|
236
|
+
mark(firstHexId);
|
|
237
|
+
if (hexLookupCalls >= HEX_LOOKUP_MIN)
|
|
238
|
+
mark(firstLookup.get(hexLookupCallee));
|
|
239
|
+
if (escapedAscii >= ESCAPED_ASCII_MIN)
|
|
240
|
+
mark(firstEscaped);
|
|
241
|
+
return {
|
|
242
|
+
hexIdentifiers: hexIds.size,
|
|
243
|
+
rotator,
|
|
244
|
+
hexLookupCalls,
|
|
245
|
+
hexLookupCallee,
|
|
246
|
+
packer,
|
|
247
|
+
decodedEval,
|
|
248
|
+
escapedAsciiStrings: escapedAscii,
|
|
249
|
+
firstIndex,
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
/** Human-readable list of the signals that crossed their thresholds (empty = not obfuscated). */
|
|
253
|
+
export function signalReasons(s) {
|
|
254
|
+
const out = [];
|
|
255
|
+
if (s.packer)
|
|
256
|
+
out.push("a Dean Edwards packer (eval(function(p,a,c,k,e,d){…}))");
|
|
257
|
+
if (s.decodedEval)
|
|
258
|
+
out.push(`code executed from a decoded string (${s.decodedEval})`);
|
|
259
|
+
if (s.rotator)
|
|
260
|
+
out.push("a string-array rotator (arr.push(arr.shift()) with a checksum loop)");
|
|
261
|
+
if (s.hexIdentifiers >= HEX_IDENT_MIN)
|
|
262
|
+
out.push(`${s.hexIdentifiers} _0x-style hexadecimal identifiers`);
|
|
263
|
+
if (s.hexLookupCalls >= HEX_LOOKUP_MIN)
|
|
264
|
+
out.push(`${s.hexLookupCalls} string-array lookups (${s.hexLookupCallee}(0x…))`);
|
|
265
|
+
if (s.escapedAsciiStrings >= ESCAPED_ASCII_MIN)
|
|
266
|
+
out.push(`${s.escapedAsciiStrings} plain-text strings spelled with \\x/\\u escapes`);
|
|
267
|
+
return out;
|
|
268
|
+
}
|
|
269
|
+
const BANNER_RE = /^\s*(?:\/\*[!*]?|\/\/)\s*(?:@license\s+)?(@?[A-Za-z][\w./-]*)\s+v?(\d+\.\d+\.\d+[\w.-]*)/;
|
|
270
|
+
const NODE_MODULES_RE = /(?:^|\/)node_modules\/((?:@[^/]+\/)?[^/]+)/g;
|
|
271
|
+
const VENDOR_RE = /(?:^|\/)(?:vendor|vendors|third[_-]?party|libs?)\/([^/]+?)(?:(?:\.min)?\.[cm]?js$|\/)/i;
|
|
272
|
+
// Module-path comments bundlers leave between modules: esbuild/rollup `// node_modules/x/index.js`,
|
|
273
|
+
// webpack `/***/ "./node_modules/x/index.js":` and `// CONCATENATED MODULE: ./src/a.js`.
|
|
274
|
+
const MODULE_MARKER_RE = /(?:^|\n)[ \t]*(?:\/\/|\/\*{1,3}\/)[ \t]*(?:CONCATENATED MODULE:[ \t]*)?["']?((?:\.{1,2}\/)?[\w@.+-]+(?:\/[\w@.+-]+)+\.[cm]?[jt]sx?)/g;
|
|
275
|
+
const PKG_IN_PATH_RE = /(?:^|\/)node_modules\/((?:@[^/]+\/)?[^/]+)\/(?!.*node_modules\/)/;
|
|
276
|
+
/** Best-effort name of the bundled npm package the obfuscated code comes from:
|
|
277
|
+
* a license banner, a node_modules/ path, a vendor/ path, or the nearest
|
|
278
|
+
* `node_modules/<pkg>` module-path comment before the obfuscated code in a bundle. */
|
|
279
|
+
export function bundledPackageName(relPath, content, index = null) {
|
|
280
|
+
const b = BANNER_RE.exec(content.slice(0, 1000));
|
|
281
|
+
if (b)
|
|
282
|
+
return `${b[1]}@${b[2]}`;
|
|
283
|
+
const nm = [...relPath.matchAll(NODE_MODULES_RE)];
|
|
284
|
+
if (nm.length)
|
|
285
|
+
return nm[nm.length - 1][1];
|
|
286
|
+
const v = VENDOR_RE.exec(relPath);
|
|
287
|
+
if (v)
|
|
288
|
+
return v[1];
|
|
289
|
+
if (index !== null && index > 0) {
|
|
290
|
+
const before = content.slice(Math.max(0, index - 200_000), index);
|
|
291
|
+
// Only the NEAREST module marker counts: if it is first-party (./src/...), don't blame a package.
|
|
292
|
+
const markers = [...before.matchAll(MODULE_MARKER_RE)];
|
|
293
|
+
const last = markers.length ? markers[markers.length - 1][1] : null;
|
|
294
|
+
const pkg = last ? PKG_IN_PATH_RE.exec(last) : null;
|
|
295
|
+
if (pkg)
|
|
296
|
+
return pkg[1];
|
|
297
|
+
}
|
|
298
|
+
return null;
|
|
299
|
+
}
|
|
300
|
+
function obf001(model) {
|
|
301
|
+
const findings = [];
|
|
302
|
+
for (const script of model.scriptFiles) {
|
|
303
|
+
if (!script.ast)
|
|
304
|
+
continue;
|
|
305
|
+
const s = obfuscationSignals(script.ast);
|
|
306
|
+
const reasons = signalReasons(s);
|
|
307
|
+
if (reasons.length === 0)
|
|
308
|
+
continue;
|
|
309
|
+
const pkg = bundledPackageName(script.relPath, script.content, s.firstIndex);
|
|
310
|
+
const from = pkg ? ` It looks like it comes from the bundled package ${pkg}.` : "";
|
|
311
|
+
findings.push({
|
|
312
|
+
ruleId: "OBF001",
|
|
313
|
+
severity: OBF001_SEVERITY,
|
|
314
|
+
confidence: reasons.length > 1 || s.packer || s.rotator ? "high" : "medium",
|
|
315
|
+
title: "Obfuscated code",
|
|
316
|
+
message: `This file shows obfuscation fingerprints: ${reasons.join("; ")}. The Chrome Web Store rejects obfuscated code (Red Titanium); minification is allowed, obfuscation is not.${from}`,
|
|
317
|
+
file: script.relPath,
|
|
318
|
+
line: s.firstIndex !== null ? findLineByIndex(script.content, s.firstIndex) : undefined,
|
|
319
|
+
chromeViolationId: "Red Titanium",
|
|
320
|
+
fixHint: pkg
|
|
321
|
+
? `Replace ${pkg} with a non-obfuscated build (or another package), or ask its maintainer for readable source. Minified code is fine.`
|
|
322
|
+
: "Ship this code minified instead of obfuscated (drop javascript-obfuscator/packer steps from the build). Minified code is fine.",
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
return findings;
|
|
326
|
+
}
|
|
327
|
+
export const obfuscationRules = [obf001];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tabsmith-lint",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Pre-submission compliance linter for Manifest V3 Chrome extensions: flags likely Chrome Web Store rejection risks (unused/excessive permissions, MV3 violations, broken packaging) before you submit.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|