Commit 60cc4d87d6
60cc4d87d6d78c99bbe339580b8e42e59b0d3529
parent: 34f0b480a6
Verified · cmc
cmc <hello@cleberg.net> · 2026-11-26 08:25 UTC
Replace in Files keeps no plaintext of tagged entries; crypt settings in their own modifier
Closes #153
Changed files (3)
Sources/Orgstar/ContentView.swift
+3 −5 · view
| @@ -407,11 +407,9 @@ struct ContentView: View { |
| 407 | 407 | commands.editor = nil |
| 408 | 408 | } |
| 409 | 409 | .onChange(of: saveMode, initial: true) { _, mode in session.saveMode = mode } |
| 410 | | .onChange(of: cryptBeforeSave, initial: true) { _, on in session.encryptsTaggedEntries = on } |
| 411 | | .onChange(of: cryptTagMatcher, initial: true) { session.cryptTag = OrgPreferences.cryptTag() } |
| 412 | | .onChange(of: session.unlockRequest, initial: true) { _, path in |
| 413 | | if let path { panel = .crypt(UUID(), CryptRequest(mode: .openFile, path: path, offset: 0)) } |
| 414 | | } |
| 410 | .modifier(CryptSettings(session: session, cryptBeforeSave: cryptBeforeSave, cryptTagMatcher: cryptTagMatcher) { path in |
| 411 | panel = .crypt(UUID(), CryptRequest(mode: .openFile, path: path, offset: 0)) |
| 412 | }) |
| 415 | 413 | .onChange(of: options, initial: true) { _, options in session.options = options } |
| 416 | 414 | .onAppear { TimerModel.shared.onDone = { [commands] in commands.show($0) } } |
| 417 | 415 | .onReceive(NotificationCenter.default.publisher(for: UserDefaults.didChangeNotification)) { _ in defaultsChanges += 1 } |
Sources/Orgstar/CryptHoldBar.swift
+18 · view
| @@ -25,3 +25,21 @@ struct CryptHoldBar: View { |
| 25 | 25 | } |
| 26 | 26 | } |
| 27 | 27 | } |
| 28 | |
| 29 | /// The session follows the org-crypt settings, and asks for the passphrase of an encrypted file |
| 30 | /// it was asked to open. |
| 31 | struct CryptSettings: ViewModifier { |
| 32 | let session: DocumentSession |
| 33 | let cryptBeforeSave: Bool |
| 34 | let cryptTagMatcher: String |
| 35 | let unlock: (String) -> Void |
| 36 | |
| 37 | func body(content: Content) -> some View { |
| 38 | content |
| 39 | .onChange(of: cryptBeforeSave, initial: true) { _, on in session.encryptsTaggedEntries = on } |
| 40 | .onChange(of: cryptTagMatcher, initial: true) { session.cryptTag = OrgPreferences.cryptTag() } |
| 41 | .onChange(of: session.unlockRequest, initial: true) { _, path in |
| 42 | if let path { unlock(path) } |
| 43 | } |
| 44 | } |
| 45 | } |
Tests/OrgAppTests/CryptSaveTests.swift
+26 · view
| @@ -136,6 +136,32 @@ struct CryptSaveTests { |
| 136 | 136 | #expect(recoveryFiles.allSatisfy { !$0.contains(Self.secret) }) |
| 137 | 137 | } |
| 138 | 138 | |
| 139 | @Test func replaceInFilesKeepsNoPlaintextAndIsHeld() throws { |
| 140 | try notes.write("a.org", "* Plain\nOpen text.\n") |
| 141 | session.open(file.path) |
| 142 | try replace(with: Self.file) |
| 143 | // The buffer has no editor: Replace in Files keeps a copy first, and can't keep one now. |
| 144 | let changed = (Self.file as NSString).replacingOccurrences(of: "Open text.", with: "Shut text.") |
| 145 | #expect(session.applyChanges(lineEdits(from: Self.file, to: changed), to: file.path, expecting: Self.file) == nil) |
| 146 | #expect(!session.save()) |
| 147 | #expect(session.held[file.path] == .entries(1)) |
| 148 | #expect(recoveryFiles.isEmpty) |
| 149 | #expect(disk == "* Plain\nOpen text.\n") |
| 150 | // With the passphrase, the copy is kept encrypted. |
| 151 | session.cryptUsed("pw", for: file.path) |
| 152 | #expect(session.applyChanges(lineEdits(from: changed, to: Self.file), to: file.path, expecting: changed) == nil) |
| 153 | #expect(session.save()) |
| 154 | #expect(!recoveryFiles.isEmpty && recoveryFiles.allSatisfy { !$0.contains(Self.secret) }) |
| 155 | #expect(!disk.contains(Self.secret)) |
| 156 | // A file that isn't open, with an unencrypted tagged entry on disk, isn't written. |
| 157 | session.cryptUsed("other", for: "/nowhere") |
| 158 | let closed = try notes.write("b.org", Self.file) |
| 159 | let message = session.applyChanges(lineEdits(from: Self.file, to: changed), to: closed.path, expecting: Self.file) |
| 160 | #expect(message?.contains("1 tagged entry isn't encrypted") == true) |
| 161 | #expect((try String(contentsOf: closed, encoding: .utf8)) == Self.file) |
| 162 | #expect(recoveryFiles.allSatisfy { !$0.contains(Self.secret) }) |
| 163 | } |
| 164 | |
| 139 | 165 | @Test func aHeldFileIsReleasedWhenTheEntryGoes() throws { |
| 140 | 166 | session.saveMode = .automatic |
| 141 | 167 | session.autosaveDelay = .seconds(60) |