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:
@@ -5,6 +5,7 @@ import { AnalysisCapabilityUnavailableError } from "../domain/analysisErrorCore.
|
||||
import type { AndroidOperation } from "../domain/android/androidAnalysis.js";
|
||||
import type { JsonValue } from "../domain/jsonValue.js";
|
||||
import type { ProviderAvailability } from "../application/AnalysisProvider.js";
|
||||
import { ZipArtifactReader } from "../artifacts/ZipArtifactReader.js";
|
||||
import {
|
||||
execFileOutput,
|
||||
execFileOutputFailure,
|
||||
@@ -13,6 +14,9 @@ import { JADX_JAR_CONFIGURATION_REMEDIATION } from "./JadxRelease.js";
|
||||
|
||||
const JAVA_MODULE_LISTING_TIMEOUT_MS = 10_000;
|
||||
const JAVA_MODULE_LISTING_MAX_OUTPUT_BYTES = 1024 * 1024;
|
||||
// Bound ZIP metadata allocation during discovery. The audited 0.7.1 JAR's
|
||||
// central directory occupies 2,294,350 bytes; class bodies are not read here.
|
||||
const JAR_METADATA_READ_MAX_BYTES = 8 * 1024 * 1024;
|
||||
|
||||
/** Caller-supplied tools, resolved only when an Android operation is selected. */
|
||||
export interface JadxConfiguration {
|
||||
@@ -116,9 +120,53 @@ const readJadxConfiguration = async (
|
||||
await requireJavaHomeExecutable(environment.JAVA_HOME, java);
|
||||
const jvmArguments = resolveJvmArguments(environment);
|
||||
await inspectJavaRuntime(java, environment, signal);
|
||||
await requireEngineArchive(jar, signal);
|
||||
return { jar, java, jvmArguments };
|
||||
};
|
||||
|
||||
const requireEngineArchive = async (
|
||||
jar: string,
|
||||
signal?: AbortSignal,
|
||||
): Promise<void> => {
|
||||
// These classes are consumed directly by ReaJadxBridge.java. This checks the
|
||||
// classpath inventory without loading engine code or starting a JADX session.
|
||||
const missing = new Set([
|
||||
"com/atxx/jhmcp/JadxSession.class",
|
||||
"com/atxx/jhmcp/SessionHolder.class",
|
||||
"jadx/api/JadxDecompiler.class",
|
||||
"com/google/gson/Gson.class",
|
||||
"io/modelcontextprotocol/kotlin/sdk/server/Server.class",
|
||||
]);
|
||||
const reader = new ZipArtifactReader(jar, "zip", JAR_METADATA_READ_MAX_BYTES);
|
||||
try {
|
||||
for await (const entry of reader.entries(signal)) {
|
||||
if (entry.kind === "file" && !entry.encrypted) missing.delete(entry.path);
|
||||
if (missing.size === 0) return;
|
||||
}
|
||||
} catch (cause) {
|
||||
if (signal?.aborted === true) throw cause;
|
||||
const reason = cause instanceof Error ? cause.message : String(cause);
|
||||
throw configurationFailure(
|
||||
"version_unresolved",
|
||||
`Cannot inspect REA_JADX_MCP_JAR ${jar} as a Java archive: ${reason}. ${JADX_JAR_CONFIGURATION_REMEDIATION}`,
|
||||
{
|
||||
jar_path: jar,
|
||||
phase: "jar-inspection",
|
||||
archive_error: reason,
|
||||
maximum_metadata_read_bytes: JAR_METADATA_READ_MAX_BYTES,
|
||||
},
|
||||
cause,
|
||||
);
|
||||
} finally {
|
||||
await reader.close();
|
||||
}
|
||||
throw configurationFailure(
|
||||
"unsupported_version",
|
||||
`REA_JADX_MCP_JAR ${jar} does not contain the required metadata bridge classes: ${[...missing].join(", ")}. ${JADX_JAR_CONFIGURATION_REMEDIATION}`,
|
||||
{ jar_path: jar, phase: "jar-inspection", missing_classes: [...missing] },
|
||||
);
|
||||
};
|
||||
|
||||
const requireSupportedHost = (): void => {
|
||||
if (process.platform === "linux" || process.platform === "darwin") return;
|
||||
throw configurationFailure(
|
||||
|
||||
@@ -14,7 +14,10 @@ import type { ZipPackageFormat } from "../domain/zipPackageFormat.js";
|
||||
class NodeFileReader extends Reader<string> {
|
||||
#handle: FileHandle | undefined;
|
||||
|
||||
constructor(private readonly path: string) {
|
||||
constructor(
|
||||
private readonly path: string,
|
||||
private readonly maximumReadBytes?: number,
|
||||
) {
|
||||
super(path);
|
||||
}
|
||||
|
||||
@@ -29,7 +32,16 @@ class NodeFileReader extends Reader<string> {
|
||||
): Promise<Uint8Array> {
|
||||
const handle = this.#handle;
|
||||
if (handle === undefined) throw new Error("ZIP reader is not initialized");
|
||||
const bytes = Buffer.alloc(Math.min(length, this.size - index));
|
||||
const readBytes = Math.min(length, this.size - index);
|
||||
if (
|
||||
this.maximumReadBytes !== undefined &&
|
||||
readBytes > this.maximumReadBytes
|
||||
)
|
||||
throw new ArtifactReaderFailure(
|
||||
"limit",
|
||||
`ZIP metadata read requires ${readBytes} bytes, exceeding the ${this.maximumReadBytes}-byte read budget`,
|
||||
);
|
||||
const bytes = Buffer.alloc(readBytes);
|
||||
const read = await handle.read(bytes, 0, bytes.length, index);
|
||||
return bytes.subarray(0, read.bytesRead);
|
||||
}
|
||||
@@ -47,9 +59,14 @@ export class ZipArtifactReader implements ArtifactReader {
|
||||
readonly #reader: ZipReader<string>;
|
||||
readonly #entries = new Map<string, Entry>();
|
||||
|
||||
constructor(path: string, format: ZipPackageFormat) {
|
||||
/** Optionally bound each metadata read before allocating its backing buffer. */
|
||||
constructor(
|
||||
path: string,
|
||||
format: ZipPackageFormat,
|
||||
maximumMetadataReadBytes?: number,
|
||||
) {
|
||||
this.format = format;
|
||||
this.#source = new NodeFileReader(path);
|
||||
this.#source = new NodeFileReader(path, maximumMetadataReadBytes);
|
||||
this.#reader = new ZipReader(this.#source, {
|
||||
checkSignature: true,
|
||||
checkOverlappingEntry: true,
|
||||
|
||||
Reference in New Issue
Block a user