Repository Public License
1// ORIX Secure2// File: App/Sources/Features/FileScanner/FileScannerView.swift3// Description: File scanner: inspects a file the user picks — real type from its bytes, extension mismatch, SHA-256, name4// tricks, archive and PDF structure — without executing or fully decompressing it. Not a malware scanner.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import ORIXDesign9import ORIXFileInspection10import ORIXSecureModel11import SwiftUI12import UniformTypeIdentifiers1314struct FileScannerView: View {15 @Environment(AppModel.self) private var model16 @Environment(Router.self) private var router17 @State private var picking = false18 @State private var inspection: FileInspection?19 @State private var progress: Double?20 @State private var error: String?21 @State private var saved = false22 @State private var task: Task<Void, Never>?2324 var body: some View {25 ScreenScroll {26 VStack(alignment: .leading, spacing: ORIX.Space.sm) {27 Text("Choose a file to inspect. ORIX Secure reads its bytes on this device; it never opens, runs or uploads it.")28 .font(.orixCallout).foregroundStyle(ORIX.Palette.textSecondary)29 Button { picking = true } label: { Label("Choose File", systemImage: "doc.badge.plus") }.buttonStyle(.orixPrimary)30 .accessibilityIdentifier("file.choose")31 if let progress {32 Text("Computing SHA-256…").font(.orixCaption).foregroundStyle(ORIX.Palette.textSecondary)33 ORIXProgressBar(value: progress)34 Button("Cancel") { task?.cancel() }.font(.orixCallout)35 }36 }37 .orixCard()38 if let error { ORIXBanner(.error, title: Text("The file could not be inspected"), message: Text(verbatim: error)) }39 if let inspection { result(inspection) } else {40 LimitsCard(title: "What file inspection is, and is not", items: FileInspector.standingLimits)41 }42 }43 .navigationTitle("File Scanner")44 .fileImporter(isPresented: $picking, allowedContentTypes: [.item]) { result in45 switch result {46 case .success(let url): inspect(url)47 case .failure(let failure): error = failure.localizedDescription48 }49 }50 .onAppear { consumePending() }51 .onChange(of: router.pendingFile) { _, _ in consumePending() }52 .accessibilityIdentifier("file")53 }5455 private func consumePending() {56 guard let url = router.pendingFile else { return }57 router.pendingFile = nil58 inspect(url)59 }6061 private func inspect(_ url: URL) {62 task?.cancel()63 error = nil64 inspection = nil65 saved = false66 progress = 067 task = Task {68 let scoped = url.startAccessingSecurityScopedResource()69 defer { if scoped { url.stopAccessingSecurityScopedResource() } }70 do {71 let result = try await FileInspector().inspect(fileAt: url) { update in72 Task { @MainActor in if progress != nil { progress = update.fraction } }73 }74 inspection = result75 ORIXHaptics.tick()76 } catch is CancellationError {77 } catch {78 self.error = error.localizedDescription79 }80 progress = nil81 }82 }8384 @ViewBuilder private func result(_ inspection: FileInspection) -> some View {85 let attention = inspection.findings.filter { $0.state == .attention }86 VStack(alignment: .leading, spacing: ORIX.Space.sm) {87 HStack(spacing: ORIX.Space.sm) {88 ORIXGlyph(attention.isEmpty ? "doc.text.magnifyingglass" : "exclamationmark.triangle", tint: attention.isEmpty ? nil : ORIX.Palette.warning, size: 44)89 VStack(alignment: .leading, spacing: 2) {90 Text(verbatim: FileNameAnalyzer.visibleName(inspection.fileName)).font(.orixHeadline).foregroundStyle(ORIX.Palette.textPrimary).lineLimit(3)91 Text(attention.isEmpty ? "No structural finding needs attention" : "\(attention.count) finding(s) to review")92 .font(.orixCallout).foregroundStyle(ORIX.Palette.textSecondary)93 }94 }95 .accessibilityElement(children: .combine)96 if attention.isEmpty {97 Label("This is not a verdict on the file's content: no malware database is consulted.", systemImage: "info.circle")98 .font(.orixCaption).foregroundStyle(ORIX.Palette.textTertiary)99 }100 }101 .frame(maxWidth: .infinity, alignment: .leading)102 .orixCard()103 .accessibilityElement(children: .combine)104 .accessibilityIdentifier("file.summary")105106 ORIXSectionHeader("Identity")107 VStack(spacing: 0) {108 ReadingRow(label: String(localized: "Detected type"), value: .measured(inspection.detectedType.displayName), note: inspection.detectedSignature)109 Divider()110 ReadingRow(label: String(localized: "Extension"), value: .measured(inspection.declaredExtension.isEmpty ? String(localized: "None") : "." + inspection.declaredExtension))111 Divider()112 ReadingRow(label: String(localized: "Size"), value: .measured(ORIXFormat.bytes(inspection.byteSize)))113 if let modified = inspection.modificationDate {114 Divider()115 ReadingRow(label: String(localized: "Modified"), value: .measured(modified.formatted(date: .abbreviated, time: .shortened)))116 }117 }118 .orixCard(padding: ORIX.Space.sm)119 if let hash = inspection.sha256 {120 VStack(alignment: .leading, spacing: ORIX.Space.xs) {121 Text("SHA-256").font(.orixCaption).foregroundStyle(ORIX.Palette.textSecondary)122 Text(verbatim: hash).font(.orixMono).foregroundStyle(ORIX.Palette.textPrimary).textSelection(.enabled)123 Button { UIPasteboard.general.string = hash; ORIXHaptics.tick() } label: { Label("Copy", systemImage: "doc.on.doc") }.font(.orixCallout)124 .accessibilityIdentifier("file.copyHash")125 Text("Compare it with the hash published by the file's author to check the file was not altered.").font(.orixCaption).foregroundStyle(ORIX.Palette.textTertiary)126 }127 .frame(maxWidth: .infinity, alignment: .leading)128 .orixCard()129 .accessibilityElement(children: .contain)130 .accessibilityIdentifier("file.hash")131 }132 if !inspection.findings.isEmpty {133 ORIXSectionHeader("Findings (\(inspection.findings.count))")134 FindingList(findings: inspection.findings.prioritized())135 }136 if let archive = inspection.archive {137 ORIXSectionHeader("Archive structure")138 VStack(spacing: 0) {139 ReadingRow(label: String(localized: "Format"), value: .measured(archive.format.rawValue.uppercased()))140 ReadingRow(label: String(localized: "Entries"), value: .measured("\(archive.parsedEntryCount)"))141 ReadingRow(label: String(localized: "Declared size once expanded"), value: .measured(ORIXFormat.bytes(Int64(clamping: archive.totalUncompressedSize))),142 note: String(localized: "Read from the archive's directory. Nothing was decompressed."))143 ReadingRow(label: String(localized: "Encrypted entries"), value: .measured("\(archive.encryptedEntryCount)"))144 ReadingRow(label: String(localized: "Nested archives"), value: .measured("\(archive.nestedArchiveCount)"))145 ReadingRow(label: String(localized: "Executable files"), value: .measured("\(archive.executableCount)"))146 }147 .orixCard(padding: ORIX.Space.sm)148 }149 if let pdf = inspection.pdf {150 ORIXSectionHeader("PDF structure")151 VStack(spacing: 0) {152 ReadingRow(label: String(localized: "Pages"), value: pdf.pageCount.map { "\($0)" })153 ReadingRow(label: String(localized: "Encrypted"), value: pdf.isEncrypted.map { $0 ? String(localized: "Yes") : String(localized: "No") })154 if let title = pdf.title { ReadingRow(label: String(localized: "Title"), value: .measured(title)) }155 if let author = pdf.author { ReadingRow(label: String(localized: "Author"), value: .measured(author)) }156 if let producer = pdf.producer { ReadingRow(label: String(localized: "Produced by"), value: .measured(producer)) }157 ReadingRow(label: String(localized: "Links found"), value: .measured("\(pdf.links.count)"))158 }159 .orixCard(padding: ORIX.Space.sm)160 if !pdf.links.isEmpty {161 ORIXSectionHeader("Links inside the PDF")162 VStack(alignment: .leading, spacing: ORIX.Space.xs) {163 ForEach(pdf.links.prefix(25), id: \.self) { link in164 Button { router.pendingLink = link; router.open(.linkAnalyzer) } label: {165 HStack {166 Text(verbatim: link).font(.orixMono).foregroundStyle(ORIX.Palette.textPrimary).lineLimit(2).multilineTextAlignment(.leading)167 Spacer(minLength: 4)168 Image(systemName: "magnifyingglass").foregroundStyle(ORIXAccent.secure.primary)169 }170 }171 .buttonStyle(.plain)172 .accessibilityHint(Text("Analyses the link without opening it"))173 }174 }175 .orixCard()176 }177 }178 LimitsCard(title: "What was not inspected", items: inspection.notInspected)179 Button {180 var report = inspection.reportDocument()181 report.generator = ReportBuilder.generator182 saved = model.store.save(report)183 ORIXHaptics.success()184 } label: { Label(saved ? "Report Saved" : "Save as Report", systemImage: saved ? "checkmark" : "square.and.arrow.down") }185 .buttonStyle(.orixSecondary).disabled(saved)186 .accessibilityIdentifier("file.save")187 }188}189