π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Normalize Space key naming
Neeraj Guptadaac37745a26
diff --git a/rust/bindings/wasm/ente-wasm/src/space.rs b/rust/bindings/wasm/ente-wasm/src/space.rsindex aab43db8a9..f270ade021 100644--- a/rust/bindings/wasm/ente-wasm/src/space.rs+++ b/rust/bindings/wasm/ente-wasm/src/space.rs@@ -53,7 +53,7 @@ impl From<CoreSpaceError> for WasmSpaceError {CoreSpaceError::Crypto(_) => ("crypto", None),CoreSpaceError::Auth(_) => ("auth", None),CoreSpaceError::InvalidInput(_) => ("invalid_input", None),- CoreSpaceError::MissingPrivateKey => ("missing_private_key", None),+ CoreSpaceError::MissingSecretKey => ("missing_secret_key", None),CoreSpaceError::MissingEncryptedSpaceKey => ("missing_encrypted_space_key", None),CoreSpaceError::EntityKeyConflict => ("entity_key_conflict", None),};diff --git a/rust/e2e/tests/support/space.rs b/rust/e2e/tests/support/space.rsindex c8fa44c52e..66af5c1ac7 100644--- a/rust/e2e/tests/support/space.rs+++ b/rust/e2e/tests/support/space.rs@@ -16,7 +16,7 @@ pub async fn open_ctx(endpoint: &str, account: &TestAccount) -> AccountSpaceCtx.post(format!("{endpoint}/space/sessions")).header("X-Auth-Token", &account.auth_token).header("X-Client-Package", App::Photos.client_package())- .json(&serde_json::json!({ "clientKey": "space-e2e-client-key" }))+ .json(&serde_json::json!({ "sessionWrapKey": "space-e2e-session-wrap-key" })).send().await.expect("space session create request failed");diff --git a/rust/space/src/client.rs b/rust/space/src/client.rsindex cb29f6be35..d00f67346f 100644--- a/rust/space/src/client.rs+++ b/rust/space/src/client.rs@@ -73,7 +73,7 @@ struct ResolvedSpaceAccess {#[derive(Debug, Clone)]struct ResolvedOwnedSpaceAccess {- root_space_key: Vec<u8>,+ space_root_key: Vec<u8>,space_key: Vec<u8>,key_version: i32,}@@ -81,14 +81,14 @@ struct ResolvedOwnedSpaceAccess {#[derive(Debug, Clone)]struct SpaceIdentity {public_key: Vec<u8>,- private_key: Vec<u8>,+ secret_key: Vec<u8>,}pub struct AccountSpaceCtx {client: HttpClient,- root_space_key: Vec<u8>,+ space_root_key: Vec<u8>,user_id: Option<i64>,- root_space_key_cache: Mutex<Option<Option<Vec<u8>>>>,+ space_root_key_cache: Mutex<Option<Option<Vec<u8>>>>,space_identity_cache: Mutex<Option<SpaceIdentity>>,owned_spaces_cache: Mutex<Option<Vec<SpaceKeyResponse>>>,friend_shares_cache: Mutex<Option<Vec<DecryptedFriendShare>>>,@@ -117,9 +117,9 @@ impl AccountSpaceCtx {)?;Ok(Self {client,- root_space_key: input.space_root_key,+ space_root_key: input.space_root_key,user_id: input.user_id,- root_space_key_cache: Mutex::new(None),+ space_root_key_cache: Mutex::new(None),space_identity_cache: Mutex::new(None),owned_spaces_cache: Mutex::new(None),friend_shares_cache: Mutex::new(None),@@ -134,8 +134,8 @@ impl AccountSpaceCtx {self.user_id}- pub fn root_space_key(&self) -> &[u8] {- &self.root_space_key+ pub fn space_root_key(&self) -> &[u8] {+ &self.space_root_key}pub async fn get_entity_key(&self, key_type: &str) -> Result<Option<EntityKeyPayload>> {@@ -183,15 +183,15 @@ impl AccountSpaceCtx {})}- pub async fn get_root_space_key(&self) -> Result<Option<Vec<u8>>> {- Ok(Some(self.root_space_key.clone()))+ pub async fn get_space_root_key(&self) -> Result<Option<Vec<u8>>> {+ Ok(Some(self.space_root_key.clone()))}- pub async fn get_or_create_root_space_key(&self) -> Result<Vec<u8>> {- let root_space_key = self.root_space_key.clone();- *cache_lock(&self.root_space_key_cache, "root space key")? =- Some(Some(root_space_key.clone()));- Ok(root_space_key)+ pub async fn get_or_create_space_root_key(&self) -> Result<Vec<u8>> {+ let space_root_key = self.space_root_key.clone();+ *cache_lock(&self.space_root_key_cache, "space root key")? =+ Some(Some(space_root_key.clone()));+ Ok(space_root_key)}pub async fn list_owned_spaces(&self) -> Result<Vec<SpaceKeyResponse>> {@@ -216,14 +216,14 @@ impl AccountSpaceCtx {fn decrypt_space_identity(&self, space: &SpaceKeyResponse) -> Result<SpaceIdentity> {if space.public_key.trim().is_empty() || space.encrypted_secret_key.trim().is_empty() {- return Err(SpaceError::MissingPrivateKey);+ return Err(SpaceError::MissingSecretKey);}let public_key = decode_b64(&space.public_key)?;let encrypted_secret_key = decode_b64(&space.encrypted_secret_key)?;- let private_key = decrypt_secretbox_payload(&self.root_space_key, &encrypted_secret_key)?;+ let secret_key = decrypt_secretbox_payload(&self.space_root_key, &encrypted_secret_key)?;Ok(SpaceIdentity {public_key,- private_key,+ secret_key,})}@@ -251,8 +251,7 @@ impl AccountSpaceCtx {if ciphertext.is_empty() {return Err(SpaceError::MissingEncryptedSpaceKey);}- let space_key =- open_with_keypair(&ciphertext, &identity.public_key, &identity.private_key)?;+ let space_key = open_with_keypair(&ciphertext, &identity.public_key, &identity.secret_key)?;Ok(DecryptedFriendShare {friend: share.friend.clone(),space_id: share.space_id.clone(),@@ -263,10 +262,10 @@ impl AccountSpaceCtx {}async fn default_profile_space_access(&self) -> Result<(SpaceKeyResponse, Vec<u8>)> {- let root_space_key = self- .get_root_space_key()+ let space_root_key = self+ .get_space_root_key().await?- .ok_or_else(|| SpaceError::InvalidInput("root space key is missing".into()))?;+ .ok_or_else(|| SpaceError::InvalidInput("space root key is missing".into()))?;let space = self.list_owned_spaces().await?@@ -274,7 +273,7 @@ impl AccountSpaceCtx {.next().ok_or_else(|| SpaceError::InvalidInput("no owned space is available".into()))?;let packed = decode_b64(&space.encrypted_space_key)?;- let space_key = decrypt_secretbox_payload(&root_space_key, &packed)?;+ let space_key = decrypt_secretbox_payload(&space_root_key, &packed)?;Ok((space, space_key))}@@ -304,11 +303,11 @@ impl AccountSpaceCtx {space_key: &[u8],profile: &[u8],) -> Result<CreatedSpace> {- let root_space_key = self.get_or_create_root_space_key().await?;+ let space_root_key = self.get_or_create_space_root_key().await?;let encrypted_space_key =- encode_b64(&encrypt_secretbox_payload(&root_space_key, space_key)?);- let (public_key, private_key) = generate_keypair()?;- let encrypted_secret_key = encrypt_secretbox_payload(&root_space_key, &private_key)?;+ encode_b64(&encrypt_secretbox_payload(&space_root_key, space_key)?);+ let (public_key, secret_key) = generate_keypair()?;+ let encrypted_secret_key = encrypt_secretbox_payload(&space_root_key, &secret_key)?;let encrypted_profile = encode_b64(&encrypt_secretbox_payload(space_key, profile)?);let request = CreateSpaceRequest {space_slug: space_slug.to_owned(),@@ -332,7 +331,7 @@ impl AccountSpaceCtx {})?;*cache_lock(&self.space_identity_cache, "space identity")? = Some(SpaceIdentity {public_key,- private_key,+ secret_key,});Ok(CreatedSpace {space_id: response.space_id,@@ -499,12 +498,12 @@ impl AccountSpaceCtx {.ok_or_else(|| SpaceError::InvalidInput("missing current profile".into()))?,};let next_space_key = generate_key();- let root_space_key = self.get_or_create_root_space_key().await?;+ let space_root_key = self.get_or_create_space_root_key().await?;let request = RotateSpaceKeyRequest {space_id: space_id.to_owned(),key_version: current.key_version,encrypted_space_key: encode_b64(&encrypt_secretbox_payload(- &root_space_key,+ &space_root_key,&next_space_key,)?),wrapped_prev_key: encode_b64(&encrypt_secretbox_payload(@@ -905,13 +904,13 @@ impl AccountSpaceCtx {}pub async fn hydrate_space_keys(&self) -> Result<HydratedKeys> {- let root_space_key = self.get_root_space_key().await?;+ let space_root_key = self.get_space_root_key().await?;let owned_records = self.list_owned_spaces().await?;let mut owned = Vec::with_capacity(owned_records.len());- if let Some(root_space_key) = root_space_key {+ if let Some(space_root_key) = space_root_key {for record in owned_records {let packed = decode_b64(&record.encrypted_space_key)?;- let space_key = decrypt_secretbox_payload(&root_space_key, &packed)?;+ let space_key = decrypt_secretbox_payload(&space_root_key, &packed)?;owned.push((record.space_id, space_key));}}@@ -1232,7 +1231,7 @@ impl AccountSpaceCtx {let identity = self.default_space_identity().await?;let sealed_key = decode_b64(&message.encrypted_message_key)?;let message_key =- open_with_keypair(&sealed_key, &identity.public_key, &identity.private_key)?;+ open_with_keypair(&sealed_key, &identity.public_key, &identity.secret_key)?;let packed_message = decode_b64(&message.message_cipher)?;let plaintext = decrypt_secretbox_payload(&message_key, &packed_message)?;let payload: MessagePayload = serde_json::from_slice(&plaintext)@@ -1523,17 +1522,17 @@ impl AccountSpaceCtx {&access.space_key,)?),encrypted_access_key: encode_b64(&encrypt_secretbox_payload(- &access.root_space_key,+ &access.space_root_key,access_key.as_bytes(),)?),};let status: SpaceLinkStatusResponse = self.client.post_json(path, &request).await?;- self.created_space_link_from_status(&access.root_space_key, status)+ self.created_space_link_from_status(&access.space_root_key, status)}fn created_space_link_from_status(&self,- root_space_key: &[u8],+ space_root_key: &[u8],status: SpaceLinkStatusResponse,) -> Result<CreatedSpaceLink> {if status.encrypted_access_key.trim().is_empty() {@@ -1542,7 +1541,7 @@ impl AccountSpaceCtx {));}let access_key_bytes =- decrypt_secretbox_payload(root_space_key, &decode_b64(&status.encrypted_access_key)?)?;+ decrypt_secretbox_payload(space_root_key, &decode_b64(&status.encrypted_access_key)?)?;let access_key = String::from_utf8(access_key_bytes).map_err(|err| {SpaceError::InvalidInput(format!("invalid space link access key utf8: {err}"))})?;@@ -1579,7 +1578,7 @@ impl AccountSpaceCtx {&self,space_id: &str,) -> Result<Option<ResolvedOwnedSpaceAccess>> {- let root_space_key = match self.get_root_space_key_cached().await? {+ let space_root_key = match self.get_space_root_key_cached().await? {Some(value) => value,None => return Ok(None),};@@ -1588,9 +1587,9 @@ impl AccountSpaceCtx {return Ok(None);};let packed = decode_b64(&record.encrypted_space_key)?;- let space_key = decrypt_secretbox_payload(&root_space_key, &packed)?;+ let space_key = decrypt_secretbox_payload(&space_root_key, &packed)?;Ok(Some(ResolvedOwnedSpaceAccess {- root_space_key,+ space_root_key,space_key,key_version: record.key_version,}))@@ -1634,12 +1633,12 @@ impl AccountSpaceCtx {Ok(history.get(&target_version).cloned())}- async fn get_root_space_key_cached(&self) -> Result<Option<Vec<u8>>> {- if let Some(value) = cache_lock(&self.root_space_key_cache, "root space key")?.clone() {+ async fn get_space_root_key_cached(&self) -> Result<Option<Vec<u8>>> {+ if let Some(value) = cache_lock(&self.space_root_key_cache, "space root key")?.clone() {return Ok(value);}- let value = self.get_root_space_key().await?;- *cache_lock(&self.root_space_key_cache, "root space key")? = Some(value.clone());+ let value = self.get_space_root_key().await?;+ *cache_lock(&self.space_root_key_cache, "space root key")? = Some(value.clone());Ok(value)}@@ -2237,11 +2236,14 @@ mod tests {const TEST_MP4_BYTES: &[u8] = b"\0\0\0\x18ftypmp42";fn test_account_ctx(base_url: &str) -> AccountSpaceCtx {- test_account_ctx_with_root_key(base_url, generate_key())+ test_account_ctx_with_space_root_key(base_url, generate_key())}- fn test_account_ctx_with_root_key(base_url: &str, space_root_key: Vec<u8>) -> AccountSpaceCtx {- let (public_key, private_key) = generate_keypair().expect("valid keypair");+ fn test_account_ctx_with_space_root_key(+ base_url: &str,+ space_root_key: Vec<u8>,+ ) -> AccountSpaceCtx {+ let (public_key, secret_key) = generate_keypair().expect("valid keypair");let ctx = AccountSpaceCtx::open(OpenAccountSpaceCtxInput {base_url: base_url.to_owned(),space_session_token: Some("space-session-token".to_owned()),@@ -2255,7 +2257,7 @@ mod tests {*cache_lock(&ctx.space_identity_cache, "space identity").expect("space identity cache") =Some(SpaceIdentity {public_key,- private_key,+ secret_key,});ctx}@@ -2295,7 +2297,7 @@ mod tests {}fn owned_space_response(- root_space_key: &[u8],+ space_root_key: &[u8],space_key: &[u8],space_id: &str,space_slug: &str,@@ -2305,7 +2307,7 @@ mod tests {"spaceId": space_id,"spaceSlug": space_slug,"encryptedSpaceKey": encode_b64(- &encrypt_secretbox_payload(root_space_key, space_key).expect("space key wrap")+ &encrypt_secretbox_payload(space_root_key, space_key).expect("space key wrap")),"encryptedProfile": "","keyVersion": key_version@@ -2314,32 +2316,32 @@ mod tests {}#[tokio::test]- async fn get_root_space_key_returns_context_root_key() {+ async fn get_space_root_key_returns_context_space_root_key() {let server = Server::new_async().await;- let expected_root = generate_key();- let ctx = test_account_ctx_with_root_key(&server.url(), expected_root.clone());+ let expected_space_root = generate_key();+ let ctx = test_account_ctx_with_space_root_key(&server.url(), expected_space_root.clone());- let root = ctx- .get_root_space_key()+ let space_root = ctx+ .get_space_root_key().await- .expect("root space key should load")- .expect("root space key should exist");+ .expect("space root key should load")+ .expect("space root key should exist");- assert_eq!(root, expected_root);+ assert_eq!(space_root, expected_space_root);}#[tokio::test]- async fn get_or_create_root_space_key_returns_context_root_key() {+ async fn get_or_create_space_root_key_returns_context_space_root_key() {let server = Server::new_async().await;- let expected_root = generate_key();- let ctx = test_account_ctx_with_root_key(&server.url(), expected_root.clone());+ let expected_space_root = generate_key();+ let ctx = test_account_ctx_with_space_root_key(&server.url(), expected_space_root.clone());- let root = ctx- .get_or_create_root_space_key()+ let space_root = ctx+ .get_or_create_space_root_key().await- .expect("root space key should load");+ .expect("space root key should load");- assert_eq!(root, expected_root);+ assert_eq!(space_root, expected_space_root);}#[tokio::test]@@ -2633,8 +2635,8 @@ mod tests {#[tokio::test]async fn account_space_key_resolution_is_cached_within_context() {let mut server = Server::new_async().await;- let root_space_key = generate_key();- let ctx = test_account_ctx_with_root_key(&server.url(), root_space_key.clone());+ let space_root_key = generate_key();+ let ctx = test_account_ctx_with_space_root_key(&server.url(), space_root_key.clone());let friend_space_key = generate_key();let encrypted_profile = encode_b64(&encrypt_secretbox_payload(&friend_space_key, b"friend-profile").expect("profile wrap"),@@ -2648,7 +2650,7 @@ mod tests {.match_header("x-space-session-token", "space-session-token").with_status(200).with_body(owned_space_response(- &root_space_key,+ &space_root_key,&generate_key(),"space_owner_main","owner",@@ -3112,8 +3114,8 @@ mod tests {#[tokio::test]async fn create_space_with_key_sends_encrypted_space_and_profile_payloads() {let mut server = Server::new_async().await;- let root_space_key = generate_key();- let ctx = test_account_ctx_with_root_key(&server.url(), root_space_key);+ let space_root_key = generate_key();+ let ctx = test_account_ctx_with_space_root_key(&server.url(), space_root_key);let space_key = generate_key();let create_space = server.mock("POST", "/space")@@ -3157,8 +3159,8 @@ mod tests {#[tokio::test]async fn create_space_updates_loaded_owned_space_cache() {let mut server = Server::new_async().await;- let root_space_key = generate_key();- let ctx = test_account_ctx_with_root_key(&server.url(), root_space_key);+ let space_root_key = generate_key();+ let ctx = test_account_ctx_with_space_root_key(&server.url(), space_root_key);let space_key = generate_key();let list_spaces = server.mock("GET", "/space")@@ -3229,14 +3231,14 @@ mod tests {async fn list_owned_spaces_reuses_loaded_cache() {let mut server = Server::new_async().await;let ctx = test_account_ctx(&server.url());- let root_space_key = generate_key();+ let space_root_key = generate_key();let space_key = generate_key();let list_spaces = server.mock("GET", "/space").match_header("x-space-session-token", "space-session-token").with_status(200).with_body(owned_space_response(- &root_space_key,+ &space_root_key,&space_key,"space_owner_main","owner-main",@@ -3267,8 +3269,8 @@ mod tests {#[tokio::test]async fn create_post_includes_space_key_version() {let mut server = Server::new_async().await;- let root_space_key = generate_key();- let ctx = test_account_ctx_with_root_key(&server.url(), root_space_key.clone());+ let space_root_key = generate_key();+ let ctx = test_account_ctx_with_space_root_key(&server.url(), space_root_key.clone());let space_key = generate_key();let spaces = server.mock("GET", "/space")@@ -3278,7 +3280,7 @@ mod tests {json!([{"spaceId": "space_owner_main","spaceSlug": "owner-main",- "encryptedSpaceKey": encode_b64(&encrypt_secretbox_payload(&root_space_key, &space_key).expect("space key wrap")),+ "encryptedSpaceKey": encode_b64(&encrypt_secretbox_payload(&space_root_key, &space_key).expect("space key wrap")),"encryptedProfile": "","keyVersion": 3}])@@ -3341,15 +3343,15 @@ mod tests {#[tokio::test]async fn update_space_profile_sends_encrypted_profile_and_profile_assets() {let mut server = Server::new_async().await;- let root_space_key = generate_key();- let ctx = test_account_ctx_with_root_key(&server.url(), root_space_key.clone());+ let space_root_key = generate_key();+ let ctx = test_account_ctx_with_space_root_key(&server.url(), space_root_key.clone());let space_key = generate_key();let spaces = server.mock("GET", "/space").match_header("x-space-session-token", "space-session-token").with_status(200).with_body(owned_space_response(- &root_space_key,+ &space_root_key,&space_key,"space_owner_main","owner-main",@@ -3431,8 +3433,8 @@ mod tests {#[tokio::test]async fn get_space_profile_decrypted_loads_and_decrypts_profile() {let mut server = Server::new_async().await;- let root_space_key = generate_key();- let ctx = test_account_ctx_with_root_key(&server.url(), root_space_key.clone());+ let space_root_key = generate_key();+ let ctx = test_account_ctx_with_space_root_key(&server.url(), space_root_key.clone());let space_key = generate_key();let encrypted_profile = encode_b64(&encrypt_secretbox_payload(&space_key, b"profile-json").expect("profile wrap"),@@ -3463,7 +3465,7 @@ mod tests {.match_header("x-space-session-token", "space-session-token").with_status(200).with_body(owned_space_response(- &root_space_key,+ &space_root_key,&space_key,"space_owner_main","owner-main",@@ -3489,8 +3491,8 @@ mod tests {#[tokio::test]async fn add_friend_from_link_creates_friend_request() {let mut server = Server::new_async().await;- let root_space_key = generate_key();- let ctx = test_account_ctx_with_root_key(&server.url(), root_space_key.clone());+ let space_root_key = generate_key();+ let ctx = test_account_ctx_with_space_root_key(&server.url(), space_root_key.clone());let requester_space_key = generate_key();let target_space_key = generate_key();let (target_public_key, _) = generate_keypair().expect("valid target keypair");@@ -3503,7 +3505,7 @@ mod tests {json!([{"spaceId": "space_viewer_main","spaceSlug": "viewer-main",- "encryptedSpaceKey": encode_b64(&encrypt_secretbox_payload(&root_space_key, &requester_space_key).expect("space key wrap")),+ "encryptedSpaceKey": encode_b64(&encrypt_secretbox_payload(&space_root_key, &requester_space_key).expect("space key wrap")),"encryptedProfile": "","keyVersion": 4}])@@ -3680,8 +3682,8 @@ mod tests {#[tokio::test]async fn message_actions_use_message_endpoints() {let mut server = Server::new_async().await;- let root_space_key = generate_key();- let ctx = test_account_ctx_with_root_key(&server.url(), root_space_key.clone());+ let space_root_key = generate_key();+ let ctx = test_account_ctx_with_space_root_key(&server.url(), space_root_key.clone());let space_key = generate_key();let (friend_public_key, _) = generate_keypair().expect("valid friend keypair");@@ -3690,7 +3692,7 @@ mod tests {.match_header("x-space-session-token", "space-session-token").with_status(200).with_body(owned_space_response(- &root_space_key,+ &space_root_key,&space_key,"space_owner_main","owner-main",@@ -3987,8 +3989,8 @@ mod tests {#[tokio::test]async fn refresh_friend_shares_accepts_empty_server_response() {let mut server = Server::new_async().await;- let root_space_key = generate_key();- let ctx = test_account_ctx_with_root_key(&server.url(), root_space_key.clone());+ let space_root_key = generate_key();+ let ctx = test_account_ctx_with_space_root_key(&server.url(), space_root_key.clone());let space_key = generate_key();let (friend_public_key, _) = generate_keypair().expect("valid friend keypair");@@ -4000,7 +4002,7 @@ mod tests {json!([{"spaceId": "space_owner_main","spaceSlug": "owner-main",- "encryptedSpaceKey": encode_b64(&encrypt_secretbox_payload(&root_space_key, &space_key).expect("space key wrap")),+ "encryptedSpaceKey": encode_b64(&encrypt_secretbox_payload(&space_root_key, &space_key).expect("space key wrap")),"encryptedProfile": "","keyVersion": 3}])@@ -4058,17 +4060,17 @@ mod tests {#[tokio::test]async fn space_link_status_create_and_delete_use_link_endpoints() {let mut server = Server::new_async().await;- let root_space_key = generate_key();- let ctx = test_account_ctx_with_root_key(&server.url(), root_space_key.clone());+ let space_root_key = generate_key();+ let ctx = test_account_ctx_with_space_root_key(&server.url(), space_root_key.clone());let space_key = generate_key();let created_access_key = "AbC123xYz789";let encrypted_created_access_key = encode_b64(- &encrypt_secretbox_payload(&root_space_key, created_access_key.as_bytes())+ &encrypt_secretbox_payload(&space_root_key, created_access_key.as_bytes()).expect("encrypted created access key"),);let rotated_access_key = "ZyX987cBa321";let encrypted_rotated_access_key = encode_b64(- &encrypt_secretbox_payload(&root_space_key, rotated_access_key.as_bytes())+ &encrypt_secretbox_payload(&space_root_key, rotated_access_key.as_bytes()).expect("encrypted rotated access key"),);@@ -4077,7 +4079,7 @@ mod tests {.match_header("x-space-session-token", "space-session-token").with_status(200).with_body(owned_space_response(- &root_space_key,+ &space_root_key,&space_key,"space_owner_main","owner-main",@@ -4197,12 +4199,12 @@ mod tests {#[tokio::test]async fn create_space_link_reuses_encrypted_access_key_returned_by_post() {let mut server = Server::new_async().await;- let root_space_key = generate_key();- let ctx = test_account_ctx_with_root_key(&server.url(), root_space_key.clone());+ let space_root_key = generate_key();+ let ctx = test_account_ctx_with_space_root_key(&server.url(), space_root_key.clone());let space_key = generate_key();let access_key = "AbC123xYz789";let encrypted_access_key = encode_b64(- &encrypt_secretbox_payload(&root_space_key, access_key.as_bytes())+ &encrypt_secretbox_payload(&space_root_key, access_key.as_bytes()).expect("encrypted access key"),);@@ -4211,7 +4213,7 @@ mod tests {.match_header("x-space-session-token", "space-session-token").with_status(200).with_body(owned_space_response(- &root_space_key,+ &space_root_key,&space_key,"space_owner_main","owner-main",@@ -4362,8 +4364,8 @@ mod tests {#[tokio::test]async fn fetch_post_decrypted_uses_post_by_id_endpoint() {let mut server = Server::new_async().await;- let root_space_key = generate_key();- let ctx = test_account_ctx_with_root_key(&server.url(), root_space_key.clone());+ let space_root_key = generate_key();+ let ctx = test_account_ctx_with_space_root_key(&server.url(), space_root_key.clone());let space_key = generate_key();let post_key = generate_key();let caption = b"hello from post";@@ -4376,7 +4378,7 @@ mod tests {json!([{"spaceId": "space_owner_gallery","spaceSlug": "owner-gallery",- "encryptedSpaceKey": encode_b64(&encrypt_secretbox_payload(&root_space_key, &space_key).expect("space key wrap")),+ "encryptedSpaceKey": encode_b64(&encrypt_secretbox_payload(&space_root_key, &space_key).expect("space key wrap")),"encryptedProfile": "","keyVersion": 3}])@@ -4428,8 +4430,8 @@ mod tests {#[tokio::test]async fn hydrate_space_keys_loads_owned_and_friends_spaces() {let mut server = Server::new_async().await;- let root_space_key = generate_key();- let ctx = test_account_ctx_with_root_key(&server.url(), root_space_key.clone());+ let space_root_key = generate_key();+ let ctx = test_account_ctx_with_space_root_key(&server.url(), space_root_key.clone());let owned_space_key = generate_key();let shared_space_key = generate_key();let sealed_share = seal_with_public_key(&shared_space_key, &test_public_key(&ctx))@@ -4443,7 +4445,7 @@ mod tests {json!([{"spaceId": "space_owner_gallery","spaceSlug": "owner-gallery",- "encryptedSpaceKey": encode_b64(&encrypt_secretbox_payload(&root_space_key, &owned_space_key).expect("owned wrap")),+ "encryptedSpaceKey": encode_b64(&encrypt_secretbox_payload(&space_root_key, &owned_space_key).expect("owned wrap")),"encryptedProfile": "","keyVersion": 1}])diff --git a/rust/space/src/crypto.rs b/rust/space/src/crypto.rsindex 7c8d1ff2f9..44e710de2a 100644--- a/rust/space/src/crypto.rs+++ b/rust/space/src/crypto.rs@@ -18,11 +18,11 @@ pub(crate) fn generate_key() -> Vec<u8> {}pub(crate) fn generate_keypair() -> Result<(Vec<u8>, Vec<u8>)> {- let private_key = SecretKey::generate();- let public_key = private_key.public_key();+ let secret_key = SecretKey::generate();+ let public_key = secret_key.public_key();Ok((public_key.as_bytes().to_vec(),- private_key.as_bytes().to_vec(),+ secret_key.as_bytes().to_vec(),))}@@ -34,11 +34,11 @@ pub(crate) fn seal_with_public_key(plaintext: &[u8], public_key: &[u8]) -> Resulpub(crate) fn open_with_keypair(ciphertext: &[u8],public_key: &[u8],- private_key: &[u8],+ secret_key: &[u8],) -> Result<Vec<u8>> {let public_key = PublicKey::try_from_slice(public_key)?;- let private_key = SecretKey::try_from_slice(private_key)?;- sealed::open(ciphertext, &public_key, &private_key).map_err(Into::into)+ let secret_key = SecretKey::try_from_slice(secret_key)?;+ sealed::open(ciphertext, &public_key, &secret_key).map_err(Into::into)}pub(crate) fn generate_space_link_access_key() -> Result<String> {diff --git a/rust/space/src/error.rs b/rust/space/src/error.rsindex 084eabd2b6..1ba5119d98 100644--- a/rust/space/src/error.rs+++ b/rust/space/src/error.rs@@ -15,8 +15,8 @@ pub enum SpaceError {#[error("invalid input: {0}")]InvalidInput(String),- #[error("missing private key material")]- MissingPrivateKey,+ #[error("missing secret key material")]+ MissingSecretKey,#[error("missing encrypted space key")]MissingEncryptedSpaceKey,diff --git a/server/migrations/125_space.up.sql b/server/migrations/125_space.up.sqlindex e1210b0aa6..8ec8564b69 100644--- a/server/migrations/125_space.up.sql+++ b/server/migrations/125_space.up.sql@@ -61,7 +61,7 @@ EXECUTE PROCEDURE trigger_updated_at_microseconds_column();CREATE TABLE IF NOT EXISTS space_browser_sessions (token_hash BYTEA PRIMARY KEY,user_id BIGINT NOT NULL REFERENCES users (user_id) ON DELETE CASCADE,- client_key TEXT NOT NULL,+ session_wrap_key TEXT NOT NULL,expires_at BIGINT NOT NULL,created_at BIGINT NOT NULL DEFAULT now_utc_micro_seconds(),updated_at BIGINT NOT NULL DEFAULT now_utc_micro_seconds(),diff --git a/server/space/README.md b/server/space/README.mdindex 2f8fa45eef..5f43e1afea 100644--- a/server/space/README.md+++ b/server/space/README.md@@ -12,7 +12,7 @@ This module is the top-level home for space functionality in `ente3/server`.The Rust space client lives alongside the rest of the Ente Rust crates at`../rust/space`, not inside `server/space`.-The root space key bootstrap remains on the existing `/user-entity/key` endpoint via `userentity.Space`.+The space root key bootstrap remains on the existing `/user-entity/key` endpoint via `userentity.Space`.## Routesdiff --git a/server/space/api/sessions.go b/server/space/api/sessions.goindex f3b4bc5510..9b2bcf078d 100644--- a/server/space/api/sessions.go+++ b/server/space/api/sessions.go@@ -40,7 +40,7 @@ func (h *Handlers) CreateBrowserSession(c *gin.Context) {respondJSON(c, nil, ente.ErrAuthenticationRequired)return}- created, err := h.Module.Sessions.CreateBrowserSession(c, userID, req.ClientKey)+ created, err := h.Module.Sessions.CreateBrowserSession(c, userID, req.SessionWrapKey)if err != nil {respondJSON(c, nil, err)returndiff --git a/server/space/api/sessions_test.go b/server/space/api/sessions_test.goindex 52a74b5370..d76af0f74c 100644--- a/server/space/api/sessions_test.go+++ b/server/space/api/sessions_test.go@@ -47,7 +47,7 @@ func TestCreateSpaceBrowserSessionReturnsToken(t *testing.T) {handlers, repos, userID := setupSpaceSessionAPITest(t)router := gin.New()router.POST("/space/sessions", handlers.CreateBrowserSession)- req := httptest.NewRequest(http.MethodPost, "/space/sessions", bytes.NewBufferString(`{"clientKey":"client-key"}`))+ req := httptest.NewRequest(http.MethodPost, "/space/sessions", bytes.NewBufferString(`{"sessionWrapKey":"session-wrap-key"}`))req.Header.Set("X-Auth-User-ID", strconv.FormatInt(userID, 10))recorder := httptest.NewRecorder()@@ -100,7 +100,7 @@ func TestRequireSpaceBrowserSessionAcceptsValidHeader(t *testing.T) {handlers, repos, userID := setupSpaceSessionAPITest(t)token := "valid-space-session-token"tokenHash := sha256.Sum256([]byte(token))- require.NoError(t, repos.Sessions.CreateBrowserSession(context.Background(), tokenHash[:], userID, "client-key", timeutil.NDaysFromNow(1)))+ require.NoError(t, repos.Sessions.CreateBrowserSession(context.Background(), tokenHash[:], userID, "session-wrap-key", timeutil.NDaysFromNow(1)))router := gin.New()router.GET("/space", handlers.RequireSpaceBrowserSession(), func(c *gin.Context) {@@ -137,7 +137,7 @@ func TestBootstrapBrowserSessionAcceptsHeader(t *testing.T) {handlers, repos, userID := setupSpaceSessionAPITest(t)token := "valid-space-session-token"tokenHash := sha256.Sum256([]byte(token))- require.NoError(t, repos.Sessions.CreateBrowserSession(context.Background(), tokenHash[:], userID, "client-key", timeutil.NDaysFromNow(1)))+ require.NoError(t, repos.Sessions.CreateBrowserSession(context.Background(), tokenHash[:], userID, "session-wrap-key", timeutil.NDaysFromNow(1)))router := gin.New()router.POST("/space/sessions/bootstrap", handlers.BootstrapBrowserSession)req := httptest.NewRequest(http.MethodPost, "/space/sessions/bootstrap", nil)@@ -147,14 +147,14 @@ func TestBootstrapBrowserSessionAcceptsHeader(t *testing.T) {router.ServeHTTP(recorder, req)require.Equal(t, http.StatusOK, recorder.Code)- require.JSONEq(t, `{"id":`+strconv.FormatInt(userID, 10)+`,"clientKey":"client-key"}`, recorder.Body.String())+ require.JSONEq(t, `{"id":`+strconv.FormatInt(userID, 10)+`,"sessionWrapKey":"session-wrap-key"}`, recorder.Body.String())}func TestDeleteBrowserSessionRevokesHeaderSession(t *testing.T) {handlers, repos, userID := setupSpaceSessionAPITest(t)token := "valid-space-session-token"tokenHash := sha256.Sum256([]byte(token))- require.NoError(t, repos.Sessions.CreateBrowserSession(context.Background(), tokenHash[:], userID, "client-key", timeutil.NDaysFromNow(1)))+ require.NoError(t, repos.Sessions.CreateBrowserSession(context.Background(), tokenHash[:], userID, "session-wrap-key", timeutil.NDaysFromNow(1)))router := gin.New()router.DELETE("/space/sessions/current", handlers.DeleteBrowserSession)req := httptest.NewRequest(http.MethodDelete, "/space/sessions/current", nil)diff --git a/server/space/controller/auth_test.go b/server/space/controller/auth_test.goindex d3dfa951c7..90b502ec61 100644--- a/server/space/controller/auth_test.go+++ b/server/space/controller/auth_test.go@@ -75,7 +75,7 @@ func TestResolveViewerAcceptsSpaceBrowserSessionHeader(t *testing.T) {space, err := testCreateSpace(ctx, repos, aliceID, "alice", "alice-space-key", "alice-public", "alice-secret", "alice-secret-nonce", "alice-profile")require.NoError(t, err)sessionHash := sha256.Sum256([]byte("space-session-token"))- require.NoError(t, repos.Sessions.CreateBrowserSession(ctx, sessionHash[:], aliceID, "client-key", timeutil.MicrosecondsAfterMinutes(5)))+ require.NoError(t, repos.Sessions.CreateBrowserSession(ctx, sessionHash[:], aliceID, "session-wrap-key", timeutil.MicrosecondsAfterMinutes(5)))ginCtx := newPublicSpaceContext()ginCtx.Request.Header.Set(SpaceBrowserSessionTokenHeader, "space-session-token")ginCtx.Request.Header.Set("X-Auth-User-ID", "999999")diff --git a/server/space/controller/posts_test.go b/server/space/controller/posts_test.goindex 621db05918..5dd81e1e41 100644--- a/server/space/controller/posts_test.go+++ b/server/space/controller/posts_test.go@@ -71,7 +71,7 @@ func TestListPostsHydratesPostAssets(t *testing.T) {})require.NoError(t, err)sessionHash := sha256.Sum256([]byte("alice-list-assets-session"))- require.NoError(t, repos.Sessions.CreateBrowserSession(ctx, sessionHash[:], aliceID, "client-key", timeutil.MicrosecondsAfterMinutes(5)))+ require.NoError(t, repos.Sessions.CreateBrowserSession(ctx, sessionHash[:], aliceID, "session-wrap-key", timeutil.MicrosecondsAfterMinutes(5)))ginCtx := newPublicSpaceContext()ginCtx.Request.Header.Set(SpaceBrowserSessionTokenHeader, "alice-list-assets-session")@@ -104,7 +104,7 @@ func TestListPostLikersUsesRequestPagination(t *testing.T) {require.NoError(t, repos.Posts.SetLike(ctx, postID, bobID, bobSpace.SpaceID, true))require.NoError(t, repos.Posts.SetLike(ctx, postID, charlieID, charlieSpace.SpaceID, true))sessionHash := sha256.Sum256([]byte("alice-list-likers-session"))- require.NoError(t, repos.Sessions.CreateBrowserSession(ctx, sessionHash[:], aliceID, "client-key", timeutil.MicrosecondsAfterMinutes(5)))+ require.NoError(t, repos.Sessions.CreateBrowserSession(ctx, sessionHash[:], aliceID, "session-wrap-key", timeutil.MicrosecondsAfterMinutes(5)))ginCtx := newPublicSpaceContext()ginCtx.Request.Header.Set(SpaceBrowserSessionTokenHeader, "alice-list-likers-session")diff --git a/server/space/controller/sessions.go b/server/space/controller/sessions.goindex 67a9ab1cfc..8c0f61e81a 100644--- a/server/space/controller/sessions.go+++ b/server/space/controller/sessions.go@@ -26,15 +26,15 @@ type CreatedBrowserSession struct {Response models.SpaceBrowserSessionResponse}-func (c *SessionsController) CreateBrowserSession(ctx *gin.Context, userID int64, clientKey string) (*CreatedBrowserSession, error) {+func (c *SessionsController) CreateBrowserSession(ctx *gin.Context, userID int64, sessionWrapKey string) (*CreatedBrowserSession, error) {sessionToken := auth.GenerateURLSafeRandomString(32)- clientKey = strings.TrimSpace(clientKey)- if clientKey == "" {- return nil, ente.NewBadRequestWithMessage("clientKey is required")+ sessionWrapKey = strings.TrimSpace(sessionWrapKey)+ if sessionWrapKey == "" {+ return nil, ente.NewBadRequestWithMessage("sessionWrapKey is required")}sessionHash := sha256.Sum256([]byte(sessionToken))expiresAt := timeutil.NDaysFromNow(spaceBrowserSessionDurationDays)- if err := c.SessionsRepo.CreateBrowserSession(ctx.Request.Context(), sessionHash[:], userID, clientKey, expiresAt); err != nil {+ if err := c.SessionsRepo.CreateBrowserSession(ctx.Request.Context(), sessionHash[:], userID, sessionWrapKey, expiresAt); err != nil {return nil, err}return &CreatedBrowserSession{@@ -48,8 +48,8 @@ func (c *SessionsController) BootstrapBrowserSession(ctx *gin.Context, sessionToreturn nil, err}return &models.SpaceBrowserSessionBootstrapResponse{- ID: session.UserID,- ClientKey: session.ClientKey,+ ID: session.UserID,+ SessionWrapKey: session.SessionWrapKey,}, nil}diff --git a/server/space/controller/spaces_test.go b/server/space/controller/spaces_test.goindex 8100d8a244..ce3e5be74f 100644--- a/server/space/controller/spaces_test.go+++ b/server/space/controller/spaces_test.go@@ -98,7 +98,7 @@ func TestGetProfileReturnsProfileAssetObjectIDs(t *testing.T) {)require.NoError(t, err)sessionHash := sha256.Sum256([]byte("alice-assets-session-token"))- require.NoError(t, repos.Sessions.CreateBrowserSession(ctx, sessionHash[:], aliceID, "client-key", timeutil.MicrosecondsAfterMinutes(5)))+ require.NoError(t, repos.Sessions.CreateBrowserSession(ctx, sessionHash[:], aliceID, "session-wrap-key", timeutil.MicrosecondsAfterMinutes(5)))ginCtx := newPublicSpaceContext()ginCtx.Request.Header.Set(SpaceBrowserSessionTokenHeader, "alice-assets-session-token")diff --git a/server/space/models/models.go b/server/space/models/models.goindex c8aa5cb264..ddafe6c1ed 100644--- a/server/space/models/models.go+++ b/server/space/models/models.go@@ -10,7 +10,7 @@ type AssetRedirectRequest struct {type ListSpacesRequest struct{}type SpaceBrowserSessionRequest struct {- ClientKey string `json:"clientKey" binding:"required"`+ SessionWrapKey string `json:"sessionWrapKey" binding:"required"`}type SpaceBrowserSessionResponse struct {@@ -18,8 +18,8 @@ type SpaceBrowserSessionResponse struct {}type SpaceBrowserSessionBootstrapResponse struct {- ID int64 `json:"id"`- ClientKey string `json:"clientKey"`+ ID int64 `json:"id"`+ SessionWrapKey string `json:"sessionWrapKey"`}type SpaceEntityKeyRequest struct {diff --git a/server/space/repo/module.go b/server/space/repo/module.goindex 8f77806317..be338409ca 100644--- a/server/space/repo/module.go+++ b/server/space/repo/module.go@@ -284,13 +284,13 @@ type SpaceLinkSessionRecord struct {}type SpaceBrowserSessionRecord struct {- TokenHash []byte- UserID int64- ClientKey string- ExpiresAt int64- CreatedAt int64- UpdatedAt int64- LastUsedAt int64+ TokenHash []byte+ UserID int64+ SessionWrapKey string+ ExpiresAt int64+ CreatedAt int64+ UpdatedAt int64+ LastUsedAt int64}type SpaceEntityKeyRecord struct {diff --git a/server/space/repo/module_test.go b/server/space/repo/module_test.goindex 64374ccadb..f43f3f55de 100644--- a/server/space/repo/module_test.go+++ b/server/space/repo/module_test.go@@ -118,13 +118,13 @@ func TestGetBrowserSession(t *testing.T) {tokenHash := sha256.Sum256([]byte("browser-session-token"))expiresAt := timeutil.NDaysFromNow(1)- err := module.Sessions.CreateBrowserSession(ctx, tokenHash[:], userID, "client-key", expiresAt)+ err := module.Sessions.CreateBrowserSession(ctx, tokenHash[:], userID, "session-wrap-key", expiresAt)require.NoError(t, err)session, err := module.Sessions.GetBrowserSession(ctx, tokenHash[:])require.NoError(t, err)require.Equal(t, userID, session.UserID)- require.Equal(t, "client-key", session.ClientKey)+ require.Equal(t, "session-wrap-key", session.SessionWrapKey)require.Equal(t, expiresAt, session.ExpiresAt)}@@ -177,7 +177,7 @@ func TestSpaceAccountDeletionResetUserAccess(t *testing.T) {_, err = testUpsertLink(ctx, module, aliceSpace.SpaceID, []byte("alice-auth-hash"), aliceSpace.CurrentVersion, "alice-link-space-key", "alice-link-access-key")require.NoError(t, err)require.NoError(t, module.Links.CreateSession(ctx, []byte("alice-link-token"), aliceSpace.SpaceID, []byte("alice-auth-hash"), aliceSpace.CurrentVersion, timeutil.NDaysFromNow(1)))- require.NoError(t, module.Sessions.CreateBrowserSession(ctx, []byte("alice-browser-token"), aliceID, "client-key", timeutil.NDaysFromNow(1)))+ require.NoError(t, module.Sessions.CreateBrowserSession(ctx, []byte("alice-browser-token"), aliceID, "session-wrap-key", timeutil.NDaysFromNow(1)))postID, err := testCreatePost(ctx, module, aliceID, aliceSpace.SpaceID, "alice-post-key", nil, aliceSpace.CurrentVersion, nil)require.NoError(t, err)@@ -252,7 +252,7 @@ func TestSpaceAccountDeletionDeleteUserData(t *testing.T) {ExpiresAt: timeutil.NDaysFromNow(1),}))require.NoError(t, testCreateEntityKey(ctx, module, aliceID, "primary", "encrypted-key"))- require.NoError(t, module.Sessions.CreateBrowserSession(ctx, []byte("alice-delete-browser-token"), aliceID, "client-key", timeutil.NDaysFromNow(1)))+ require.NoError(t, module.Sessions.CreateBrowserSession(ctx, []byte("alice-delete-browser-token"), aliceID, "session-wrap-key", timeutil.NDaysFromNow(1)))require.NoError(t, testAddFriend(ctx, module, bobID, bobSpace.SpaceID, aliceSpace.SpaceID, "alice-share-key", aliceSpace.CurrentVersion, "bob-share-key", bobSpace.CurrentVersion))_, err = testUpsertLink(ctx, module, aliceSpace.SpaceID, []byte("alice-delete-auth-hash"), aliceSpace.CurrentVersion, "alice-link-space-key", "alice-link-access-key")require.NoError(t, err)diff --git a/server/space/repo/sessions.go b/server/space/repo/sessions.goindex 8ed563230c..d1ab83016a 100644--- a/server/space/repo/sessions.go+++ b/server/space/repo/sessions.go@@ -7,17 +7,17 @@ import ("github.com/ente-io/stacktrace")-func (r *SessionsRepository) CreateBrowserSession(ctx context.Context, tokenHash []byte, userID int64, clientKey string, expiresAt int64) error {+func (r *SessionsRepository) CreateBrowserSession(ctx context.Context, tokenHash []byte, userID int64, sessionWrapKey string, expiresAt int64) error {_, err := r.DB.ExecContext(ctx, `- INSERT INTO space_browser_sessions (token_hash, user_id, client_key, expires_at)+ INSERT INTO space_browser_sessions (token_hash, user_id, session_wrap_key, expires_at)VALUES ($1, $2, $3, $4)- `, tokenHash, userID, clientKey, expiresAt)+ `, tokenHash, userID, sessionWrapKey, expiresAt)return stacktrace.Propagate(err, "")}func (r *SessionsRepository) GetBrowserSession(ctx context.Context, tokenHash []byte) (*SpaceBrowserSessionRecord, error) {row := r.DB.QueryRowContext(ctx, `- SELECT s.token_hash, s.user_id, s.client_key, s.expires_at, s.created_at, s.updated_at, s.last_used_at+ SELECT s.token_hash, s.user_id, s.session_wrap_key, s.expires_at, s.created_at, s.updated_at, s.last_used_atFROM space_browser_sessions sWHERE s.token_hash = $1`, tokenHash)@@ -25,7 +25,7 @@ func (r *SessionsRepository) GetBrowserSession(ctx context.Context, tokenHash []err := row.Scan(&rec.TokenHash,&rec.UserID,- &rec.ClientKey,+ &rec.SessionWrapKey,&rec.ExpiresAt,&rec.CreatedAt,&rec.UpdatedAt,diff --git a/web/apps/space/src/services/spacePersistentSession.ts b/web/apps/space/src/services/spacePersistentSession.tsindex 7f202c40a9..1edfd17f4d 100644--- a/web/apps/space/src/services/spacePersistentSession.ts+++ b/web/apps/space/src/services/spacePersistentSession.ts@@ -33,7 +33,7 @@ const SpaceBrowserSessionResponse = z.object({ sessionToken: z.string() });const SpaceBrowserSessionBootstrapResponse = z.object({id: z.number(),- clientKey: z.string(),+ sessionWrapKey: z.string(),});const SpaceEntityKeyResponse = z.object({@@ -77,14 +77,14 @@ export const createSpaceBrowserSession = async (spaceRootKey: string,authToken: string,) => {- const clientKey = await generateKey();+ const sessionWrapKey = await generateKey();const res = await fetch(await apiURL("/space/sessions"), {method: "POST",headers: {...spaceBootstrapAuthHeaders(authToken),"Content-Type": "application/json",},- body: JSON.stringify({ clientKey }),+ body: JSON.stringify({ sessionWrapKey }),});ensureOk(res);const { sessionToken } = SpaceBrowserSessionResponse.parse(@@ -95,7 +95,7 @@ export const createSpaceBrowserSession = async (if (!user?.id || !user.email) {throw new Error("Space user is missing.");}- const box = await encryptBox(spaceRootKey, clientKey);+ const box = await encryptBox(spaceRootKey, sessionWrapKey);localStorage.setItem(spaceBrowserSessionStorageKey,JSON.stringify({@@ -154,7 +154,7 @@ const restoreSpaceBrowserSession = async () => {encryptedData: persisted.encryptedSpaceRootKey,nonce: persisted.nonce,},- bootstrap.clientKey,+ bootstrap.sessionWrapKey,);replaceSavedLocalUser({ id: bootstrap.id, email: persisted.email });saveSpaceRootKeyInSpaceSession(spaceRootKey);