SPB Git

Repository Public License

StructureTests.swift 7.5 KB 132 lines swift

1// ORIX Secure2// File: Packages/ORIXFileInspection/Tests/ORIXFileInspectionTests/StructureTests.swift3// Description: Structure checks: PNG and JPEG declared dimensions and trailing data, RTF objects, OLE2 header.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import ORIXSecureModel9import Testing10@testable import ORIXFileInspection1112@Suite("Document and image structure")13struct StructureTests {14    @Test("PNG: absurd declared dimensions")15    func pngDimensions() throws {16        let core = try inspectBytes(Samples.pngImage(width: 60_000, height: 60_000), name: "tiny.png")17        expectHonest(core.findings)18        let finding = try #require(core.findings.with("file.image.dimensions"))19        #expect(finding.severity == .moderate)20        #expect(finding.confidence == .measured)21        #expect(finding.evidence.contains { $0.value == "60000 × 60000" })22        let maximum = try inspectBytes(Samples.pngImage(width: 0xFFFF_FFFF, height: 0xFFFF_FFFF), name: "max.png")23        #expect(maximum.findings.has("file.image.dimensions"))24        let normal = try inspectBytes(Samples.pngImage(width: 4032, height: 3024), name: "photo.png")25        #expect(!normal.findings.contains { $0.state == .attention })26        var options = InspectionOptions()27        options.maxImagePixels = 1_000_00028        let strict = try inspectBytes(Samples.pngImage(width: 4032, height: 3024), name: "photo.png", options: options)29        #expect(strict.findings.has("file.image.dimensions"))30    }3132    @Test("PNG: trailing data, truncation, broken header")33    func pngStructure() throws {34        let trailing = try inspectBytes(Samples.pngImage(width: 2, height: 2, trailing: Samples.plainZip), name: "poly.png")35        expectHonest(trailing.findings)36        let finding = try #require(trailing.findings.with("file.image.trailingData"))37        #expect(finding.severity == .low)38        #expect(finding.evidence.contains { $0.value == "\(Samples.plainZip.count)" })39        #expect(trailing.type == .png)4041        let cut = try inspectBytes(Array(Samples.png.dropLast(6)), name: "cut.png")42        #expect(cut.findings.has("file.png.truncated"))43        var lying = Samples.png44        lying.replaceSubrange(33..<37, with: be32(0xFFFF_FFF0))45        let huge = try inspectBytes(lying, name: "lying.png")46        #expect(huge.findings.has("file.png.truncated"))47        let zero = try inspectBytes(Samples.pngImage(width: 0, height: 10), name: "zero.png")48        #expect(zero.findings.has("file.png.malformed"))49        let headerOnly = try inspectBytes(Array(Samples.png.prefix(8)), name: "sig.png")50        #expect(headerOnly.findings.has("file.png.malformed"))51    }5253    @Test("JPEG: declared dimensions, trailing data, missing frame")54    func jpeg() throws {55        let big = try inspectBytes(Samples.jpegImage(width: 65_535, height: 65_535), name: "big.jpg")56        expectHonest(big.findings)57        #expect(big.findings.with("file.image.dimensions")?.severity == .moderate)58        let normal = try inspectBytes(Samples.jpegImage(width: 4032, height: 3024), name: "photo.jpg")59        #expect(!normal.findings.contains { $0.state == .attention })60        let trailing = try inspectBytes(Samples.jpegImage(width: 8, height: 8, trailing: bytes("PK appended archive bytes")), name: "poly.jpg")61        let finding = try #require(trailing.findings.with("file.image.trailingData"))62        #expect(finding.confidence == .heuristic)63        #expect(finding.evidence.contains { $0.value == "25" })64        let padded = try inspectBytes(Samples.jpegImage(width: 8, height: 8, trailing: zeros(32)), name: "padded.jpg")65        #expect(!padded.findings.has("file.image.trailingData"))66        let stub = try inspectBytes([0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x04, 0x00, 0x00, 0x12, 0x13], name: "stub.jpg")67        #expect(stub.findings.has("file.jpeg.noFrame"))68        #expect(stub.findings.has("file.jpeg.noEnd"))69        // A segment length of zero must not loop.70        let loop = try inspectBytes([0xFF, 0xD8, 0xFF, 0xE1, 0x00, 0x00] + zeros(64), name: "loop.jpg")71        #expect(loop.findings.has("file.jpeg.noFrame"))72    }7374    @Test("RTF: embedded objects by bounded scan")75    func rtf() throws {76        let plain = try inspectBytes(bytes("{\\rtf1\\ansi\\deff0 {\\fonttbl{\\f0 Arial;}} Hello \\b world\\b0.}"), name: "plain.rtf")77        #expect(!plain.findings.contains { $0.state == .attention })78        let data = bytes("{\\rtf1 text {\\object\\objemb\\objupdate{\\*\\objclass Equation.3}{\\*\\objdata 0105000002000000}} more}")79        let core = try inspectBytes(data, name: "letter.rtf")80        expectHonest(core.findings)81        let finding = try #require(core.findings.with("file.rtf.objects"))82        #expect(finding.severity == .moderate)83        #expect(finding.confidence == .heuristic)84        let noUpdate = try inspectBytes(bytes("{\\rtf1 {\\object\\objemb{\\*\\objdata 01}}}"), name: "chart.rtf")85        #expect(noUpdate.findings.with("file.rtf.objects")?.severity == .low)86        // Beyond the scan window: stated, not found.87        var options = InspectionOptions()88        options.rawScanWindowBytes = 64 << 1089        let far = bytes("{\\rtf1 ") + [UInt8](repeating: 0x41, count: 70_000) + bytes("{\\object{\\*\\objdata 01}}}")90        let limited = try inspectBytes(far, name: "far.rtf", options: options)91        #expect(!limited.findings.has("file.rtf.objects"))92        #expect(limited.notInspected.contains { $0.contains("RTF file were scanned") })93    }9495    @Test("OLE2: header fields and storage names")96    func ole2() throws {97        let valid = try inspectBytes(Samples.ole2, name: "legacy.doc")98        #expect(!valid.findings.contains { $0.state == .attention })99        var broken = Samples.ole2100        broken.replaceSubrange(28..<32, with: le16(0x1234) + le16(31))101        let core = try inspectBytes(broken, name: "odd.xls")102        expectHonest(core.findings)103        let header = try #require(core.findings.with("file.ole2.header"))104        #expect(header.evidence.first?.value.contains("sector shift 31") == true)105        let short = try inspectBytes(Array(Samples.ole2.prefix(100)), name: "short.doc")106        #expect(short.findings.has("file.ole2.header"))107108        func utf16(_ text: String) -> [UInt8] { text.utf16.flatMap { [UInt8($0 & 0xFF), UInt8($0 >> 8)] } }109        let macros = try inspectBytes(Samples.ole2 + utf16("_VBA_PROJECT") + zeros(64), name: "budget.xls")110        let finding = try #require(macros.findings.with("file.ole2.macros"))111        #expect(finding.confidence == .heuristic)112        #expect(finding.severity == .moderate)113        let encrypted = try inspectBytes(Samples.ole2 + utf16("EncryptedPackage") + zeros(64), name: "private.docx")114        #expect(encrypted.findings.has("file.ole2.encrypted"))115        expectHonest(macros.findings + encrypted.findings)116    }117118    @Test("Pattern scanner counts across chunk boundaries without double counting")119    func patternScanner() throws {120        var data = [UInt8](repeating: 0x2E, count: 20_000)121        let pattern = bytes("NEEDLE")122        for offset in [0, 4087, 4093, 4099, 8189, 12_288, 19_994] { data.replaceSubrange(offset..<(offset + 6), with: pattern) }123        let counts = try PatternScanner.count([pattern, bytes("NE"), bytes("absent"), bytes(".")], in: DataByteSource(data), from: 0, length: 20_000, chunkSize: 4096)124        #expect(counts[0] == 7)125        #expect(counts[1] == 7)126        #expect(counts[2] == 0)127        #expect(counts[3] == 20_000 - 42)128        let window = try PatternScanner.count([pattern], in: DataByteSource(data), from: 4091, length: 4200, chunkSize: 4096)129        #expect(window[0] == 3)130    }131}132
Files main
Menu

Theme