fix(zip): require complete local headers in trailing archive probes (#12182)

Co-authored-by: ShuaixinHuang <197200901+ShuaixinHuang@users.noreply.github.com>
This commit is contained in:
ShuaixinHuangandShuaixinHuang authored and GitHub committed 2026-11-18 23:51:30 -05:00
1 parent d785d63267
commit 13e9ac3871
4 files changed
+62 -14

No files matched your search

+14
View File
@@ -0,0 +1,14 @@
import type { BinaryReader } from "../binaryContainer.js";
export const ZIP_LOCAL_HEADER_BYTES = 30;
const ZIP_LOCAL_HEADER_SIGNATURE = 0x04034b50;
/** Locate the data after the fixed header, file name and extra field. */
export const zipLocalDataOffset = (
header: BinaryReader,
offset: number,
): number | undefined =>
header.bytes.length >= ZIP_LOCAL_HEADER_BYTES &&
header.u32(0) === ZIP_LOCAL_HEADER_SIGNATURE
? offset + ZIP_LOCAL_HEADER_BYTES + header.u16(26) + header.u16(28)
: undefined;
+8 -7
View File
@@ -1,4 +1,8 @@
import { BinaryReader } from "../binaryContainer.js";
import {
ZIP_LOCAL_HEADER_BYTES,
zipLocalDataOffset,
} from "./zipLocalHeader.js";
import {
apkSigningPairEnd,
APK_SIGNING_BLOCK_MAX_PAIRS,
@@ -28,8 +32,6 @@ export interface ZipMemberRegionAccount {
readonly unverifiedExtents: number;
}
const LOCAL_HEADER_BYTES = 30;
const LOCAL_HEADER_SIGNATURE = 0x04034b50;
const DATA_DESCRIPTOR_SIGNATURE = 0x08074b50;
const DATA_DESCRIPTOR_FLAG = 0x8;
const ZIP64_EXTRA = 0x1;
@@ -112,17 +114,16 @@ export const accountZipMemberRegion = async (
firstUnaccountedOffset ??= cursor;
};
for (const record of [...records].sort((a, b) => a.offset - b.offset)) {
if (record.offset + LOCAL_HEADER_BYTES > directoryOffset) {
if (record.offset + ZIP_LOCAL_HEADER_BYTES > directoryOffset) {
unlocatedHeaders += 1;
continue;
}
const header = await view(record.offset, LOCAL_HEADER_BYTES);
if (header.u32(0) !== LOCAL_HEADER_SIGNATURE) {
const header = await view(record.offset, ZIP_LOCAL_HEADER_BYTES);
const dataOffset = zipLocalDataOffset(header, record.offset);
if (dataOffset === undefined) {
unlocatedHeaders += 1;
continue;
}
const dataOffset =
record.offset + LOCAL_HEADER_BYTES + header.u16(26) + header.u16(28);
const end = dataOffset + record.compressedSize;
const deferred = (header.u16(6) & DATA_DESCRIPTOR_FLAG) !== 0;
// A directory/encrypted entry may never be opened. Do not let an unchecked
+28
View File
@@ -0,0 +1,28 @@
import { TextReader, Uint8ArrayWriter, ZipWriter } from "@zip.js/zip.js";
import { expect, it } from "vitest";
import { locateTrailingZipArchive } from "./zipPackageFormat.js";
it("requires the first local ZIP header and its variable fields to fit before the directory", async () => {
const writer = new ZipWriter(new Uint8ArrayWriter());
await writer.add("a.txt", new TextReader("hello"));
const zip = Buffer.from(await writer.close());
const prefix = Buffer.from("#!/bin/sh\nexit 0\n");
const directory = zip.readUInt32LE(zip.length - 22 + 16);
const locate = async (archive: Buffer) => {
const file = Buffer.concat([prefix, archive]);
return locateTrailingZipArchive(file.length, async (start, length) =>
file.subarray(start, start + length),
);
};
const overlappingFixedHeader = Buffer.from(zip);
overlappingFixedHeader.writeUInt32LE(0x04034b50, directory - 4);
overlappingFixedHeader.writeUInt32LE(directory - 4, directory + 42);
const overlappingExtraField = Buffer.from(zip);
overlappingExtraField.writeUInt16LE(directory, 28);
expect(await locate(zip)).toEqual({ leadingBytes: prefix.length });
expect(await locate(overlappingFixedHeader)).toBeUndefined();
expect(await locate(overlappingExtraField)).toBeUndefined();
});
+12 -7
View File
@@ -1,4 +1,8 @@
import { BinaryReader } from "./binaryContainer.js";
import {
ZIP_LOCAL_HEADER_BYTES,
zipLocalDataOffset,
} from "./zip/zipLocalHeader.js";
import {
zipEndOfCentralDirectoryCandidates,
zipEndUsesSingleVolume,
@@ -45,7 +49,6 @@ export const hasZipSignature = (bytes: Uint8Array): boolean =>
);
const CENTRAL_DIRECTORY_HEADER = 0x02014b50;
const LOCAL_FILE_HEADER = 0x04034b50;
const CENTRAL_HEADER_BYTES = 46;
/** Positional read of at most `length` bytes; a short result means end of file. */
@@ -98,12 +101,14 @@ export const locateTrailingZipArchive = async (
)
continue;
const firstLocal = headerReader.u32(42) + shift;
if (firstLocal === 0 || firstLocal >= directory) continue;
const local = Buffer.from(await read(firstLocal, 4));
if (
local.length === 4 &&
new BinaryReader(local, true, "zip").u32(0) === LOCAL_FILE_HEADER
)
if (firstLocal === 0 || firstLocal > directory - ZIP_LOCAL_HEADER_BYTES)
continue;
const local = Buffer.from(await read(firstLocal, ZIP_LOCAL_HEADER_BYTES));
const dataOffset = zipLocalDataOffset(
new BinaryReader(local, true, "zip"),
firstLocal,
);
if (dataOffset !== undefined && dataOffset <= directory)
return { leadingBytes: firstLocal };
}
return undefined;