Encrypt tagged entries on save; whole-file encryption !489
open
opened by cmc on 2026-11-26 08:44 UTC
· krz/orgstar:m5-crypt into main
3 files changed, +47 −5
One commit's changes, read-only. Review comments go on all commits.
Changed files (3)
Sources/Orgstar/ContentView.swift
+3 −5 · view
| @@ -407,11 +407,9 @@ struct ContentView: View { |
| 407 | commands.editor = nil |
407 | commands.editor = nil |
| 408 | } |
408 | } |
| 409 | .onChange(of: saveMode, initial: true) { _, mode in session.saveMode = mode } |
409 | .onChange(of: saveMode, initial: true) { _, mode in session.saveMode = mode } |
| 410 | .onChange(of: cryptBeforeSave, initial: true) { _, on in session.encryptsTaggedEntries = on } |
410 | .modifier(CryptSettings(session: session, cryptBeforeSave: cryptBeforeSave, cryptTagMatcher: cryptTagMatcher) { path in |
| 411 | .onChange(of: cryptTagMatcher, initial: true) { session.cryptTag = OrgPreferences.cryptTag() } |
411 | panel = .crypt(UUID(), CryptRequest(mode: .openFile, path: path, offset: 0)) |
| 412 | .onChange(of: session.unlockRequest, initial: true) { _, path in |
412 | }) |
| 413 | if let path { panel = .crypt(UUID(), CryptRequest(mode: .openFile, path: path, offset: 0)) } |
| |
| 414 | } |
| |
| 415 | .onChange(of: options, initial: true) { _, options in session.options = options } |
413 | .onChange(of: options, initial: true) { _, options in session.options = options } |
| 416 | .onAppear { TimerModel.shared.onDone = { [commands] in commands.show($0) } } |
414 | .onAppear { TimerModel.shared.onDone = { [commands] in commands.show($0) } } |
| 417 | .onReceive(NotificationCenter.default.publisher(for: UserDefaults.didChangeNotification)) { _ in defaultsChanges += 1 } |
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 | #expect(recoveryFiles.allSatisfy { !$0.contains(Self.secret) }) |
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 | @Test func aHeldFileIsReleasedWhenTheEntryGoes() throws { |
165 | @Test func aHeldFileIsReleasedWhenTheEntryGoes() throws { |
| 140 | session.saveMode = .automatic |
166 | session.saveMode = .automatic |
| 141 | session.autosaveDelay = .seconds(60) |
167 | session.autosaveDelay = .seconds(60) |