feat(android): integrate static APK analysis with headless JADX (#738)
* feat(android): integrate static APK analysis with headless JADX * test(android): include APK tools in explicit effects audit
This commit is contained in:
Vendored
+135
@@ -0,0 +1,135 @@
|
||||
import { McpServer } from "@modelcontextprotocol/server";
|
||||
import { StdioServerTransport } from "@modelcontextprotocol/server/stdio";
|
||||
import { z } from "zod";
|
||||
|
||||
// An actual child MCP server exercising the production stdio/process seam.
|
||||
// It is a protocol fixture, not evidence of JADX decompiler support.
|
||||
const mode = process.env.REA_FAKE_JADX_MODE ?? "normal";
|
||||
const server = new McpServer({
|
||||
name: "jadx-headless-mcp",
|
||||
version: mode === "version" ? "99.0.0" : "0.7.1",
|
||||
});
|
||||
const classes = [
|
||||
"fixture.Target",
|
||||
...Array.from({ length: 400 }, (_, index) => `fixture.Class${index}`),
|
||||
];
|
||||
const methods = [
|
||||
{
|
||||
name: "onCreate",
|
||||
signature: "onCreate(Bundle): void",
|
||||
is_constructor: false,
|
||||
def_pos: 0,
|
||||
},
|
||||
{
|
||||
name: "choose",
|
||||
signature: "choose(String): void",
|
||||
is_constructor: false,
|
||||
def_pos: 0,
|
||||
},
|
||||
{
|
||||
name: "choose",
|
||||
signature: "choose(int): void",
|
||||
is_constructor: false,
|
||||
def_pos: 0,
|
||||
},
|
||||
];
|
||||
const text = (value) => ({ content: [{ type: "text", text: value }] });
|
||||
const json = (value) => text(JSON.stringify(value));
|
||||
const register = (name, handler) =>
|
||||
server.registerTool(name, { inputSchema: z.looseObject({}) }, handler);
|
||||
register("load_apk", async ({ path }) => {
|
||||
if (mode === "stall") await new Promise(() => {});
|
||||
return json({
|
||||
state: "LOADED",
|
||||
apk_path: mode === "wrong-apk" ? "/different.apk" : path,
|
||||
class_count: classes.length,
|
||||
resource_count: 1,
|
||||
threads: 1,
|
||||
resources: "full",
|
||||
});
|
||||
});
|
||||
register("get_app_info", () =>
|
||||
json({
|
||||
package: "fixture",
|
||||
version_name: "1",
|
||||
version_code: "1",
|
||||
min_sdk: "26",
|
||||
target_sdk: "33",
|
||||
permissions: ["android.permission.INTERNET"],
|
||||
}),
|
||||
);
|
||||
register("get_android_manifest", () => {
|
||||
if (mode === "tool-error")
|
||||
return {
|
||||
...text("manifest decoder rejected malformed binary XML"),
|
||||
isError: true,
|
||||
};
|
||||
if (mode === "frame-overflow") return text("x".repeat(9 * 1024 * 1024));
|
||||
return text('<manifest package="fixture"/>');
|
||||
});
|
||||
register("list_classes", ({ offset, limit }) =>
|
||||
json({
|
||||
total: classes.length,
|
||||
offset,
|
||||
limit,
|
||||
items: mode === "empty-page" ? [] : classes.slice(offset, offset + limit),
|
||||
}),
|
||||
);
|
||||
register("get_class_summary", () =>
|
||||
json({
|
||||
full_name: mode === "wrong-class" ? "fixture.Other" : "fixture.Target",
|
||||
name: "Target",
|
||||
method_count: mode === "summary-error" ? -1 : methods.length,
|
||||
field_count: 0,
|
||||
inner_class_count: 0,
|
||||
methods: mode === "summary-error" ? [] : methods,
|
||||
fields: [],
|
||||
inner_classes: [],
|
||||
...(mode === "summary-error" ? { error: "summary timed out" } : {}),
|
||||
}),
|
||||
);
|
||||
register("get_method_body", ({ method_name, overload_index }) =>
|
||||
json({
|
||||
class_name: "fixture.Target",
|
||||
method_name,
|
||||
full_name: `fixture.Target.${method_name}`,
|
||||
overload_index:
|
||||
mode === "wrong-method" ? overload_index + 1 : overload_index,
|
||||
overload_count: method_name === "choose" ? 2 : 1,
|
||||
mode: mode === "smali" || mode === "overloaded-smali" ? "smali" : "java",
|
||||
fell_back: mode === "smali" || mode === "overloaded-smali",
|
||||
markers:
|
||||
mode === "smali" || mode === "overloaded-smali"
|
||||
? ["Method not decompiled"]
|
||||
: [],
|
||||
body:
|
||||
mode === "no-body"
|
||||
? "// method exists but has no decompiled body (native/abstract)"
|
||||
: mode === "body-error"
|
||||
? "// ERROR: decompile timed out or failed"
|
||||
: mode === "truncated"
|
||||
? "void onCreate(){}\n\n... [truncated: exceeds 18 bytes, total 100 bytes]"
|
||||
: `void ${method_name}() { /* overload ${overload_index} */ }`,
|
||||
}),
|
||||
);
|
||||
for (const name of ["get_xrefs_to_class", "get_xrefs_to_method"])
|
||||
register(name, ({ class_name, method_name }) =>
|
||||
json({
|
||||
target:
|
||||
method_name === undefined ? class_name : `${class_name}.${method_name}`,
|
||||
total: mode === "partial-xrefs" ? 2 : 1,
|
||||
count: 1,
|
||||
resolve_line: false,
|
||||
items: [
|
||||
{
|
||||
kind: "method",
|
||||
name: "caller",
|
||||
full_name: "fixture.Other.caller",
|
||||
containing_class: "fixture.Other",
|
||||
top_class: "fixture.Other",
|
||||
def_pos: 42,
|
||||
},
|
||||
],
|
||||
}),
|
||||
);
|
||||
await server.connect(new StdioServerTransport());
|
||||
Vendored
+90
@@ -0,0 +1,90 @@
|
||||
import { access, rm, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { expect, onTestFinished } from "vitest";
|
||||
import { AndroidAnalysisService } from "../../../src/application/AndroidAnalysisService.js";
|
||||
import { JadxProvider } from "../../../src/android/JadxProvider.js";
|
||||
import {
|
||||
ProviderProcessSupervisor,
|
||||
spawnOwnedProviderProcess,
|
||||
} from "../../../src/process/ProviderProcess.js";
|
||||
import { cleanupOwnedProcessGroup } from "../../../src/process/ProcessOwnership.js";
|
||||
import { writeOrderedZip } from "../artifactEntryOrder.js";
|
||||
import { createTestTempDirectory } from "../temporaryDirectory.js";
|
||||
|
||||
const fixture = fileURLToPath(new URL("./jadx-mcp.mjs", import.meta.url));
|
||||
|
||||
/** Real owned subprocess running a synthetic MCP producer, with no module mocks. */
|
||||
export const createJadxProtocolFixture = async (mode = "normal") => {
|
||||
const root = await createTestTempDirectory("rea-android-boundary-");
|
||||
const apk = join(root, "fixture.apk");
|
||||
const jar = join(root, "fixture.jar");
|
||||
await writeOrderedZip(apk, ["AndroidManifest.xml", "classes.dex"]);
|
||||
await writeFile(jar, "synthetic engine bytes; not a Java archive");
|
||||
const launches: {
|
||||
runId: string;
|
||||
cwd: string | undefined;
|
||||
pid: number | undefined;
|
||||
}[] = [];
|
||||
const provider = new JadxProvider(
|
||||
{ ...process.env, REA_JADX_MCP_JAR: jar },
|
||||
async (options) => {
|
||||
const spawned = await spawnOwnedProviderProcess({
|
||||
...options,
|
||||
command: process.execPath,
|
||||
arguments: [fixture],
|
||||
expectedCommand: process.execPath,
|
||||
env: { ...options.env, REA_FAKE_JADX_MODE: mode },
|
||||
});
|
||||
launches.push({
|
||||
runId: options.runId,
|
||||
cwd: options.cwd,
|
||||
pid: spawned.process.pid,
|
||||
});
|
||||
if (mode === "cleanup-failure") {
|
||||
onTestFinished(async () => {
|
||||
const supervisor = new ProviderProcessSupervisor({
|
||||
...spawned,
|
||||
ownsProcessLifetime: true,
|
||||
cleanup: () => cleanupOwnedProcessGroup(spawned.ownership),
|
||||
});
|
||||
expect((await supervisor.stop()).status).not.toBe("incomplete");
|
||||
if (options.cwd !== undefined)
|
||||
await rm(options.cwd, { recursive: true, force: true });
|
||||
});
|
||||
return {
|
||||
...spawned,
|
||||
cleanup: async () => ({
|
||||
cleaned: false,
|
||||
reason: "injected ownership verification failure",
|
||||
}),
|
||||
};
|
||||
}
|
||||
return spawned;
|
||||
},
|
||||
);
|
||||
return {
|
||||
service: new AndroidAnalysisService(provider),
|
||||
provider,
|
||||
apk,
|
||||
jar,
|
||||
launches,
|
||||
};
|
||||
};
|
||||
|
||||
/** Verify only this fixture's acquired group and workspace, without scanning unrelated host environments. */
|
||||
export const verifyJadxFixtureCleanup = async (
|
||||
launches: Awaited<ReturnType<typeof createJadxProtocolFixture>>["launches"],
|
||||
): Promise<void> => {
|
||||
for (const launch of launches) {
|
||||
const pid = launch.pid;
|
||||
if (pid !== undefined)
|
||||
expect(() => process.kill(-pid, 0)).toThrowError(
|
||||
expect.objectContaining({ code: "ESRCH" }),
|
||||
);
|
||||
if (launch.cwd !== undefined)
|
||||
await expect(access(launch.cwd)).rejects.toMatchObject({
|
||||
code: "ENOENT",
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user