diff --git a/package.json b/package.json index 1e172e5d32..3a12835bc8 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "bili-proxy": "./dist/index.js" }, "scripts": { - "build": "npm --prefix kernel run build && npm --prefix pi-subagents run build && tsup && node scripts/write-build-info.mjs && node scripts/check-dist-import-annotations.mjs && node scripts/check-dist-external-zod.mjs", + "build": "npm --prefix kernel run build && npm --prefix pi-subagents run build && tsup && node scripts/write-build-info.mjs && node scripts/check-dist-import-annotations.mjs && node scripts/check-dist-external.mjs", "prepare": "node scripts/check-dist-ready.mjs", "registry:snapshot": "node scripts/update-registry-snapshot.mjs", "codex-models:snapshot": "node scripts/update-codex-models-snapshot.mjs", diff --git a/scripts/check-dist-external-zod.mjs b/scripts/check-dist-external-zod.mjs deleted file mode 100644 index 58c766309d..0000000000 --- a/scripts/check-dist-external-zod.mjs +++ /dev/null @@ -1,86 +0,0 @@ -// Build gate: fail if any STATIC import of "zod" survives into dist. -// -// zod is a declared runtime dependency for exactly one sanctioned consumer: -// the opencode V1 host interop (src/agent/opencode-native.ts), which loads it -// LAZILY through an opaque specifier so it resolves the HOST's zod instance -// inside opencode and degrades to proxy mode on thin installs. Everything -// else must be bundled: #3425 inlined jimp (devDependency, noExternal) whose -// module-scope `import { z } from "zod"` leaked 16 static `from"zod"` imports -// into dist/index.js and dist/agent/pi-native.js. Static imports resolve at -// module load with NO degrade path — installs that ship the package without -// its node_modules (dsh profile bare copies, thin plugin copies: the real -// dsh/pi plugin shapes) died with ERR_MODULE_NOT_FOUND and model traffic -// silently went direct, uncompressed (#3511 P0). The fix bundles zod via -// noExternal; this check keeps it bundled. -// -// Violations matched (specifier must be the bare literal "zod"): -// - import ... from "zod" (any static import form) -// - export ... from "zod" -// - require("zod") -// - import("zod") (literal dynamic — esbuild would have -// bundled or rewritten it; the sanctioned -// lazy load goes through a variable) -// -// Bare-specifier NODE builtins are fine (node:*, fs, crypto, ...); other -// packages are inlined by noExternal, so "zod" is the only name that can -// appear here legitimately only through the sanctioned opaque path, which -// this regex cannot match. - -import { readdirSync, readFileSync, realpathSync, statSync } from "node:fs"; -import path from "node:path"; -import process from "node:process"; -import { fileURLToPath } from "node:url"; - -const STATIC_ZOD = [ - /import\s*[^;(]*?from\s*(["'])zod\1/g, - /export\s*[^;(]*?from\s*(["'])zod\1/g, - /require\s*\(\s*(["'])zod\1\s*\)/g, - /import\s*\(\s*(["'])zod\1\s*\)/g, -]; - -export function findStaticZodRefs(text) { - const hits = []; - for (const re of STATIC_ZOD) { - re.lastIndex = 0; - let m; - while ((m = re.exec(text)) !== null) { - const line = text.slice(0, m.index).split("\n").length; - hits.push(`${re.source.slice(0, 40)}… line ${line}`); - } - } - return hits; -} - -export function checkDistExternalZod(distDir) { - const violations = []; - const walk = (dir) => { - for (const name of readdirSync(dir)) { - const full = path.join(dir, name); - const st = statSync(full); - if (st.isDirectory()) { walk(full); continue; } - if (!name.endsWith(".js")) continue; - const refs = findStaticZodRefs(readFileSync(full, "utf8")); - if (refs.length > 0) violations.push({ file: full, refs }); - } - }; - walk(distDir); - return violations; -} - -const invoked = process.argv[1]; -// Realpath both sides: Node resolves entry-point symlinks, so argv[1] can differ from import.meta.url -const isMain = !!invoked && realpathSync(fileURLToPath(import.meta.url)) === realpathSync(invoked); -if (isMain) { - const distDir = process.argv[2] ?? "dist"; - const violations = checkDistExternalZod(distDir); - if (violations.length > 0) { - for (const v of violations) { - console.error(`dist-external-zod: ${v.file}:`); - for (const r of v.refs.slice(0, 5)) console.error(` ${r}`); - if (v.refs.length > 5) console.error(` … (+${v.refs.length - 5} more)`); - } - console.error(`dist-external-zod: FAIL — ${violations.reduce((n, v) => n + v.refs.length, 0)} static zod import ref(s) across ${violations.length} file(s). dist must stay self-contained: bundle zod (tsup noExternal) or load it through the sanctioned opaque lazy path (opencode V1 interop). Static zod imports crash thin installs (no node_modules) with ERR_MODULE_NOT_FOUND (#3511 P0).`); - process.exit(1); - } - console.log("dist-external-zod: OK"); -} diff --git a/scripts/check-dist-external.mjs b/scripts/check-dist-external.mjs new file mode 100644 index 0000000000..fd71a56127 --- /dev/null +++ b/scripts/check-dist-external.mjs @@ -0,0 +1,169 @@ +// Build gate: fail if any external bare import survives into dist outside +// the sanctioned allowlist below. +// +// dist must stay self-contained for thin installs — the package copied +// WITHOUT its node_modules tree (dsh profile bare copies, plugin copies, +// bili's own e2e rigs). A STATIC bare import resolves at module load with NO +// degrade path: the process dies before it binds a port and model traffic +// silently goes direct, uncompressed. History of the class this pins: +// #3425/#3511 P0 — jimp inlining leaked 16 static `from"zod"` imports into +// dist (this script started life as the zod-only guard); +// #3516 — jimp itself moved to external + lazy dynamic load (allowlisted); +// #4008 — mysql2 (declared dep, auto-externalized by tsup) arrived with a +// TOP-LEVEL static import in src/storage/mysql.ts; openBackend's +// internal-module dynamic import got hoisted by the single-file bundle +// and the driver import rode along to the top of dist/index.js and +// dist/agent/pi-native.js — every thin install crashed at startup with +// ERR_MODULE_NOT_FOUND even when sqlite was selected. This +// generalization closes the class: ANY new external specifier fails the +// build unless explicitly listed below WITH a reason, and listed +// specifiers may only appear in their sanctioned forms (dynamic-only +// entries keep their lazy-load contract — a static form of them is still +// a violation). +// +// Node builtins (node:* and module.builtinModules) and relative paths are +// fine. Opaque variable specifiers (`const s="zod"; import(s)`) are invisible +// to a text scan by design — that is the sanctioned lazy path (opencode V1 +// interop loads the HOST's zod instance through a variable; see +// src/agent/opencode-native.ts). A literal `import("zod")` is NOT sanctioned +// (esbuild would bundle or rewrite it) and fails via the allowlist. + +import { builtinModules } from "node:module"; +import { readdirSync, readFileSync, realpathSync, statSync } from "node:fs"; +import path from "node:path"; +import process from "node:process"; +import { fileURLToPath } from "node:url"; + +const NODE_BUILTINS = new Set(builtinModules); + +/** Sanctioned external specifiers. kind "static" covers every form that + * resolves at module load (import decl / export-from / require); "dynamic" + * covers import(). `files` restricts an entry to specific dist-relative + * artifact paths when given. Every entry needs a reason — the allowlist is + * evidence, not convenience. */ +const ALLOWED_EXTERNALS = [ + { + spec: "jimp", + kinds: ["dynamic"], + reason: "#3516 optional image-rescale dependency — loaded at first use (src/image-rescale.ts) and degrades to pass-through when absent; a static form would crash thin installs at module load", + }, + { + spec: "react", + kinds: ["static"], + files: ["agent/dsh-native-client.js"], + reason: "#1590 browser CJS bundle for the dsh web profile — react resolves through dsh's own module system at runtime (classic-script wrapper); nothing else may reference it", + }, + { + spec: "mysql2/promise", + kinds: ["dynamic"], + reason: "#4008 storage driver — loaded at openBackend(\"mysql\") time only (src/storage/backend.ts); a static form crashes thin installs at module load even when sqlite is selected", + }, +]; + +// Import forms, each capturing the bare specifier. Static forms first; the +// dynamic form is separate because its KIND is what the allowlist polices. +const STATIC_REF_PATTERNS = [ + // side-effect: import "x" + /\bimport\s*["']([^"'\n]+)["']/g, + // named: import { a, b as c } from "x" (minified or spaced) + /\bimport\s*\{[^}]*\}\s*from\s*["']([^"'\n]+)["']/g, + // namespace (optionally with named tail): import * as ns from "x" + /\bimport\s*\*\s*as\s+[\w$]+\s*,?\s*(?:\{[^}]*\})?\s*from\s*["']([^"'\n]+)["']/g, + // default + namespace: import def, * as ns from "x" + /\bimport\s+[\w$]+\s*,\s*\*\s*as\s+[\w$]+\s+from\s*["']([^"'\n]+)["']/g, + // default + named: import def, { a } from "x" + /\bimport\s+[\w$]+\s*,\s*\{[^}]*\}\s*from\s*["']([^"'\n]+)["']/g, + // default: import def from "x" + /\bimport\s+[\w$]+\s+from\s*["']([^"'\n]+)["']/g, + // re-export: export * [as ns] from "x" / export { a } from "x" + // (\s* after export and before from: minified output writes + // export{x}from"x" — braces need no separators, unlike identifier-from + // which always has one; exports.foo never matches — the group demands * or {) + /\bexport\s*(?:\*\s*(?:as\s+[\w$]+\s*)?|\{[^}]*\})\s*from\s*["']([^"'\n]+)["']/g, + // CJS require (resolves at call, i.e. factory execution for our bundles) + /\brequire\s*\(\s*["']([^"'\n]+)["']\s*\)/g, +]; +const DYNAMIC_REF_PATTERN = /\bimport\s*\(\s*["']([^"'\n]+)["']\s*\)/g; + +function lineOf(text, index) { + return text.slice(0, index).split("\n").length; +} + +function collectRefs(text, re, kind) { + const refs = []; + re.lastIndex = 0; + let m; + while ((m = re.exec(text)) !== null) { + const spec = m[1]; + if (!spec || spec.startsWith(".") || spec.startsWith("/")) continue; + const base = spec.replace(/^node:/, "").split("/")[0]; + if (NODE_BUILTINS.has(base)) continue; + refs.push({ spec, kind, line: lineOf(text, m.index) }); + } + return refs; +} + +/** All bare external refs in a source text (builtins/relative excluded), + * each tagged with the import kind that produced it. */ +export function findExternalRefs(text) { + const refs = []; + for (const re of STATIC_REF_PATTERNS) refs.push(...collectRefs(text, re, "static")); + refs.push(...collectRefs(text, DYNAMIC_REF_PATTERN, "dynamic")); + return refs; +} + +function classify(refs, relFile) { + const violations = []; + for (const ref of refs) { + const entry = ALLOWED_EXTERNALS.find((e) => e.spec === ref.spec); + if (!entry) { + violations.push(`${ref.kind} ${ref.spec} @ line ${ref.line} — not on the allowlist (bundle it via tsup noExternal, or add it here WITH a reason)`); + continue; + } + if (!entry.kinds.includes(ref.kind)) { + violations.push(`${ref.kind} ${ref.spec} @ line ${ref.line} — allowed kinds: ${entry.kinds.join(", ")} (${entry.reason})`); + continue; + } + if (entry.files && !entry.files.includes(relFile)) { + violations.push(`${ref.kind} ${ref.spec} @ line ${ref.line} — restricted to ${entry.files.join(", ")}`); + } + } + return violations; +} + +/** Walk a dist tree and report every disallowed external ref per file. */ +export function checkDistExternal(distDir) { + const violations = []; + const walk = (dir) => { + for (const name of readdirSync(dir)) { + const full = path.join(dir, name); + const st = statSync(full); + if (st.isDirectory()) { walk(full); continue; } + if (!name.endsWith(".js")) continue; + const relFile = path.relative(distDir, full).split(path.sep).join("/"); + const refs = findExternalRefs(readFileSync(full, "utf8")); + const bad = classify(refs, relFile); + if (bad.length > 0) violations.push({ file: full, refs: bad }); + } + }; + walk(distDir); + return violations; +} + +const invoked = process.argv[1]; +// Realpath both sides: Node resolves entry-point symlinks, so argv[1] can differ from import.meta.url +const isMain = !!invoked && realpathSync(fileURLToPath(import.meta.url)) === realpathSync(invoked); +if (isMain) { + const distDir = process.argv[2] ?? "dist"; + const violations = checkDistExternal(distDir); + if (violations.length > 0) { + for (const v of violations) { + console.error(`dist-external: ${v.file}:`); + for (const r of v.refs.slice(0, 5)) console.error(` ${r}`); + if (v.refs.length > 5) console.error(` … (+${v.refs.length - 5} more)`); + } + console.error(`dist-external: FAIL — ${violations.reduce((n, v) => n + v.refs.length, 0)} disallowed external import ref(s) across ${violations.length} file(s). dist must stay self-contained for thin installs (no node_modules): bundle new deps via tsup noExternal, keep lazy loads DYNAMIC, or extend the allowlist in scripts/check-dist-external.mjs with a reason (#3511/#3516/#4008 class).`); + process.exit(1); + } + console.log("dist-external: OK"); +} diff --git a/scripts/check-dist-thin-install.mjs b/scripts/check-dist-thin-install.mjs index f202fe870d..1709ca2040 100644 --- a/scripts/check-dist-thin-install.mjs +++ b/scripts/check-dist-thin-install.mjs @@ -5,8 +5,8 @@ // 2. every Node-consumed dist/agent/*.js entry IMPORTS in its own process // without throwing — an undeclared runtime dependency fails here at // import time, exactly where it would fail in a real consumer's process -// (static analysis like check-dist-external-zod can only see one known -// offender; this catches any future one). +// (the static gate scripts/check-dist-external.mjs pins the allowlist; +// this smoke proves the installed artifact actually RUNS end to end). // Run after `npm run build` in CI (static-gates). Deliberately NOT part of // the local build chain or the prepare gate: it needs registry access for // the package's declared deps and must never slow a consumer install. diff --git a/src/storage/backend.ts b/src/storage/backend.ts index ffdee5e74f..1ac855e919 100644 --- a/src/storage/backend.ts +++ b/src/storage/backend.ts @@ -1,5 +1,6 @@ import type { SqliteEngineName } from "./driver.ts"; import type { MessageContentStore } from "acp-kernel"; +import type { Mysql2Module } from "./mysql.js"; import { UnifiedStore } from "./store.ts"; /** #3931 / #3388 step 1 — the storage backend contract. @@ -14,10 +15,12 @@ import { UnifiedStore } from "./store.ts"; * * `sqlite` is the default. `mysql` is IMPLEMENTED (#3388 step 2, #3931): * MysqlBackend implements the full contract against MySQL 8.0 (mysql2 - * driver, a bundled dependency — #3931 owner decision). The module loads - * lazily inside openBackend("mysql", dsn), so boots that do not select - * mysql never load mysql2; the `indexPath` argument doubles as the DSN for - * mysql, mirroring how SqliteBackend.open consumes the index path. + * driver — #3931 owner decision). Driver AND module load lazily inside + * openBackend("mysql", dsn) through dynamic imports that survive bundling + * as real runtime imports (#4008: a static import of the driver would hoist + * to the top of dist and crash thin installs), so boots that do not select + * mysql never resolve mysql2; the `indexPath` argument doubles as the DSN + * for mysql, mirroring how SqliteBackend.open consumes the index path. */ export type { @@ -299,8 +302,9 @@ export function listBackendNames(): string[] { * is the DSN (mysql://user:pass@host:port/db), the pool connects lazily, * and `init()` (DDL + schema stamp) must run once before first use — the * contract suite does this in `before`. Unknown names throw with the valid - * list. Async because the mysql module is dynamically imported (#3388 - * step-2 decision: bundled dependency, lazy load). */ + * list. Async because the mysql driver + module are dynamically imported at + * open time (#3388 step-2 decision; #4008: the driver stays external to the + * bundle and must never be statically imported). */ export async function openBackend(name: string, indexPath: string, engine?: SqliteEngineName): Promise { if (name === "sqlite") return SqliteBackend.open(indexPath, engine); if (name === "mysql") { @@ -309,8 +313,20 @@ export async function openBackend(name: string, indexPath: string, engine?: Sqli `[storage] backend "mysql" needs a DSN — set BILI_MYSQL_DSN or storage.mysql.dsn (mysql://user:pass@host:3306/db); runs on "sqlite" until then (#3388/#3931)`, ); } + // #4008: literal dynamic import of an EXTERNAL specifier — it survives + // the single-file bundle as a real runtime import and resolves only + // now, never at module load. The pool itself still opens lazily; this + // only moves driver resolution from boot time to open time. + let driver: Mysql2Module; + try { + driver = await import("mysql2/promise"); + } catch (err) { + throw new Error( + `[storage] mysql backend selected but the mysql2 driver cannot be loaded (${err instanceof Error ? err.message : String(err)}) — installs without a node_modules tree cannot run the mysql backend; set storage.backend="sqlite" or install with dependencies (#4008)`, + ); + } const { MysqlBackend } = await import("./mysql.js"); - return new MysqlBackend(indexPath); + return new MysqlBackend(indexPath, driver); } throw new Error(`[storage] unknown backend "${name}" — valid names: ${REGISTERED_BACKENDS.join(", ")}`); } diff --git a/src/storage/mysql.ts b/src/storage/mysql.ts index 68fe1c93f1..79de335a6c 100644 --- a/src/storage/mysql.ts +++ b/src/storage/mysql.ts @@ -2,10 +2,10 @@ * MySQL backend for the storage contract (#3388 step 2, #3931). * * Implements the same StorageBackend operation surface as the sqlite - * store, against MySQL 8.0 (mysql2 driver, bundled dependency per the - * 2026-10-20 owner decision — see the #3931 comment; this module is only - * ever reached through openBackend("mysql", dsn), so boots that do not - * select mysql never load it). + * store, against MySQL 8.0 (mysql2 driver per the 2026-10-20 owner decision + * — see the #3931 comment). The driver stays EXTERNAL to the bundle (#4008): + * openBackend("mysql", dsn) loads it lazily and injects it into the + * constructor, so boots that do not select mysql never resolve it. * * Representation discipline mirrors src/storage/schema.ts: verbatim facts * (payload_json / content bytes / *_json bags) are the truth; scalar @@ -27,7 +27,7 @@ * hardening plan). */ -import { createPool, type Pool, type PoolConnection, type PoolOptions } from "mysql2/promise"; +import type { Pool, PoolConnection, PoolOptions } from "mysql2/promise"; import { mkdirSync, writeFileSync, appendFileSync, readFileSync, existsSync } from "node:fs"; import { join } from "node:path"; import { log as loggerLog } from "../logger.js"; @@ -46,6 +46,15 @@ import type { IngestSessionInput, } from "./store.js"; +/** The mysql2/promise driver module, injected by openBackend() at open time. + * THIS FILE MUST NEVER HOLD A TOP-LEVEL VALUE IMPORT OF THE DRIVER (#4008): + * under tsup's single-file bundle (splitting:false) any value import here + * hoists to the top of dist/index.js and resolves at MODULE LOAD — crashing + * every install without a node_modules tree with ERR_MODULE_NOT_FOUND even + * when sqlite is selected. Type imports erase at compile time and are safe. + * Same discipline as the jimp lazy load (src/image-rescale.ts, #3516). */ +export type Mysql2Module = typeof import("mysql2/promise"); + const MYSQL_SCHEMA_VERSION = 7; /** Fresh-install DDL: the schema.ts v7 shape in MySQL dialect. LONGTEXT for @@ -222,9 +231,10 @@ function nullableStr(v: unknown): string | null { export class MysqlBackend implements StorageBackend { private readonly pool: Pool; - constructor(dsnOrOpts: string | PoolOptions) { - this.pool = - typeof dsnOrOpts === "string" ? createPool(dsnOrOpts) : createPool(dsnOrOpts); + constructor(dsnOrOpts: string | PoolOptions, driver: Mysql2Module) { + this.pool = typeof dsnOrOpts === "string" + ? driver.createPool(dsnOrOpts) + : driver.createPool(dsnOrOpts); } /** Applies the DDL + schema-version stamp. Idempotent. */ diff --git a/tests/check-dist-external-zod.test.ts b/tests/check-dist-external-zod.test.ts deleted file mode 100644 index c3dbabc839..0000000000 --- a/tests/check-dist-external-zod.test.ts +++ /dev/null @@ -1,47 +0,0 @@ -import test from "node:test"; -import assert from "node:assert/strict"; -const gateSpec = "../scripts/check-dist-external-zod.mjs"; -const { findStaticZodRefs, checkDistExternalZod } = await import(gateSpec) as { - findStaticZodRefs: (text: string) => string[]; - checkDistExternalZod: (distDir: string) => Array<{ file: string; refs: string[] }>; -}; - -/** The guard exists because #3425's jimp inlining leaked 16 STATIC - * `from"zod"` imports into dist — module-load-time imports with no degrade - * path, crashing every thin install (package without node_modules). These - * tests pin the regex so the gate keeps catching exactly that class and - * never flags the sanctioned lazy path (opaque specifier) or plain - * "zod" strings (zod's own vendor markers survive bundling). */ - -test("static zod import forms are violations (the #3425 leak shapes)", () => { - const hits = findStaticZodRefs( - `import{z as z2}from"zod";const r=require("zod");await import("zod");export{x}from"zod";`, - ); - assert.equal(hits.length, 4, "minified static import, require, literal dynamic import, re-export"); -}); - -test("the sanctioned lazy path and incidental zod strings are not violations", () => { - const clean = findStaticZodRefs( - `const zodSpecifier="zod";const z=await import(zodSpecifier);const v={vendor:"zod"};`, - ); - assert.equal(clean.length, 0); -}); - -test("checkDistExternalZod reports per file", async () => { - const { mkdirSync, writeFileSync, rmSync } = await import("node:fs"); - const { tmpdir } = await import("node:os"); - const path = await import("node:path"); - const dir = path.join(tmpdir(), `bili-zod-guard-${process.pid}-${Date.now()}`); - mkdirSync(dir, { recursive: true }); - try { - writeFileSync(path.join(dir, "good.js"), `const s="zod";await import(s);\n`, "utf8"); - writeFileSync(path.join(dir, "bad.js"), `import{z}from"zod";\n`, "utf8"); - writeFileSync(path.join(dir, "not-js.txt"), `import{z}from"zod"\n`, "utf8"); - const violations = checkDistExternalZod(dir); - assert.equal(violations.length, 1); - assert.match(violations[0].file, /bad\.js$/); - assert.equal(violations[0].refs.length, 1); - } finally { - rmSync(dir, { recursive: true, force: true }); - } -}); diff --git a/tests/check-dist-external.test.ts b/tests/check-dist-external.test.ts new file mode 100644 index 0000000000..60a06a6285 --- /dev/null +++ b/tests/check-dist-external.test.ts @@ -0,0 +1,135 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +const gateSpec = "../scripts/check-dist-external.mjs"; +const { findExternalRefs, checkDistExternal } = await import(gateSpec) as { + findExternalRefs: (text: string) => Array<{ spec: string; kind: string; line: number }>; + checkDistExternal: (distDir: string) => Array<{ file: string; refs: string[] }>; +}; + +/** The guard exists because external bare imports in dist kill thin installs + * (package without node_modules): a STATIC one resolves at module load with + * no degrade path — #3425/#3511 (zod leak), #4008 (mysql2 driver hoisted by + * the single-file bundle). These tests pin BOTH directions (#3617): every + * disallowed shape is caught, and every sanctioned lazy path / builtin / + * allowlisted form stays clean. */ + +test("the #4008 shape: minified static mysql2 import is a violation", () => { + const text = 'import{createPool}from"mysql2/promise";'; + const refs = findExternalRefs(text); + assert.equal(refs.length, 1); + assert.deepEqual({ spec: refs[0].spec, kind: refs[0].kind }, { spec: "mysql2/promise", kind: "static" }); +}); + +test("dynamic (lazy) loads of allowlisted externals pass the checker", async () => { + const { mkdirSync, writeFileSync, rmSync } = await import("node:fs"); + const { tmpdir } = await import("node:os"); + const path = await import("node:path"); + const dir = path.join(tmpdir(), `bili-ext-guard-${process.pid}-${Date.now()}`); + mkdirSync(dir, { recursive: true }); + try { + const text = 'const d=await import("mysql2/promise");const j=await import("jimp").catch(()=>null);\n'; + writeFileSync(path.join(dir, "index.js"), text, "utf8"); + const refs = findExternalRefs(text); + assert.equal(refs.length, 2, "both dynamic refs are found..."); + assert.ok(refs.every((r) => r.kind === "dynamic")); + assert.equal(checkDistExternal(dir).length, 0, "...and neither violates (dynamic-only allowlist entries)"); + } finally { + rmSync(dir, { recursive: true, force: true }); + } +}); + +test("zod stays blocked in every literal form (the #3511 pin)", () => { + const hits = findExternalRefs( + 'import{z as z2}from"zod";const r=require("zod");await import("zod");export{x}from"zod";', + ); + assert.equal(hits.length, 4, "minified static import, require, literal dynamic import, re-export"); + assert.ok(hits.every((h) => h.spec === "zod")); +}); + +test("the sanctioned opaque path and incidental strings are invisible", () => { + const clean = findExternalRefs( + 'const zodSpecifier="zod";const z=await import(zodSpecifier);const v={vendor:"zod"};', + ); + assert.equal(clean.length, 0); +}); + +test("node builtins (with and without node: prefix) are clean", () => { + const clean = findExternalRefs( + 'import{readFile}from"node:fs";import crypto from"crypto";const u=require("util");', + ); + assert.equal(clean.length, 0); +}); + +test("an unknown new package fails in every form (no silent new externals)", () => { + for (const text of [ + 'import{a}from"left-pad";', + 'import leftPad from"left-pad";', + 'import*as lp from"left-pad";', + 'import"left-pad";', + 'export{a}from"left-pad";', + 'const l=require("left-pad");', + ]) { + const refs = findExternalRefs(text); + assert.equal(refs.length, 1, `one static ref for: ${text}`); + assert.equal(refs[0].spec, "left-pad"); + assert.equal(refs[0].kind, "static"); + } +}); + +test("a STATIC form of an allowlisted dynamic-only entry still fails", async () => { + // jimp + mysql2 are allowed ONLY as lazy dynamic imports; a static form + // of them is exactly the #3516/#4008 crash shape. + const { mkdirSync, writeFileSync, rmSync } = await import("node:fs"); + const { tmpdir } = await import("node:os"); + const path = await import("node:path"); + const dir = path.join(tmpdir(), `bili-ext-guard-${process.pid}-${Date.now()}`); + mkdirSync(dir, { recursive: true }); + try { + writeFileSync(path.join(dir, "index.js"), 'import Jimp from"jimp";\n', "utf8"); + const violations = checkDistExternal(dir); + assert.equal(violations.length, 1); + assert.match(violations[0].file, /index\.js$/); + assert.equal(violations[0].refs.length, 1); + assert.match(violations[0].refs[0], /allowed kinds: dynamic/); + } finally { + rmSync(dir, { recursive: true, force: true }); + } +}); + +test("react is confined to the dsh browser bundle", async () => { + const { mkdirSync, writeFileSync, rmSync } = await import("node:fs"); + const { tmpdir } = await import("node:os"); + const path = await import("node:path"); + const dir = path.join(tmpdir(), `bili-ext-guard-${process.pid}-${Date.now()}`); + mkdirSync(path.join(dir, "agent"), { recursive: true }); + try { + writeFileSync(path.join(dir, "agent", "dsh-native-client.js"), 'var r=require("react");\n', "utf8"); + let violations = checkDistExternal(dir); + assert.equal(violations.length, 0, "require(\"react\") inside dsh-native-client.js is sanctioned"); + writeFileSync(path.join(dir, "index.js"), 'var r=require("react");\n', "utf8"); + violations = checkDistExternal(dir); + assert.equal(violations.length, 1, "require(\"react\") anywhere else fails"); + assert.match(violations[0].file, /index\.js$/); + } finally { + rmSync(dir, { recursive: true, force: true }); + } +}); + +test("checkDistExternal reports per file and passes a clean tree", async () => { + const { mkdirSync, writeFileSync, rmSync } = await import("node:fs"); + const { tmpdir } = await import("node:os"); + const path = await import("node:path"); + const dir = path.join(tmpdir(), `bili-ext-guard-${process.pid}-${Date.now()}`); + mkdirSync(dir, { recursive: true }); + try { + writeFileSync(path.join(dir, "good.js"), 'const s="zod";await import(s);\n', "utf8"); + writeFileSync(path.join(dir, "bad.js"), 'import{z}from"zod";\n', "utf8"); + writeFileSync(path.join(dir, "not-js.txt"), 'import{z}from"zod"\n', "utf8"); + const violations = checkDistExternal(dir); + assert.equal(violations.length, 1); + assert.match(violations[0].file, /bad\.js$/); + assert.equal(violations[0].refs.length, 1); + } finally { + rmSync(dir, { recursive: true, force: true }); + } +}); diff --git a/tsup.config.ts b/tsup.config.ts index 2622e25bca..3d1d757a4c 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -53,9 +53,10 @@ defineConfig({ // to pass-through when absent (the sharp optionalDependencies precedent). // Explicit external: tsup only auto-externalizes dependencies/peerDeps — // an optionalDependency would otherwise still be bundled. - // Defense in depth: scripts/check-dist-external-zod.mjs (wired into - // `npm run build`) still fails the build if a STATIC zod import ever - // reappears in any dist artifact. + // Defense in depth: scripts/check-dist-external.mjs (wired into + // `npm run build`) fails the build if ANY external bare import appears + // in dist outside its allowlist — static forms of allowlisted lazy deps + // included (#3511 zod, #3516 jimp, #4008 mysql2). external: ["jimp"], noExternal: ["acp-kernel", "billion-context-pi-subagents", "fzstd", "node-forge", "semver", "tar", "undici", "jsonc-parser", "ws"], banner: {