π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Align adaptive KDF floor with server limits
Manav Rathi94b524937920
diff --git a/mobile/apps/auth/architecture/README.md b/mobile/apps/auth/architecture/README.mdindex e924068cc7..addf1d6dfe 100644--- a/mobile/apps/auth/architecture/README.md+++ b/mobile/apps/auth/architecture/README.md@@ -234,7 +234,7 @@ is used to derive your `keyEncryptionKey` from your password.used as the limits for computation and memory respectively. If the operationfails due to insufficient memory, the former is doubled and the latter is halvedprogressively, until a key can be derived. If during this process the memory-limit is reduced to a value less than `crypto_pwhash_MEMLIMIT_MIN`, the client+limit is reduced to a value less than the server-accepted floor of 128 MiB, the clientwill not let you register from that device.Internally, this uses [Argon2diff --git a/mobile/apps/photos/plugins/ente_crypto/lib/src/crypto.dart b/mobile/apps/photos/plugins/ente_crypto/lib/src/crypto.dartindex 9af1fdb979..d7eddd4f74 100644--- a/mobile/apps/photos/plugins/ente_crypto/lib/src/crypto.dart+++ b/mobile/apps/photos/plugins/ente_crypto/lib/src/crypto.dart@@ -939,8 +939,11 @@ class CryptoUtil {);}+ // Keep the adaptive floor aligned with the server-side minimum accepted+ // account KDF memory limit (128 MiB), not libsodium's absolute minimum.+ const int minMemLimit = 128 * 1024 * 1024;Uint8List key;- while (memLimit >= Sodium.cryptoPwhashMemlimitMin &&+ while (memLimit >= minMemLimit &&opsLimit <= Sodium.cryptoPwhashOpslimitMax) {try {key = await deriveKey(password, salt, memLimit, opsLimit);diff --git a/rust/core/src/crypto/impl_pure/argon.rs b/rust/core/src/crypto/impl_pure/argon.rsindex a0f9634405..129a46d7b7 100644--- a/rust/core/src/crypto/impl_pure/argon.rs+++ b/rust/core/src/crypto/impl_pure/argon.rs@@ -41,6 +41,11 @@ pub const MEMLIMIT_MODERATE: u32 = 268_435_456;/// Memory limit for sensitive operations (1 GiB).pub const MEMLIMIT_SENSITIVE: u32 = 1_073_741_824;+/// Minimum memory limit for adaptive sensitive derivation (128 MiB).+///+/// This matches the server-side floor for accepted account key attributes.+pub const MEMLIMIT_SENSITIVE_MIN: u32 = 134_217_728;+/// Minimum memory limit.pub const MEMLIMIT_MIN: u32 = 8_192;@@ -241,7 +246,7 @@ pub fn derive_sensitive_key_with_salt_adaptive(}let mut last_error = None;- while mem_limit > MEMLIMIT_MIN {+ while mem_limit >= MEMLIMIT_SENSITIVE_MIN {match derive_key_bytes(password, salt, mem_limit, ops_limit) {Ok(key) => {return Ok(DerivedKeyResult {@@ -431,7 +436,7 @@ mod tests {);assert!(result.mem_limit <= MEMLIMIT_MODERATE);assert!(result.ops_limit >= OPSLIMIT_SENSITIVE);- assert!(result.mem_limit > MEMLIMIT_MIN);+ assert!(result.mem_limit >= MEMLIMIT_SENSITIVE_MIN);}#[test]@@ -442,7 +447,7 @@ mod tests {let mut ops_limit = OPSLIMIT_SENSITIVE * factor;let mut attempts = Vec::new();- while mem_limit > MEMLIMIT_MIN {+ while mem_limit >= MEMLIMIT_SENSITIVE_MIN {attempts.push((mem_limit, ops_limit));mem_limit /= 2;ops_limit *= 2;@@ -452,7 +457,7 @@ mod tests {attempts.first(),Some(&(MEMLIMIT_MODERATE, OPSLIMIT_SENSITIVE * factor)));- assert!(attempts.last().unwrap().0 > MEMLIMIT_MIN);+ assert_eq!(attempts.last(), Some(&(MEMLIMIT_SENSITIVE_MIN, 32)));assert!(attempts.iter()diff --git a/rust/core/src/http.rs b/rust/core/src/http.rsindex d91aeca6e3..2bb995ea4f 100644--- a/rust/core/src/http.rs+++ b/rust/core/src/http.rs@@ -96,9 +96,18 @@ impl HttpClient {));}- let path_only = path- .split_once(['?', '#'])- .map_or(path, |(path_only, _)| path_only);+ let (path_and_query, fragment) = path+ .split_once('#')+ .map_or((path, None), |(path, fragment)| (path, Some(fragment)));+ if fragment.is_some() {+ return Err(Error::InvalidUrl(+ "request paths must not contain fragments".to_string(),+ ));+ }++ let (path_only, query) = path_and_query+ .split_once('?')+ .map_or((path_and_query, None), |(path, query)| (path, Some(query)));if path_only.split('/').any(|segment| matches!(segment, "." | ".."))@@ -110,15 +119,18 @@ impl HttpClient {let mut base = Url::parse(&self.base_url).map_err(|e| Error::InvalidUrl(format!("invalid base URL: {e}")))?;+ base.set_query(None);+ base.set_fragment(None);let base_path = base.path().trim_end_matches('/');let resolved_path = if base_path.is_empty() || base_path == "/" {- path.to_string()+ path_only.to_string()} else {- format!("{base_path}{path}")+ format!("{base_path}{path_only}")};base.set_path(&resolved_path);+ base.set_query(query);Ok(base)}@@ -162,6 +174,13 @@ mod tests {assert_eq!(url.as_str(), "https://api.ente.io/v1/ping");}+ #[test]+ fn test_resolve_url_preserves_request_query_without_base_query() {+ let client = test_client("https://api.ente.io/v1?stale=true#old");+ let url = client.resolve_url("/ping?fresh=true").unwrap();+ assert_eq!(url.as_str(), "https://api.ente.io/v1/ping?fresh=true");+ }+#[test]fn test_resolve_url_rejects_host_confusion_payload() {let client = test_client("https://api.ente.io");@@ -183,6 +202,13 @@ mod tests {assert!(matches!(err, Error::InvalidUrl(_)));}+ #[test]+ fn test_resolve_url_rejects_fragments() {+ let client = test_client("https://api.ente.io");+ let err = client.resolve_url("/ping#frag").unwrap_err();+ assert!(matches!(err, Error::InvalidUrl(_)));+ }+#[test]fn test_resolve_url_rejects_invalid_base_url() {let client = test_client("not a url");diff --git a/web/packages/base/crypto/libsodium.ts b/web/packages/base/crypto/libsodium.tsindex 18129942d3..bb4c17091f 100644--- a/web/packages/base/crypto/libsodium.ts+++ b/web/packages/base/crypto/libsodium.ts@@ -828,8 +828,10 @@ export const deriveSensitiveKey = async (throw new Error(`Invalid mem and ops limits: ${memLimit}, ${opsLimit}`);}- const minMemLimit = sodium.crypto_pwhash_MEMLIMIT_MIN;- while (memLimit > minMemLimit) {+ // Keep the adaptive floor aligned with the server-side minimum accepted+ // account KDF memory limit (128 MiB), not libsodium's absolute minimum.+ const minMemLimit = 128 * 1024 * 1024;+ while (memLimit >= minMemLimit) {try {const key = await deriveKey(passphrase, salt, opsLimit, memLimit);return { key, salt, opsLimit, memLimit };diff --git a/web/packages/wasm/src/http.rs b/web/packages/wasm/src/http.rsindex 8faf0c5341..5afd950fd0 100644--- a/web/packages/wasm/src/http.rs+++ b/web/packages/wasm/src/http.rs@@ -13,7 +13,7 @@ pub struct HttpError {#[wasm_bindgen]impl HttpError {- /// Error code: "network", "http", or "parse".+ /// Error code: "invalid_url", "network", "http", or "parse".#[wasm_bindgen(getter)]pub fn code(&self) -> String {self.code.clone()