fix(android): validate engine archive prerequisites (#1145)

* fix(android): validate engine archive prerequisites

* fix(android): bound engine metadata reads before allocation
This commit is contained in:
Ysopwnable
2026-10-09 09:15:33 +08:00
committed by GitHub
parent 13935077ba
commit e9ebfb94ef
9 changed files with 180 additions and 17 deletions
@@ -9,14 +9,40 @@ import {
} from "../../../src/android/JadxConfiguration.js";
import { AnalysisCapabilityUnavailableError } from "../../../src/domain/analysisErrorCore.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { writeOrderedZip } from "../../fixtures/artifactEntryOrder.js";
import { writeJadxJarInventory } from "../../fixtures/android/jadxJar.js";
const it = test.skipIf(process.platform === "win32");
it("rejects malformed or unrelated engine archives with JAR-specific recovery", async () => {
const root = await createTestTempDirectory("rea-jadx-invalid-jar-");
const javaHome = await createFakeJdk(root);
const jar = join(root, "engine.jar");
for (const malformed of [true, false]) {
if (malformed) await writeFile(jar, "not a Java archive");
else await writeOrderedZip(jar, ["unrelated/Library.class"]);
const environment = { REA_JADX_MCP_JAR: jar, JAVA_HOME: javaHome };
const availability = await inspectJadxAvailability(environment);
expect(availability).toMatchObject({
status: "unavailable",
reason: expect.stringContaining("REA_JADX_MCP_JAR"),
diagnostics: { jar_path: await realpath(jar), phase: "jar-inspection" },
});
await expect(
resolveJadxConfiguration(environment, "inspect_android_package"),
).rejects.toMatchObject({
_tag: "AnalysisCapabilityUnavailableError",
reason: expect.stringContaining("REA_JADX_MCP_JAR"),
});
expect(availability.reason).not.toContain("Select a full JDK");
}
});
it("rejects an explicit JAVA_HOME without an executable java binary", async () => {
const root = await createTestTempDirectory("rea-jadx-config-");
const jar = join(root, "jadx-headless-mcp.jar");
const javaHome = join(root, "missing-jdk");
await writeFile(jar, "fixture jar");
await writeJadxJarInventory(jar);
await expect(
resolveJadxConfiguration(
@@ -33,7 +59,7 @@ it("accepts a full JDK and canonicalizes the selected JAR", async () => {
const root = await createTestTempDirectory("rea-jadx-config-");
const jar = join(root, "jadx-headless-mcp.jar");
const javaHome = await createFakeJdk(root);
await writeFile(jar, "fixture jar");
await writeJadxJarInventory(jar);
await expect(
resolveJadxConfiguration(
@@ -51,7 +77,7 @@ it("configures heap and visible processors independently without forcing either"
const root = await createTestTempDirectory("rea-jadx-config-");
const jar = join(root, "engine.jar");
const javaHome = await createFakeJdk(root);
await writeFile(jar, "fixture jar");
await writeJadxJarInventory(jar);
const configuration = await resolveJadxConfiguration(
{
REA_JADX_MCP_JAR: jar,
@@ -98,7 +124,7 @@ it("configures heap and visible processors independently without forcing either"
it("distinguishes a runnable JRE from a missing Java executable", async () => {
const root = await createTestTempDirectory("rea-jadx-runtime-");
const jar = join(root, "jadx-headless-mcp.jar");
await writeFile(jar, "fixture jar");
await writeJadxJarInventory(jar);
const jreHome = await createFakeJdk(root, false);
await expect(
resolveJadxConfiguration(
@@ -142,7 +168,7 @@ it("preserves exact Java probe output and marks max-buffer capture as truncated"
const bin = join(javaHome, "bin");
const java = join(bin, "java");
await mkdir(bin, { recursive: true });
await writeFile(jar, "fixture jar");
await writeJadxJarInventory(jar);
await writeFile(
java,
`#!/bin/sh\nexec ${process.execPath} -e 'process.stdout.write("x".repeat(2 * 1024 * 1024))'\n`,
@@ -186,7 +212,7 @@ it("preserves exact Java probe output and marks max-buffer capture as truncated"
it("distinguishes an unreported Java version from an unsupported old version", async () => {
const root = await createTestTempDirectory("rea-jadx-version-listing-");
const jar = join(root, "jadx-headless-mcp.jar");
await writeFile(jar, "fixture jar");
await writeJadxJarInventory(jar);
const unresolvedHome = await createFakeJdk(root);
await writeFile(
@@ -233,7 +259,7 @@ it("cancels an in-flight Java readiness probe", async () => {
const java = join(bin, "java");
const marker = join(root, "java-probe-started");
await mkdir(bin, { recursive: true });
await writeFile(jar, "fixture jar");
await writeJadxJarInventory(jar);
await writeFile(
java,
`#!/bin/sh\nexec ${process.execPath} -e 'require("node:fs").writeFileSync(process.argv[1], "ready"); setTimeout(() => {}, 30_000)' '${marker}'\n`,
@@ -269,7 +295,7 @@ it("reports a killed Java probe as unresolved instead of missing runtime", async
const java = join(bin, "java");
const marker = join(root, "java-probe-started");
await mkdir(bin, { recursive: true });
await writeFile(jar, "fixture jar");
await writeJadxJarInventory(jar);
await writeFile(
java,
`#!/bin/sh\nexec ${process.execPath} -e 'require("node:fs").writeFileSync(process.argv[1], "ready"); setTimeout(() => {}, 30_000)' '${marker}'\n`,
@@ -9,8 +9,32 @@ import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { parseBinaryTarget } from "../../../src/application/BinaryTargetResolver.js";
import { ArtifactProvider } from "../../../src/artifacts/ArtifactProvider.js";
import { ZipArtifactReader } from "../../../src/artifacts/ZipArtifactReader.js";
import { artifactInventoryResultSchema } from "../../../src/domain/artifactGraph.js";
it("bounds a requested metadata read without rejecting the underlying valid ZIP", async () => {
const root = await createTestTempDirectory("rea-zip-metadata-budget-");
const path = join(root, "large-directory.zip");
const name = `${"a".repeat(8192)}.class`;
await writeOrderedZip(path, [name]);
const bounded = new ZipArtifactReader(path, "zip", 4096);
const control = new ZipArtifactReader(path, "zip");
const collect = async (reader: ZipArtifactReader): Promise<string[]> => {
const names: string[] = [];
for await (const entry of reader.entries()) names.push(entry.path);
return names;
};
try {
await expect(collect(bounded)).rejects.toMatchObject({
reason: "limit",
message: expect.stringContaining("4096-byte read budget"),
});
await expect(collect(control)).resolves.toEqual([name]);
} finally {
await Promise.all([bounded.close(), control.close()]);
}
});
it("rejects overlapping ZIP member data ranges through artifact inventory", async () => {
const root = await createTestTempDirectory("rea-zip-overlap-");
const path = join(root, "overlap.zip");
@@ -13,6 +13,7 @@ import {
createJadxProtocolFixture as setup,
verifyJadxFixtureCleanup as verifyCleanup,
} from "../../fixtures/android/jadx.js";
import { writeJadxJarInventory } from "../../fixtures/android/jadxJar.js";
const it = test.skipIf(process.platform === "win32");
@@ -104,7 +105,7 @@ it("retire sessions when APK bytes, engine bytes or JVM options change", async (
const changed = await inspect();
expect(changed.subject?.digest.sha256).not.toBe(first.subject?.digest.sha256);
await verifyCleanup(launches.slice(0, 1));
await writeFile(jar, "new engine bytes");
await writeJadxJarInventory(jar, ["fixture/NewEngineMarker.class"]);
await inspect();
environment.JAVA_TOOL_OPTIONS = "-Xmx8g";
environment._JAVA_OPTIONS = "-XX:+UseG1GC";