fix(core): cover Linux keyring access with a pure-Rust Secret Service client

The keyring crate's Linux backend requires C libdbus, which cross builds
(zigbuild) cannot link, so Linux desktop users hit the same
desktop-migrated-but-MCP-blocked failure that macOS/Windows users hit in
0.4.94 - with no native build to fix it on. Replace the Linux keyring
path with the secret-service crate (pure-Rust zbus, no C dependencies,
speaks the session-bus protocol directly), keeping the exact on-disk
attribute layout the keyring crate writes (target/service/username,
with the legacy no-target fallback) so existing desktop-provisioned
keys remain readable both ways.

- connect/collection failures (no session bus, headless servers) map to
  a missing provider so callers keep falling back to the managed
  .dbx/secret.key file exactly as before
- found-but-unreadable keyring items now fail fast with a specific
  KEYRING_ACCESS_FAILED code on every platform instead of silently
  provisioning a new key that would strand existing ciphertext
- the Linux zigbuild MCP/CLI package legs build with os-keyring
This commit is contained in:
t8y2 committed 2026-09-24 13:53:14 +08:00
1 parent df31d9baf3
commit 734503f4cf
4 files changed
+105 -35

No files matched your search

+4 -4
View File
@@ -424,7 +424,7 @@ jobs:
shell: bash shell: bash
run: | run: |
if [[ "${{ runner.os }}" == "Linux" ]]; then if [[ "${{ runner.os }}" == "Linux" ]]; then
cargo zigbuild --release -p dbx-cli --no-default-features --target "${{ matrix.target }}.2.31" cargo zigbuild --release -p dbx-cli --no-default-features --features dbx-mcp/os-keyring --target "${{ matrix.target }}.2.31"
else else
# Same keychain rationale as the MCP native legs; the feature is # Same keychain rationale as the MCP native legs; the feature is
# forwarded through the dbx-mcp dependency. # forwarded through the dbx-mcp dependency.
@@ -662,9 +662,9 @@ jobs:
shell: bash shell: bash
run: | run: |
if [[ "${{ runner.os }}" == "Linux" ]]; then if [[ "${{ runner.os }}" == "Linux" ]]; then
# Linux zigbuild cannot link the dbus Secret Service backend; the # Pure-Rust (zbus) Secret Service backend: no C libdbus, so the
# managed .dbx/secret.key file remains the key material source. # zigbuild legs can read desktop-provisioned keys too.
cargo zigbuild --release -p dbx-mcp --target "${{ matrix.target }}.2.31" cargo zigbuild --release -p dbx-mcp --features os-keyring --target "${{ matrix.target }}.2.31"
else else
# Native macOS/Windows builds read the OS keychain key that DBX # Native macOS/Windows builds read the OS keychain key that DBX
# Desktop provisions during the data security upgrade, so the MCP # Desktop provisions during the data security upgrade, so the MCP
Generated
+3 -11
View File
@@ -2190,6 +2190,7 @@ dependencies = [
"rusqlite", "rusqlite",
"rustls 0.23.43", "rustls 0.23.43",
"rustls-pemfile 2.2.0", "rustls-pemfile 2.2.0",
"secret-service",
"serde", "serde",
"serde_json", "serde_json",
"serde_yaml_ng", "serde_yaml_ng",
@@ -4862,7 +4863,6 @@ checksum = "eebcc3aff044e5944a8fbaf69eb277d11986064cba30c468730e8b9909fb551c"
dependencies = [ dependencies = [
"byteorder", "byteorder",
"dbus-secret-service", "dbus-secret-service",
"linux-keyutils",
"log", "log",
"secret-service", "secret-service",
"security-framework 2.11.1", "security-framework 2.11.1",
@@ -5020,16 +5020,6 @@ dependencies = [
"vcpkg", "vcpkg",
] ]
[[package]]
name = "linux-keyutils"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "83270a18e9f90d0707c41e9f35efada77b64c0e6f3f1810e71c8368a864d5590"
dependencies = [
"bitflags 2.13.1",
"libc",
]
[[package]] [[package]]
name = "linux-raw-sys" name = "linux-raw-sys"
version = "0.12.1" version = "0.12.1"
@@ -9711,6 +9701,7 @@ dependencies = [
"signal-hook-registry", "signal-hook-registry",
"socket2 0.6.5", "socket2 0.6.5",
"tokio-macros", "tokio-macros",
"tracing",
"windows-sys 0.61.2", "windows-sys 0.61.2",
] ]
@@ -11677,6 +11668,7 @@ dependencies = [
"serde_repr", "serde_repr",
"sha1 0.10.7", "sha1 0.10.7",
"static_assertions", "static_assertions",
"tokio",
"tracing", "tracing",
"uds_windows", "uds_windows",
"windows-sys 0.52.0", "windows-sys 0.52.0",
+14 -7
View File
@@ -15,9 +15,11 @@ sqlite-multiple-ciphers = ["dbx-drivers/sqlite-multiple-ciphers"]
# (dbx-cli, dbx-mcp); do not combine with the cipher features. # (dbx-cli, dbx-mcp); do not combine with the cipher features.
sqlite-bundled = ["rusqlite/bundled", "dbx-drivers/sqlite-bundled"] sqlite-bundled = ["rusqlite/bundled", "dbx-drivers/sqlite-bundled"]
openapi = ["dbx-types/openapi", "dbx-sql/openapi"] openapi = ["dbx-types/openapi", "dbx-sql/openapi"]
# OS keyring storage for the secret-store key material (macOS/Windows keychain, # OS keyring storage for the secret-store key material: macOS/Windows
# Linux dbus Secret Service with persistent fallback). Desktop only. # keychain via the keyring crate, Linux Secret Service via the pure-Rust
os-keyring = ["dep:keyring", "keyring/linux-native-sync-persistent"] # secret-service/zbus client so cross builds (zigbuild) link no C libdbus.
# Desktop and native package builds enable this.
os-keyring = ["dep:keyring", "dep:secret-service"]
system-fonts = ["font-kit"] system-fonts = ["font-kit"]
[dependencies] [dependencies]
@@ -67,10 +69,9 @@ aes-gcm = "0.10"
aes = "0.8" aes = "0.8"
cbc = "0.1" cbc = "0.1"
argon2 = "0.5" argon2 = "0.5"
# The Linux dbus Secret Service backend (linux-native-sync-persistent) pulls # macOS/Windows keychain backend. The crate's own Linux backend requires
# libdbus-sys, whose pkg-config build script cannot cross-compile (musl static # C libdbus (cannot cross-compile); Linux uses the secret-service crate below
# browser, docker zigbuild). It stays opt-in so server/cross builds skip it and # with the same on-disk attribute layout instead.
# fall back to the managed .dbx/secret.key file.
keyring = { version = "3.6.3", default-features = false, optional = true, features = ["apple-native", "windows-native", "crypto-rust"] } keyring = { version = "3.6.3", default-features = false, optional = true, features = ["apple-native", "windows-native", "crypto-rust"] }
sha2 = "0.10" sha2 = "0.10"
async-trait = "0.1" async-trait = "0.1"
@@ -99,3 +100,9 @@ tokio = { version = "1", features = ["test-util"] }
[target.'cfg(all(unix, not(target_os = "redox")))'.dependencies] [target.'cfg(all(unix, not(target_os = "redox")))'.dependencies]
nix = { version = "0.31.3", features = ["user"] } nix = { version = "0.31.3", features = ["user"] }
[target.'cfg(target_os = "linux")'.dependencies]
# Pure-Rust (zbus) Secret Service client; speaks the session-bus protocol
# directly so zigbuild targets need no system libdbus. Attribute names match
# what the keyring crate writes (target/service/username) for interop.
secret-service = { version = "4.0", optional = true, default-features = false, features = ["rt-tokio-crypto-rust"] }
+84 -13
View File
@@ -20,6 +20,12 @@ const PREFIX: &str = "dbxenc1";
const KEYRING_SERVICE: &str = "com.dbx.app.secret-store.v1"; const KEYRING_SERVICE: &str = "com.dbx.app.secret-store.v1";
#[cfg(feature = "os-keyring")] #[cfg(feature = "os-keyring")]
const KEYRING_USER: &str = "local-data-encryption-key"; const KEYRING_USER: &str = "local-data-encryption-key";
// Secret Service attribute layout written by the keyring crate (the desktop
// writer): target is always present and defaults to "default"; entries from
// older keyring versions may lack it, so lookups fall back to the two-attribute
// search.
#[cfg(all(feature = "os-keyring", target_os = "linux"))]
const KEYRING_TARGET: &str = "default";
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SecretKeyPolicy { pub enum SecretKeyPolicy {
@@ -131,7 +137,7 @@ impl SecretCodec {
platform_provider: F, platform_provider: F,
) -> Result<SecretKeyResolution, String> ) -> Result<SecretKeyResolution, String>
where where
F: FnOnce(bool) -> Option<SecretCodec>, F: FnOnce(bool) -> Result<Option<SecretCodec>, String>,
{ {
let compatibility_error = if let Some(path) = compatibility_path.as_ref().filter(|path| path.exists()) { let compatibility_error = if let Some(path) = compatibility_path.as_ref().filter(|path| path.exists()) {
match Self::read_key_file(path, false) { match Self::read_key_file(path, false) {
@@ -141,8 +147,13 @@ impl SecretCodec {
} else { } else {
None None
}; };
if let Some(codec) = platform_provider(allow_create) { // A keychain item that exists but cannot be read must not silently
return Ok(SecretKeyResolution { codec, source: SecretKeySource::PlatformStore }); // fall through to provisioning a brand-new key, which would strand the
// existing ciphertext behind the wrong key.
match platform_provider(allow_create) {
Ok(Some(codec)) => return Ok(SecretKeyResolution { codec, source: SecretKeySource::PlatformStore }),
Ok(None) => {}
Err(detail) => return Err(detail),
} }
if allow_create { if allow_create {
if let Some(path) = compatibility_path { if let Some(path) = compatibility_path {
@@ -248,29 +259,89 @@ impl SecretCodec {
} }
} }
#[cfg(feature = "os-keyring")] #[cfg(all(feature = "os-keyring", any(target_os = "macos", target_os = "windows")))]
fn platform_keyring_codec(allow_create: bool) -> Option<SecretCodec> { fn platform_keyring_codec(allow_create: bool) -> Result<Option<SecretCodec>, String> {
let entry = keyring::Entry::new(KEYRING_SERVICE, KEYRING_USER).ok()?; let entry = keyring::Entry::new(KEYRING_SERVICE, KEYRING_USER)
.map_err(|error| format!("keyring entry unavailable: {error}"))?;
match entry.get_password() { match entry.get_password() {
Ok(value) => return SecretCodec::from_key_material(value.trim()).ok(), Ok(value) => return Ok(SecretCodec::from_key_material(value.trim()).ok()),
Err(keyring::Error::NoEntry) => {} Err(keyring::Error::NoEntry) => {}
Err(_) => return None, // Distinguish a present-but-unreadable keychain item (ACL denial,
// locked keychain) from a missing one: callers surface this class in
// diagnostics instead of the generic provider-unavailable code.
Err(error) => return Err(format!("KEYRING_ACCESS_FAILED: {error}")),
} }
if !allow_create { if !allow_create {
return None; return Ok(None);
} }
let mut key = [0u8; 32]; let mut key = [0u8; 32];
OsRng.fill_bytes(&mut key); OsRng.fill_bytes(&mut key);
let encoded = base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(key); let encoded = base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(key);
entry.set_password(&encoded).ok()?; entry.set_password(&encoded).map_err(|error| format!("KEYRING_WRITE_FAILED: {error}"))?;
Some(SecretCodec::new(key)) Ok(Some(SecretCodec::new(key)))
}
#[cfg(all(feature = "os-keyring", target_os = "linux"))]
fn platform_keyring_codec(allow_create: bool) -> Result<Option<SecretCodec>, String> {
use secret_service::{blocking::SecretService, EncryptionType};
use std::collections::HashMap;
// A missing session bus (headless server/container) means the provider is
// absent rather than broken; return None so callers fall back to the
// managed .dbx/secret.key file exactly as before.
let service = match SecretService::connect(EncryptionType::Dh) {
Ok(service) => service,
Err(_) => return Ok(None),
};
let collection = match service.get_default_collection() {
Ok(collection) => collection,
Err(_) => return Ok(None),
};
let mut attributes = HashMap::new();
attributes.insert("target", KEYRING_TARGET);
attributes.insert("service", KEYRING_SERVICE);
attributes.insert("username", KEYRING_USER);
let mut found = collection
.search_items(attributes)
.map_err(|error| format!("KEYRING_ACCESS_FAILED: secret search failed: {error}"))?;
if found.is_empty() {
let mut legacy = HashMap::new();
legacy.insert("service", KEYRING_SERVICE);
legacy.insert("username", KEYRING_USER);
found = collection
.search_items(legacy)
.map_err(|error| format!("KEYRING_ACCESS_FAILED: secret search failed: {error}"))?;
}
let item = found.into_iter().next();
match item {
Some(item) => {
let secret =
item.get_secret().map_err(|error| format!("KEYRING_ACCESS_FAILED: secret read failed: {error}"))?;
let material = String::from_utf8(secret).map_err(|_| "SECRET_KEY_INVALID".to_string())?;
Ok(SecretCodec::from_key_material(material.trim()).ok())
}
None if allow_create => {
let mut key = [0u8; 32];
OsRng.fill_bytes(&mut key);
let encoded = base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(key);
let mut created = HashMap::new();
created.insert("target", KEYRING_TARGET);
created.insert("service", KEYRING_SERVICE);
created.insert("username", KEYRING_USER);
collection
.create_item("DBX secret-store key", created, encoded.as_bytes(), true, "text/plain")
.map_err(|error| format!("KEYRING_WRITE_FAILED: {error}"))?;
Ok(Some(SecretCodec::new(key)))
}
None => Ok(None),
}
} }
#[cfg(not(feature = "os-keyring"))] #[cfg(not(feature = "os-keyring"))]
fn platform_keyring_codec(_allow_create: bool) -> Option<SecretCodec> { fn platform_keyring_codec(_allow_create: bool) -> Result<Option<SecretCodec>, String> {
// Cross/server builds compile without the OS keyring backend; the managed // Cross/server builds compile without the OS keyring backend; the managed
// .dbx/secret.key file remains the key material source. // .dbx/secret.key file remains the key material source.
None Ok(None)
} }
fn create_managed_key(path: &std::path::Path) -> Result<SecretCodec, String> { fn create_managed_key(path: &std::path::Path) -> Result<SecretCodec, String> {