Files
Multi-Hoster-Upload/lib/mcp-store.js
T
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

95 lines
5.6 KiB
JavaScript

const fs = require('node:fs/promises');
const path = require('node:path');
const { randomUUID, randomBytes } = require('node:crypto');
const { McpAccessError } = require('./mcp-access');
const RELAY_URL = 'https://downloader.24-music.de/mcp-relay';
const CAPABILITIES = ['read', 'control', 'files', 'secrets', 'updates', 'full_access'];
class McpStore {
constructor({ directory, encrypt, decrypt }) {
this.file = path.join(directory, 'mcp-access.json');
this.disabledMarker = path.join(directory, 'mcp-disabled.marker');
this.encrypt = encrypt;
this.decrypt = decrypt;
this.value = { version: 1, enabled: false, name: 'Multi-Hoster-Uploader', capabilities: ['read'], grants: [], identity: null, revocations: [] };
this.tail = Promise.resolve();
}
async load() {
const disabled = await fs.stat(this.disabledMarker).then(() => true, error => { if (error.code === 'ENOENT') return false; throw new McpAccessError('ACCESS_STORE_UNAVAILABLE'); });
try {
const text = await fs.readFile(this.file, 'utf8');
if (text.length > 100000) throw new Error('size');
const value = JSON.parse(text);
if (value.version !== 1 || !Array.isArray(value.capabilities) || !Array.isArray(value.grants) || !Array.isArray(value.revocations)) throw new Error('format');
if (typeof value.enabled !== 'boolean' || typeof value.name !== 'string' || !value.name.trim() || value.name.length > 100
|| value.capabilities.some(item => !CAPABILITIES.includes(item)) || value.grants.length > 30 || value.revocations.length > 32
|| value.grants.some(grant => !grant || typeof grant.id !== 'string' || typeof grant.path !== 'string' || !path.isAbsolute(grant.path)
|| typeof grant.label !== 'string' || typeof grant.readOnly !== 'boolean'
|| (grant.allowTransfer !== undefined && typeof grant.allowTransfer !== 'boolean'))) throw new Error('format');
const decode = async identity => {
if (!identity) return null;
if (identity.relayUrl !== RELAY_URL || !/^[a-f0-9-]{36}$/.test(identity.installationId)) throw new Error('identity');
if (!identity.agentToken?.startsWith('enc:v1:') || !identity.clientToken?.startsWith('enc:v1:')) throw new Error('encryption');
const decoded = { ...identity, agentToken: await this.decrypt(identity.agentToken), clientToken: await this.decrypt(identity.clientToken) };
if (![decoded.agentToken, decoded.clientToken].every(token => /^[A-Za-z0-9_-]{43}$/.test(token)) || decoded.agentToken === decoded.clientToken) throw new Error('credentials');
return decoded;
};
const identity = await decode(value.identity);
const revocations = [];
for (const revoked of value.revocations) revocations.push(await decode(revoked));
this.value = { ...value, capabilities: value.capabilities.filter(item => CAPABILITIES.includes(item)), identity, revocations };
if (disabled) {
this.value.enabled = false;
if (this.value.identity) {
if (!this.value.revocations.some(identity => identity.installationId === this.value.identity.installationId)) this.value.revocations.push(this.value.identity);
this.value.identity = null;
}
}
} catch (error) {
if (error.code !== 'ENOENT') throw new McpAccessError('ACCESS_STORE_UNAVAILABLE');
}
return this.value;
}
async markDisabled() {
await fs.mkdir(path.dirname(this.file), { recursive: true });
const handle = await fs.open(this.disabledMarker, 'w', 0o600);
try { await handle.writeFile('disabled\n'); await handle.sync(); }
finally { await handle.close(); }
}
async clearDisabled() { await fs.unlink(this.disabledMarker).catch(error => { if (error.code !== 'ENOENT') throw error; }); }
async _encode(next) {
const encode = async identity => identity ? { ...identity, agentToken: await this.encrypt(identity.agentToken), clientToken: await this.encrypt(identity.clientToken) } : null;
const revocations = [];
for (const revoked of next.revocations) revocations.push(await encode(revoked));
const disk = { ...next, identity: await encode(next.identity), revocations };
for (const identity of [disk.identity, ...disk.revocations].filter(Boolean)) {
if (!identity.agentToken.startsWith('enc:v1:') || !identity.clientToken.startsWith('enc:v1:')) throw new McpAccessError('ENCRYPTION_UNAVAILABLE');
}
return disk;
}
save(next) {
const encoded = this._encode(next);
encoded.catch(() => {});
const task = this.tail.then(async () => {
const disk = await encoded;
await fs.mkdir(path.dirname(this.file), { recursive: true });
const temporary = this.file + '.' + randomUUID() + '.tmp';
try { await fs.writeFile(temporary, JSON.stringify(disk), { mode: 0o600, flag: 'wx' }); await fs.rename(temporary, this.file); }
finally { await fs.unlink(temporary).catch(() => {}); }
this.value = next;
});
this.tail = task.catch(() => {});
return task;
}
newIdentity() { return { relayUrl: RELAY_URL, installationId: randomUUID(), agentToken: randomBytes(32).toString('base64url'), clientToken: randomBytes(32).toString('base64url') }; }
code() {
const value = this.value;
if (!value.enabled || !value.identity) throw new McpAccessError('NOT_ENABLED');
const identity = value.identity;
return 'media-mcp:v1:' + Buffer.from(JSON.stringify({ v: 1, url: `${identity.relayUrl}/mcp/${identity.installationId}`, installationId: identity.installationId, kind: 'uploader', name: value.name, token: identity.clientToken })).toString('base64url');
}
}
module.exports = { McpStore, RELAY_URL, CAPABILITIES };