SPB Git

Repository Public License

FuzzTests.swift 9.2 KB 173 lines swift

1// ORIX Secure2// File: Packages/ORIXFileInspection/Tests/ORIXFileInspectionTests/FuzzTests.swift3// Description: Robustness: random buffers, bit-flipped and truncated valid samples, and hostile ZIP directories pushed4//              through every parser. Nothing may crash, hang or break the wording rules.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation9import ORIXSecureModel10import Testing11@testable import ORIXFileInspection1213@Suite("Fuzzing")14struct FuzzTests {15    static let fuzzNames = ["a.pdf", "a.zip", "a.docx", "a.png", "a.jpg", "a.tar", "a.gz", "a.rtf", "a.doc", "a.exe", "a", "a.txt", "a.mobileconfig", "a.mp4"]1617    /// Runs every byte-level parser on the data, whatever its detected type.18    func runAllParsers(_ data: [UInt8], name: String, options: InspectionOptions) throws -> Int {19        let source = DataByteSource(data)20        let core = try FileInspector.inspectContent(source: source, name: name, options: options)21        var findings = core.findings22        let zip = try ZipInspector.inspect(source: source, options: options)23        findings += ZipInspector.findings(for: zip.report, facts: zip.facts, container: MagicDetector.refineZip(zip.facts), declaredExtension: "docx", options: options)24        let tar = try TarInspector.inspect(source: source, options: options)25        findings += TarInspector.findings(for: tar, options: options)26        let gzip = try GzipInspector.inspect(source: source)27        findings += GzipInspector.findings(for: gzip, options: options)28        let pdf = try PDFInspector.scan(source: source, options: options)29        findings += PDFInspector.findings(for: pdf, fileSize: source.size).findings30        for type in [DetectedFileType.png, .jpeg, .rtf, .ole2] {31            let structure = try StructureChecks.check(type: type, source: source, options: options)32            findings += structure.findings33        }34        #expect(zip.report.parsedEntryCount <= options.entryLimit)35        #expect(tar.parsedEntryCount <= options.entryLimit)36        #expect(zip.report.entries.count <= options.maxReportedArchiveEntries)37        expectHonest(findings)38        return findings.count39    }4041    @Test("2 000 random buffers through every parser")42    func randomBuffers() throws {43        var generator = SplitMix64(seed: 0x0421_C0DE)44        let options = InspectionOptions()45        let heads = Samples.all.map(\.bytes).filter { !$0.isEmpty }46        var total = 047        for iteration in 0..<2000 {48            let length = iteration % 7 == 0 ? Int.random(in: 0...64, using: &generator) : Int.random(in: 0...4096, using: &generator)49            var data = (0..<length).map { _ in UInt8.random(in: 0...255, using: &generator) }50            // Half of the buffers start with a real signature so that the deeper parsers are reached.51            if iteration % 2 == 0, let head = heads.randomElement(using: &generator) {52                let prefix = Array(head.prefix(Int.random(in: 2...48, using: &generator)))53                data.replaceSubrange(0..<min(prefix.count, data.count), with: prefix)54            }55            // Some buffers end with a ZIP end record whose fields are random.56            if iteration % 5 == 0 {57                data += [0x50, 0x4B, 0x05, 0x06] + (0..<16).map { _ in UInt8.random(in: 0...255, using: &generator) } + [0, 0]58            }59            let name = Self.fuzzNames[iteration % Self.fuzzNames.count]60            total += try runAllParsers(data, name: name, options: options)61        }62        #expect(total > 2000)63    }6465    @Test("Bit-flipped and truncated valid samples through every parser")66    func mutatedSamples() throws {67        var generator = SplitMix64(seed: 0xB17_F11B)68        let options = InspectionOptions()69        var extra: [Sample] = [70            Sample(name: "active.pdf", bytes: PDFTests.active, type: .pdf),71            Sample(name: "zip64.zip", bytes: buildZip([ZipEntrySpec("big", zeros(8), method: 8, declaredCompressed: 5_000_000_000, declaredUncompressed: 9_000_000_000, zip64: true)], zip64Record: true), type: .zip),72            Sample(name: "objects.rtf", bytes: bytes("{\\rtf1 {\\object\\objemb{\\*\\objdata 0105}}}"), type: .rtf),73            Sample(name: "trail.png", bytes: Samples.pngImage(width: 9, height: 9, trailing: zeros(40)), type: .png),74        ]75        extra += Samples.all76        var runs = 077        for sample in extra where !sample.bytes.isEmpty {78            for round in 0..<40 {79                var data = sample.bytes80                switch round % 4 {81                case 0, 1:82                    for _ in 0..<Int.random(in: 1...8, using: &generator) {83                        let index = Int.random(in: 0..<data.count, using: &generator)84                        data[index] ^= UInt8(1 << UInt8.random(in: 0...7, using: &generator))85                    }86                case 2:87                    data = Array(data.prefix(Int.random(in: 0...data.count, using: &generator)))88                default:89                    let index = Int.random(in: 0..<data.count, using: &generator)90                    let fill: [UInt8] = [0xFF, 0x00, 0x7F, 0x80].randomElement(using: &generator).map { [UInt8](repeating: $0, count: 8) } ?? []91                    data.replaceSubrange(index..<min(data.count, index + fill.count), with: fill.prefix(min(fill.count, data.count - index)))92                }93                _ = try runAllParsers(data, name: sample.name, options: options)94                runs += 195            }96        }97        #expect(runs > 3000)98    }99100    @Test("Hostile ZIP directories: every header field randomised")101    func zipDirectories() throws {102        var generator = SplitMix64(seed: 0x21BB_0B)103        var options = InspectionOptions()104        options.maxArchiveEntries = 500105        let base = buildZip((0..<12).map { ZipEntrySpec("dir\($0 % 3)/file\($0).bin", zeros($0)) })106        let zip64 = buildZip((0..<4).map { ZipEntrySpec("f\($0)", zeros(4), zip64: true) }, zip64Record: true)107        for iteration in 0..<1500 {108            var data = iteration % 3 == 0 ? zip64 : base109            let directoryStart = data.count * 2 / 5110            for _ in 0..<Int.random(in: 1...12, using: &generator) {111                let index = Int.random(in: directoryStart..<data.count, using: &generator)112                data[index] = [0x00, 0xFF, 0x50, 0x4B, UInt8.random(in: 0...255, using: &generator)].randomElement(using: &generator) ?? 0113            }114            let inspection = try ZipInspector.inspect(source: DataByteSource(data), options: options)115            #expect(inspection.report.parsedEntryCount <= 500)116            let findings = ZipInspector.findings(for: inspection.report, facts: inspection.facts, container: .zip, declaredExtension: "zip", options: options)117            expectHonest(findings)118        }119    }120121    @Test("File names: random Unicode never breaks the analyzer")122    func randomNames() {123        var generator = SplitMix64(seed: 99)124        let pool: [Unicode.Scalar] = ["a", "Z", ".", " ", "/", "\u{202E}", "\u{200B}", "\u{0435}", "\u{FF0E}", "\u{2024}", "é", "\u{0}", "\u{1F600}", "\u{FE0F}", "\u{200D}", "-", "_", "x", "e", "p", "d", "f"]125        for _ in 0..<3000 {126            var view = String.UnicodeScalarView()127            for _ in 0..<Int.random(in: 0...60, using: &generator) {128                if Bool.random(using: &generator), let scalar = pool.randomElement(using: &generator) {129                    view.append(scalar)130                } else if let scalar = Unicode.Scalar(UInt32.random(in: 0...0x2FFFF, using: &generator)) {131                    view.append(scalar)132                }133            }134            let name = String(view)135            let findings = FileNameAnalyzer.analyze(fileName: name)136            for finding in findings {137                #expect(!finding.evidence.isEmpty)138                #expect(!finding.explanation.isEmpty)139            }140            let visible = FileNameAnalyzer.visibleName(name)141            #expect(!visible.unicodeScalars.contains { FileNameAnalyzer.isDirectionOverride($0) })142            _ = FileNameAnalyzer.extensionMismatch(declaredExtension: FileNameAnalyzer.declaredExtension(of: name), detected: .pe)143        }144    }145146    @Test("Mutated samples through real files and the public inspector")147    func throughFiles() async throws {148        var generator = SplitMix64(seed: 0xF11E)149        let dir = try TempDir()150        var options = InspectionOptions()151        // PDFKit is Apple's parser, not this package's: it is kept out of the fuzz loop.152        options.usePDFKit = false153        let inspector = FileInspector()154        var count = 0155        for sample in Samples.all where sample.bytes.count > 8 {156            for round in 0..<3 {157                var data = sample.bytes158                for _ in 0..<4 {159                    let index = Int.random(in: 0..<data.count, using: &generator)160                    data[index] = UInt8.random(in: 0...255, using: &generator)161                }162                if round == 2 { data = Array(data.prefix(data.count / 2)) }163                let url = try dir.write("fuzz-\(count)-\(sample.name)", data)164                let inspection = try await inspector.inspect(fileAt: url, options: options)165                #expect(inspection.sha256 == FileHasher.sha256(of: Data(data)))166                expectHonest(inspection.findings)167                count += 1168            }169        }170        #expect(count > 200)171    }172}173
Files main
Menu

Theme