From fa809e861ba19416bc7d8825f6dff3ef4ea9dcba Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Tue, 27 Oct 2026 07:55:08 +0000 Subject: [PATCH] fix(client): auto-detect Linux password store before app ready (#15450) ## Summary - Detect Linux desktop/session environments before Electron app readiness and set a secure `--password-store` backend when Electron would otherwise miss it. - Prefer `gnome-libsecret` for GNOME-compatible desktops and common tiling/window-manager sessions that often run libsecret/gnome-keyring. - Prefer the appropriate KWallet backend for KDE/Plasma sessions, including KDE 5/6 detection, while respecting any user-provided `--password-store` flag. - Add unit coverage for Linux/non-Linux behavior, existing user flags, GNOME/libsecret desktops, tiling WMs, KDE/Plasma KWallet variants, colon-separated desktop values, and fallback session variables. ## Test plan - [x] `yarn vitest packages/frontend/apps/electron/test/main/secure-storage.spec.ts` - [x] Electron main-process related tests were run; relevant tests passed, with unrelated existing failures noted in `recording-feature.spec.ts`. - [x] Prettier, oxlint, eslint, and TypeScript checks passed for the modified Electron files. Closes: #15301 ## Summary by CodeRabbit * **Bug Fixes** * Improved secure password-store selection on Linux. * Added support for KDE/Plasma and GNOME-compatible desktop environments. * Preserves manually configured password-store settings and selects an appropriate fallback when needed. * **Tests** * Added coverage for Linux desktop detection, fallback behavior, session variations, and non-Linux environments. --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: DarkSky <25152247+darkskygit@users.noreply.github.com> Co-authored-by: donqu1xotevincent --- .../frontend/apps/electron/src/main/index.ts | 3 + .../apps/electron/src/main/secure-storage.ts | 104 ++++++++ .../electron/test/main/secure-storage.spec.ts | 251 ++++++++++++++++++ 3 files changed, 358 insertions(+) create mode 100644 packages/frontend/apps/electron/src/main/secure-storage.ts create mode 100644 packages/frontend/apps/electron/test/main/secure-storage.spec.ts diff --git a/packages/frontend/apps/electron/src/main/index.ts b/packages/frontend/apps/electron/src/main/index.ts index 83ec479db3..abc35da7e7 100644 --- a/packages/frontend/apps/electron/src/main/index.ts +++ b/packages/frontend/apps/electron/src/main/index.ts @@ -13,6 +13,7 @@ import { registerHandlers } from './handlers'; import { logger } from './logger'; import { registerProtocol } from './protocol'; import { setupRecordingFeature } from './recording/feature'; +import { ensureSecureLinuxPasswordStore } from './secure-storage'; import { registerSecurityRestrictions } from './security-restrictions'; import { setupTrayState } from './tray'; import { registerUpdater } from './updater'; @@ -21,6 +22,8 @@ import { launchStage } from './windows-manager/stage'; app.enableSandbox(); +ensureSecureLinuxPasswordStore(); + if (isDev) { // In electron the dev server will be resolved to 0.0.0.0, but it // might be blocked by electron. diff --git a/packages/frontend/apps/electron/src/main/secure-storage.ts b/packages/frontend/apps/electron/src/main/secure-storage.ts new file mode 100644 index 0000000000..ff666f8984 --- /dev/null +++ b/packages/frontend/apps/electron/src/main/secure-storage.ts @@ -0,0 +1,104 @@ +import { app } from "electron"; + +import { logger } from "./logger"; + +// Desktops that Electron already maps to gnome-libsecret, or to which it is +// safe to force that backend (GNOME, MATE, Budgie, etc., and tiling window +// managers that typically run gnome-keyring/libsecret). +const GNOME_LIBSECRET_DESKTOPS = new Set([ + // Electron's recognized GNOME-based desktops + "gnome", + "x-cinnamon", + "cinnamon", + "deepin", + "pantheon", + "xfce", + "ukui", + "unity", + // Other common libsecret-compatible environments + "mate", + "budgie", + // Common tiling / minimalist WMs where users often run gnome-keyring + "sway", + "i3", + "i3wm", + "hyprland", + "bspwm", + "awesome", + "dwm", + "qtile", + "xmonad", + "niri", + "wayfire", + "river", + "weston", + "wlroots", +]); + +// KDE/Plasma sessions should use KWallet rather than gnome-libsecret. +const KWALLET_DESKTOPS = new Set(["kde", "plasma"]); + +function getKWalletFlag(): string { + const version = process.env.KDE_SESSION_VERSION; + if (version === "6") return "kwallet6"; + if (version === "5") return "kwallet5"; + if (version === "4") return "kwallet"; + // Fallback matches Chromium's behaviour for KDE without KDE_SESSION_VERSION. + return "kwallet"; +} + +function isKdeSession(): boolean { + return ( + process.env.KDE_FULL_SESSION === "true" || + process.env.KDE_SESSION_VERSION !== undefined + ); +} + +function getDesktopTokens(): string[] { + const raw = [process.env.XDG_CURRENT_DESKTOP, process.env.XDG_SESSION_DESKTOP] + .filter((desktop): desktop is string => Boolean(desktop)) + .join(":"); + if (!raw) { + return []; + } + return raw + .split(":") + .map((token) => token.trim().toLowerCase()) + .filter(Boolean); +} + +function detectLinuxPasswordStore(): string | null { + const tokens = getDesktopTokens(); + + if (tokens.some((token) => KWALLET_DESKTOPS.has(token))) { + return getKWalletFlag(); + } + + // When the desktop is unknown, fall back on KDE session variables so we do + // not force gnome-libsecret on a Plasma/i3 hybrid that uses KWallet. + if (tokens.length > 0 && isKdeSession()) { + return getKWalletFlag(); + } + + if (tokens.some((token) => GNOME_LIBSECRET_DESKTOPS.has(token))) { + return "gnome-libsecret"; + } + + return null; +} + +export function ensureSecureLinuxPasswordStore() { + if (process.platform !== "linux") { + return; + } + + if (app.commandLine.hasSwitch("password-store")) { + return; + } + + const store = detectLinuxPasswordStore(); + if (store) { + logger.info(`Secure password store detected; using ${store}`); + app.commandLine.appendSwitch("password-store", store); + } +} diff --git a/packages/frontend/apps/electron/test/main/secure-storage.spec.ts b/packages/frontend/apps/electron/test/main/secure-storage.spec.ts new file mode 100644 index 0000000000..e68d9ba383 --- /dev/null +++ b/packages/frontend/apps/electron/test/main/secure-storage.spec.ts @@ -0,0 +1,251 @@ +import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'; + +const commandLine = vi.hoisted(() => ({ + hasSwitch: vi.fn(), + appendSwitch: vi.fn(), +})); + +vi.mock('electron', () => ({ + app: { commandLine }, +})); + +vi.mock('../../src/main/logger', () => ({ + logger: { info: vi.fn() }, +})); + +import { ensureSecureLinuxPasswordStore } from '../../src/main/secure-storage'; + +let platformSpy: ReturnType | undefined; + +beforeEach(() => { + vi.resetAllMocks(); + vi.unstubAllEnvs(); + // Avoid leaking the host desktop environment into tests. + delete process.env.XDG_CURRENT_DESKTOP; + delete process.env.XDG_SESSION_DESKTOP; + delete process.env.KDE_SESSION_VERSION; + delete process.env.KDE_FULL_SESSION; + commandLine.hasSwitch.mockReturnValue(false); +}); + +afterEach(() => { + vi.unstubAllEnvs(); + platformSpy?.mockRestore(); + platformSpy = undefined; +}); + +describe('ensureSecureLinuxPasswordStore', () => { + test('does nothing on non-Linux platforms', () => { + platformSpy = vi.spyOn(process, 'platform', 'get').mockReturnValue('win32'); + + ensureSecureLinuxPasswordStore(); + + expect(commandLine.appendSwitch).not.toHaveBeenCalled(); + }); + + test('does nothing when password-store is already set by the user', () => { + platformSpy = vi.spyOn(process, 'platform', 'get').mockReturnValue('linux'); + commandLine.hasSwitch.mockReturnValue(true); + + ensureSecureLinuxPasswordStore(); + + expect(commandLine.appendSwitch).not.toHaveBeenCalled(); + }); + + test('does nothing when no desktop environment variables are set', () => { + platformSpy = vi.spyOn(process, 'platform', 'get').mockReturnValue('linux'); + + ensureSecureLinuxPasswordStore(); + + expect(commandLine.appendSwitch).not.toHaveBeenCalled(); + }); + + test('does nothing for an unrecognized desktop environment', () => { + platformSpy = vi.spyOn(process, 'platform', 'get').mockReturnValue('linux'); + vi.stubEnv('XDG_CURRENT_DESKTOP', 'EXWM'); + + ensureSecureLinuxPasswordStore(); + + expect(commandLine.appendSwitch).not.toHaveBeenCalled(); + }); + + test('sets password-store=gnome-libsecret for recognized GNOME desktops', () => { + platformSpy = vi.spyOn(process, 'platform', 'get').mockReturnValue('linux'); + vi.stubEnv('XDG_CURRENT_DESKTOP', 'GNOME'); + + ensureSecureLinuxPasswordStore(); + + expect(commandLine.appendSwitch).toHaveBeenCalledWith( + 'password-store', + 'gnome-libsecret' + ); + }); + + test('sets password-store=gnome-libsecret for colon-separated desktop lists', () => { + platformSpy = vi.spyOn(process, 'platform', 'get').mockReturnValue('linux'); + vi.stubEnv('XDG_CURRENT_DESKTOP', 'sway:wlroots'); + + ensureSecureLinuxPasswordStore(); + + expect(commandLine.appendSwitch).toHaveBeenCalledWith( + 'password-store', + 'gnome-libsecret' + ); + }); + + test.each([ + 'sway', + 'i3', + 'i3wm', + 'hyprland', + 'bspwm', + 'awesome', + 'dwm', + 'qtile', + 'xmonad', + 'niri', + 'wayfire', + 'river', + 'weston', + ])('sets password-store=gnome-libsecret for tiling WM %s', desktop => { + platformSpy = vi.spyOn(process, 'platform', 'get').mockReturnValue('linux'); + vi.stubEnv('XDG_CURRENT_DESKTOP', desktop); + + ensureSecureLinuxPasswordStore(); + + expect(commandLine.appendSwitch).toHaveBeenCalledWith( + 'password-store', + 'gnome-libsecret' + ); + }); + + test.each([ + { version: '6', expected: 'kwallet6' }, + { version: '5', expected: 'kwallet5' }, + { version: '4', expected: 'kwallet' }, + { version: undefined, expected: 'kwallet' }, + ])( + 'sets password-store=$expected for KDE with KDE_SESSION_VERSION=$version', + ({ version, expected }) => { + platformSpy = vi + .spyOn(process, 'platform', 'get') + .mockReturnValue('linux'); + vi.stubEnv('XDG_CURRENT_DESKTOP', 'KDE'); + if (version !== undefined) { + vi.stubEnv('KDE_SESSION_VERSION', version); + } + + ensureSecureLinuxPasswordStore(); + + expect(commandLine.appendSwitch).toHaveBeenCalledWith( + 'password-store', + expected + ); + } + ); + + test('sets password-store=kwallet5 for plasma', () => { + platformSpy = vi.spyOn(process, 'platform', 'get').mockReturnValue('linux'); + vi.stubEnv('XDG_CURRENT_DESKTOP', 'plasma'); + vi.stubEnv('KDE_SESSION_VERSION', '5'); + + ensureSecureLinuxPasswordStore(); + + expect(commandLine.appendSwitch).toHaveBeenCalledWith( + 'password-store', + 'kwallet5' + ); + }); + + test('sets kwallet for an unknown desktop when KDE session variables are present', () => { + platformSpy = vi.spyOn(process, 'platform', 'get').mockReturnValue('linux'); + vi.stubEnv('XDG_CURRENT_DESKTOP', 'custom'); + vi.stubEnv('KDE_FULL_SESSION', 'true'); + vi.stubEnv('KDE_SESSION_VERSION', '5'); + + ensureSecureLinuxPasswordStore(); + + expect(commandLine.appendSwitch).toHaveBeenCalledWith( + 'password-store', + 'kwallet5' + ); + }); + + test('sets kwallet for i3 when KDE session variables are present', () => { + platformSpy = vi.spyOn(process, 'platform', 'get').mockReturnValue('linux'); + vi.stubEnv('XDG_CURRENT_DESKTOP', 'i3'); + vi.stubEnv('KDE_SESSION_VERSION', '6'); + + ensureSecureLinuxPasswordStore(); + + expect(commandLine.appendSwitch).toHaveBeenCalledWith( + 'password-store', + 'kwallet6' + ); + }); + + test('combines differing XDG_CURRENT_DESKTOP and XDG_SESSION_DESKTOP values, keeping KWallet precedence', () => { + platformSpy = vi.spyOn(process, 'platform', 'get').mockReturnValue('linux'); + vi.stubEnv('XDG_CURRENT_DESKTOP', 'gnome'); + vi.stubEnv('XDG_SESSION_DESKTOP', 'kde'); + vi.stubEnv('KDE_SESSION_VERSION', '6'); + + ensureSecureLinuxPasswordStore(); + + expect(commandLine.appendSwitch).toHaveBeenCalledWith( + 'password-store', + 'kwallet6' + ); + }); + + test('ignores an empty XDG_CURRENT_DESKTOP and still selects KWallet via XDG_SESSION_DESKTOP', () => { + platformSpy = vi.spyOn(process, 'platform', 'get').mockReturnValue('linux'); + vi.stubEnv('XDG_CURRENT_DESKTOP', ''); + vi.stubEnv('XDG_SESSION_DESKTOP', 'KDE'); + vi.stubEnv('KDE_SESSION_VERSION', '6'); + + ensureSecureLinuxPasswordStore(); + + expect(commandLine.appendSwitch).toHaveBeenCalledWith( + 'password-store', + 'kwallet6' + ); + }); + + test('is case-insensitive', () => { + platformSpy = vi.spyOn(process, 'platform', 'get').mockReturnValue('linux'); + vi.stubEnv('XDG_CURRENT_DESKTOP', 'SWAY:Wlroots'); + + ensureSecureLinuxPasswordStore(); + + expect(commandLine.appendSwitch).toHaveBeenCalledWith( + 'password-store', + 'gnome-libsecret' + ); + }); + + test('falls back to XDG_SESSION_DESKTOP when XDG_CURRENT_DESKTOP is not set', () => { + platformSpy = vi.spyOn(process, 'platform', 'get').mockReturnValue('linux'); + vi.stubEnv('XDG_SESSION_DESKTOP', 'sway'); + + ensureSecureLinuxPasswordStore(); + + expect(commandLine.appendSwitch).toHaveBeenCalledWith( + 'password-store', + 'gnome-libsecret' + ); + }); + + test('prioritizes KWallet over gnome-libsecret in colon-separated KDE desktops', () => { + platformSpy = vi.spyOn(process, 'platform', 'get').mockReturnValue('linux'); + vi.stubEnv('XDG_CURRENT_DESKTOP', 'kde:gnome'); + vi.stubEnv('KDE_SESSION_VERSION', '6'); + + ensureSecureLinuxPasswordStore(); + + expect(commandLine.appendSwitch).toHaveBeenCalledWith( + 'password-store', + 'kwallet6' + ); + }); +});