mirror of
https://github.com/morluto/rea.git
synced 2026-11-19 13:12:26 +00:00
fix(apk): require agreeing single-volume ZIP64 counts (#12198)
This commit is contained in:
1 parent
195e62ef1f
commit
7503c538af
4 files changed
+91
-38
No files matched your search
@@ -1,7 +1,9 @@
|
||||
import { BinaryReader } from "../binaryContainer.js";
|
||||
import {
|
||||
readZip64DirectoryRecord,
|
||||
readZip64Locator,
|
||||
zip64EndUsesSingleVolume,
|
||||
zip64LocatorUsesSingleVolume,
|
||||
zipEndOfCentralDirectoryCandidates,
|
||||
zipEndUsesSingleVolume,
|
||||
ZIP_CLASSIC_DIRECTORY_LIMITS,
|
||||
@@ -166,8 +168,9 @@ const centralDirectoryOffset = (
|
||||
"zip",
|
||||
);
|
||||
// Zip64: a locator precedes the EOCD, pointing at the Zip64 EOCD record.
|
||||
const locator = eocdOffset - ZIP64_LOCATOR.bytes;
|
||||
if (locator < 0 || reader.u32(locator) !== ZIP64_LOCATOR.signature) {
|
||||
const locatorOffset = eocdOffset - ZIP64_LOCATOR.bytes;
|
||||
const locator = readZip64Locator(reader, locatorOffset);
|
||||
if (locator === undefined) {
|
||||
// Exactly 65,535 entries saturates the count without Zip64; that archive
|
||||
// is still described completely by its 32-bit EOCD fields.
|
||||
if (
|
||||
@@ -183,26 +186,24 @@ const centralDirectoryOffset = (
|
||||
}
|
||||
// APPNOTE 6.3.10 §4.3.15 makes the record offset relative to its disk.
|
||||
// A selected file cannot resolve another disk's offset as a local span.
|
||||
const recordDisk = reader.u32(locator + 4);
|
||||
const totalDisks = reader.u32(locator + 16);
|
||||
const { disk: recordDisk, totalDisks, recordOffset: offsetOnDisk } = locator;
|
||||
if (totalDisks === 0)
|
||||
return err({
|
||||
kind: "malformed-zip",
|
||||
offset: locator + 16,
|
||||
offset: locatorOffset + ZIP64_LOCATOR.totalDisksOffset,
|
||||
detail: "Zip64 locator declares zero disks",
|
||||
});
|
||||
if (recordDisk >= totalDisks)
|
||||
return err({
|
||||
kind: "malformed-zip",
|
||||
offset: locator + 4,
|
||||
offset: locatorOffset + ZIP64_LOCATOR.diskOffset,
|
||||
detail: "Zip64 EOCD disk is outside the declared disk count",
|
||||
});
|
||||
if (totalDisks > 1)
|
||||
if (!zip64LocatorUsesSingleVolume(locator))
|
||||
return err({
|
||||
kind: "zip64-unsupported",
|
||||
detail: "multi-disk Zip64 archives are not supported",
|
||||
});
|
||||
const offsetOnDisk = reader.word(locator + ZIP64_LOCATOR.recordOffset, 64);
|
||||
const maximumExact = BigInt(Number.MAX_SAFE_INTEGER);
|
||||
if (offsetOnDisk > maximumExact)
|
||||
return err({
|
||||
@@ -210,13 +211,13 @@ const centralDirectoryOffset = (
|
||||
detail: "Zip64 EOCD offset exceeds safe range",
|
||||
});
|
||||
const recordOffset = Number(offsetOnDisk);
|
||||
const record = readZip64DirectoryRecord(reader, recordOffset, locator);
|
||||
const record = readZip64DirectoryRecord(reader, recordOffset, locatorOffset);
|
||||
if (!record.ok) {
|
||||
switch (record.error) {
|
||||
case "fixed-fields":
|
||||
return err({
|
||||
kind: "malformed-zip",
|
||||
offset: locator + ZIP64_LOCATOR.recordOffset,
|
||||
offset: locatorOffset + ZIP64_LOCATOR.recordOffset,
|
||||
detail: "Zip64 EOCD record extends beyond its locator",
|
||||
});
|
||||
case "signature":
|
||||
@@ -235,13 +236,20 @@ const centralDirectoryOffset = (
|
||||
}
|
||||
if (
|
||||
!zipEndUsesSingleVolume(eocd, "zip64Classic") ||
|
||||
!zip64EndUsesSingleVolume(reader, locator, recordOffset)
|
||||
!zip64EndUsesSingleVolume(record.value)
|
||||
)
|
||||
return err({
|
||||
kind: "zip64-unsupported",
|
||||
detail: "Zip64 end records do not describe the selected single disk",
|
||||
});
|
||||
const { directoryOffset, directorySize, totalEntries } = record.value;
|
||||
const { directoryOffset, directorySize, entriesOnDisk, totalEntries } =
|
||||
record.value;
|
||||
if (entriesOnDisk !== totalEntries)
|
||||
return err({
|
||||
kind: "malformed-zip",
|
||||
offset: recordOffset + ZIP64_END.entriesOnDiskOffset,
|
||||
detail: "single-disk Zip64 entry counts disagree",
|
||||
});
|
||||
if (
|
||||
directoryOffset > maximumExact ||
|
||||
directorySize > maximumExact ||
|
||||
|
||||
@@ -7,7 +7,9 @@ import {
|
||||
ZIP64_LOCATOR,
|
||||
ZIP_EOCD_BYTES,
|
||||
readZip64DirectoryRecord,
|
||||
readZip64Locator,
|
||||
zip64EndUsesSingleVolume,
|
||||
zip64LocatorUsesSingleVolume,
|
||||
zipEndOfCentralDirectoryCandidates,
|
||||
zipEndUsesSingleVolume,
|
||||
} from "../zipEndOfCentralDirectory.js";
|
||||
@@ -50,22 +52,23 @@ const directoryEnd = (reader: BinaryReader) => {
|
||||
entries: BigInt(totalEntries),
|
||||
}
|
||||
: undefined;
|
||||
const locator = end - ZIP64_LOCATOR.bytes;
|
||||
const locatorOffset = end - ZIP64_LOCATOR.bytes;
|
||||
// Like Python's zipfile, follow a locator whether or not the classic record defers to it.
|
||||
if (locator < 0 || reader.u32(locator) !== ZIP64_LOCATOR.signature)
|
||||
return classic;
|
||||
const declared = reader.word(locator + ZIP64_LOCATOR.recordOffset, 64);
|
||||
const fallback = locator - ZIP64_END.fixedBytes;
|
||||
const locator = readZip64Locator(reader, locatorOffset);
|
||||
if (locator === undefined) return classic;
|
||||
const declared = locator.recordOffset;
|
||||
const fallback = locatorOffset - ZIP64_END.fixedBytes;
|
||||
// Keep the fixed-record fallback for archives with an unadjusted prepended offset.
|
||||
for (const at of [
|
||||
declared <= BigInt(fallback) ? Number(declared) : -1,
|
||||
fallback,
|
||||
]) {
|
||||
const record = readZip64DirectoryRecord(reader, at, locator);
|
||||
if (!record.ok || record.value.end !== locator) continue;
|
||||
const record = readZip64DirectoryRecord(reader, at, locatorOffset);
|
||||
if (!record.ok || record.value.end !== locatorOffset) continue;
|
||||
if (
|
||||
!zipEndUsesSingleVolume(eocd, "zip64Classic") ||
|
||||
!zip64EndUsesSingleVolume(reader, locator, at) ||
|
||||
!zip64LocatorUsesSingleVolume(locator) ||
|
||||
!zip64EndUsesSingleVolume(record.value) ||
|
||||
record.value.entriesOnDisk !== record.value.totalEntries
|
||||
)
|
||||
return undefined;
|
||||
@@ -252,7 +255,7 @@ export const boundedDirectoryTail = (directory: ZipDirectory): Uint8Array => {
|
||||
BigInt(offset) + BigInt(size),
|
||||
true,
|
||||
);
|
||||
locator.setUint32(16, 1, true);
|
||||
locator.setUint32(ZIP64_LOCATOR.totalDisksOffset, 1, true);
|
||||
}
|
||||
const end = new DataView(tail.buffer, size + wideBytes, ZIP_EOCD_BYTES);
|
||||
end.setUint32(0, 0x06054b50, true);
|
||||
|
||||
@@ -18,6 +18,8 @@ export const ZIP64_END = {
|
||||
fixedBytes: 56,
|
||||
sizePrefixBytes: 12,
|
||||
sizeOffset: 4,
|
||||
diskOffset: 16,
|
||||
directoryDiskOffset: 20,
|
||||
entriesOnDiskOffset: 24,
|
||||
totalEntriesOffset: 32,
|
||||
directorySizeOffset: 40,
|
||||
@@ -27,11 +29,38 @@ export const ZIP64_END = {
|
||||
export const ZIP64_LOCATOR = {
|
||||
signature: 0x07064b50,
|
||||
bytes: 20,
|
||||
diskOffset: 4,
|
||||
recordOffset: 8,
|
||||
totalDisksOffset: 16,
|
||||
} as const;
|
||||
|
||||
type Zip64Locator = {
|
||||
readonly disk: number;
|
||||
readonly totalDisks: number;
|
||||
readonly recordOffset: bigint;
|
||||
};
|
||||
|
||||
export const readZip64Locator = (
|
||||
reader: BinaryReader,
|
||||
offset: number,
|
||||
): Zip64Locator | undefined => {
|
||||
if (
|
||||
offset < 0 ||
|
||||
offset + ZIP64_LOCATOR.bytes > reader.bytes.length ||
|
||||
reader.u32(offset) !== ZIP64_LOCATOR.signature
|
||||
)
|
||||
return undefined;
|
||||
return {
|
||||
disk: reader.u32(offset + ZIP64_LOCATOR.diskOffset),
|
||||
totalDisks: reader.u32(offset + ZIP64_LOCATOR.totalDisksOffset),
|
||||
recordOffset: reader.word(offset + ZIP64_LOCATOR.recordOffset, 64),
|
||||
};
|
||||
};
|
||||
|
||||
type Zip64DirectoryRecord = {
|
||||
readonly end: number;
|
||||
readonly disk: number;
|
||||
readonly directoryDisk: number;
|
||||
readonly entriesOnDisk: bigint;
|
||||
readonly totalEntries: bigint;
|
||||
readonly directorySize: bigint;
|
||||
@@ -55,6 +84,8 @@ export const readZip64DirectoryRecord = (
|
||||
return err("size");
|
||||
return ok({
|
||||
end: offset + ZIP64_END.sizePrefixBytes + Number(bodyBytes),
|
||||
disk: reader.u32(offset + ZIP64_END.diskOffset),
|
||||
directoryDisk: reader.u32(offset + ZIP64_END.directoryDiskOffset),
|
||||
entriesOnDisk: reader.word(offset + ZIP64_END.entriesOnDiskOffset, 64),
|
||||
totalEntries: reader.word(offset + ZIP64_END.totalEntriesOffset, 64),
|
||||
directorySize: reader.word(offset + ZIP64_END.directorySizeOffset, 64),
|
||||
@@ -76,12 +107,12 @@ export const ZIP_SINGLE_VOLUME = {
|
||||
classic: [0],
|
||||
zip64Classic: [0, 0xffff],
|
||||
zip64Locator: [
|
||||
[4, 0],
|
||||
[16, 1],
|
||||
[ZIP64_LOCATOR.diskOffset, 0],
|
||||
[ZIP64_LOCATOR.totalDisksOffset, 1],
|
||||
],
|
||||
zip64End: [
|
||||
[16, 0],
|
||||
[20, 0],
|
||||
[ZIP64_END.diskOffset, 0],
|
||||
[ZIP64_END.directoryDiskOffset, 0],
|
||||
],
|
||||
} as const;
|
||||
|
||||
@@ -109,18 +140,19 @@ export const zipEndUsesSingleVolume = (
|
||||
return allowed.includes(disk) && allowed.includes(directoryDisk);
|
||||
};
|
||||
|
||||
/** Check the disk fields in the located Zip64 locator and end record. */
|
||||
export const zip64EndUsesSingleVolume = (
|
||||
reader: BinaryReader,
|
||||
locator: number,
|
||||
end: number,
|
||||
): boolean =>
|
||||
ZIP_SINGLE_VOLUME.zip64Locator.every(
|
||||
([field, value]) => reader.u32(locator + field) === value,
|
||||
) &&
|
||||
ZIP_SINGLE_VOLUME.zip64End.every(
|
||||
([field, value]) => reader.u32(end + field) === value,
|
||||
);
|
||||
export const zip64LocatorUsesSingleVolume = ({
|
||||
disk,
|
||||
totalDisks,
|
||||
}: Zip64Locator): boolean =>
|
||||
disk === ZIP_SINGLE_VOLUME.zip64Locator[0][1] &&
|
||||
totalDisks === ZIP_SINGLE_VOLUME.zip64Locator[1][1];
|
||||
|
||||
export const zip64EndUsesSingleVolume = ({
|
||||
disk,
|
||||
directoryDisk,
|
||||
}: Zip64DirectoryRecord): boolean =>
|
||||
disk === ZIP_SINGLE_VOLUME.zip64End[0][1] &&
|
||||
directoryDisk === ZIP_SINGLE_VOLUME.zip64End[1][1];
|
||||
|
||||
/**
|
||||
* Observe complete classic records whose declared comment ends this window,
|
||||
|
||||
@@ -132,6 +132,12 @@ cliTest(
|
||||
diagnosticOffset: locatorOffset + 4,
|
||||
detail: "Zip64 EOCD disk is outside the declared disk count",
|
||||
},
|
||||
{
|
||||
at: recordOffset + 24,
|
||||
value: 0,
|
||||
diagnosticOffset: recordOffset + 24,
|
||||
detail: "single-disk Zip64 entry counts disagree",
|
||||
},
|
||||
];
|
||||
for (const scenario of cases) {
|
||||
const bytes = Uint8Array.from(fixture.bytes);
|
||||
@@ -219,7 +225,11 @@ cliTest(
|
||||
fixture.recordOffset + 48,
|
||||
]) {
|
||||
const bytes = Uint8Array.from(fixture.bytes);
|
||||
new DataView(bytes.buffer).setBigUint64(at, 1n << 53n, true);
|
||||
const view = new DataView(bytes.buffer);
|
||||
const tooWide = 1n << 53n;
|
||||
view.setBigUint64(at, tooWide, true);
|
||||
if (at === fixture.recordOffset + 32)
|
||||
view.setBigUint64(fixture.recordOffset + 24, tooWide, true);
|
||||
await expectRefusal(cli, path, bytes, {
|
||||
code: "unsupported_target",
|
||||
category: "unsupported_target",
|
||||
|
||||
Reference in new issue
Block a user