Files
reverse-engineering-anything/src/cli/apktoolCommands.ts
T
Phonandmorluto 2fe0c9eb6a fix(cli): align local paths and SQLite selection validation (#9882)
fix(cli): batch of small fixes for #9714, #9718, #9759, #9856 and #9809

#9714: replace bare z.string().min(1) local paths in ADB CLI commands with localPathStringSchema so NUL bytes are rejected at the CLI boundary.
#9718: decode-android-resources path uses localPathStringSchema, matching apktoolInputSchemas.
#9759: inspect-sqlite-database CLI options now reject an empty column array and reject rowLimit/rowOffset/column/schemaScope=selected_table without --table via superRefine, mirroring inspectSqliteDatabaseInputSchema.
#9856 and #9809: shared snapshotSelectionOption is now localPathStringSchema.optional(), so every analyze/inspect/decompile/xrefs/trace/function/instructions/search --snapshot rejects NUL bytes.

Tests: tests/boundary/cli/overnightBatch1CliSchemas.test.ts (15 tests) proves empty/NUL input returns VALIDATION_ERROR before any provider runs and valid input is admitted.

Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
2026-11-11 23:40:01 +08:00

147 lines
5.7 KiB
TypeScript

import { projectCliAnalysisResult } from "./projectCliAnalysisResult.js";
import { z } from "incur";
import { ApktoolResourceAnalysisService } from "../application/apktool/ApktoolResourceAnalysisService.js";
import type { ApktoolResourceAnalysisPort } from "../application/apktool/ApktoolResourceAnalysisPort.js";
import { createApktoolResourceAnalysisProvider } from "../composition/apktool.js";
import { CLI_COMMANDS } from "../cliCommandNames.js";
import { logCliCommand } from "../cliLogging.js";
import {
apktoolInputSchemas,
type ApktoolOperation,
} from "../domain/apktool/apktoolResourceAnalysis.js";
import { localPathStringSchema } from "../domain/localPath.js";
import type { Logger } from "pino";
import { cliDecimalField } from "./options.js";
import type { CliInstance } from "./types.js";
import { withCommandCancellation } from "./commandCancellation.js";
import { finishAnalysisCommand } from "./finishAnalysisCommand.js";
import { bindCliEvidenceCallerPath } from "./cliEvidenceCallerPath.js";
/** Expose the same explicit Apktool requests through one-shot CLI commands. */
export const registerApktoolCommands = (
cli: CliInstance,
logger: Logger,
environment: Readonly<Record<string, string | undefined>>,
provider: ApktoolResourceAnalysisPort = createApktoolResourceAnalysisProvider(
environment,
),
): void => {
const service = new ApktoolResourceAnalysisService(provider);
const execute = (
name: string,
operation: ApktoolOperation,
input: unknown,
callerPath?: string,
) =>
withCommandCancellation((signal) =>
logCliCommand(logger, name, () =>
finishAnalysisCommand(
callerPath === undefined
? () => service.execute(operation, input, { signal })
: bindCliEvidenceCallerPath(callerPath, () =>
service.execute(operation, input, { signal }),
),
{ close: () => provider.close() },
"apktool",
operation,
signal,
).then(projectCliAnalysisResult),
),
);
cli.command(CLI_COMMANDS.inspectApktoolClient, {
description:
"Inspect the caller-selected apktool launcher without decoding anything",
args: z.object({}),
run: () =>
execute(CLI_COMMANDS.inspectApktoolClient, "inspect_apktool_client", {}),
});
cli.command(CLI_COMMANDS.decodeAndroidResources, {
description:
"Decode one APK's manifest, metadata, and string resources with apktool",
args: z.object({
path: localPathStringSchema.describe("Local APK file to decode"),
}),
// The locale grammar and the string-projection default belong to the
// apktool input owner. Read them from that schema rather than restating a
// narrower locale pattern, which refused the owner's own advertised
// `b+zh+Hans+CN` form at the CLI boundary.
options: z.object({
locale: apktoolInputSchemas.decode_android_resources.shape.locale,
"string-tables-only":
apktoolInputSchemas.decode_android_resources.shape.string_tables_only,
locales:
apktoolInputSchemas.decode_android_resources.shape.locales.describe(
"Repeat to project multiple localized string tables, e.g. --locales de --locales pt-BR",
),
decodeTimeoutMs: cliDecimalField(
apktoolInputSchemas.decode_android_resources.shape.decode_timeout_ms,
),
stringOffset: cliDecimalField(
apktoolInputSchemas.decode_android_resources.shape.string_offset,
),
stringLimit: cliDecimalField(
apktoolInputSchemas.decode_android_resources.shape.string_limit,
),
maxTotalStrings: cliDecimalField(
apktoolInputSchemas.decode_android_resources.shape.max_total_strings,
),
maxStringTableBytes: cliDecimalField(
apktoolInputSchemas.decode_android_resources.shape
.max_string_table_bytes,
),
maxTotalStringTableBytes: cliDecimalField(
apktoolInputSchemas.decode_android_resources.shape
.max_total_string_table_bytes,
),
strings: z
.boolean()
.default(
apktoolInputSchemas.decode_android_resources.shape.include_strings.parse(
undefined,
),
)
.describe(
"Include string resources; use --no-strings to skip projecting them",
),
}),
run: ({ args, options }) =>
execute(
CLI_COMMANDS.decodeAndroidResources,
"decode_android_resources",
{
path: args.path,
include_strings: options.strings,
...(options["string-tables-only"] === undefined
? {}
: { string_tables_only: options["string-tables-only"] }),
...(options.locale === undefined ? {} : { locale: options.locale }),
...(options.locales === undefined
? {}
: { locales: options.locales }),
...(options.decodeTimeoutMs === undefined
? {}
: { decode_timeout_ms: options.decodeTimeoutMs }),
...(options.stringOffset === undefined
? {}
: { string_offset: options.stringOffset }),
...(options.stringLimit === undefined
? {}
: { string_limit: options.stringLimit }),
...(options.maxTotalStrings === undefined
? {}
: { max_total_strings: options.maxTotalStrings }),
...(options.maxStringTableBytes === undefined
? {}
: { max_string_table_bytes: options.maxStringTableBytes }),
...(options.maxTotalStringTableBytes === undefined
? {}
: {
max_total_string_table_bytes: options.maxTotalStringTableBytes,
}),
},
args.path,
),
});
};