Repository Public License
1// ORIX Convert — Packages/ORIXConvertCore/Tests/ConvertCoreTests/SecretsTests.swift2// Description: Option values whose key contains "password" are never persisted: not in history rows (direct record3// or through the queue), not in preset files, not in job logs.4// Author: Simon-Pierre Boucher — contact@spboucher.ai5import Foundation6import Testing7@testable import ConvertCore89@Suite struct SecretsTests {10 static let secret = "hunter2-ORIX-secret"11 static let options = ConversionOptions(["password": .string(secret), "output.password.user": .string(secret), "Owner.PASSWORD": .string(secret),12 "quality": .double(0.8)])1314 @Test func redactionKeepsOrdinaryOptions() {15 let redacted = Self.options.redactingSecrets()16 #expect(redacted.values == ["quality": .double(0.8)])17 #expect(Self.options.secretKeys == ["Owner.PASSWORD", "output.password.user", "password"])18 #expect(ConversionOptions.isSecretKey("pdf.Password") && !ConversionOptions.isSecretKey("pass"))19 }2021 @Test func historyStoreNeverWritesSecrets() async throws {22 let temp = TempDir("secrets-history")23 let url = temp.file("history.sqlite")24 let store = try ConversionHistoryStore(url: url)25 var entry = HistoryEntry(jobID: UUID(), sourcePath: "/a.pdf", destinationFormat: .png, status: "finished")26 entry.options = Self.options27 try await store.record(entry)28 let read = try #require(try await store.entries().first)29 #expect(read.options.values == ["quality": .double(0.8)])30 for name in ["history.sqlite", "history.sqlite-wal"] {31 if let data = try? Data(contentsOf: temp.file(name)) { #expect(data.range(of: Data(Self.secret.utf8)) == nil) }32 }33 }3435 @Test func queueJobsRecordNoSecretsInHistoryOrLog() async throws {36 let bed = TestBed()37 let source = bed.source()38 let id = await bed.queue.enqueue(ConversionJobRequest(source: source, to: .markdown, options: Self.options, destination: bed.destination()))39 let job = try #require(await bed.queue.waitForJob(id))40 #expect(job.state == .finished)41 // The request keeps the secret in memory (the engine needs it)…42 #expect(job.request.options.string("password") == Self.secret)43 // …but nothing persisted or logged carries it.44 #expect(!job.log.contains { $0.message.contains(Self.secret) })45 let rows = try await bed.history.entries()46 #expect(rows.count == 1)47 #expect(rows.allSatisfy { $0.options.secretKeys.isEmpty && $0.options.has("quality") })48 }4950 @Test func presetStoreNeverWritesSecrets() async throws {51 let temp = TempDir("secrets-presets")52 let store = PresetStore(fileURL: temp.file("presets.json"))53 let added = try await store.add(ConversionPreset(name: "Locked", source: .format(.pdf), destinationFormat: .png, options: Self.options))54 #expect(added.options.secretKeys.isEmpty)55 var changed = added56 changed.options = Self.options57 try await store.update(changed)58 #expect(await store.userPresets.allSatisfy { $0.options.secretKeys.isEmpty })59 #expect(!temp.read("presets.json").contains(Self.secret))60 // A hand-edited file carrying a secret is cleaned when loaded and when imported.61 let dirty = #"{"presets":[{"id":"\#(UUID().uuidString)","name":"x","summary":"","source":{"any":{}},"destinationFormat":"png","options":{"values":{"password":{"string":{"_0":"\#(Self.secret)"}}}},"isBuiltIn":false}]}"#62 if let imported = try? await store.importData(Data(dirty.utf8)) { #expect(imported.allSatisfy { $0.options.secretKeys.isEmpty }) }63 #expect(!temp.read("presets.json").contains(Self.secret))64 }65 @Test func workflowLibraryNeverWritesSecrets() async throws {66 let temp = TempDir("secrets-workflows")67 let url = temp.file("workflows.json")68 let library = WorkflowLibrary(fileURL: url)69 let workflow = Workflow(name: "Protected", nodes: [70 .convert(destination: .png, operation: nil, options: Self.options),71 .branch(condition: .formatIs(.png), then: [.convert(destination: .jpeg, operation: nil, options: Self.options)],72 otherwise: [.branch(condition: .sizeAbove(1), then: [], otherwise: [.convert(destination: .pdf, operation: "x", options: Self.options)])]),73 ])74 try await library.save(workflow)75 let stored = try #require(await library.workflow(id: workflow.id))76 #expect(stored == workflow.redactingSecrets())77 if case .convert(_, _, let options) = stored.nodes[0] { #expect(options.values == ["quality": .double(0.8)]) } else { Issue.record("first node changed") }78 let copy = try await library.duplicate(id: workflow.id)79 let exported = try await library.exportData()80 // An export made by another build (or edited by hand) that carries secrets is cleaned on import.81 let foreign = try JSONEncoder().encode(["version": AnyEncodable(1), "workflows": AnyEncodable([workflow])])82 #expect(foreign.range(of: Data(Self.secret.utf8)) != nil)83 let imported = try await library.importData(foreign)84 #expect(imported.count == 1 && imported[0] == { var w = workflow.redactingSecrets(); w.id = imported[0].id; return w }())85 #expect(copy.nodes == stored.nodes)86 for data in [try Data(contentsOf: url), exported] { #expect(data.range(of: Data(Self.secret.utf8)) == nil) }87 // A file written before this rule existed is cleaned when it is loaded and at the next save.88 try foreign.write(to: url)89 let reopened = WorkflowLibrary(fileURL: url)90 #expect(await reopened.all.allSatisfy { $0 == $0.redactingSecrets() })91 try await reopened.save(Workflow(name: "Other", nodes: [.rename(template: "{name}")]))92 #expect(try Data(contentsOf: url).range(of: Data(Self.secret.utf8)) == nil)93 }94}9596private struct AnyEncodable: Encodable {97 let encodeValue: (Encoder) throws -> Void98 init<T: Encodable>(_ value: T) { encodeValue = value.encode }99 func encode(to encoder: Encoder) throws { try encodeValue(encoder) }100}101