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 {
163 let pane = EditorPane() 163 let pane = EditorPane()
164 panes = [pane] 164 panes = [pane]
165 focused = pane 165 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 }
173 } 166 }
174 167
175 // MARK: - Encryption 168 // MARK: - Encryption
176 169
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
182 /// `org-crypt-use-before-save-magic`: entries with `cryptTag` are encrypted on every save. 170 /// `org-crypt-use-before-save-magic`: entries with `cryptTag` are encrypted on every save.
183 public var encryptsTaggedEntries: Bool { 171 public var encryptsTaggedEntries: Bool {
184 get { crypt.encryptsTaggedEntries } 172 get { crypt.encryptsTaggedEntries }
Sources/OrgDocument/Crypt.swift +1 −10 · view
@@ -32,7 +32,6 @@ public final class CryptVault: @unchecked Sendable {
32 private var revealed: [String: [String: String]] = [:] 32 private var revealed: [String: [String: String]] = [:]
33 /// By file: whether the encrypted file was armored when read. 33 /// By file: whether the encrypted file was armored when read.
34 private var armored: [String: Bool] = [:] 34 private var armored: [String: Bool] = [:]
35 private var onWrite: ((String) -> Void)?
36 35
37 public init() {} 36 public init() {}
38 37
@@ -59,12 +58,6 @@ public final class CryptVault: @unchecked Sendable {
59 set { locked { entryTag = newValue } } 58 set { locked { entryTag = newValue } }
60 } 59 }
61 60
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
68 public func passphrase(for path: String) -> String? { 61 public func passphrase(for path: String) -> String? {
69 locked { passphrases[Self.key(path)] } 62 locked { passphrases[Self.key(path)] }
70 } 63 }
@@ -175,11 +168,9 @@ public final class CryptVault: @unchecked Sendable {
175 /// The messages in a file `cipher` gave were written: reading it back gives their text. 168 /// The messages in a file `cipher` gave were written: reading it back gives their text.
176 public func didWrite(_ messages: [(message: String, plaintext: String)], to path: String) { 169 public func didWrite(_ messages: [(message: String, plaintext: String)], to path: String) {
177 let key = Self.key(path) 170 let key = Self.key(path)
178 let callback = locked { 171 locked {
179 for (message, plaintext) in messages { revealed[key, default: [:]][message] = plaintext } 172 for (message, plaintext) in messages { revealed[key, default: [:]][message] = plaintext }
180 return onWrite
181 } 173 }
182 callback?(path)
183 } 174 }
184 175
185 /// Whether `passphrase` opens the encrypted file `bytes`; an empty file takes any. 176 /// 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 {
64 var asked = 0 64 var asked = 0
65 session.storedPassphrase = { _ in asked += 1; return "pw" } 65 session.storedPassphrase = { _ in asked += 1; return "pw" }
66 var written: [String] = [] 66 var written: [String] = []
67 session.fileSaved = { written.append($0) } 67 session.fileWritten = { written.append($0) }
68 session.open(file.path) 68 session.open(file.path)
69 try replace(with: Self.file) 69 try replace(with: Self.file)
70 #expect(!session.save()) 70 #expect(!session.save())
@@ -98,10 +98,13 @@ struct CryptSaveTests {
98 98
99 @Test func holdsTheWholeFileWithoutThePassphrase() throws { 99 @Test func holdsTheWholeFileWithoutThePassphrase() throws {
100 try notes.write("a.org", "* Plain\nOpen text.\n") 100 try notes.write("a.org", "* Plain\nOpen text.\n")
101 var written: [String] = []
102 session.fileWritten = { written.append($0) }
101 session.open(file.path) 103 session.open(file.path)
102 try replace(with: Self.file) 104 try replace(with: Self.file)
103 #expect(!session.save()) 105 #expect(!session.save())
104 #expect(session.held[file.path] == .entries(1)) 106 #expect(session.held[file.path] == .entries(1))
107 #expect(written.isEmpty)
105 #expect(disk == "* Plain\nOpen text.\n") 108 #expect(disk == "* Plain\nOpen text.\n")
106 #expect(recoveryFiles.isEmpty) 109 #expect(recoveryFiles.isEmpty)
107 #expect(session.isDirty) 110 #expect(session.isDirty)
@@ -112,14 +115,27 @@ struct CryptSaveTests {
112 #expect(disk == "* Plain\nOpen text.\n") 115 #expect(disk == "* Plain\nOpen text.\n")
113 #expect(recoveryFiles.isEmpty) 116 #expect(recoveryFiles.isEmpty)
114 // The passphrase, when given, encrypts and saves. 117 // The passphrase, when given, encrypts and saves.
118 #expect(written.isEmpty)
115 #expect(session.unlock("pw", for: file.path) == nil) 119 #expect(session.unlock("pw", for: file.path) == nil)
116 #expect(session.held.isEmpty) 120 #expect(session.held.isEmpty)
117 #expect(!session.isDirty) 121 #expect(!session.isDirty)
122 #expect(written == [file.path])
118 #expect(!disk.contains(Self.secret)) 123 #expect(!disk.contains(Self.secret))
119 #expect(recoveryFiles.allSatisfy { !$0.contains(Self.secret) }) 124 #expect(recoveryFiles.allSatisfy { !$0.contains(Self.secret) })
120 #expect(session.openTexts[file.path] == nil) 125 #expect(session.openTexts[file.path] == nil)
121 } 126 }
122 127
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
123 @Test func aHeldFileIsReleasedWhenTheEntryGoes() throws { 139 @Test func aHeldFileIsReleasedWhenTheEntryGoes() throws {
124 session.saveMode = .automatic 140 session.saveMode = .automatic
125 session.autosaveDelay = .seconds(60) 141 session.autosaveDelay = .seconds(60)
@@ -222,7 +238,7 @@ struct CryptSaveTests {
222 let path = notes.url.appendingPathComponent("diary.org.gpg") 238 let path = notes.url.appendingPathComponent("diary.org.gpg")
223 try message.write(to: path) 239 try message.write(to: path)
224 var written: [String] = [] 240 var written: [String] = []
225 session.fileSaved = { written.append($0) } 241 session.fileWritten = { written.append($0) }
226 session.open(path.path) 242 session.open(path.path)
227 #expect(session.unlockRequest == path.path) 243 #expect(session.unlockRequest == path.path)
228 #expect(!session.isOpen(path.path)) 244 #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