Repository Public License
1// ORIX OS Automation2// File: Packages/ORIXSecurity/Tests/ORIXSecurityTests/PolicyEngineTests.swift3// Description: Unit tests of the policy engine against an in-memory grant store: deny by default, scopes, tiers, expiry, revocation, confinement.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import ORIXCore9import ORIXTestingSupport10import Testing11@testable import ORIXSecurity1213/// A clock the test moves by hand.14final class TestClock: @unchecked Sendable {15 private let lock = NSLock()16 private var current: Date17 init(_ start: Date = Date(timeIntervalSince1970: 1_800_000_000)) { current = start }18 var now: Date { lock.lock(); defer { lock.unlock() }; return current }19 func advance(_ seconds: TimeInterval) { lock.lock(); current = current.addingTimeInterval(seconds); lock.unlock() }20}2122private func makeEngine(grants: [PermissionGrant] = []) -> (PolicyEngine, InMemoryGrantStore, CollectingAuditSink, TestClock) {23 let clock = TestClock()24 let store = InMemoryGrantStore(grants: grants)25 let audit = CollectingAuditSink()26 return (PolicyEngine(store: store, audit: audit, now: { clock.now }), store, audit, clock)27}2829private func request(_ capabilities: [Capability], tier: ApprovalTier = .readOnly, run: String = "run-1", call: String = "call-1",30 workflow: String? = nil, mode: ExecutionMode = .guided, dryRun: Bool = false) -> AccessRequest {31 AccessRequest(runID: run, callID: call, workflowID: workflow, toolID: "filesystem.test", capabilities: capabilities, tier: tier,32 sideEffect: tier == .readOnly ? .read : .write, mode: mode, summary: "Test call", dryRun: dryRun)33}3435private func approval(_ decision: PolicyDecision) -> ApprovalRequest? {36 if case .needsApproval(let r) = decision { return r }37 return nil38}3940private func allowedIDs(_ decision: PolicyDecision) -> [String]? {41 if case .allow(let ids) = decision { return ids }42 return nil43}4445private func isDeny(_ decision: PolicyDecision) -> Bool {46 if case .deny = decision { return true }47 return false48}4950@Suite("PolicyEngine (unit, in-memory grant store)")51struct PolicyEngineTests {52 @Test func readsAreNotImplicitlyAllowed() async {53 let (engine, _, _, _) = makeEngine()54 let decision = await engine.evaluate(request([.fileRead(path: "/Users/x/Documents/a.txt")]))55 let ask = approval(decision)56 #expect(ask?.missing == [.fileRead(path: "/Users/x/Documents/a.txt")])57 #expect(ask?.reason.contains("No active grant") == true)58 }5960 @Test func protectedPathsAreDeniedEvenWithARootGrant() async {61 let root = PermissionGrant(capability: .fileRead(path: "/"), scope: .persistent, maxTier: .highRisk)62 let (engine, _, audit, _) = makeEngine(grants: [root])63 let ssh = NSHomeDirectory() + "/.ssh/id_ed25519"64 #expect(isDeny(await engine.evaluate(request([.fileRead(path: ssh)]))))65 #expect(isDeny(await engine.evaluate(request([.fileWrite(path: "/System/Library/x")], tier: .reversibleWrite))))66 #expect(isDeny(await engine.evaluate(request([.fileDelete(path: NSHomeDirectory() + "/Library/Keychains/login.keychain-db")], tier: .highRisk))))67 // The same grant does cover an ordinary path.68 #expect(allowedIDs(await engine.evaluate(request([.fileRead(path: "/tmp/a")]))) == [root.id])69 #expect(await audit.entries.filter { $0.kind == .policyViolation }.count == 3)70 }7172 @Test func protectedGrantsCannotBeAddedOrApproved() async {73 let (engine, store, _, _) = makeEngine()74 let added = await engine.add(PermissionGrant(capability: .fileRead(path: NSHomeDirectory() + "/.ssh"), scope: .persistent, maxTier: .readOnly))75 #expect(!added)76 let forged = ApprovalRequest(access: request([.fileRead(path: NSHomeDirectory() + "/.aws/credentials")]),77 missing: [.fileRead(path: NSHomeDirectory() + "/.aws/credentials")], reason: "forged")78 #expect(await engine.recordApproval(.approvePersistent, for: forged).isEmpty)79 #expect(await store.allGrants().isEmpty)80 }8182 @Test func reviewModeIsReadOnly() async {83 let grant = PermissionGrant(capability: .fileWrite(path: "/tmp/work"), scope: .persistent, maxTier: .consequential)84 let read = PermissionGrant(capability: .fileRead(path: "/tmp/work"), scope: .persistent, maxTier: .readOnly)85 let (engine, _, _, _) = makeEngine(grants: [grant, read])86 let write = await engine.evaluate(request([.fileWrite(path: "/tmp/work/a")], tier: .reversibleWrite, mode: .review))87 if case .deny(let reason) = write { #expect(reason.contains("review mode is read-only")) } else { Issue.record("expected deny") }88 // A dry run is not refused by the mode, and a tier-0 read with a grant is allowed.89 #expect(allowedIDs(await engine.evaluate(request([.fileWrite(path: "/tmp/work/a")], tier: .reversibleWrite, mode: .review, dryRun: true))) == [grant.id])90 #expect(allowedIDs(await engine.evaluate(request([.fileRead(path: "/tmp/work/a")], mode: .review))) == [read.id])91 }9293 @Test func highRiskIgnoresStandingGrants() async {94 let cap = Capability.fileDelete(path: "/tmp/work")95 let grants = [96 PermissionGrant(capability: cap, scope: .persistent, maxTier: .highRisk),97 PermissionGrant(capability: cap, scope: .run(runID: "run-1"), maxTier: .highRisk),98 PermissionGrant(capability: cap, scope: .workflow(workflowID: "wf"), maxTier: .highRisk),99 PermissionGrant(capability: cap, scope: .once(callID: "another-call"), maxTier: .highRisk),100 ]101 let (engine, _, _, _) = makeEngine(grants: grants)102 let ask = approval(await engine.evaluate(request([.fileDelete(path: "/tmp/work/a")], tier: .highRisk, workflow: "wf")))103 #expect(ask?.missing == [.fileDelete(path: "/tmp/work/a")])104 #expect(ask?.reason.contains("every time") == true)105 }106107 @Test func highRiskApprovalIsAlwaysSingleUse() async throws {108 let (engine, store, audit, _) = makeEngine()109 let req = request([.fileDelete(path: "/tmp/work/a")], tier: .highRisk)110 for response in [ApprovalResponse.approvePersistent, .approveForRun, .approveOnce] {111 let ask = try #require(approval(await engine.evaluate(req)))112 let ids = await engine.recordApproval(response, for: ask)113 #expect(ids.count == 1)114 let stored = try #require(await store.allGrants().first { $0.id == ids[0] })115 #expect(stored.scope == .once(callID: "call-1"))116 #expect(stored.maxTier == .highRisk)117 #expect(allowedIDs(await engine.evaluate(req)) == ids)118 // Another call in the same run is not covered.119 #expect(approval(await engine.evaluate(request([.fileDelete(path: "/tmp/work/a")], tier: .highRisk, call: "call-2"))) != nil)120 await engine.consume(grantIDs: ids, callID: "call-1")121 #expect(approval(await engine.evaluate(req)) != nil)122 }123 #expect(await audit.entries.filter { $0.kind == .approvalGranted }.count == 3)124 #expect(await audit.entries.filter { $0.kind == .approvalGranted && $0.data["downgradedToOnce"] == true }.count == 2)125 }126127 @Test func approveOnceCoversOneCallAndIsConsumed() async throws {128 let (engine, store, audit, _) = makeEngine()129 let req = request([.fileWrite(path: "/tmp/work/a"), .fileRead(path: "/tmp/work/a")], tier: .reversibleWrite)130 let ask = try #require(approval(await engine.evaluate(req)))131 #expect(ask.missing.count == 2)132 let ids = await engine.recordApproval(.approveOnce, for: ask)133 #expect(ids.count == 2)134 #expect(Set(allowedIDs(await engine.evaluate(req)) ?? []) == Set(ids))135 #expect(approval(await engine.evaluate(request([.fileWrite(path: "/tmp/work/a")], tier: .reversibleWrite, call: "call-2"))) != nil)136 await engine.consume(grantIDs: ids, callID: "call-1")137 #expect(approval(await engine.evaluate(req))?.missing.count == 2)138 #expect(await store.allGrants().allSatisfy { $0.revokedAt != nil })139 #expect(await audit.entries.filter { $0.kind == .grantCreated }.count == 2)140 }141142 @Test func approveForRunCoversTheRunOnly() async throws {143 let (engine, _, _, _) = makeEngine()144 let ask = try #require(approval(await engine.evaluate(request([.network(host: "api.example.com")], tier: .consequential))))145 let ids = await engine.recordApproval(.approveForRun, for: ask)146 #expect(allowedIDs(await engine.evaluate(request([.network(host: "api.example.com")], tier: .consequential, call: "call-9"))) == ids)147 #expect(approval(await engine.evaluate(request([.network(host: "api.example.com")], tier: .consequential, run: "run-2"))) != nil)148 // Consuming does not spend a run grant.149 await engine.consume(grantIDs: ids, callID: "call-9")150 #expect(allowedIDs(await engine.evaluate(request([.network(host: "api.example.com")], tier: .consequential, call: "call-10"))) == ids)151 #expect(await engine.revokeRun(runID: "run-1") == 1)152 #expect(approval(await engine.evaluate(request([.network(host: "api.example.com")], tier: .consequential, call: "call-11"))) != nil)153 }154155 @Test func approvePersistentCoversLaterRuns() async throws {156 let (engine, _, _, _) = makeEngine()157 let ask = try #require(approval(await engine.evaluate(request([.clipboardWrite], tier: .reversibleWrite))))158 let ids = await engine.recordApproval(.approvePersistent, for: ask)159 #expect(allowedIDs(await engine.evaluate(request([.clipboardWrite], tier: .reversibleWrite, run: "run-77", call: "c"))) == ids)160 }161162 @Test func denialAndSilenceCreateNoGrant() async throws {163 let (engine, store, audit, _) = makeEngine()164 let ask = try #require(approval(await engine.evaluate(request([.notify], tier: .reversibleWrite))))165 #expect(await engine.recordApproval(.deny(reason: "no"), for: ask).isEmpty)166 #expect(await engine.recordApproval(.unanswered, for: ask).isEmpty)167 #expect(await store.allGrants().isEmpty)168 #expect(await audit.entries.map(\.kind) == [.approvalDenied, .approvalDenied])169 }170171 @Test func workflowGrantsMatchOnlyTheirWorkflow() async {172 let grant = PermissionGrant(capability: .fileRead(path: "/tmp/reports"), scope: .workflow(workflowID: "wf-a"), maxTier: .readOnly, source: "workflow:wf-a")173 let (engine, _, _, _) = makeEngine(grants: [grant])174 #expect(allowedIDs(await engine.evaluate(request([.fileRead(path: "/tmp/reports/x.pdf")], workflow: "wf-a"))) == [grant.id])175 #expect(approval(await engine.evaluate(request([.fileRead(path: "/tmp/reports/x.pdf")], workflow: "wf-b"))) != nil)176 #expect(approval(await engine.evaluate(request([.fileRead(path: "/tmp/reports/x.pdf")]))) != nil)177 #expect(await engine.grantsForWorkflow("wf-a").map(\.id) == [grant.id])178 #expect(await engine.grantsForWorkflow("wf-b").isEmpty)179 }180181 @Test func grantTierIsACeiling() async {182 let grant = PermissionGrant(capability: .fileWrite(path: "/tmp/work"), scope: .persistent, maxTier: .reversibleWrite)183 let (engine, _, _, _) = makeEngine(grants: [grant])184 #expect(allowedIDs(await engine.evaluate(request([.fileWrite(path: "/tmp/work/a")], tier: .reversibleWrite))) == [grant.id])185 let ask = approval(await engine.evaluate(request([.fileWrite(path: "/tmp/work/a")], tier: .consequential, mode: .autonomous)))186 #expect(ask?.reason.contains("lower tier") == true)187 }188189 @Test func coverageNeverWidens() async {190 let grants = [191 PermissionGrant(capability: .fileWrite(path: "/tmp/work"), scope: .persistent, maxTier: .consequential),192 PermissionGrant(capability: .network(host: "*.example.com"), scope: .persistent, maxTier: .consequential),193 ]194 let (engine, _, _, _) = makeEngine(grants: grants)195 // Write does not imply read or delete; a sibling folder with the same prefix is not inside the root.196 #expect(approval(await engine.evaluate(request([.fileRead(path: "/tmp/work/a")]))) != nil)197 #expect(approval(await engine.evaluate(request([.fileDelete(path: "/tmp/work/a")], tier: .reversibleWrite))) != nil)198 #expect(approval(await engine.evaluate(request([.fileWrite(path: "/tmp/workspace/a")], tier: .reversibleWrite))) != nil)199 #expect(allowedIDs(await engine.evaluate(request([.network(host: "api.example.com")], tier: .consequential))) != nil)200 #expect(approval(await engine.evaluate(request([.network(host: "example.org")], tier: .consequential))) != nil)201 #expect(approval(await engine.evaluate(request([.browser(host: "api.example.com")], tier: .consequential))) != nil)202 // Partially covered request: only the uncovered capability is asked for.203 let partial = approval(await engine.evaluate(request([.fileWrite(path: "/tmp/work/a"), .processExec(executable: "git")], tier: .consequential)))204 #expect(partial?.missing == [.processExec(executable: "git")])205 }206207 @Test func expiredAndRevokedGrantsNeverMatch() async {208 let clock = TestClock()209 let expiring = PermissionGrant(capability: .fileRead(path: "/tmp/a"), scope: .persistent, maxTier: .readOnly,210 createdAt: clock.now, expiresAt: clock.now.addingTimeInterval(60))211 let revoked = PermissionGrant(capability: .fileRead(path: "/tmp/b"), scope: .persistent, maxTier: .readOnly,212 createdAt: clock.now, revokedAt: clock.now)213 let store = InMemoryGrantStore(grants: [expiring, revoked])214 let engine = PolicyEngine(store: store, now: { clock.now })215 #expect(allowedIDs(await engine.evaluate(request([.fileRead(path: "/tmp/a")]))) == [expiring.id])216 #expect(approval(await engine.evaluate(request([.fileRead(path: "/tmp/b")]))) != nil)217 clock.advance(61)218 #expect(approval(await engine.evaluate(request([.fileRead(path: "/tmp/a")]))) != nil)219 #expect(await engine.activeGrants().isEmpty)220 #expect(await engine.confinement(runID: "run-1", workflowID: nil).readRoots.isEmpty)221 #expect(await engine.purgeExpired() == 1)222 #expect(await engine.purgeExpired() == 0)223 #expect(await engine.grants().count == 2)224 }225226 @Test func unconsumedSingleUseGrantsGoStale() async throws {227 let (engine, _, _, clock) = makeEngine()228 let req = request([.fileWrite(path: "/tmp/work/a")], tier: .reversibleWrite)229 let ask = try #require(approval(await engine.evaluate(req)))230 _ = await engine.recordApproval(.approveOnce, for: ask)231 clock.advance(PolicyEngine.onceGrantLifetime + 1)232 #expect(approval(await engine.evaluate(req)) != nil)233 }234235 @Test func revokeAndRevokeAll() async {236 let a = PermissionGrant(capability: .fileRead(path: "/tmp/a"), scope: .persistent, maxTier: .readOnly)237 let b = PermissionGrant(capability: .fileRead(path: "/tmp/b"), scope: .run(runID: "run-1"), maxTier: .readOnly)238 let (engine, _, audit, _) = makeEngine()239 #expect(await engine.add(a))240 #expect(await engine.add(b))241 await engine.revoke(id: a.id)242 #expect(approval(await engine.evaluate(request([.fileRead(path: "/tmp/a")]))) != nil)243 #expect(allowedIDs(await engine.evaluate(request([.fileRead(path: "/tmp/b")]))) == [b.id])244 await engine.revokeAll()245 #expect(approval(await engine.evaluate(request([.fileRead(path: "/tmp/b")]))) != nil)246 #expect(await engine.activeGrants().isEmpty)247 let kinds = await audit.entries.map(\.kind)248 #expect(kinds == [.grantCreated, .grantCreated, .grantRevoked, .emergencyStop])249 }250251 @Test func confinementAggregatesMatchingScopes() async {252 let grants = [253 PermissionGrant(capability: .fileRead(path: "/tmp/in"), scope: .persistent, maxTier: .readOnly),254 PermissionGrant(capability: .fileWrite(path: "/tmp/out"), scope: .run(runID: "run-1"), maxTier: .reversibleWrite),255 PermissionGrant(capability: .fileWrite(path: "/tmp/other-run"), scope: .run(runID: "run-2"), maxTier: .reversibleWrite),256 PermissionGrant(capability: .network(host: "API.example.com"), scope: .workflow(workflowID: "wf"), maxTier: .consequential),257 PermissionGrant(capability: .browser(host: "example.com"), scope: .workflow(workflowID: "other"), maxTier: .consequential),258 PermissionGrant(capability: .browser(host: "docs.example.com"), scope: .persistent, maxTier: .consequential),259 PermissionGrant(capability: .fileWrite(path: "/tmp/once"), scope: .once(callID: "call-1"), maxTier: .reversibleWrite),260 PermissionGrant(capability: .fileDelete(path: "/tmp/del"), scope: .persistent, maxTier: .consequential),261 ]262 let (engine, _, _, _) = makeEngine(grants: grants)263 let c = await engine.confinement(runID: "run-1", workflowID: "wf")264 #expect(c == Confinement(readRoots: ["/tmp/in"], writeRoots: ["/tmp/out"], networkHosts: ["api.example.com"], browserHosts: ["docs.example.com"]))265 let noWorkflow = await engine.confinement(runID: "run-9", workflowID: nil)266 #expect(noWorkflow == Confinement(readRoots: ["/tmp/in"], browserHosts: ["docs.example.com"]))267 #expect(!noWorkflow.allowsNetwork)268 }269270 @Test func callsWithoutCapabilities() async {271 let (engine, _, _, _) = makeEngine()272 #expect(allowedIDs(await engine.evaluate(request([]))) == [])273 let ask = approval(await engine.evaluate(request([], tier: .consequential)))274 #expect(ask?.missing == [])275 #expect(ask?.reason.contains("no specific capability") == true)276 }277278 @Test func scheduledRunsWaitForApproval() async {279 let (engine, _, _, _) = makeEngine()280 let ask = approval(await engine.evaluate(request([.externalSend(channel: "mail")], tier: .consequential, mode: .scheduled)))281 #expect(ask?.reason.contains("Scheduled runs wait") == true)282 }283284 @Test func inMemoryStoreKeepsOrderAndRevocationDate() async {285 let store = InMemoryGrantStore()286 let a = PermissionGrant(id: "a", capability: .notify, scope: .persistent, maxTier: .reversibleWrite)287 let b = PermissionGrant(id: "b", capability: .clipboardRead, scope: .persistent, maxTier: .readOnly)288 await store.saveGrant(a); await store.saveGrant(b); await store.saveGrant(a)289 #expect(await store.allGrants().map(\.id) == ["a", "b"])290 let first = Date(timeIntervalSince1970: 10)291 await store.revokeGrant(id: "a", at: first)292 await store.revokeAllGrants(at: Date(timeIntervalSince1970: 20))293 let all = await store.allGrants()294 #expect(all[0].revokedAt == first)295 #expect(all[1].revokedAt == Date(timeIntervalSince1970: 20))296 }297}298