Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

fix(browser): keep bare module locations unresolved (#676)

Rudy Mizrahi Celekli818908151144
diff --git a/src/browser/WebSourceMapFetcher.ts b/src/browser/WebSourceMapFetcher.ts
index 94170bf34..a18bc29ae 100644
--- a/src/browser/WebSourceMapFetcher.ts
+++ b/src/browser/WebSourceMapFetcher.ts
@@ -2,6 +2,7 @@ import * as t from "@babel/types";
import { AnyMap, eachMapping } from "@jridgewell/trace-mapping";
import { sanitizeBrowserUrl } from "../domain/browserObservation.js";
+import { isUrlLikeModuleSpecifier } from "../domain/webBundleAnalyzerAst.js";
import { analyzeParsedJavaScriptSemantics } from "../domain/javascriptSemanticAnalysis.js";
import { parseJavaScriptSource } from "../domain/javascriptSourceParser.js";
import { hasValidSourceMapContents } from "../domain/sourceMapContents.js";
@@ -352,6 +353,7 @@ const resolveOriginalSource = (
specifier: string,
base: string,
): string | null => {
+ if (!isUrlLikeModuleSpecifier(specifier)) return null;
try {
return sanitizeSource(new URL(specifier, base).href);
} catch (cause: unknown) {
diff --git a/src/domain/webBundleAnalyzer.test.ts b/src/domain/webBundleAnalyzer.test.ts
index 7d3f96512..aa1d3b574 100644
--- a/src/domain/webBundleAnalyzer.test.ts
+++ b/src/domain/webBundleAnalyzer.test.ts
@@ -295,3 +295,31 @@ const inspection = (source: string) =>
},
limitations: [],
});
+
+it("keeps bare module locations unresolved without inventing URL-relative paths", () => {
+ const result = analyzeCapturedWebBundle(
+ inspection(`
+ import "package-name";
+ import("@scope/package");
+ require("package-name");
+ import "./actual.js";
+ importScripts("worker.js");
+ `),
+ );
+ for (const specifier of ["package-name", "@scope/package"])
+ expect(
+ result.observations.chunks.edges
+ .filter((edge) => edge.specifier === specifier)
+ .every((edge) => edge.resolved_url === null),
+ ).toBe(true);
+ expect(
+ result.observations.chunks.edges.find(
+ (edge) => edge.specifier === "./actual.js",
+ )?.resolved_url,
+ ).toBe(`${origin}/assets/actual.js`);
+ expect(
+ result.observations.chunks.edges.find(
+ (edge) => edge.specifier === "worker.js",
+ )?.resolved_url,
+ ).toBe(`${origin}/assets/worker.js`);
+});
diff --git a/src/domain/webBundleAnalyzerAst.ts b/src/domain/webBundleAnalyzerAst.ts
index 9b8704e63..963a194a7 100644
--- a/src/domain/webBundleAnalyzerAst.ts
+++ b/src/domain/webBundleAnalyzerAst.ts
@@ -44,6 +44,13 @@ export const objectString = (
return t.isStringLiteral(value) ? value.value : undefined;
};
+/** Whether module syntax supplies a URL location without a package/import-map resolver. */
+export const isUrlLikeModuleSpecifier = (specifier: string): boolean =>
+ specifier.startsWith("/") ||
+ specifier.startsWith("./") ||
+ specifier.startsWith("../") ||
+ /^[A-Za-z][A-Za-z0-9+.-]*:/u.test(specifier);
+
export const resolveSpecifier = (
specifier: string,
base: string,
diff --git a/src/domain/webBundleAnalyzerInspection.ts b/src/domain/webBundleAnalyzerInspection.ts
index ed45f8948..9c28482ec 100644
--- a/src/domain/webBundleAnalyzerInspection.ts
+++ b/src/domain/webBundleAnalyzerInspection.ts
@@ -13,6 +13,7 @@ import {
objectValue,
propertyName,
resolveSpecifier,
+ isUrlLikeModuleSpecifier,
stringArgument,
} from "./webBundleAnalyzerAst.js";
@@ -198,7 +199,11 @@ const addEdge = (context: AddEdgeContext): void => {
from_script_key: context.script.script_key,
kind: context.kind,
specifier: context.specifier,
- resolved_url: resolveSpecifier(context.specifier, context.script.url),
+ resolved_url:
+ context.kind === "worker_import" ||
+ isUrlLikeModuleSpecifier(context.specifier)
+ ? resolveSpecifier(context.specifier, context.script.url)
+ : null,
location: location(context.script.script_key, context.node),
}),
);
diff --git a/tests/boundary/browser/webSourceMapFetcher.test.ts b/tests/boundary/browser/webSourceMapFetcher.test.ts
index 15b25752a..9cb36f1d4 100644
--- a/tests/boundary/browser/webSourceMapFetcher.test.ts
+++ b/tests/boundary/browser/webSourceMapFetcher.test.ts
@@ -524,3 +524,29 @@ const input = (overrides: Record<string, unknown> = {}) => {
...overrides,
});
};
+
+it("keeps bare original module specifiers unresolved", async () => {
+ const result = await fetchWebSourceMaps([request], input(), undefined, {
+ fetch: () =>
+ Promise.resolve(
+ new Response(
+ JSON.stringify({
+ version: 3,
+ names: [],
+ sources: ["main.ts"],
+ sourcesContent: ['import "package-name"; import "./actual.js";'],
+ mappings: "AAAA",
+ }),
+ ),
+ ),
+ });
+ const edges = result.items[0]?.original_module_edges;
+ expect(
+ edges?.find(({ specifier }) => specifier === "package-name")
+ ?.resolved_source,
+ ).toBeNull();
+ expect(
+ edges?.find(({ specifier }) => specifier === "./actual.js")
+ ?.resolved_source,
+ ).toContain("/assets/actual.js");
+});