Skip to content

Native iCloud apps for Windows and Linux, built in Rust. Notes, Reminders, Calendar, Contacts, Drive and Photos, offline and in sync with your iPhone.

The wait before another sign-in outlives a keyring that cannot keep it

Yashwanth Kamireddi92822eef9ac7
diff --git a/crates/quince-auth/src/lib.rs b/crates/quince-auth/src/lib.rs
index 10cf26e..24a8d4a 100644
--- a/crates/quince-auth/src/lib.rs
+++ b/crates/quince-auth/src/lib.rs
@@ -451,6 +451,13 @@ impl Client {
/// The password is never sent to Apple — SRP proves knowledge of it without transmitting
/// it. Apple retired the old cleartext endpoint; it now answers 503.
pub async fn sign_in(&mut self, apple_id: &str, password: &str) -> Result<SignInOutcome> {
+ // What this account already owes, from beside the cookies rather than from the
+ // keyring — so a machine with no keyring still remembers, which is where forgetting
+ // would matter most. Per account, so signing in as somebody else inherits nothing.
+ let owed = self.store.cooldown_for(apple_id);
+ self.session.consecutive_failures = owed.consecutive_failures;
+ self.session.retry_after = owed.retry_after;
+
let outcome = self.sign_in_inner(apple_id, password).await;
match &outcome {
// Reaching the two-factor step means the password was accepted: not a failure.
@@ -471,6 +478,15 @@ impl Client {
self.session.retry_after = (wait > 0).then(|| unix_now() + wait);
}
}
+ // Written on its own and first. `persist` needs the keyring and is allowed to fail;
+ // the wait must be remembered either way, or a machine without a keyring forgets it
+ // every time the window closes and somebody can go on hammering Apple until the
+ // account they are trying to reach is locked.
+ let _ = self.store.save_cooldown(&crate::session::Cooldown {
+ account: Some(crate::session::account_fingerprint(apple_id)),
+ consecutive_failures: self.session.consecutive_failures,
+ retry_after: self.session.retry_after,
+ });
let _ = self.persist();
outcome
}
diff --git a/crates/quince-auth/src/session.rs b/crates/quince-auth/src/session.rs
index a7745fb..8336a34 100644
--- a/crates/quince-auth/src/session.rs
+++ b/crates/quince-auth/src/session.rs
@@ -128,15 +128,50 @@ enum Backend {
pub struct Store {
cookie_path: PathBuf,
+ cooldown_path: PathBuf,
backend: Backend,
}
+/// How long to wait before trying to sign in again, kept where the keyring cannot lose it.
+///
+/// This is not secret — it is a count and a timestamp — so it does not belong in the keyring,
+/// and keeping it there made it depend on the keyring working. On a machine with none, or one
+/// that is locked, the cooldown lived only in memory: closing the window forgot it, and
+/// somebody mistyping a password could go on doing it as fast as they could reopen the app.
+/// Hammering sign-in is what gets an account temporarily locked, and it is the person's own
+/// account at stake.
+#[derive(Clone, Default, Serialize, Deserialize)]
+pub struct Cooldown {
+ /// Which account owes the wait, as a hash rather than an address — this file sits in the
+ /// clear beside the cookies and there is no reason for somebody's email to be in it.
+ ///
+ /// Held per account because both things are true at once: signing out to use a different
+ /// Apple ID must not inherit the first one's wait, and signing out must not be the way to
+ /// wipe a wait and carry on hammering the same one.
+ #[serde(default)]
+ pub account: Option<String>,
+ #[serde(default)]
+ pub consecutive_failures: u32,
+ #[serde(default)]
+ pub retry_after: Option<u64>,
+}
+
+/// Which account a wait belongs to, without writing down who they are.
+#[must_use]
+pub fn account_fingerprint(apple_id: &str) -> String {
+ use sha2::{Digest, Sha256};
+ let mut hasher = Sha256::new();
+ hasher.update(apple_id.trim().to_lowercase().as_bytes());
+ format!("{:x}", hasher.finalize())[..32].to_string()
+}
+
impl Store {
/// Production: secrets in the OS keyring.
pub fn keyring(state_dir: &Path) -> std::io::Result<Self> {
std::fs::create_dir_all(state_dir)?;
Ok(Self {
cookie_path: state_dir.join("cookies.json"),
+ cooldown_path: state_dir.join("cooldown.json"),
backend: Backend::Keyring,
})
}
@@ -147,6 +182,7 @@ impl Store {
std::fs::create_dir_all(state_dir)?;
Ok(Self {
cookie_path: state_dir.join("cookies.json"),
+ cooldown_path: state_dir.join("cooldown.json"),
backend: Backend::File(state_dir.join("session.json")),
})
}
@@ -155,6 +191,33 @@ impl Store {
&self.cookie_path
}
+ /// What this account owes before the next sign-in attempt. Unreadable, absent, or
+ /// belonging to somebody else is no wait — which is right for a first run and right for
+ /// signing in as a different person.
+ pub fn cooldown_for(&self, apple_id: &str) -> Cooldown {
+ let stored = self.load_cooldown();
+ if stored.account.as_deref() == Some(account_fingerprint(apple_id).as_str()) {
+ stored
+ } else {
+ Cooldown::default()
+ }
+ }
+
+ /// What is owed, whoever owes it.
+ pub fn load_cooldown(&self) -> Cooldown {
+ std::fs::read_to_string(&self.cooldown_path)
+ .ok()
+ .and_then(|json| serde_json::from_str(&json).ok())
+ .unwrap_or_default()
+ }
+
+ /// Written on its own, next to the cookies rather than into the keyring, so a machine
+ /// with no keyring still remembers to wait.
+ pub fn save_cooldown(&self, cooldown: &Cooldown) -> std::io::Result<()> {
+ let json = serde_json::to_string(cooldown).unwrap_or_else(|_| "{}".into());
+ std::fs::write(&self.cooldown_path, json)
+ }
+
fn entry() -> Result<keyring::Entry, keyring::Error> {
keyring::Entry::new(KEYRING_SERVICE, KEYRING_ENTRY)
}
@@ -222,6 +285,192 @@ impl Store {
if self.cookie_path.exists() {
std::fs::remove_file(&self.cookie_path)?;
}
+ // The cooldown stays. It is owed to Apple, not to the session — clearing it here
+ // would make signing out a way to wipe the wait and start hammering again, which is
+ // exactly what it exists to prevent.
Ok(())
}
}
+
+/// Remembering to wait, on a machine that cannot keep secrets.
+#[cfg(test)]
+mod cooldown_tests {
+ use super::*;
+
+ fn store() -> (tempfile::TempDir, Store) {
+ let dir = tempfile::tempdir().expect("a temporary directory");
+ let store = Store::file(dir.path()).expect("a store");
+ (dir, store)
+ }
+
+ /// A first run owes nothing.
+ #[test]
+ fn nothing_stored_is_no_wait() {
+ let (_dir, store) = store();
+ let waiting = store.cooldown_for("[email protected]");
+ assert_eq!(waiting.consecutive_failures, 0);
+ assert_eq!(waiting.retry_after, None);
+ }
+
+ /// The point of keeping it in a file: a cooldown that is forgotten when the window closes
+ /// is no cooldown at all, and it used to live only in the keyring — so a machine with none
+ /// forgot it every time.
+ #[test]
+ fn a_wait_survives_being_read_back_by_something_new() {
+ let (dir, store) = store();
+ store
+ .save_cooldown(&Cooldown {
+ account: Some(account_fingerprint("[email protected]")),
+ consecutive_failures: 4,
+ retry_after: Some(1_789_000_000),
+ })
+ .expect("written");
+
+ let reopened = Store::file(dir.path()).expect("a second store over the same directory");
+ let waiting = reopened.cooldown_for("[email protected]");
+ assert_eq!(waiting.consecutive_failures, 4);
+ assert_eq!(waiting.retry_after, Some(1_789_000_000));
+ }
+
+ /// Signing out must not wipe it, or signing out becomes the way round it.
+ #[test]
+ fn forgetting_the_session_does_not_forget_the_wait() {
+ let (_dir, store) = store();
+ store
+ .save_cooldown(&Cooldown {
+ account: Some(account_fingerprint("[email protected]")),
+ consecutive_failures: 3,
+ retry_after: Some(1_789_000_000),
+ })
+ .expect("written");
+ store.save(&Session::default()).expect("a session");
+
+ store.clear().expect("cleared");
+
+ let waiting = store.cooldown_for("[email protected]");
+ assert_eq!(
+ waiting.retry_after,
+ Some(1_789_000_000),
+ "the wait is owed to Apple, not to the session"
+ );
+ }
+
+ /// Rubbish in the file is not a reason to refuse to sign in for ever.
+ #[test]
+ fn an_unreadable_file_is_read_as_no_wait() {
+ let (dir, store) = store();
+ std::fs::write(dir.path().join("cooldown.json"), "not json at all").expect("written");
+ assert_eq!(
+ store.cooldown_for("[email protected]").retry_after,
+ None
+ );
+ }
+
+ /// The schedule itself: nearly free at first, and a high ceiling, because the penalty for
+ /// hammering is somebody's own account being locked.
+ #[test]
+ fn the_wait_grows_and_stops_growing() {
+ let waits: Vec<u64> = (0..7).map(backoff_seconds).collect();
+ assert_eq!(waits, vec![0, 5, 15, 60, 180, 600, 600]);
+ assert!(
+ waits.windows(2).all(|w| w[1] >= w[0]),
+ "never goes backwards"
+ );
+ }
+}
+
+#[cfg(test)]
+mod per_account_tests {
+ use super::*;
+
+ /// Both things have to be true at once, and they pull in opposite directions: signing out
+ /// to use a different Apple ID must not inherit the first one's wait, and signing out must
+ /// not be the way to wipe a wait and carry on hammering the same one. Holding it per
+ /// account is what satisfies both.
+ #[test]
+ fn a_wait_follows_the_account_and_not_the_session() {
+ let dir = tempfile::tempdir().expect("a temporary directory");
+ let store = Store::file(dir.path()).expect("a store");
+ store
+ .save_cooldown(&Cooldown {
+ account: Some(account_fingerprint("[email protected]")),
+ consecutive_failures: 5,
+ retry_after: Some(1_789_000_000),
+ })
+ .expect("written");
+
+ assert_eq!(
+ store.cooldown_for("[email protected]").retry_after,
+ Some(1_789_000_000),
+ "the account that earned it still waits"
+ );
+ assert_eq!(
+ store.cooldown_for("[email protected]").retry_after,
+ None,
+ "somebody else does not"
+ );
+ }
+
+ /// An address is an address however it was typed.
+ #[test]
+ fn the_same_account_is_recognised_however_it_was_typed() {
+ let one = account_fingerprint("[email protected]");
+ assert_eq!(one, account_fingerprint("[email protected]"));
+ assert_eq!(one, account_fingerprint(" [email protected] "));
+ assert_ne!(one, account_fingerprint("[email protected]"));
+ }
+
+ /// The reason it is a file of its own rather than part of the session.
+ ///
+ /// The session lives in the keyring. On a machine with no keyring service, or one that is
+ /// locked, storing it fails — and the wait went with it, so closing the window forgot it
+ /// and somebody could go on hammering as fast as they could reopen the app. Keeping it
+ /// beside the cookies means losing the secrets does not lose the wait.
+ #[test]
+ fn the_wait_outlives_a_secret_store_that_cannot_keep_anything() {
+ let dir = tempfile::tempdir().expect("a temporary directory");
+ let store = Store::file(dir.path()).expect("a store");
+ store
+ .save_cooldown(&Cooldown {
+ account: Some(account_fingerprint("[email protected]")),
+ consecutive_failures: 4,
+ retry_after: Some(1_789_000_000),
+ })
+ .expect("written");
+ store.save(&Session::default()).expect("a session");
+
+ // Everything the secret store held, gone — as it is on a machine where the keyring
+ // never accepted it in the first place.
+ std::fs::remove_file(dir.path().join("session.json")).expect("removed");
+
+ let reopened = Store::file(dir.path()).expect("a store over the same directory");
+ assert!(
+ reopened.load().trust_token.is_none(),
+ "the secrets really are gone"
+ );
+ assert_eq!(
+ reopened.cooldown_for("[email protected]").retry_after,
+ Some(1_789_000_000),
+ "and the wait is still owed"
+ );
+ }
+
+ /// The file sits in the clear beside the cookies, so it must not say who anybody is.
+ #[test]
+ fn the_file_does_not_hold_an_address() {
+ let dir = tempfile::tempdir().expect("a temporary directory");
+ let store = Store::file(dir.path()).expect("a store");
+ store
+ .save_cooldown(&Cooldown {
+ account: Some(account_fingerprint("[email protected]")),
+ consecutive_failures: 1,
+ retry_after: Some(1),
+ })
+ .expect("written");
+
+ let written = std::fs::read_to_string(dir.path().join("cooldown.json")).expect("read");
+ assert!(!written.contains("someone"), "no local part");
+ assert!(!written.contains("example.invalid"), "no domain");
+ assert!(!written.contains('@'), "nothing that looks like an address");
+ }
+}
diff --git a/crates/quince-auth/tests/auth_flow.rs b/crates/quince-auth/tests/auth_flow.rs
index f9d1e66..3ebc3db 100644
--- a/crates/quince-auth/tests/auth_flow.rs
+++ b/crates/quince-auth/tests/auth_flow.rs
@@ -555,3 +555,63 @@ async fn resume_rebuilds_a_session_from_tokens_alone() {
"and the service map must come back with it"
);
}
+
+/// A cooldown that a restart forgets is not a cooldown.
+///
+/// It used to live only in the keyring, so on a machine with none — or one that is locked —
+/// closing the window forgot it, and somebody mistyping a password could go on doing it as
+/// fast as they could reopen the app. Hammering sign-in is what gets an account temporarily
+/// locked, and with other people using this it is their account at stake, not ours.
+#[tokio::test]
+async fn a_new_client_still_owes_the_wait_the_old_one_earned() {
+ let server = MockServer::start().await;
+ mount_init(&server, 429).await;
+
+ let dir = tempfile::tempdir().unwrap();
+ {
+ let mut client = client_for(&server, dir.path());
+ client
+ .sign_in("[email protected]", "hunter2")
+ .await
+ .unwrap_err();
+ assert!(client.session().retry_after.is_some());
+ }
+
+ // A second client over the same directory: the app, reopened.
+ let mut again = client_for(&server, dir.path());
+ let err = again
+ .sign_in("[email protected]", "hunter2")
+ .await
+ .unwrap_err();
+ assert!(
+ matches!(err, quince_auth::Error::CoolingDown { .. }),
+ "reopening the app must not clear the wait, got: {err}"
+ );
+}
+
+/// And the other half: it is owed by the account, not by the machine. Someone signing in with
+/// a different Apple ID inherits nothing.
+#[tokio::test]
+async fn a_different_account_does_not_inherit_the_wait() {
+ let server = MockServer::start().await;
+ mount_init(&server, 429).await;
+
+ let dir = tempfile::tempdir().unwrap();
+ {
+ let mut client = client_for(&server, dir.path());
+ client
+ .sign_in("[email protected]", "hunter2")
+ .await
+ .unwrap_err();
+ }
+
+ let mut other = client_for(&server, dir.path());
+ let err = other
+ .sign_in("[email protected]", "hunter2")
+ .await
+ .unwrap_err();
+ assert!(
+ !matches!(err, quince_auth::Error::CoolingDown { .. }),
+ "a different account must start clean, got: {err}"
+ );
+}