Skip to content

armgit / ente

Public Imported from GitHub
0

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

Touchups

Manav Rathi94b4c6b0bb1e
diff --git a/web/apps/photos/src/pages/_app.tsx b/web/apps/photos/src/pages/_app.tsx
index 9547992de3..5458061407 100644
--- a/web/apps/photos/src/pages/_app.tsx
+++ b/web/apps/photos/src/pages/_app.tsx
@@ -131,7 +131,9 @@ const App: React.FC<AppProps> = ({ Component, pageProps }) => {
redirectToFamilyPortal();
router.events.on("routeChangeStart", (url) => {
- if (process.env.NEXT_PUBLIC_ENTE_TRACE) console.log("route", url);
+ if (process.env.NEXT_PUBLIC_ENTE_TRACE_RT) {
+ log.debug(() => ["route", url]);
+ }
if (needsFamilyRedirect && savedPartialLocalUser()?.token) {
redirectToFamilyPortal();
diff --git a/web/packages/accounts/pages/credentials.tsx b/web/packages/accounts/pages/credentials.tsx
index 17d4f6afd4..6f5b710261 100644
--- a/web/packages/accounts/pages/credentials.tsx
+++ b/web/packages/accounts/pages/credentials.tsx
@@ -39,7 +39,7 @@ import {
verifySRP,
} from "ente-accounts/services/srp";
import {
- decryptAndStoreToken,
+ decryptAndStoreTokenIfNeeded,
generateAndSaveInteractiveKeyAttributes,
type KeyAttributes,
} from "ente-accounts/services/user";
@@ -137,7 +137,7 @@ const Page: React.FC = () => {
keyAttributes: KeyAttributes,
) => {
await saveMasterKeyInSessionAndSafeStore(masterKey);
- await decryptAndStoreToken(keyAttributes, masterKey);
+ await decryptAndStoreTokenIfNeeded(keyAttributes, masterKey);
try {
let srpAttributes = savedSRPAttributes();
if (!srpAttributes) {
diff --git a/web/packages/accounts/pages/passkeys/finish.tsx b/web/packages/accounts/pages/passkeys/finish.tsx
index f695012368..7a1f6ddcf8 100644
--- a/web/packages/accounts/pages/passkeys/finish.tsx
+++ b/web/packages/accounts/pages/passkeys/finish.tsx
@@ -2,6 +2,7 @@ import {
saveKeyAttributes,
updateSavedLocalUser,
} from "ente-accounts/services/accounts-db";
+import { clearInflightPasskeySessionID } from "ente-accounts/services/passkey";
import { unstashRedirect } from "ente-accounts/services/redirect";
import {
resetSavedLocalUserTokens,
@@ -85,7 +86,7 @@ const saveQueryCredentialsAndNavigateTo = async (
return "/";
}
- sessionStorage.removeItem("inflightPasskeySessionID");
+ clearInflightPasskeySessionID();
// Decode response string (inverse of the steps we perform in
// `passkeyAuthenticationSuccessRedirectURL`).
diff --git a/web/packages/accounts/pages/recover.tsx b/web/packages/accounts/pages/recover.tsx
index aaace99bc6..ac4b791e7c 100644
--- a/web/packages/accounts/pages/recover.tsx
+++ b/web/packages/accounts/pages/recover.tsx
@@ -10,7 +10,10 @@ import {
import { recoveryKeyFromMnemonic } from "ente-accounts/services/recovery-key";
import { appHomeRoute, stashRedirect } from "ente-accounts/services/redirect";
import type { KeyAttributes } from "ente-accounts/services/user";
-import { decryptAndStoreToken, sendOTT } from "ente-accounts/services/user";
+import {
+ decryptAndStoreTokenIfNeeded,
+ sendOTT,
+} from "ente-accounts/services/user";
import { LinkButton } from "ente-base/components/LinkButton";
import {
SingleInputForm,
@@ -81,7 +84,7 @@ const Page: React.FC = () => {
await recoveryKeyFromMnemonic(recoveryKeyMnemonic),
);
await saveMasterKeyInSessionAndSafeStore(masterKey);
- await decryptAndStoreToken(keyAttr, masterKey);
+ await decryptAndStoreTokenIfNeeded(keyAttr, masterKey);
void router.push("/change-password?op=reset");
} catch (e) {
diff --git a/web/packages/accounts/pages/two-factor/recover.tsx b/web/packages/accounts/pages/two-factor/recover.tsx
index c92097ca16..42e9b9f9df 100644
--- a/web/packages/accounts/pages/two-factor/recover.tsx
+++ b/web/packages/accounts/pages/two-factor/recover.tsx
@@ -6,7 +6,7 @@ import {
} from "ente-accounts/components/layouts/centered-paper";
import { savedPartialLocalUser } from "ente-accounts/services/accounts-db";
import {
- recoverTwoFactor,
+ getRecoverTwoFactor,
recoverTwoFactorFinish,
type TwoFactorRecoveryResponse,
type TwoFactorType,
@@ -78,7 +78,7 @@ const Page: React.FC<RecoverPageProps> = ({ twoFactorType }) => {
setSessionID(sessionID);
try {
setRecoveryResponse(
- await recoverTwoFactor(twoFactorType, sessionID),
+ await getRecoverTwoFactor(twoFactorType, sessionID),
);
} catch (e) {
log.error("Second factor recovery page setup failed", e);
diff --git a/web/packages/accounts/services/passkey.ts b/web/packages/accounts/services/passkey.ts
index b3cef840a1..0dce3b22bf 100644
--- a/web/packages/accounts/services/passkey.ts
+++ b/web/packages/accounts/services/passkey.ts
@@ -258,7 +258,7 @@ export const saveCredentialsAndNavigateTo = async (
// goes through the passkey flow in the browser itself (when they are using
// the web app).
- sessionStorage.removeItem("inflightPasskeySessionID");
+ clearInflightPasskeySessionID();
const { id, encryptedToken, keyAttributes } = response;
@@ -268,3 +268,13 @@ export const saveCredentialsAndNavigateTo = async (
return unstashRedirect() ?? "/credentials";
};
+
+/**
+ * Remove the inflight passkey session ID, if any, present in session storage.
+ *
+ * This should be called whenever we get back control from the passkey app to
+ * clean up after ourselves.
+ */
+export const clearInflightPasskeySessionID = () => {
+ sessionStorage.removeItem("inflightPasskeySessionID");
+};
diff --git a/web/packages/accounts/services/user.ts b/web/packages/accounts/services/user.ts
index 8c00ddf100..91b1f1ea5f 100644
--- a/web/packages/accounts/services/user.ts
+++ b/web/packages/accounts/services/user.ts
@@ -28,7 +28,6 @@ import {
ensureOk,
publicRequestHeaders,
} from "ente-base/http";
-import log from "ente-base/log";
import { apiURL } from "ente-base/origins";
import { ensureMasterKeyFromSession } from "ente-base/session";
import {
@@ -39,6 +38,7 @@ import {
import { ensure } from "ente-utils/ensure";
import { nullToUndefined } from "ente-utils/transform";
import { z } from "zod/v4";
+import { clearInflightPasskeySessionID } from "./passkey";
import { getUserRecoveryKey, recoveryKeyFromMnemonic } from "./recovery-key";
/**
@@ -732,19 +732,20 @@ export const changePassword = async (password: string) => {
};
/**
- * Update the {@link encryptedToken} present in the saved partial local user.
+ * Update the {@link id} and {@link encryptedToken} present in the saved partial
+ * local user.
*
* This function removes the {@link token}, if any, present in the saved partial
* local user and sets the provided {@link encryptedToken}.
*
* It is expected that the code will subsequently redirect to "/credentials",
- * which should call {@link decryptAndStoreToken} which will decrypt the newly
- * set {@link encryptedToken} and write out the decrypted value as the
+ * which should call {@link decryptAndStoreTokenIfNeeded} which will decrypt the
+ * newly set {@link encryptedToken} and write out the decrypted value as the
* {@link token} in the saved local user.
*
- * @param userID The ID of the user whose token this is. This is used as a
- * sanity check to ensure that the we do not overwrite saved partial local user
- * data for a different user.
+ * @param userID The ID of the user whose token this is. This is also saved to
+ * the partial local user (after doing a sanity check that we're not replacing
+ * partial data with a different userID).
*
* @param encryptedToken The newly obtained base64 encoded encrypted token from
* remote (e.g. as a result of the user verifying their email).
@@ -757,7 +758,12 @@ export const resetSavedLocalUserTokens = async (
if (user?.id && user.id != userID) {
throw new Error(`User ID mismatch (${user.id}, ${userID})`);
}
- replaceSavedLocalUser({ ...user, token: undefined, encryptedToken });
+ replaceSavedLocalUser({
+ ...user,
+ id: userID,
+ token: undefined,
+ encryptedToken,
+ });
return removeAuthToken();
};
@@ -769,15 +775,12 @@ export const resetSavedLocalUserTokens = async (
*
* @param masterKey The user's master key (base64 encoded).
*/
-export const decryptAndStoreToken = async (
+export const decryptAndStoreTokenIfNeeded = async (
keyAttributes: KeyAttributes,
masterKey: string,
) => {
const { encryptedToken } = savedPartialLocalUser() ?? {};
- if (!encryptedToken) {
- log.info("Skipping token decryption (no encrypted token found)");
- return;
- }
+ if (!encryptedToken) return;
const { encryptedSecretKey, secretKeyDecryptionNonce, publicKey } =
keyAttributes;
@@ -954,7 +957,7 @@ export type TwoFactorRecoveryResponse = z.infer<
* sends a encrypted recovery secret (see {@link configurePasskeyRecovery}).
*
* 3. When the user wishes to reset or bypass their second factor, the client
- * asks remote for these encrypted secrets (using {@link recoverTwoFactor}).
+ * asks remote for these encrypted secrets (using {@link getRecoverTwoFactor}).
*
* 4. User then enters their recovery key, which the client uses to decrypt the
* recovery secret and provide it back to remote for verification (using
@@ -963,7 +966,7 @@ export type TwoFactorRecoveryResponse = z.infer<
* 5. If the recovery secret matches, then remote resets (TOTP based) or bypass
* (passkey based) the user's second factor.
*/
-export const recoverTwoFactor = async (
+export const getRecoverTwoFactor = async (
twoFactorType: TwoFactorType,
sessionID: string,
): Promise<TwoFactorRecoveryResponse> => {
@@ -977,22 +980,23 @@ export const recoverTwoFactor = async (
/**
* Finish the second factor recovery / bypass initiated by
- * {@link recoverTwoFactor} using the provided recovery key mnemonic entered by
- * the user.
+ * {@link getRecoverTwoFactor} using the provided recovery key mnemonic entered
+ * by the user.
*
* See: [Note: Second factor recovery].
*
* This completes the recovery process both locally, and on remote.
*
* @param twoFactorType The second factor type (same value as what would've been
- * passed to {@link recoverTwoFactor} for obtaining {@link recoveryResponse}).
+ * passed to {@link getRecoverTwoFactor} for obtaining
+ * {@link recoveryResponse}).
*
* @param sessionID The second factor session ID (same value as what would've
- * been passed to {@link recoverTwoFactor} for obtaining
+ * been passed to {@link getRecoverTwoFactor} for obtaining
* {@link recoveryResponse}).
*
* @param recoveryResponse The response to a previous call to
- * {@link recoverTwoFactor}.
+ * {@link getRecoverTwoFactor}.
*
* @param recoveryKeyMnemonic The 24-word BIP-39 recovery key mnemonic provided
* by the user to complete recovery.
@@ -1020,6 +1024,7 @@ export const recoverTwoFactorFinish = async (
twoFactorSessionID: undefined,
passkeySessionID: undefined,
});
+ if (twoFactorType == "passkey") clearInflightPasskeySessionID();
saveKeyAttributes(keyAttributes);
};