Open History to see the last commit
1export type LinuxPasswordStorePreference =
2 | "auto"
3 | "gnome-libsecret"
4 | "kwallet"
5 | "kwallet5"
6 | "kwallet6";
7export type LinuxPasswordStoreSwitch = Exclude<LinuxPasswordStorePreference, "auto">;
8
9export const DEFAULT_LINUX_PASSWORD_STORE: LinuxPasswordStorePreference = "auto";
10
11// Chromium matches XDG_CURRENT_DESKTOP values case-sensitively and returns on the first value it
12// recognizes, so these stay exact literals and are scanned in order.
13const ELECTRON_LIBSECRET_DESKTOPS = new Set([
14 "Deepin",
15 "GNOME",
16 "Pantheon",
17 "UKUI",
18 "Unity",
19 "X-Cinnamon",
20 "XFCE",
21]);
22// Chromium selects a KWallet generation for KDE from KDE_SESSION_VERSION, so it needs no help.
23const ELECTRON_KDE_DESKTOP = "KDE";
24// Chromium recognizes LXQt and still selects basic text for it, so it does need a forced backend.
25const ELECTRON_UNPROTECTED_DESKTOPS = new Set(["LXQt"]);
26
27export function normalizeLinuxPasswordStorePreference(
28 value: unknown,
29): LinuxPasswordStorePreference {
30 return value === "gnome-libsecret" ||
31 value === "kwallet" ||
32 value === "kwallet5" ||
33 value === "kwallet6"
34 ? value
35 : DEFAULT_LINUX_PASSWORD_STORE;
36}
37
38// Otherwise force gnome-libsecret, because the alternative is basic text, which is barely
39// encryption at all.
40export function resolveLinuxPasswordStoreSwitch(input: {
41 readonly preference: LinuxPasswordStorePreference;
42 readonly env: NodeJS.ProcessEnv;
43}): LinuxPasswordStoreSwitch | null {
44 if (input.preference !== "auto") {
45 return input.preference;
46 }
47
48 return electronSelectsProtectedBackend(input.env) ? null : "gnome-libsecret";
49}
50
51// Only an exact XDG_CURRENT_DESKTOP literal proves Electron will protect the session. Chromium can
52// also reach a real backend through DESKTOP_SESSION and the legacy KDE markers, but those are the
53// variables a previous session leaves behind, and trusting them is what let stale hints suppress
54// the forced backend before.
55
56function electronSelectsProtectedBackend(env: NodeJS.ProcessEnv): boolean {
57 for (const name of splitDesktopNameList(env.XDG_CURRENT_DESKTOP)) {
58 const trimmed = name.trim();
59 if (trimmed.length === 0) {
60 continue;
61 }
62 if (trimmed === ELECTRON_KDE_DESKTOP || ELECTRON_LIBSECRET_DESKTOPS.has(trimmed)) {
63 return true;
64 }
65 if (ELECTRON_UNPROTECTED_DESKTOPS.has(trimmed)) {
66 return false;
67 }
68 }
69
70 return false;
71}
72
73function splitDesktopNameList(value: string | undefined): string[] {
74 return value?.split(":") ?? [];
75}