refactor(apk): read signing block and v1 local header fields through BinaryReader (#12450)

Move the eight remaining raw DataView reads in src/domain/apkSignature
onto the shared BinaryReader and drop the three APK signature entries
from the raw-buffer-read baseline. Every read stays behind its existing
range check, so results, error kinds and messages are unchanged.

Refs #11865
This commit is contained in:
speedcubing authored and GitHub committed 2026-11-19 17:59:56 +00:00
1 parent c84fde05e3
commit d58942a4ea
4 files changed
+30 -23

No files matched your search

@@ -1,7 +1,4 @@
{
"src/domain/apkSignature/apkSignatureSchemes.ts": 1,
"src/domain/apkSignature/apkSigningBlock.ts": 2,
"src/domain/apkSignature/apkV1Signature.ts": 5,
"src/domain/apple/appleMachoFixups.ts": 22,
"src/domain/apple/appleMachoSelection.ts": 2,
"src/domain/apple/jvmClassFile.ts": 2,
@@ -11,6 +11,7 @@
* or inconsistent length is a `malformed-signer` error, and a count past a
* resource admission cap is a distinct `budget-exceeded` error, never conflated.
*/
import { BinaryReader } from "../binaryContainer.js";
import { type Result, err, ok } from "../result.js";
export type ApkSchemeVersion = "v2" | "v3" | "v3.1";
@@ -78,16 +79,20 @@ const LINEAGE_VERSION = 1;
/** Sequential little-endian reader over a byte range; every read is bounds-checked. */
class LeReader {
offset = 0;
private readonly dv: DataView;
private readonly reader: BinaryReader;
constructor(private readonly bytes: Uint8Array) {
this.dv = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
this.reader = new BinaryReader(
Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength),
true,
"apk",
);
}
get remaining(): number {
return this.bytes.length - this.offset;
}
u32(): number | null {
if (this.remaining < 4) return null;
const v = this.dv.getUint32(this.offset, true);
const v = this.reader.u32(this.offset);
this.offset += 4;
return v;
}
+12 -11
View File
@@ -98,13 +98,10 @@ export interface ApkSigningBlock {
readonly pairs: readonly ApkSigningBlockPair[];
}
const view = (bytes: Uint8Array): DataView =>
new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
/** Read an unsigned 64-bit little-endian value as a Number, or null if unsafe/oob. */
const u64 = (dv: DataView, at: number): number | null => {
if (at < 0 || at + 8 > dv.byteLength) return null;
const value = dv.getBigUint64(at, true);
const u64 = (reader: BinaryReader, at: number): number | null => {
if (at < 0 || at + 8 > reader.bytes.length) return null;
const value = reader.word(at, 64);
return value <= BigInt(Number.MAX_SAFE_INTEGER) ? Number(value) : null;
};
@@ -355,8 +352,12 @@ export const locateApkSigningBlock = (
): Result<ApkSigningBlock | null, ApkSigningBlockError> => {
const cdOffset = tail.centralDirectoryOffset;
if (!hasBlockMagic(bytes, cdOffset)) return ok(null);
const dv = view(bytes);
const trailingSize = u64(dv, cdOffset - 24);
const reader = new BinaryReader(
Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength),
true,
"zip",
);
const trailingSize = u64(reader, cdOffset - 24);
// The footer alone (trailing size + magic) is 24 bytes; a smaller declared
// size would alias the leading and trailing fields (apksig rejects it too).
if (trailingSize === null || trailingSize < 24)
@@ -372,7 +373,7 @@ export const locateApkSigningBlock = (
offset: cdOffset,
detail: "block extends before file start",
});
const leadingSize = u64(dv, blockStart);
const leadingSize = u64(reader, blockStart);
if (leadingSize === null || leadingSize !== trailingSize)
return err({
kind: "malformed-block",
@@ -384,7 +385,7 @@ export const locateApkSigningBlock = (
let cursor = blockStart + 8;
const pairsEnd = cdOffset - 24; // stop before trailing size + magic
while (cursor < pairsEnd) {
const length = u64(dv, cursor);
const length = u64(reader, cursor);
const next =
length === null
? undefined
@@ -401,7 +402,7 @@ export const locateApkSigningBlock = (
resource: "signing-block-pairs",
limit: APK_SIGNING_BLOCK_MAX_PAIRS,
});
const id = dv.getUint32(cursor + 8, true);
const id = reader.u32(cursor + 8);
const valueStart = cursor + 12;
pairs.push({
id,
+10 -6
View File
@@ -445,12 +445,16 @@ export const localEntryData = (
return err({
detail: `local header at byte ${String(at)} is out of range`,
});
const dv = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
if (dv.getUint32(at, true) !== LOCAL_SIGNATURE)
const reader = new BinaryReader(
Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength),
true,
"zip",
);
if (reader.u32(at) !== LOCAL_SIGNATURE)
return err({
detail: `local header signature mismatch at byte ${String(at)}`,
});
const flags = dv.getUint16(at + 6, true);
const flags = reader.u16(at + 6);
const descriptor = (flags & FLAG_DATA_DESCRIPTOR) !== 0;
if (descriptor !== ((entry.flags & FLAG_DATA_DESCRIPTOR) !== 0))
return err({
@@ -463,13 +467,13 @@ export const localEntryData = (
["compressed size", 18, entry.compressedSize],
["uncompressed size", 22, entry.uncompressedSize],
] as const) {
const local = dv.getUint32(at + offset, true);
const local = reader.u32(at + offset);
if (local !== expected)
return err({
detail: `local header ${field} ${String(local)} differs from the central directory's ${String(expected)}`,
});
}
const nameLength = dv.getUint16(at + 26, true);
const nameLength = reader.u16(at + 26);
const nameStart = at + LOCAL_FIXED_SIZE;
if (
nameStart + nameLength > limit ||
@@ -483,7 +487,7 @@ export const localEntryData = (
return err({
detail: "local header name differs from the central directory's",
});
const start = nameStart + nameLength + dv.getUint16(at + 28, true);
const start = nameStart + nameLength + reader.u16(at + 28);
const end = start + entry.compressedSize;
if (end > limit)
return err({