|
| 1 | +import JSZip from 'jszip'; |
| 2 | + |
| 3 | +import { isZipFile } from './guard'; |
| 4 | + |
| 5 | +const DEFAULT_MAX_FILE_COUNT = 10000; |
| 6 | +const DEFAULT_MAX_UNZIP_SIZE_BYTES = 1e9; // 1GB |
| 7 | +const DEFAULT_MAX_ZIP_RECURSION_DEPTH = Infinity; |
| 8 | + |
| 9 | +/** |
| 10 | + * Provides an interface to safely read all files from a zip. |
| 11 | + * |
| 12 | + * Ensure `jszip` is installed as a dependency to use. |
| 13 | + */ |
| 14 | +export interface ZipReaderConfig { |
| 15 | + /** |
| 16 | + * Provides protection from zip bombs by only reading up to this many files. |
| 17 | + * Will throw an error if the zip file exceeds this count. |
| 18 | + */ |
| 19 | + maxFileCount: number; |
| 20 | + /** |
| 21 | + * Provides protection from zip bombs by only reading up to this many bytes total. |
| 22 | + * Will throw an error if the total bytes exceeds this count. |
| 23 | + */ |
| 24 | + maxUnzipSizeBytes: number; |
| 25 | + /** |
| 26 | + * If the zip file contains zip files, they will be extracted up to this many deep. |
| 27 | + * If the depth exceeds this number, then these deeper zip files will be returned instead of extracted. |
| 28 | + * |
| 29 | + * `0` means do not recurse. |
| 30 | + */ |
| 31 | + maxZipRecursionDepth: number; |
| 32 | +} |
| 33 | + |
| 34 | +export interface ZipEntry { |
| 35 | + name: string; |
| 36 | + content: Uint8Array; |
| 37 | +} |
| 38 | + |
| 39 | +export class ZipReader { |
| 40 | + private readonly config: ZipReaderConfig; |
| 41 | + |
| 42 | + public constructor(private readonly zip: JSZip, config: Partial<ZipReaderConfig> = {}) { |
| 43 | + this.config = { |
| 44 | + maxFileCount: DEFAULT_MAX_FILE_COUNT, |
| 45 | + maxUnzipSizeBytes: DEFAULT_MAX_UNZIP_SIZE_BYTES, |
| 46 | + maxZipRecursionDepth: DEFAULT_MAX_ZIP_RECURSION_DEPTH, |
| 47 | + ...config, |
| 48 | + }; |
| 49 | + } |
| 50 | + |
| 51 | + public async *getFiles(): AsyncGenerator<ZipEntry> { |
| 52 | + yield* this.getFilesRecursively(this.zip, this.config.maxZipRecursionDepth); |
| 53 | + } |
| 54 | + |
| 55 | + private async *getFilesRecursively(zip: JSZip, maxDepth: number, currentDepth = 0): AsyncGenerator<ZipEntry> { |
| 56 | + const objects = zip.filter(() => true); |
| 57 | + |
| 58 | + let fileCount = 0; |
| 59 | + let totalSize = 0; |
| 60 | + |
| 61 | + // eslint-disable-next-line no-restricted-syntax |
| 62 | + for (const obj of objects) { |
| 63 | + if (obj.dir) continue; |
| 64 | + |
| 65 | + fileCount += 1; |
| 66 | + if (fileCount > this.config.maxFileCount) { |
| 67 | + throw new Error(`File count exceeded maximum (${this.config.maxFileCount})`); |
| 68 | + } |
| 69 | + |
| 70 | + // eslint-disable-next-line no-await-in-loop |
| 71 | + const content = await obj.async('uint8array'); |
| 72 | + |
| 73 | + totalSize += content.byteLength; |
| 74 | + if (totalSize > this.config.maxUnzipSizeBytes) { |
| 75 | + throw new Error(`Total file size exceeded maximum (${this.config.maxUnzipSizeBytes} bytes)`); |
| 76 | + } |
| 77 | + |
| 78 | + if (currentDepth < maxDepth && isZipFile(content)) { |
| 79 | + // eslint-disable-next-line no-await-in-loop |
| 80 | + const zip = await JSZip.loadAsync(content); |
| 81 | + yield* this.getFilesRecursively(zip, maxDepth, currentDepth + 1); |
| 82 | + } else { |
| 83 | + yield { |
| 84 | + name: obj.name, |
| 85 | + content, |
| 86 | + }; |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments