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, +19 −24

One commit's changes, read-only. Review comments go on all commits.

Layout: unified · split

Changed files (3)
Sources/OrgApp/DocumentSession.swift −12 · view
@@ -163,22 +163,10 @@ public final class DocumentSession {
163163 let pane = EditorPane()
164164 panes = [pane]
165165 focused = pane
166 crypt.written = { [weak self] path in
167 if Thread.isMainThread {
168 MainActor.assumeIsolated { self?.fileSaved?(path) }
169 } else {
170 Task { @MainActor in self?.fileSaved?(path) }
171 }
172 }
173166 }
174167
175168 // MARK: - Encryption
176169
177 /// Called with a file's path each time the session wrote it to disk: a buffer saved (by
178 /// hand or on its own), Keep Mine, and refile, archive or agenda changes to files that
179 /// aren't open. Not called for a save that was held, failed or conflicted.
180 @ObservationIgnored public var fileSaved: ((String) -> Void)?
181
182170 /// `org-crypt-use-before-save-magic`: entries with `cryptTag` are encrypted on every save.
183171 public var encryptsTaggedEntries: Bool {
184172 get { crypt.encryptsTaggedEntries }
Sources/OrgDocument/Crypt.swift +1 −10 · view
@@ -32,7 +32,6 @@ public final class CryptVault: @unchecked Sendable {
3232 private var revealed: [String: [String: String]] = [:]
3333 /// By file: whether the encrypted file was armored when read.
3434 private var armored: [String: Bool] = [:]
35 private var onWrite: ((String) -> Void)?
3635
3736 public init() {}
3837
@@ -59,12 +58,6 @@ public final class CryptVault: @unchecked Sendable {
5958 set { locked { entryTag = newValue } }
6059 }
6160
62 /// Called with a file's path each time it was written.
63 public var written: ((String) -> Void)? {
64 get { locked { onWrite } }
65 set { locked { onWrite = newValue } }
66 }
67
6861 public func passphrase(for path: String) -> String? {
6962 locked { passphrases[Self.key(path)] }
7063 }
@@ -175,11 +168,9 @@ public final class CryptVault: @unchecked Sendable {
175168 /// The messages in a file `cipher` gave were written: reading it back gives their text.
176169 public func didWrite(_ messages: [(message: String, plaintext: String)], to path: String) {
177170 let key = Self.key(path)
178 let callback = locked {
171 locked {
179172 for (message, plaintext) in messages { revealed[key, default: [:]][message] = plaintext }
180 return onWrite
181173 }
182 callback?(path)
183174 }
184175
185176 /// Whether `passphrase` opens the encrypted file `bytes`; an empty file takes any.
Tests/OrgAppTests/CryptSaveTests.swift +18 −2 · view
@@ -64,7 +64,7 @@ struct CryptSaveTests {
6464 var asked = 0
6565 session.storedPassphrase = { _ in asked += 1; return "pw" }
6666 var written: [String] = []
67 session.fileSaved = { written.append($0) }
67 session.fileWritten = { written.append($0) }
6868 session.open(file.path)
6969 try replace(with: Self.file)
7070 #expect(!session.save())
@@ -98,10 +98,13 @@ struct CryptSaveTests {
9898
9999 @Test func holdsTheWholeFileWithoutThePassphrase() throws {
100100 try notes.write("a.org", "* Plain\nOpen text.\n")
101 var written: [String] = []
102 session.fileWritten = { written.append($0) }
101103 session.open(file.path)
102104 try replace(with: Self.file)
103105 #expect(!session.save())
104106 #expect(session.held[file.path] == .entries(1))
107 #expect(written.isEmpty)
105108 #expect(disk == "* Plain\nOpen text.\n")
106109 #expect(recoveryFiles.isEmpty)
107110 #expect(session.isDirty)
@@ -112,14 +115,27 @@ struct CryptSaveTests {
112115 #expect(disk == "* Plain\nOpen text.\n")
113116 #expect(recoveryFiles.isEmpty)
114117 // The passphrase, when given, encrypts and saves.
118 #expect(written.isEmpty)
115119 #expect(session.unlock("pw", for: file.path) == nil)
116120 #expect(session.held.isEmpty)
117121 #expect(!session.isDirty)
122 #expect(written == [file.path])
118123 #expect(!disk.contains(Self.secret))
119124 #expect(recoveryFiles.allSatisfy { !$0.contains(Self.secret) })
120125 #expect(session.openTexts[file.path] == nil)
121126 }
122127
128 @Test func restoringAVersionOfAHeldFileKeepsNoPlaintext() throws {
129 try notes.write("a.org", "* Plain\nOpen text.\n")
130 session.open(file.path)
131 try replace(with: Self.file)
132 #expect(!session.save())
133 #expect(session.restore(text: "* Plain\nOpen text.\n* Other\n") == nil)
134 #expect(recoveryFiles.isEmpty)
135 #expect(session.save())
136 #expect(recoveryFiles.allSatisfy { !$0.contains(Self.secret) })
137 }
138
123139 @Test func aHeldFileIsReleasedWhenTheEntryGoes() throws {
124140 session.saveMode = .automatic
125141 session.autosaveDelay = .seconds(60)
@@ -222,7 +238,7 @@ struct CryptSaveTests {
222238 let path = notes.url.appendingPathComponent("diary.org.gpg")
223239 try message.write(to: path)
224240 var written: [String] = []
225 session.fileSaved = { written.append($0) }
241 session.fileWritten = { written.append($0) }
226242 session.open(path.path)
227243 #expect(session.unlockRequest == path.path)
228244 #expect(!session.isOpen(path.path))
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form