forked from Furumi/furumi-web
Charge ZIP entries what fflate copies for them
A stored entry is copied by its compressed size, so a record that declares an original size of zero could still bring hundreds of megabytes, once per central directory record that points at it. Each extracted file is now charged the larger of its sizes, a stored entry must declare equal sizes, and unknown methods and duplicate names are refused. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d275e7326b
commit
7af037066e
2 files changed
+76
-4
No files matched your search
@@ -140,8 +140,9 @@ document.addEventListener('alpine:init', () => {
|
||||
const MiB = 1024 * 1024;
|
||||
// The request limit of /api/imports. The wizard checks it before sending,
|
||||
// and the archive limits keep a ZIP bomb from filling the browser memory:
|
||||
// fflate never inflates an entry beyond its declared size, so the declared
|
||||
// sizes of the extracted files bound what is decompressed.
|
||||
// fflate inflates a deflated entry into a buffer of its declared size and
|
||||
// copies a stored entry by its compressed size, so each extracted file is
|
||||
// charged the larger of both, and a stored entry must declare equal sizes.
|
||||
const LIMITS = {archiveBytes: 256 * MiB, entries: 2000, files: 256, exportBytes: 32 * MiB, requestBytes: 32 * MiB};
|
||||
// Only these files of an export are read; listening history stays in the browser.
|
||||
const EXPORT_FILES = {spotify: /(^|\/)(Playlist\d*|YourLibrary)\.json$/};
|
||||
@@ -624,13 +625,16 @@ document.addEventListener('alpine:init', () => {
|
||||
return fflateLoading;
|
||||
},
|
||||
// Extracts only the export's playlist files. Archive entries, files and
|
||||
// declared sizes are limited before anything is decompressed.
|
||||
// the sizes fflate allocates for them are limited before anything is
|
||||
// decompressed; unknown methods, duplicates and stored entries whose
|
||||
// sizes disagree are refused.
|
||||
async readArchive(file, pattern, budget) {
|
||||
if (file.size > LIMITS.archiveBytes) throw new Error(this.text('archiveTooLarge'));
|
||||
const fflate = await this.zipLibrary();
|
||||
const bytes = new Uint8Array(await file.arrayBuffer());
|
||||
let entries = 0;
|
||||
let problem = '';
|
||||
const names = new Set();
|
||||
const extracted = await new Promise((resolve, reject) => {
|
||||
try {
|
||||
fflate.unzip(bytes, {filter: entry => {
|
||||
@@ -638,9 +642,17 @@ document.addEventListener('alpine:init', () => {
|
||||
if (problem) return false;
|
||||
if (entries > LIMITS.entries) problem = 'zipTooManyEntries';
|
||||
if (problem || !pattern.test(entry.name)) return false;
|
||||
const packed = Number(entry.size);
|
||||
const size = Number(entry.originalSize);
|
||||
const valid = [packed, size].every(n => Number.isFinite(n) && n >= 0)
|
||||
&& (entry.compression === 8 || (entry.compression === 0 && packed === size));
|
||||
if (!valid || names.has(entry.name)) {
|
||||
problem = 'zipInvalid';
|
||||
return false;
|
||||
}
|
||||
names.add(entry.name);
|
||||
budget.files += 1;
|
||||
budget.bytes += Number.isFinite(size) && size >= 0 ? size : Infinity;
|
||||
budget.bytes += Math.max(packed, size);
|
||||
if (budget.files > LIMITS.files) problem = 'zipTooManyFiles';
|
||||
else if (budget.bytes > LIMITS.exportBytes) problem = 'exportTooLarge';
|
||||
return !problem;
|
||||
|
||||
@@ -108,6 +108,66 @@ test('archive, entry, file and declared size limits stop a ZIP before anything i
|
||||
same(store.wizard.files, []);
|
||||
});
|
||||
|
||||
// A stored entry whose central directory records may all point at the one
|
||||
// local header, each with its own name and declared sizes.
|
||||
function storedZip(data, records) {
|
||||
const local = Buffer.alloc(30 + 14);
|
||||
local.writeUInt32LE(0x04034b50, 0);
|
||||
local.writeUInt16LE(20, 4);
|
||||
local.writeUInt32LE(data.length, 18);
|
||||
local.writeUInt32LE(data.length, 22);
|
||||
local.writeUInt16LE(14, 26);
|
||||
local.write('Playlist1.json', 30);
|
||||
const central = records.map(({name, size = data.length, originalSize = data.length, compression = 0}) => {
|
||||
const record = Buffer.alloc(46 + name.length);
|
||||
record.writeUInt32LE(0x02014b50, 0);
|
||||
record.writeUInt16LE(20, 4);
|
||||
record.writeUInt16LE(20, 6);
|
||||
record.writeUInt16LE(compression, 10);
|
||||
record.writeUInt32LE(size, 20);
|
||||
record.writeUInt32LE(originalSize, 24);
|
||||
record.writeUInt16LE(name.length, 28);
|
||||
record.write(name, 46);
|
||||
return record;
|
||||
});
|
||||
const directory = Buffer.concat(central);
|
||||
const end = Buffer.alloc(22);
|
||||
end.writeUInt32LE(0x06054b50, 0);
|
||||
end.writeUInt16LE(records.length, 8);
|
||||
end.writeUInt16LE(records.length, 10);
|
||||
end.writeUInt32LE(directory.length, 12);
|
||||
end.writeUInt32LE(local.length + data.length, 16);
|
||||
return new File([Buffer.concat([local, data, directory, end])], 'stored.zip', {type: 'application/zip'});
|
||||
}
|
||||
|
||||
test('stored and overlapping entries are charged what fflate copies for them', async () => {
|
||||
const {store, calls} = setup(() => preview([playlist('a')]));
|
||||
store.chooseMethod('spotify', 'file');
|
||||
const data = Buffer.from(JSON.stringify({playlists: []}).padEnd(MiB, ' '));
|
||||
// A stored entry is copied by its compressed size, whatever it declares.
|
||||
await store.readFiles([storedZip(data, [{name: 'Playlist1.json', originalSize: 0}])]);
|
||||
assert.equal(store.wizard.error, 'player_import_zip_invalid');
|
||||
// Many records of one stored entry, each declaring nothing, are a bomb.
|
||||
const names = Array.from({length: 10}, (_, i) => ({name: `Playlist${i + 1}.json`, originalSize: 0}));
|
||||
await store.readFiles([storedZip(data, names)]);
|
||||
assert.equal(store.wizard.error, 'player_import_zip_invalid');
|
||||
// Honest records of one entry still add up against the export limit.
|
||||
const honest = Array.from({length: 40}, (_, i) => ({name: `Playlist${i + 1}.json`}));
|
||||
await store.readFiles([storedZip(data, honest)]);
|
||||
assert.equal(store.wizard.error, 'player_import_export_too_large');
|
||||
// Duplicate names and unknown methods are refused.
|
||||
await store.readFiles([storedZip(data, [{name: 'Playlist1.json'}, {name: 'Playlist1.json'}])]);
|
||||
assert.equal(store.wizard.error, 'player_import_zip_invalid');
|
||||
await store.readFiles([storedZip(data, [{name: 'Playlist1.json', compression: 12}])]);
|
||||
assert.equal(store.wizard.error, 'player_import_zip_invalid');
|
||||
assert.equal(calls.length, 0);
|
||||
// A valid stored entry is read.
|
||||
await store.readFiles([storedZip(data, [{name: 'Playlist1.json'}])]);
|
||||
assert.equal(store.wizard.error, '');
|
||||
assert.equal(calls.length, 1);
|
||||
same(calls[0].body.files.map(file => file.name), ['Playlist1.json']);
|
||||
});
|
||||
|
||||
test('unzipped JSON files are accepted and an oversized request is refused in the browser', async () => {
|
||||
const {store, calls} = setup(() => preview([playlist('a')]));
|
||||
store.chooseMethod('spotify', 'file');
|
||||
|
||||
Reference in new issue
Block a user