diff --git a/CHANGELOG.md b/CHANGELOG.md index 6fa1822..856d5af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added +- **WASM JS-loader hook ESM auto-wire (`detectWasmJsLoaderCarrier` / `esmStaticImportsAreNodeBuiltinsOnly` / `listStaticEsmModuleSpecifiers`, `src/shields/runtime/sidecar-entangle.ts`; protect + harden post-pass):** Extends the 2026-11-16 Node CJS auto-wire so a same-directory Node.js ESM loader (`.mjs` or ESM `.js`) can also receive the already-landed `generateSidecarWasmLoaderSnippet` hook (`carrier: "module"`, `import { createRequire } from "node:module"` + `import.meta.url`). ESM is admitted only when every static `import` / `export-from` specifier is a Node builtin (`fs`, `node:path`, `node:url`, …), so hoisting cannot run user-module side effects or instantiate WASM before the hook. Known names stay `.*`, `loader.*`, `index.*`; `.cjs` still wins a same-rank tie, then `.js`, then `.mjs`. Browser `fetch`-only loaders, ESM files that statically import user modules or `.wasm`, minified / test files, and already-entangled entries remain untouched. Vector `wasm-sidecar-entangle` is recorded and `wasm-sidecar-host-verify-only` is cleared only when the hook was actually written. Default still OFF via existing `sidecarEntangle`. +- **defenseScore honesty:** `jsModuleHasHoistedDependencyGraph` no longer treats Node builtin-only static imports as a user hoisted graph, so a dedicated ESM WASM loader does not get a spurious `esm-import-hoist-before-hooks` soft-cap. User-module `import` / `export-from` (e.g. `./lib.js`) is still flagged. +- **`REA_INTERNAL_LIMITS` catalog hygiene:** Inspection / walk ceilings from REA tip `05be423ac` (DWARF units, ELF debug-link, ELF DT_NEEDED, Maven/Gradle metadata, NuGet `.nupkg`, Android OTA / boot image). Honesty catalog only, not densify trip targets. + +### Notes +- REA tip `05be423ac` (+41 since `26d88826c`): new offline analyzers (Android OTA / boot, Maven POM + Gradle module, NuGet, DWARF compile units, ELF debug-link, ELF dynamic dependencies, CycloneDX Go/Rust projection) plus PE certificate-table alignment padding reject (#11823), HAR body-identity (#11829), and more LimitRef typed refusals. **No new 1:1 trip-able densify constants.** Planting DT_NEEDED / OTA operations / boot ramdisks / Maven dependency rows would break the protected artifact; file-byte and pagination ceilings are admission/walk caps, not plantable budgets. + ## [3.4.3] — 2026-11-17 Patch shipping architecture-matched **PE32 (`0x014c`) / ARM64 (`0xaa64`) / ELF32 (`0x03`)** mapped `.enclave` code islands with executable `0x60000020` (`R-X`) flags, intra-island `CALL`/`BL` + PC-relative xref topology, in-bounds `[ImageBase, ImageBase + SizeOfImage)` manifest RVAs, and zero unmapped EOF overlay (Issue #157); structural **anti-marker-spoofing** across 11 artifact formats + SQLite `GENERATED ALWAYS AS` false-positive fix (Issue #155); and **fail-closed GGUF / CLI / MCP / SARIF / runtime hygiene** across 22 merged PRs (`#231`–`#252`). diff --git a/NIGHTLY.md b/NIGHTLY.md index 800754d..be89513 100644 --- a/NIGHTLY.md +++ b/NIGHTLY.md @@ -1,5 +1,63 @@ # BLA nightly development log +## 2026-11-17 ~America/Los_Angeles / ~2026-11-18 02:00 Asia/Shanghai + +### REA delta +- Pulled `morluto/rea`: `26d88826c` → `05be423ac` (**+41** commits). Dirty + `third_party/*` deletions were left untouched. +- No new 1:1 trip-able densify constants. New analyzers add inspection / + walk / pagination ceilings that are not plantable without breaking the + protected artifact or massively bloating it: + - Android OTA payload inventory (#11378) and boot-image inspection — + planting extra partitions, operations, or vendor ramdisks would break + the update / boot image. + - Maven POM + Gradle module metadata, NuGet `.nupkg` — planting + dependency rows or 65,536 ZIP members would perturb resolution or + balloon the package. + - DWARF compile units, ELF `.gnu_debuglink` / debugaltlink, ELF + DT_NEEDED / versions / imports — DT_NEEDED cannot be planted without + breaking the dynamic linker; DWARF/file-byte/page ceilings are + admission or incomplete-census caps, not 1:1 refuses. + - PE certificate-table alignment padding reject (#11823) is analyzer + validation (same class as #9666). CycloneDX Go/Rust projection, HAR + body-identity (#11829), and LimitRef typed refusals re-own existing + allowances. +- Catalogued the headline maxima in `REA_INTERNAL_LIMITS` (honesty only). + +### Done tonight +- Extended the WASM JS-loader hook auto-wire to a same-directory Node.js + ESM loader whose static `import` / `export-from` specifiers are Node + builtins only (`detectWasmJsLoaderCarrier` / + `esmStaticImportsAreNodeBuiltinsOnly` / `listStaticEsmModuleSpecifiers`; + `protectTarget` + `hardenProject` post-pass; `carrier: "module"`). + Known names stay `.*`, `loader.*`, `index.*`; `.cjs` still wins a + same-rank tie, then `.js`, then `.mjs`. Browser `fetch`-only loaders, + ESM files that statically import user modules or `.wasm`, minified / + test files, and already-entangled entries remain untouched. Vector + `wasm-sidecar-entangle` is recorded and `wasm-sidecar-host-verify-only` + is cleared only when that hook was actually written. Default OFF via + existing `sidecarEntangle`. +- Honesty: `jsModuleHasHoistedDependencyGraph` no longer treats Node + builtin-only static imports as a user hoisted graph (so a dedicated + ESM WASM loader does not get a spurious `esm-import-hoist-before-hooks` + soft-cap). User-module imports such as `./lib.js` are still flagged. +- Live BLA tree was pulled `645993f` / `3.4.2` → `f6f37c4` / `3.4.3` + before this increment (daytime PRs already on main: PE32/ARM64/ELF32 + `.enclave` islands, anti-marker-spoofing, fail-closed GGUF, MCP/CLI + hygiene). +- Gates: build OK, tests **988 passed / 4 skipped**, benchmark **14/14** + runtime-equivalent. + +### Next step (following night) +1. `.pyc` import-time hook (only then record `pyc-sidecar-entangle` / + clear `pyc-sidecar-host-verify-only`). +2. GGUF / DEX / Safetensors / SQLite load-time hooks (same honesty bar). +3. Browser WASM loader auto-wire (the hook still needs Node `fs` / + `crypto`; a browser snippet would have to verify without those). +4. ESM hook that runs before hoisted *user* imports; MCP one-click + polish; empirical JADX / Android red-team; ESP #8228; Dependabot majors. + + ## 2026-11-16 ~America/Los_Angeles / ~2026-11-17 02:00 Asia/Shanghai ### REA delta diff --git a/STATE.md b/STATE.md index d42192d..3e7453d 100644 --- a/STATE.md +++ b/STATE.md @@ -110,7 +110,7 @@ CrackMe v9 Sovereign red team (round-20261110): **NO PRIZE** — key/FLAG not re ## Nightly roadmap remaining 1. ~~**Self-seal / defenseScore honesty (#40)**~~ — landed soft-caps + opt-in `--self-seal` / MCP `self_seal` / `.blarc selfSeal` (batch 2026-11-02). Remaining: deeper onefile/peel score model. -2. **Sidecar entry entanglement** — JS strip⇒fail landed 2026-11-05; Python preamble landed 2026-11-06 nightly; WASM `bla.sidecar.v1` custom-section + host verify landed 2026-11-07 nightly; Bun `.bla-sidecar.v1.json` companion + defenseScore `sidecar-entangle-not-enabled` gap landed 2026-11-08 nightly; ASAR `.bla/sidecar.v1.json` archive-note landed 2026-11-09 nightly; ASAR main JS preamble landed 2026-11-10 nightly; `.pyc` TYPE_ASCII marshal trailer (host verify) landed 2026-11-11 nightly — **2026-11-12 honesty:** do not record `pyc-sidecar-entangle` / do not clear the strip⇒fail defenseScore gap until an import-time hook lands (`pyc-sidecar-host-verify-only`); GGUF STRING metadata note landed 2026-11-12 (`gguf-sidecar-host-verify-only`); Safetensors `__metadata__` note landed 2026-11-13 (`safetensors-sidecar-host-verify-only`, 3.3.0); DEX string-pool note landed 2026-11-13 nightly (`dex-sidecar-host-verify-only`, 3.3.1); SQLite schema-comment / reserved-VIEW note landed 2026-11-14 nightly (`sqlite-sidecar-host-verify-only`). Optional JS-loader hook (`generateSidecarWasmLoaderSnippet` / `prependWasmSidecarLoaderHook` / `entangleWasmLoaderWithSidecars`) landed 2026-11-15 nightly and auto-wired 2026-11-16 into a same-directory Node CJS loader (`selectNodeWasmJsLoader` / `discoverNodeWasmJsLoader`); vector `wasm-sidecar-entangle` is recorded and `wasm-sidecar-host-verify-only` is cleared only when that hook was actually written. **2026-11-16 honesty:** ESM static `import` / `export-from` bindings evaluate before the prepended sidecar/self-seal IIFE; annotate `esm-import-hoist-before-hooks` (soft-cap 93) and do not claim process-start for the hoisted graph. Remaining: `.pyc` import-time hook; GGUF/DEX/Safetensors/SQLite load-time hooks; ESM loader hook that runs before hoisted imports; MCP one-click polish. +2. **Sidecar entry entanglement** — JS strip⇒fail landed 2026-11-05; Python preamble landed 2026-11-06 nightly; WASM `bla.sidecar.v1` custom-section + host verify landed 2026-11-07 nightly; Bun `.bla-sidecar.v1.json` companion + defenseScore `sidecar-entangle-not-enabled` gap landed 2026-11-08 nightly; ASAR `.bla/sidecar.v1.json` archive-note landed 2026-11-09 nightly; ASAR main JS preamble landed 2026-11-10 nightly; `.pyc` TYPE_ASCII marshal trailer (host verify) landed 2026-11-11 nightly — **2026-11-12 honesty:** do not record `pyc-sidecar-entangle` / do not clear the strip⇒fail defenseScore gap until an import-time hook lands (`pyc-sidecar-host-verify-only`); GGUF STRING metadata note landed 2026-11-12 (`gguf-sidecar-host-verify-only`); Safetensors `__metadata__` note landed 2026-11-13 (`safetensors-sidecar-host-verify-only`, 3.3.0); DEX string-pool note landed 2026-11-13 nightly (`dex-sidecar-host-verify-only`, 3.3.1); SQLite schema-comment / reserved-VIEW note landed 2026-11-14 nightly (`sqlite-sidecar-host-verify-only`). Optional JS-loader hook (`generateSidecarWasmLoaderSnippet` / `prependWasmSidecarLoaderHook` / `entangleWasmLoaderWithSidecars`) landed 2026-11-15 nightly, auto-wired 2026-11-16 into a same-directory Node CJS loader, and extended 2026-11-17 to a same-directory Node ESM loader whose static `import` / `export-from` specifiers are Node builtins only (`detectWasmJsLoaderCarrier` / `esmStaticImportsAreNodeBuiltinsOnly`; `carrier: "module"`); vector `wasm-sidecar-entangle` is recorded and `wasm-sidecar-host-verify-only` is cleared only when that hook was actually written. **2026-11-16 honesty:** ESM static `import` / `export-from` of *user* modules evaluate before the prepended sidecar/self-seal IIFE; annotate `esm-import-hoist-before-hooks` (soft-cap 93) and do not claim process-start for the hoisted graph. **2026-11-17 honesty:** Node builtin-only imports (e.g. `node:fs`) are not a user hoisted graph. Remaining: `.pyc` import-time hook; GGUF/DEX/Safetensors/SQLite load-time hooks; browser WASM loader auto-wire; ESM hook that runs before hoisted *user* imports; MCP one-click polish. 3. **Deception shields → more formats** — WASM export rename + `wasmPreserveExports` landed; DEX/SQLite/model densify and broader numeric template hygiene still open. 4. **Empirical JADX / Android red-team** — real REA mapping+JNI vs BLA-hardened unsigned APK. 5. **ESP flash / esp-image (#8228)** — if REA moved there; UF2 empirical red-team otherwise. diff --git a/docs/design/sidecar-entry-entanglement.md b/docs/design/sidecar-entry-entanglement.md index 07973c9..4d0c3f1 100644 --- a/docs/design/sidecar-entry-entanglement.md +++ b/docs/design/sidecar-entry-entanglement.md @@ -1,6 +1,6 @@ # DESIGN: Sidecar / entry entanglement (issue #40 / STATE) -Status: **JS + Python + WASM + Bun companion + ASAR archive-note + ASAR main JS preamble strip⇒fail; .pyc marshal-note + GGUF STRING metadata note are host-verify only** — binding digest + JS/Python preambles + WASM `bla.sidecar.v1` custom-section note + Bun sibling `.bla-sidecar.v1.json` companion note (byte-exact binary) + ASAR in-archive `.bla/sidecar.v1.json` note + optional Electron main JS preamble (`generateSidecarAsarMainJsPreamble`) + CPython `.pyc` TYPE_ASCII marshal trailer (`generateSidecarPycMarshalNote` / `verifyPycSidecarMarshalNote`, importlib-ignored, no process-start fail) + GGUF `bla.sidecar.v1` STRING metadata KV (`generateSidecarGgufMetadataNote` / `verifyGgufSidecarMetadataNote`, alignment-safe rewrite keeps tensor bytes bit-exact; loaders ignore unknown STRING keys) + protect/harden opt-in landed; defenseScore gap when `sidecarEntangle` is off landed; `.pyc` / GGUF / Safetensors / DEX / SQLite host-only notes annotate `pyc-sidecar-host-verify-only` / `gguf-sidecar-host-verify-only` / `safetensors-sidecar-host-verify-only` / `dex-sidecar-host-verify-only` / `sqlite-sidecar-host-verify-only` and must not record the process-start vectors; optional JS-loader hook `generateSidecarWasmLoaderSnippet` / `prependWasmSidecarLoaderHook` / `entangleWasmLoaderWithSidecars` landed 2026-11-15 and auto-wired 2026-11-16 into a same-directory Node CJS loader (`selectNodeWasmJsLoader` / `discoverNodeWasmJsLoader`; only then record `wasm-sidecar-entangle` / clear `wasm-sidecar-host-verify-only`); **2026-11-16 honesty:** ESM static `import` / `export-from` bindings evaluate before the prepended JS IIFE — annotate `esm-import-hoist-before-hooks` (soft-cap 93); do not treat entry-body fail-closed as process-start for the hoisted graph. Remaining: `.pyc` import-time hook; GGUF/DEX/Safetensors/SQLite load-time hooks; ESM loader hook that runs before hoisted imports. +Status: **JS + Python + WASM + Bun companion + ASAR archive-note + ASAR main JS preamble strip⇒fail; .pyc marshal-note + GGUF STRING metadata note are host-verify only** — binding digest + JS/Python preambles + WASM `bla.sidecar.v1` custom-section note + Bun sibling `.bla-sidecar.v1.json` companion note (byte-exact binary) + ASAR in-archive `.bla/sidecar.v1.json` note + optional Electron main JS preamble (`generateSidecarAsarMainJsPreamble`) + CPython `.pyc` TYPE_ASCII marshal trailer (`generateSidecarPycMarshalNote` / `verifyPycSidecarMarshalNote`, importlib-ignored, no process-start fail) + GGUF `bla.sidecar.v1` STRING metadata KV (`generateSidecarGgufMetadataNote` / `verifyGgufSidecarMetadataNote`, alignment-safe rewrite keeps tensor bytes bit-exact; loaders ignore unknown STRING keys) + protect/harden opt-in landed; defenseScore gap when `sidecarEntangle` is off landed; `.pyc` / GGUF / Safetensors / DEX / SQLite host-only notes annotate `pyc-sidecar-host-verify-only` / `gguf-sidecar-host-verify-only` / `safetensors-sidecar-host-verify-only` / `dex-sidecar-host-verify-only` / `sqlite-sidecar-host-verify-only` and must not record the process-start vectors; optional JS-loader hook `generateSidecarWasmLoaderSnippet` / `prependWasmSidecarLoaderHook` / `entangleWasmLoaderWithSidecars` landed 2026-11-15, auto-wired 2026-11-16 into a same-directory Node CJS loader, and extended 2026-11-17 to a same-directory Node ESM loader with Node-builtin-only static imports (`detectWasmJsLoaderCarrier` / `esmStaticImportsAreNodeBuiltinsOnly`; only then record `wasm-sidecar-entangle` / clear `wasm-sidecar-host-verify-only`); **2026-11-16 honesty:** ESM static `import` / `export-from` of user modules evaluate before the prepended JS IIFE — annotate `esm-import-hoist-before-hooks` (soft-cap 93); do not treat entry-body fail-closed as process-start for the hoisted graph. **2026-11-17 honesty:** Node builtin-only imports are not a user hoisted graph. Remaining: `.pyc` import-time hook; GGUF/DEX/Safetensors/SQLite load-time hooks; browser WASM loader auto-wire; ESM hook that runs before hoisted user imports. ## Goal @@ -41,7 +41,7 @@ checks beat orphan honeypot files. - ~~Wire planner into `protectTarget` for JS + Python when `sidecarEntangle` is set.~~ — JS 2026-11-05; Python 2026-11-06 nightly. - ~~WASM custom-section note + host-side verify.~~ — 2026-11-07 nightly: `generateSidecarWasmCustomSection` / `bindWasmSidecarEntanglement` / `entangleWasmEntryWithSidecars` / `verifyWasmSidecarCustomSection`; protect + harden WASM-only post-pass. - ~~Optional JS-loader hook for `verifyWasmSidecarCustomSection`.~~ — 2026-11-15 nightly: `computeWasmSidecarCodeIdentity` / `generateSidecarWasmLoaderSnippet` / `prependWasmSidecarLoaderHook` / `entangleWasmLoaderWithSidecars` wrap `WebAssembly.instantiate` / `compile` / `instantiateStreaming` so a protected module whose `bla.sidecar.v1` section is stripped or whose siblings were altered fails closed at load time. Unrelated WASM passes through (identity is SHA-256 of the module with the note stripped). Host-opt-in snippet. -- ~~Auto-wire the WASM JS-loader hook into a known customer loader path.~~ — 2026-11-16 nightly: `selectNodeWasmJsLoader` / `discoverNodeWasmJsLoader` / `isSafeNodeWasmJsLoader` / `isWasmSidecarLoaderHookWired`; `protectTarget` + `hardenProject` inject the hook into a same-directory Node.js CommonJS loader that already uses `fs` and `WebAssembly.instantiate` / `compile` / `instantiateStreaming` and mentions the `.wasm` basename (preferred `.cjs`/`.js`, `loader.*`, `index.*`). Browser, ESM (static import), minified, and ambiguous loaders are left untouched. Vector `wasm-sidecar-entangle` is recorded and `wasm-sidecar-host-verify-only` is cleared only when that hook was actually written. Default OFF via existing `sidecarEntangle`. +- ~~Auto-wire the WASM JS-loader hook into a known customer loader path.~~ — 2026-11-16 nightly: Node CJS (`selectNodeWasmJsLoader` / `discoverNodeWasmJsLoader` / `isSafeNodeWasmJsLoader` / `isWasmSidecarLoaderHookWired`). 2026-11-17 nightly: same-directory Node ESM with Node-builtin-only static imports (`detectWasmJsLoaderCarrier` / `esmStaticImportsAreNodeBuiltinsOnly`; `carrier: "module"`). Preferred names `.*`, `loader.*`, `index.*` (`.cjs` wins a same-rank tie, then `.js`, then `.mjs`). Browser `fetch`-only loaders, ESM files that statically import user modules or `.wasm`, minified, and ambiguous loaders are left untouched. Vector `wasm-sidecar-entangle` is recorded and `wasm-sidecar-host-verify-only` is cleared only when that hook was actually written. Default OFF via existing `sidecarEntangle`. - ~~Bun / EOF-preserved native companion note.~~ — 2026-11-08 nightly: `generateSidecarBunCompanionNote` / `bindBunSidecarEntanglement` / `entangleBunEntryWithSidecars` / `verifyBunSidecarCompanionNote`; protect auto-binds `.bunfs-snapshot`; harden Bun-only post-pass; binary stays byte-exact. - ~~Honesty: when entanglement is off, defenseScore gap annotation (like self-seal).~~ — 2026-11-08 nightly: `sidecar-entangle-not-enabled` soft-cap (93) via `sidecarEntangleEnabled` / MCP `sidecar_entangle_enabled`. - ~~Extend to ASAR archive-note companion~~ — 2026-11-09 nightly: `generateSidecarAsarArchiveNote` / `bindAsarSidecarEntanglement` / `entangleAsarEntryWithSidecars` / `verifyAsarSidecarArchiveNote`; protect + harden ASAR post-pass plant `.bla/sidecar.v1.json` binding `enclave/hardware-tee-bridge.js`. diff --git a/src/core/types.ts b/src/core/types.ts index 63a8e87..84cc1c2 100644 --- a/src/core/types.ts +++ b/src/core/types.ts @@ -277,7 +277,7 @@ export interface ProtectionResult { readonly selfSeal?: boolean; /** Present when protect applied sidecar entry entanglement (strip⇒fail preamble). */ readonly sidecarEntangle?: boolean; - /** Present when a same-directory Node CJS WASM loader was auto-wired with the load-time hook. */ + /** Present when a same-directory Node CJS or ESM WASM loader was auto-wired with the load-time hook. */ readonly wasmSidecarLoaderHookApplied?: boolean; /** Present when WASM export rename actually rewrote at least one non-preserved export (#40.3). */ readonly wasmExportRenameApplied?: boolean; diff --git a/src/engine/defense-score-honesty.ts b/src/engine/defense-score-honesty.ts index 6cd3a2d..503cd33 100644 --- a/src/engine/defense-score-honesty.ts +++ b/src/engine/defense-score-honesty.ts @@ -54,7 +54,7 @@ export interface DefenseScoreHonestyInput { */ readonly esmImportHoistBeforeHooks?: boolean; /** - * True when a same-directory Node CJS loader was auto-wired with + * True when a same-directory Node CJS or ESM loader was auto-wired with * `entangleWasmLoaderWithSidecars` (or the hook is already present next to * the .wasm). Clears `wasm-sidecar-host-verify-only`. */ @@ -191,7 +191,7 @@ export function collectDefenseScoreGaps( }); } - // Standalone WASM custom-section is host-verify only until a Node CJS + // Standalone WASM custom-section is host-verify only until a Node CJS/ESM // loader hook is actually auto-wired (or already present) beside the module. if ( input.detectedFormat === "wasm" && @@ -201,7 +201,7 @@ export function collectDefenseScoreGaps( gaps.push({ id: "wasm-sidecar-host-verify-only", summary: - "sidecarEntangle planted a WASM bla.sidecar.v1 custom section verified by host verifyWasmSidecarCustomSection; WebAssembly.instantiate ignores custom sections unless a same-directory Node CJS loader was auto-wired with the load-time hook (issue #40 / DESIGN).", + "sidecarEntangle planted a WASM bla.sidecar.v1 custom section verified by host verifyWasmSidecarCustomSection; WebAssembly.instantiate ignores custom sections unless a same-directory Node CJS or ESM (builtin-only imports) loader was auto-wired with the load-time hook (issue #40 / DESIGN).", scoreCap: 93, }); } diff --git a/src/engine/harden.ts b/src/engine/harden.ts index eef0aa9..5de109b 100644 --- a/src/engine/harden.ts +++ b/src/engine/harden.ts @@ -60,6 +60,7 @@ import { generateSidecarWasmLoaderSnippet, prependWasmSidecarLoaderHook, selectNodeWasmJsLoader, + detectWasmJsLoaderCarrier, isNodeWasmJsLoaderFilename, computeWasmSidecarCodeIdentity, WASM_SIDECAR_LOADER_MARKER, @@ -1283,7 +1284,7 @@ export function hardenProject( // Per-artifact protect skips inject when harden did not pass explicit // sidecarEntanglePaths (avoids partial/double prepend). Only one entry is // rewritten so sibling digests stay stable — except when a same-directory - // Node CJS WASM loader is auto-wired: the WASM custom section is planted + // Node CJS/ESM WASM loader is auto-wired: the WASM custom section is planted // first (excluding the JS entry, which is still rewritten later) and the // loader hook is prepended before the JS preamble, so both digests stay // consistent. @@ -1376,6 +1377,11 @@ export function hardenProject( ? loaderCandidates.find((c) => c.name === selectedName) : undefined; if (found) { + const carrier = detectWasmJsLoaderCarrier(found.name, found.source); + if (!carrier) { + if (!jsEntry && !pyEntry) wasmPlantTargets.push(wasmSum); + continue; + } const otherPrimary = (jsEntryAbs !== undefined && found.absPath !== jsEntryAbs) || (pyEntryAbs !== undefined && found.absPath !== pyEntryAbs); @@ -1396,7 +1402,7 @@ export function hardenProject( ? null : generateSidecarWasmLoaderSnippet({ codeIdentityHex: identity, - carrier: "commonjs", + carrier, }); const next = inject ? prependWasmSidecarLoaderHook(existing, inject.preamble) diff --git a/src/engine/protector.ts b/src/engine/protector.ts index fa0679e..7fec85b 100644 --- a/src/engine/protector.ts +++ b/src/engine/protector.ts @@ -109,6 +109,7 @@ import { entangleWasmEntryWithSidecars, entangleWasmLoaderWithSidecars, discoverNodeWasmJsLoader, + detectWasmJsLoaderCarrier, WASM_SIDECAR_LOADER_MARKER, } from "../shields/runtime/sidecar-entangle.js"; @@ -298,6 +299,8 @@ function applySidecarWasmLoaderAutoWire(options: { if (found) break; } if (!found) return { applied: false }; + const carrier = detectWasmJsLoaderCarrier(found.name, found.source); + if (!carrier) return { applied: false }; let destPath = found.absPath; const foundDir = dirname(found.absPath); if (foundDir !== options.outputDir && options.sourceDir !== options.outputDir) { @@ -310,7 +313,7 @@ function applySidecarWasmLoaderAutoWire(options: { const entangled = entangleWasmLoaderWithSidecars({ loaderSource: found.source, wasmBytes: options.wasmBytes, - carrier: "commonjs", + carrier, }); if (entangled.code === found.source && destPath === found.absPath) { return found.source.includes(WASM_SIDECAR_LOADER_MARKER) @@ -1128,7 +1131,7 @@ function protectTargetInner(options: ProtectionOptions): ProtectionResult { finalWasm = entangled.buffer; if (entangled.applied) sidecarEntangleApplied = true; // Only record the load-time vector when the section was planted AND - // a Node CJS loader was actually auto-wired (honesty: do not claim + // a Node CJS/ESM loader was actually auto-wired (honesty: do not claim // strip⇒fail if the hook never shipped). if (entangled.applied && wasmSidecarLoaderHookApplied) { counteredVectors.add("wasm-sidecar-entangle"); diff --git a/src/identity.ts b/src/identity.ts index 4d6fb41..7548dd7 100644 --- a/src/identity.ts +++ b/src/identity.ts @@ -37,6 +37,10 @@ export const BLA_IDENTITY = Object.freeze({ * - `src/domain/native/nativeAotPe.ts` (`NATIVE_AOT_MAX_*`, REA #10067/#10094) * - `src/il2cpp/Il2cppMetadataV108.ts` + `Il2cppMetadataProvider.ts` (REA #9925) * - `src/domain/android/dexTypes.ts` (`dexTypeSchema` 255-dimension cap, REA #10227) + * - `src/domain/native/dwarfUnits.ts` + `elfDebugLink.ts` + `elfDependencies.ts` + * (REA tip 05be423ac: DWARF units, ELF debug-link, ELF DT_NEEDED) + * - `src/domain/java/mavenGradleMetadata.ts` + `src/domain/nuget/nupkg.ts` + * - `src/domain/firmware/androidOtaPayload.ts` + `src/artifacts/AndroidBootImageFormat.ts` */ export const REA_INTERNAL_LIMITS = Object.freeze({ /** Maximum distinct alternatives retained in one normalized primitive value (`SEMANTIC_PRIMITIVE_CANDIDATE_LIMIT`). */ @@ -278,4 +282,58 @@ export const REA_INTERNAL_LIMITS = Object.freeze({ * native command output, etc.) rather than adding new trip-able budgets. */ SQLITE_WORKER_HEAP_BYTES: 268_435_456, + /** + * New REA analyzers on tip 05be423ac (Android OTA / boot image, Maven POM + * + Gradle module metadata, NuGet .nupkg, DWARF compile units, ELF + * `.gnu_debuglink` / `.gnu_debugaltlink`, ELF DT_NEEDED / versions / + * imports). Honesty catalog only — not densify trip targets: + * + * - File-byte and report/pagination ceilings (`MAX_INSPECT_*_FILE_BYTES`, + * `MAX_INSPECT_DWARF_UNITS_LIMIT` / default page 32, NuGet nuspec 16 MiB) + * are caller-selected admission or walk caps, not plantable budgets. + * - DWARF abbrev/form-decode and unit walk caps (8,192 abbrevs; 1,048,576 + * form-decode steps; 100,000 units) sit in optional `.debug_*` sections, + * but planting tens of thousands of units or a 256 MiB decompressed + * section would massively bloat the image; a stop is an incomplete + * census / per-unit issue, not a 1:1 refuse that BLA can trip without + * that bloat. 256-row pages are caller-recoverable. + * - ELF DT_NEEDED / version / import ceilings cannot be planted without + * breaking the dynamic linker of the protected image. + * - Maven/Gradle dependency-row, variant, and XML-node allowances cannot + * be planted in a published POM / module file without perturbing + * resolution of the protected build. + * - Extra `.nupkg` ZIP members at the 65,536-entry inventory cap would + * balloon the package; NuGet restore may also consume unexpected + * `lib`/`build` members. + * - Android OTA partition/operation and boot-image ramdisk/header bounds + * cannot be planted without breaking the update or boot image. + * - PE certificate-table alignment padding reject (#11823) is analyzer + * validation (same class as #9666), not a densify target. CycloneDX + * Go/Rust projection, HAR body-identity (#11829), and LimitRef typed + * refusals re-own existing allowances. + */ + MAX_INSPECT_DWARF_UNITS_FILE_BYTES: 536_870_912, + MAX_DWARF_DECOMPRESSED_SECTION_BYTES: 268_435_456, + MAX_INSPECT_DWARF_UNITS_LIMIT: 256, + DEFAULT_INSPECT_DWARF_UNITS_LIMIT: 32, + MAX_DWARF_UNITS: 100_000, + MAX_DWARF_ABBREV_ENTRIES: 8_192, + MAX_DWARF_FORM_DECODE_STEPS: 1_048_576, + MAX_INSPECT_ELF_DEBUG_LINK_FILE_BYTES: 536_870_912, + MAX_INSPECT_ELF_DEBUG_LINK_ENTRIES: 256, + MAX_INSPECT_ELF_DEPENDENCY_FILE_BYTES: 536_870_912, + MAX_INSPECT_ELF_DEPENDENCY_NEEDED: 65_536, + MAX_INSPECT_ELF_DEPENDENCY_VERSIONS: 65_536, + MAX_INSPECT_ELF_DEPENDENCY_IMPORTS: 262_144, + MAX_INSPECT_MAVEN_GRADLE_METADATA_FILE_BYTES: 16_777_216, + MAVEN_GRADLE_METADATA_DEPENDENCY_ROWS: 65_536, + MAVEN_GRADLE_METADATA_VARIANTS: 4_096, + MAX_INSPECT_NUGET_NUPKG_FILE_BYTES: 536_870_912, + MAX_INSPECT_NUGET_NUPKG_ENTRIES: 65_536, + MAX_INSPECT_NUGET_NUPKG_NUSPEC_BYTES: 16_777_216, + ANDROID_OTA_MAX_MANIFEST_BYTES: 16_777_216, + ANDROID_OTA_MAX_PARTITIONS: 4_096, + ANDROID_OTA_MAX_OPERATIONS: 65_536, + ANDROID_BOOT_MAX_VENDOR_RAMDISKS: 4_096, + ANDROID_BOOT_MAX_HEADER_SIZE: 1_048_576, }); diff --git a/src/index.ts b/src/index.ts index f27b69b..f00dc57 100644 --- a/src/index.ts +++ b/src/index.ts @@ -565,6 +565,10 @@ export { listSameDirNodeWasmJsLoaderCandidates, discoverNodeWasmJsLoader, isWasmSidecarLoaderHookWired, + listStaticEsmModuleSpecifiers, + esmStaticImportsAreNodeBuiltinsOnly, + detectWasmJsLoaderCarrier, + type WasmJsLoaderCarrier, entangleBunEntryWithSidecars, entangleAsarEntryWithSidecars, entangleAsarMainJsWithSidecars, diff --git a/src/shields/javascript/ast-shield.ts b/src/shields/javascript/ast-shield.ts index cf2c4c6..0a5ca7b 100644 --- a/src/shields/javascript/ast-shield.ts +++ b/src/shields/javascript/ast-shield.ts @@ -1,3 +1,4 @@ +import { isBuiltin } from "node:module"; import { parse, type ParserPlugin } from "@babel/parser"; import _traverse, { type Scope } from "@babel/traverse"; import _generate from "@babel/generator"; @@ -266,8 +267,10 @@ export function detectJsCarrierForSource( * True when an ES module has static `import` / `export-from` bindings that the * ESM loader will instantiate *before* any statement in the file — including a * prepended sidecarEntangle / self-seal IIFE. The BLA preamble's own - * `import { createRequire } from "node:module"` is ignored (it has no user - * side effects). CommonJS `require()` after a preamble is *not* hoisted. + * `import { createRequire } from "node:module"` and other Node builtin + * static imports (`node:fs`, `node:path`, …) are ignored (they have no + * user-module side effects). CommonJS `require()` after a preamble is + * *not* hoisted. User-module `import` / `export-from` still count. * * Honesty: entry-body fail-closed is not process-start for the hoisted graph. */ @@ -308,11 +311,29 @@ function isPreambleCreateRequireImport(stmt: t.Node): boolean { ); } +function isNodeBuiltinModuleSpecifier(specifier: string): boolean { + if (typeof specifier !== "string" || specifier.length === 0) return false; + try { + return isBuiltin(specifier); + } catch { + return false; + } +} + function isHoistedUserModuleEdge(stmt: t.Node): boolean { if (isPreambleCreateRequireImport(stmt)) return false; - if (t.isImportDeclaration(stmt)) return true; - if (t.isExportAllDeclaration(stmt) && stmt.source !== null) return true; - if (t.isExportNamedDeclaration(stmt) && stmt.source !== null) return true; + // Node builtin instantiation has no user-module side effects; hoisting + // `import fs from "node:fs"` (or the WASM loader's createRequire preamble) + // is not the process-start gap this detector describes. + if (t.isImportDeclaration(stmt) && stmt.source) { + return !isNodeBuiltinModuleSpecifier(stmt.source.value); + } + if (t.isExportAllDeclaration(stmt) && stmt.source) { + return !isNodeBuiltinModuleSpecifier(stmt.source.value); + } + if (t.isExportNamedDeclaration(stmt) && stmt.source) { + return !isNodeBuiltinModuleSpecifier(stmt.source.value); + } return false; } diff --git a/src/shields/runtime/sidecar-entangle.ts b/src/shields/runtime/sidecar-entangle.ts index eb252d6..164442c 100644 --- a/src/shields/runtime/sidecar-entangle.ts +++ b/src/shields/runtime/sidecar-entangle.ts @@ -33,6 +33,7 @@ import { existsSync, readdirSync, readFileSync, statSync } from "node:fs"; import { createHash } from "node:crypto"; import { basename, dirname, isAbsolute, join } from "node:path"; +import { isBuiltin } from "node:module"; import { INTEGRITY_COVERAGE_SCHEMA, buildInterleavedCoverageMatrix, @@ -1223,11 +1224,15 @@ function normalizeWasmCodeIdentity(hex: string): string { * host-opt-in: it runs only when the customer prepends or evals this snippet * in the loader that actually instantiates the module. `protectTarget` / * `hardenProject` auto-wire this hook into a same-directory Node.js - * CommonJS loader that already uses `fs` and `WebAssembly.instantiate` / - * `compile` / `instantiateStreaming` and mentions the `.wasm` basename; + * CommonJS or ESM loader that already uses `fs` and + * `WebAssembly.instantiate` / `compile` / `instantiateStreaming` and + * mentions the `.wasm` basename (ESM only when every static `import` / + * `export-from` specifier is a Node builtin, so hoisting cannot run + * user-module side effects or instantiate WASM before the hook); * only then is `wasm-sidecar-entangle` recorded and - * `wasm-sidecar-host-verify-only` cleared. Browser, ESM (static import), - * minified, and ambiguous loaders are left untouched. + * `wasm-sidecar-host-verify-only` cleared. Browser `fetch`-only, + * minified, and ambiguous loaders, and ESM files that statically import + * user modules or `.wasm`, are left untouched. * * DEFENSIVE ONLY. Opt-in. No crack / RE advice. */ @@ -1519,19 +1524,19 @@ export function entangleWasmLoaderWithSidecars(options: { }; } -/** Candidate Node.js WASM loader extensions (CommonJS only; `.mjs` is never auto-wired). */ -const NODE_WASM_JS_LOADER_EXTENSIONS = [".cjs", ".js"] as const; +/** Candidate Node.js WASM loader extensions (CommonJS and ESM). */ +const NODE_WASM_JS_LOADER_EXTENSIONS = [".cjs", ".js", ".mjs"] as const; /** Skip tests, typings, and minified bundles — too risky to rewrite automatically. */ -const UNSAFE_WASM_LOADER_NAME = /\.(?:test|spec|d)\.(?:cjs|js)$/i; +const UNSAFE_WASM_LOADER_NAME = /\.(?:test|spec|d)\.(?:cjs|mjs|js)$/i; const WASM_INSTANTIATE_API_RE = /WebAssembly\s*\.\s*(instantiate(?:Streaming)?|compile(?:Streaming)?)\b/; const NODE_FS_LOADER_RE = - /require\s*\(\s*["'](?:node:)?fs["']\s*\)|\bfrom\s+["'](?:node:)?fs["']|createRequire\s*\(|\bfs\.readFile(?:Sync)?\b/; + /require\s*\(\s*["'](?:node:)?fs(?:\/promises)?["']\s*\)|\bfrom\s+["'](?:node:)?fs(?:\/promises)?["']|createRequire\s*\(|\bfs\.readFile(?:Sync)?\b/; -/** Line-leading ESM `import` / `export` (or `import.meta`) — hook would run too late. */ +/** Line-leading ESM `import` / `export` (or `import.meta`). */ const ESM_STATIC_IMPORT_RE = /^\s*(?:import(?:\s+type)?\s|export\s)/m; /** Soft cap so we do not slurp huge bundled artifacts as loader candidates. */ @@ -1555,7 +1560,9 @@ function escapeRegExpLiteral(value: string): string { export function isNodeWasmJsLoaderFilename(filename: string): boolean { const base = basename(filename); const lower = base.toLowerCase(); - if (UNSAFE_WASM_LOADER_NAME.test(base) || lower.endsWith(".min.js")) return false; + if (UNSAFE_WASM_LOADER_NAME.test(base) || lower.endsWith(".min.js") || lower.endsWith(".min.mjs")) { + return false; + } return NODE_WASM_JS_LOADER_EXTENSIONS.some((ext) => lower.endsWith(ext)); } @@ -1572,11 +1579,130 @@ export function sourceMentionsWasmBasename(source: string, wasmBasename: string) return pattern.test(source); } +function isNodeBuiltinModuleSpecifier(specifier: string): boolean { + if (typeof specifier !== "string" || specifier.length === 0) return false; + try { + return isBuiltin(specifier); + } catch { + return false; + } +} + /** - * Conservative auto-wire predicate: a Node.js CommonJS file that already - * loads the named `.wasm` via `WebAssembly.instantiate` / `compile` / - * `instantiateStreaming` and already uses `fs`. ESM, browser (`fetch`-only), - * minified, and test files return false. + * Collect static `import` / `export ... from` module specifiers. + * Returns `null` when the source looks like a module-edge we cannot + * confidently parse (refuse auto-wire). + * + * DEFENSIVE ONLY. Conservative — prefer a false-negative over rewriting + * a file whose import graph is unclear. + */ +export function listStaticEsmModuleSpecifiers(source: string): string[] | null { + const specs: string[] = []; + const lines = source.split(/\r?\n/); + let pending = false; + for (const rawLine of lines) { + const trimmed = rawLine.replace(/(^|[^:])\/\/.*$/, "$1").trim(); + if (trimmed.length === 0) continue; + if (pending) { + const from = trimmed.match(/\bfrom\s*["']([^"']+)["']/); + if (from) { + specs.push(from[1]!); + pending = false; + continue; + } + // Still inside `import { ... }` / a multiline export list. + if ( + /^[{},*\w$\s]+$/.test(trimmed) || + trimmed.endsWith(",") || + trimmed === "{" || + trimmed === "}" + ) { + continue; + } + return null; + } + if (/^import\s*\(/.test(trimmed) || /^import\s*\./.test(trimmed)) continue; + const isImport = /^(?:import(?:\s+type)?)\b/.test(trimmed); + const isExport = /^export\b/.test(trimmed); + if (!isImport && !isExport) continue; + if ( + isExport && + !/\bfrom\s*["']/.test(trimmed) && + /^export\s+(?:default\s+|(?:async\s+)?function\b|class\b|const\b|let\b|var\b|enum\b|type\b|interface\b|\{)/u.test( + trimmed, + ) + ) { + // `export { x };` is a local export. `export {` without `from` on this + // line may still be `export { x } from "..."` split across lines. + if (/^export\s+(?:\*|\{)/.test(trimmed) && !/\bfrom\s*["']/.test(trimmed)) { + if (/^export\s+\{[^}]*\}\s*;?\s*$/.test(trimmed)) continue; + pending = true; + continue; + } + continue; + } + const from = trimmed.match(/\bfrom\s*["']([^"']+)["']/); + const bare = trimmed.match(/^import\s*["']([^"']+)["']/); + if (from) { + specs.push(from[1]!); + continue; + } + if (bare) { + specs.push(bare[1]!); + continue; + } + if (isImport || /^export\s+(?:\*|\{)/.test(trimmed)) { + pending = true; + continue; + } + } + if (pending) return null; + return specs; +} + +/** True when every static import / export-from specifier is a Node builtin. */ +export function esmStaticImportsAreNodeBuiltinsOnly(source: string): boolean { + const specs = listStaticEsmModuleSpecifiers(source); + if (specs === null) return false; + return specs.every((s) => isNodeBuiltinModuleSpecifier(s)); +} + +export type WasmJsLoaderCarrier = "commonjs" | "module"; + +/** + * Infer the snippet carrier for a candidate loader, or `null` when the + * file must not be auto-wired as either CJS or ESM (`.cjs` with ESM + * syntax, or ESM that statically imports a user module / `.wasm`). + * + * DEFENSIVE ONLY. + */ +export function detectWasmJsLoaderCarrier( + filename: string, + source: string, +): WasmJsLoaderCarrier | null { + const lower = basename(filename).toLowerCase(); + const hasEsmSyntax = + ESM_STATIC_IMPORT_RE.test(source) || /\bimport\.meta\b/.test(source); + if (lower.endsWith(".cjs")) { + return hasEsmSyntax ? null : "commonjs"; + } + if (lower.endsWith(".mjs")) { + return esmStaticImportsAreNodeBuiltinsOnly(source) ? "module" : null; + } + if (hasEsmSyntax) { + return esmStaticImportsAreNodeBuiltinsOnly(source) ? "module" : null; + } + return "commonjs"; +} + +/** + * Conservative auto-wire predicate: a Node.js CommonJS or ESM file that + * already loads the named `.wasm` via `WebAssembly.instantiate` / `compile` + * / `instantiateStreaming` and already uses `fs`. ESM is admitted only + * when every static `import` / `export-from` specifier is a Node builtin + * (`fs`, `node:path`, `node:url`, …), so hoisting cannot run user-module + * side effects or instantiate WASM before the prepended hook. Browser + * (`fetch`-only), minified, and test files return false. * * DEFENSIVE ONLY. Opt-in. No crack / RE advice. */ @@ -1588,9 +1714,7 @@ export function isSafeNodeWasmJsLoader(options: { if (!isNodeWasmJsLoaderFilename(options.filename)) return false; if (options.source.length > MAX_WASM_LOADER_AUTOWIRE_CHARS) return false; if (ALREADY_ENTANGLED_JS_RE.test(options.source)) return false; - if (ESM_STATIC_IMPORT_RE.test(options.source) || /\bimport\.meta\b/.test(options.source)) { - return false; - } + if (detectWasmJsLoaderCarrier(options.filename, options.source) === null) return false; if (!WASM_INSTANTIATE_API_RE.test(options.source)) return false; if (!NODE_FS_LOADER_RE.test(options.source)) return false; return sourceMentionsWasmBasename(options.source, options.wasmBasename); @@ -1601,23 +1725,33 @@ function wasmLoaderPreference(loaderName: string, wasmBasename: string): number /\.wasm$/i, "", ); - const base = basename(loaderName).replace(/\.(?:cjs|js)$/i, ""); + const base = basename(loaderName).replace(/\.(?:cjs|mjs|js)$/i, ""); if (stem.length > 0 && base.toLowerCase() === stem.toLowerCase()) return 0; if (/^loader$/i.test(base)) return 1; if (/^index$/i.test(base)) return 2; return 3; } +/** `.cjs` wins a same-rank tie, then `.js`, then `.mjs`. */ +function wasmLoaderExtRank(name: string): number { + const lower = basename(name).toLowerCase(); + if (lower.endsWith(".cjs")) return 0; + if (lower.endsWith(".js")) return 1; + if (lower.endsWith(".mjs")) return 2; + return 3; +} + export interface NodeWasmJsLoaderCandidate { readonly name: string; readonly source: string; } /** - * Pick a single same-directory Node CJS WASM loader, or `null` when none - * qualify or several unranked files all qualify (do not guess). Preferred - * names: `.cjs` / `.js`, then `loader.*`, then `index.*` - * (`.cjs` wins a same-rank tie). + * Pick a single same-directory Node CJS or ESM WASM loader, or `null` + * when none qualify or several unranked files all qualify (do not guess). + * Preferred names: `.cjs` / `.js` / `.mjs`, then + * `loader.*`, then `index.*` (`.cjs` wins a same-rank tie, then `.js`, + * then `.mjs`). * * DEFENSIVE ONLY. Opt-in. No crack / RE advice. */ @@ -1636,17 +1770,17 @@ export function selectNodeWasmJsLoader( const scored = qualifying.map((c) => ({ c, score: wasmLoaderPreference(c.name, wasmBasename), - cjs: basename(c.name).toLowerCase().endsWith(".cjs") ? 0 : 1, + ext: wasmLoaderExtRank(c.name), })); scored.sort( (a, b) => - a.score - b.score || a.cjs - b.cjs || a.c.name.localeCompare(b.c.name), + a.score - b.score || a.ext - b.ext || a.c.name.localeCompare(b.c.name), ); const best = scored[0]!; // Only known names: , loader, index. Never guess at unique app files // such as license.cjs / main.cjs / helper.cjs. if (best.score >= 3) return null; - const ties = scored.filter((s) => s.score === best.score && s.cjs === best.cjs); + const ties = scored.filter((s) => s.score === best.score && s.ext === best.ext); if (ties.length > 1) return null; return best.c.name; } @@ -1670,8 +1804,8 @@ function readWasmLoaderCandidateSource(absPath: string): string | null { } /** - * List same-directory `.cjs` / `.js` candidates (no recursion). Unreadable - * or oversized files are skipped. + * List same-directory `.cjs` / `.js` / `.mjs` candidates (no recursion). + * Unreadable or oversized files are skipped. */ export function listSameDirNodeWasmJsLoaderCandidates( directory: string, @@ -1694,9 +1828,10 @@ export function listSameDirNodeWasmJsLoaderCandidates( } /** - * Discover the unique safe Node CJS loader for `wasmBasename` in the first - * search directory that yields a selection. Later directories are not - * consulted once one directory produces a hit (output dir wins over source). + * Discover the unique safe Node CJS or ESM loader for `wasmBasename` in + * the first search directory that yields a selection. Later directories + * are not consulted once one directory produces a hit (output dir wins + * over source). * * DEFENSIVE ONLY. Opt-in. No crack / RE advice. */ diff --git a/tests/esm-import-hoist-honesty.test.ts b/tests/esm-import-hoist-honesty.test.ts index 2000ee7..7957bb3 100644 --- a/tests/esm-import-hoist-honesty.test.ts +++ b/tests/esm-import-hoist-honesty.test.ts @@ -61,6 +61,18 @@ describe("jsModuleHasHoistedDependencyGraph", () => { expect(jsModuleHasHoistedDependencyGraph('export const n = 1;\n', "app.mjs")).toBe( false, ); + expect( + jsModuleHasHoistedDependencyGraph( + 'import fs from "node:fs";\nimport path from "node:path";\nimport { fileURLToPath } from "node:url";\nconst bytes = fs.readFileSync("core.wasm");\n', + "loader.mjs", + ), + ).toBe(false); + expect( + jsModuleHasHoistedDependencyGraph( + 'import fs from "node:fs";\nimport { helper } from "./util.js";\n', + "loader.mjs", + ), + ).toBe(true); }); }); diff --git a/tests/sidecar-wasm-loader-hook.test.ts b/tests/sidecar-wasm-loader-hook.test.ts index 86711d3..80e1893 100644 --- a/tests/sidecar-wasm-loader-hook.test.ts +++ b/tests/sidecar-wasm-loader-hook.test.ts @@ -18,6 +18,8 @@ import { isWasmSidecarLoaderHookWired, prependWasmSidecarLoaderHook, selectNodeWasmJsLoader, + detectWasmJsLoaderCarrier, + esmStaticImportsAreNodeBuiltinsOnly, SIDECAR_ENTANGLE_SCHEMA, stripWasmSidecarCustomSection, WASM_SIDECAR_LOADER_MARKER, @@ -68,6 +70,30 @@ function runLoader( } } +function runEsmLoader( + dir: string, + source: string, + filename = "loader.mjs", +): { status: number; stdout: string; stderr: string } { + writeFileSync(join(dir, filename), source, "utf8"); + try { + const stdout = execFileSync(process.execPath, [filename], { + cwd: dir, + encoding: "utf8", + timeout: 15_000, + stdio: ["ignore", "pipe", "pipe"], + }); + return { status: 0, stdout, stderr: "" }; + } catch (error) { + const err = error as { status?: number; stdout?: string; stderr?: string }; + return { + status: typeof err.status === "number" ? err.status : 1, + stdout: String(err.stdout ?? ""), + stderr: String(err.stderr ?? ""), + }; + } +} + const LOADER_BODY = ` "use strict"; const fs = require("node:fs"); @@ -91,6 +117,27 @@ WebAssembly.instantiate(bytes).then(({ instance }) => { }); `; + +const ESM_LOADER_BODY = ` +import fs from "node:fs"; +import path from "node:path"; +import { fileURLToPath } from "node:url"; +const __dirname = path.dirname(fileURLToPath(import.meta.url)); +const bytes = fs.readFileSync(path.join(__dirname, "core.wasm")); +WebAssembly.instantiate(bytes).then(({ instance }) => { + const fn = instance.exports.compute_score; + if (typeof fn !== "function") { + console.error("missing export"); + process.exit(2); + } + const result = fn(1); + console.log("ok " + result); +}).catch((err) => { + console.error(err && err.message ? err.message : String(err)); + process.exit(1); +}); +`; + const EMPTY_LOADER_BODY = ` "use strict"; const fs = require("node:fs"); @@ -302,8 +349,21 @@ fetch("./core.wasm").then((r) => WebAssembly.instantiateStreaming(r)); const esmLoader = ` import fs from "node:fs"; import path from "node:path"; -const bytes = fs.readFileSync(path.join(__dirname, "core.wasm")); +import { fileURLToPath } from "node:url"; +const dir = path.dirname(fileURLToPath(import.meta.url)); +const bytes = fs.readFileSync(path.join(dir, "core.wasm")); WebAssembly.instantiate(bytes); +`; + const esmUserImport = ` +import fs from "node:fs"; +import { helper } from "./util.js"; +const bytes = fs.readFileSync(new URL("./core.wasm", import.meta.url)); +WebAssembly.instantiate(bytes); +`; + const esmWasmImport = ` +import fs from "node:fs"; +import wasm from "./core.wasm"; +WebAssembly.instantiate(wasm); `; const otherWasm = ` "use strict"; @@ -327,7 +387,23 @@ WebAssembly.instantiate(fs.readFileSync("./other.wasm")); expect( isSafeNodeWasmJsLoader({ source: esmLoader, - filename: "loader.js", + filename: "loader.mjs", + wasmBasename: "core.wasm", + }), + ).toBe(true); + expect(detectWasmJsLoaderCarrier("loader.mjs", esmLoader)).toBe("module"); + expect(esmStaticImportsAreNodeBuiltinsOnly(esmLoader)).toBe(true); + expect( + isSafeNodeWasmJsLoader({ + source: esmUserImport, + filename: "loader.mjs", + wasmBasename: "core.wasm", + }), + ).toBe(false); + expect( + isSafeNodeWasmJsLoader({ + source: esmWasmImport, + filename: "loader.mjs", wasmBasename: "core.wasm", }), ).toBe(false); @@ -368,6 +444,17 @@ WebAssembly.instantiate(fs.readFileSync("./other.wasm")); { name: "helper.cjs", source: nodeLoader }, ]), ).toBeNull(); + expect( + selectNodeWasmJsLoader("core.wasm", [ + { name: "loader.cjs", source: nodeLoader }, + { name: "loader.mjs", source: esmLoader }, + ]), + ).toBe("loader.cjs"); + expect( + selectNodeWasmJsLoader("core.wasm", [ + { name: "loader.mjs", source: esmLoader }, + ]), + ).toBe("loader.mjs"); }); it("protectTarget auto-wires a same-directory Node CJS loader and records wasm-sidecar-entangle", () => { @@ -490,4 +577,136 @@ WebAssembly.instantiate(fs.readFileSync("./other.wasm")); } }); + it("protectTarget auto-wires a same-directory Node ESM loader and records wasm-sidecar-entangle", () => { + const dir = mkdtempSync(join(tmpdir(), "bla-wasm-loader-protect-esm-")); + try { + const entry = join(dir, "core.wasm"); + const sibSrc = "sidecar-payload\n"; + writeFileSync(entry, fixtureComputeScore()); + writeFileSync(join(dir, "sib.bin"), sibSrc, "utf8"); + writeFileSync(join(dir, "loader.mjs"), ESM_LOADER_BODY, "utf8"); + + const res = protectTarget({ + inputPath: entry, + outputPath: entry, + intensity: "balanced", + seed: "pt-wasm-loader-hook-esm", + wasmPreserveExports: ["compute_score"], + sidecarEntangle: true, + sidecarEntanglePaths: ["sib.bin"], + inPlace: true, + }); + expect(res.sidecarEntangle).toBe(true); + expect(res.wasmSidecarLoaderHookApplied).toBe(true); + expect(res.counteredReaVectors).toContain("wasm-sidecar-entangle"); + + const hooked = readFileSync(join(dir, "loader.mjs"), "utf8"); + expect(hooked).toContain(WASM_SIDECAR_LOADER_MARKER); + expect(hooked).toMatch(/import \{ createRequire as /); + expect(hooked).toContain("import.meta.url"); + expect( + isWasmSidecarLoaderHookWired({ + wasmBytes: readFileSync(entry), + wasmPath: entry, + }), + ).toBe(true); + + const inspected = inspectWasmBinary(readFileSync(entry)); + expect(inspected.validMagic).toBe(true); + expect(inspected.customSectionNames).toContain(WASM_SIDECAR_SECTION_NAME); + expect(inspected.exportNames).toContain("compute_score"); + + const ran = runEsmLoader(dir, hooked); + expect(ran.status, ran.stderr || ran.stdout).toBe(0); + expect(ran.stdout).toMatch(/ok 49/); + + const verification = verifyTarget(entry, undefined, { + sidecarEntangleEnabled: true, + }); + const gapIds = (verification.defenseScoreGaps ?? []).map((g) => g.id); + expect(gapIds).not.toContain("wasm-sidecar-host-verify-only"); + expect(gapIds).not.toContain("esm-import-hoist-before-hooks"); + + const bound = readFileSync(entry); + writeFileSync(entry, stripWasmSidecarCustomSection(bound)); + const failed = runEsmLoader(dir, hooked); + expect(failed.status).not.toBe(0); + expect(failed.stderr + failed.stdout).toMatch( + /bla\.sidecar\.v1 custom section missing|integrity binding mismatch/, + ); + } finally { + rmSync(dir, { recursive: true, force: true }); + } + }); + + it("protectTarget does not rewrite an ESM loader that statically imports a user module", () => { + const dir = mkdtempSync(join(tmpdir(), "bla-wasm-loader-esm-user-")); + try { + const entry = join(dir, "core.wasm"); + writeFileSync(entry, fixtureComputeScore()); + writeFileSync(join(dir, "sib.bin"), "sidecar-payload\n", "utf8"); + const esm = + 'import fs from "node:fs";\nimport { helper } from "./util.js";\nWebAssembly.instantiate(fs.readFileSync(new URL("./core.wasm", import.meta.url)));\n'; + writeFileSync(join(dir, "loader.mjs"), esm, "utf8"); + + const res = protectTarget({ + inputPath: entry, + outputPath: entry, + intensity: "balanced", + seed: "pt-wasm-loader-esm-user", + wasmPreserveExports: ["compute_score"], + sidecarEntangle: true, + sidecarEntanglePaths: ["sib.bin"], + inPlace: true, + }); + expect(res.wasmSidecarLoaderHookApplied).toBeUndefined(); + expect(res.counteredReaVectors).not.toContain("wasm-sidecar-entangle"); + expect(readFileSync(join(dir, "loader.mjs"), "utf8")).toBe(esm); + } finally { + rmSync(dir, { recursive: true, force: true }); + } + }); + + it("hardenProject auto-wires dist/loader.mjs beside core.wasm", () => { + const dir = mkdtempSync(join(tmpdir(), "bla-wasm-loader-harden-esm-")); + try { + const dist = join(dir, "dist"); + mkdirSync(dist, { recursive: true }); + writeFileSync(join(dist, "core.wasm"), fixtureComputeScore()); + writeFileSync(join(dist, "sib.bin"), "sidecar-payload\n", "utf8"); + writeFileSync(join(dist, "loader.mjs"), ESM_LOADER_BODY, "utf8"); + + const report = hardenProject({ + directory: dir, + intensity: "balanced", + seed: "harden-wasm-loader-hook-esm", + sidecarEntangle: true, + wasmPreserveExports: ["compute_score"], + }); + expect(report.overallSuccess).toBe(true); + expect(report.sidecarEntangleError).toBeUndefined(); + + const outDist = join(report.outputDirectory, "dist"); + const hooked = readFileSync(join(outDist, "loader.mjs"), "utf8"); + expect(hooked).toContain(WASM_SIDECAR_LOADER_MARKER); + expect(hooked).toMatch(/import \{ createRequire as /); + const wasmBytes = readFileSync(join(outDist, "core.wasm")); + expect(inspectWasmBinary(wasmBytes).customSectionNames).toContain( + WASM_SIDECAR_SECTION_NAME, + ); + expect( + isWasmSidecarLoaderHookWired({ + wasmBytes, + wasmPath: join(outDist, "core.wasm"), + }), + ).toBe(true); + + const ran = runEsmLoader(outDist, hooked); + expect(ran.status, ran.stderr || ran.stdout).toBe(0); + expect(ran.stdout).toMatch(/ok 49/); + } finally { + rmSync(dir, { recursive: true, force: true }); + } + }); + });