|
| 1 | +// Minimal Android App Bundle (.aab) manifest reader; replaces the unmaintained |
| 2 | +// aab-parser, which pinned a vulnerable protobufjs (^6.11.2). |
| 3 | + |
| 4 | +import * as fs from "fs"; |
| 5 | +import * as jszip from "jszip"; |
| 6 | +import * as protobuf from "protobufjs"; |
| 7 | + |
| 8 | +export type AabManifest = { |
| 9 | + versionCode: number; |
| 10 | + versionName: string; |
| 11 | + packageName: string; |
| 12 | + compiledSdkVersion: number; |
| 13 | + compiledSdkVersionCodename: number; |
| 14 | +}; |
| 15 | + |
| 16 | +type ManifestAttribute = { name: string; value: string }; |
| 17 | + |
| 18 | +// An AAB's <manifest> is protobuf-encoded as an aapt.pb.XmlNode. We only read a |
| 19 | +// few attributes, so we declare just that slice (field numbers from AOSP |
| 20 | +// aapt2/Resources.proto); the decoder skips every field we omit. |
| 21 | +const XmlNode = protobuf.parse(` |
| 22 | + syntax = "proto3"; |
| 23 | + package aapt.pb; |
| 24 | + message XmlAttribute { string name = 2; string value = 3; } |
| 25 | + message XmlElement { string name = 3; repeated XmlAttribute attribute = 4; } |
| 26 | + message XmlNode { XmlElement element = 1; } |
| 27 | +`).root.lookupType("aapt.pb.XmlNode"); |
| 28 | + |
| 29 | +async function readManifestAttributes(file: string | Buffer): Promise<ManifestAttribute[]> { |
| 30 | + const buffer = typeof file === "string" ? await fs.promises.readFile(file) : file; |
| 31 | + const archive = await jszip.loadAsync(buffer); |
| 32 | + const manifest = await archive.file("base/manifest/AndroidManifest.xml")?.async("nodebuffer"); |
| 33 | + if (manifest === undefined) { |
| 34 | + throw new Error("Could not find AndroidManifest.xml file inside the app bundle file"); |
| 35 | + } |
| 36 | + |
| 37 | + const decoded = XmlNode.decode(manifest).toJSON() as { element?: { attribute?: ManifestAttribute[] } }; |
| 38 | + return decoded.element?.attribute ?? []; |
| 39 | +} |
| 40 | + |
| 41 | +export async function parseAabManifest(file: string | Buffer): Promise<AabManifest> { |
| 42 | + const attributes = await readManifestAttributes(file); |
| 43 | + |
| 44 | + function getAttribute(name: string): string { |
| 45 | + const attribute = attributes.find((attr) => attr.name === name); |
| 46 | + if (attribute === undefined) { |
| 47 | + throw new Error(`Attribute "${name}" not found in AndroidManifest.xml`); |
| 48 | + } |
| 49 | + return attribute.value; |
| 50 | + } |
| 51 | + |
| 52 | + return { |
| 53 | + versionCode: Number(getAttribute("versionCode")), |
| 54 | + versionName: getAttribute("versionName"), |
| 55 | + packageName: getAttribute("package"), |
| 56 | + compiledSdkVersion: Number(getAttribute("compileSdkVersion")), |
| 57 | + compiledSdkVersionCodename: Number(getAttribute("compileSdkVersionCodename")), |
| 58 | + }; |
| 59 | +} |
0 commit comments