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. Omitting a real desktop fails
13// safe: we force gnome-libsecret, which is the backend Chromium selects for all of these anyway.
14const ELECTRON_LIBSECRET_DESKTOPS = new Set([
15 "Deepin",
16 "GNOME",
17 "Pantheon",
18 "UKUI",
19 "Unity",
20 "X-Cinnamon",
21 "XFCE",
22]);
23// Chromium selects a KWallet generation for KDE from KDE_SESSION_VERSION, so it needs no help.
24const ELECTRON_KDE_DESKTOP = "KDE";
25// Chromium recognizes LXQt and still selects basic text for it, so it does need a forced backend.
26const ELECTRON_UNPROTECTED_DESKTOPS = new Set(["LXQt"]);
27
28const KDE_NAME_PREFIXES = ["kde", "plasma"];
29const NEGATIVE_FLAG_VALUES = new Set(["0", "false", "no", "off"]);
30
31export function normalizeLinuxPasswordStorePreference(
32 value: unknown,
33): LinuxPasswordStorePreference {
34 return value === "gnome-libsecret" ||
35 value === "kwallet" ||
36 value === "kwallet5" ||
37 value === "kwallet6"
38 ? value
39 : DEFAULT_LINUX_PASSWORD_STORE;
40}
41
42// Auto mode asks one question: will Electron select a real keyring on its own? If so, stay out of
43// the way, which is how canonical KDE sessions keep the KWallet generation Chromium picks for them.
44// Otherwise force gnome-libsecret, because the alternative is basic text, which is barely
45// encryption at all. Forcing never guesses a KWallet generation; a KDE session that needs a
46// specific one sets linuxPasswordStore explicitly.
47export function resolveLinuxPasswordStoreSwitch(input: {
48 readonly preference: LinuxPasswordStorePreference;
49 readonly env: NodeJS.ProcessEnv;
50}): LinuxPasswordStoreSwitch | null {
51 if (input.preference !== "auto") {
52 return input.preference;
53 }
54
55 return electronSelectsProtectedBackend(input.env) ? null : "gnome-libsecret";
56}
57
58// Only an exact XDG_CURRENT_DESKTOP literal proves Electron will protect the session. Chromium can
59// also reach a real backend through DESKTOP_SESSION and the legacy KDE markers, but those are the
60// variables a previous session leaves behind, and trusting them is what let stale hints suppress
61// the forced backend before. Forcing where Chromium would have chosen libsecret is harmless, since
62// it lands on the same backend.
63function electronSelectsProtectedBackend(env: NodeJS.ProcessEnv): boolean {
64 for (const name of splitDesktopNameList(env.XDG_CURRENT_DESKTOP)) {
65 const trimmed = name.trim();
66 if (trimmed.length === 0) {
67 continue;
68 }
69 if (trimmed === ELECTRON_KDE_DESKTOP || ELECTRON_LIBSECRET_DESKTOPS.has(trimmed)) {
70 return true;
71 }
72 if (ELECTRON_UNPROTECTED_DESKTOPS.has(trimmed)) {
73 return false;
74 }
75 }
76
77 return false;
78}
79
80export function resolveLinuxSecretStorageUnavailableMessage(input: {
81 readonly configuredPreference: LinuxPasswordStorePreference;
82 readonly selectedBackend: string | null;
83 readonly env: NodeJS.ProcessEnv;
84}): string {
85 if (input.configuredPreference === "gnome-libsecret") {
86 return getGnomeKeyringRemediationMessage();
87 }
88
89 if (
90 input.configuredPreference === "kwallet" ||
91 input.configuredPreference === "kwallet5" ||
92 input.configuredPreference === "kwallet6"
93 ) {
94 return getKWalletRemediationMessage();
95 }
96
97 const backend = normalizeSelectedStorageBackend(input.selectedBackend);
98 if (backend === "gnome-libsecret") {
99 return getGnomeKeyringRemediationMessage();
100 }
101
102 if (
103 backend === "kwallet" ||
104 backend === "kwallet5" ||
105 backend === "kwallet6" ||
106 looksLikeKdeSession(input.env)
107 ) {
108 return getKWalletRemediationMessage();
109 }
110
111 return getGnomeKeyringRemediationMessage();
112}
113
114function getGnomeKeyringRemediationMessage(): string {
115 return "T3 Code could not access GNOME Keyring to save this environment credential. Install and start GNOME Keyring, then restart T3 Code.";
116}
117
118function getKWalletRemediationMessage(): string {
119 return "T3 Code could not access KWallet to save this environment credential. Enable the KDE wallet subsystem in System Settings, then restart T3 Code.";
120}
121
122// Advisory only: this picks between the GNOME Keyring and KWallet wording in the failure notice. It
123// never decides which backend to select, so a loose match costs a user slightly wrong instructions
124// rather than an unprotected credential store.
125function looksLikeKdeSession(env: NodeJS.ProcessEnv): boolean {
126 const currentDesktopNames = nonEmptyDesktopNames(env.XDG_CURRENT_DESKTOP);
127 if (currentDesktopNames.length > 0) {
128 return currentDesktopNames.some(isKdeDesktopName);
129 }
130
131 const legacyNames = legacyDesktopNames(env);
132 if (legacyNames.length > 0) {
133 return legacyNames.some(isKdeDesktopName);
134 }
135
136 return isSet(env.KDE_SESSION_VERSION) || isAffirmativeFlag(env.KDE_FULL_SESSION);
137}
138
139function isKdeDesktopName(name: string): boolean {
140 return KDE_NAME_PREFIXES.some((prefix) => name.startsWith(prefix));
141}
142
143function legacyDesktopNames(env: NodeJS.ProcessEnv): string[] {
144 return [env.XDG_SESSION_DESKTOP, env.DESKTOP_SESSION, env.GDMSESSION].flatMap((entry) => {
145 const normalized = normalizeDesktopName(entry);
146 return normalized ? [normalized] : [];
147 });
148}
149
150function nonEmptyDesktopNames(value: string | undefined): string[] {
151 return splitDesktopNameList(value).flatMap((entry) => {
152 const normalized = normalizeDesktopName(entry);
153 return normalized ? [normalized] : [];
154 });
155}
156
157function isSet(value: string | undefined): boolean {
158 return Boolean(value?.trim());
159}
160
161function isAffirmativeFlag(value: string | undefined): boolean {
162 const normalized = value?.trim().toLowerCase();
163 return normalized ? !NEGATIVE_FLAG_VALUES.has(normalized) : false;
164}
165
166function splitDesktopNameList(value: string | undefined): string[] {
167 return value?.split(":") ?? [];
168}
169
170function normalizeDesktopName(value: string | undefined): string | null {
171 const normalized = value?.trim().toLowerCase();
172 return normalized && normalized.length > 0 ? normalized : null;
173}
174
175function normalizeSelectedStorageBackend(value: string | null): string | null {
176 const normalized = value?.trim().toLowerCase().replace(/_/gu, "-");
177 return normalized && normalized.length > 0 ? normalized : null;
178}