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>
This commit is contained in:
1 parent
94e7b91797
commit
0b30a9180a
7 files changed
+125
-65
No files matched your search
+59
-31
@@ -20,12 +20,12 @@ class SecretStoreError extends Error {
|
||||
}
|
||||
|
||||
let _safeStorageCache = undefined;
|
||||
function getSafeStorage() {
|
||||
async function getSafeStorage() {
|
||||
if (_safeStorageCache !== undefined) return _safeStorageCache;
|
||||
try {
|
||||
const { safeStorage } = require('electron');
|
||||
if (safeStorage && typeof safeStorage.isEncryptionAvailable === 'function'
|
||||
&& safeStorage.isEncryptionAvailable()) {
|
||||
if (safeStorage && typeof safeStorage.isAsyncEncryptionAvailable === 'function'
|
||||
&& await safeStorage.isAsyncEncryptionAvailable()) {
|
||||
_safeStorageCache = safeStorage;
|
||||
return _safeStorageCache;
|
||||
}
|
||||
@@ -33,6 +33,14 @@ function getSafeStorage() {
|
||||
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);
|
||||
@@ -41,68 +49,88 @@ function isEncrypted(value) {
|
||||
return decoded.length > 0 && decoded.toString('base64') === encoded;
|
||||
}
|
||||
|
||||
function encryptField(value) {
|
||||
async function encryptField(value) {
|
||||
if (!value || typeof value !== 'string') return value;
|
||||
if (isEncrypted(value)) return value;
|
||||
const ss = getSafeStorage();
|
||||
if (!ss) {
|
||||
throw new SecretStoreError('SECRET_STORE_UNAVAILABLE', 'Sicherer Zugangsdaten-Speicher ist nicht verfügbar');
|
||||
}
|
||||
const ss = await getSafeStorage();
|
||||
if (!ss) throw unavailableError();
|
||||
try {
|
||||
const buf = ss.encryptString(value);
|
||||
return SENTINEL + buf.toString('base64');
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
function decryptField(value) {
|
||||
if (!value || typeof value !== 'string') return value;
|
||||
async function decryptFieldWithStatus(value) {
|
||||
if (!value || typeof value !== 'string') return { value, shouldReEncrypt: false };
|
||||
if (!isEncrypted(value)) {
|
||||
if (value.startsWith(SENTINEL)) {
|
||||
throw new SecretStoreError('SECRET_STORE_DECRYPT_FAILED', 'Gespeicherte Zugangsdaten konnten nicht entschlüsselt werden');
|
||||
}
|
||||
return value;
|
||||
}
|
||||
const ss = getSafeStorage();
|
||||
if (!ss) {
|
||||
throw new SecretStoreError('SECRET_STORE_UNAVAILABLE', 'Sicherer Zugangsdaten-Speicher ist nicht verfügbar');
|
||||
if (value.startsWith(SENTINEL)) throw decryptFailedError();
|
||||
return { value, shouldReEncrypt: false };
|
||||
}
|
||||
const ss = await getSafeStorage();
|
||||
if (!ss) throw unavailableError();
|
||||
let decrypted;
|
||||
try {
|
||||
const buf = Buffer.from(value.slice(SENTINEL.length), 'base64');
|
||||
return ss.decryptString(buf);
|
||||
decrypted = await ss.decryptStringAsync(Buffer.from(value.slice(SENTINEL.length), 'base64'));
|
||||
} catch (cause) {
|
||||
throw new SecretStoreError('SECRET_STORE_DECRYPT_FAILED', 'Gespeicherte Zugangsdaten konnten nicht entschlüsselt werden', cause);
|
||||
throw decryptFailedError(cause);
|
||||
}
|
||||
if (!decrypted || typeof decrypted.result !== 'string') throw decryptFailedError();
|
||||
return { value: decrypted.result, shouldReEncrypt: decrypted.shouldReEncrypt === true };
|
||||
}
|
||||
|
||||
function mapHosterAccounts(config, fn) {
|
||||
if (config?.globalSettings?.cloudLogSync?.apiKey) {
|
||||
config.globalSettings.cloudLogSync.apiKey = fn(config.globalSettings.cloudLogSync.apiKey);
|
||||
}
|
||||
if (!config || !config.hosters || typeof config.hosters !== 'object') return config;
|
||||
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]) acc[f] = fn(acc[f]);
|
||||
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 encryptCredentials(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);
|
||||
}
|
||||
function decryptCredentials(config) { return mapHosterAccounts(config, decryptField); }
|
||||
|
||||
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
|
||||
};
|
||||
Reference in new issue
Block a user