SPB Git

Repository Public

ZipTests.swift 16.3 KB 320 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : ORIX Sheet9// File             : Packages/SheetIO/Tests/SheetIOTests/ZipTests.swift10// Module           : SheetIOTests11// Repository       : https://git.spboucher.ai/orix-sheet12// Created          : 2026-11-2113// Last Modified    : 2026-11-2114//15// =============================================================================16// AUTHOR & PROFESSIONAL AFFILIATION17// =============================================================================18//19// Author           : Simon-Pierre Boucher20// Position         : Professor of Finance21// Institution      : Université du Québec en Outaouais (UQO)22// Department       : Département des sciences administratives23// Organization     : SP Boucher24//25// Website          : https://spboucher.ai26// Git              : https://git.spboucher.ai27//28// =============================================================================29// OFFICIAL CONTACT DIRECTORY30// =============================================================================31//32// General          : info@spboucher.ai33// Professional     : contact@spboucher.ai34// Direct           : simon@spboucher.ai35// Research         : research@spboucher.ai36// Development      : dev@spboucher.ai37// Support          : support@spboucher.ai38// Security/Admin   : admin@spboucher.ai39// Notifications    : notifications@spboucher.ai40//41// Preferred Contact: support@spboucher.ai42//43// =============================================================================44// COPYRIGHT & INTELLECTUAL PROPERTY45// =============================================================================46//47// Copyright (c) 202648// Simon-Pierre Boucher49//50// All rights reserved where applicable.51//52// This file is governed by the applicable project license,53// copyright notices, institutional agreements and relevant law.54//55// Unless expressly authorized by the applicable license or56// rights holder, copying, redistribution, sublicensing,57// publication and commercial exploitation are not permitted.58//59// Existing third-party rights and notices remain unaffected.60//61// =============================================================================62// CONFIDENTIALITY & SECURITY63// =============================================================================64//65// Classification   : Public66// Distribution     : Public repository; use is governed by the project license67// Security Contact : admin@spboucher.ai68//69// Access to this source does not independently grant permission70// to redistribute or disclose confidential materials.71//72// Do not embed credentials, private keys, access tokens,73// certificates or confidential personal information.74//75// =============================================================================76// LICENSE & MAINTENANCE77// =============================================================================78//79// License          : None specified (no license file in this repository)80// Maintainer       : Simon-Pierre Boucher81// Technical Contact: dev@spboucher.ai82//83// =============================================================================84// END OF SP BOUCHER SOURCE HEADER85// =============================================================================8687import Foundation88import Testing89@testable import SheetIO9091/// Builds deliberately unusual or malformed archives byte by byte.92struct RawZip {93    struct Entry {94        var name: String95        var data: Data96        var method: UInt16 = 097        var flags: UInt16 = 0x080098        var declaredSize: UInt32?99        var declaredCRC: UInt32?100        var payload: Data?101        var dataDescriptor = false102        var zip64 = false103        var offsetOverride: UInt32?104    }105    var entries: [Entry] = []106    var zip64End = false107    var declaredCount: UInt16?108109    func build() -> Data {110        var out = Data()111        var central = Data()112        for e in entries {113            let name = Array(e.name.utf8)114            let payload = e.payload ?? e.data115            let crc = e.declaredCRC ?? Deflate.crc32(e.data)116            let usize = e.declaredSize ?? UInt32(e.data.count)117            let offset = UInt32(out.count)118            var flags = e.flags119            if e.dataDescriptor { flags |= 8 }120            out.le32(0x0403_4b50); out.le16(20); out.le16(flags); out.le16(e.method); out.le16(0); out.le16(0x21)121            if e.dataDescriptor { out.le32(0); out.le32(0); out.le32(0) }122            else { out.le32(crc); out.le32(UInt32(payload.count)); out.le32(usize) }123            out.le16(UInt16(name.count)); out.le16(0)124            out.append(contentsOf: name)125            out.append(payload)126            if e.dataDescriptor { out.le32(0x0807_4b50); out.le32(crc); out.le32(UInt32(payload.count)); out.le32(usize) }127            central.le32(0x0201_4b50); central.le16(45); central.le16(45); central.le16(flags); central.le16(e.method); central.le16(0); central.le16(0x21)128            central.le32(crc)129            if e.zip64 { central.le32(0xFFFF_FFFF); central.le32(0xFFFF_FFFF) } else { central.le32(UInt32(payload.count)); central.le32(usize) }130            central.le16(UInt16(name.count)); central.le16(e.zip64 ? 28 : 0); central.le16(0); central.le16(0); central.le16(0); central.le32(0)131            central.le32(e.zip64 ? 0xFFFF_FFFF : (e.offsetOverride ?? offset))132            central.append(contentsOf: name)133            if e.zip64 { central.le16(1); central.le16(24); central.le64(UInt64(usize)); central.le64(UInt64(payload.count)); central.le64(UInt64(offset)) }134        }135        let cdOffset = out.count136        out.append(central)137        let count = entries.count138        if zip64End {139            let z = out.count140            out.le32(0x0606_4b50); out.le64(44); out.le16(45); out.le16(45); out.le32(0); out.le32(0)141            out.le64(UInt64(count)); out.le64(UInt64(count)); out.le64(UInt64(central.count)); out.le64(UInt64(cdOffset))142            out.le32(0x0706_4b50); out.le32(0); out.le64(UInt64(z)); out.le32(1)143            out.le32(0x0605_4b50); out.le16(0); out.le16(0); out.le16(0xFFFF); out.le16(0xFFFF); out.le32(0xFFFF_FFFF); out.le32(0xFFFF_FFFF); out.le16(0)144        } else {145            out.le32(0x0605_4b50); out.le16(0); out.le16(0); out.le16(declaredCount ?? UInt16(count)); out.le16(declaredCount ?? UInt16(count))146            out.le32(UInt32(central.count)); out.le32(UInt32(cdOffset)); out.le16(0)147        }148        return out149    }150}151152private func expectError(_ kind: String, _ body: () throws -> Void, sourceLocation: SourceLocation = #_sourceLocation) {153    do { try body(); Issue.record("expected \(kind) error", sourceLocation: sourceLocation) }154    catch let e as SheetIOError {155        let name: String156        switch e {157        case .unsafeArchive: name = "unsafe"158        case .corrupted: name = "corrupted"159        case .unsupported: name = "unsupported"160        case .tooLarge: name = "tooLarge"161        default: name = "other"162        }163        #expect(kind.split(separator: "|").contains(Substring(name)), "got \(e)", sourceLocation: sourceLocation)164        #expect(e.errorDescription?.isEmpty == false, sourceLocation: sourceLocation)165    } catch { Issue.record("unexpected error type \(error)", sourceLocation: sourceLocation) }166}167168@Suite struct ZipTests {169    static func sample() throws -> (Data, [String: Data]) {170        var w = ZipWriter()171        var content = [String: Data]()172        content["empty.txt"] = Data()173        content["short.txt"] = Data("hello".utf8)174        content["dir/été/日本.xml"] = Data(String(repeating: "<row>été 日本</row>", count: 2000).utf8)175        var rng = SplitMix64(seed: 7)176        content["random.bin"] = Data((0..<50_000).map { _ in UInt8.random(in: 0...255, using: &rng) })177        content["zeros.bin"] = Data(count: 300_000)178        for name in content.keys.sorted() { try w.add(name, content[name]!) }179        return (w.finish(), content)180    }181182    @Test func writerAndReaderRoundTrip() throws {183        let (data, content) = try Self.sample()184        let zip = try ZipArchive(data: data)185        #expect(Set(zip.names) == Set(content.keys))186        for (name, expected) in content { #expect(try zip.data(for: name) == expected, "\(name)") }187        #expect(zip.entry("zeros.bin")?.method == 8 && zip.entry("short.txt")?.method == 0)188        #expect(zip.entry("ZEROS.BIN") != nil)        // OPC part names are case-insensitive189        expectError("corrupted") { _ = try zip.data(for: "missing") }190        // the system unzip accepts the archive191        let dir = Scratch.dir("zip")192        defer { try? FileManager.default.removeItem(at: dir) }193        let file = dir.appendingPathComponent("a.zip")194        try data.write(to: file)195        #expect(try Tools.run("/usr/bin/unzip", ["-tq", file.path]).status == 0)196    }197198    @Test func rejectsDangerousEntryNames() {199        for bad in ["../evil.txt", "/etc/passwd", "a/../../b", "dir\\file", "C:/windows", "a//b", "./a", "a/./b", "nul\0byte", ""] {200            let raw = RawZip(entries: [.init(name: bad, data: Data("x".utf8))]).build()201            expectError("unsafe") { _ = try ZipArchive(data: raw) }202        }203        #expect(throws: SheetIOError.self) { var w = ZipWriter(); try w.add("../x", Data()) }204    }205206    @Test func enforcesLimits() throws {207        var limits = ZipLimits()208        limits.maxEntries = 3209        let many = RawZip(entries: (0..<4).map { .init(name: "f\($0)", data: Data("x".utf8)) }).build()210        expectError("unsafe") { _ = try ZipArchive(data: many, limits: limits) }211        // an EOCD claiming far more entries than the directory can hold212        var lying = RawZip(entries: [.init(name: "a", data: Data("x".utf8))])213        lying.declaredCount = 60_000214        expectError("corrupted") { _ = try ZipArchive(data: lying.build()) }215216        limits = ZipLimits(); limits.maxEntrySize = 1000217        var w = ZipWriter(); try w.add("big", Data(count: 5000))218        let big = w.finish()219        expectError("unsafe") { _ = try ZipArchive(data: big, limits: limits) }220221        limits = ZipLimits(); limits.maxTotalSize = 8000222        w = ZipWriter(); try w.add("a", Data(count: 5000)); try w.add("b", Data(count: 5000))223        let total = w.finish()224        expectError("unsafe") { _ = try ZipArchive(data: total, limits: limits) }225226        // compression bomb: 8 MiB of zeros deflate to a few kilobytes227        limits = ZipLimits(); limits.ratioFloor = 1 << 20; limits.maxCompressionRatio = 100228        w = ZipWriter(); try w.add("bomb", Data(count: 8 << 20))229        let bomb = w.finish()230        #expect(bomb.count < 20_000)231        expectError("unsafe") { _ = try ZipArchive(data: bomb, limits: limits) }232        #expect(try ZipArchive(data: bomb).data(for: "bomb").count == 8 << 20)      // allowed by the defaults233    }234235    @Test func declaredSizesAreNotTrusted() throws {236        let payload = try Deflate.compress(Data(count: 1 << 20))237        // declares 10 bytes, really inflates to 1 MiB: must stop instead of allocating238        let small = RawZip(entries: [.init(name: "a", data: Data(count: 1 << 20), method: 8, declaredSize: 10, payload: payload)]).build()239        expectError("unsafe|corrupted") { _ = try ZipArchive(data: small).data(for: "a") }240        // declares more than it contains241        let large = RawZip(entries: [.init(name: "a", data: Data(count: 1 << 20), method: 8, declaredSize: 2 << 20, payload: payload)]).build()242        expectError("corrupted") { _ = try ZipArchive(data: large).data(for: "a") }243        let badCRC = RawZip(entries: [.init(name: "a", data: Data("hello".utf8), declaredCRC: 0xDEADBEEF)]).build()244        expectError("corrupted") { _ = try ZipArchive(data: badCRC).data(for: "a") }245        let garbage = RawZip(entries: [.init(name: "a", data: Data("hello".utf8), method: 8, payload: Data([0xFF, 0xFE, 0x00, 0x12, 0x34]))]).build()246        expectError("corrupted") { _ = try ZipArchive(data: garbage).data(for: "a") }247        let method = RawZip(entries: [.init(name: "a", data: Data("hello".utf8), method: 14)]).build()248        expectError("unsupported") { _ = try ZipArchive(data: method) }249        let encrypted = RawZip(entries: [.init(name: "a", data: Data("hello".utf8), flags: 0x0801)]).build()250        expectError("unsupported") { _ = try ZipArchive(data: encrypted) }251    }252253    @Test func rejectsOverlapsAndDuplicates() {254        let dup = RawZip(entries: [.init(name: "a", data: Data("1".utf8)), .init(name: "a", data: Data("2".utf8))]).build()255        expectError("unsafe") { _ = try ZipArchive(data: dup) }256        // two directory entries pointing at the same local header (overlapping-file bomb)257        let overlap = RawZip(entries: [.init(name: "a", data: Data(count: 100)), .init(name: "b", data: Data(count: 100), offsetOverride: 0)]).build()258        expectError("unsafe|corrupted") { _ = try ZipArchive(data: overlap) }259    }260261    @Test func readsZip64AndDataDescriptors() throws {262        var raw = RawZip(entries: [.init(name: "big64.txt", data: Data("zip64 content".utf8), zip64: true),263                                   .init(name: "desc.txt", data: Data("descriptor content".utf8), dataDescriptor: true)])264        raw.zip64End = true265        let zip = try ZipArchive(data: raw.build())266        #expect(try zip.data(for: "big64.txt") == Data("zip64 content".utf8))267        #expect(try zip.data(for: "desc.txt") == Data("descriptor content".utf8))268    }269270    @Test func truncatedAndCorruptedArchivesFailCleanly() throws {271        let (data, _) = try Self.sample()272        // every truncation point in the tail plus a sample of the body273        var cuts = Array(stride(from: 0, to: data.count, by: max(1, data.count / 400)))274        cuts += Array(max(0, data.count - 300)..<data.count)275        for cut in cuts {276            do {277                let zip = try ZipArchive(data: data.prefix(cut))278                for e in zip.entries { _ = try zip.data(for: e) }279                Issue.record("a truncated archive (\(cut) of \(data.count) bytes) was read without error")280            } catch is SheetIOError {} catch { Issue.record("unexpected error \(error)") }281        }282        // random byte flips: any outcome but a crash or a non-SheetIO error283        var rng = SplitMix64(seed: 99)284        var rejected = 0285        for _ in 0..<400 {286            var d = data287            for _ in 0..<Int.random(in: 1...6, using: &rng) {288                // bias towards the directory at the end, where the structure lives289                let i = Bool.random(using: &rng) ? Int.random(in: max(0, d.count - 400)..<d.count, using: &rng) : Int.random(in: 0..<d.count, using: &rng)290                d[i] = UInt8.random(in: 0...255, using: &rng)291            }292            do {293                let zip = try ZipArchive(data: d)294                for e in zip.entries { _ = try zip.data(for: e) }295            } catch is SheetIOError { rejected += 1 } catch { Issue.record("unexpected error \(error)") }296        }297        #expect(rejected > 100)298        for junk in [Data(), Data("PK".utf8), Data(count: 22), Data("not a zip file at all, just text that is long enough".utf8)] {299            #expect(throws: SheetIOError.self) { _ = try ZipArchive(data: junk) }300        }301    }302303    @Test func extractionStaysInsideTheDestination() throws {304        let (data, content) = try Self.sample()305        let dir = Scratch.dir("extract")306        defer { try? FileManager.default.removeItem(at: dir) }307        let dest = dir.appendingPathComponent("out")308        try ZipArchive(data: data).extractAll(to: dest)309        for (name, expected) in content { #expect(try Data(contentsOf: dest.appendingPathComponent(name)) == expected) }310        // a symbolic link planted in the destination must not redirect writes elsewhere311        let outside = dir.appendingPathComponent("outside")312        try FileManager.default.createDirectory(at: outside, withIntermediateDirectories: true)313        let trap = dir.appendingPathComponent("trap")314        try FileManager.default.createDirectory(at: trap, withIntermediateDirectories: true)315        try FileManager.default.createSymbolicLink(at: trap.appendingPathComponent("dir"), withDestinationURL: outside)316        #expect(throws: SheetIOError.self) { try ZipArchive(data: data).extractAll(to: trap) }317        #expect(try FileManager.default.contentsOfDirectory(atPath: outside.path).isEmpty)318    }319}320
Files main
Menu

Theme