Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix sync password recovery across devices (#4936)

Vinay Kumare1e97adc1515
diff --git a/Cargo.lock b/Cargo.lock
index 43126d486..dc871b12d 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1792,7 +1792,7 @@ dependencies = [
[[package]]
name = "cpal"
version = "0.15.3"
-source = "git+https://github.com/EzraEllette/cpal.git?rev=93647b9b5de39d6d4fd9f0a8224fbc0921e1e6bd#93647b9b5de39d6d4fd9f0a8224fbc0921e1e6bd"
+source = "git+https://github.com/screenpipe/cpal.git?rev=e7edfa174466e87a467d1a58e3a4aec233ff8a21#e7edfa174466e87a467d1a58e3a4aec233ff8a21"
dependencies = [
"alsa",
"cidre 0.15.1 (git+https://github.com/yury/cidre.git?rev=8481f3f0dc7dd39bb5be80d9424bfac0cad3801a)",
@@ -8095,7 +8095,7 @@ dependencies = [
[[package]]
name = "sck-rs"
version = "0.1.0"
-source = "git+https://github.com/screenpipe/sck-rs?rev=81855c0db45bde9390fc7765629ecc115b48711d#81855c0db45bde9390fc7765629ecc115b48711d"
+source = "git+https://github.com/screenpipe/sck-rs?rev=3d035cb51f0a9f71d8b716bc67d5380557976df6#3d035cb51f0a9f71d8b716bc67d5380557976df6"
dependencies = [
"cidre 0.15.1 (git+https://github.com/yury/cidre.git)",
"image",
diff --git a/crates/screenpipe-core/src/sync/keys.rs b/crates/screenpipe-core/src/sync/keys.rs
index f1f66ded1..6048b099e 100644
--- a/crates/screenpipe-core/src/sync/keys.rs
+++ b/crates/screenpipe-core/src/sync/keys.rs
@@ -18,6 +18,21 @@ use super::error::{SyncError, SyncResult};
/// Domain separation constants for key derivation
const DATA_KEY_DOMAIN: &[u8] = b"screenpipe-sync-data-key-v1";
const SEARCH_KEY_DOMAIN: &[u8] = b"screenpipe-sync-search-key-v1";
+const AUTO_SYNC_PASSWORD_DOMAIN: &str = "screenpipe-cloud-sync-v1";
+
+/// Derive the auto-managed cloud sync password for an account.
+///
+/// This preserves the legacy web/desktop derivation (`sha256(account_id +
+/// "screenpipe-cloud-sync-v1")`) so devices signed into the same account can
+/// unlock the same cloud sync key without inventing a per-device random secret.
+pub fn derive_auto_sync_password(account_id: &str) -> String {
+ use sha2::{Digest, Sha256};
+
+ let mut hasher = Sha256::new();
+ hasher.update(account_id.as_bytes());
+ hasher.update(AUTO_SYNC_PASSWORD_DOMAIN.as_bytes());
+ hex::encode(hasher.finalize())
+}
/// The complete set of keys needed for sync operations.
///
@@ -242,6 +257,19 @@ impl ExistingUserKeyData {
mod tests {
use super::*;
+ #[test]
+ fn test_auto_sync_password_is_stable() {
+ assert_eq!(
+ derive_auto_sync_password("user_123"),
+ derive_auto_sync_password("user_123")
+ );
+ assert_ne!(
+ derive_auto_sync_password("user_123"),
+ derive_auto_sync_password("user_456")
+ );
+ assert_eq!(derive_auto_sync_password("user_123").len(), 64);
+ }
+
#[test]
fn test_new_user_key_generation() {
let password = "test-password-123";
diff --git a/crates/screenpipe-core/src/sync/mod.rs b/crates/screenpipe-core/src/sync/mod.rs
index 53cdad845..3eec111fe 100644
--- a/crates/screenpipe-core/src/sync/mod.rs
+++ b/crates/screenpipe-core/src/sync/mod.rs
@@ -111,7 +111,7 @@ pub use crypto::{
};
pub use device::get_or_create_machine_id;
pub use error::{SyncError, SyncResult};
-pub use keys::{ExistingUserKeyData, NewUserKeyData, SyncKeys};
+pub use keys::{derive_auto_sync_password, ExistingUserKeyData, NewUserKeyData, SyncKeys};
pub use manager::{DownloadedBlob, SearchMatchInfo, SearchResult, SyncManager, UploadResult};
pub use service::{
PendingBlob, SyncCommand, SyncDataProvider, SyncEvent, SyncReport, SyncService,
diff --git a/crates/screenpipe-engine/src/sync_api.rs b/crates/screenpipe-engine/src/sync_api.rs
index 85e464b73..0cb463776 100644
--- a/crates/screenpipe-engine/src/sync_api.rs
+++ b/crates/screenpipe-engine/src/sync_api.rs
@@ -14,9 +14,14 @@ use axum::{
http::StatusCode,
response::Json as JsonResponse,
};
+use base64::{
+ engine::general_purpose::{URL_SAFE, URL_SAFE_NO_PAD},
+ Engine as _,
+};
use oasgen::{oasgen, OaSchema};
use screenpipe_core::sync::{
- BlobType, SyncClientConfig, SyncManager, SyncService, SyncServiceConfig, SyncServiceHandle,
+ derive_auto_sync_password, BlobType, SyncClientConfig, SyncManager, SyncService,
+ SyncServiceConfig, SyncServiceHandle,
};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
@@ -169,7 +174,7 @@ async fn sync_init_inner(
error!("failed to initialize sync: {}", e);
(
StatusCode::INTERNAL_SERVER_ERROR,
- JsonResponse(json!({"error": format!("failed to initialize sync: {}", e)})),
+ JsonResponse(json!({"error": format_sync_init_error_message(&e.to_string())})),
)
})?;
@@ -456,20 +461,60 @@ async fn sync_init_inner(
/// `/sync/init` path and the engine-driven [`ensure_sync_runtime`] path.
const SYNC_MASTER_PASSWORD_KEY: &str = "sync.master_password";
-/// Generate a strong random password (256-bit, hex-encoded) used to derive the
-/// sync master key. Uses the sync crypto RNG so we add no new dependency.
-fn generate_sync_password() -> String {
- let a = screenpipe_core::sync::generate_salt();
- let b = screenpipe_core::sync::generate_salt();
- a.iter()
- .chain(b.iter())
- .map(|byte| format!("{:02x}", byte))
- .collect()
+/// Extract the Clerk account id from the unsigned JWT payload.
+///
+/// This is only a stable, non-secret account identifier for deriving the
+/// app-managed sync password. Cloud API requests still authenticate the token
+/// server-side.
+fn jwt_subject(token: &str) -> Option<String> {
+ let payload = token.split('.').nth(1)?;
+ let decoded = URL_SAFE_NO_PAD
+ .decode(payload)
+ .or_else(|_| URL_SAFE.decode(payload))
+ .ok()?;
+ let json: Value = serde_json::from_slice(&decoded).ok()?;
+ json.get("sub")
+ .and_then(Value::as_str)
+ .filter(|s| !s.is_empty())
+ .map(str::to_string)
+}
+
+fn auto_sync_password_from_token(token: &str) -> Option<String> {
+ jwt_subject(token).map(|subject| derive_auto_sync_password(&subject))
+}
+
+fn sync_init_error_text(err: &(StatusCode, JsonResponse<Value>)) -> Option<&str> {
+ err.1 .0.get("error").and_then(Value::as_str)
+}
+
+const SYNC_ACCOUNT_KEY_MISMATCH_MESSAGE: &str = concat!(
+ "failed to initialize sync: this account has existing cloud sync data encrypted ",
+ "with an older device-local key; open screenpipe on a device where sync still ",
+ "works and reset account sync from that device"
+);
+
+fn is_sync_password_decrypt_message(message: &str) -> bool {
+ message.contains("decryption failed (authentication error)")
+ || message.contains("aead::Error")
+ || message.contains("does not match this account's existing cloud sync key")
+ || message.contains("older device-local key")
+}
+
+fn format_sync_init_error_message(message: &str) -> String {
+ if is_sync_password_decrypt_message(message) {
+ SYNC_ACCOUNT_KEY_MISMATCH_MESSAGE.to_string()
+ } else {
+ format!("failed to initialize sync: {}", message)
+ }
+}
+
+fn is_sync_password_decrypt_error(err: &(StatusCode, JsonResponse<Value>)) -> bool {
+ sync_init_error_text(err).is_some_and(is_sync_password_decrypt_message)
}
/// Lazily initialize the sync runtime if it isn't already running.
///
-/// Connection- and memory-sync endpoints used to hard-fail with
+/// Connection-, pipe-, and memory-sync endpoints used to hard-fail with
/// "sync not initialized" whenever the app hadn't already POSTed `/sync/init`
/// — e.g. when the user enabled "connection sync across devices" from the
/// account page, which flips a setting and calls `/sync/connections/*` but
@@ -529,44 +574,73 @@ pub async fn ensure_sync_runtime(
Ok(Some(bytes)) => String::from_utf8(bytes).unwrap_or_default(),
_ => String::new(),
};
- let password = if existing.is_empty() {
- let generated = generate_sync_password();
- if let Err(e) = secret_store
- .set(SYNC_MASTER_PASSWORD_KEY, generated.as_bytes())
- .await
- {
- error!("failed to persist sync master password: {}", e);
- return Err((
- StatusCode::INTERNAL_SERVER_ERROR,
- JsonResponse(json!({"error": "failed to persist sync password"})),
- ));
+
+ let auto_password = auto_sync_password_from_token(&token);
+ if existing.is_empty() && auto_password.is_none() {
+ return Err((
+ StatusCode::UNAUTHORIZED,
+ JsonResponse(json!({
+ "error": "cloud token is missing an account id; cannot initialize sync"
+ })),
+ ));
+ }
+
+ let mut candidates: Vec<(String, bool, &'static str)> = Vec::new();
+ if !existing.is_empty() {
+ candidates.push((existing.clone(), false, "stored legacy sync key"));
+ }
+ if let Some(auto_password) = auto_password {
+ if existing != auto_password {
+ candidates.push((auto_password, true, "account-managed sync key"));
}
- generated
- } else {
- existing
- };
+ }
let machine_id = screenpipe_core::sync::get_or_create_machine_id();
-
- let request = SyncInitRequest {
- token,
- password,
- machine_id: Some(machine_id),
- sync_interval_secs: Some(300),
- };
+ let mut last_password_error: Option<(StatusCode, JsonResponse<Value>)> = None;
// blob_upload = false: connection/credential sync only. Enabling credential
// sync must never start uploading OCR/transcripts/accessibility to the
// cloud.
- match sync_init_inner(state.clone(), request, false).await {
- Ok(_) => {
- info!("sync auto-initialized (lazy) for connection/memory sync");
- Ok(())
+ for (password, persist_on_success, source) in candidates {
+ let request = SyncInitRequest {
+ token: token.clone(),
+ password: password.clone(),
+ machine_id: Some(machine_id.clone()),
+ sync_interval_secs: Some(300),
+ };
+
+ match sync_init_inner(state.clone(), request, false).await {
+ Ok(_) => {
+ if persist_on_success {
+ if let Err(e) = secret_store
+ .set(SYNC_MASTER_PASSWORD_KEY, password.as_bytes())
+ .await
+ {
+ warn!("failed to persist account-managed sync key: {}", e);
+ }
+ }
+ info!(
+ "sync auto-initialized (lazy) for connection/pipe/memory sync via {}",
+ source
+ );
+ return Ok(());
+ }
+ // A concurrent caller initialized first — fine.
+ Err((StatusCode::CONFLICT, _)) => return Ok(()),
+ Err(e) if is_sync_password_decrypt_error(&e) => {
+ warn!("sync lazy init: {} could not unlock cloud keys", source);
+ last_password_error = Some(e);
+ }
+ Err(e) => return Err(e),
}
- // A concurrent caller initialized first — fine.
- Err((StatusCode::CONFLICT, _)) => Ok(()),
- Err(e) => Err(e),
}
+
+ Err(last_password_error.unwrap_or_else(|| {
+ (
+ StatusCode::INTERNAL_SERVER_ERROR,
+ JsonResponse(json!({"error": "failed to initialize sync"})),
+ )
+ }))
}
/// Response for sync status.
@@ -969,6 +1043,8 @@ pub async fn sync_pipes_push(
) -> Result<JsonResponse<PipeSyncResponse>, (StatusCode, JsonResponse<Value>)> {
use screenpipe_core::pipes::sync::*;
+ ensure_sync_runtime(&state).await?;
+
let sync_state = state.sync_state.read().await;
let runtime = sync_state.as_ref().ok_or_else(|| {
(
@@ -1026,6 +1102,8 @@ pub async fn sync_pipes_pull(
) -> Result<JsonResponse<PipeSyncResponse>, (StatusCode, JsonResponse<Value>)> {
use screenpipe_core::pipes::sync::*;
+ ensure_sync_runtime(&state).await?;
+
let sync_state = state.sync_state.read().await;
let runtime = sync_state.as_ref().ok_or_else(|| {
(
@@ -1542,6 +1620,8 @@ pub async fn sync_memories_push(
) -> Result<JsonResponse<MemoriesSyncResponse>, (StatusCode, JsonResponse<Value>)> {
use screenpipe_core::memories::sync::*;
+ ensure_sync_runtime(&state).await?;
+
let sync_state = state.sync_state.read().await;
let runtime = sync_state.as_ref().ok_or_else(|| {
(
@@ -1579,6 +1659,8 @@ pub async fn sync_memories_pull(
) -> Result<JsonResponse<MemoriesSyncResponse>, (StatusCode, JsonResponse<Value>)> {
use screenpipe_core::memories::sync::*;
+ ensure_sync_runtime(&state).await?;
+
let sync_state = state.sync_state.read().await;
let runtime = sync_state.as_ref().ok_or_else(|| {
(
@@ -1687,14 +1769,27 @@ mod tests {
}
#[test]
- fn test_generate_sync_password() {
- let a = generate_sync_password();
- let b = generate_sync_password();
- // Two SALT_SIZE (32-byte) salts hex-encoded => 128 chars, all hex.
- assert_eq!(a.len(), 128);
- assert!(a.bytes().all(|c| c.is_ascii_hexdigit()));
- // Random: two generations must not collide.
- assert_ne!(a, b);
+ fn test_auto_sync_password_from_jwt_subject() {
+ let token = "eyJhbGciOiJub25lIn0.eyJzdWIiOiJ1c2VyXzEyMyJ9.sig";
+
+ assert_eq!(jwt_subject(token).as_deref(), Some("user_123"));
+ assert_eq!(
+ auto_sync_password_from_token(token).as_deref(),
+ Some(derive_auto_sync_password("user_123").as_str())
+ );
+ }
+
+ #[test]
+ fn test_auto_sync_password_requires_jwt_subject() {
+ assert_eq!(jwt_subject("not-a-jwt"), None);
+ assert_eq!(auto_sync_password_from_token("not-a-jwt"), None);
+ }
+
+ #[test]
+ fn test_sync_decrypt_error_does_not_ask_for_password() {
+ let message = format_sync_init_error_message("crypto error: aead::Error");
+ assert!(message.contains("older device-local key"));
+ assert!(!message.contains("enter the password"));
}
#[test]