fix(android): preserve decoded values and duplicate class inventories
Parse Apktool YAML and XML at the producer boundary, preserving scalar spelling, XML text and partial string coverage. Retain repeated JADX inventory rows with explicit duplicate-name counts and lookup ambiguity. Make the Apktool verifier report cleanup failures. Validation: parser, provider and JADX fixture boundary tests; typecheck, lint and documentation checks. Real Apktool and JADX engines were not exercised. Refs #1978
This commit is contained in:
@@ -1,11 +1,17 @@
|
||||
/**
|
||||
* Parsers for the decoded workspace `apktool d --no-src` produces.
|
||||
*
|
||||
* Apktool's apktool.yml is a small YAML document; the facts REA projects
|
||||
* are flat enough that line-oriented extraction stays honest without a
|
||||
* YAML dependency, and unrecognized content is ignored rather than guessed.
|
||||
*/
|
||||
import {
|
||||
DOMParser,
|
||||
type Document,
|
||||
type Element,
|
||||
type Node,
|
||||
} from "@xmldom/xmldom";
|
||||
import {
|
||||
isMap,
|
||||
isScalar,
|
||||
parseDocument,
|
||||
type Document as YamlDocument,
|
||||
} from "yaml";
|
||||
|
||||
/** Project only the Apktool metadata fields REA publishes. */
|
||||
export interface ApktoolMetadata {
|
||||
readonly versionName: string | null;
|
||||
readonly versionCode: string | null;
|
||||
@@ -13,43 +19,48 @@ export interface ApktoolMetadata {
|
||||
readonly targetSdkVersion: string | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Project version and SDK facts from apktool.yml. Values keep apktool's
|
||||
* own spelling (version codes can print as `45` or `45.1`).
|
||||
*/
|
||||
export const parseApktoolYml = (yml: string): ApktoolMetadata => {
|
||||
const metadata: {
|
||||
versionName: string | null;
|
||||
versionCode: string | null;
|
||||
minSdkVersion: string | null;
|
||||
targetSdkVersion: string | null;
|
||||
} = {
|
||||
versionName: null,
|
||||
versionCode: null,
|
||||
minSdkVersion: null,
|
||||
targetSdkVersion: null,
|
||||
};
|
||||
for (const line of yml.split(/\r?\n/u)) {
|
||||
const versionName = /^\s*versionName:\s*(?:'([^']*)'|(.*))$/u.exec(line);
|
||||
if (versionName !== null && metadata.versionName === null)
|
||||
metadata.versionName = (versionName[1] ?? versionName[2] ?? "").trim();
|
||||
const versionCode = /^\s*versionCode:\s*(?:'([^']*)'|(.*))$/u.exec(line);
|
||||
if (versionCode !== null && metadata.versionCode === null)
|
||||
metadata.versionCode = (versionCode[1] ?? versionCode[2] ?? "").trim();
|
||||
const minSdk = /minSdkVersion:\s*'?([0-9]+)'?/u.exec(line);
|
||||
if (minSdk !== null && metadata.minSdkVersion === null)
|
||||
metadata.minSdkVersion = minSdk[1]!;
|
||||
const targetSdk = /targetSdkVersion:\s*'?([0-9]+)'?/u.exec(line);
|
||||
if (targetSdk !== null && metadata.targetSdkVersion === null)
|
||||
metadata.targetSdkVersion = targetSdk[1]!;
|
||||
}
|
||||
return metadata;
|
||||
const metaInfoTag = {
|
||||
tag: "tag:yaml.org,2002:brut.androlib.meta.MetaInfo",
|
||||
collection: "map" as const,
|
||||
resolve: (map: unknown) => map,
|
||||
};
|
||||
|
||||
/** Read the package attribute from decoded AndroidManifest.xml text. */
|
||||
/**
|
||||
* Read values from Apktool's tagged metadata document. YAML's core schema
|
||||
* distinguishes null from quoted strings; numeric values use their source
|
||||
* range so version codes retain the spelling Apktool emitted.
|
||||
*/
|
||||
export const parseApktoolYml = (yml: string): ApktoolMetadata => {
|
||||
const document = parseDocument(yml, {
|
||||
schema: "core",
|
||||
customTags: [metaInfoTag],
|
||||
strict: true,
|
||||
uniqueKeys: true,
|
||||
});
|
||||
if (document.errors.length > 0)
|
||||
throw new TypeError(
|
||||
`apktool.yml is not valid Apktool metadata: ${document.errors[0]?.message ?? "YAML parse error"}`,
|
||||
);
|
||||
if (!isMap(document.contents))
|
||||
throw new TypeError("apktool.yml root must be an Apktool metadata mapping");
|
||||
return {
|
||||
versionName: yamlScalar(document, ["versionInfo", "versionName"], yml),
|
||||
versionCode: yamlScalar(document, ["versionInfo", "versionCode"], yml),
|
||||
minSdkVersion: yamlScalar(document, ["sdkInfo", "minSdkVersion"], yml),
|
||||
targetSdkVersion: yamlScalar(
|
||||
document,
|
||||
["sdkInfo", "targetSdkVersion"],
|
||||
yml,
|
||||
),
|
||||
};
|
||||
};
|
||||
|
||||
/** Read the package attribute from the decoded Android manifest root. */
|
||||
export const parseManifestPackage = (manifest: string): string | null => {
|
||||
const match = /<manifest[^>]*\spackage="([^"]+)"/u.exec(manifest);
|
||||
return match === null ? null : match[1]!;
|
||||
const document = parseXml(manifest, "AndroidManifest.xml");
|
||||
const root = document.documentElement;
|
||||
if (root === null || root.nodeName !== "manifest") return null;
|
||||
return root.getAttribute("package") || null;
|
||||
};
|
||||
|
||||
export interface DecodedStringEntry {
|
||||
@@ -58,10 +69,10 @@ export interface DecodedStringEntry {
|
||||
}
|
||||
|
||||
/**
|
||||
* Project `<string name="...">value</string>` entries from one decoded
|
||||
* strings.xml. Values are preserved exactly as apktool printed them —
|
||||
* escaped entities stay escaped — and entries containing nested markup
|
||||
* are counted as unparsed rather than flattened.
|
||||
* Project direct `<string>` children from decoded resources XML. DOM parsing
|
||||
* identifies elements and attributes; text values are sliced from the input
|
||||
* at parser-reported locations so entity spelling and whitespace are kept.
|
||||
* Markup-bearing string values are omitted and counted as partial evidence.
|
||||
*/
|
||||
export const parseDecodedStrings = (
|
||||
stringsXml: string,
|
||||
@@ -69,20 +80,107 @@ export const parseDecodedStrings = (
|
||||
entries: readonly DecodedStringEntry[];
|
||||
unparsedCount: number;
|
||||
} => {
|
||||
let document: Document;
|
||||
try {
|
||||
document = parseXml(stringsXml, "strings.xml");
|
||||
} catch {
|
||||
return { entries: [], unparsedCount: 1 };
|
||||
}
|
||||
const root = document.documentElement;
|
||||
if (root === null || root.nodeName !== "resources")
|
||||
return { entries: [], unparsedCount: 1 };
|
||||
|
||||
const entries: DecodedStringEntry[] = [];
|
||||
let unparsedCount = 0;
|
||||
const pattern = /<string\s+name="([^"]+)"[^>]*>([\s\S]*?)<\/string>/gu;
|
||||
for (
|
||||
let match = pattern.exec(stringsXml);
|
||||
match !== null;
|
||||
match = pattern.exec(stringsXml)
|
||||
) {
|
||||
const value = match[2]!;
|
||||
if (value.includes("<")) {
|
||||
const offsets = sourceLineOffsets(stringsXml);
|
||||
for (const child of childNodes(root)) {
|
||||
if (child.nodeType !== 1) continue;
|
||||
const element = child as Element;
|
||||
if (element.nodeName !== "string") continue;
|
||||
const name = element.getAttribute("name");
|
||||
if (name === null || name.length === 0) {
|
||||
unparsedCount += 1;
|
||||
continue;
|
||||
}
|
||||
entries.push({ name: match[1]!, value: value.trim() });
|
||||
const content = childNodes(element);
|
||||
if (content.some((node) => node.nodeType !== 3 && node.nodeType !== 8)) {
|
||||
unparsedCount += 1;
|
||||
continue;
|
||||
}
|
||||
if (content.every((node) => node.nodeType === 8)) {
|
||||
entries.push({ name, value: "" });
|
||||
continue;
|
||||
}
|
||||
const value = content
|
||||
.filter((node) => node.nodeType === 3)
|
||||
.map((node) => {
|
||||
const start = sourceOffset(node, offsets);
|
||||
if (start === undefined) return null;
|
||||
// A literal '<' cannot appear in well-formed XML text. Ending here
|
||||
// also excludes comments after the document element from this value.
|
||||
const end = stringsXml.indexOf("<", start);
|
||||
if (end < start) return null;
|
||||
return stringsXml.slice(start, end);
|
||||
});
|
||||
if (value.some((part) => part === null)) {
|
||||
unparsedCount += 1;
|
||||
continue;
|
||||
}
|
||||
entries.push({ name, value: value.join("") });
|
||||
}
|
||||
return { entries, unparsedCount };
|
||||
};
|
||||
|
||||
const yamlScalar = (
|
||||
document: YamlDocument,
|
||||
path: readonly string[],
|
||||
source: string,
|
||||
): string | null => {
|
||||
const node = document.getIn(path, true);
|
||||
if (!isScalar(node) || node.value === null) return null;
|
||||
if (typeof node.value === "string") return node.value;
|
||||
const range = node.range;
|
||||
return range === null || range === undefined
|
||||
? String(node.value)
|
||||
: source.slice(range[0], range[1]);
|
||||
};
|
||||
|
||||
const parseXml = (xml: string, label: string) => {
|
||||
const errors: string[] = [];
|
||||
const document = new DOMParser({
|
||||
normalizeLineEndings: (source) => source,
|
||||
onError: (_level, message) => errors.push(message),
|
||||
}).parseFromString(xml, "application/xml");
|
||||
if (errors.length > 0 || document.documentElement === null)
|
||||
throw new TypeError(
|
||||
`${label} is not well-formed XML${errors[0] === undefined ? "" : `: ${errors[0]}`}`,
|
||||
);
|
||||
return document;
|
||||
};
|
||||
|
||||
const childNodes = (node: Node): Node[] => {
|
||||
const children: Node[] = [];
|
||||
for (let child = node.firstChild; child !== null; child = child.nextSibling)
|
||||
children.push(child);
|
||||
return children;
|
||||
};
|
||||
|
||||
const sourceLineOffsets = (source: string): readonly number[] => {
|
||||
const offsets = [0];
|
||||
for (const match of source.matchAll(/\r\n?|\n/gu)) {
|
||||
const index = match.index;
|
||||
offsets.push(index + match[0].length);
|
||||
}
|
||||
return offsets;
|
||||
};
|
||||
|
||||
const sourceOffset = (
|
||||
node: Node,
|
||||
lineOffsets: readonly number[],
|
||||
): number | undefined => {
|
||||
const line = node.lineNumber;
|
||||
const column = node.columnNumber;
|
||||
if (line === undefined || column === undefined) return undefined;
|
||||
const lineStart = lineOffsets[line - 1];
|
||||
return lineStart === undefined ? undefined : lineStart + column - 1;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user