feat(apktool): decode Android resource tables through a caller-supplied launcher (#1556)

* feat(apktool): decode Android resource tables through a caller-supplied launcher

Add an Apktool family with two operations over the caller's apktool
launcher (REA_APKTOOL_COMMAND, absolute, or apktool on PATH):

- inspect_apktool_client: resolved launcher, selection source, and
  version; decodes nothing.
- decode_android_resources: apktool d --no-src in a provider-owned
  workspace that is removed before the result returns. Projects the
  decoded AndroidManifest.xml (bounded at 512 KiB), apktool.yml version
  and SDK facts exactly as printed, string resources verbatim (default or
  one locale, bounded at 2000 entries, nested-markup entries counted as
  unparsed), available string locales, and the decoded file count with a
  hard cap. The target APK is digested with SHA-256 before decoding so
  Evidence binds the observation to the exact input.

Smali production is skipped by design: Java-source recovery belongs to
the JADX family. Workspace cleanup happens before results return; an
unremovable workspace is named in the limitations instead of abandoned.

Verified against apktool 2.7.0-dirty (Debian packaging) on Linux with
OpenJDK 25 against a signed aapt2-built probe APK with two resource
locales: launcher version, on-disk digest agreement, metadata projection,
manifest package agreement, and both string tables (default and de).
Parser goldens come from the same decode. READMEs carry the new
'Android resources' row in every supported language.

* fix(apktool): own snapshots and bound decoded resource reads

* docs(apktool): describe owned resources and bounded projection

---------

Co-authored-by: REA Contributor <contributor@rea.tools>
Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
Quentin Limo
2026-10-10 23:04:12 +08:00
committed by GitHub
co-authored by REA Contributor morluto
parent 749e58772a
commit 96fd98cce0
47 changed files with 2208 additions and 2 deletions
@@ -0,0 +1,23 @@
import type {
AnalysisExecution,
ExecutionOptions,
ProviderAvailability,
} from "../AnalysisProvider.js";
import type { ApktoolRequest } from "../../domain/apktool/apktoolResourceAnalysis.js";
import type { AnalysisError } from "../../domain/analysisErrorBase.js";
import type { Result } from "../../domain/result.js";
/**
* Apktool resource-decoding boundary over a caller-selected launcher.
* Implementation details belong to the provider adapter.
*/
export interface ApktoolResourceAnalysisPort {
/** Probe the selected apktool launcher without decoding anything. */
inspectAvailability(signal?: AbortSignal): Promise<ProviderAvailability>;
/** The provider owns only per-call workspaces; safe to call always. */
close(): Promise<void>;
execute(
request: ApktoolRequest,
options?: ExecutionOptions,
): Promise<Result<AnalysisExecution, AnalysisError>>;
}
@@ -0,0 +1,85 @@
import { expect, it } from "vitest";
import { ApktoolResourceAnalysisService } from "./ApktoolResourceAnalysisService.js";
import type { ApktoolResourceAnalysisPort } from "./ApktoolResourceAnalysisPort.js";
import { createAnalysisExecution } from "../AnalysisProvider.js";
import { AnalysisCancelledError } from "../../domain/analysisErrorCore.js";
import { parseEvidence } from "../../domain/evidence.js";
import { ok } from "../../domain/result.js";
const identity = { id: "apktool", name: "Apktool", version: null };
const execution = createAnalysisExecution(
{
client: {
command: "/usr/bin/apktool",
command_source: "path",
apktool_version: "2.7.0-dirty",
},
},
identity,
);
const refusedPort: ApktoolResourceAnalysisPort = {
async inspectAvailability() {
throw new Error("must not probe");
},
async close() {},
async execute() {
throw new Error("must not execute");
},
};
it.each([
[{ path: "/targets/app.apk", include_strings: "yes" }],
[{ path: "/targets/app.apk", locale: "not a locale" }],
[{ path: "/targets/app.apk", extra: true }],
])("rejects invalid decode selection before any effect: %j", async (input) => {
const service = new ApktoolResourceAnalysisService(refusedPort);
expect(
await service.execute("decode_android_resources", input),
).toMatchObject({ ok: false, error: { _tag: "AnalysisInputError" } });
});
it("composes observed Evidence with the caller parameters inline", async () => {
const port: ApktoolResourceAnalysisPort = {
async inspectAvailability() {
return { status: "available", code: null, reason: null, diagnostics: {} };
},
async close() {},
async execute(request) {
expect(request).toEqual({
operation: "decode_android_resources",
input: { path: "/targets/app.apk", include_strings: true },
});
return ok(execution);
},
};
const result = await new ApktoolResourceAnalysisService(port).execute(
"decode_android_resources",
{ path: "/targets/app.apk", include_strings: true },
);
if (!result.ok) throw new Error(result.error.message);
const evidence = parseEvidence(result.value);
expect(evidence.provider).toEqual(identity);
expect(evidence.operation).toBe("decode_android_resources");
expect(evidence.parameters).toEqual({
path: "/targets/app.apk",
include_strings: true,
});
expect(evidence.confidence).toBe("observed");
});
it("preserves cancellation before execution", async () => {
const controller = new AbortController();
controller.abort();
const service = new ApktoolResourceAnalysisService(refusedPort);
const result = await service.execute(
"decode_android_resources",
{ path: "/targets/app.apk", include_strings: true },
{ signal: controller.signal },
);
expect(result.ok).toBe(false);
if (result.ok) return;
expect(result.error).toBeInstanceOf(AnalysisCancelledError);
});
@@ -0,0 +1,46 @@
import type { ApktoolResourceAnalysisPort } from "./ApktoolResourceAnalysisPort.js";
import type { ExecutionOptions } from "../AnalysisProvider.js";
import {
apktoolRequestSchema,
type ApktoolOperation,
} from "../../domain/apktool/apktoolResourceAnalysis.js";
import {
AnalysisCancelledError,
AnalysisInputError,
} from "../../domain/analysisErrorCore.js";
import type { AnalysisError } from "../../domain/analysisErrorBase.js";
import { createEvidence, type Evidence } from "../../domain/evidence.js";
import { jsonObjectSchema } from "../../domain/jsonValue.js";
import { err, ok, type Result } from "../../domain/result.js";
/** Shared CLI/MCP admission, execution, and Evidence composition for Apktool. */
export class ApktoolResourceAnalysisService {
constructor(readonly provider: ApktoolResourceAnalysisPort) {}
/** Run one caller-selected Apktool request and retain target identity inline. */
async execute(
operation: ApktoolOperation,
input: unknown,
options?: ExecutionOptions,
): Promise<Result<Evidence, AnalysisError>> {
if (options?.signal?.aborted === true)
return err(new AnalysisCancelledError(operation));
const request = apktoolRequestSchema.safeParse({ operation, input });
if (!request.success)
return err(new AnalysisInputError(operation, { cause: request.error }));
const executed = await this.provider.execute(request.data, options);
if (!executed.ok) return executed;
const execution = executed.value;
return ok(
createEvidence(execution.subject ?? undefined, execution.provider, {
operation,
parameters: jsonObjectSchema.parse(request.data.input),
result: execution.result,
rawResult: execution.rawResult,
limitations: execution.limitations,
locations: execution.locations,
confidence: "observed",
}),
);
}
}