From 440e7b7ae851c42e360908225ece982bd25429c7 Mon Sep 17 00:00:00 2001 From: Moepchi Date: Fri, 23 Oct 2026 20:43:40 +0000 Subject: [PATCH] Options -> Sicherheit: real master password Identities, favorites (with their passwords), saved channel passwords and the store admin token are kept AES-GCM encrypted (PBKDF2, 310k rounds) in one localStorage entry and only decrypted in memory. The app asks for the password on load before App is imported; "forgot password" wipes the encrypted data. Enable, change and disable (the latter two re-ask the current password). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU --- web/src/App.css | 27 +++++++ web/src/App.tsx | 85 ++++++++++++++++++++-- web/src/locales/cs-CZ.json | 14 +++- web/src/locales/de.json | 14 +++- web/src/locales/en.json | 14 +++- web/src/locales/fa.json | 14 +++- web/src/locales/zh-CN.json | 14 +++- web/src/main.tsx | 69 ++++++++++++++++-- web/src/vault.ts | 145 +++++++++++++++++++++++++++++++++++++ 9 files changed, 377 insertions(+), 19 deletions(-) create mode 100644 web/src/vault.ts diff --git a/web/src/App.css b/web/src/App.css index 7c6b56f..47d25ae 100644 --- a/web/src/App.css +++ b/web/src/App.css @@ -4872,3 +4872,30 @@ background: var(--bg-toolbar-1); border: 1px solid var(--border); } + +/* Master password unlock (main.tsx) */ +.ts-vault-dialog { + display: flex; + flex-direction: column; + gap: 8px; + padding: 16px; + min-width: 300px; +} +.ts-vault-dialog h3, +.ts-vault-dialog p { + margin: 0; +} +.ts-vault-wrong { + color: var(--log-error); +} +.ts-master-form { + display: flex; + flex-direction: column; + gap: 6px; + margin: 4px 0 4px 24px; + max-width: 320px; +} +.ts-master-note { + margin: 0; + font-size: 0.9em; +} diff --git a/web/src/App.tsx b/web/src/App.tsx index b316876..fd395cd 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -141,6 +141,7 @@ import { type SoundEventId, } from "./sounds"; import { parseSoundpack } from "./soundpack"; +import { checkMasterPassword, isVaultUnlocked, removeMasterPassword, setMasterPassword } from "./vault"; import { BUILTIN_PACK, NO_SOUNDS_PACK, @@ -6623,6 +6624,82 @@ function ChatPanel() { ); } +/** Options -> Sicherheit -> "Master Passwort benutzen" / "ändern" (vault.ts). */ +function MasterPasswordRow() { + const t = useT(); + const [on, setOn] = useState(isVaultUnlocked); + // What the inline form is doing: turning it on, changing it, or turning it off. + const [mode, setMode] = useState<"enable" | "change" | "disable" | null>(null); + const [current, setCurrent] = useState(""); + const [next, setNext] = useState(""); + const [repeat, setRepeat] = useState(""); + const [error, setError] = useState(""); + const [busy, setBusy] = useState(false); + const open = (m: typeof mode) => { + setMode(m); + setCurrent(""); + setNext(""); + setRepeat(""); + setError(""); + }; + const submit = async (e: React.FormEvent) => { + e.preventDefault(); + if (mode !== "enable" && !(await checkMasterPassword(current))) return setError(t("vault.wrong")); + if (mode !== "disable" && next !== repeat) return setError(t("securitySettings.masterMismatch")); + setBusy(true); + if (mode === "disable") await removeMasterPassword(); + else await setMasterPassword(next); + setBusy(false); + setOn(isVaultUnlocked()); + setMode(null); + }; + return ( + <> +
+ + +
+ {mode && ( +
+ {mode !== "enable" && ( + + )} + {mode !== "disable" && ( + <> + + + + )} + {mode === "enable" &&

{t("securitySettings.masterNote")}

} + {error &&

{error}

} +
+ + +
+
+ )} + + ); +} + function SecurityPanel({ onOpenIdentities, onClearCache }: { onOpenIdentities: () => void; onClearCache: () => void }) { const t = useT(); const { security, setSecurity } = useContext(DisplayPrefsContext); @@ -6652,13 +6729,7 @@ function SecurityPanel({ onOpenIdentities, onClearCache }: { onOpenIdentities: ( {t("securitySettings.clearChannelPasswords")} -
- - -
+
{t("securitySettings.cleanup")} diff --git a/web/src/locales/cs-CZ.json b/web/src/locales/cs-CZ.json index 32e4d2d..814b934 100644 --- a/web/src/locales/cs-CZ.json +++ b/web/src/locales/cs-CZ.json @@ -905,5 +905,17 @@ "notify.applyToFavorites": "Použít tento zvukový balíček pro všechny oblíbené", "notify.applyToFavoritesConfirm": "Nastavit všem oblíbeným tento zvukový balíček? Vlastní balíčky jednotlivých oblíbených budou zrušeny.", "notify.whenMuted.important": "Přehrávat jen důležité zvuky", - "notify.text.client": "Klient" + "notify.text.client": "Klient", + "vault.title": "Hlavní heslo", + "vault.prompt": "Vaše identity a uložená hesla jsou zašifrovány. Zadejte hlavní heslo.", + "vault.unlock": "Odemknout", + "vault.wrong": "Špatné heslo.", + "vault.forgot": "Zapomenuté heslo", + "vault.forgotConfirm": "Bez hesla nelze data obnovit. Trvale smazat identity, oblíbené a uložená hesla a začít znovu?", + "securitySettings.masterHint": "Šifruje identity, oblíbené (s hesly) a uložená hesla kanálů. Vyžaduje se po každém načtení.", + "securitySettings.masterCurrent": "Současné hlavní heslo", + "securitySettings.masterNew": "Nové hlavní heslo", + "securitySettings.masterRepeat": "Zopakovat", + "securitySettings.masterMismatch": "Hesla se neshodují.", + "securitySettings.masterNote": "Dobře si ho zapamatujte: bez něj přijdete o identity a uložená hesla." } diff --git a/web/src/locales/de.json b/web/src/locales/de.json index 21bef50..c155b8b 100644 --- a/web/src/locales/de.json +++ b/web/src/locales/de.json @@ -905,5 +905,17 @@ "notify.applyToFavorites": "Dieses Sound Pack für alle Favoriten verwenden", "notify.applyToFavoritesConfirm": "Alle Favoriten auf dieses Sound Pack setzen? Eigene Sound Packs einzelner Favoriten werden dabei zurückgesetzt.", "notify.whenMuted.important": "Nur wichtige Sounds spielen", - "notify.text.client": "Client" + "notify.text.client": "Client", + "vault.title": "Master Passwort", + "vault.prompt": "Deine Identitäten und gespeicherten Passwörter sind verschlüsselt. Gib dein Master Passwort ein.", + "vault.unlock": "Entsperren", + "vault.wrong": "Falsches Passwort.", + "vault.forgot": "Passwort vergessen", + "vault.forgotConfirm": "Ohne Passwort lassen sich die Daten nicht wiederherstellen. Identitäten, Favoriten und gespeicherte Passwörter endgültig löschen und neu anfangen?", + "securitySettings.masterHint": "Verschlüsselt Identitäten, Favoriten (mit Passwörtern) und gespeicherte Channel-Passwörter. Nach jedem Neuladen wird das Passwort abgefragt.", + "securitySettings.masterCurrent": "Aktuelles Master Passwort", + "securitySettings.masterNew": "Neues Master Passwort", + "securitySettings.masterRepeat": "Wiederholen", + "securitySettings.masterMismatch": "Die Passwörter stimmen nicht überein.", + "securitySettings.masterNote": "Merk es dir gut: Ohne das Passwort sind Identitäten und gespeicherte Passwörter weg." } diff --git a/web/src/locales/en.json b/web/src/locales/en.json index 96faadd..243180a 100644 --- a/web/src/locales/en.json +++ b/web/src/locales/en.json @@ -905,5 +905,17 @@ "notify.applyToFavorites": "Use this sound pack for all favorites", "notify.applyToFavoritesConfirm": "Set all favorites to this sound pack? Sound packs set on individual favorites will be reset.", "notify.whenMuted.important": "Play only important sounds", - "notify.text.client": "Client" + "notify.text.client": "Client", + "vault.title": "Master password", + "vault.prompt": "Your identities and saved passwords are encrypted. Enter your master password.", + "vault.unlock": "Unlock", + "vault.wrong": "Wrong password.", + "vault.forgot": "Forgot password", + "vault.forgotConfirm": "Without the password the data can't be recovered. Permanently delete identities, favorites and saved passwords and start over?", + "securitySettings.masterHint": "Encrypts identities, favorites (with passwords) and saved channel passwords. Asked for after every reload.", + "securitySettings.masterCurrent": "Current master password", + "securitySettings.masterNew": "New master password", + "securitySettings.masterRepeat": "Repeat", + "securitySettings.masterMismatch": "The passwords don't match.", + "securitySettings.masterNote": "Remember it well: without it, identities and saved passwords are lost." } diff --git a/web/src/locales/fa.json b/web/src/locales/fa.json index 928cd53..34d55f2 100644 --- a/web/src/locales/fa.json +++ b/web/src/locales/fa.json @@ -904,5 +904,17 @@ "notify.applyToFavorites": "استفاده از این بسته صدا برای همه موارد علاقه", "notify.applyToFavoritesConfirm": "همه موارد علاقه روی این بسته صدا تنظیم شوند؟ بسته‌های صدای جداگانه بازنشانی می‌شوند.", "notify.whenMuted.important": "فقط صداهای مهم پخش شوند", - "notify.text.client": "کلاینت" + "notify.text.client": "کلاینت", + "vault.title": "رمز اصلی", + "vault.prompt": "هویت‌ها و رمزهای ذخیره‌شده شما رمزنگاری شده‌اند. رمز اصلی را وارد کنید.", + "vault.unlock": "باز کردن", + "vault.wrong": "رمز اشتباه است.", + "vault.forgot": "رمز را فراموش کرده‌ام", + "vault.forgotConfirm": "بدون رمز، داده‌ها قابل بازیابی نیستند. هویت‌ها، موارد علاقه و رمزهای ذخیره‌شده برای همیشه حذف شوند و از نو شروع کنید؟", + "securitySettings.masterHint": "هویت‌ها، موارد علاقه (با رمزها) و رمزهای ذخیره‌شده کانال را رمزنگاری می‌کند. پس از هر بارگذاری پرسیده می‌شود.", + "securitySettings.masterCurrent": "رمز اصلی فعلی", + "securitySettings.masterNew": "رمز اصلی جدید", + "securitySettings.masterRepeat": "تکرار", + "securitySettings.masterMismatch": "رمزها یکسان نیستند.", + "securitySettings.masterNote": "آن را به خاطر بسپارید: بدون آن، هویت‌ها و رمزهای ذخیره‌شده از دست می‌روند." } diff --git a/web/src/locales/zh-CN.json b/web/src/locales/zh-CN.json index e4cd99e..d679cfa 100644 --- a/web/src/locales/zh-CN.json +++ b/web/src/locales/zh-CN.json @@ -904,5 +904,17 @@ "notify.applyToFavorites": "将此声音包用于所有收藏", "notify.applyToFavoritesConfirm": "将所有收藏设为此声音包?单个收藏的声音包设置将被重置。", "notify.whenMuted.important": "仅播放重要声音", - "notify.text.client": "客户端" + "notify.text.client": "客户端", + "vault.title": "主密码", + "vault.prompt": "你的身份和已保存的密码已加密。请输入主密码。", + "vault.unlock": "解锁", + "vault.wrong": "密码错误。", + "vault.forgot": "忘记密码", + "vault.forgotConfirm": "没有密码无法恢复数据。永久删除身份、收藏和已保存的密码并重新开始?", + "securitySettings.masterHint": "加密身份、收藏(含密码)和已保存的频道密码。每次重新加载后都会询问。", + "securitySettings.masterCurrent": "当前主密码", + "securitySettings.masterNew": "新主密码", + "securitySettings.masterRepeat": "重复", + "securitySettings.masterMismatch": "两次密码不一致。", + "securitySettings.masterNote": "请牢记:没有它,身份和已保存的密码将丢失。" } diff --git a/web/src/main.tsx b/web/src/main.tsx index 5c3cfe5..78509eb 100644 --- a/web/src/main.tsx +++ b/web/src/main.tsx @@ -1,12 +1,67 @@ import './storageFallback' -import { StrictMode } from 'react' +import { StrictMode, useState, type FormEvent } from 'react' import { createRoot } from 'react-dom/client' import './index.css' import './styles/greenteaspeak.css' -import App from './App.tsx' +import './App.css' +import { LanguageProvider, useT } from './i18n' +import { discardVault, hasVault, unlockVault } from './vault' -createRoot(document.getElementById('root')!).render( - - - , -) +const root = createRoot(document.getElementById('root')!) + +// App reads its stored identities/favorites while loading, so it's imported only after the vault is open. +async function start() { + const { default: App } = await import('./App.tsx') + root.render( + + + , + ) +} + +function Unlock() { + const t = useT() + const [password, setPassword] = useState('') + const [wrong, setWrong] = useState(false) + const [busy, setBusy] = useState(false) + const submit = async (e: FormEvent) => { + e.preventDefault() + setBusy(true) + if (await unlockVault(password)) return void start() + setBusy(false) + setWrong(true) + } + return ( +
+
+

{t('vault.title')}

+

{t('vault.prompt')}

+ setPassword(e.target.value)} /> + {wrong &&

{t('vault.wrong')}

} +
+ + +
+
+
+ ) +} + +if (hasVault()) + root.render( + + + , + ) +else void start() diff --git a/web/src/vault.ts b/web/src/vault.ts new file mode 100644 index 0000000..8c81361 --- /dev/null +++ b/web/src/vault.ts @@ -0,0 +1,145 @@ +// Options -> Sicherheit -> "Master Passwort benutzen". The keys below hold secrets +// (identity private keys, server/channel passwords, the store admin token). With a +// master password they live AES-GCM encrypted in one localStorage entry and only in +// memory once unlocked. Like storageFallback.ts, this swaps window.localStorage for +// a stand-in, so the dozens of plain localStorage reads in the app keep working. +// main.tsx unlocks before importing App, whose module-level code reads these keys. + +export const PROTECTED_KEYS = [ + "webspeak3:identities", + "webspeak3:identity", + "webspeak3:favorites", + "webspeak3:channel-passwords", + "webspeak3:store-admin-token", +]; + +const VAULT_KEY = "webspeak3:vault"; +const ITERATIONS = 310_000; + +interface StoredVault { + salt: string; + iv: string; + data: string; +} + +const real = window.localStorage; +let unlocked: { key: CryptoKey; salt: Uint8Array; secrets: Map } | null = null; + +const b64 = (bytes: Uint8Array) => btoa(String.fromCharCode(...bytes)); +const unb64 = (s: string) => Uint8Array.from(atob(s), (c) => c.charCodeAt(0)); + +async function deriveKey(password: string, salt: Uint8Array): Promise { + const base = await crypto.subtle.importKey("raw", new TextEncoder().encode(password), "PBKDF2", false, ["deriveKey"]); + return crypto.subtle.deriveKey( + { name: "PBKDF2", salt: salt as BufferSource, iterations: ITERATIONS, hash: "SHA-256" }, + base, + { name: "AES-GCM", length: 256 }, + false, + ["encrypt", "decrypt"] + ); +} + +export const hasVault = () => real.getItem(VAULT_KEY) !== null; +export const isVaultUnlocked = () => unlocked !== null; + +// Writes are chained so an older encryption never lands after a newer one. +let writing: Promise = Promise.resolve(); +function persist(): Promise { + writing = writing.then(async () => { + if (!unlocked) return; + const iv = crypto.getRandomValues(new Uint8Array(12)); + const plain = new TextEncoder().encode(JSON.stringify(Object.fromEntries(unlocked.secrets))); + const data = new Uint8Array(await crypto.subtle.encrypt({ name: "AES-GCM", iv }, unlocked.key, plain)); + real.setItem(VAULT_KEY, JSON.stringify({ salt: b64(unlocked.salt), iv: b64(iv), data: b64(data) } satisfies StoredVault)); + }); + return writing; +} + +function installProxy(): void { + const secrets = () => unlocked!.secrets; + const isProtected = (k: string) => PROTECTED_KEYS.includes(k); + const proxy: Storage = { + get length() { + return real.length + secrets().size; + }, + key: (i) => (i < real.length ? real.key(i) : ([...secrets().keys()][i - real.length] ?? null)), + getItem: (k) => (isProtected(k) ? (secrets().get(k) ?? null) : real.getItem(k)), + setItem: (k, v) => { + if (!isProtected(k)) return real.setItem(k, v); + secrets().set(k, String(v)); + void persist(); + }, + removeItem: (k) => { + if (!isProtected(k)) return real.removeItem(k); + secrets().delete(k); + void persist(); + }, + clear: () => { + real.clear(); + secrets().clear(); + }, + }; + // Object.keys(localStorage) has to see real keys too. + for (let i = 0; i < real.length; i++) { + const k = real.key(i)!; + if (k !== VAULT_KEY) Object.defineProperty(proxy, k, { get: () => real.getItem(k), enumerable: true, configurable: true }); + } + Object.defineProperty(window, "localStorage", { value: proxy, configurable: true }); +} + +async function openStored(password: string) { + await writing; + const stored: StoredVault = JSON.parse(real.getItem(VAULT_KEY)!); + const salt = unb64(stored.salt); + const key = await deriveKey(password, salt); + try { + const plain = await crypto.subtle.decrypt({ name: "AES-GCM", iv: unb64(stored.iv) as BufferSource }, key, unb64(stored.data) as BufferSource); + return { key, salt, secrets: new Map(Object.entries(JSON.parse(new TextDecoder().decode(plain)))) }; + } catch { + return null; // wrong password + } +} + +/** false = wrong password. */ +export async function unlockVault(password: string): Promise { + unlocked = await openStored(password); + if (unlocked) installProxy(); + return unlocked !== null; +} + +/** Re-asks the current password before changing or removing it. */ +export const checkMasterPassword = async (password: string) => (await openStored(password)) !== null; + +/** Turns the master password on, or changes it when already on. */ +export async function setMasterPassword(password: string): Promise { + const salt = crypto.getRandomValues(new Uint8Array(16)); + const key = await deriveKey(password, salt); + if (unlocked) { + unlocked = { ...unlocked, key, salt }; + } else { + const secrets = new Map(); + for (const k of PROTECTED_KEYS) { + const v = real.getItem(k); + if (v !== null) secrets.set(k, v); + } + unlocked = { key, salt, secrets }; + installProxy(); + } + await persist(); + for (const k of PROTECTED_KEYS) real.removeItem(k); +} + +/** Turns it off: secrets go back to plain localStorage. */ +export async function removeMasterPassword(): Promise { + if (!unlocked) return; + await writing; + for (const [k, v] of unlocked.secrets) real.setItem(k, v); + real.removeItem(VAULT_KEY); + unlocked = null; + Object.defineProperty(window, "localStorage", { value: real, configurable: true }); +} + +/** "Passwort vergessen": drops the encrypted data for good. */ +export function discardVault(): void { + real.removeItem(VAULT_KEY); +}