From 35ef6f65444fb9134728599c333332b0078217bb Mon Sep 17 00:00:00 2001 From: Ivan Lomovtcev <47413395+advanceddev@users.noreply.github.com> Date: Fri, 30 Oct 2026 15:01:43 +0500 Subject: [PATCH] Potential fix for code scanning alert no. 1: Clear text storage of sensitive information Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> --- core/storage.ts | 61 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/core/storage.ts b/core/storage.ts index 89ef2e1..4291b93 100644 --- a/core/storage.ts +++ b/core/storage.ts @@ -5,6 +5,60 @@ import { assertBrowser, TBridgeError } from './errors'; +const ENC_PREFIX = 'enc:v1:'; +const IV_LENGTH = 12; +let cachedStorageKey: Promise | null = null; + +function bytesToBase64(bytes: Uint8Array): string { + let binary = ''; + for (let i = 0; i < bytes.length; i += 1) binary += String.fromCharCode(bytes[i]); + return btoa(binary); +} + +function base64ToBytes(base64: string): Uint8Array { + const binary = atob(base64); + const bytes = new Uint8Array(binary.length); + for (let i = 0; i < binary.length; i += 1) bytes[i] = binary.charCodeAt(i); + return bytes; +} + +async function getStorageCryptoKey(): Promise { + if (!cachedStorageKey) { + cachedStorageKey = (async () => { + const secret = + typeof (globalThis as { __TBRIDGE_STORAGE_SECRET__?: string }).__TBRIDGE_STORAGE_SECRET__ === + 'string' + ? (globalThis as { __TBRIDGE_STORAGE_SECRET__?: string }).__TBRIDGE_STORAGE_SECRET__! + : `${window.location.origin}:tbridge:storage:fallback`; + const material = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(secret)); + return crypto.subtle.importKey('raw', material, 'AES-GCM', false, ['encrypt', 'decrypt']); + })(); + } + return cachedStorageKey; +} + +async function encryptForStorage(plain: string): Promise { + const iv = crypto.getRandomValues(new Uint8Array(IV_LENGTH)); + const key = await getStorageCryptoKey(); + const data = new TextEncoder().encode(plain); + const encrypted = await crypto.subtle.encrypt({ name: 'AES-GCM', iv }, key, data); + const payload = new Uint8Array(iv.length + encrypted.byteLength); + payload.set(iv, 0); + payload.set(new Uint8Array(encrypted), iv.length); + return `${ENC_PREFIX}${bytesToBase64(payload)}`; +} + +async function decryptFromStorage(stored: string): Promise { + if (!stored.startsWith(ENC_PREFIX)) return stored; + const payload = base64ToBytes(stored.slice(ENC_PREFIX.length)); + if (payload.length <= IV_LENGTH) throw new Error('Invalid encrypted payload'); + const iv = payload.slice(0, IV_LENGTH); + const ciphertext = payload.slice(IV_LENGTH); + const key = await getStorageCryptoKey(); + const decrypted = await crypto.subtle.decrypt({ name: 'AES-GCM', iv }, key, ciphertext); + return new TextDecoder().decode(decrypted); +} + export interface StorageAdapter { getItem(key: string): Promise; setItem(key: string, value: string): Promise; @@ -20,7 +74,9 @@ export class LocalStorageAdapter implements StorageAdapter { async getItem(key: string): Promise { assertBrowser(); try { - return window.localStorage.getItem(key); + const stored = window.localStorage.getItem(key); + if (stored === null) return null; + return await decryptFromStorage(stored); } catch (e) { throw new TBridgeError('STORAGE_ERROR', 'Failed to read from localStorage', e); } @@ -29,7 +85,8 @@ export class LocalStorageAdapter implements StorageAdapter { async setItem(key: string, value: string): Promise { assertBrowser(); try { - window.localStorage.setItem(key, value); + const encrypted = await encryptForStorage(value); + window.localStorage.setItem(key, encrypted); } catch (e) { throw new TBridgeError('STORAGE_ERROR', 'Failed to write to localStorage', e); }