SPB Git

Repository Public License

FileNameTests.swift 9.9 KB 191 lines swift

1// ORIX Secure2// File: Packages/ORIXFileInspection/Tests/ORIXFileInspectionTests/FileNameTests.swift3// Description: File-name heuristics and the comparison between declared extension and detected type.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import ORIXSecureModel9import Testing10@testable import ORIXFileInspection1112@Suite("File names and extension mismatch")13struct FileNameTests {14    func ids(_ name: String) -> Set<String> {15        let findings = FileNameAnalyzer.analyze(fileName: name)16        expectHonest(findings)17        return Set(findings.map(\.id))18    }1920    @Test("Ordinary names produce no finding")21    func ordinaryNames() {22        for name in ["report.pdf", "Photo 2026-11-20 at 10.32.15.jpg", "archive.tar.gz", "Résumé final (2).docx", "notes", "données_été.csv", "v1.2.3-release-notes.txt", "家族写真.png", "تقرير.pdf", "👨‍👩‍👧 family.jpg"] {23            #expect(ids(name).isEmpty, "\(name) → \(ids(name))")24        }25    }2627    @Test("Double extension ending in a program type")28    func doubleExtension() throws {29        let findings = FileNameAnalyzer.analyze(fileName: "invoice.pdf.exe")30        let finding = try #require(findings.with("file.name.doubleExtension"))31        #expect(finding.severity == .high)32        #expect(finding.confidence == .heuristic)33        #expect(finding.evidence.contains { $0.value == ".exe" })34        #expect(ids("photo.jpg.scr").contains("file.name.doubleExtension"))35        #expect(ids("Statement.docx.js").contains("file.name.doubleExtension"))36        #expect(!ids("backup.tar.gz").contains("file.name.doubleExtension"))37        #expect(!ids("setup.exe").contains("file.name.doubleExtension"))38        #expect(!ids("report.final.pdf").contains("file.name.doubleExtension"))39    }4041    @Test("Right-to-left override is reported and made visible")42    func bidiOverride() throws {43        let name = "annual-report-\u{202E}fdp.exe"44        let findings = FileNameAnalyzer.analyze(fileName: name)45        let finding = try #require(findings.with("file.name.bidi"))46        #expect(finding.severity == .high)47        #expect(finding.evidence.contains { $0.value.contains("<U+202E>") })48        #expect(finding.evidence.contains { $0.value.contains("U+202E") && !$0.value.contains("<") })49        #expect(FileNameAnalyzer.visibleName(name) == "annual-report-<U+202E>fdp.exe")50        #expect(ids("a\u{2066}b\u{2069}.txt").contains("file.name.bidi"))51    }5253    @Test("Other invisible characters are reported at low severity")54    func invisibleCharacters() throws {55        let findings = FileNameAnalyzer.analyze(fileName: "pay\u{200B}roll.xlsx")56        let finding = try #require(findings.with("file.name.invisible"))57        #expect(finding.severity == .low)58        #expect(finding.evidence.contains { $0.value.contains("pay<U+200B>roll.xlsx") })59        #expect(ids("tab\there.txt").contains("file.name.invisible"))60        #expect(ids("soft\u{00AD}hyphen.txt").contains("file.name.invisible"))61        #expect(FileNameAnalyzer.visibleName("a\u{0}b") == "a<U+0000>b")62    }6364    @Test("Padding spaces before the extension")65    func padding() {66        #expect(ids("document.pdf" + String(repeating: " ", count: 40) + ".exe").contains("file.name.padding"))67        #expect(ids("photo   .jpg").contains("file.name.padding"))68        #expect(ids("with\u{00A0}\u{00A0}\u{00A0}\u{00A0}.txt").contains("file.name.padding"))69        #expect(!ids("my holiday photo.jpg").contains("file.name.padding"))70    }7172    @Test("Leading and trailing whitespace, trailing dot")73    func edges() {74        #expect(ids(" report.pdf").contains("file.name.edgeWhitespace"))75        #expect(ids("report.pdf ").contains("file.name.edgeWhitespace"))76        #expect(ids("report.exe.").contains("file.name.edgeWhitespace"))77        #expect(!ids("report.pdf").contains("file.name.edgeWhitespace"))78    }7980    @Test("Very long names")81    func longNames() throws {82        let name = String(repeating: "a", count: 220) + ".pdf"83        let finding = try #require(FileNameAnalyzer.analyze(fileName: name).with("file.name.long"))84        #expect(finding.evidence.contains { $0.value.hasSuffix(".pdf") })85        var options = InspectionOptions()86        options.longFileNameLength = 30087        #expect(!FileNameAnalyzer.analyze(fileName: name, options: options).has("file.name.long"))88    }8990    @Test("Look-alike extension characters")91    func homoglyphs() throws {92        // Cyrillic "е" and "х" in "exe".93        let finding = try #require(FileNameAnalyzer.analyze(fileName: "setup.\u{0435}\u{0445}\u{0435}").with("file.name.homoglyphExtension"))94        #expect(finding.evidence.contains { $0.value == ".exe" })95        #expect(finding.evidence.contains { $0.value.contains("U+0435") })96        // One-dot leader instead of a full stop, fullwidth letters.97        #expect(ids("invoice\u{2024}pdf").contains("file.name.homoglyphExtension"))98        #expect(ids("photo.\u{FF4A}\u{FF50}\u{FF47}").contains("file.name.homoglyphExtension"))99        #expect(!ids("photo.jpg").contains("file.name.homoglyphExtension"))100        #expect(!ids("fichier.données").contains("file.name.homoglyphExtension"))101    }102103    @Test("Hidden dot files")104    func hidden() throws {105        let finding = try #require(FileNameAnalyzer.analyze(fileName: ".profile").with("file.name.hidden"))106        #expect(finding.severity == .low)107        #expect(!ids("profile").contains("file.name.hidden"))108    }109110    @Test("Names worded like system or security messages")111    func systemPrompts() throws {112        let finding = try #require(FileNameAnalyzer.analyze(fileName: "Apple_Security-Update.pkg").with("file.name.systemPrompt"))113        #expect(finding.severity == .moderate)114        let document = try #require(FileNameAnalyzer.analyze(fileName: "Password reset procedure.pdf").with("file.name.systemPrompt"))115        #expect(document.severity == .low)116        #expect(!ids("holiday.pdf").contains("file.name.systemPrompt"))117    }118119    @Test("Declared extension parsing")120    func declaredExtension() {121        #expect(FileNameAnalyzer.declaredExtension(of: "a.PDF") == "pdf")122        #expect(FileNameAnalyzer.declaredExtension(of: "archive.tar.gz") == "gz")123        #expect(FileNameAnalyzer.declaredExtension(of: ".bashrc") == "")124        #expect(FileNameAnalyzer.declaredExtension(of: "noext") == "")125        #expect(FileNameAnalyzer.declaredExtension(of: "trailing.") == "")126        #expect(FileNameAnalyzer.declaredExtension(of: "x.exe ") == "exe")127    }128129    // MARK: Mismatch130131    @Test("A Windows program named .pdf is the high-severity case")132    func programNamedPDF() throws {133        let core = try inspectBytes(Samples.pe, name: "invoice.pdf")134        let finding = try #require(core.findings.with("file.type.executableDisguised"))135        #expect(finding.severity == .high)136        #expect(finding.confidence == .measured)137        #expect(finding.verification == .parsedData)138        #expect(finding.evidence.contains { $0.value == ".pdf" })139        expectHonest(core.findings)140        for (data, name) in [([UInt8]([0x7F, 0x45, 0x4C, 0x46] + zeros(60)), "song.mp3"), ([0xCF, 0xFA, 0xED, 0xFE] + zeros(28), "photo.jpg"), (bytes("#!/bin/sh\nrm x\n"), "picture.png")] {141            let other = try inspectBytes(data, name: name)142            #expect(other.findings.with("file.type.executableDisguised")?.severity == .high, "\(name)")143        }144    }145146    @Test("A PNG named .jpg is a low-severity mismatch")147    func imageUnderOtherImageExtension() throws {148        let core = try inspectBytes(Samples.png, name: "photo.jpg")149        let finding = try #require(core.findings.with("file.type.extensionMismatch"))150        #expect(finding.severity == .low)151        #expect(!core.findings.has("file.type.executableDisguised"))152    }153154    @Test("Text named .png is reported; text under a text extension is not")155    func textNamedPNG() throws {156        let core = try inspectBytes(bytes("just some words\n"), name: "picture.png")157        let finding = try #require(core.findings.with("file.type.extensionMismatch"))158        #expect(finding.severity == .moderate)159        for name in ["notes.txt", "config.yaml", "weird.customext", "Makefile", "data.csv", "run.command"] {160            let plain = try inspectBytes(bytes("just some words\n"), name: name)161            #expect(!plain.findings.contains { $0.id.hasPrefix("file.type.extension") }, "\(name)")162        }163    }164165    @Test("Equivalent extensions and containers are accepted")166    func equivalences() throws {167        let cases: [([UInt8], String)] = [168            (Samples.jpegImage(width: 2, height: 2), "a.jpeg"), (Samples.jpegImage(width: 2, height: 2), "a.JPG"), (Samples.docx, "a.zip"),169            (Samples.docx, "a.docm"), (Samples.plainZip, "a.pages"), (Samples.ftyp("qt  "), "a.mp4"), (Samples.ftyp("isom"), "a.mov"),170            (Samples.ole2, "a.xls"), (Samples.ole2, "a.msi"), (Samples.mobileconfig, "a.xml"), (bytes("<?xml version=\"1.0\"?><plist/>"), "a.plist"),171            (bytes("{\\rtf1 x}"), "a.doc"), (Samples.pe, "a.dll"), (Samples.pe, "a.unknownext"), ([0xCF, 0xFA, 0xED, 0xFE] + zeros(28), "tool"),172        ]173        for (data, name) in cases {174            let core = try inspectBytes(data, name: name)175            #expect(!core.findings.contains { $0.id.hasPrefix("file.type.extension") || $0.id == "file.type.executableDisguised" }, "\(name)")176        }177    }178179    @Test("Unknown content: a finding only when the extension promises a known signature")180    func unknownContent() throws {181        let blob: [UInt8] = [0x9A, 0x00, 0x13, 0x7F, 0xC1, 0x02, 0xFE, 0x00, 0x88, 0x01]182        let unknownBoth = try inspectBytes(blob, name: "data.xyz")183        #expect(!unknownBoth.findings.contains { $0.state == .attention })184        let promised = try inspectBytes(blob, name: "photo.png")185        let finding = try #require(promised.findings.with("file.type.signatureMissing"))186        #expect(finding.severity == .low)187        let empty = try inspectBytes([], name: "empty.png")188        #expect(!empty.findings.contains { $0.id.hasPrefix("file.type.signature") })189    }190}191
Files main
Menu

Theme