Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Fix native SRP bridges and HTTP client contract

Manav Rathi90955c4c4ea3
diff --git a/mobile/native/android/apps/ensu/crypto-auth-core/src/main/java/io/ente/ensu/crypto/EnsuCryptoBridge.kt b/mobile/native/android/apps/ensu/crypto-auth-core/src/main/java/io/ente/ensu/crypto/EnsuCryptoBridge.kt
index c387a1b4ed..22c3992b75 100644
--- a/mobile/native/android/apps/ensu/crypto-auth-core/src/main/java/io/ente/ensu/crypto/EnsuCryptoBridge.kt
+++ b/mobile/native/android/apps/ensu/crypto-auth-core/src/main/java/io/ente/ensu/crypto/EnsuCryptoBridge.kt
@@ -24,11 +24,12 @@ object EnsuCryptoBridge {
}
fun srpDecryptSecrets(
+ srpM2: String,
keyAttributes: KeyAttributes,
encryptedToken: String?,
plainToken: String?
): AuthSecrets {
- return io.ente.ensu.crypto.srpDecryptSecrets(keyAttributes, encryptedToken, plainToken)
+ return io.ente.ensu.crypto.srpDecryptSecrets(srpM2, keyAttributes, encryptedToken, plainToken)
}
fun deriveKekForLogin(password: String, kekSalt: String, memLimit: Int, opsLimit: Int): ByteArray {
diff --git a/mobile/native/android/apps/ensu/data/src/main/java/io/ente/ensu/data/auth/EnsuAuthService.kt b/mobile/native/android/apps/ensu/data/src/main/java/io/ente/ensu/data/auth/EnsuAuthService.kt
index ca90f88655..e7286dd2b7 100644
--- a/mobile/native/android/apps/ensu/data/src/main/java/io/ente/ensu/data/auth/EnsuAuthService.kt
+++ b/mobile/native/android/apps/ensu/data/src/main/java/io/ente/ensu/data/auth/EnsuAuthService.kt
@@ -191,6 +191,7 @@ class EnsuAuthService(
val keyAttributes = payload.keyAttributes ?: throw IllegalStateException("Invalid response")
val secrets = withContext(Dispatchers.Default) {
EnsuCryptoBridge.srpDecryptSecrets(
+ srpM2 = response.srpM2.orEmpty(),
keyAttributes = keyAttributes.toCrypto(),
encryptedToken = payload.encryptedToken,
plainToken = payload.token
diff --git a/mobile/native/android/apps/ensu/data/src/main/java/io/ente/ensu/data/network/AuthenticationGateway.kt b/mobile/native/android/apps/ensu/data/src/main/java/io/ente/ensu/data/network/AuthenticationGateway.kt
index 866ed8dd9f..6234e37fa6 100644
--- a/mobile/native/android/apps/ensu/data/src/main/java/io/ente/ensu/data/network/AuthenticationGateway.kt
+++ b/mobile/native/android/apps/ensu/data/src/main/java/io/ente/ensu/data/network/AuthenticationGateway.kt
@@ -80,6 +80,7 @@ internal data class AuthorizationResponse(
@SerialName("keyAttributes") val keyAttributes: KeyAttributesDto? = null,
@SerialName("encryptedToken") val encryptedToken: String? = null,
@SerialName("token") val token: String? = null,
+ @SerialName("srpM2") val srpM2: String? = null,
@SerialName("twoFactorSessionID") val twoFactorSessionId: String? = null,
@SerialName("twoFactorSessionIDV2") val twoFactorSessionIdV2: String? = null,
@SerialName("passkeySessionID") val passkeySessionId: String? = null,
diff --git a/mobile/native/darwin/Apps/ensu/ensu/Services/EnsuAuthService.swift b/mobile/native/darwin/Apps/ensu/ensu/Services/EnsuAuthService.swift
index c07be3d945..7867d54e61 100644
--- a/mobile/native/darwin/Apps/ensu/ensu/Services/EnsuAuthService.swift
+++ b/mobile/native/darwin/Apps/ensu/ensu/Services/EnsuAuthService.swift
@@ -175,6 +175,7 @@ final class EnsuAuthService {
let secrets = try await Task.detached(priority: .userInitiated) {
try srpDecryptSecrets(
+ srpM2: authResponse.srpM2 ?? "",
keyAttrs: keyAttrs,
encryptedToken: authResponse.encryptedToken,
plainToken: authResponse.token
diff --git a/mobile/native/darwin/Packages/EnteNetwork/Sources/EnteNetwork/Gateways/AuthenticationGateway.swift b/mobile/native/darwin/Packages/EnteNetwork/Sources/EnteNetwork/Gateways/AuthenticationGateway.swift
index b6a982ae67..9110953cff 100644
--- a/mobile/native/darwin/Packages/EnteNetwork/Sources/EnteNetwork/Gateways/AuthenticationGateway.swift
+++ b/mobile/native/darwin/Packages/EnteNetwork/Sources/EnteNetwork/Gateways/AuthenticationGateway.swift
@@ -58,16 +58,18 @@ public struct AuthorizationResponse: Codable {
public let keyAttributes: KeyAttributes?
public let encryptedToken: String?
public let token: String?
+ public let srpM2: String?
public let twoFactorSessionID: String?
public let twoFactorSessionIDV2: String?
public let passkeySessionID: String?
public let accountsUrl: String?
public let id: UserID
- public init(keyAttributes: KeyAttributes?, encryptedToken: String?, token: String?, twoFactorSessionID: String?, twoFactorSessionIDV2: String?, passkeySessionID: String?, accountsUrl: String?, id: UserID) {
+ public init(keyAttributes: KeyAttributes?, encryptedToken: String?, token: String?, srpM2: String?, twoFactorSessionID: String?, twoFactorSessionIDV2: String?, passkeySessionID: String?, accountsUrl: String?, id: UserID) {
self.keyAttributes = keyAttributes
self.encryptedToken = encryptedToken
self.token = token
+ self.srpM2 = srpM2
self.twoFactorSessionID = twoFactorSessionID
self.twoFactorSessionIDV2 = twoFactorSessionIDV2
self.passkeySessionID = passkeySessionID
@@ -171,4 +173,4 @@ public class AuthenticationGateway {
responseType: AuthorizationResponse.self
)
}
-}
\ No newline at end of file
+}
diff --git a/rust/core/src/http.rs b/rust/core/src/http.rs
index fdd62ec56e..68524b82ee 100644
--- a/rust/core/src/http.rs
+++ b/rust/core/src/http.rs
@@ -1,5 +1,6 @@
//! HTTP client for communicating with the Ente API.
+use reqwest::Url;
use serde::Deserialize;
use thiserror::Error;
@@ -95,6 +96,14 @@ impl HttpClient {
));
}
+ let base = Url::parse(&self.base_url)
+ .map_err(|e| Error::InvalidUrl(format!("invalid base URL: {e}")))?;
+ if base.query().is_some() || base.fragment().is_some() {
+ return Err(Error::InvalidUrl(
+ "base URL must not contain a query or fragment".to_string(),
+ ));
+ }
+
Ok(format!("{}{}", self.base_url, path))
}
@@ -151,4 +160,25 @@ mod tests {
let err = client.request_url("ping").unwrap_err();
assert!(matches!(err, Error::InvalidUrl(_)));
}
+
+ #[test]
+ fn test_request_url_rejects_invalid_base_url() {
+ let client = test_client("not a url");
+ let err = client.request_url("/ping").unwrap_err();
+ assert!(matches!(err, Error::InvalidUrl(_)));
+ }
+
+ #[test]
+ fn test_request_url_rejects_base_url_with_query() {
+ let client = test_client("https://api.ente.io/v1?stale=true");
+ let err = client.request_url("/ping").unwrap_err();
+ assert!(matches!(err, Error::InvalidUrl(_)));
+ }
+
+ #[test]
+ fn test_request_url_rejects_base_url_with_fragment() {
+ let client = test_client("https://api.ente.io/v1#old");
+ let err = client.request_url("/ping").unwrap_err();
+ assert!(matches!(err, Error::InvalidUrl(_)));
+ }
}
diff --git a/rust/uniffi/core/src/lib.rs b/rust/uniffi/core/src/lib.rs
index db6d9229be..6751e28851 100644
--- a/rust/uniffi/core/src/lib.rs
+++ b/rust/uniffi/core/src/lib.rs
@@ -216,16 +216,13 @@ pub fn srp_decrypt_secrets(
encrypted_token: Option<String>,
plain_token: Option<String>,
) -> Result<AuthSecrets, EnsuError> {
+ // Consume the SRP state up front so any downstream failure also clears the
+ // KEK and lets callers start a fresh session.
let mut guard = lock_srp_state()?;
- let state = guard.as_mut().ok_or_else(|| EnsuError::Message {
- reason: "SRP session not initialized".to_string(),
- })?;
- verify_srp_m2(&state.session, &srp_m2)?;
-
- // Consume the SRP state so KEK doesn't linger in memory if the caller forgets srp_clear().
let state = guard.take().ok_or_else(|| EnsuError::Message {
reason: "SRP session not initialized".to_string(),
})?;
+ verify_srp_m2(&state.session, &srp_m2)?;
let core_attrs = to_core_key_attrs(&key_attrs);
@@ -257,6 +254,19 @@ mod tests {
auth::SrpSession::new("test-user-id", &[0u8; 16], &[0u8; 16]).unwrap()
}
+ fn sample_key_attributes() -> KeyAttributes {
+ KeyAttributes {
+ kek_salt: String::new(),
+ encrypted_key: String::new(),
+ key_decryption_nonce: String::new(),
+ public_key: String::new(),
+ encrypted_secret_key: String::new(),
+ secret_key_decryption_nonce: String::new(),
+ mem_limit: None,
+ ops_limit: None,
+ }
+ }
+
#[test]
fn test_verify_srp_m2_rejects_missing_server_proof() {
let session = sample_session();
@@ -270,6 +280,25 @@ mod tests {
let err = verify_srp_m2(&session, "%%%").unwrap_err();
assert!(err.to_string().starts_with("srp_m2: "));
}
+
+ #[test]
+ fn test_srp_decrypt_secrets_clears_state_on_bad_server_proof() {
+ srp_clear();
+ *SRP_STATE.lock().unwrap() = Some(SrpState {
+ session: sample_session(),
+ kek: crypto::SecretVec::new(vec![0u8; 32]),
+ });
+
+ let err = srp_decrypt_secrets(
+ String::new(),
+ sample_key_attributes(),
+ None,
+ Some("token".to_string()),
+ )
+ .unwrap_err();
+ assert_eq!(err.to_string(), "Missing server proof");
+ assert!(SRP_STATE.lock().unwrap().is_none());
+ }
}
pub fn derive_kek_for_login(