Files
simstudioai--sim/apps/sim/lib/file-parsers/zip-guard.test.ts
T
wehub-resource-sync d25d482dc2
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

124 lines
4.3 KiB
TypeScript

/**
* @vitest-environment node
*/
import JSZip from 'jszip'
import { describe, expect, it } from 'vitest'
import {
assertOoxmlArchiveWithinLimits,
type OoxmlSizeLimits,
ZipBombError,
} from '@/lib/file-parsers/zip-guard'
const HIGH_LIMITS: OoxmlSizeLimits = {
maxTotalUncompressedBytes: 1024 * 1024 * 1024,
maxCompressionRatio: 10_000,
ratioCheckFloorBytes: 1024 * 1024 * 1024,
}
async function buildZip(
entries: Record<string, string>,
options: { comment?: string } = {}
): Promise<Buffer> {
const zip = new JSZip()
for (const [name, content] of Object.entries(entries)) {
zip.file(name, content)
}
return zip.generateAsync({
type: 'nodebuffer',
compression: 'DEFLATE',
comment: options.comment,
})
}
describe('assertOoxmlArchiveWithinLimits', () => {
it('accepts a well-formed archive within limits', async () => {
const buffer = await buildZip({ 'word/document.xml': '<xml>hello world</xml>' })
expect(() => assertOoxmlArchiveWithinLimits(buffer, HIGH_LIMITS)).not.toThrow()
})
it('rejects an archive whose declared expanded size exceeds the absolute cap', async () => {
const buffer = await buildZip({ 'xl/worksheets/sheet1.xml': 'A'.repeat(200_000) })
expect(() =>
assertOoxmlArchiveWithinLimits(buffer, {
maxTotalUncompressedBytes: 100_000,
maxCompressionRatio: 10_000,
ratioCheckFloorBytes: 1024 * 1024 * 1024,
})
).toThrow(ZipBombError)
})
it('rejects an archive whose compression ratio exceeds the limit', async () => {
const buffer = await buildZip({ 'xl/worksheets/sheet1.xml': 'A'.repeat(200_000) })
expect(() =>
assertOoxmlArchiveWithinLimits(buffer, {
maxTotalUncompressedBytes: 1024 * 1024 * 1024,
maxCompressionRatio: 5,
ratioCheckFloorBytes: 1000,
})
).toThrow(ZipBombError)
})
it('does not flag a small but highly compressible archive below the ratio floor', async () => {
const buffer = await buildZip({ 'xl/worksheets/sheet1.xml': 'A'.repeat(200_000) })
expect(() =>
assertOoxmlArchiveWithinLimits(buffer, {
maxTotalUncompressedBytes: 1024 * 1024 * 1024,
maxCompressionRatio: 5,
ratioCheckFloorBytes: 1024 * 1024 * 1024,
})
).not.toThrow()
})
it('sums declared sizes across multiple entries', async () => {
const buffer = await buildZip({
'a.xml': 'A'.repeat(60_000),
'b.xml': 'B'.repeat(60_000),
})
expect(() =>
assertOoxmlArchiveWithinLimits(buffer, {
maxTotalUncompressedBytes: 100_000,
maxCompressionRatio: 10_000,
ratioCheckFloorBytes: 1024 * 1024 * 1024,
})
).toThrow(ZipBombError)
})
it('accepts a well-formed archive that carries a trailing comment', async () => {
const buffer = await buildZip(
{ 'word/document.xml': '<xml>hello</xml>' },
{ comment: 'generated by test' }
)
expect(() => assertOoxmlArchiveWithinLimits(buffer, HIGH_LIMITS)).not.toThrow()
})
it('fails closed for a ZIP-shaped buffer whose central directory is unparseable', () => {
const buffer = Buffer.alloc(64)
buffer.writeUInt32LE(0x04034b50, 0) // local file header signature, no valid EOCD
expect(() => assertOoxmlArchiveWithinLimits(buffer)).toThrow(ZipBombError)
})
it('rejects a decoy EOCD signature that does not validate against the buffer tail', async () => {
const realZip = await buildZip({ 'xl/worksheets/sheet1.xml': 'A'.repeat(200_000) })
// A decoy EOCD (zeroed central directory) appended after the genuine archive
// would, without tail validation, redirect the guard to an empty directory
// and undercount the real entries.
const decoy = Buffer.alloc(64)
decoy.writeUInt32LE(0x06054b50, 0)
const tampered = Buffer.concat([realZip, decoy])
expect(() => assertOoxmlArchiveWithinLimits(tampered)).toThrow(ZipBombError)
})
it('no-ops for buffers that are not ZIP archives', () => {
const plaintext = Buffer.from('this is just plain text, not a zip archive at all')
expect(() => assertOoxmlArchiveWithinLimits(plaintext)).not.toThrow()
})
it('no-ops for buffers too small to contain an EOCD record', () => {
expect(() => assertOoxmlArchiveWithinLimits(Buffer.from('PK'))).not.toThrow()
})
it('no-ops for an empty buffer', () => {
expect(() => assertOoxmlArchiveWithinLimits(Buffer.alloc(0))).not.toThrow()
})
})