Commit fbc792eb21

fbc792eb21866a31f8edcffd23f5105df3be2348

parent: 1e2be994af

Verified · cmc

cmc <hello@cleberg.net> · 2026-11-26 09:46 UTC

Handoff and Share Subtree keep what is encrypted

Closes #153

Layout: unified · split

Changed files (7)
Sources/OrgApp/Handoff.swift +2 · view
@@ -246,6 +246,8 @@ extension DocumentSession {
246 /// in none of `roots` or an encrypted one. 246 /// in none of `roots` or an encrypted one.
247 public func handoffPlace(_ path: String? = nil, roots: [String]) -> HandoffPlace? { 247 public func handoffPlace(_ path: String? = nil, roots: [String]) -> HandoffPlace? {
248 guard let path = path ?? self.path, let text = document(of: path)?.text else { return nil } 248 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 }
249 return HandoffPlace.make(path: path, text: text, caret: caret(of: path) ?? 0, roots: roots, settings: defaults) 251 return HandoffPlace.make(path: path, text: text, caret: caret(of: path) ?? 0, roots: roots, settings: defaults)
250 } 252 }
251} 253}
Sources/OrgApp/SubtreeShare.swift +31 −2 · view
@@ -62,9 +62,38 @@ public struct SubtreeShare: Sendable, Identifiable {
62} 62}
63 63
64extension DocumentSession { 64extension 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`.
66 public func subtreeShare(_ path: String, at offset: Int) -> SubtreeShare? { 67 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 }
68 return SubtreeShare(text: text, at: offset, path: path, settings: defaults) 74 return SubtreeShare(text: text, at: offset, path: path, settings: defaults)
69 } 75 }
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 }
70} 99}
Sources/Orgstar/ContentView.swift +1 · view
@@ -1538,6 +1538,7 @@ struct ContentView: View {
1538 copyRichText() 1538 copyRichText()
1539 case "app.share-subtree": 1539 case "app.share-subtree":
1540 guard let path = session.path else { return commands.show(String(localized: "No file is open")) } 1540 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) }
1541 guard let share = session.subtreeShare(path, at: session.caret) else { return commands.show(String(localized: "Not in a subtree")) } 1542 guard let share = session.subtreeShare(path, at: session.caret) else { return commands.show(String(localized: "Not in a subtree")) }
1542 panel = .shareSubtree(UUID(), share) 1543 panel = .shareSubtree(UUID(), share)
1543 case "app.export-dialog": 1544 case "app.export-dialog":
Sources/OrgstarMobile/EditorScreen.swift +3 −1 · view
@@ -509,7 +509,9 @@ struct EditorScreen: View {
509 } else if id == EditorScreen.exportSubtreeCommand { 509 } else if id == EditorScreen.exportSubtreeCommand {
510 exportSubtree() 510 exportSubtree()
511 } else if id == "app.share-subtree" { 511 } 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) {
513 sheet = .shareSubtree(share) 515 sheet = .shareSubtree(share)
514 } else { 516 } else {
515 message = String(localized: "Not in a subtree") 517 message = String(localized: "Not in a subtree")
Tests/OrgAppTests/CryptReviewTests.swift +28 · view
@@ -245,6 +245,34 @@ struct CryptReviewTests {
245 #expect(try OpenPGP.decryptFile(Data(contentsOf: path), passphrase: "pw") == Data("210x* D\nbody\n".utf8)) 245 #expect(try OpenPGP.decryptFile(Data(contentsOf: path), passphrase: "pw") == Data("210x* D\nbody\n".utf8))
246 } 246 }
247 247
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
248 // 9 276 // 9
249 @Test func unchangedEntriesKeepTheirMessageAcrossSaves() throws { 277 @Test func unchangedEntriesKeepTheirMessageAcrossSaves() throws {
250 session.open(file.path) 278 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
971- 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. 971- 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.
972- 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. 972- 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.
973- 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. 973- 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.
975 975
976* Encrypted files 976* Encrypted files
977 977
@@ -982,7 +982,7 @@ Opening one asks for its passphrase, or takes it from the Keychain after Touch I
982The decrypted text is kept in memory only: 982The decrypted text is kept in memory only:
983- Files are written with AES-256 and an iterated and salted SHA-256 key, binary, or armored when the file was armored. 983- Files are written with AES-256 and an iterated and salted SHA-256 key, binary, or armored when the file was armored.
984- 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. 984- 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.
986- 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. 986- 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.
987- 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. 987- 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.
988 988
l10n/Localizable.xcstrings +34 · view
@@ -709,6 +709,40 @@
709 } 709 }
710 } 710 }
711 }, 711 },
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 },
712 "%@ is saved encrypted with this passphrase.": { 746 "%@ is saved encrypted with this passphrase.": {
713 "localizations": { 747 "localizations": {
714 "de": { 748 "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