|
| 1 | +import 'dart:convert'; |
| 2 | +import 'dart:io'; |
| 3 | + |
| 4 | +import 'package:archive/archive.dart'; |
| 5 | +import 'package:flutter_test/flutter_test.dart'; |
| 6 | +import 'package:path/path.dart' as p; |
| 7 | +import 'package:querya_desktop/core/security/safe_zip_extractor.dart'; |
| 8 | + |
| 9 | +const _tightLimits = ZipDecodeLimits( |
| 10 | + maxCompressedBytes: 4096, |
| 11 | + maxTotalUncompressedBytes: 8192, |
| 12 | + maxEntryCount: 5, |
| 13 | + maxEntryUncompressedBytes: 4096, |
| 14 | + maxCompressionRatio: 10, |
| 15 | +); |
| 16 | + |
| 17 | +Archive _singleFileArchive(String name, List<int> content) { |
| 18 | + return Archive()..addFile(ArchiveFile(name, content.length, content)); |
| 19 | +} |
| 20 | + |
| 21 | +Future<File> _writeZip(Directory dir, Archive archive, String name) async { |
| 22 | + final bytes = ZipEncoder().encode(archive); |
| 23 | + final file = File(p.join(dir.path, name)); |
| 24 | + await file.writeAsBytes(bytes); |
| 25 | + return file; |
| 26 | +} |
| 27 | + |
| 28 | +void main() { |
| 29 | + group('SafeZipExtractor', () { |
| 30 | + late Directory tempDir; |
| 31 | + |
| 32 | + setUp(() async { |
| 33 | + tempDir = await Directory.systemTemp.createTemp('querya_safe_zip_'); |
| 34 | + }); |
| 35 | + |
| 36 | + tearDown(() async { |
| 37 | + if (await tempDir.exists()) { |
| 38 | + await tempDir.delete(recursive: true); |
| 39 | + } |
| 40 | + }); |
| 41 | + |
| 42 | + test('decodes a small valid archive', () { |
| 43 | + final archive = _singleFileArchive('hello.txt', utf8.encode('hello')); |
| 44 | + final zipBytes = ZipEncoder().encode(archive); |
| 45 | + |
| 46 | + final decoded = SafeZipExtractor.decodeBytes(zipBytes, limits: _tightLimits); |
| 47 | + expect(decoded.length, 1); |
| 48 | + expect(decoded.first.name, 'hello.txt'); |
| 49 | + }); |
| 50 | + |
| 51 | + test('rejects archives exceeding max compressed bytes', () async { |
| 52 | + final file = File(p.join(tempDir.path, 'oversize.zip')); |
| 53 | + await file.writeAsBytes(List<int>.filled(5000, 1)); |
| 54 | + |
| 55 | + expect( |
| 56 | + () => SafeZipExtractor.readBoundedBytes(file, limits: _tightLimits), |
| 57 | + throwsA(isA<SafeZipException>().having( |
| 58 | + (e) => e.message, |
| 59 | + 'message', |
| 60 | + contains('maximum compressed size'), |
| 61 | + )), |
| 62 | + ); |
| 63 | + }); |
| 64 | + |
| 65 | + test('rejects archives with too many entries', () { |
| 66 | + final archive = Archive(); |
| 67 | + for (var i = 0; i < 6; i++) { |
| 68 | + archive.addFile(ArchiveFile('file$i.txt', 1, [i])); |
| 69 | + } |
| 70 | + final zipBytes = ZipEncoder().encode(archive); |
| 71 | + |
| 72 | + expect( |
| 73 | + () => SafeZipExtractor.decodeBytes(zipBytes, limits: _tightLimits), |
| 74 | + throwsA(isA<SafeZipException>().having( |
| 75 | + (e) => e.message, |
| 76 | + 'message', |
| 77 | + contains('too many entries'), |
| 78 | + )), |
| 79 | + ); |
| 80 | + }); |
| 81 | + |
| 82 | + test('rejects archives exceeding total uncompressed size', () { |
| 83 | + const limits = ZipDecodeLimits( |
| 84 | + maxCompressedBytes: 4096, |
| 85 | + maxTotalUncompressedBytes: 6000, |
| 86 | + maxEntryCount: 5, |
| 87 | + maxEntryUncompressedBytes: 5000, |
| 88 | + maxCompressionRatio: 100, |
| 89 | + ); |
| 90 | + final archive = Archive() |
| 91 | + ..addFile(ArchiveFile('a.bin', 4000, List<int>.filled(4000, 1))) |
| 92 | + ..addFile(ArchiveFile('b.bin', 4000, List<int>.filled(4000, 2))); |
| 93 | + final zipBytes = ZipEncoder().encode(archive); |
| 94 | + |
| 95 | + expect( |
| 96 | + () => SafeZipExtractor.decodeBytes(zipBytes, limits: limits), |
| 97 | + throwsA(isA<SafeZipException>().having( |
| 98 | + (e) => e.message, |
| 99 | + 'message', |
| 100 | + contains('total uncompressed size'), |
| 101 | + )), |
| 102 | + ); |
| 103 | + }); |
| 104 | + |
| 105 | + test('rejects high compression ratio zip bombs', () { |
| 106 | + const limits = ZipDecodeLimits( |
| 107 | + maxCompressedBytes: 4096, |
| 108 | + maxTotalUncompressedBytes: 8192, |
| 109 | + maxEntryCount: 5, |
| 110 | + maxEntryUncompressedBytes: 10000, |
| 111 | + maxCompressionRatio: 10, |
| 112 | + ); |
| 113 | + final payload = List<int>.filled(5000, 0); |
| 114 | + final archive = _singleFileArchive('bomb.bin', payload); |
| 115 | + final zipBytes = ZipEncoder().encode(archive); |
| 116 | + |
| 117 | + expect( |
| 118 | + () => SafeZipExtractor.decodeBytes(zipBytes, limits: limits), |
| 119 | + throwsA(isA<SafeZipException>().having( |
| 120 | + (e) => e.message, |
| 121 | + 'message', |
| 122 | + contains('compression ratio'), |
| 123 | + )), |
| 124 | + ); |
| 125 | + }); |
| 126 | + |
| 127 | + test('readAndDecodeFile reads bounded archives from disk', () async { |
| 128 | + final archive = _singleFileArchive('ok.txt', utf8.encode('ok')); |
| 129 | + final zipFile = await _writeZip(tempDir, archive, 'ok.zip'); |
| 130 | + |
| 131 | + final decoded = |
| 132 | + await SafeZipExtractor.readAndDecodeFile(zipFile, limits: _tightLimits); |
| 133 | + expect(decoded.first.name, 'ok.txt'); |
| 134 | + }); |
| 135 | + }); |
| 136 | +} |
0 commit comments