From 61af8abeb6f8b95fe99e2470e6cb57ab974f5bca Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Thu, 19 Nov 2026 11:07:25 +0800 Subject: [PATCH] fix(android): share decoded Android XML admission (#12158) * fix(android): share decoded manifest root and package admission Refs #12139 * fix(apktool): keep foreign XML out of decoded string entries Refs #12139 --- .../AndroidManifestComponentDetails.ts | 8 ++-- src/android/AndroidManifestComponents.ts | 33 +++++-------- src/android/AndroidManifestXml.ts | 11 ----- src/apktool/ApktoolDecodeOutput.test.ts | 47 +++++++++++-------- src/apktool/ApktoolDecodeOutput.ts | 34 ++++++-------- src/domain/android/decodedAndroidManifest.ts | 27 +++++++++++ src/domain/xmlDocument.ts | 14 +++++- .../android/androidManifestComponents.test.ts | 34 +++++++++----- 8 files changed, 120 insertions(+), 88 deletions(-) create mode 100644 src/domain/android/decodedAndroidManifest.ts diff --git a/src/android/AndroidManifestComponentDetails.ts b/src/android/AndroidManifestComponentDetails.ts index 3f5ecdbf5d..0e6de39369 100644 --- a/src/android/AndroidManifestComponentDetails.ts +++ b/src/android/AndroidManifestComponentDetails.ts @@ -6,11 +6,11 @@ import type { androidIntentFilterSchema, androidComponentMetadataSchema, } from "../domain/android/androidManifestComponents.js"; +import { isUnqualifiedXmlElement } from "../domain/xmlDocument.js"; import { androidAttribute, createManifestLocationReader, isAndroidAttribute, - isManifestElement, } from "./AndroidManifestXml.js"; type Filter = z.output; @@ -37,7 +37,7 @@ const declaresAction = (filter: Element): boolean => { for (let index = 0; index < filter.childNodes.length; index += 1) { const member = filter.childNodes.item(index); if ( - isManifestElement(member) && + isUnqualifiedXmlElement(member) && member.nodeName === "action" && androidAttribute(member, "name") !== null ) @@ -116,7 +116,7 @@ export const createAndroidManifestDetailsReader = ( let actionFilter = false; for (let index = 0; index < component.childNodes.length; index += 1) { const child = component.childNodes.item(index); - if (!isManifestElement(child)) continue; + if (!isUnqualifiedXmlElement(child)) continue; if (child.nodeName === "meta-data") { if (admit()) metadata.push({ @@ -141,7 +141,7 @@ export const createAndroidManifestDetailsReader = ( const priority = take(androidAttribute(child, "priority")); for (let at = 0; at < child.childNodes.length; at += 1) { const member = child.childNodes.item(at); - if (!isManifestElement(member)) continue; + if (!isUnqualifiedXmlElement(member)) continue; if (member.nodeName === "action" || member.nodeName === "category") { const value = androidAttribute(member, "name"); if (value === null) continue; diff --git a/src/android/AndroidManifestComponents.ts b/src/android/AndroidManifestComponents.ts index ca9049f00a..dfe3040909 100644 --- a/src/android/AndroidManifestComponents.ts +++ b/src/android/AndroidManifestComponents.ts @@ -1,10 +1,6 @@ -import { parseXmlDocument } from "../domain/xmlDocument.js"; -import { - androidAttribute, - plainAttribute, - childNodes, - isManifestElement, -} from "./AndroidManifestXml.js"; +import { parseDecodedAndroidManifest } from "../domain/android/decodedAndroidManifest.js"; +import { isUnqualifiedXmlElement } from "../domain/xmlDocument.js"; +import { androidAttribute, childNodes } from "./AndroidManifestXml.js"; import type { KnownCoverageStatus } from "../domain/coverage.js"; import { createAndroidManifestDetailsReader } from "./AndroidManifestComponentDetails.js"; import { @@ -160,20 +156,14 @@ export const parseAndroidManifestComponents = ( items: [], limitations: [PARTIAL_LIMITATION], }; - const parsed = parseXmlDocument(manifestText, { - doctype: "reject", - lineEndings: "preserve", - }); + const parsed = parseDecodedAndroidManifest(manifestText, "reject"); if (!parsed.ok) return unavailable( - `The decoded manifest is not well-formed XML (${parsed.error.failure.field ?? parsed.error.kind}), so its component inventory is unavailable.`, + parsed.error.kind === "unexpected-root" + ? "The decoded manifest root is not , so its component inventory is unavailable." + : `The decoded manifest is not well-formed XML (${parsed.error.failure.field ?? parsed.error.kind}), so its component inventory is unavailable.`, ); - const root = parsed.value.documentElement; - if (!isManifestElement(root) || root.nodeName !== "manifest") - return unavailable( - "The decoded manifest root is not , so its component inventory is unavailable.", - ); - const rawPackageName = plainAttribute(root, "package"); + const { root, packageName: rawPackageName } = parsed.value; const packageName = rawPackageName?.trim() || null; const packageHasInvalidWhitespace = rawPackageName !== null && rawPackageName !== rawPackageName.trim(); @@ -220,7 +210,8 @@ export const parseAndroidManifestComponents = ( let targetSdkDeclared = false; let minimumSdkDeclared = false; for (const child of children) { - if (!isManifestElement(child) || child.nodeName !== "uses-sdk") continue; + if (!isUnqualifiedXmlElement(child) || child.nodeName !== "uses-sdk") + continue; // Match the bridge's declaration owner: the last uses-sdk supplies values. targetSdkDeclared = androidAttribute(child, "targetSdkVersion") !== null; minimumSdkDeclared = androidAttribute(child, "minSdkVersion") !== null; @@ -237,7 +228,7 @@ export const parseAndroidManifestComponents = ( // Parse the effective SDK once even when hundreds of providers share it. const providerDefault = targetSdkLevel === null ? null : targetSdkLevel <= 16; for (const node of children) { - if (!isManifestElement(node)) continue; + if (!isUnqualifiedXmlElement(node)) continue; const element = node; if (element.nodeName !== "application") continue; applicationSeen = true; @@ -259,7 +250,7 @@ export const parseAndroidManifestComponents = ( ? null : Buffer.byteLength(applicationPermission); for (const child of childNodes(element)) { - if (!isManifestElement(child)) continue; + if (!isUnqualifiedXmlElement(child)) continue; const component = child; if (!isComponentKind(component.nodeName)) continue; // Every declaration counts against the bound, named or not, so an diff --git a/src/android/AndroidManifestXml.ts b/src/android/AndroidManifestXml.ts index 7255c720cb..21c3fb968d 100644 --- a/src/android/AndroidManifestXml.ts +++ b/src/android/AndroidManifestXml.ts @@ -5,17 +5,6 @@ import { XML_LINE_ENDING } from "../domain/xmlDocument.js"; const ANDROID_NAMESPACE = "http://schemas.android.com/apk/res/android"; export const childNodes = (element: Element): readonly Node[] => Array.from(element.childNodes); -export const isManifestElement = (node: Node | null): node is Element => - node !== null && - node.nodeType === 1 && - (node as Element).namespaceURI === null; -export const plainAttribute = ( - element: Element, - name: string, -): string | null => { - const value = element.getAttribute(name); - return value === "" ? null : value; -}; /** Namespace identity, never the spelling of a bound prefix, selects a fact. */ export const isAndroidAttribute = (attribute: Attr): boolean => attribute.namespaceURI === ANDROID_NAMESPACE || diff --git a/src/apktool/ApktoolDecodeOutput.test.ts b/src/apktool/ApktoolDecodeOutput.test.ts index 4bc9a3edc8..2e7ee51b87 100644 --- a/src/apktool/ApktoolDecodeOutput.test.ts +++ b/src/apktool/ApktoolDecodeOutput.test.ts @@ -323,6 +323,14 @@ describe("parseDecodedStrings", () => { ); }); + it("ignores foreign namespace string elements", () => { + const xml = + 'decoyone'; + expect(parseDecodedStrings(xml)).toEqual( + ok({ entries: [{ name: "local", value: "one" }], unparsedCount: 0 }), + ); + }); + it("preserves literal U+FFFD and unaffected sibling strings", () => { expect( parseDecodedStrings( @@ -450,23 +458,24 @@ describe("string document validity and omission counts", () => { }); }); -it.each(["", 'one'])( - "keeps a wrong document root separate from an entry count: %s", - (xml) => { - const parsed = parseDecodedStrings(xml); - expect(parsed).toMatchObject({ - ok: false, - error: { - _tag: "ParseFailureError", - kind: "malformed", - failure: { - format: "android-string-resources", - field: "documentElement", - }, +it.each([ + "", + 'one', + 'one', +])("keeps a wrong document root separate from an entry count: %s", (xml) => { + const parsed = parseDecodedStrings(xml); + expect(parsed).toMatchObject({ + ok: false, + error: { + _tag: "ParseFailureError", + kind: "malformed", + failure: { + format: "android-string-resources", + field: "documentElement", }, - }); - expect(parsed).not.toHaveProperty("value"); - if (parsed.ok) throw new Error("Expected document failure"); - expect(parsed.error).not.toHaveProperty("unparsedCount"); - }, -); + }, + }); + expect(parsed).not.toHaveProperty("value"); + if (parsed.ok) throw new Error("Expected document failure"); + expect(parsed.error).not.toHaveProperty("unparsedCount"); +}); diff --git a/src/apktool/ApktoolDecodeOutput.ts b/src/apktool/ApktoolDecodeOutput.ts index 8c9d24ff75..bf91942f06 100644 --- a/src/apktool/ApktoolDecodeOutput.ts +++ b/src/apktool/ApktoolDecodeOutput.ts @@ -1,5 +1,7 @@ -import type { Element, Node } from "@xmldom/xmldom"; +import type { Node } from "@xmldom/xmldom"; +import { parseDecodedAndroidManifest } from "../domain/android/decodedAndroidManifest.js"; import { + isUnqualifiedXmlElement, parseXmlDocument, type XmlParseFailure, } from "../domain/xmlDocument.js"; @@ -142,19 +144,14 @@ const apktool3String = (source: string): string => { /** Read the package attribute from the decoded Android manifest root. */ export const parseManifestPackage = (manifest: string): string | null => { - const parsed = parseXmlDocument(manifest, { - doctype: "allow", - lineEndings: "preserve", - }); - if (!parsed.ok) - throw new TypeError( - `AndroidManifest.xml is not well-formed XML: ${parsed.error.message}`, - { cause: parsed.error }, - ); - const root = parsed.value.documentElement; - if (root === null || root.nodeName !== "manifest") + const parsed = parseDecodedAndroidManifest(manifest, "allow"); + if (parsed.ok) return parsed.value.packageName; + if (parsed.error.kind === "unexpected-root") throw new TypeError("AndroidManifest.xml root must be "); - return root.getAttribute("package") || null; + throw new TypeError( + `AndroidManifest.xml is not well-formed XML: ${parsed.error.message}`, + { cause: parsed.error }, + ); }; export interface DecodedStringEntry { @@ -184,7 +181,7 @@ export const parseDecodedStrings = ( }); if (!parsed.ok) return err(parsed.error); const root = parsed.value.documentElement; - if (root === null || root.nodeName !== "resources") + if (!isUnqualifiedXmlElement(root) || root.localName !== "resources") return err( malformedInput( "android-string-resources", @@ -197,15 +194,14 @@ export const parseDecodedStrings = ( let unparsedCount = 0; 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 (!isUnqualifiedXmlElement(child) || child.localName !== "string") + continue; + const name = child.getAttributeNS(null, "name"); if (name === null || name.length === 0) { unparsedCount += 1; continue; } - const content = childNodes(element); + const content = childNodes(child); if (content.some((node) => node.nodeType !== 3 && node.nodeType !== 8)) { unparsedCount += 1; continue; diff --git a/src/domain/android/decodedAndroidManifest.ts b/src/domain/android/decodedAndroidManifest.ts new file mode 100644 index 0000000000..16969a8f7d --- /dev/null +++ b/src/domain/android/decodedAndroidManifest.ts @@ -0,0 +1,27 @@ +import type { Element } from "@xmldom/xmldom"; + +import { err, ok, type Result } from "../result.js"; +import { + isUnqualifiedXmlElement, + parseXmlDocument, + type XmlParseFailure, +} from "../xmlDocument.js"; + +/** Read the decoded root and its package without validating package-name syntax. */ +export const parseDecodedAndroidManifest = ( + source: string, + doctype: "allow" | "reject", +): Result< + { readonly root: Element; readonly packageName: string | null }, + XmlParseFailure | { readonly kind: "unexpected-root" } +> => { + const parsed = parseXmlDocument(source, { doctype, lineEndings: "preserve" }); + if (!parsed.ok) return parsed; + const root = parsed.value.documentElement; + if (!isUnqualifiedXmlElement(root) || root.localName !== "manifest") + return err({ kind: "unexpected-root" }); + return ok({ + root, + packageName: root.getAttributeNS(null, "package") || null, + }); +}; diff --git a/src/domain/xmlDocument.ts b/src/domain/xmlDocument.ts index 7648829a55..06b917c82d 100644 --- a/src/domain/xmlDocument.ts +++ b/src/domain/xmlDocument.ts @@ -1,4 +1,10 @@ -import { DOMParser, onErrorStopParsing, type Document } from "@xmldom/xmldom"; +import { + DOMParser, + onErrorStopParsing, + type Document, + type Element, + type Node, +} from "@xmldom/xmldom"; import { SaxesParser, type SaxesTagNS } from "saxes"; import { err, ok, type Result } from "./result.js"; import { @@ -10,6 +16,12 @@ import { /** XML 1.0 literal line endings; Unicode separators remain source characters. */ export const XML_LINE_ENDING = /\r\n?|\n/gu; +/** Match an element in no namespace, including under an explicit empty default. */ +export const isUnqualifiedXmlElement = (node: Node | null): node is Element => + node !== null && + node.nodeType === 1 && + (node as Element).namespaceURI === null; + const XML_FORMAT = "xml"; /** How XML failed: ill-formed syntax, or a DOCTYPE the policy refuses. */ diff --git a/tests/boundary/android/androidManifestComponents.test.ts b/tests/boundary/android/androidManifestComponents.test.ts index 3fb954d615..5ead48bd74 100644 --- a/tests/boundary/android/androidManifestComponents.test.ts +++ b/tests/boundary/android/androidManifestComponents.test.ts @@ -1,4 +1,5 @@ import { expect, it, vi } from "vitest"; +import { parseManifestPackage } from "../../../src/apktool/ApktoolDecodeOutput.js"; import { androidResultSchemas } from "../../../src/domain/android/androidAnalysis.js"; import { @@ -357,14 +358,21 @@ it.each([ }, ); -it("refuses a namespaced manifest root", () => { - const inventory = parseAndroidManifestComponents( - '', - "complete", - "30", +it("admits the same unqualified manifest root in both Android readers", () => { + const manifest = (namespace: string) => + ``; + const valid = manifest(""); + expect(parseManifestPackage(valid)).toBe("com.example"); + expect(parseAndroidManifestComponents(valid, "complete", "30")).toMatchObject( + { status: "complete", items: [{ name: "com.example.Main" }] }, + ); + const unexpected = manifest("urn:vendor"); + expect( + parseAndroidManifestComponents(unexpected, "complete", "30"), + ).toMatchObject({ status: "unavailable", items: [] }); + expect(() => parseManifestPackage(unexpected)).toThrow( + "AndroidManifest.xml root must be ", ); - expect(inventory.status).toBe("unavailable"); - expect(inventory.items).toEqual([]); }); it("does not index foreign activities as alias targets", () => { @@ -927,12 +935,12 @@ it("reports unavailable inventory for malformed or unexpected manifests", () => "complete", ), ).toMatchObject({ status: "unavailable", items: [] }); - expect( - parseAndroidManifestComponents( - '', - "complete", - ), - ).toMatchObject({ status: "unavailable", items: [] }); + const withDoctype = + ''; + expect(parseAndroidManifestComponents(withDoctype, "complete")).toMatchObject( + { status: "unavailable", items: [] }, + ); + expect(parseManifestPackage(withDoctype)).toBe("com.example"); }); it("bounds the component inventory and says so", () => {