Files
reverse-engineering-anything/tests/acceptance/applications/webpackArrayModuleTablesCli.test.ts
ThejeshandVincent Koc aeca62b117 fix(javascript): recover webpack chunks with array module tables (#13829)
* fix(javascript): recover webpack chunks with array module tables

Webpack writes a chunk's module table as a sparse array when numeric
module ids are dense (webpack 5 moduleIds natural/size, webpack 4
production builds), and webpack 4 registers chunks on webpackJsonp.
Chunk registration accepted only object tables and *Chunk* globals, so
these chunks recovered no modules while coverage stayed complete.

Read object, sparse-array and Array(n).concat([...]) tables through one
moduleTableEntries helper (index plus offset is the module id) and
recognize webpackJsonp as a chunk runtime.

* fix(javascript): mark webpackJsonp chunks as webpack vendor

A minified webpack 4 chunk whose factories never spell
__webpack_require__ now yields a webpackJsonp registration with
bundler "webpack", but detectVendors matched only __webpack_require__
and webpackChunk, so the same file reported no webpack vendor and the
artifact graph's vendor_markers stayed empty.

Add webpackJsonp to the webpack vendor markers, next to webpackChunk,
so the marker and the chunk-runtime classification agree. This also
covers the not-examined path, which reports vendors without
registrations.

* fix(javascript): validate intrinsic array module table offsets

* fix(javascript): guard inherited array slots in table recovery

---------

Co-authored-by: Vincent Koc <vincentkoc@ieee.org>
2026-11-30 22:52:55 +08:00

49 lines
1.8 KiB
TypeScript

import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect } from "vitest";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { cliTest } from "../../support/cli/cliFixture.js";
import { analyzeJavaScriptDirectory } from "../../support/cli/javascriptApplication.js";
import {
findGraphEdge,
findModuleNodeByKey,
} from "../../support/javascriptApplicationFixture.js";
cliTest.for(["CLI", "stdio MCP"] as const)(
"recovers exact sparse-array module identities and their dependency through %s",
async (surface, { cli }) => {
const root = await createTestTempDirectory("rea-webpack-array-");
await writeFile(
join(root, "chunk.js"),
`globalThis.webpackChunkFixture.push([[5], Array(7).concat([
function (module, exports, require) { exports.greet = require(9).greet; },
,
function (module, exports) { exports.greet = () => "hello"; }
])]);`,
);
const { graph } = await analyzeJavaScriptDirectory(surface, cli, root);
const modules = graph.nodes.filter(
(node) =>
node.kind === "javascript-module" &&
node.observations.some(
(o) => typeof o.properties.module_key === "string",
),
);
expect(
modules.map((node) => node.observations[0]?.properties.module_key).sort(),
).toEqual(["7", "9"]);
const first = findModuleNodeByKey(graph, "7");
const second = findModuleNodeByKey(graph, "9");
expect(first?.observations[0]).toMatchObject({
properties: { exports: ["greet"] },
evidence: {
location: { value: { source: "chunk.js", start: { line: 2 } } },
},
});
expect(second?.observations[0]).toMatchObject({
properties: { exports: ["greet"] },
});
expect(findGraphEdge(graph, first, second, "imports")).toBeDefined();
},
);