diff --git a/docs/microsoft-account.md b/docs/microsoft-account.md index 380aa0785..eeb63f56a 100644 --- a/docs/microsoft-account.md +++ b/docs/microsoft-account.md @@ -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 ``, `.1`, `.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 diff --git a/launcher/src/account.rs b/launcher/src/account.rs index 257055911..313dbb2f8 100644 --- a/launcher/src/account.rs +++ b/launcher/src/account.rs @@ -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, 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 { + 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, 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 diff --git a/launcher/src/account/tests.rs b/launcher/src/account/tests.rs index ec3766c7e..6d0152722 100644 --- a/launcher/src/account/tests.rs +++ b/launcher/src/account/tests.rs @@ -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()));