mirror of
https://github.com/morluto/rea.git
synced 2026-11-19 21:23:45 +00:00
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
This commit is contained in:
1 parent
3b8151125c
commit
61af8abeb6
8 files changed
+120
-88
No files matched your search
@@ -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<typeof androidIntentFilterSchema>;
|
||||
@@ -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;
|
||||
|
||||
@@ -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 <manifest>, 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 <manifest>, 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
|
||||
|
||||
@@ -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 ||
|
||||
|
||||
@@ -323,6 +323,14 @@ describe("parseDecodedStrings", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("ignores foreign namespace string elements", () => {
|
||||
const xml =
|
||||
'<resources><string xmlns="urn:vendor" name="foreign">decoy</string><string name="local">one</string></resources>';
|
||||
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(["<manifest/>", '<other><string name="a">one</string></other>'])(
|
||||
"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([
|
||||
"<manifest/>",
|
||||
'<other><string name="a">one</string></other>',
|
||||
'<resources xmlns="urn:vendor"><string name="a">one</string></resources>',
|
||||
])("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");
|
||||
});
|
||||
@@ -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 <manifest>");
|
||||
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;
|
||||
|
||||
@@ -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,
|
||||
});
|
||||
};
|
||||
@@ -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. */
|
||||
|
||||
@@ -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(
|
||||
'<manifest xmlns="urn:vendor" xmlns:android="http://schemas.android.com/apk/res/android" package="com.example"><application><activity android:name=".Decoy"/></application></manifest>',
|
||||
"complete",
|
||||
"30",
|
||||
it("admits the same unqualified manifest root in both Android readers", () => {
|
||||
const manifest = (namespace: string) =>
|
||||
`<manifest xmlns="${namespace}" xmlns:a="http://schemas.android.com/apk/res/android" xmlns:vendor="urn:vendor" package="com.example" vendor:package="com.decoy"><application><activity a:name=".Main"/></application></manifest>`;
|
||||
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 <manifest>",
|
||||
);
|
||||
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(
|
||||
'<?xml version="1.0" encoding="utf-8"?><!DOCTYPE manifest><manifest />',
|
||||
"complete",
|
||||
),
|
||||
).toMatchObject({ status: "unavailable", items: [] });
|
||||
const withDoctype =
|
||||
'<?xml version="1.0" encoding="utf-8"?><!DOCTYPE manifest><manifest package="com.example" />';
|
||||
expect(parseAndroidManifestComponents(withDoctype, "complete")).toMatchObject(
|
||||
{ status: "unavailable", items: [] },
|
||||
);
|
||||
expect(parseManifestPackage(withDoctype)).toBe("com.example");
|
||||
});
|
||||
|
||||
it("bounds the component inventory and says so", () => {
|
||||
|
||||
Reference in new issue
Block a user