2026-10-06 20:46:48 +08:00
|
|
|
import { createHash } from "node:crypto";
|
2026-10-11 00:27:55 +08:00
|
|
|
import type { Stats } from "node:fs";
|
|
|
|
|
import { chmod, open, rm, stat, type FileHandle } from "node:fs/promises";
|
2026-10-06 20:46:48 +08:00
|
|
|
import { join } from "node:path";
|
2026-10-11 00:27:55 +08:00
|
|
|
import { Transform, type TransformCallback } from "node:stream";
|
|
|
|
|
import { pipeline } from "node:stream/promises";
|
2026-10-07 16:50:41 +08:00
|
|
|
import {
|
|
|
|
|
AnalysisInputError,
|
|
|
|
|
AnalysisCapabilityUnavailableError,
|
|
|
|
|
} from "../domain/analysisErrorCore.js";
|
2026-10-11 00:27:55 +08:00
|
|
|
import {
|
|
|
|
|
openRegularFile,
|
|
|
|
|
sameRegularFileState,
|
|
|
|
|
} from "../filesystem/RegularFile.js";
|
2026-10-07 14:09:57 +08:00
|
|
|
import type { AndroidOperation } from "../domain/android/androidAnalysis.js";
|
2026-10-06 20:46:48 +08:00
|
|
|
|
|
|
|
|
/** Bind decompiler input to the admitted artifact, even if the original changes. */
|
|
|
|
|
export const snapshotAndroidTarget = async (
|
|
|
|
|
path: string,
|
|
|
|
|
sha256: string,
|
|
|
|
|
root: string,
|
|
|
|
|
operation: AndroidOperation,
|
|
|
|
|
): Promise<string> => {
|
|
|
|
|
const snapshot = join(root, "target.apk");
|
2026-10-11 00:27:55 +08:00
|
|
|
try {
|
|
|
|
|
await copyAndroidSnapshot(path, snapshot, sha256);
|
|
|
|
|
await chmod(snapshot, 0o400);
|
|
|
|
|
return snapshot;
|
|
|
|
|
} catch (cause: unknown) {
|
|
|
|
|
if (!(cause instanceof AndroidSnapshotChangedError)) throw cause;
|
|
|
|
|
throw new AnalysisInputError(operation, { cause }, [
|
2026-10-06 20:46:48 +08:00
|
|
|
{
|
|
|
|
|
path: ["path"],
|
|
|
|
|
reason: "invalid_value",
|
2026-10-11 00:27:55 +08:00
|
|
|
message: `APK bytes changed after admission at ${path}; retry against a stable file.`,
|
2026-10-06 20:46:48 +08:00
|
|
|
},
|
|
|
|
|
]);
|
2026-10-11 00:27:55 +08:00
|
|
|
}
|
2026-10-06 20:46:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** Fingerprint the actual immutable engine bytes executed for this observation. */
|
|
|
|
|
export const snapshotAndroidEngine = async (
|
|
|
|
|
source: string,
|
2026-10-07 16:50:41 +08:00
|
|
|
sha256: string,
|
2026-10-06 20:46:48 +08:00
|
|
|
root: string,
|
2026-10-07 16:50:41 +08:00
|
|
|
operation: AndroidOperation,
|
2026-10-06 20:46:48 +08:00
|
|
|
): Promise<{ path: string; sha256: string }> => {
|
|
|
|
|
const path = join(root, "engine.jar");
|
2026-10-11 00:27:55 +08:00
|
|
|
try {
|
|
|
|
|
const observed = await copyAndroidSnapshot(source, path, sha256);
|
|
|
|
|
await chmod(path, 0o400);
|
|
|
|
|
return { path, sha256: observed };
|
|
|
|
|
} catch (cause: unknown) {
|
|
|
|
|
if (!(cause instanceof AndroidSnapshotChangedError)) throw cause;
|
2026-10-07 16:50:41 +08:00
|
|
|
throw new AnalysisCapabilityUnavailableError(
|
|
|
|
|
"jadx",
|
|
|
|
|
operation,
|
2026-10-11 00:27:55 +08:00
|
|
|
`REA_JADX_MCP_JAR bytes changed during admission at ${source}; retry with a stable engine file.`,
|
|
|
|
|
{ cause },
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const copyAndroidSnapshot = async (
|
|
|
|
|
sourcePath: string,
|
|
|
|
|
snapshotPath: string,
|
|
|
|
|
expectedSha256: string,
|
|
|
|
|
): Promise<string> => {
|
|
|
|
|
const source = await openRegularFile(sourcePath, { symlinks: "follow" });
|
|
|
|
|
let destination: FileHandle | undefined;
|
|
|
|
|
let copiedBytes = 0;
|
|
|
|
|
const hash = createHash("sha256");
|
|
|
|
|
try {
|
|
|
|
|
const initial = await source.stat();
|
|
|
|
|
const digest = new Transform({
|
|
|
|
|
transform(
|
|
|
|
|
chunk: Buffer,
|
|
|
|
|
_encoding: BufferEncoding,
|
|
|
|
|
callback: TransformCallback,
|
|
|
|
|
) {
|
|
|
|
|
copiedBytes += chunk.length;
|
|
|
|
|
if (copiedBytes > initial.size) {
|
|
|
|
|
callback(
|
|
|
|
|
new AndroidSnapshotChangedError(
|
|
|
|
|
`Android input grew while being copied: ${sourcePath}`,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
hash.update(chunk);
|
|
|
|
|
callback(null, chunk);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
try {
|
|
|
|
|
destination = await open(snapshotPath, "wx", 0o600);
|
|
|
|
|
await pipeline(
|
|
|
|
|
source.createReadStream({
|
|
|
|
|
start: 0,
|
|
|
|
|
end: initial.size,
|
|
|
|
|
autoClose: false,
|
|
|
|
|
}),
|
|
|
|
|
digest,
|
|
|
|
|
destination.createWriteStream({ autoClose: false }),
|
|
|
|
|
);
|
|
|
|
|
await verifySourceState(source, sourcePath, initial, copiedBytes);
|
|
|
|
|
const observedSha256 = hash.digest("hex");
|
|
|
|
|
if (observedSha256 !== expectedSha256)
|
|
|
|
|
throw new AndroidSnapshotChangedError(
|
|
|
|
|
`Android snapshot digest mismatch for ${sourcePath}`,
|
|
|
|
|
);
|
|
|
|
|
return observedSha256;
|
|
|
|
|
} catch (cause: unknown) {
|
|
|
|
|
if (destination !== undefined)
|
|
|
|
|
await rm(snapshotPath, { force: true }).catch(() => undefined);
|
|
|
|
|
throw cause;
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
await Promise.all([source.close(), destination?.close()]);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const verifySourceState = async (
|
|
|
|
|
handle: Awaited<ReturnType<typeof openRegularFile>>,
|
|
|
|
|
path: string,
|
|
|
|
|
initial: Stats,
|
|
|
|
|
observedBytes: number,
|
|
|
|
|
): Promise<void> => {
|
|
|
|
|
const [opened, currentPath] = await Promise.all([handle.stat(), stat(path)]);
|
|
|
|
|
if (
|
|
|
|
|
observedBytes !== initial.size ||
|
|
|
|
|
!sameRegularFileState(initial, opened) ||
|
|
|
|
|
!sameRegularFileState(initial, currentPath)
|
|
|
|
|
)
|
|
|
|
|
throw new AndroidSnapshotChangedError(
|
|
|
|
|
`Android input changed while being read: ${path}`,
|
2026-10-07 16:50:41 +08:00
|
|
|
);
|
2026-10-06 20:46:48 +08:00
|
|
|
};
|
2026-10-11 00:27:55 +08:00
|
|
|
|
|
|
|
|
class AndroidSnapshotChangedError extends Error {
|
|
|
|
|
constructor(message: string, options?: ErrorOptions) {
|
|
|
|
|
super(message, options);
|
|
|
|
|
}
|
|
|
|
|
}
|