@tauri-cordis/review 0.0.0-stage → 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 +2 -2
- package/package.json +35 -3
- package/src/cli.mjs +79 -0
- package/src/files.mjs +109 -0
- package/src/index.d.mts +93 -0
- package/src/index.mjs +7 -0
- package/src/lexer.mjs +205 -0
- package/src/lockfile.mjs +124 -0
- package/src/markdown.mjs +86 -0
- package/src/review.mjs +207 -0
- package/src/scan.mjs +188 -0
- package/src/version.mjs +106 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 TixXin
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@tauri-cordis/review`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Version-to-version plugin source and bundle diff analyzer (green / yellow / red risk tiers) for [`tauri-cordis`](https://github.com/TixXin/tauri-cordis).
|
package/package.json
CHANGED
|
@@ -1,6 +1,38 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tauri-cordis/review",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Version-to-version plugin source and bundle diff analyzer for tauri-cordis markets",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/TixXin/tauri-cordis.git",
|
|
10
|
+
"directory": "packages/review"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/TixXin/tauri-cordis",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/TixXin/tauri-cordis/issues"
|
|
15
|
+
},
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"src",
|
|
21
|
+
"README.md",
|
|
22
|
+
"LICENSE"
|
|
23
|
+
],
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./src/index.d.mts",
|
|
27
|
+
"default": "./src/index.mjs"
|
|
28
|
+
},
|
|
29
|
+
"./cli": "./src/cli.mjs",
|
|
30
|
+
"./package.json": "./package.json"
|
|
31
|
+
},
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=22.15"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"test": "node --test \"test/**/*.test.mjs\""
|
|
37
|
+
}
|
|
6
38
|
}
|
package/src/cli.mjs
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { realpathSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { resolve } from "node:path";
|
|
4
|
+
import { pathToFileURL } from "node:url";
|
|
5
|
+
import { parseArgs } from "node:util";
|
|
6
|
+
import { renderMarkdown } from "./markdown.mjs";
|
|
7
|
+
import { review } from "./review.mjs";
|
|
8
|
+
|
|
9
|
+
const USAGE = `用法:fuyu-plugin-review <旧版本目录 | -> <新版本目录> [选项]
|
|
10
|
+
|
|
11
|
+
- 没有旧版本(首次上架)
|
|
12
|
+
--old-built 旧版本的构建产物目录
|
|
13
|
+
--new-built 新版本的构建产物目录
|
|
14
|
+
--json <文件> 把 JSON 报告写到文件
|
|
15
|
+
--markdown <文件> 把 Markdown 报告写到文件
|
|
16
|
+
--format <json|markdown> 标准输出的格式,默认 markdown
|
|
17
|
+
|
|
18
|
+
退出码:0 绿档或黄档,1 红档,2 参数或读取出错。`;
|
|
19
|
+
|
|
20
|
+
export function run(argv, io = { stdout: process.stdout, stderr: process.stderr }) {
|
|
21
|
+
let parsed;
|
|
22
|
+
try {
|
|
23
|
+
parsed = parseArgs({
|
|
24
|
+
args: argv,
|
|
25
|
+
allowPositionals: true,
|
|
26
|
+
options: {
|
|
27
|
+
"old-built": { type: "string" },
|
|
28
|
+
"new-built": { type: "string" },
|
|
29
|
+
json: { type: "string" },
|
|
30
|
+
markdown: { type: "string" },
|
|
31
|
+
format: { type: "string", default: "markdown" },
|
|
32
|
+
help: { type: "boolean", short: "h" },
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
} catch (error) {
|
|
36
|
+
io.stderr.write(`${error instanceof Error ? error.message : String(error)}\n\n${USAGE}\n`);
|
|
37
|
+
return 2;
|
|
38
|
+
}
|
|
39
|
+
const { values, positionals } = parsed;
|
|
40
|
+
if (values.help) {
|
|
41
|
+
io.stdout.write(`${USAGE}\n`);
|
|
42
|
+
return 0;
|
|
43
|
+
}
|
|
44
|
+
if (positionals.length !== 2 || !["json", "markdown"].includes(values.format)) {
|
|
45
|
+
io.stderr.write(`${USAGE}\n`);
|
|
46
|
+
return 2;
|
|
47
|
+
}
|
|
48
|
+
const [oldArg, newArg] = positionals;
|
|
49
|
+
let report;
|
|
50
|
+
try {
|
|
51
|
+
report = review({
|
|
52
|
+
oldDir: oldArg === "-" ? null : resolve(oldArg),
|
|
53
|
+
newDir: resolve(newArg),
|
|
54
|
+
oldBuilt: values["old-built"] ? resolve(values["old-built"]) : null,
|
|
55
|
+
newBuilt: values["new-built"] ? resolve(values["new-built"]) : null,
|
|
56
|
+
});
|
|
57
|
+
} catch (error) {
|
|
58
|
+
io.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
|
|
59
|
+
return 2;
|
|
60
|
+
}
|
|
61
|
+
const json = `${JSON.stringify(report, null, 2)}\n`;
|
|
62
|
+
const markdown = renderMarkdown(report);
|
|
63
|
+
if (values.json) writeFileSync(values.json, json);
|
|
64
|
+
if (values.markdown) writeFileSync(values.markdown, markdown);
|
|
65
|
+
io.stdout.write(values.format === "json" ? json : markdown);
|
|
66
|
+
return report.tier === "red" ? 1 : 0;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function isMain() {
|
|
70
|
+
try {
|
|
71
|
+
return Boolean(process.argv[1]) && import.meta.url === pathToFileURL(realpathSync(process.argv[1])).href;
|
|
72
|
+
} catch {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (isMain()) {
|
|
78
|
+
process.exitCode = run(process.argv.slice(2));
|
|
79
|
+
}
|
package/src/files.mjs
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { lstatSync, readdirSync, readFileSync } from "node:fs";
|
|
3
|
+
import { join, relative, sep } from "node:path";
|
|
4
|
+
|
|
5
|
+
const SKIP_DIRS = new Set([".git", "node_modules"]);
|
|
6
|
+
export const CODE_EXTENSIONS = /\.(?:[cm]?js|jsx|[cm]?ts|tsx)$/i;
|
|
7
|
+
export const LOCKFILES = new Set(["package-lock.json", "npm-shrinkwrap.json", "pnpm-lock.yaml"]);
|
|
8
|
+
const EXECUTABLE_EXTENSIONS = new Set([".exe", ".dll", ".sys", ".scr", ".com", ".msi", ".so", ".dylib", ".bin", ".elf", ".app", ".appimage", ".deb", ".rpm", ".pkg", ".dmg", ".wasm", ".jar", ".class"]);
|
|
9
|
+
const SCRIPT_EXTENSIONS = /\.(?:sh|bash|zsh|ps1|psm1|bat|cmd|command|vbs|applescript|scpt)$/i;
|
|
10
|
+
const ASSET_EXTENSIONS = /\.(?:png|jpe?g|gif|webp|ico|icns|bmp|avif|woff2?|ttf|otf|eot|mp3|wav|ogg|m4a|flac|mp4|webm|pdf)$/i;
|
|
11
|
+
const MACHO = new Set([0xfeedface, 0xfeedfacf, 0xcefaedfe, 0xcffaedfe, 0xbebafeca, 0xcafebabf, 0xbfbafeca]);
|
|
12
|
+
|
|
13
|
+
function extensionOf(path) {
|
|
14
|
+
const base = path.slice(path.lastIndexOf("/") + 1).toLowerCase();
|
|
15
|
+
const dot = base.lastIndexOf(".");
|
|
16
|
+
return dot > 0 ? base.slice(dot) : "";
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function zipEntryNames(data) {
|
|
20
|
+
const names = new Set();
|
|
21
|
+
for (const [signature, nameAt, lengthAt] of [[Buffer.from([0x50, 0x4b, 0x03, 0x04]), 30, 26], [Buffer.from([0x50, 0x4b, 0x01, 0x02]), 46, 28]]) {
|
|
22
|
+
for (let at = data.indexOf(signature); at >= 0 && at + nameAt <= data.length; at = data.indexOf(signature, at + 4)) {
|
|
23
|
+
const length = data.readUInt16LE(at + lengthAt);
|
|
24
|
+
if (at + nameAt + length <= data.length) names.add(data.toString("utf8", at + nameAt, at + nameAt + length));
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return [...names];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const isJarEntry = (name) => name === "META-INF/MANIFEST.MF" || name.toLowerCase().endsWith(".class");
|
|
31
|
+
|
|
32
|
+
export function executableKind(data) {
|
|
33
|
+
if (data.length >= 2 && data[0] === 0x4d && data[1] === 0x5a) return "PE";
|
|
34
|
+
if (data.length < 4) return null;
|
|
35
|
+
const magic = data.readUInt32BE(0);
|
|
36
|
+
if (magic === 0x7f454c46) return "ELF";
|
|
37
|
+
if (magic === 0xcafebabe) return data.length >= 8 && data.readUInt32BE(4) >= 45 ? "Java class" : "Mach-O";
|
|
38
|
+
if (MACHO.has(magic)) return "Mach-O";
|
|
39
|
+
if (magic === 0x0061736d) return "WebAssembly";
|
|
40
|
+
if (magic === 0x504b0304 && zipEntryNames(data).some(isJarEntry)) return "Java archive";
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function classify(path, buffer) {
|
|
45
|
+
const binary = buffer.subarray(0, 8000).includes(0);
|
|
46
|
+
const extension = extensionOf(path);
|
|
47
|
+
if (extension === ".node") return { kind: "native", label: ".node" };
|
|
48
|
+
const magic = executableKind(buffer);
|
|
49
|
+
if (magic) return { kind: "executable", label: magic };
|
|
50
|
+
if (EXECUTABLE_EXTENSIONS.has(extension)) return { kind: "executable", label: extension };
|
|
51
|
+
if (SCRIPT_EXTENSIONS.test(path)) return { kind: "script" };
|
|
52
|
+
if (binary) return { kind: ASSET_EXTENSIONS.test(path) ? "asset" : "binary" };
|
|
53
|
+
if (ASSET_EXTENSIONS.test(path)) return { kind: "asset" };
|
|
54
|
+
return { kind: "text" };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function readTree(root) {
|
|
58
|
+
const files = new Map();
|
|
59
|
+
const symlinks = [];
|
|
60
|
+
const walk = (dir) => {
|
|
61
|
+
for (const entry of readdirSync(dir, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
|
|
62
|
+
const full = join(dir, entry.name);
|
|
63
|
+
const path = relative(root, full).split(sep).join("/");
|
|
64
|
+
const stat = lstatSync(full);
|
|
65
|
+
if (stat.isSymbolicLink()) {
|
|
66
|
+
symlinks.push(path);
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
if (stat.isDirectory()) {
|
|
70
|
+
if (!SKIP_DIRS.has(entry.name)) walk(full);
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if (!stat.isFile()) continue;
|
|
74
|
+
const buffer = readFileSync(full);
|
|
75
|
+
const { kind, label } = classify(path, buffer);
|
|
76
|
+
files.set(path, {
|
|
77
|
+
path,
|
|
78
|
+
kind,
|
|
79
|
+
...(label ? { label } : {}),
|
|
80
|
+
size: buffer.length,
|
|
81
|
+
hash: createHash("sha256").update(buffer).digest("hex"),
|
|
82
|
+
text: kind === "text" || kind === "script" ? buffer.toString("utf8") : null,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
walk(root);
|
|
87
|
+
return { root, files, symlinks };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function countLines(text) {
|
|
91
|
+
if (!text) return 0;
|
|
92
|
+
const lines = text.split(/\r?\n/);
|
|
93
|
+
return lines[lines.length - 1] === "" ? lines.length - 1 : lines.length;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function lineDelta(oldText, newText) {
|
|
97
|
+
const counts = new Map();
|
|
98
|
+
const split = (text) => (text ? text.split(/\r?\n/).filter((line, index, all) => index < all.length - 1 || line !== "") : []);
|
|
99
|
+
for (const line of split(oldText)) counts.set(line, (counts.get(line) ?? 0) + 1);
|
|
100
|
+
let added = 0;
|
|
101
|
+
for (const line of split(newText)) {
|
|
102
|
+
const left = counts.get(line) ?? 0;
|
|
103
|
+
if (left > 0) counts.set(line, left - 1);
|
|
104
|
+
else added += 1;
|
|
105
|
+
}
|
|
106
|
+
let removed = 0;
|
|
107
|
+
for (const left of counts.values()) removed += left;
|
|
108
|
+
return { added, removed };
|
|
109
|
+
}
|
package/src/index.d.mts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
export type Tier = "green" | "yellow" | "red";
|
|
2
|
+
export type FindingTier = "red" | "yellow" | "info";
|
|
3
|
+
export type Check = "release" | "manifest" | "nodeApis" | "network" | "dependencies" | "sensitive" | "obfuscation" | "files" | "size";
|
|
4
|
+
|
|
5
|
+
export interface Location {
|
|
6
|
+
file: string;
|
|
7
|
+
line: number;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface Finding {
|
|
11
|
+
tier: FindingTier;
|
|
12
|
+
check: Check;
|
|
13
|
+
message: string;
|
|
14
|
+
detail?: string;
|
|
15
|
+
file?: string;
|
|
16
|
+
line?: number;
|
|
17
|
+
snippet?: string;
|
|
18
|
+
kind?: string;
|
|
19
|
+
lines?: number[];
|
|
20
|
+
locations?: Location[];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface PackageChange {
|
|
24
|
+
name: string;
|
|
25
|
+
versions: string[];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface DependencySummary {
|
|
29
|
+
declared: { field: string; name: string; from: string | null; to: string | null }[];
|
|
30
|
+
lockfile: string | null;
|
|
31
|
+
added: PackageChange[];
|
|
32
|
+
removed: PackageChange[];
|
|
33
|
+
changed: { name: string; from: string[]; to: string[] }[];
|
|
34
|
+
tampered: { name: string; version: string; from: string; to: string }[];
|
|
35
|
+
installScripts: { name: string; version: string }[];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface Report {
|
|
39
|
+
format: "fuyu-plugin-review/1";
|
|
40
|
+
tier: Tier;
|
|
41
|
+
plugin: { name: string | null; from: string | null; to: string | null };
|
|
42
|
+
firstRelease: boolean;
|
|
43
|
+
findings: Finding[];
|
|
44
|
+
summary: {
|
|
45
|
+
nodeApis: { all: string[]; added: string[]; removed: string[]; declared: string[] | null };
|
|
46
|
+
network: { declared: string[]; used: string[]; added: string[]; undeclared: string[] };
|
|
47
|
+
dependencies: DependencySummary;
|
|
48
|
+
manifest: { provides: string[]; replaces: string[]; experimental: string[] };
|
|
49
|
+
files: { added: string[]; removed: string[]; changed: string[] };
|
|
50
|
+
lines: { added: number; removed: number; threshold: number };
|
|
51
|
+
binaries: { file: string; kind: string; label?: string; size: number }[];
|
|
52
|
+
built: boolean;
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface ReviewOptions {
|
|
57
|
+
oldDir?: string | null;
|
|
58
|
+
newDir: string;
|
|
59
|
+
oldBuilt?: string | null;
|
|
60
|
+
newBuilt?: string | null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface LockEntry {
|
|
64
|
+
name: string;
|
|
65
|
+
version: string;
|
|
66
|
+
integrity?: string;
|
|
67
|
+
resolved?: string;
|
|
68
|
+
installScript?: boolean;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface CodeScan {
|
|
72
|
+
modules: { name: string; specifier: string; line: number }[];
|
|
73
|
+
domains: { domain: string; line: number; concatenated: boolean }[];
|
|
74
|
+
sensitive: { kind: string; line: number; snippet: string; detail?: string }[];
|
|
75
|
+
obfuscation: { kind: string; severity: "red" | "yellow"; line: number; snippet: string; detail?: string }[];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface Writable {
|
|
79
|
+
write(chunk: string): unknown;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export declare const FORMAT: "fuyu-plugin-review/1";
|
|
83
|
+
export declare const LINE_THRESHOLD: number;
|
|
84
|
+
export declare function review(options: ReviewOptions): Report;
|
|
85
|
+
export declare function compareVersions(previous: unknown, next: unknown): Report;
|
|
86
|
+
export declare function scanVersion(dir: string, builtDir?: string | null): unknown;
|
|
87
|
+
export declare function scanCode(text: string): CodeScan;
|
|
88
|
+
export declare function executableKind(data: Uint8Array): "PE" | "ELF" | "Mach-O" | "Java class" | "WebAssembly" | "Java archive" | null;
|
|
89
|
+
export declare function renderMarkdown(report: Report): string;
|
|
90
|
+
export declare function parsePackageLock(text: string): Map<string, LockEntry>;
|
|
91
|
+
export declare function parsePnpmLock(text: string): Map<string, LockEntry>;
|
|
92
|
+
export declare function diffLockfiles(before: Map<string, LockEntry>, after: Map<string, LockEntry>): Omit<DependencySummary, "declared" | "lockfile">;
|
|
93
|
+
export declare function run(argv: string[], io?: { stdout: Writable; stderr: Writable }): number;
|
package/src/index.mjs
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { FORMAT, LINE_THRESHOLD, compareVersions, review } from "./review.mjs";
|
|
2
|
+
export { renderMarkdown } from "./markdown.mjs";
|
|
3
|
+
export { scanVersion } from "./version.mjs";
|
|
4
|
+
export { scanCode } from "./scan.mjs";
|
|
5
|
+
export { diffLockfiles, parsePackageLock, parsePnpmLock } from "./lockfile.mjs";
|
|
6
|
+
export { run } from "./cli.mjs";
|
|
7
|
+
export { executableKind } from "./files.mjs";
|
package/src/lexer.mjs
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
const REGEX_AFTER = new Set(["", "(", ",", "=", ":", "[", "!", "&", "|", "?", "{", "}", ";", "+", "-", "*", "%", "<", ">", "~", "^"]);
|
|
2
|
+
const REGEX_AFTER_WORDS = /(?:^|[^\w$])(?:return|typeof|instanceof|in|of|new|delete|void|throw|case|do|else|yield|await)$/;
|
|
3
|
+
|
|
4
|
+
function decodeEscape(text, index) {
|
|
5
|
+
const next = text[index + 1];
|
|
6
|
+
const simple = { n: "\n", t: "\t", r: "\r", b: "\b", f: "\f", v: "\v", 0: "\0" };
|
|
7
|
+
if (next === undefined) return { value: "", length: 1 };
|
|
8
|
+
if (next in simple && !(next === "0" && /\d/.test(text[index + 2] ?? ""))) return { value: simple[next], length: 2 };
|
|
9
|
+
if (next === "x") {
|
|
10
|
+
const hex = text.slice(index + 2, index + 4);
|
|
11
|
+
if (/^[0-9a-fA-F]{2}$/.test(hex)) return { value: String.fromCharCode(parseInt(hex, 16)), length: 4 };
|
|
12
|
+
}
|
|
13
|
+
if (next === "u") {
|
|
14
|
+
if (text[index + 2] === "{") {
|
|
15
|
+
const end = text.indexOf("}", index + 3);
|
|
16
|
+
const hex = end > 0 ? text.slice(index + 3, end) : "";
|
|
17
|
+
if (/^[0-9a-fA-F]{1,6}$/.test(hex) && parseInt(hex, 16) <= 0x10ffff) {
|
|
18
|
+
return { value: String.fromCodePoint(parseInt(hex, 16)), length: end - index + 1 };
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
const hex = text.slice(index + 2, index + 6);
|
|
22
|
+
if (/^[0-9a-fA-F]{4}$/.test(hex)) return { value: String.fromCharCode(parseInt(hex, 16)), length: 6 };
|
|
23
|
+
}
|
|
24
|
+
if (next === "\r" && text[index + 2] === "\n") return { value: "", length: 3 };
|
|
25
|
+
if (next === "\n" || next === "\r") return { value: "", length: 2 };
|
|
26
|
+
return { value: next, length: 2 };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function lastSignificant(out) {
|
|
30
|
+
let end = out.length - 1;
|
|
31
|
+
while (end >= 0 && /\s/.test(out[end])) end -= 1;
|
|
32
|
+
if (end < 0) return "";
|
|
33
|
+
if (/[\w$]/.test(out[end])) {
|
|
34
|
+
let start = end;
|
|
35
|
+
while (start > 0 && /[\w$]/.test(out[start - 1])) start -= 1;
|
|
36
|
+
return out.slice(start, end + 1).join("");
|
|
37
|
+
}
|
|
38
|
+
return out[end];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function lineStarts(text) {
|
|
42
|
+
const starts = [0];
|
|
43
|
+
for (let i = 0; i < text.length; i += 1) if (text[i] === "\n") starts.push(i + 1);
|
|
44
|
+
return starts;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function lineAt(starts, offset) {
|
|
48
|
+
let low = 0;
|
|
49
|
+
let high = starts.length - 1;
|
|
50
|
+
while (low < high) {
|
|
51
|
+
const mid = (low + high + 1) >> 1;
|
|
52
|
+
if (starts[mid] <= offset) low = mid;
|
|
53
|
+
else high = mid - 1;
|
|
54
|
+
}
|
|
55
|
+
return low + 1;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function lex(text) {
|
|
59
|
+
const masked = [];
|
|
60
|
+
const stripped = [];
|
|
61
|
+
const strings = [];
|
|
62
|
+
const templateStack = [];
|
|
63
|
+
let i = 0;
|
|
64
|
+
|
|
65
|
+
const keep = (char) => {
|
|
66
|
+
masked.push(char);
|
|
67
|
+
stripped.push(char);
|
|
68
|
+
};
|
|
69
|
+
const blank = (char, keepInStripped) => {
|
|
70
|
+
const space = char === "\n" ? "\n" : " ";
|
|
71
|
+
masked.push(space);
|
|
72
|
+
stripped.push(keepInStripped ? char : space);
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const readQuoted = (quote) => {
|
|
76
|
+
const start = i;
|
|
77
|
+
let value = "";
|
|
78
|
+
keep(quote);
|
|
79
|
+
i += 1;
|
|
80
|
+
while (i < text.length && text[i] !== quote && text[i] !== "\n") {
|
|
81
|
+
if (text[i] === "\\") {
|
|
82
|
+
const escape = decodeEscape(text, i);
|
|
83
|
+
for (let k = 0; k < escape.length && i + k < text.length; k += 1) blank(text[i + k], true);
|
|
84
|
+
value += escape.value;
|
|
85
|
+
i += escape.length;
|
|
86
|
+
} else {
|
|
87
|
+
value += text[i];
|
|
88
|
+
blank(text[i], true);
|
|
89
|
+
i += 1;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (i < text.length && text[i] === quote) {
|
|
93
|
+
keep(quote);
|
|
94
|
+
i += 1;
|
|
95
|
+
}
|
|
96
|
+
strings.push({ start, end: i, value, quote, template: false, parts: [value] });
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const readTemplate = (resume) => {
|
|
100
|
+
let entry = resume;
|
|
101
|
+
if (!entry) {
|
|
102
|
+
entry = { start: i, end: i, value: "", quote: "`", template: true, parts: [""], expressions: 0 };
|
|
103
|
+
strings.push(entry);
|
|
104
|
+
keep("`");
|
|
105
|
+
i += 1;
|
|
106
|
+
}
|
|
107
|
+
while (i < text.length) {
|
|
108
|
+
const char = text[i];
|
|
109
|
+
if (char === "`") {
|
|
110
|
+
keep("`");
|
|
111
|
+
i += 1;
|
|
112
|
+
entry.end = i;
|
|
113
|
+
entry.value = entry.parts.join("${}");
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
if (char === "$" && text[i + 1] === "{") {
|
|
117
|
+
keep("$");
|
|
118
|
+
keep("{");
|
|
119
|
+
i += 2;
|
|
120
|
+
entry.expressions += 1;
|
|
121
|
+
entry.parts.push("");
|
|
122
|
+
templateStack.push({ entry, depth: 0 });
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
if (char === "\\") {
|
|
126
|
+
const escape = decodeEscape(text, i);
|
|
127
|
+
for (let k = 0; k < escape.length && i + k < text.length; k += 1) blank(text[i + k], true);
|
|
128
|
+
entry.parts[entry.parts.length - 1] += escape.value;
|
|
129
|
+
i += escape.length;
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
entry.parts[entry.parts.length - 1] += char;
|
|
133
|
+
blank(char, true);
|
|
134
|
+
i += 1;
|
|
135
|
+
}
|
|
136
|
+
entry.end = i;
|
|
137
|
+
entry.value = entry.parts.join("${}");
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
const readRegex = () => {
|
|
141
|
+
let inClass = false;
|
|
142
|
+
blank(text[i], true);
|
|
143
|
+
i += 1;
|
|
144
|
+
while (i < text.length && text[i] !== "\n") {
|
|
145
|
+
const char = text[i];
|
|
146
|
+
if (char === "\\") {
|
|
147
|
+
blank(char, true);
|
|
148
|
+
if (i + 1 < text.length) blank(text[i + 1], true);
|
|
149
|
+
i += 2;
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
blank(char, true);
|
|
153
|
+
i += 1;
|
|
154
|
+
if (char === "[") inClass = true;
|
|
155
|
+
else if (char === "]") inClass = false;
|
|
156
|
+
else if (char === "/" && !inClass) break;
|
|
157
|
+
}
|
|
158
|
+
while (i < text.length && /[a-z]/.test(text[i])) keep(text[i++]);
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
while (i < text.length) {
|
|
162
|
+
const char = text[i];
|
|
163
|
+
const next = text[i + 1];
|
|
164
|
+
if (char === "/" && next === "/") {
|
|
165
|
+
while (i < text.length && text[i] !== "\n") blank(text[i++], false);
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
if (char === "/" && next === "*") {
|
|
169
|
+
const end = text.indexOf("*/", i + 2);
|
|
170
|
+
const stop = end < 0 ? text.length : end + 2;
|
|
171
|
+
while (i < stop) blank(text[i++], false);
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
if (char === "'" || char === '"') {
|
|
175
|
+
readQuoted(char);
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
if (char === "`") {
|
|
179
|
+
readTemplate(null);
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
if (char === "/") {
|
|
183
|
+
const previous = lastSignificant(masked);
|
|
184
|
+
if (REGEX_AFTER.has(previous) || REGEX_AFTER_WORDS.test(previous)) {
|
|
185
|
+
readRegex();
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
const top = templateStack[templateStack.length - 1];
|
|
190
|
+
if (top && char === "{") top.depth += 1;
|
|
191
|
+
if (top && char === "}") {
|
|
192
|
+
if (top.depth === 0) {
|
|
193
|
+
templateStack.pop();
|
|
194
|
+
keep("}");
|
|
195
|
+
i += 1;
|
|
196
|
+
readTemplate(top.entry);
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
top.depth -= 1;
|
|
200
|
+
}
|
|
201
|
+
keep(char);
|
|
202
|
+
i += 1;
|
|
203
|
+
}
|
|
204
|
+
return { masked: masked.join(""), stripped: stripped.join(""), strings };
|
|
205
|
+
}
|
package/src/lockfile.mjs
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
function addEntry(packages, name, version, extra) {
|
|
2
|
+
if (!name || !version) return;
|
|
3
|
+
const key = `${name}@${version}`;
|
|
4
|
+
const existing = packages.get(key);
|
|
5
|
+
packages.set(key, { name, version, ...existing, ...Object.fromEntries(Object.entries(extra).filter(([, v]) => v !== undefined && v !== false)) });
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function parsePackageLock(text) {
|
|
9
|
+
const lock = JSON.parse(text);
|
|
10
|
+
const packages = new Map();
|
|
11
|
+
if (lock.packages && typeof lock.packages === "object") {
|
|
12
|
+
for (const [path, entry] of Object.entries(lock.packages)) {
|
|
13
|
+
if (!path || !entry || entry.link) continue;
|
|
14
|
+
const name = entry.name ?? path.slice(path.lastIndexOf("node_modules/") + "node_modules/".length);
|
|
15
|
+
addEntry(packages, name, entry.version, { integrity: entry.integrity, resolved: entry.resolved, installScript: entry.hasInstallScript === true });
|
|
16
|
+
}
|
|
17
|
+
return packages;
|
|
18
|
+
}
|
|
19
|
+
const walk = (dependencies) => {
|
|
20
|
+
for (const [name, entry] of Object.entries(dependencies ?? {})) {
|
|
21
|
+
addEntry(packages, name, entry.version, { integrity: entry.integrity, resolved: entry.resolved });
|
|
22
|
+
walk(entry.dependencies);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
walk(lock.dependencies);
|
|
26
|
+
return packages;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function unquote(value) {
|
|
30
|
+
const trimmed = value.trim();
|
|
31
|
+
if ((trimmed.startsWith("'") && trimmed.endsWith("'")) || (trimmed.startsWith('"') && trimmed.endsWith('"'))) return trimmed.slice(1, -1);
|
|
32
|
+
return trimmed;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function splitPnpmKey(raw) {
|
|
36
|
+
let key = unquote(raw).replace(/^\//, "").replace(/\(.*$/, "");
|
|
37
|
+
const legacy = /^((?:@[^/]+\/)?[^/@]+)\/(\d[^/_]*)(?:_.*)?$/.exec(key);
|
|
38
|
+
if (legacy) return { name: legacy[1], version: legacy[2] };
|
|
39
|
+
key = key.replace(/_.*$/, "");
|
|
40
|
+
const at = key.lastIndexOf("@");
|
|
41
|
+
if (at <= 0) return null;
|
|
42
|
+
return { name: key.slice(0, at), version: key.slice(at + 1) };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function parsePnpmLock(text) {
|
|
46
|
+
const packages = new Map();
|
|
47
|
+
let section = null;
|
|
48
|
+
let current = null;
|
|
49
|
+
for (const line of text.split(/\r?\n/)) {
|
|
50
|
+
if (/^\S/.test(line)) {
|
|
51
|
+
section = line.replace(/:\s*$/, "");
|
|
52
|
+
current = null;
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
if (section !== "packages" && section !== "snapshots") continue;
|
|
56
|
+
const keyMatch = /^ {2}(\S.*?):\s*(\{.*\})?\s*$/.exec(line);
|
|
57
|
+
if (keyMatch) {
|
|
58
|
+
const parsed = splitPnpmKey(keyMatch[1]);
|
|
59
|
+
current = parsed;
|
|
60
|
+
if (parsed && section === "packages") addEntry(packages, parsed.name, parsed.version, {});
|
|
61
|
+
if (parsed && section === "snapshots") addEntry(packages, parsed.name, parsed.version, {});
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
if (!current || section !== "packages") continue;
|
|
65
|
+
const integrity = /integrity:\s*([^,}\s]+)/.exec(line);
|
|
66
|
+
const tarball = /tarball:\s*([^,}\s]+)/.exec(line);
|
|
67
|
+
const install = /^\s+requiresBuild:\s*true/.test(line) || /^\s+hasInstallScript:\s*true/.test(line);
|
|
68
|
+
if (integrity || tarball || install) {
|
|
69
|
+
addEntry(packages, current.name, current.version, { integrity: integrity?.[1], resolved: tarball?.[1], installScript: install });
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return packages;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function readLockfile(tree) {
|
|
76
|
+
for (const [file, parse] of [["pnpm-lock.yaml", parsePnpmLock], ["package-lock.json", parsePackageLock], ["npm-shrinkwrap.json", parsePackageLock]]) {
|
|
77
|
+
const entry = tree.files.get(file);
|
|
78
|
+
if (!entry?.text) continue;
|
|
79
|
+
try {
|
|
80
|
+
return { file, packages: parse(entry.text), error: null };
|
|
81
|
+
} catch (error) {
|
|
82
|
+
return { file, packages: new Map(), error: error instanceof Error ? error.message : String(error) };
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return { file: null, packages: new Map(), error: null };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function byName(packages) {
|
|
89
|
+
const map = new Map();
|
|
90
|
+
for (const entry of packages.values()) {
|
|
91
|
+
if (!map.has(entry.name)) map.set(entry.name, []);
|
|
92
|
+
map.get(entry.name).push(entry);
|
|
93
|
+
}
|
|
94
|
+
return map;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function diffLockfiles(oldPackages, newPackages) {
|
|
98
|
+
const oldByName = byName(oldPackages);
|
|
99
|
+
const newByName = byName(newPackages);
|
|
100
|
+
const added = [];
|
|
101
|
+
const removed = [];
|
|
102
|
+
const changed = [];
|
|
103
|
+
const tampered = [];
|
|
104
|
+
const installScripts = [];
|
|
105
|
+
for (const [name, entries] of newByName) {
|
|
106
|
+
const before = oldByName.get(name);
|
|
107
|
+
const versions = entries.map((e) => e.version).sort();
|
|
108
|
+
if (!before) added.push({ name, versions });
|
|
109
|
+
else {
|
|
110
|
+
const previous = before.map((e) => e.version).sort();
|
|
111
|
+
if (previous.join() !== versions.join()) changed.push({ name, from: previous, to: versions });
|
|
112
|
+
}
|
|
113
|
+
for (const entry of entries) {
|
|
114
|
+
const old = oldPackages.get(`${name}@${entry.version}`);
|
|
115
|
+
if (old && ((old.integrity && entry.integrity && old.integrity !== entry.integrity) || (old.resolved && entry.resolved && old.resolved !== entry.resolved))) {
|
|
116
|
+
tampered.push({ name, version: entry.version, from: old.integrity ?? old.resolved, to: entry.integrity ?? entry.resolved });
|
|
117
|
+
}
|
|
118
|
+
if (entry.installScript && !old?.installScript) installScripts.push({ name, version: entry.version });
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
for (const [name, entries] of oldByName) if (!newByName.has(name)) removed.push({ name, versions: entries.map((e) => e.version).sort() });
|
|
122
|
+
const sort = (list) => list.sort((a, b) => a.name.localeCompare(b.name));
|
|
123
|
+
return { added: sort(added), removed: sort(removed), changed: sort(changed), tampered: sort(tampered), installScripts: sort(installScripts) };
|
|
124
|
+
}
|
package/src/markdown.mjs
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
const TIER_TITLES = {
|
|
2
|
+
green: "🟢 绿档:没有敏感变化,扫一眼差异就能批",
|
|
3
|
+
yellow: "🟡 黄档:下面每一项都要看,必要时请作者说明",
|
|
4
|
+
red: "🔴 红档:自动拒绝",
|
|
5
|
+
};
|
|
6
|
+
const TIER_ICONS = { red: "🔴", yellow: "🟡", info: "⚪" };
|
|
7
|
+
const CHECK_TITLES = {
|
|
8
|
+
release: "上架",
|
|
9
|
+
manifest: "清单",
|
|
10
|
+
nodeApis: "Node 模块",
|
|
11
|
+
network: "联网",
|
|
12
|
+
dependencies: "依赖",
|
|
13
|
+
sensitive: "敏感调用",
|
|
14
|
+
obfuscation: "混淆迹象",
|
|
15
|
+
files: "文件",
|
|
16
|
+
size: "改动量",
|
|
17
|
+
};
|
|
18
|
+
const MAX_FINDINGS = 80;
|
|
19
|
+
|
|
20
|
+
export function code(value) {
|
|
21
|
+
const text = String(value ?? "")
|
|
22
|
+
.replace(/[\r\n\t]+/g, " ")
|
|
23
|
+
.replace(/[\u0000-\u001f\u007f\u2028\u2029]/g, "")
|
|
24
|
+
.slice(0, 120);
|
|
25
|
+
if (!text) return "";
|
|
26
|
+
const longest = Math.max(0, ...(text.match(/`+/g) ?? []).map((run) => run.length));
|
|
27
|
+
const fence = "`".repeat(longest + 1);
|
|
28
|
+
const pad = text.startsWith("`") || text.endsWith("`") || text.startsWith(" ") || text.endsWith(" ") ? " " : "";
|
|
29
|
+
return `${fence}${pad}${text}${pad}${fence}`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function list(values, empty = "无") {
|
|
33
|
+
return values.length ? values.map((value) => code(String(value).replace(/\|/g, "¦"))).join("、") : empty;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function location(finding) {
|
|
37
|
+
if (!finding.file) return "";
|
|
38
|
+
return code(finding.line ? `${finding.file}:${finding.line}` : finding.file);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function findingLine(finding) {
|
|
42
|
+
const parts = [`${TIER_ICONS[finding.tier]} **${finding.message}**`];
|
|
43
|
+
if (finding.detail) parts.push(code(finding.detail));
|
|
44
|
+
const at = location(finding);
|
|
45
|
+
if (at) parts.push(`— ${at}`);
|
|
46
|
+
if (finding.lines && finding.lines.length > 1) parts.push(`(第 ${finding.lines.join("、")} 行)`);
|
|
47
|
+
let line = `- ${parts.join(" ")}`;
|
|
48
|
+
if (finding.snippet) line += `\n ${code(finding.snippet)}`;
|
|
49
|
+
return line;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function renderMarkdown(report) {
|
|
53
|
+
const { summary, plugin } = report;
|
|
54
|
+
const out = [];
|
|
55
|
+
const range = report.firstRelease ? `${code(plugin.to)}(首次上架)` : `${code(plugin.from)} → ${code(plugin.to)}`;
|
|
56
|
+
out.push(`## 插件审核:${TIER_TITLES[report.tier]}`, "", `${code(plugin.name ?? "(没有名字)")} ${range}`, "");
|
|
57
|
+
|
|
58
|
+
const deps = summary.dependencies;
|
|
59
|
+
const depCount = deps.added.length + deps.changed.length + deps.removed.length + deps.tampered.length + deps.declared.length;
|
|
60
|
+
out.push("| 项 | 结果 |", "|---|---|");
|
|
61
|
+
out.push(`| Node 内置模块 | 新增 ${list(summary.nodeApis.added)};全部 ${list(summary.nodeApis.all)} |`);
|
|
62
|
+
out.push(`| 联网域名 | 新增 ${list(summary.network.added)};没声明 ${list(summary.network.undeclared)};清单声明 ${list(summary.network.declared)} |`);
|
|
63
|
+
out.push(`| 依赖 | ${depCount ? `新增 ${deps.added.length}、换版本 ${deps.changed.length}、删除 ${deps.removed.length}、同版本内容变了 ${deps.tampered.length}、package.json 改动 ${deps.declared.length}` : "没有变化"}(${deps.lockfile ? code(deps.lockfile) : "没有锁文件"}) |`);
|
|
64
|
+
out.push(`| 提供 / 替换的服务 | ${list(summary.manifest.provides)} / ${list(summary.manifest.replaces)} |`);
|
|
65
|
+
out.push(`| 实验接口 | ${list(summary.manifest.experimental)} |`);
|
|
66
|
+
out.push(`| 改动 | 新增 ${summary.files.added.length}、修改 ${summary.files.changed.length}、删除 ${summary.files.removed.length} 个文件,+${summary.lines.added} / -${summary.lines.removed} 行(超过 ${summary.lines.threshold} 行算黄档) |`);
|
|
67
|
+
out.push(`| 构建产物 | ${summary.built ? "已一起检查" : "没有提供"} |`, "");
|
|
68
|
+
|
|
69
|
+
const visible = report.findings.filter((f) => f.tier !== "info");
|
|
70
|
+
if (!visible.length) {
|
|
71
|
+
out.push("没有命中任何检查项。");
|
|
72
|
+
} else {
|
|
73
|
+
out.push(report.tier === "red" ? "### 拒绝的原因和其他问题" : "### 要逐项看的");
|
|
74
|
+
const groups = new Map();
|
|
75
|
+
for (const finding of visible.slice(0, MAX_FINDINGS)) {
|
|
76
|
+
if (!groups.has(finding.check)) groups.set(finding.check, []);
|
|
77
|
+
groups.get(finding.check).push(finding);
|
|
78
|
+
}
|
|
79
|
+
for (const [check, findings] of groups) {
|
|
80
|
+
out.push("", `**${CHECK_TITLES[check] ?? check}**`, "", ...findings.map(findingLine));
|
|
81
|
+
}
|
|
82
|
+
if (visible.length > MAX_FINDINGS) out.push("", `还有 ${visible.length - MAX_FINDINGS} 条没列出,完整结果在 JSON 报告里。`);
|
|
83
|
+
}
|
|
84
|
+
out.push("", "<sub>由 `@tauri-cordis/review` 生成。分档只是给维护者的提示:每个版本都要维护者看过差异后点批准。</sub>", "");
|
|
85
|
+
return out.join("\n");
|
|
86
|
+
}
|
package/src/review.mjs
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { LOCKFILES, lineDelta } from "./files.mjs";
|
|
2
|
+
import { diffLockfiles } from "./lockfile.mjs";
|
|
3
|
+
import { emptyVersion, scanVersion } from "./version.mjs";
|
|
4
|
+
|
|
5
|
+
export const FORMAT = "fuyu-plugin-review/1";
|
|
6
|
+
export const LINE_THRESHOLD = 300;
|
|
7
|
+
const TIER_ORDER = { red: 0, yellow: 1, info: 2 };
|
|
8
|
+
|
|
9
|
+
const SENSITIVE_LABELS = {
|
|
10
|
+
"child-process": "运行程序(child_process)",
|
|
11
|
+
vm: "vm 模块",
|
|
12
|
+
eval: "eval 或把字符串当代码执行",
|
|
13
|
+
"function-constructor": "new Function",
|
|
14
|
+
"native-binding": "process.binding / dlopen",
|
|
15
|
+
"import-nonliteral": "非字面量的动态 import()",
|
|
16
|
+
"require-nonliteral": "非字面量的 require()",
|
|
17
|
+
"home-dir": "用户主目录、临时目录或系统环境变量里的路径",
|
|
18
|
+
"absolute-path": "绝对路径",
|
|
19
|
+
"parent-path": "带 .. 的路径",
|
|
20
|
+
"app-data": "浮屿或别的插件的数据目录",
|
|
21
|
+
"dynamic-url": "运行时拼出来的网址",
|
|
22
|
+
};
|
|
23
|
+
const OBFUSCATION_LABELS = {
|
|
24
|
+
"obfuscator-names": "混淆器风格的变量名(_0x…)",
|
|
25
|
+
"char-codes": "用 fromCharCode 拼字符串",
|
|
26
|
+
"escape-density": "密集的 \\x / \\u 转义",
|
|
27
|
+
"long-line": "超长行",
|
|
28
|
+
"hex-block": "大块十六进制",
|
|
29
|
+
"base64-block": "大块 base64",
|
|
30
|
+
"high-entropy": "高熵长字符串",
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
function where(locations, limit = 5) {
|
|
34
|
+
return locations.slice(0, limit).map(({ file, line }) => ({ file, line }));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function hostAllowed(host, network) {
|
|
38
|
+
return network.some((pattern) => (pattern.startsWith("*.") ? host.endsWith(pattern.slice(1)) : host === pattern));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function setDiff(before, after) {
|
|
42
|
+
const old = new Set(before);
|
|
43
|
+
const now = new Set(after);
|
|
44
|
+
return { added: [...now].filter((x) => !old.has(x)).sort(), removed: [...old].filter((x) => !now.has(x)).sort() };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function fileChecks(previous, next, add) {
|
|
48
|
+
const trees = [["", previous.tree, next.tree]];
|
|
49
|
+
if (next.built) trees.push(["built:", previous.built, next.built]);
|
|
50
|
+
for (const [prefix, before, after] of trees) {
|
|
51
|
+
for (const file of after.files.values()) {
|
|
52
|
+
const path = `${prefix}${file.path}`;
|
|
53
|
+
const old = before?.files.get(file.path);
|
|
54
|
+
const changed = !old || old.hash !== file.hash;
|
|
55
|
+
if (file.kind === "native") add("red", "files", "带 Node 原生扩展(.node)", { file: path });
|
|
56
|
+
else if (file.kind === "executable") add("red", "files", "带可执行程序或编译过的代码", { file: path, detail: `${file.label},${file.size} B` });
|
|
57
|
+
else if (file.kind === "script" && changed) add("yellow", "files", old ? "脚本文件有改动" : "新的脚本文件", { file: path });
|
|
58
|
+
else if (file.kind === "binary" && changed) add("yellow", "files", old ? "二进制文件有改动" : "新的二进制文件", { file: path, detail: `${file.size} B` });
|
|
59
|
+
}
|
|
60
|
+
for (const link of after.symlinks) add("yellow", "files", "符号链接", { file: `${prefix}${link}` });
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function lineStats(previous, next) {
|
|
65
|
+
const paths = new Set([...previous.tree.files.keys(), ...next.tree.files.keys()]);
|
|
66
|
+
let added = 0;
|
|
67
|
+
let removed = 0;
|
|
68
|
+
const changed = [];
|
|
69
|
+
const created = [];
|
|
70
|
+
const deleted = [];
|
|
71
|
+
for (const path of [...paths].sort()) {
|
|
72
|
+
const old = previous.tree.files.get(path);
|
|
73
|
+
const now = next.tree.files.get(path);
|
|
74
|
+
if (old && now && old.hash === now.hash) continue;
|
|
75
|
+
if (!old) created.push(path);
|
|
76
|
+
else if (!now) deleted.push(path);
|
|
77
|
+
else changed.push(path);
|
|
78
|
+
if (LOCKFILES.has(path.split("/").pop())) continue;
|
|
79
|
+
const delta = lineDelta(old?.text ?? "", now?.text ?? "");
|
|
80
|
+
added += delta.added;
|
|
81
|
+
removed += delta.removed;
|
|
82
|
+
}
|
|
83
|
+
return { added, removed, files: { added: created, removed: deleted, changed } };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function dependencyChecks(previous, next, firstRelease, add) {
|
|
87
|
+
const before = previous.facts.dependencies;
|
|
88
|
+
const after = next.facts.dependencies;
|
|
89
|
+
const declared = [];
|
|
90
|
+
for (const key of new Set([...Object.keys(before), ...Object.keys(after)])) {
|
|
91
|
+
if (before[key] === after[key]) continue;
|
|
92
|
+
const [field, name] = [key.slice(0, key.indexOf(":")), key.slice(key.indexOf(":") + 1)];
|
|
93
|
+
declared.push({ field, name, from: before[key] ?? null, to: after[key] ?? null });
|
|
94
|
+
add("yellow", "dependencies", before[key] === undefined ? "package.json 新增依赖" : after[key] === undefined ? "package.json 删除依赖" : "package.json 依赖范围变了", { file: "package.json", detail: `${field}: ${name} ${before[key] ?? "—"} → ${after[key] ?? "—"}` });
|
|
95
|
+
}
|
|
96
|
+
const lock = diffLockfiles(previous.lock.packages, next.lock.packages);
|
|
97
|
+
const lockfile = next.lock.file ?? "lockfile";
|
|
98
|
+
for (const item of lock.added) add("yellow", "dependencies", firstRelease ? "锁文件里的包" : "锁文件里新增的包", { file: lockfile, detail: `${item.name}@${item.versions.join(", ")}` });
|
|
99
|
+
for (const item of lock.changed) add("yellow", "dependencies", "锁文件里换了版本的包", { file: lockfile, detail: `${item.name} ${item.from.join(", ")} → ${item.to.join(", ")}` });
|
|
100
|
+
for (const item of lock.removed) add("yellow", "dependencies", "锁文件里删除的包", { file: lockfile, detail: `${item.name}@${item.versions.join(", ")}` });
|
|
101
|
+
for (const item of lock.tampered) add("yellow", "dependencies", "同一版本的包,内容哈希或下载地址变了", { file: lockfile, detail: `${item.name}@${item.version}` });
|
|
102
|
+
for (const item of lock.installScripts) add("yellow", "dependencies", "依赖带安装脚本", { file: lockfile, detail: `${item.name}@${item.version}` });
|
|
103
|
+
if (next.lock.error) add("yellow", "dependencies", "锁文件读不懂", { file: lockfile, detail: next.lock.error });
|
|
104
|
+
const hasRuntimeDeps = Object.keys(after).some((key) => !key.startsWith("peerDependencies:"));
|
|
105
|
+
if (!next.lock.file && hasRuntimeDeps) add("yellow", "dependencies", "有依赖但没有锁文件(package-lock.json 或 pnpm-lock.yaml)", { file: "package.json" });
|
|
106
|
+
const hooks = Object.keys(next.facts.installHooks).filter((hook) => previous.facts.installHooks[hook] !== next.facts.installHooks[hook]);
|
|
107
|
+
for (const hook of hooks) add("yellow", "dependencies", "package.json 的安装脚本变了", { file: "package.json", detail: `scripts.${hook}` });
|
|
108
|
+
return { declared, lockfile: next.lock.file, ...lock };
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function sensitiveChecks(previous, next, add) {
|
|
112
|
+
for (const [key, occurrences] of next.sensitive) {
|
|
113
|
+
const before = previous.sensitive.get(key)?.length ?? 0;
|
|
114
|
+
if (occurrences.length <= before) continue;
|
|
115
|
+
const kind = key.slice(0, key.indexOf("\u0000"));
|
|
116
|
+
const first = occurrences[0];
|
|
117
|
+
const detail = before ? `${before} → ${occurrences.length} 处` : occurrences.length > 1 ? `${occurrences.length} 处` : undefined;
|
|
118
|
+
add("yellow", "sensitive", SENSITIVE_LABELS[kind] ?? kind, { file: first.file, line: first.line, snippet: first.snippet, detail, kind, lines: occurrences.map((o) => o.line).slice(0, 10) });
|
|
119
|
+
}
|
|
120
|
+
const count = (list) => {
|
|
121
|
+
const map = new Map();
|
|
122
|
+
for (const item of list) map.set(`${item.kind}\u0000${item.file}`, (map.get(`${item.kind}\u0000${item.file}`) ?? 0) + 1);
|
|
123
|
+
return map;
|
|
124
|
+
};
|
|
125
|
+
const before = count(previous.obfuscation);
|
|
126
|
+
const seen = new Map();
|
|
127
|
+
for (const item of next.obfuscation) {
|
|
128
|
+
const key = `${item.kind}\u0000${item.file}`;
|
|
129
|
+
seen.set(key, (seen.get(key) ?? 0) + 1);
|
|
130
|
+
if (item.severity === "yellow" && seen.get(key) <= (before.get(key) ?? 0)) continue;
|
|
131
|
+
add(item.severity, "obfuscation", OBFUSCATION_LABELS[item.kind] ?? item.kind, { file: item.file, line: item.line, snippet: item.snippet, detail: item.detail, kind: item.kind });
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function compareVersions(previous, next) {
|
|
136
|
+
const firstRelease = previous === null;
|
|
137
|
+
const base = previous ?? emptyVersion();
|
|
138
|
+
const findings = [];
|
|
139
|
+
const add = (tier, check, message, extra = {}) => findings.push({ tier, check, message, ...Object.fromEntries(Object.entries(extra).filter(([, v]) => v !== undefined)) });
|
|
140
|
+
if (firstRelease) add("yellow", "release", "首次上架,没有可比的旧版本:维护者要通读源码");
|
|
141
|
+
if (next.manifestError) add("red", "manifest", "读不到 package.json", { file: "package.json", detail: next.manifestError });
|
|
142
|
+
|
|
143
|
+
const facts = next.facts;
|
|
144
|
+
if (facts.experimental.length) add("red", "manifest", "用了实验接口(fuyu.experimental 不为空),市场不收", { file: "package.json", detail: facts.experimental.join(", ") });
|
|
145
|
+
for (const field of ["provides", "replaces"]) {
|
|
146
|
+
const diff = setDiff(base.facts[field], facts[field]);
|
|
147
|
+
for (const item of diff.added) add("yellow", "manifest", `fuyu.${field} 新增`, { file: "package.json", detail: item });
|
|
148
|
+
for (const item of diff.removed) add("yellow", "manifest", `fuyu.${field} 删除`, { file: "package.json", detail: item });
|
|
149
|
+
}
|
|
150
|
+
const networkDiff = setDiff(base.facts.network, facts.network);
|
|
151
|
+
for (const host of networkDiff.added) add("yellow", "network", "fuyu.network 新声明的域名", { file: "package.json", detail: host });
|
|
152
|
+
|
|
153
|
+
const modules = setDiff(base.modules.keys(), next.modules.keys());
|
|
154
|
+
for (const name of modules.added) {
|
|
155
|
+
const locations = next.modules.get(name);
|
|
156
|
+
add("yellow", "nodeApis", "新用到的 Node 内置模块", { detail: name, file: locations[0].file, line: locations[0].line, locations: where(locations) });
|
|
157
|
+
}
|
|
158
|
+
if (facts.nodeApis) {
|
|
159
|
+
const undeclared = [...next.modules.keys()].filter((name) => !facts.nodeApis.includes(name)).sort();
|
|
160
|
+
if (undeclared.length) add("yellow", "nodeApis", "代码用到的 Node 模块不在清单的 fuyu.nodeApis 里", { file: "package.json", detail: undeclared.join(", ") });
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const undeclaredDomains = [];
|
|
164
|
+
for (const [host, locations] of [...next.domains].sort(([a], [b]) => a.localeCompare(b))) {
|
|
165
|
+
const isNew = !base.domains.has(host);
|
|
166
|
+
const declared = hostAllowed(host, facts.network);
|
|
167
|
+
if (!declared) undeclaredDomains.push(host);
|
|
168
|
+
if (!isNew && declared) continue;
|
|
169
|
+
const hidden = locations.some((l) => l.concatenated);
|
|
170
|
+
const parts = [isNew ? "新出现的域名" : "域名", declared ? null : "没在 fuyu.network 里声明", hidden ? "由字符串拼接得到" : null].filter(Boolean);
|
|
171
|
+
add("yellow", "network", parts.join(","), { detail: host, file: locations[0].file, line: locations[0].line, locations: where(locations) });
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const dependencies = dependencyChecks(base, next, firstRelease, add);
|
|
175
|
+
sensitiveChecks(base, next, add);
|
|
176
|
+
fileChecks(base, next, add);
|
|
177
|
+
const lines = lineStats(base, next);
|
|
178
|
+
if (!firstRelease && lines.added + lines.removed > LINE_THRESHOLD) {
|
|
179
|
+
add("yellow", "size", `源码改动超过 ${LINE_THRESHOLD} 行`, { detail: `+${lines.added} / -${lines.removed}` });
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
findings.sort((a, b) => TIER_ORDER[a.tier] - TIER_ORDER[b.tier]);
|
|
183
|
+
const tier = findings.some((f) => f.tier === "red") ? "red" : findings.some((f) => f.tier === "yellow") ? "yellow" : "green";
|
|
184
|
+
return {
|
|
185
|
+
format: FORMAT,
|
|
186
|
+
tier,
|
|
187
|
+
plugin: { name: facts.name ?? base.facts.name, from: firstRelease ? null : base.facts.version, to: facts.version },
|
|
188
|
+
firstRelease,
|
|
189
|
+
findings,
|
|
190
|
+
summary: {
|
|
191
|
+
nodeApis: { all: [...next.modules.keys()].sort(), added: modules.added, removed: modules.removed, declared: facts.nodeApis },
|
|
192
|
+
network: { declared: facts.network, used: [...next.domains.keys()].sort(), added: setDiff(base.domains.keys(), next.domains.keys()).added, undeclared: undeclaredDomains },
|
|
193
|
+
dependencies,
|
|
194
|
+
manifest: { provides: facts.provides, replaces: facts.replaces, experimental: facts.experimental },
|
|
195
|
+
files: lines.files,
|
|
196
|
+
lines: { added: lines.added, removed: lines.removed, threshold: LINE_THRESHOLD },
|
|
197
|
+
binaries: [...next.tree.files.values()].filter((f) => f.kind !== "text").map((f) => ({ file: f.path, kind: f.kind, ...(f.label ? { label: f.label } : {}), size: f.size })),
|
|
198
|
+
built: Boolean(next.built),
|
|
199
|
+
},
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export function review({ oldDir = null, newDir, oldBuilt = null, newBuilt = null }) {
|
|
204
|
+
if (!newDir) throw new TypeError("newDir is required");
|
|
205
|
+
const previous = oldDir ? scanVersion(oldDir, oldBuilt) : null;
|
|
206
|
+
return compareVersions(previous, scanVersion(newDir, newBuilt));
|
|
207
|
+
}
|
package/src/scan.mjs
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import { builtinModules } from "node:module";
|
|
2
|
+
import { lex, lineAt, lineStarts } from "./lexer.mjs";
|
|
3
|
+
|
|
4
|
+
const BUILTINS = new Set(builtinModules.filter((name) => !name.startsWith("_")).map((name) => name.split("/")[0]));
|
|
5
|
+
const FILE_EXTENSIONS = new Set(["js", "mjs", "cjs", "ts", "mts", "cts", "jsx", "tsx", "json", "css", "html", "htm", "png", "jpg", "jpeg", "gif", "svg", "webp", "ico", "md", "txt", "map", "node", "wasm", "lock", "yaml", "yml", "toml", "xml", "zip", "fuyu", "log", "csv", "py", "rs", "sh", "ps1", "exe", "dll", "mp3", "wav", "ogg", "woff", "woff2", "ttf"]);
|
|
6
|
+
const PROPERTY_LIKE = new Set(["id", "in", "is", "it", "at", "as", "to", "do", "on", "no", "or", "so", "go", "my", "me", "us"]);
|
|
7
|
+
const COMMON_TLDS = new Set(["com", "net", "org", "io", "dev", "app", "ai", "xyz", "info", "biz", "top", "site", "online", "tech", "cloud", "store", "shop", "live", "pro", "club", "vip", "link", "click", "fun", "space", "website", "page", "run", "workers", "pages", "ngrok", "onion", "gov", "edu", "mil", "int", "museum", "name", "mobi"]);
|
|
8
|
+
const IGNORED_HOSTS = /^(?:localhost|127(?:\.\d{1,3}){3}|0\.0\.0\.0|\[::1\]|www\.w3\.org|(?:[\w-]+\.)*example\.(?:com|org|net))$/i;
|
|
9
|
+
const URL_PATTERN = /\b(?:https?|wss?|ftp):\/\/([^/\s"'`:?#$\\]+)/gi;
|
|
10
|
+
const SCHEME_ONLY = /^(?:https?|wss?):\/\/[^/?#]*$/i;
|
|
11
|
+
const IMPORT_BEFORE = /(?:(?:^|[^\w$.])from|(?:^|[^\w$.])import|(?:^|[^\w$.])require\s*\(|(?:^|[^\w$.])import\s*\()\s*$/;
|
|
12
|
+
|
|
13
|
+
export function normalizeHost(host) {
|
|
14
|
+
return host.toLowerCase().replace(/\.$/, "");
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function isHost(value, strict) {
|
|
18
|
+
const host = normalizeHost(value);
|
|
19
|
+
if (/^\d{1,3}(?:\.\d{1,3}){3}$/.test(host)) return true;
|
|
20
|
+
if (!/^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,24}$/.test(host)) return false;
|
|
21
|
+
const tld = host.slice(host.lastIndexOf(".") + 1);
|
|
22
|
+
if (FILE_EXTENSIONS.has(tld)) return false;
|
|
23
|
+
if (!strict) return true;
|
|
24
|
+
if (PROPERTY_LIKE.has(tld)) return false;
|
|
25
|
+
return tld.length === 2 || COMMON_TLDS.has(tld);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function hostsIn(value, strict) {
|
|
29
|
+
const found = [];
|
|
30
|
+
for (const match of value.matchAll(URL_PATTERN)) {
|
|
31
|
+
const host = normalizeHost(match[1].replace(/^[^@]*@/, ""));
|
|
32
|
+
if (isHost(host, false)) found.push(host);
|
|
33
|
+
}
|
|
34
|
+
if (!found.length && isHost(value.trim(), strict)) found.push(normalizeHost(value.trim()));
|
|
35
|
+
return found.filter((host) => !IGNORED_HOSTS.test(host));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function chains(masked, strings) {
|
|
39
|
+
const sorted = [...strings].sort((a, b) => a.start - b.start);
|
|
40
|
+
const result = [];
|
|
41
|
+
let index = 0;
|
|
42
|
+
while (index < sorted.length) {
|
|
43
|
+
const chain = [sorted[index]];
|
|
44
|
+
while (index + 1 < sorted.length && /^\s*\+\s*$/.test(masked.slice(sorted[index].end, sorted[index + 1].start))) {
|
|
45
|
+
index += 1;
|
|
46
|
+
chain.push(sorted[index]);
|
|
47
|
+
}
|
|
48
|
+
const last = chain[chain.length - 1];
|
|
49
|
+
result.push({ parts: chain, value: chain.map((s) => s.value).join(""), start: chain[0].start, plusAfter: /^\s*\+/.test(masked.slice(last.end, last.end + 40)) });
|
|
50
|
+
index += 1;
|
|
51
|
+
}
|
|
52
|
+
for (let i = 0; i < sorted.length; i += 1) {
|
|
53
|
+
if (!/\[\s*$/.test(masked.slice(Math.max(0, sorted[i].start - 20), sorted[i].start))) continue;
|
|
54
|
+
const parts = [sorted[i]];
|
|
55
|
+
let j = i;
|
|
56
|
+
while (j + 1 < sorted.length && /^\s*,\s*$/.test(masked.slice(sorted[j].end, sorted[j + 1].start))) parts.push(sorted[++j]);
|
|
57
|
+
const tail = masked.slice(sorted[j].end, sorted[j].end + 40);
|
|
58
|
+
const join = /^\s*,?\s*\]\s*\.\s*join\s*\(\s*(\S?)/.exec(tail);
|
|
59
|
+
if (parts.length < 2 || !join) continue;
|
|
60
|
+
const separatorString = join[1] === '"' || join[1] === "'" || join[1] === "`" ? sorted[j + 1] : null;
|
|
61
|
+
const separator = separatorString ? separatorString.value : join[1] === ")" ? "," : "";
|
|
62
|
+
result.push({ parts, value: parts.map((s) => s.value).join(separator), start: parts[0].start, plusAfter: false });
|
|
63
|
+
}
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function moduleSpecifiers(masked, strings) {
|
|
68
|
+
return strings
|
|
69
|
+
.filter((s) => !(s.template && s.expressions) && IMPORT_BEFORE.test(masked.slice(Math.max(0, s.start - 40), s.start)))
|
|
70
|
+
.map((s) => ({ specifier: s.value, start: s.start, string: s }));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function builtinName(specifier) {
|
|
74
|
+
const bare = specifier.startsWith("node:") ? specifier.slice(5) : specifier;
|
|
75
|
+
const name = bare.split("/")[0];
|
|
76
|
+
if (specifier.startsWith("node:")) return name;
|
|
77
|
+
return BUILTINS.has(name) ? name : null;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function dynamicLoads(masked, strings, push) {
|
|
81
|
+
const byStart = new Map(strings.map((s) => [s.start, s]));
|
|
82
|
+
for (const match of masked.matchAll(/(?<![\w$.])(import|require)\s*\(/g)) {
|
|
83
|
+
let position = match.index + match[0].length;
|
|
84
|
+
while (/\s/.test(masked[position] ?? "")) position += 1;
|
|
85
|
+
const literal = byStart.get(position);
|
|
86
|
+
const literalOk = literal && !(literal.template && literal.expressions) && /^\s*[),]/.test(masked.slice(literal.end, literal.end + 20));
|
|
87
|
+
if (!literalOk) push(match[1] === "require" ? "require-nonliteral" : "import-nonliteral", match.index);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function entropy(value) {
|
|
92
|
+
const counts = new Map();
|
|
93
|
+
for (const char of value) counts.set(char, (counts.get(char) ?? 0) + 1);
|
|
94
|
+
let bits = 0;
|
|
95
|
+
for (const count of counts.values()) {
|
|
96
|
+
const p = count / value.length;
|
|
97
|
+
bits -= p * Math.log2(p);
|
|
98
|
+
}
|
|
99
|
+
return bits;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const SENSITIVE_CODE = [
|
|
103
|
+
["eval", /(?<![\w$])eval\s*\(|\(\s*0\s*,\s*eval\s*\)/g],
|
|
104
|
+
["function-constructor", /(?<![\w$.])(?:new\s+)?Function\s*\(/g],
|
|
105
|
+
["eval", /(?<![\w$.])set(?:Timeout|Interval|Immediate)\s*\(\s*["'`]/g],
|
|
106
|
+
["native-binding", /\bprocess\s*\.\s*(?:binding|_linkedBinding|dlopen)\s*\(/g],
|
|
107
|
+
["home-dir", /\b(?:homedir|tmpdir|userInfo)\s*\(/g],
|
|
108
|
+
];
|
|
109
|
+
const SENSITIVE_STRIPPED = [["home-dir", /\bprocess\s*\.\s*env\s*(?:\.\s*|\[\s*["'`])(?:HOME|USERPROFILE|APPDATA|LOCALAPPDATA|PROGRAMDATA|TEMP|TMP|TMPDIR|XDG_[A-Z_]+)\b/g]];
|
|
110
|
+
const ABSOLUTE_PATH = /^(?:\/(?:Users|home|etc|var|tmp|private|Library|System|usr|opt|root|Applications|Volumes)(?:\/|$)|[A-Za-z]:[\\/]|\\\\[^\\]|~[\\/])/;
|
|
111
|
+
const HOME_IN_STRING = /(?:^|[\s"'=:])~[\\/]|\$HOME\b|%(?:USERPROFILE|APPDATA|LOCALAPPDATA)%|(?:^|[\\/])\.ssh(?:[\\/]|$)/i;
|
|
112
|
+
const PARENT_PATH = /(?:^|[\\/])\.\.(?:[\\/]|$)/;
|
|
113
|
+
const APP_DATA = /com\.tixxin\.fuyu|com\.focusd\.island|focusd-island|fly-island|(?:^|[\\/])\.fuyu(?:[\\/]|$)|[\\/]plugins[\\/]@?[\w.-]+|agent-status\.json/i;
|
|
114
|
+
|
|
115
|
+
export function scanCode(text) {
|
|
116
|
+
const { masked, stripped, strings } = lex(text);
|
|
117
|
+
const starts = lineStarts(text);
|
|
118
|
+
const lines = text.split(/\r?\n/);
|
|
119
|
+
const at = (offset) => lineAt(starts, offset);
|
|
120
|
+
const snippet = (line) => (lines[line - 1] ?? "").trim().slice(0, 160);
|
|
121
|
+
const sensitive = [];
|
|
122
|
+
const push = (kind, offset, detail) => {
|
|
123
|
+
const line = at(offset);
|
|
124
|
+
sensitive.push({ kind, line, snippet: snippet(line), ...(detail ? { detail } : {}) });
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const modules = [];
|
|
128
|
+
const specifierStrings = new Set();
|
|
129
|
+
for (const { specifier, start, string } of moduleSpecifiers(masked, strings)) {
|
|
130
|
+
specifierStrings.add(string);
|
|
131
|
+
const name = builtinName(specifier);
|
|
132
|
+
if (!name) continue;
|
|
133
|
+
modules.push({ name, specifier, line: at(start) });
|
|
134
|
+
if (name === "child_process") push("child-process", start, specifier);
|
|
135
|
+
if (name === "vm") push("vm", start, specifier);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
dynamicLoads(masked, strings, push);
|
|
139
|
+
for (const [kind, pattern] of SENSITIVE_CODE) for (const match of masked.matchAll(pattern)) push(kind, match.index);
|
|
140
|
+
for (const [kind, pattern] of SENSITIVE_STRIPPED) for (const match of stripped.matchAll(pattern)) push(kind, match.index);
|
|
141
|
+
|
|
142
|
+
for (const s of strings) {
|
|
143
|
+
if (specifierStrings.has(s)) continue;
|
|
144
|
+
for (const part of s.parts) {
|
|
145
|
+
if (ABSOLUTE_PATH.test(part)) push("absolute-path", s.start, part.slice(0, 80));
|
|
146
|
+
else if (PARENT_PATH.test(part)) push("parent-path", s.start, part.slice(0, 80));
|
|
147
|
+
if (HOME_IN_STRING.test(part)) push("home-dir", s.start, part.slice(0, 80));
|
|
148
|
+
if (APP_DATA.test(part)) push("app-data", s.start, part.slice(0, 80));
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const domains = [];
|
|
153
|
+
const seen = new Set();
|
|
154
|
+
for (const chain of chains(masked, strings)) {
|
|
155
|
+
if (chain.parts.length === 1 && specifierStrings.has(chain.parts[0])) continue;
|
|
156
|
+
const joined = chain.parts.length > 1;
|
|
157
|
+
for (const host of hostsIn(chain.value, !joined || chain.value.length < 80)) {
|
|
158
|
+
const key = `${host}:${chain.start}`;
|
|
159
|
+
if (seen.has(key)) continue;
|
|
160
|
+
seen.add(key);
|
|
161
|
+
domains.push({ domain: host, line: at(chain.start), concatenated: joined && !chain.parts.some((p) => hostsIn(p.value, true).includes(host)) });
|
|
162
|
+
}
|
|
163
|
+
const first = chain.parts[0];
|
|
164
|
+
const runtimeUrl = (SCHEME_ONLY.test(chain.value) && chain.plusAfter) || (first.template && first.expressions && SCHEME_ONLY.test(first.parts[0]));
|
|
165
|
+
if (runtimeUrl) push("dynamic-url", chain.start, chain.value.slice(0, 80));
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const obfuscation = [];
|
|
169
|
+
const flag = (kind, severity, line, detail) => obfuscation.push({ kind, severity, line, snippet: snippet(line).slice(0, 80), ...(detail ? { detail } : {}) });
|
|
170
|
+
const hexNames = new Set(masked.match(/\b_0x[0-9a-f]{4,}\b/gi) ?? []);
|
|
171
|
+
if (hexNames.size >= 3) flag("obfuscator-names", "red", at(masked.search(/\b_0x[0-9a-f]{4,}\b/i)), `${hexNames.size}`);
|
|
172
|
+
for (const match of masked.matchAll(/fromCharCode\s*\(\s*(?:0x[0-9a-f]+|\d+)(?:\s*,\s*(?:0x[0-9a-f]+|\d+)){7,}/gi)) flag("char-codes", "red", at(match.index));
|
|
173
|
+
lines.forEach((line, index) => {
|
|
174
|
+
const escapes = line.match(/\\x[0-9a-fA-F]{2}|\\u[0-9a-fA-F]{4}|\\u\{[0-9a-fA-F]+\}/g) ?? [];
|
|
175
|
+
const escapeLength = escapes.reduce((sum, e) => sum + e.length, 0);
|
|
176
|
+
if (escapes.length >= 8 && escapeLength >= line.trim().length * 0.4) flag("escape-density", "red", index + 1, `${escapes.length}`);
|
|
177
|
+
if (line.length > 1000) flag("long-line", "yellow", index + 1, `${line.length}`);
|
|
178
|
+
});
|
|
179
|
+
for (const s of strings) {
|
|
180
|
+
const value = s.value;
|
|
181
|
+
if (value.length < 160) continue;
|
|
182
|
+
if (/(?:[0-9a-fA-F]{2}){80,}/.test(value)) flag("hex-block", "yellow", at(s.start), `${value.length}`);
|
|
183
|
+
else if (/[A-Za-z0-9+/_-]{160,}={0,2}/.test(value)) flag("base64-block", "yellow", at(s.start), `${value.length}`);
|
|
184
|
+
else if (value.length >= 200 && entropy(value) >= 4.5) flag("high-entropy", "yellow", at(s.start), `${value.length}`);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
return { modules, domains, sensitive, obfuscation };
|
|
188
|
+
}
|
package/src/version.mjs
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { CODE_EXTENSIONS, readTree } from "./files.mjs";
|
|
2
|
+
import { readLockfile } from "./lockfile.mjs";
|
|
3
|
+
import { scanCode } from "./scan.mjs";
|
|
4
|
+
|
|
5
|
+
const DEPENDENCY_FIELDS = ["dependencies", "optionalDependencies", "peerDependencies", "devDependencies"];
|
|
6
|
+
const INSTALL_HOOKS = ["preinstall", "install", "postinstall", "prepare", "prepublish", "preprepare", "postprepare"];
|
|
7
|
+
|
|
8
|
+
function list(value) {
|
|
9
|
+
return Array.isArray(value) ? value.filter((item) => typeof item === "string") : [];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function readManifest(tree) {
|
|
13
|
+
const entry = tree.files.get("package.json");
|
|
14
|
+
if (!entry?.text) return { manifest: null, error: "missing" };
|
|
15
|
+
try {
|
|
16
|
+
const manifest = JSON.parse(entry.text);
|
|
17
|
+
if (!manifest || typeof manifest !== "object" || Array.isArray(manifest)) return { manifest: null, error: "not an object" };
|
|
18
|
+
return { manifest, error: null };
|
|
19
|
+
} catch (error) {
|
|
20
|
+
return { manifest: null, error: error instanceof Error ? error.message : String(error) };
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function manifestFacts(manifest) {
|
|
25
|
+
const fuyu = manifest?.fuyu && typeof manifest.fuyu === "object" ? manifest.fuyu : {};
|
|
26
|
+
const dependencies = {};
|
|
27
|
+
for (const field of DEPENDENCY_FIELDS) {
|
|
28
|
+
const value = manifest?.[field];
|
|
29
|
+
if (value && typeof value === "object") for (const [name, range] of Object.entries(value)) dependencies[`${field}:${name}`] = String(range);
|
|
30
|
+
}
|
|
31
|
+
const scripts = {};
|
|
32
|
+
for (const hook of INSTALL_HOOKS) if (typeof manifest?.scripts?.[hook] === "string") scripts[hook] = manifest.scripts[hook];
|
|
33
|
+
return {
|
|
34
|
+
name: typeof manifest?.name === "string" ? manifest.name : null,
|
|
35
|
+
version: typeof manifest?.version === "string" ? manifest.version : null,
|
|
36
|
+
network: list(fuyu.network).map((host) => host.toLowerCase()),
|
|
37
|
+
provides: list(fuyu.provides),
|
|
38
|
+
replaces: list(fuyu.replaces),
|
|
39
|
+
experimental: list(fuyu.experimental),
|
|
40
|
+
nodeApis: Array.isArray(fuyu.nodeApis) ? list(fuyu.nodeApis) : null,
|
|
41
|
+
dependencies,
|
|
42
|
+
installHooks: scripts,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function aggregate(tree, prefix, withObfuscation) {
|
|
47
|
+
const modules = new Map();
|
|
48
|
+
const domains = new Map();
|
|
49
|
+
const sensitive = new Map();
|
|
50
|
+
const obfuscation = [];
|
|
51
|
+
const add = (map, key, value) => {
|
|
52
|
+
if (!map.has(key)) map.set(key, []);
|
|
53
|
+
map.get(key).push(value);
|
|
54
|
+
};
|
|
55
|
+
for (const file of tree.files.values()) {
|
|
56
|
+
if (!file.text || !CODE_EXTENSIONS.test(file.path)) continue;
|
|
57
|
+
const path = `${prefix}${file.path}`;
|
|
58
|
+
const scan = scanCode(file.text);
|
|
59
|
+
for (const item of scan.modules) add(modules, item.name, { file: path, line: item.line });
|
|
60
|
+
for (const item of scan.domains) add(domains, item.domain, { file: path, line: item.line, concatenated: item.concatenated });
|
|
61
|
+
for (const item of scan.sensitive) add(sensitive, `${item.kind}\u0000${path}`, { ...item, file: path });
|
|
62
|
+
if (withObfuscation) for (const item of scan.obfuscation) obfuscation.push({ ...item, file: path });
|
|
63
|
+
}
|
|
64
|
+
return { modules, domains, sensitive, obfuscation };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function scanVersion(dir, builtDir) {
|
|
68
|
+
const tree = readTree(dir);
|
|
69
|
+
const { manifest, error } = readManifest(tree);
|
|
70
|
+
const source = aggregate(tree, "", true);
|
|
71
|
+
const built = builtDir ? readTree(builtDir) : null;
|
|
72
|
+
const builtScan = built ? aggregate(built, "built:", false) : null;
|
|
73
|
+
const merge = (key) => {
|
|
74
|
+
const map = new Map(source[key]);
|
|
75
|
+
if (builtScan) for (const [k, v] of builtScan[key]) map.set(k, [...(map.get(k) ?? []), ...v]);
|
|
76
|
+
return map;
|
|
77
|
+
};
|
|
78
|
+
return {
|
|
79
|
+
dir,
|
|
80
|
+
tree,
|
|
81
|
+
built,
|
|
82
|
+
manifestError: error,
|
|
83
|
+
facts: manifestFacts(manifest),
|
|
84
|
+
lock: readLockfile(tree),
|
|
85
|
+
modules: merge("modules"),
|
|
86
|
+
domains: merge("domains"),
|
|
87
|
+
sensitive: merge("sensitive"),
|
|
88
|
+
obfuscation: source.obfuscation,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function emptyVersion() {
|
|
93
|
+
const tree = { root: null, files: new Map(), symlinks: [] };
|
|
94
|
+
return {
|
|
95
|
+
dir: null,
|
|
96
|
+
tree,
|
|
97
|
+
built: null,
|
|
98
|
+
manifestError: null,
|
|
99
|
+
facts: manifestFacts(null),
|
|
100
|
+
lock: { file: null, packages: new Map(), error: null },
|
|
101
|
+
modules: new Map(),
|
|
102
|
+
domains: new Map(),
|
|
103
|
+
sensitive: new Map(),
|
|
104
|
+
obfuscation: [],
|
|
105
|
+
};
|
|
106
|
+
}
|