π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
chore(use-app-lock):code-verification
Aswin Asok3c6aea5f52df
diff --git a/web/packages/new/photos/components/utils/use-app-lock.ts b/web/packages/new/photos/components/utils/use-app-lock.tsindex 58bc473d6e..9f524a31fb 100644--- a/web/packages/new/photos/components/utils/use-app-lock.ts+++ b/web/packages/new/photos/components/utils/use-app-lock.ts@@ -15,11 +15,19 @@ import {* This is meant to be called once from the top-level `_app.tsx`.*/export const useSetupAppLock = () => {+ /**+ * Since we require the Electron safe-storage on the desktop environment, this state variable+ * initalizes as false on the desktop environment and later after ensuring that refreshAppLockStateFromSession()+ * is completed transistions into the true.+ *+ * Since this safe-storage is not used in the web environment in web environments it's by default true.+ */const [isAppLockReady, setIsAppLockReady] = useState(() => !isDesktop);useEffect(() => {const isElectron = !!globalThis.electron;- const isAppLockEnabled = localStorage.getItem("appLock.enabled") === "true";+ const isAppLockEnabled =+ localStorage.getItem("appLock.enabled") === "true";initAppLock();if (!isElectron || !isAppLockEnabled) {@@ -29,8 +37,18 @@ export const useSetupAppLock = () => {void (async () => {try {+ /**+ * The current session's master key might be already existing in the OS's safe+ * storage, so if that is foudn then writing it to the browser sessionStorage+ *+ * Without this the user will have to enter the password again on every launch.+ */await updateSessionFromElectronSafeStorageIfNeeded();} finally {+ /**+ * The app lock config is actually persisted across sessions, and for refreshing this+ * the user must haveMasterKeyInSession().+ */refreshAppLockStateFromSession();setIsAppLockReady(true);}@@ -49,22 +67,28 @@ export const useAutoLockWhenBackgrounded = (isLocked: AppLockState["isLocked"],autoLockTimeMs: AppLockState["autoLockTimeMs"],) => {- const autoLockTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);- const autoLockDeadlineRef = useRef<number | null>(null);+ // Holds the current timeout handle for a scheduled auto-lock.+ const pendingAutoLockTimeoutRef = useRef<ReturnType<+ typeof setTimeout+ > | null>(null);+ // Stores the exact timestamp when auto-lock should happen.+ const autoLockDueAtTimestampRef = useRef<number | null>(null);useEffect(() => {if (!enabled) return;const clearAutoLockTimer = () => {- if (autoLockTimerRef.current) {- clearTimeout(autoLockTimerRef.current);- autoLockTimerRef.current = null;+ if (pendingAutoLockTimeoutRef.current) {+ clearTimeout(pendingAutoLockTimeoutRef.current);+ pendingAutoLockTimeoutRef.current = null;}- autoLockDeadlineRef.current = null;+ autoLockDueAtTimestampRef.current = null;};const lockIfDeadlineElapsed = () => {- const deadline = autoLockDeadlineRef.current;+ // Return early if the lock deadline has not been reached yet.+ // If the deadline has passed, clear timer state and lock now.+ const deadline = autoLockDueAtTimestampRef.current;if (deadline === null) return false;if (Date.now() < deadline) return false;@@ -73,12 +97,15 @@ export const useAutoLockWhenBackgrounded = (return true;};+ // Called when the app loses foreground focus (blur/visibility change).+ // Starts auto-lock unless the app is already locked.const startAutoLockTimer = () => {if (isLocked) return;// Avoid extending the countdown when blur + visibility events both// fire for a single background transition.- const existingDeadline = autoLockDeadlineRef.current;++ const existingDeadline = autoLockDueAtTimestampRef.current;if (existingDeadline !== null && Date.now() < existingDeadline) {return;}@@ -89,21 +116,24 @@ export const useAutoLockWhenBackgrounded = (return;}- if (autoLockTimerRef.current) {- clearTimeout(autoLockTimerRef.current);+ if (pendingAutoLockTimeoutRef.current) {+ clearTimeout(pendingAutoLockTimeoutRef.current);}- autoLockDeadlineRef.current = Date.now() + autoLockTimeMs;- autoLockTimerRef.current = setTimeout(() => {- autoLockDeadlineRef.current = null;+ autoLockDueAtTimestampRef.current = Date.now() + autoLockTimeMs;+ pendingAutoLockTimeoutRef.current = setTimeout(() => {+ autoLockDueAtTimestampRef.current = null;lock();}, autoLockTimeMs);};+ // On foreground, lock immediately if the deadline passed; otherwise clear pending timer.const handleAppForegrounded = () => {if (lockIfDeadlineElapsed()) return;clearAutoLockTimer();};+ // Hidden means backgrounded, so start auto-lock countdown.+ // Visible means foregrounded, so re-check deadline and clear timer if needed.const handleVisibilityChange = () => {if (document.hidden) {startAutoLockTimer();@@ -124,13 +154,16 @@ export const useAutoLockWhenBackgrounded = (let unsubscribeMainWindowFocus: (() => void) | undefined;if (globalThis.electron) {- unsubscribeMainWindowFocus =- subscribeMainWindowFocus(handleAppForegrounded);+ unsubscribeMainWindowFocus = subscribeMainWindowFocus(+ handleAppForegrounded,+ );}document.addEventListener("visibilitychange", handleVisibilityChange);window.addEventListener("blur", handleWindowBlur);window.addEventListener("focus", handleWindowFocus);++ // cleanupreturn () => {document.removeEventListener("visibilitychange",diff --git a/web/packages/new/photos/components/utils/use-snapshot.ts b/web/packages/new/photos/components/utils/use-snapshot.tsindex 0b76c01b3a..7eaee6a8c1 100644--- a/web/packages/new/photos/components/utils/use-snapshot.ts+++ b/web/packages/new/photos/components/utils/use-snapshot.ts@@ -21,7 +21,7 @@ import {* to updates.*/export const useAppLockSnapshot = () =>- useSyncExternalStore(appLockSubscribe, appLockSnapshot, appLockSnapshot);+ useSyncExternalStore(appLockSubscribe, appLockSnapshot);/*** A convenience hook that returns {@link settingsSnapshot}, and alsodiff --git a/web/packages/new/photos/services/app-lock.ts b/web/packages/new/photos/services/app-lock.tsindex 9f6ffdecb6..05f9e29fe1 100644--- a/web/packages/new/photos/services/app-lock.ts+++ b/web/packages/new/photos/services/app-lock.ts@@ -100,10 +100,11 @@ const setSnapshot = (snapshot: AppLockState) => {// -- localStorage keys (synchronous, for cold-start reads) --const lsKeyEnabled = "appLock.enabled";-const lsKeyLockType = "appLock.lockType";+// Stores the selected app-lock method ("pin" | "password" | "device" | "none").+const lsKeyAppLockMethod = "appLock.lockType";const lsKeyAutoLockTimeMs = "appLock.autoLockTimeMs";// Removed config key from older app-lock implementation; clear it on resets.-const lsKeyDeviceLockEnabled = "appLock.deviceLockEnabled";+const lsLegacyKeyDeviceLockEnabled = "appLock.deviceLockEnabled";// -- KV DB keys (IndexedDB, async) --@@ -283,13 +284,20 @@ interface PersistedAppLockConfig {const readPersistedAppLockConfig = (): PersistedAppLockConfig => {let enabled = localStorage.getItem(lsKeyEnabled) === "true";- const lockTypeRaw = localStorage.getItem(lsKeyLockType);++ // Getting the current applock method.+ const persistedLockType = localStorage.getItem(lsKeyAppLockMethod);// Remove stale key from removed "hide content when switching apps" setting.localStorage.removeItem("appLock.hideContentOnBlur");+ /**+ * Normalize the persisted lock type value. If an unsupported type is+ * detected, it will be automatically downgraded to "none" to prevent the+ * app from being stuck in a stale state.+ */const lockType = normalizeDeviceLockType(- normalizeLockType(lockTypeRaw ?? "none"),+ normalizeLockType(persistedLockType ?? "none"),);if (enabled && lockType === "none") {@@ -298,9 +306,9 @@ const readPersistedAppLockConfig = (): PersistedAppLockConfig => {}if (lockType === "none") {- localStorage.removeItem(lsKeyLockType);- } else if (lockTypeRaw !== lockType) {- localStorage.setItem(lsKeyLockType, lockType);+ localStorage.removeItem(lsKeyAppLockMethod);+ } else if (persistedLockType !== lockType) {+ localStorage.setItem(lsKeyAppLockMethod, lockType);}return {@@ -624,7 +632,7 @@ const setupPassphraseLock = async (resetBruteForceState(true),]);- localStorage.setItem(lsKeyLockType, lockType);+ localStorage.setItem(lsKeyAppLockMethod, lockType);localStorage.setItem(lsKeyEnabled, "true");setSnapshot({@@ -669,7 +677,7 @@ export const setupDeviceLock = async (): Promise<SetupDeviceLockResult> => {await resetBruteForceState(true);- localStorage.setItem(lsKeyLockType, "device");+ localStorage.setItem(lsKeyAppLockMethod, "device");localStorage.setItem(lsKeyEnabled, "true");setSnapshot({@@ -841,9 +849,9 @@ export const lock = () => {*/export const logoutAppLock = async () => {localStorage.removeItem(lsKeyEnabled);- localStorage.removeItem(lsKeyLockType);+ localStorage.removeItem(lsKeyAppLockMethod);localStorage.removeItem(lsKeyAutoLockTimeMs);- localStorage.removeItem(lsKeyDeviceLockEnabled);+ localStorage.removeItem(lsLegacyKeyDeviceLockEnabled);await Promise.all([clearPassphraseMaterial(),@@ -882,8 +890,8 @@ export const disableAppLock = async () => {]);localStorage.setItem(lsKeyEnabled, "false");- localStorage.removeItem(lsKeyLockType);- localStorage.removeItem(lsKeyDeviceLockEnabled);+ localStorage.removeItem(lsKeyAppLockMethod);+ localStorage.removeItem(lsLegacyKeyDeviceLockEnabled);setSnapshot({..._state.snapshot,