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;
|
||||
|
||||
Reference in new issue
Block a user