Files
rea/src/domain/apktool/apktoolResourceAnalysis.ts
T
96fd98cce0 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>
2026-10-10 23:04:12 +08:00

104 lines
3.2 KiB
TypeScript

import { z } from "zod";
import { localPathStringSchema } from "../localPath.js";
import { digestSchema } from "../digests.js";
/** Upper bound for one decoded resource projection in one response. */
const MAX_MANIFEST_BYTES = 512 * 1024;
const MAX_STRINGS = 2_000;
/**
* Caller intent for Apktool-backed resource decoding. Apktool is
* bring-your-own: REA never installs it, and decoding happens in a
* provider-owned workspace that is removed after the facts are projected.
*/
export const apktoolInputSchemas = {
inspect_apktool_client: z.strictObject({}),
decode_android_resources: z.strictObject({
path: localPathStringSchema.describe("Local APK file to decode"),
include_strings: z
.boolean()
.default(true)
.describe(
`Project res/values/strings.xml entries (bounded to ${String(MAX_STRINGS)} entries)`,
),
locale: z
.string()
.min(2)
.max(16)
.regex(
/^(?:[a-z]{2,3}(?:-(?:r?[A-Z]{2}|r?[0-9]{3}))?|b\+[a-z]{2,3}(?:\+[A-Za-z0-9]{2,8})*)$/u,
"Locale must look like de, pt-BR, pt-rBR, or b+zh+Hans+CN",
)
.optional()
.describe(
"Project strings from res/values-<locale>/strings.xml instead of the default table",
),
}),
} as const;
/** Supported Apktool-backed operations. */
export type ApktoolOperation = keyof typeof apktoolInputSchemas;
/** Validated Apktool requests carry their selected operation. */
export type ApktoolRequest = {
[Name in ApktoolOperation]: {
operation: Name;
input: z.infer<(typeof apktoolInputSchemas)[Name]>;
};
}[ApktoolOperation];
/** Validate operation and input together so their types remain correlated. */
export const apktoolRequestSchema = z.discriminatedUnion("operation", [
z.strictObject({
operation: z.literal("inspect_apktool_client"),
input: apktoolInputSchemas.inspect_apktool_client,
}),
z.strictObject({
operation: z.literal("decode_android_resources"),
input: apktoolInputSchemas.decode_android_resources,
}),
]);
const client = z.strictObject({
command: z.string().min(1),
command_source: z.enum(["environment", "path"]),
apktool_version: z.string().min(1).nullable(),
});
/** Portable Apktool observations with explicit coverage limitations. */
export const apktoolResultSchemas = {
inspect_apktool_client: z.strictObject({
client,
}),
decode_android_resources: z.strictObject({
client,
target: z.strictObject({
path: localPathStringSchema,
bytes: z.number().int().nonnegative(),
sha256: digestSchema,
}),
metadata: z.strictObject({
version_name: z.string().nullable(),
version_code: z
.string()
.nullable()
.describe(
"Version code exactly as apktool.yml prints it, e.g. '45' or '45.1'",
),
min_sdk_version: z.string().nullable(),
target_sdk_version: z.string().nullable(),
package_name: z.string().nullable(),
}),
manifest: z.string().nullable(),
strings: z.array(
z.strictObject({
name: z.string().min(1),
value: z.string(),
}),
),
locale: z.string().nullable(),
locales: z.array(z.string().min(1)),
decoded_file_count: z.number().int().nonnegative(),
coverage: z.enum(["complete", "partial"]),
}),
} as const;