Avoid keyring unlock prompts on headless Linux (#63544)

## Why

Reading a locked Secret Service credential can trigger a graphical unlock
prompt even when Codex is running without a display.

## What changed

When both `DISPLAY` and `WAYLAND_DISPLAY` are unset or empty, read Secret
Service credentials with the default target only if they are already
unlocked. Preserve legacy lookup for missing entries and ambiguity errors
for multiple matches.

GitOrigin-RevId: d7f347edc05dcaaa12847f5e1a4aff5ea7f69cf9
This commit is contained in:
zm-oai
2026-11-24 05:32:26 +00:00
committed by copyberry
parent 96681d86f0
commit a90526e8a7
2 changed files with 61 additions and 1 deletions
+8 -1
View File
@@ -58,6 +58,9 @@ mod error_display_tests;
mod error_kind;
#[cfg(target_os = "linux")]
mod secret_service_read;
impl From<CredentialStoreError> for std::io::Error {
fn from(error: CredentialStoreError) -> Self {
Self::new(error_kind::classify(&error), error)
@@ -78,7 +81,11 @@ impl KeyringStore for DefaultKeyringStore {
fn load(&self, service: &str, account: &str) -> Result<Option<String>, CredentialStoreError> {
trace!("keyring.load start");
let entry = Entry::new(service, account).map_err(CredentialStoreError::new)?;
match entry.get_password() {
#[cfg(target_os = "linux")]
let password = secret_service_read::get_password(&entry, service, account);
#[cfg(not(target_os = "linux"))]
let password = entry.get_password();
match password {
Ok(password) => {
trace!("keyring.load success");
Ok(Some(password))
@@ -0,0 +1,53 @@
//! Headless reads must never ask the OS to display an unlock dialog.
use keyring::secret_service::SsCredential;
use keyring::secret_service::decode_error;
use secret_service::blocking;
use std::collections::HashMap;
pub(super) fn get_password(
entry: &keyring::Entry,
service: &str,
account: &str,
) -> keyring::Result<String> {
let has_display = ["DISPLAY", "WAYLAND_DISPLAY"]
.iter()
.any(|name| std::env::var_os(name).is_some_and(|value| !value.is_empty()));
let Some(credential) = entry.get_credential().downcast_ref::<SsCredential>() else {
return entry.get_password();
};
if has_display || credential.attributes.get("target").map(String::as_str) != Some("default") {
return entry.get_password();
}
let store = blocking::SecretService::connect(secret_service::EncryptionType::Dh)
.map_err(|error| keyring::Error::PlatformFailure(Box::new(error)))?;
let search = store
.search_items(HashMap::from([
("service", service),
("username", account),
("target", "default"),
]))
.map_err(decode_error)?;
let mut items = search.unlocked;
items.extend(search.locked);
let read = |item: &blocking::Item<'_>| {
item.ensure_unlocked().map_err(decode_error)?;
keyring::secret_service::get_item_password(item)
};
match items.as_slice() {
// keyring's legacy lookup already reads without opening an unlock dialog.
[] => credential
.map_matching_legacy_items(&store, read, true)?
.pop()
.ok_or(keyring::Error::NoEntry),
[item] => read(item),
items => {
let mut matches: Vec<Box<keyring::credential::Credential>> = Vec::new();
for item in items {
matches.push(Box::new(SsCredential::new_from_item(item)?));
}
Err(keyring::Error::Ambiguous(matches))
}
}
}