Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

fix(apktool): preserve valid replacement characters in decoded XML (#4697)

Sua Choi326c073880c0
diff --git a/src/apktool/ApktoolDecodeOutput.test.ts b/src/apktool/ApktoolDecodeOutput.test.ts
index 6ee69ec18..94ff2ae81 100644
--- a/src/apktool/ApktoolDecodeOutput.test.ts
+++ b/src/apktool/ApktoolDecodeOutput.test.ts
@@ -127,6 +127,22 @@ describe("parseManifestPackage", () => {
expect(parseManifestPackage("<manifest>\n</manifest>\n")).toBeNull();
});
+ it("reads the package when unrelated manifest text contains U+FFFD", () => {
+ expect(
+ parseManifestPackage(
+ '<manifest package="org.rea.app"><application label="\uFFFD"/></manifest>',
+ ),
+ ).toBe("org.rea.app");
+ });
+
+ it("still rejects malformed attributes alongside U+FFFD", () => {
+ expect(() =>
+ parseManifestPackage(
+ '<manifest package=org.rea.app><application label="\uFFFD"/></manifest>',
+ ),
+ ).toThrow(/AndroidManifest.xml is not well-formed XML/u);
+ });
+
it("rejects a well-formed document with the wrong root element", () => {
expect(() => parseManifestPackage("<resources/>")).toThrowError(
new TypeError("AndroidManifest.xml root must be <manifest>"),
@@ -172,6 +188,30 @@ describe("parseDecodedStrings", () => {
);
});
+ it("preserves literal U+FFFD and unaffected sibling strings", () => {
+ expect(
+ parseDecodedStrings(
+ '<resources><string name="good">Hello</string><string name="edge">\uFFFD</string></resources>',
+ ),
+ ).toEqual({
+ entries: [
+ { name: "good", value: "Hello" },
+ { name: "edge", value: "\uFFFD" },
+ ],
+ unparsedCount: 0,
+ });
+ });
+
+ it.each([
+ "<resources><string name=unquoted>\uFFFD</string></resources>",
+ '<resources><string name="broken">\uFFFD</resources>',
+ ])("still rejects malformed XML containing U+FFFD: %s", (xml) => {
+ expect(parseDecodedStrings(xml)).toEqual({
+ entries: [],
+ unparsedCount: 1,
+ });
+ });
+
it("keeps empty, self-closing, and reordered string attributes", () => {
expect(
parseDecodedStrings(
diff --git a/src/apktool/ApktoolDecodeOutput.ts b/src/apktool/ApktoolDecodeOutput.ts
index 05c5f8af1..f545935a2 100644
--- a/src/apktool/ApktoolDecodeOutput.ts
+++ b/src/apktool/ApktoolDecodeOutput.ts
@@ -150,7 +150,16 @@ const parseXml = (xml: string, label: string) => {
const errors: string[] = [];
const document = new DOMParser({
normalizeLineEndings: (source) => source,
- onError: (_level, message) => errors.push(message),
+ onError: (level, message) => {
+ // U+FFFD is a legal XML character. The provider's fatal UTF-8 decoder
+ // already rejects invalid bytes; this warning alone is not malformed XML.
+ if (
+ level === "warning" &&
+ message.startsWith("Unicode replacement character detected")
+ )
+ return;
+ errors.push(message);
+ },
}).parseFromString(xml, "application/xml");
if (errors.length > 0 || document.documentElement === null)
throw new TypeError(
diff --git a/src/apktool/ApktoolProvider.test.ts b/src/apktool/ApktoolProvider.test.ts
index 2796c933b..386d96ad1 100644
--- a/src/apktool/ApktoolProvider.test.ts
+++ b/src/apktool/ApktoolProvider.test.ts
@@ -263,6 +263,59 @@ it("preserves empty resource strings and reports markup omissions as partial", a
expect(result.coverage).toBe("partial");
});
+it("preserves literal U+FFFD in decoded XML with complete coverage", async () => {
+ const stringsXml =
+ '<resources><string name="good">Hello</string><string name="edge">\uFFFD</string></resources>';
+ const { command, root } = await writeApktoolStub((root) =>
+ decodeStub({
+ stringsXml,
+ manifestFile: join(root, "AndroidManifest.xml"),
+ })(root),
+ );
+ const manifest = REAL_MANIFEST.replace("@string/app_name", "\uFFFD");
+ await writeFile(join(root, "AndroidManifest.xml"), manifest, "utf8");
+ const apk = await apkFixture(root);
+ const result = await evidenceResult(providerOver(command), {
+ operation: "decode_android_resources",
+ input: { path: apk.path, include_strings: true },
+ });
+ expect(result).toMatchObject({
+ manifest,
+ metadata: { package_name: "com.rea.apktool.probe" },
+ strings: [
+ { name: "good", value: "Hello" },
+ { name: "edge", value: "\uFFFD" },
+ ],
+ coverage: "complete",
+ });
+});
+
+it("still refuses invalid UTF-8 bytes in decoded strings", async () => {
+ const { command, root } = await writeApktoolStub((root) =>
+ decodeStub({ stringsFile: join(root, "strings.xml") })(root),
+ );
+ await writeFile(
+ join(root, "strings.xml"),
+ Buffer.concat([
+ Buffer.from('<resources><string name="bad">'),
+ Buffer.from([0xff]),
+ Buffer.from("</string></resources>"),
+ ]),
+ );
+ const apk = await apkFixture(root);
+ const result = await providerOver(command).execute({
+ operation: "decode_android_resources",
+ input: { path: apk.path, include_strings: true },
+ });
+ expect(result.ok).toBe(false);
+ if (result.ok) throw new Error("expected a refusal");
+ expect(projectAnalysisError(result.error)).toMatchObject({
+ code: "unreadable_output",
+ });
+ expect(result.error.message).toContain("invalid UTF-8");
+ expect(result.error.message).toContain("strings.xml");
+});
+
it("ignores Android resource qualifiers when discovering locales", async () => {
const { command, root } = await writeApktoolStub(
decodeStub({ extraValuesDirectories: ["car", "night", "v21"] }),