mirror of
https://github.com/block/buzz.git
synced 2026-11-26 19:21:50 +00:00
fix(desktop): switch macOS keychain to Data Protection Keychain (#1264)
Signed-off-by: Will Pfleger <pfleger.will@gmail.com> Co-authored-by: npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7 <dcfd242e557282d7a1e2cf2e6877522682f1e5c6156dc92ca7d90eaedd3b0f95@sprout-oss.stage.blox.sqprod.co>
This commit is contained in:
@@ -35,6 +35,7 @@ ctrlc = { version = "3", features = ["termination"] }
|
||||
[target.'cfg(target_os = "macos")'.dependencies]
|
||||
objc2-app-kit = { version = "0.3.2", default-features = false, features = ["NSHapticFeedback"] }
|
||||
keyring = { version = "3.6.3", default-features = false, features = ["apple-native", "vendored"], optional = true }
|
||||
security-framework = { version = "3.7.0", features = ["OSX_10_15"] }
|
||||
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
windows-sys = { version = "0.61", features = ["Win32_Storage_FileSystem", "Win32_System_JobObjects", "Win32_System_Threading", "Win32_Foundation"] }
|
||||
|
||||
@@ -63,10 +63,47 @@ fn keyring_entry(service: &str, key: &str) -> Result<keyring::Entry, keyring::Er
|
||||
keyring::Entry::new(service, key)
|
||||
}
|
||||
|
||||
// macOS-specific imports for the Data Protection Keychain backend.
|
||||
#[cfg(all(feature = "system-keyring", target_os = "macos"))]
|
||||
use security_framework::base::Error as SFError;
|
||||
#[cfg(all(feature = "system-keyring", target_os = "macos"))]
|
||||
use security_framework::passwords::{
|
||||
delete_generic_password_options, generic_password, set_generic_password_options,
|
||||
PasswordOptions,
|
||||
};
|
||||
|
||||
/// Returns true when the security-framework error is "item not found" (-25300).
|
||||
#[cfg(all(feature = "system-keyring", target_os = "macos"))]
|
||||
fn is_not_found(e: &SFError) -> bool {
|
||||
e.code() == -25300
|
||||
}
|
||||
|
||||
/// Build a `PasswordOptions` for the Data Protection Keychain.
|
||||
#[cfg(all(feature = "system-keyring", target_os = "macos"))]
|
||||
fn dpk_opts(service: &str, key: &str) -> PasswordOptions {
|
||||
let mut opts = PasswordOptions::new_generic_password(service, key);
|
||||
opts.use_protected_keychain();
|
||||
opts
|
||||
}
|
||||
|
||||
impl SecretStore {
|
||||
/// Probe whether `key` exists and whether the backend is reachable.
|
||||
pub fn probe(&self, key: &str) -> KeyringProbe {
|
||||
#[cfg(feature = "system-keyring")]
|
||||
// macOS: probe the Data Protection Keychain only. Items still in the
|
||||
// old keychain will be migrated on the first `load` call.
|
||||
#[cfg(all(feature = "system-keyring", target_os = "macos"))]
|
||||
{
|
||||
match generic_password(dpk_opts(&self.service, key)) {
|
||||
Ok(_) => KeyringProbe::Present,
|
||||
Err(ref e) if is_not_found(e) => KeyringProbe::ReachableButEmpty,
|
||||
Err(ref e) if is_keyring_availability_error(&e.to_string()) => {
|
||||
KeyringProbe::Unreachable
|
||||
}
|
||||
Err(_) => KeyringProbe::ReachableButEmpty,
|
||||
}
|
||||
}
|
||||
// Non-macOS system-keyring path (Windows, Linux).
|
||||
#[cfg(all(feature = "system-keyring", not(target_os = "macos")))]
|
||||
{
|
||||
match keyring_entry(&self.service, key) {
|
||||
Ok(entry) => match entry.get_password() {
|
||||
@@ -75,8 +112,6 @@ impl SecretStore {
|
||||
Err(e) if is_keyring_availability_error(&e.to_string()) => {
|
||||
KeyringProbe::Unreachable
|
||||
}
|
||||
// A non-availability per-entry error (e.g. bad attributes)
|
||||
// means the backend is reachable but the entry is unusable.
|
||||
Err(_) => KeyringProbe::ReachableButEmpty,
|
||||
},
|
||||
Err(e) if is_keyring_availability_error(&e.to_string()) => {
|
||||
@@ -95,7 +130,35 @@ impl SecretStore {
|
||||
/// Load the secret for `key`. `Ok(None)` when there is no entry; `Err` only
|
||||
/// when the backend errored in a way that is not "missing".
|
||||
pub fn load(&self, key: &str) -> Result<Option<String>, String> {
|
||||
#[cfg(feature = "system-keyring")]
|
||||
// macOS: try Data Protection Keychain first; fall back to old keychain
|
||||
// and migrate on a miss (one-time per item).
|
||||
#[cfg(all(feature = "system-keyring", target_os = "macos"))]
|
||||
{
|
||||
match generic_password(dpk_opts(&self.service, key)) {
|
||||
Ok(bytes) => String::from_utf8(bytes)
|
||||
.map(Some)
|
||||
.map_err(|e| format!("keyring utf8: {e}")),
|
||||
Err(ref e) if is_not_found(e) => {
|
||||
// Not in DPK — check old keychain and migrate if found.
|
||||
let entry = keyring_entry(&self.service, key)
|
||||
.map_err(|e| format!("keyring entry: {e}"))?;
|
||||
match entry.get_password() {
|
||||
Ok(old_val) => {
|
||||
// Migrate to DPK.
|
||||
self.store(key, &old_val)?;
|
||||
// Best-effort cleanup from old keychain.
|
||||
let _ = entry.delete_credential();
|
||||
Ok(Some(old_val))
|
||||
}
|
||||
Err(keyring::Error::NoEntry) => Ok(None),
|
||||
Err(e) => Err(format!("keyring get: {e}")),
|
||||
}
|
||||
}
|
||||
Err(e) => Err(format!("keyring get: {e}")),
|
||||
}
|
||||
}
|
||||
// Non-macOS system-keyring path (Windows, Linux).
|
||||
#[cfg(all(feature = "system-keyring", not(target_os = "macos")))]
|
||||
{
|
||||
let entry =
|
||||
keyring_entry(&self.service, key).map_err(|e| format!("keyring entry: {e}"))?;
|
||||
@@ -115,7 +178,14 @@ impl SecretStore {
|
||||
/// Store `value` for `key`. Reports `Err` on availability failures — callers
|
||||
/// decide whether to fall back to file storage.
|
||||
pub fn store(&self, key: &str, value: &str) -> Result<(), String> {
|
||||
#[cfg(feature = "system-keyring")]
|
||||
// macOS: write directly to the Data Protection Keychain.
|
||||
#[cfg(all(feature = "system-keyring", target_os = "macos"))]
|
||||
{
|
||||
set_generic_password_options(value.as_bytes(), dpk_opts(&self.service, key))
|
||||
.map_err(|e| format!("keyring set: {e}"))
|
||||
}
|
||||
// Non-macOS system-keyring path (Windows, Linux).
|
||||
#[cfg(all(feature = "system-keyring", not(target_os = "macos")))]
|
||||
{
|
||||
let entry =
|
||||
keyring_entry(&self.service, key).map_err(|e| format!("keyring entry: {e}"))?;
|
||||
@@ -132,7 +202,28 @@ impl SecretStore {
|
||||
|
||||
/// Delete the secret for `key`. A missing entry is not an error.
|
||||
pub fn delete(&self, key: &str) -> Result<(), String> {
|
||||
#[cfg(feature = "system-keyring")]
|
||||
// macOS: delete from both DPK and old keychain (best-effort on old).
|
||||
#[cfg(all(feature = "system-keyring", target_os = "macos"))]
|
||||
{
|
||||
// Delete from Data Protection Keychain; missing is fine.
|
||||
match delete_generic_password_options(dpk_opts(&self.service, key)) {
|
||||
Ok(()) => {}
|
||||
Err(ref e) if is_not_found(e) => {}
|
||||
Err(e) => return Err(format!("keyring delete: {e}")),
|
||||
}
|
||||
// Best-effort cleanup from old keychain.
|
||||
if let Ok(entry) = keyring_entry(&self.service, key) {
|
||||
match entry.delete_credential() {
|
||||
Ok(()) | Err(keyring::Error::NoEntry) => {}
|
||||
Err(e) => {
|
||||
eprintln!("buzz-desktop: old-keychain delete for {key}: {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
// Non-macOS system-keyring path (Windows, Linux).
|
||||
#[cfg(all(feature = "system-keyring", not(target_os = "macos")))]
|
||||
{
|
||||
let entry =
|
||||
keyring_entry(&self.service, key).map_err(|e| format!("keyring entry: {e}"))?;
|
||||
|
||||
Reference in New Issue
Block a user