Skip to content

Commit 00f0f8c

Browse files
authored
zlib: validate central directory record count
ZIP readers trusted the EOCD record count. They did not check that the parsed headers consumed the declared central directory size. Reject archives whose count leaves directory bytes unparsed. Signed-off-by: Matteo Collina <[email protected]> PR-URL: #65002 Reviewed-By: Filip Skokan <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent 2fbd056 commit 00f0f8c

3 files changed

Lines changed: 42 additions & 0 deletions

File tree

‎lib/internal/zip/entry.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,10 @@ function* readArchiveEntries(buf, end) {
941941
});
942942
pos = central.byteOffset + central.byteLength;
943943
}
944+
if (pos !== cdEnd) {
945+
throw new ERR_ZIP_INVALID_ARCHIVE(
946+
'central directory record count is inconsistent with its size');
947+
}
944948
// Sort a separate range list; entries themselves are yielded in central
945949
// directory order.
946950
const ranges = ArrayPrototypeSort(ArrayPrototypeSlice(parsed), (a, b) => a.start - b.start);

‎lib/internal/zip/headers.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,10 @@ function readCentralDirectory(buffer, count) {
553553
ArrayPrototypePush(result, header);
554554
pos += header.byteLength;
555555
}
556+
if (pos !== buffer.length) {
557+
throw new ERR_ZIP_INVALID_ARCHIVE(
558+
'central directory record count is inconsistent with its size');
559+
}
556560
return result;
557561
}
558562

‎test/parallel/test-zlib-zip-security.js‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,40 @@ test('open() rejects contradictory classic-EOCD vs Zip64 metadata', async () =>
230230
}
231231
}, { timeout: 120_000 });
232232

233+
test('central-directory record count must account for its full declared size', async () => {
234+
const chunks = [];
235+
for await (const chunk of zlib.createZipArchive([
236+
await zlib.ZipEntry.create('visible.txt', Buffer.from('visible'), { method: 'store' }),
237+
await zlib.ZipEntry.create('hidden.txt', Buffer.from('hidden'), { method: 'store' }),
238+
])) chunks.push(chunk);
239+
const tampered = Buffer.concat(chunks);
240+
const eocd = tampered.length - 22;
241+
assert.strictEqual(tampered.readUInt16LE(eocd + 8), 2);
242+
assert.strictEqual(tampered.readUInt16LE(eocd + 10), 2);
243+
244+
// Keep the single-disk counts consistent with each other, but make both
245+
// disagree with the two complete records in the declared directory size.
246+
tampered.writeUInt16LE(1, eocd + 8);
247+
tampered.writeUInt16LE(1, eocd + 10);
248+
const expected = {
249+
code: 'ERR_ZIP_INVALID_ARCHIVE',
250+
message: /central directory record count is inconsistent with its size/,
251+
};
252+
253+
assert.throws(() => [...zlib.ZipEntry.read(tampered)], expected);
254+
assert.throws(() => new zlib.ZipBuffer(tampered), expected);
255+
256+
const dir = await fsp.mkdtemp(path.join(tmpdir.path, `zip-sec-${seq++}-`));
257+
const p = path.join(dir, 'record-count-mismatch.zip');
258+
try {
259+
await fsp.writeFile(p, tampered);
260+
await assert.rejects(zlib.ZipFile.open(p), expected);
261+
assert.throws(() => zlib.ZipFile.openSync(p), expected);
262+
} finally {
263+
await fsp.rm(dir, { recursive: true, force: true });
264+
}
265+
});
266+
233267
// -- Finding 4: the file-backed open-time overlap check uses a 30-byte lower
234268
// bound for each local header, while the in-memory reader uses the exact
235269
// local-header length. A crafted "quoted overlap" archive therefore passes

0 commit comments

Comments
 (0)