diff --git a/src/lib/security.ts b/src/lib/security.ts index 752fdfa..b32b780 100644 --- a/src/lib/security.ts +++ b/src/lib/security.ts @@ -165,8 +165,7 @@ export class Security { } public static encodeBase64(input: WordArray): string { - const base64 = CryptoJS.enc.Base64.stringify(input); - return this.convertToSpecialChars(base64); + return CryptoJS.enc.Base64.stringify(input); } public static decodeBase64(input: string): WordArray { diff --git a/src/store/storage/secure-storage.ts b/src/store/storage/secure-storage.ts index 7f58358..670ace1 100644 --- a/src/store/storage/secure-storage.ts +++ b/src/store/storage/secure-storage.ts @@ -22,9 +22,20 @@ const parseStoredValue = (value: string): StorageValue => { const stringifyStorageValue = (value: StorageValue): string => { const stringifyValue = JSON.stringify(value); - return stringifyValue; + return clientEnv.ENV === "prod" ? Security.encryptBase64(stringifyValue) : stringifyValue; }; +const isPlainJsonValue = (value: string) => { + const trimmed = value.trim(); + return trimmed.startsWith("{") || trimmed.startsWith("["); +}; + +const isLegacyEncryptedStorageValue = (value: string) => + value.includes("\u200b") || value.includes("\u200c"); + +const shouldMigrateStorageValue = (value: string) => + isPlainJsonValue(value) || isLegacyEncryptedStorageValue(value); + export const setLocalStorage = (key: string, value: StorageValue): void => { try { localStorage.setItem(key, stringifyStorageValue(value)); @@ -41,10 +52,16 @@ export const getLocalStorage = (key: string): StorageValue => { } try { - return parseStoredValue(storedValue); + const parsedValue = parseStoredValue(storedValue); + if (parsedValue != null && shouldMigrateStorageValue(storedValue)) { + localStorage.setItem(key, stringifyStorageValue(parsedValue)); + } + return parsedValue; } catch (decryptError) { try { - return JSON.parse(storedValue); + const parsedValue = JSON.parse(storedValue); + localStorage.setItem(key, stringifyStorageValue(parsedValue)); + return parsedValue; } catch (fallbackError) { console.warn("[storage] localStorage parse failed", { key, @@ -84,10 +101,16 @@ export const getSessionStorage = (key: string): StorageValue => { } try { - return parseStoredValue(storedValue); + const parsedValue = parseStoredValue(storedValue); + if (parsedValue != null && shouldMigrateStorageValue(storedValue)) { + sessionStorage.setItem(key, stringifyStorageValue(parsedValue)); + } + return parsedValue; } catch (decryptError) { try { - return JSON.parse(storedValue); + const parsedValue = JSON.parse(storedValue); + sessionStorage.setItem(key, stringifyStorageValue(parsedValue)); + return parsedValue; } catch (fallbackError) { console.warn("[storage] sessionStorage parse failed", { key,