Keeping passwords off the drag pasteboard, and wiping them from memory
You open an encrypted ZIP in Purrchiver, type the password, and drag a file from the list into Finder. Purrchiver can’t extract that file until you drop it, and extracting it needs the password. So the password has to travel with the drag somehow.
The first version of drag-out did the obvious thing: the dragged value was a small struct with the archive URL, the entry path, and the password. That’s the bug this post is about.
The drag pasteboard is shared
A drag on macOS goes through a named system pasteboard, the drag pasteboard. Like the general pasteboard, it’s a shared system resource, not private to your app. I treat anything placed on it as readable by other processes for as long as the drag lasts.
With SwiftUI’s Transferable, which representation gets written out, and
when, is decided by the framework. I didn’t want a password’s safety to
depend on those details, or on them staying the same across OS releases. The
rule became simple: nothing secret goes in a value that’s dragged.
A token instead of the password
The dragged value carries a random UUID instead. The password stays in an actor inside the app’s own process, keyed by that UUID:
actor PassphraseRegistry {
private var entries: [UUID: [UInt8]] = [:]
private var everIssued: Set<UUID> = []
func issue(_ passphrase: String) -> UUID {
let token = UUID()
entries[token] = Array(passphrase.utf8)
everIssued.insert(token)
return token
}
func resolve(_ token: UUID) -> String? {
entries[token].map { String(decoding: $0, as: UTF8.self) }
}
}A token is issued when you unlock an archive. When the drop arrives, the extraction code resolves it inside the process. Anyone reading the pasteboard sees a random UUID that means nothing outside Purrchiver.
The token is revoked whenever the window leaves its “archive loaded” state: you close the window or the archive, or open a different one. A drag that’s still in flight then fails cleanly. The registry remembers which tokens it has ever issued, so it can tell “this token was revoked” apart from “this token was never real.” The first means the archive was closed mid-drag, and the user can be told to reopen it and try again.
In the same round of fixes as the token, the password also left the view.
It used to sit in SwiftUI @State. Now the view holds only the token, so the
plaintext isn’t part of the view’s state at all.
In late May, drag-out moved from SwiftUI to an AppKit file promise, so that
filenames like .gitignore arrive in Finder exactly as named. The token
design carried over unchanged. The promise’s delegate holds a token, never a
password.
Wiping the stored copy
The registry originally stored [UUID: String]. Revoking a token dropped the
string, which freed its memory but didn’t clear it. The password stayed in
freed heap until the allocator happened to reuse that block.
On June 7 the storage changed to raw UTF-8 bytes, cleared when the token is revoked:
func revoke(_ token: UUID) {
guard var bytes = entries.removeValue(forKey: token) else { return }
bytes.withUnsafeMutableBytes { raw in
guard let base = raw.baseAddress, raw.count > 0 else { return }
_ = memset_s(base, raw.count, 0, raw.count)
}
}Two details matter here:
memset_s, not a loop or plainmemset. The compiler can remove a write to memory that’s about to be freed, since nothing reads it again.memset_sis defined so that it can’t be removed.removeValuefirst. It hands back the dictionary’s only reference to the array, so the wipe hits the real buffer. If the array were still shared, writing to it would trigger copy-on-write: you’d zero a fresh copy and leave the original untouched.
The password you type when creating an archive gets the same treatment. It’s
held as bytes, cleared when the operation ends, and turned into a String
only for the duration of the call that needs one.
What can’t be wiped
This part matters most, because “we zero passwords” is easy to oversell.
- The text field’s string. You type the password into a SwiftUI
SecureFieldbound to a SwiftString. Swift has no supported way to zero aString’s storage, and the runtime is free to copy it. - The copy at the engine boundary. The archive engines take the password
as a
String, soresolvebuilds a new one each time. That copy lives only as long as the operation, but I can’t clear it. - Copies inside the C libraries. libarchive, for example,
strdups the password internally. It does clear that copy withmemsetwhen the archive handle is freed. Whether a plainmemsetright beforefreesurvives the optimizer in that build is something I haven’t verified.
So the zeroing is a real improvement, not a guarantee. The long-lived copy, the one that would otherwise sit around for the whole session, is cleared the moment the session ends. The short-lived copies are only as safe as Swift and the C libraries make them.
The guarantees that do hold
The stronger promises are about where the password never goes:
- never onto any pasteboard (drags carry only a token)
- never written to disk, preferences, or logs
- never sent anywhere: Purrchiver makes no network connections and has no third-party SDKs
Those are properties of the code paths, not of memory management, so they hold. Memory scrubbing on top of them limits how long a plaintext copy lingers. It doesn’t make the process memory secret.
Two input rules sit at the same boundary. Passwords containing a NUL or control character are rejected, because a C string would silently cut them short at the NUL. Non-ASCII passwords aren’t supported yet for encrypted ZIP, 7z, and RAR. Purrchiver rejects them up front with an error that says so, instead of letting them fail as a wrong password.
What to take away
- Treat a drag payload like a clipboard copy. Put a handle in it, not the secret, and resolve the handle in-process when the drop arrives.
- Store long-lived secrets as bytes you own, and zero them with
memset_safter removing the last reference. - Write down what you can’t wipe. The honest list is short. Leaving it out makes the feature sound stronger than it is.