Keep long Microsoft sign-in secrets in several keychain items
Credential Manager takes at most 2560 bytes per item. The secrets of a Microsoft sign-in (the refresh token and the Minecraft token) are longer, so saving them failed on Windows with "password encoded as UTF-16 is longer than platform limit of 2560 chars" and the sign-in did not finish. On macOS and Windows a secret is now split over the items `<uuid>`, `<uuid>.1`, `<uuid>.2`... of at most 1000 UTF-8 bytes each (under 2560 bytes of UTF-16). Reads join the pieces, a write removes the leftovers of a longer secret, and sign-out removes all of them. Existing single items keep working. Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -74,7 +74,8 @@ one is selected. "Sign out" deletes the account's secrets from the keychain.
|
||||
- Secrets (the Microsoft refresh token, the Minecraft access token and its expiry) go to the OS
|
||||
keychain, one item per account: Keychain on macOS, Credential Manager on Windows, the Secret
|
||||
Service (GNOME Keyring, KWallet) on Linux. Service name `dev.playhorizon.launcher`, account = the
|
||||
Minecraft UUID.
|
||||
Minecraft UUID. Credential Manager takes at most 2560 bytes per item, which the secrets of a sign-in
|
||||
exceed: on macOS and Windows a longer secret is split over the items `<uuid>`, `<uuid>.1`, `<uuid>.2`...
|
||||
- `accounts.json` in the launcher's data folder keeps only public data: UUID, name, skin URL, which
|
||||
account is selected. Never a token.
|
||||
- No keychain available (a Linux box without a Secret Service, a locked keychain): sign-in still
|
||||
|
||||
+85
-11
@@ -580,28 +580,102 @@ impl SecretStore for SystemStore {
|
||||
}
|
||||
}
|
||||
|
||||
/// Keychain on macOS, Credential Manager on Windows (the keyring crate).
|
||||
/// Keychain on macOS, Credential Manager on Windows (the keyring crate). Credential Manager takes at
|
||||
/// most 2560 bytes of UTF-16 per item and a sign-in's secrets are longer, so a secret is kept in
|
||||
/// pieces: the first under the account's key, the rest under `key.1`, `key.2`...
|
||||
#[cfg(any(target_os = "macos", target_os = "windows"))]
|
||||
pub struct SystemStore;
|
||||
|
||||
/// Bytes of UTF-8 per keychain item: at most twice as many in UTF-16, well under 2560.
|
||||
#[cfg(any(target_os = "macos", target_os = "windows"))]
|
||||
const PIECE: usize = 1000;
|
||||
|
||||
#[cfg(any(target_os = "macos", target_os = "windows"))]
|
||||
fn piece_key(key: &str, n: usize) -> String {
|
||||
if n == 0 {
|
||||
key.to_string()
|
||||
} else {
|
||||
format!("{key}.{n}")
|
||||
}
|
||||
}
|
||||
|
||||
/// `secret` cut into pieces of at most `PIECE` bytes, on character boundaries.
|
||||
#[cfg(any(target_os = "macos", target_os = "windows"))]
|
||||
fn split_secret(mut secret: &str) -> Vec<&str> {
|
||||
let mut pieces = Vec::new();
|
||||
while secret.len() > PIECE {
|
||||
let mut cut = PIECE;
|
||||
while !secret.is_char_boundary(cut) {
|
||||
cut -= 1;
|
||||
}
|
||||
let (head, tail) = secret.split_at(cut);
|
||||
pieces.push(head);
|
||||
secret = tail;
|
||||
}
|
||||
pieces.push(secret);
|
||||
pieces
|
||||
}
|
||||
|
||||
#[cfg(any(target_os = "macos", target_os = "windows"))]
|
||||
fn read_item(key: &str) -> Result<Option<String>, String> {
|
||||
match keyring::Entry::new(SERVICE, key).and_then(|e| e.get_password()) {
|
||||
Ok(secret) => Ok(Some(secret)),
|
||||
Err(keyring::Error::NoEntry) => Ok(None),
|
||||
Err(e) => Err(e.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(target_os = "macos", target_os = "windows"))]
|
||||
fn write_item(key: &str, secret: &str) -> Result<(), String> {
|
||||
keyring::Entry::new(SERVICE, key)
|
||||
.and_then(|e| e.set_password(secret))
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// Removes one item; false when there was none.
|
||||
#[cfg(any(target_os = "macos", target_os = "windows"))]
|
||||
fn remove_item(key: &str) -> Result<bool, String> {
|
||||
match keyring::Entry::new(SERVICE, key).and_then(|e| e.delete_credential()) {
|
||||
Ok(()) => Ok(true),
|
||||
Err(keyring::Error::NoEntry) => Ok(false),
|
||||
Err(e) => Err(e.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(target_os = "macos", target_os = "windows"))]
|
||||
impl SecretStore for SystemStore {
|
||||
fn get(&self, key: &str) -> Result<Option<String>, String> {
|
||||
match keyring::Entry::new(SERVICE, key).and_then(|e| e.get_password()) {
|
||||
Ok(secret) => Ok(Some(secret)),
|
||||
Err(keyring::Error::NoEntry) => Ok(None),
|
||||
Err(e) => Err(e.to_string()),
|
||||
let Some(mut secret) = read_item(key)? else {
|
||||
return Ok(None);
|
||||
};
|
||||
for n in 1.. {
|
||||
match read_item(&piece_key(key, n))? {
|
||||
Some(piece) => secret.push_str(&piece),
|
||||
None => break,
|
||||
}
|
||||
}
|
||||
Ok(Some(secret))
|
||||
}
|
||||
fn set(&self, key: &str, secret: &str) -> Result<(), String> {
|
||||
keyring::Entry::new(SERVICE, key)
|
||||
.and_then(|e| e.set_password(secret))
|
||||
.map_err(|e| e.to_string())
|
||||
let pieces = split_secret(secret);
|
||||
for (n, piece) in pieces.iter().enumerate() {
|
||||
write_item(&piece_key(key, n), piece)?;
|
||||
}
|
||||
// Pieces left over from a longer secret would be read back as part of this one.
|
||||
for n in pieces.len().. {
|
||||
if !remove_item(&piece_key(key, n))? {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn delete(&self, key: &str) -> Result<(), String> {
|
||||
match keyring::Entry::new(SERVICE, key).and_then(|e| e.delete_credential()) {
|
||||
Ok(()) | Err(keyring::Error::NoEntry) => Ok(()),
|
||||
Err(e) => Err(e.to_string()),
|
||||
for n in 0.. {
|
||||
if !remove_item(&piece_key(key, n))? {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn persistent(&self) -> bool {
|
||||
true
|
||||
|
||||
@@ -372,6 +372,36 @@ fn accounts_keep_secrets_in_the_store_only() {
|
||||
assert!(!store.persistent());
|
||||
}
|
||||
|
||||
/// The OS keychain itself: Credential Manager and the Keychain take one secret per item, and a
|
||||
/// sign-in's secrets are longer than one item holds. Ignored by default: it writes to the keychain
|
||||
/// (a test key, removed again).
|
||||
#[cfg(any(target_os = "macos", target_os = "windows"))]
|
||||
#[test]
|
||||
#[ignore = "writes to the OS keychain"]
|
||||
fn system_keychain_keeps_long_secrets() {
|
||||
let key = "test-long-secret";
|
||||
let secret = format!("{{\"refresh_token\":\"{}\"}}", "r".repeat(3000));
|
||||
SystemStore.set(key, &secret).unwrap();
|
||||
assert_eq!(SystemStore.get(key).unwrap(), Some(secret.clone()));
|
||||
SystemStore.delete(key).unwrap();
|
||||
assert_eq!(SystemStore.get(key).unwrap(), None);
|
||||
}
|
||||
|
||||
#[cfg(any(target_os = "macos", target_os = "windows"))]
|
||||
#[test]
|
||||
fn long_secrets_split_into_keychain_sized_pieces() {
|
||||
// Mixed 1-, 2- and 4-byte characters, so the cuts land inside some of them.
|
||||
let secret = "aé🔑".repeat(300);
|
||||
let pieces = split_secret(&secret);
|
||||
assert!(pieces.len() > 1);
|
||||
assert_eq!(pieces.concat(), secret);
|
||||
for piece in &pieces {
|
||||
assert!(piece.len() <= PIECE);
|
||||
assert!(piece.encode_utf16().count() * 2 <= 2560);
|
||||
}
|
||||
assert_eq!(split_secret("short"), vec!["short"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn session_files_are_private_and_complete() {
|
||||
let dir = std::env::temp_dir().join(format!("horizon-launcher-session-{}", std::process::id()));
|
||||
|
||||
Reference in New Issue
Block a user