From c7b0655ec241cac8bd269a0be920df878752604b Mon Sep 17 00:00:00 2001 From: kric_jspark Date: Fri, 8 May 2026 11:04:18 +0900 Subject: [PATCH] =?UTF-8?q?260508=5F08=5FENH:=20=EC=83=88=EB=A1=9C?= =?UTF-8?q?=EA=B3=A0=EC=B9=A8=20=EC=8B=9C=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=20?= =?UTF-8?q?=ED=92=80=EB=A6=AC=EB=8A=94=20=EB=AC=B8=EC=A0=9C=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=20=EC=A4=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/store/storage/secure-storage.ts | 100 ++++++++++++---------------- 1 file changed, 43 insertions(+), 57 deletions(-) diff --git a/src/store/storage/secure-storage.ts b/src/store/storage/secure-storage.ts index 56d6aef..0db14cf 100644 --- a/src/store/storage/secure-storage.ts +++ b/src/store/storage/secure-storage.ts @@ -1,71 +1,62 @@ import { Security } from "@/lib/security"; -import {clientEnv} from "@/lib/env.client"; +import { clientEnv } from "@/lib/env.client"; -type StorageValue = any; // 요청대로 any 유지 +type StorageValue = any; -// --- 내부 헬퍼 함수들 --- - -const getDecryptedValue = (value: string): StorageValue => { - try { - if (!value || value.trim() === "") { - return null; - } - - const decryptValue = clientEnv.ENV ==="prod" ? Security.decryptBase64(value) : value; - - if (!decryptValue || decryptValue.trim() === "") { - return null; - } - - return JSON.parse(decryptValue); - } catch (error) { - // 손상된 데이터인 경우 null 반환 +const parseStoredValue = (value: string): StorageValue => { + if (!value || value.trim() === "") { return null; } + + if (clientEnv.ENV !== "prod") { + return JSON.parse(value); + } + + const decryptValue = Security.decryptBase64(value); + if (!decryptValue || decryptValue.trim() === "") { + return null; + } + + return JSON.parse(decryptValue); }; -const getEncryptedValue = (value: StorageValue): string => { +const stringifyStorageValue = (value: StorageValue): string => { const stringifyValue = JSON.stringify(value); - return clientEnv.ENV ==="prod" ? Security.encryptBase64(stringifyValue) : stringifyValue; + return clientEnv.ENV === "prod" ? Security.encryptBase64(stringifyValue) : stringifyValue; }; - -// --- LocalStorage 함수들 --- - export const setLocalStorage = (key: string, value: StorageValue): void => { try { - const encryptValue = getEncryptedValue(value); - localStorage.setItem(key, encryptValue); + localStorage.setItem(key, stringifyStorageValue(value)); } catch (error) { - // 필요하면 로깅 추가 + console.warn("[storage] localStorage set failed", { key, error }); } }; export const getLocalStorage = (key: string): StorageValue => { try { - const encryptValue: string | null = localStorage.getItem(key); - if (encryptValue === null || encryptValue === "") { + const storedValue = localStorage.getItem(key); + if (storedValue === null || storedValue === "") { return null; } - let value: StorageValue = null; - try { - // 암호화된 값 복호화 시도 - value = getDecryptedValue(encryptValue); + return parseStoredValue(storedValue); } catch (decryptError) { - // 복호화 실패 시 평문으로 파싱 시도 (fallback) try { - value = JSON.parse(encryptValue); + return JSON.parse(storedValue); } catch (fallbackError) { - // 완전히 실패한 경우 저장된 데이터 제거 localStorage.removeItem(key); + console.warn("[storage] localStorage parse failed; removed corrupted value", { + key, + decryptError, + fallbackError, + }); return null; } } - - return value; } catch (error) { + console.warn("[storage] localStorage get failed", { key, error }); return null; } }; @@ -78,43 +69,38 @@ export const clearLocalStorage = (): void => { localStorage.clear(); }; -// --- SessionStorage 함수들 --- - -// 세션스토리지는 테스트/실제 서버 모두 암호화 없음 export const setSessionStorage = (key: string, value: StorageValue): void => { try { - const encryptValue = getEncryptedValue(value); - sessionStorage.setItem(key, encryptValue); + sessionStorage.setItem(key, stringifyStorageValue(value)); } catch (error) { - // 필요하면 로깅 추가 + console.warn("[storage] sessionStorage set failed", { key, error }); } }; export const getSessionStorage = (key: string): StorageValue => { try { - const encryptValue: string | null = sessionStorage.getItem(key); - if (encryptValue === null || encryptValue === "") { + const storedValue = sessionStorage.getItem(key); + if (storedValue === null || storedValue === "") { return null; } - let value: StorageValue = null; - try { - // 암호화된 값 복호화 시도 - value = getDecryptedValue(encryptValue); + return parseStoredValue(storedValue); } catch (decryptError) { - // 복호화 실패 시 평문으로 파싱 시도 (fallback) try { - value = JSON.parse(encryptValue); + return JSON.parse(storedValue); } catch (fallbackError) { - // 완전히 실패한 경우 저장된 데이터 제거 - localStorage.removeItem(key); + sessionStorage.removeItem(key); + console.warn("[storage] sessionStorage parse failed; removed corrupted value", { + key, + decryptError, + fallbackError, + }); return null; } } - - return value; } catch (error) { + console.warn("[storage] sessionStorage get failed", { key, error }); return null; } }; @@ -125,4 +111,4 @@ export const removeSessionStorage = (key: string): void => { export const clearSessionStorage = (): void => { sessionStorage.clear(); -}; \ No newline at end of file +};