Files
Multi-Hoster-Upload/lib/secret-store.js
SucukdeluxeandClaude Opus 5.5 0b30a9180a
CI / verify (push) Canceled after 0s
Migrate credential encryption to the async safeStorage API
Electron 45 deprecates the synchronous safeStorage methods and Electron 46
removes them. Credential encryption now uses encryptStringAsync,
decryptStringAsync and isAsyncEncryptionAvailable.

- secret-store: async encryptField/decryptField/encryptCredentials/
  decryptCredentials, decryptFieldWithStatus exposes shouldReEncrypt.
  Unavailable or failing encryption still rejects with the existing
  SECRET_STORE_* codes; legacy plaintext stays readable.
- config-store: the first load runs asynchronously through init();
  writes encrypt before the atomic write. A synchronous load() of an
  encrypted config before init() fails with SECRET_STORE_NOT_READY and
  schedules a refresh. Values flagged with shouldReEncrypt are
  re-encrypted once after loading.
- main: await configStore.init() after app ready, before the first load.
- MCP access store and online backup keyring await the async helpers;
  MCP saves keep their call order.

Existing enc:v1 values written with encryptString stay readable: on
Windows both APIs use the same v10 format and Local State key, verified
in both directions with real Electron 41 and 45.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 07:57:54 +02:00

137 lines
4.6 KiB
JavaScript

// Wraps Electron's safeStorage (OS-level credential encryption: DPAPI on
// Windows, Keychain on macOS, libsecret on Linux) to keep hoster passwords and
// API keys out of the plaintext electron-config.json.
//
// On Windows the DPAPI key is tied to the current user profile, so credentials
// encrypted here are only readable by the same Windows user. For backups we
// export to plaintext (the .mhu envelope has its own AES-GCM layer) so moving
// between machines/users works transparently.
const SENTINEL = 'enc:v1:';
const CRED_FIELDS = ['password', 'apiKey'];
class SecretStoreError extends Error {
constructor(code, message, cause) {
super(message);
this.name = 'SecretStoreError';
this.code = code;
if (cause !== undefined) this.cause = cause;
}
}
let _safeStorageCache = undefined;
async function getSafeStorage() {
if (_safeStorageCache !== undefined) return _safeStorageCache;
try {
const { safeStorage } = require('electron');
if (safeStorage && typeof safeStorage.isAsyncEncryptionAvailable === 'function'
&& await safeStorage.isAsyncEncryptionAvailable()) {
_safeStorageCache = safeStorage;
return _safeStorageCache;
}
} catch {}
return null;
}
function unavailableError() {
return new SecretStoreError('SECRET_STORE_UNAVAILABLE', 'Sicherer Zugangsdaten-Speicher ist nicht verfügbar');
}
function decryptFailedError(cause) {
return new SecretStoreError('SECRET_STORE_DECRYPT_FAILED', 'Gespeicherte Zugangsdaten konnten nicht entschlüsselt werden', cause);
}
function isEncrypted(value) {
if (typeof value !== 'string' || !value.startsWith(SENTINEL)) return false;
const encoded = value.slice(SENTINEL.length);
if (!encoded || encoded.length % 4 !== 0) return false;
const decoded = Buffer.from(encoded, 'base64');
return decoded.length > 0 && decoded.toString('base64') === encoded;
}
async function encryptField(value) {
if (!value || typeof value !== 'string') return value;
if (isEncrypted(value)) return value;
const ss = await getSafeStorage();
if (!ss) throw unavailableError();
try {
const buf = await ss.encryptStringAsync(value);
return SENTINEL + Buffer.from(buf).toString('base64');
} catch (cause) {
throw new SecretStoreError('SECRET_STORE_ENCRYPT_FAILED', 'Zugangsdaten konnten nicht sicher verschlüsselt werden', cause);
}
}
async function decryptFieldWithStatus(value) {
if (!value || typeof value !== 'string') return { value, shouldReEncrypt: false };
if (!isEncrypted(value)) {
if (value.startsWith(SENTINEL)) throw decryptFailedError();
return { value, shouldReEncrypt: false };
}
const ss = await getSafeStorage();
if (!ss) throw unavailableError();
let decrypted;
try {
decrypted = await ss.decryptStringAsync(Buffer.from(value.slice(SENTINEL.length), 'base64'));
} catch (cause) {
throw decryptFailedError(cause);
}
if (!decrypted || typeof decrypted.result !== 'string') throw decryptFailedError();
return { value: decrypted.result, shouldReEncrypt: decrypted.shouldReEncrypt === true };
}
async function decryptField(value) {
return (await decryptFieldWithStatus(value)).value;
}
function credentialSlots(config) {
const slots = [];
const cloudLogSync = config?.globalSettings?.cloudLogSync;
if (cloudLogSync?.apiKey) slots.push([cloudLogSync, 'apiKey']);
if (!config || !config.hosters || typeof config.hosters !== 'object') return slots;
for (const accounts of Object.values(config.hosters)) {
if (!Array.isArray(accounts)) continue;
for (const acc of accounts) {
if (!acc || typeof acc !== 'object') continue;
for (const f of CRED_FIELDS) {
if (acc[f]) slots.push([acc, f]);
}
}
}
return slots;
}
async function mapHosterAccounts(config, fn) {
for (const [owner, field] of credentialSlots(config)) owner[field] = await fn(owner[field]);
return config;
}
function hasEncryptedCredentials(config) {
return credentialSlots(config).some(([owner, field]) => typeof owner[field] === 'string' && owner[field].startsWith(SENTINEL));
}
async function encryptCredentials(config) {
return mapHosterAccounts(config, encryptField);
}
async function decryptCredentials(config) {
let shouldReEncrypt = false;
await mapHosterAccounts(config, async value => {
const decrypted = await decryptFieldWithStatus(value);
if (decrypted.shouldReEncrypt) shouldReEncrypt = true;
return decrypted.value;
});
return { config, shouldReEncrypt };
}
module.exports = {
SecretStoreError,
encryptField,
decryptField,
decryptFieldWithStatus,
encryptCredentials,
decryptCredentials,
hasEncryptedCredentials,
isEncrypted
};