Encrypt tagged entries on save; whole-file encryption !489

merged merged by cmc on 2026-11-26 10:40 UTC · krz/orgstar:m5-crypt into main

7 files changed, +101 −5

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

Layout: unified · split

Changed files (7)
Sources/OrgApp/Handoff.swift +2 · view
@@ -246,6 +246,8 @@ extension DocumentSession {
246246 /// in none of `roots` or an encrypted one.
247247 public func handoffPlace(_ path: String? = nil, roots: [String]) -> HandoffPlace? {
248248 guard let path = path ?? self.path, let text = document(of: path)?.text else { return nil }
249 // The outline and ID could name decrypted headings.
250 guard !crypt.isPrivate(text, for: path) else { return nil }
249251 return HandoffPlace.make(path: path, text: text, caret: caret(of: path) ?? 0, roots: roots, settings: defaults)
250252 }
251253}
Sources/OrgApp/SubtreeShare.swift +31 −2 · view
@@ -62,9 +62,38 @@ public struct SubtreeShare: Sendable, Identifiable {
6262}
6363
6464extension DocumentSession {
65 /// Share Subtree… for the entry at `offset` in the open file at `path`.
65 /// Share Subtree… for the entry at `offset` in the open file at `path`, its tagged entries
66 /// encrypted as they are on disk. Nil when there's no entry there, or `refusesSubtreeShare`.
6667 public func subtreeShare(_ path: String, at offset: Int) -> SubtreeShare? {
67 guard let text = document(of: path)?.text else { return nil }
68 guard refusesSubtreeShare(path, at: offset) == nil, var text = document(of: path)?.text else { return nil }
69 var offset = offset
70 if let (sealed, heading) = sealedSubtree(path, text: text, at: offset) {
71 text = sealed
72 offset = heading
73 }
6874 return SubtreeShare(text: text, at: offset, path: path, settings: defaults)
6975 }
76
77 /// Why the entry at `offset` in `path` isn't shared: its file is encrypted, or it has tagged
78 /// entries the file encrypts on disk and their passphrase isn't known.
79 public func refusesSubtreeShare(_ path: String, at offset: Int) -> String? {
80 let name = (path as NSString).lastPathComponent
81 if Self.isEncryptedFile(path) { return String(localized: "\(name) is encrypted; its entries aren't shared.") }
82 guard let text = document(of: path)?.text, crypt.sealsEntries(for: path), crypt.passphrase(for: path) == nil,
83 let range = subtreeRange(text, at: offset), !OrgCrypt.unencrypted(text, tag: cryptTag, within: range).isEmpty else { return nil }
84 return String(localized: "Tagged entries in the subtree aren't encrypted; give the passphrase with Encrypt… first.")
85 }
86
87 private func subtreeRange(_ text: String, at offset: Int) -> Range<Int>? {
88 guard let heading = OrgCrypt.heading(text, at: offset) else { return nil }
89 return heading..<max(heading + 1, OrgCrypt.bodyRange(text, at: heading)?.upperBound ?? heading)
90 }
91
92 /// `text` with the subtree's tagged entries encrypted, and where its heading starts.
93 private func sealedSubtree(_ path: String, text: String, at offset: Int) -> (String, Int)? {
94 guard crypt.sealsEntries(for: path), let passphrase = crypt.passphrase(for: path), let range = subtreeRange(text, at: offset),
95 !OrgCrypt.unencrypted(text, tag: cryptTag, within: range).isEmpty,
96 let sealed = try? OrgCrypt.seal(text, passphrase: passphrase, tag: cryptTag, within: range).text else { return nil }
97 return (sealed, range.lowerBound)
98 }
7099}
Sources/Orgstar/ContentView.swift +1 · view
@@ -1538,6 +1538,7 @@ struct ContentView: View {
15381538 copyRichText()
15391539 case "app.share-subtree":
15401540 guard let path = session.path else { return commands.show(String(localized: "No file is open")) }
1541 if let refusal = session.refusesSubtreeShare(path, at: session.caret) { return commands.show(refusal) }
15411542 guard let share = session.subtreeShare(path, at: session.caret) else { return commands.show(String(localized: "Not in a subtree")) }
15421543 panel = .shareSubtree(UUID(), share)
15431544 case "app.export-dialog":
Sources/OrgstarMobile/EditorScreen.swift +3 −1 · view
@@ -509,7 +509,9 @@ struct EditorScreen: View {
509509 } else if id == EditorScreen.exportSubtreeCommand {
510510 exportSubtree()
511511 } else if id == "app.share-subtree" {
512 if let caret = self.editor?.editContext().caret, let share = session.subtreeShare(path, at: caret) {
512 if let caret = self.editor?.editContext().caret, let refusal = session.refusesSubtreeShare(path, at: caret) {
513 message = refusal
514 } else if let caret = self.editor?.editContext().caret, let share = session.subtreeShare(path, at: caret) {
513515 sheet = .shareSubtree(share)
514516 } else {
515517 message = String(localized: "Not in a subtree")
Tests/OrgAppTests/CryptReviewTests.swift +28 · view
@@ -245,6 +245,34 @@ struct CryptReviewTests {
245245 #expect(try OpenPGP.decryptFile(Data(contentsOf: path), passphrase: "pw") == Data("210x* D\nbody\n".utf8))
246246 }
247247
248 // MR 490 hooks
249 @Test func handoffAndShareSubtreeKeepWhatIsEncrypted() throws {
250 let text = "* Bank :crypt:\nintro\n** Secret child\nbody\n* Open\n"
251 try notes.write("a.org", text)
252 session.open(file.path)
253 let child = (text as NSString).range(of: "** Secret").location
254 // Without the passphrase, the subtree isn't shared.
255 #expect(session.handoffPlace(file.path, roots: [notes.url.path]) == nil)
256 #expect(session.refusesSubtreeShare(file.path, at: child)?.contains("Encrypt…") == true)
257 #expect(session.subtreeShare(file.path, at: child) == nil)
258 session.cryptUsed("pw", for: file.path)
259 let share = try #require(session.subtreeShare(file.path, at: child))
260 #expect(!share.text.contains("body") && share.text.contains("-----BEGIN PGP MESSAGE-----"))
261 #expect(session.document?.text == text)
262 let bank = try #require(session.subtreeShare(file.path, at: 0))
263 #expect(!bank.text.contains("intro") && !bank.text.contains("Secret child"))
264 // An open file without tagged entries is advertised as before.
265 let plain = try notes.write("b.org", "* Plain\n")
266 session.open(plain.path)
267 #expect(session.handoffPlace(plain.path, roots: [notes.url.path]) != nil)
268 // An encrypted file's entries aren't shared.
269 let gpg = notes.url.appendingPathComponent("d.org.gpg")
270 try OpenPGP.encryptedMessage(Data("* D\nsecret\n".utf8), passphrase: "pw").write(to: gpg)
271 session.open(gpg.path)
272 #expect(session.unlock("pw", for: gpg.path) == nil)
273 #expect(session.refusesSubtreeShare(gpg.path, at: 0) != nil && session.subtreeShare(gpg.path, at: 0) == nil)
274 }
275
248276 // 9
249277 @Test func unchangedEntriesKeepTheirMessageAcrossSaves() throws {
250278 session.open(file.path)
docs/manual/guide/04-outlines.org +2 −2 · view
@@ -971,7 +971,7 @@ With =org-crypt-use-before-save-magic = true= in =config.toml=, every save, the
971971- When another program saves the file with tagged entries in plaintext, the file is held the same way until the passphrase lets orgstar keep a copy of that version encrypted and merge it. A version another program writes in the moment orgstar saves goes into the editor, between conflict markers if it conflicts, when no copy of it can be kept.
972972- An entry that the passphrase can't encrypt (text typed below a message encrypted with another passphrase, or to a public key) holds the file too; the notice says why, without an Encrypt… button.
973973- Closing the file, its window, or quitting while it's held asks for the passphrase, or whether to leave without saving; any other save that fails then offers Don't Save. On iOS, the edits stay in memory while orgstar is in the background, and the notice is there on return; if iOS ends orgstar meanwhile, the edits are lost, and orgstar says so when it next opens.
974- While an entry is decrypted in the editor, the agenda, widgets, the watch and reminders read the file from disk, where the entry is encrypted. The index, Spotlight and Quick Look read files from disk too. iCalendar export and publishing read such a file from disk and say so. Recalculating a table in Emacs, exporting with Emacs and =elisp:= links in Emacs aren't given a copy of the file. Clocking in to a heading inside such an entry shows "Encrypted entry" in the clock's history, widgets, the Live Activity and the menu bar.
974- While an entry is decrypted in the editor, the agenda, widgets, the watch and reminders read the file from disk, where the entry is encrypted. The index, Spotlight and Quick Look read files from disk too. iCalendar export and publishing read such a file from disk and say so. Recalculating a table in Emacs, exporting with Emacs and =elisp:= links in Emacs aren't given a copy of the file. Clocking in to a heading inside such an entry shows "Encrypted entry" in the clock's history, widgets, the Live Activity and the menu bar. Handoff doesn't offer the file, and Share Subtree… shares tagged entries encrypted as they are on disk, or, without the passphrase, not at all.
975975
976976* Encrypted files
977977
@@ -982,7 +982,7 @@ Opening one asks for its passphrase, or takes it from the Keychain after Touch I
982982The decrypted text is kept in memory only:
983983- Files are written with AES-256 and an iterated and salted SHA-256 key, binary, or armored when the file was armored.
984984- Recovery versions and hourly snapshots are kept encrypted with the file's passphrase, and Recovery Versions… shows them decrypted. Changes made to the file elsewhere are merged in memory, and a conflict is compared in memory.
985- The index, search, Spotlight, Quick Look, the agenda, widgets and the watch don't read the file's text. iCalendar export and publishing leave it out, and Emacs isn't given a copy of it. A clocked heading shows as "Encrypted entry" outside the editor.
985- The index, search, Spotlight, Quick Look, the agenda, widgets and the watch don't read the file's text. iCalendar export and publishing leave it out, and Emacs isn't given a copy of it. A clocked heading shows as "Encrypted entry" outside the editor. Handoff doesn't offer the file, and Share Subtree… refuses its entries.
986986- Archiving from =NAME.org.gpg= goes to =NAME_archive.org.gpg=, encrypted with the same passphrase (Org would write =NAME.org.gpg_archive=, unencrypted). Entries aren't refiled or copied from it to a file that isn't encrypted.
987987- Remembered passphrases are kept by the file's path, on this device only, readable while it's unlocked and after Touch ID, Face ID or the device password; renaming the file forgets it. Passphrases remembered by earlier versions move to this kind of item the first time they're used. The version of orgstar downloaded from the website can't make such items (it has no Keychain entitlement): it keeps remembered passphrases in the login keychain, not limited to this Mac, still asking for Touch ID or the password before one is used, and says so the first time.
988988
l10n/Localizable.xcstrings +34 · view
@@ -709,6 +709,40 @@
709709 }
710710 }
711711 },
712 "%@ is encrypted; its entries aren't shared.": {
713 "localizations": {
714 "de": {
715 "stringUnit": {
716 "state": "translated",
717 "value": "„%@“ ist verschlüsselt; seine Einträge werden nicht geteilt."
718 }
719 },
720 "es": {
721 "stringUnit": {
722 "state": "translated",
723 "value": "«%@» está cifrado; sus entradas no se comparten."
724 }
725 },
726 "fr": {
727 "stringUnit": {
728 "state": "translated",
729 "value": "« %@ » est chiffré ; ses entrées ne sont pas partagées."
730 }
731 },
732 "ja": {
733 "stringUnit": {
734 "state": "translated",
735 "value": "“%@”は暗号化されているため、エントリは共有されません。"
736 }
737 },
738 "pt-BR": {
739 "stringUnit": {
740 "state": "translated",
741 "value": "\"%@\" está criptografado; suas entradas não são compartilhadas."
742 }
743 }
744 }
745 },
712746 "%@ is saved encrypted with this passphrase.": {
713747 "localizations": {
714748 "de": {
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