Skip to content

armgit / ente

Public Imported from GitHub
0

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

Restore missing SRP metadata during session checks

Neeraj Guptaa2f70e712468
diff --git a/rust/crates/accounts/src/lifecycle.rs b/rust/crates/accounts/src/lifecycle.rs
index 9474c5fe29..1d22eecbb6 100644
--- a/rust/crates/accounts/src/lifecycle.rs
+++ b/rust/crates/accounts/src/lifecycle.rs
@@ -43,7 +43,7 @@ impl fmt::Debug for ChangePasswordParams {
#[derive(Debug)]
pub struct CheckSessionValidityParams {
pub email: String,
- pub local_srp_attributes: SrpAttributes,
+ pub local_srp_attributes: Option<SrpAttributes>,
}
#[derive(Debug, Serialize)]
@@ -187,7 +187,11 @@ pub async fn check_session_validity(
Err(Error::SrpNotConfigured) => return Ok(SessionValidity::Valid),
Err(error) => return Err(error),
};
- if remote_srp_attributes.kek_salt != params.local_srp_attributes.kek_salt {
+ if params
+ .local_srp_attributes
+ .as_ref()
+ .is_none_or(|local| remote_srp_attributes.kek_salt != local.kek_salt)
+ {
return Ok(SessionValidity::ValidButPasswordChanged {
updated_key_attributes: remote_key_attributes,
updated_srp_attributes: remote_srp_attributes,
diff --git a/rust/crates/accounts/src/lifecycle/tests.rs b/rust/crates/accounts/src/lifecycle/tests.rs
index 10c74dcc20..17c2de28b9 100644
--- a/rust/crates/accounts/src/lifecycle/tests.rs
+++ b/rust/crates/accounts/src/lifecycle/tests.rs
@@ -303,7 +303,6 @@ async fn change_password_updates_srp_and_keys() {
"email".into(),
))
- .with_status(503)
.expect(0)
.create_async()
.await;
diff --git a/rust/crates/accounts/tests/accounts.rs b/rust/crates/accounts/tests/accounts.rs
index 5eaf552374..559e7ccad5 100644
--- a/rust/crates/accounts/tests/accounts.rs
+++ b/rust/crates/accounts/tests/accounts.rs
@@ -109,11 +109,23 @@ async fn run(endpoint: String) -> TestResult {
},
)
.await?;
+ assert!(matches!(
+ check_session_validity(
+ &recovery_client,
+ CheckSessionValidityParams {
+ email: email.clone(),
+ local_srp_attributes: None,
+ },
+ )
+ .await?,
+ SessionValidity::ValidButPasswordChanged { updated_key_attributes, .. }
+ if updated_key_attributes.kek_salt == changed.kek_salt
+ ));
let validity = check_session_validity(
&client,
CheckSessionValidityParams {
email: email.clone(),
- local_srp_attributes: original_srp_attributes,
+ local_srp_attributes: Some(original_srp_attributes),
},
)
.await?;
@@ -164,7 +176,7 @@ async fn run(endpoint: String) -> TestResult {
&client,
CheckSessionValidityParams {
email: email.clone(),
- local_srp_attributes: updated_srp_attributes,
+ local_srp_attributes: Some(updated_srp_attributes),
},
)
.await?,
diff --git a/web/apps/photos/tests/password-change.test.ts b/web/apps/photos/tests/password-change.test.ts
index 41abd9d72b..e9dbd9a430 100644
--- a/web/apps/photos/tests/password-change.test.ts
+++ b/web/apps/photos/tests/password-change.test.ts
@@ -85,6 +85,7 @@ beforeEach(() => {
vi.stubGlobal("localStorage", {
getItem: (key: string) => storage.get(key) ?? null,
setItem: (key: string, value: string) => storage.set(key, value),
+ removeItem: (key: string) => storage.delete(key),
});
replaceSavedLocalUser({ id: 1, email: "[email protected]", token: "token" });
saveKeyAttributes(original);
@@ -99,35 +100,66 @@ beforeEach(() => {
});
});
-test("password change saves keys and the next session check reconciles SRP", async () => {
- await expect(change()).resolves.toBeUndefined();
-
- expect(generateInteractiveKeyAttributes).toHaveBeenCalledBefore(
- rustChangePassword,
- );
- expect(savedKeyAttributes()).toEqual(updated);
- expect(savedOriginalKeyAttributes()).toEqual(remote);
- expect(savedSRPAttributes()).toEqual(originalSRPAttributes);
-
- await expect(checkSessionValidity()).resolves.toEqual({ status: "valid" });
- expect(savedSRPAttributes()).toEqual(updatedSRPAttributes);
-});
-
-test("a subsequent password change elsewhere is still detected", async () => {
- await change();
- const result = {
- status: "validButPasswordChanged",
- updatedKeyAttributes: { ...remote, kekSalt: "another-salt" },
- updatedSRPAttributes: {
- ...updatedSRPAttributes,
- kekSalt: "another-salt",
- },
- };
- rustCheckSessionValidity.mockResolvedValue(result);
-
- await expect(checkSessionValidity()).resolves.toEqual(result);
- expect(savedSRPAttributes()).toEqual(originalSRPAttributes);
-});
+test.each([true, false])(
+ "password change reconciles SRP (cached: %s)",
+ async (cached) => {
+ if (!cached) localStorage.removeItem("srpAttributes");
+ await expect(change()).resolves.toBeUndefined();
+
+ expect(generateInteractiveKeyAttributes).toHaveBeenCalledBefore(
+ rustChangePassword,
+ );
+ expect(savedKeyAttributes()).toEqual(updated);
+ expect(savedOriginalKeyAttributes()).toEqual(remote);
+ expect(savedSRPAttributes()).toEqual(
+ cached ? originalSRPAttributes : undefined,
+ );
+
+ await expect(checkSessionValidity()).resolves.toEqual({
+ status: "valid",
+ });
+ expect(rustCheckSessionValidity).toHaveBeenCalledWith(
+ expect.anything(),
+ "token",
+ cached ? originalSRPAttributes : undefined,
+ );
+ expect(savedSRPAttributes()).toEqual(updatedSRPAttributes);
+ },
+);
+
+test.each([
+ [true, true, "validButPasswordChanged"],
+ [false, true, "validButPasswordChanged"],
+ [true, false, "validButPasswordChanged"],
+ [false, false, "valid"],
+])(
+ "session check with SRP cache %s and original keys %s returns %s",
+ async (hasSRP, hasOriginal, status) => {
+ if (!hasSRP) localStorage.removeItem("srpAttributes");
+ if (!hasOriginal) localStorage.removeItem("originalKeyAttributes");
+ const result = {
+ status: "validButPasswordChanged",
+ updatedKeyAttributes: { ...remote, kekSalt: "another-salt" },
+ updatedSRPAttributes: {
+ ...updatedSRPAttributes,
+ kekSalt: "another-salt",
+ },
+ };
+ rustCheckSessionValidity.mockResolvedValue(result);
+
+ await expect(checkSessionValidity()).resolves.toEqual(
+ status === "valid" ? { status } : result,
+ );
+ expect(savedSRPAttributes()).toEqual(
+ status === "valid"
+ ? result.updatedSRPAttributes
+ : hasSRP
+ ? originalSRPAttributes
+ : undefined,
+ );
+ },
+);
test("local key preparation failure does not change the password", async () => {
const error = new Error("key preparation failed");
diff --git a/web/packages/accounts/services/session.ts b/web/packages/accounts/services/session.ts
index 7b046410bc..d3d2099d4a 100644
--- a/web/packages/accounts/services/session.ts
+++ b/web/packages/accounts/services/session.ts
@@ -37,20 +37,23 @@ const SessionValidityResponse = z.object({
export const checkSessionValidity = async () => {
const token = await ensureAuthToken();
+ const localSRPAttributes = savedSRPAttributes();
+ const originalKeyAttributes = savedOriginalKeyAttributes();
const result = SessionValidity.parse(
await withAccountsClient((client) =>
rustCheckSessionValidity(
client,
token,
ensureLocalUser().email,
- savedSRPAttributes()!,
+ localSRPAttributes,
),
),
);
if (
result.status === "validButPasswordChanged" &&
- result.updatedSRPAttributes.kekSalt ===
- savedOriginalKeyAttributes()?.kekSalt
+ (result.updatedSRPAttributes.kekSalt ===
+ originalKeyAttributes?.kekSalt ||
+ (!localSRPAttributes && !originalKeyAttributes))
) {
saveSRPAttributes(result.updatedSRPAttributes);
return { status: "valid" as const };