Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

fix: keep media ZIP exports within import limits

ammann525b586cb842
diff --git a/frontend/src/lib/backup-media.test.js b/frontend/src/lib/backup-media.test.js
index d7221406e..7c356e36f 100644
--- a/frontend/src/lib/backup-media.test.js
+++ b/frontend/src/lib/backup-media.test.js
@@ -30,6 +30,23 @@ describe('export with photos & videos', () => {
expect(entries.map(e => e.name)).toEqual([BACKUP_JSON, `media/${main.poster.hash}.png`, `media/${main.hash}.jpg`, `media/${vid.hash}.mov`, 'README.txt'])
expect(JSON.parse(await entries[0].blob.text())).toEqual(JSON.parse(JSON.stringify(S, null, 2)))
})
+
+ it('only exports archives the import path accepts at the entry limit', async () => {
+ const media = createMediaStore(memoryBackend())
+ const S = stateWith([])
+ for (let i = 1; i <= 1999; i++) {
+ const bytes = png(i, 1)
+ const hash = await sha256Hex(bytes)
+ await media.put(hash, new Blob([bytes]), { mime: 'image/png' })
+ S.customEx.push({ id: `e${i}`, media: { kind: 'image', hash, mime: 'image/png', size: bytes.length, width: i, height: 1, at: 1 } })
+ if (i === 1998) {
+ const out = await exportBackupZip(S, { media })
+ expect(out).toMatchObject({ included: 1998, missing: 0 })
+ expect((await readBackupFile(new File([out.blob], 'backup.zip'))).files).toHaveLength(1998)
+ }
+ }
+ await expect(exportBackupZip(S, { media })).rejects.toMatchObject({ code: 'too-many' })
+ })
})
describe('import', () => {
diff --git a/frontend/src/lib/zip.js b/frontend/src/lib/zip.js
index 9993efc27..abb284050 100644
--- a/frontend/src/lib/zip.js
+++ b/frontend/src/lib/zip.js
@@ -9,8 +9,8 @@
*
* What readZip refuses, and why: any compression method but "stored" (this app never writes
* one — an archiver repacked the file), encryption, more than 2000 entries, a name with '..' or a
- * leading '/', and more than 1 GB in total. ZIP64 is neither written nor read: an export over
- * 4 GB or 65,535 files is refused rather than written broken.
+ * leading '/', and more than 1 GiB of entry data. zipStore uses the same entry and data limits,
+ * so it cannot export a backup this app would refuse to import. ZIP64 is neither written nor read.
*/
export class ZipError extends Error {
@@ -45,7 +45,8 @@ async function crcOfBlob(blob) {
}
const LIMIT_32 = 0xffffffff
-const MAX_ENTRIES_WRITE = 65535
+export const MAX_ENTRIES = 2000
+export const MAX_TOTAL = 1024 * 1024 * 1024
function dosTime(d) {
return {
@@ -56,20 +57,23 @@ function dosTime(d) {
/**
* A store-only zip of `entries` ([{ name, blob }]) as one Blob. Names are written as UTF-8 (flag
- * bit 11), so a name any archiver shows as written. Throws ZipError('too-large') past what a zip
- * without ZIP64 can hold.
+ * bit 11), so a name any archiver shows as written. Refuses more entries or data than readZip
+ * accepts, and anything needing ZIP64.
*/
export async function zipStore(entries, { now = new Date() } = {}) {
- if (entries.length > MAX_ENTRIES_WRITE) throw new ZipError('too-large')
+ if (entries.length > MAX_ENTRIES) throw new ZipError('too-many')
const enc = new TextEncoder()
const { time, date } = dosTime(now)
const parts = []
const central = []
let offset = 0
+ let total = 0
for (const { name, blob } of entries) {
const nameBytes = enc.encode(name)
const size = blob.size
if (size > LIMIT_32) throw new ZipError('too-large')
+ total += size
+ if (total > MAX_TOTAL) throw new ZipError('too-large')
const crc = await crcOfBlob(blob)
const local = new DataView(new ArrayBuffer(30))
local.setUint32(0, 0x04034b50, true)
@@ -111,8 +115,6 @@ export async function zipStore(entries, { now = new Date() } = {}) {
return new Blob([...parts, ...central, end.buffer], { type: 'application/zip' })
}
-export const MAX_ENTRIES = 2000
-export const MAX_TOTAL = 1024 * 1024 * 1024
const EOCD_SEARCH = 65557 // 22-byte record + the largest possible comment
const readBytes = async (file, start, end) => new Uint8Array(await file.slice(start, end).arrayBuffer())
diff --git a/frontend/src/lib/zip.test.js b/frontend/src/lib/zip.test.js
index 234aa9106..d9bae4a50 100644
--- a/frontend/src/lib/zip.test.js
+++ b/frontend/src/lib/zip.test.js
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest'
-import { zipStore, readZip, looksLikeZip, crc32, ZipError, MAX_ENTRIES } from './zip.js'
+import { zipStore, readZip, looksLikeZip, crc32, ZipError, MAX_ENTRIES, MAX_TOTAL } from './zip.js'
const text = async blob => new TextDecoder().decode(new Uint8Array(await blob.arrayBuffer()))
const u8 = async blob => new Uint8Array(await blob.arrayBuffer())
@@ -60,4 +60,9 @@ describe('zip', () => {
expect(await refusal(handZip([{ name: 'a.txt', data }], { count: MAX_ENTRIES + 1 }))).toBe('too-many')
expect(await refusal(new Blob(['not a zip at all']))).toBe('not-zip')
})
+
+ it('refuses an export above the import size limit before reading the file', async () => {
+ const blob = { size: MAX_TOTAL + 1, slice: () => { throw new Error('read past the size guard') } }
+ await expect(zipStore([{ name: 'media/large.mp4', blob }])).rejects.toMatchObject({ code: 'too-large' })
+ })
})