Add guarded MCP file transfer and complete diagnostic access
CI / verify (push) Canceled after 0s

This commit is contained in:
Sucukdeluxe committed 2026-10-15 17:31:34 +02:00
1 parent d5022d2d59
commit cd2394dd22
19 files changed
+1757 -32

No files matched your search

+18 -8
View File
@@ -1,4 +1,5 @@
const { McpAccessError, validateInput, actionDigest } = require('./mcp-access');
const { sanitizeTransferResult } = require('./mcp-transfers');
const text = (maxLength = 4096) => ({ type: 'string', maxLength });
const id = { type: 'string', minLength: 1, maxLength: 128, pattern: '^[A-Za-z0-9_.:-]+$' };
@@ -18,6 +19,7 @@ const definitions = [
['app_info', 'Read application version and runtime information.', 'read', {}, []],
['app_state', 'Read live transfer and automation summary.', 'read', {}, []],
['queue_list', 'Read a bounded page of upload jobs.', 'read', { ...page, status: text(40) }, []],
['queue_job_log', 'Read a bounded, secret-filtered log for one upload job.', 'read', { jobId: id, ...page }, ['jobId']],
['queue_add', 'Add files from a granted directory to the upload queue.', 'control', { rootId: id, paths: array(text(), 1000), hosters: array(hoster, 5) }, ['rootId', 'paths', 'hosters']],
['queue_start', 'Start eligible queued uploads.', 'control', jobIds, []],
['queue_pause', 'Pause new uploads after current transfers.', 'control', {}, []],
@@ -27,6 +29,7 @@ const definitions = [
['queue_remove', 'Remove selected jobs without deleting source files.', 'control', jobIds, ['jobIds'], true],
['queue_clear', 'Clear inactive jobs without deleting source files.', 'control', {}, [], true],
['accounts_list', 'Read a bounded page of configured accounts with secrets hidden.', 'read', { ...page, hoster }, []],
['accounts_runtime', 'Read temporary account cooldowns and manual-action states.', 'read', { ...page, hoster }, []],
['accounts_check', 'Check selected account credentials without revealing them.', 'accounts', { accountIds: array(id, 100) }, ['accountIds']],
['accounts_add', 'Add a hoster account with explicit credentials.', 'accounts', { hoster, label: text(200), username: text(320), password: text(), apiKey: text(), authType: enumeration(['login', 'api']) }, ['hoster', 'authType']],
['accounts_update', 'Update one account without exposing stored credentials.', 'accounts', { ...account, label: text(200), username: text(320), password: text(), apiKey: text(), enabled: boolean }, ['accountId']],
@@ -69,10 +72,19 @@ const definitions = [
name: enumeration(['debug', 'fileuploader', 'uploadAudit', 'accountRotation', 'crash']),
tailKb: integer(1, 256), backup: integer(0, 2), grep: text(200)
}, ['name']],
['diagnostics_files', 'List startup journals, native logs and saved startup failure reports.', 'diagnostics', page, []],
['diagnostics_read', 'Read a page of a fully secret-filtered startup diagnostic artifact. Offsets and limits count redacted characters.', 'diagnostics',
{ artifactId: id, offset: integer(0, 100000000), limit: integer(1, 65536) }, ['artifactId']],
['files_roots', 'List configured directory grants, without granting new access.', 'files', {}, []],
['files_list', 'List a bounded page within a granted directory.', 'files', { ...file, ...page }, ['rootId']],
['files_stat', 'Read metadata for a file within a granted directory.', 'files', file, ['rootId', 'relativePath']],
['files_hash', 'Compute a bounded file checksum within a granted directory.', 'files', { ...file, algorithm: enumeration(['sha256', 'sha512']) }, ['rootId', 'relativePath']],
['files_transfer_open', 'Prepare a stable read-only file transfer from a directory with explicit content-transfer permission. Use a receiver to stream chunks outside chat.', 'files', file, ['rootId', 'relativePath']],
['files_transfer_get', 'Read preparation state, byte size and SHA-256 for an owned file transfer.', 'files', { transferId: id }, ['transferId']],
['files_transfer_list', 'List this authenticated client\'s active file transfers.', 'files', {}, []],
['files_transfer_read', 'Read one verified binary chunk of an owned prepared transfer. Intended for a streaming receiver, not rendering in chat.', 'files',
{ transferId: id, offset: integer(0, 32 * 1024 ** 3), length: integer(1, 1024 * 1024) }, ['transferId', 'offset']],
['files_transfer_close', 'Close an owned temporary transfer handle without modifying its source file.', 'files', { transferId: id }, ['transferId']],
['files_delete', 'Delete a specifically selected file inside a writable grant.', 'files.write', file, ['rootId', 'relativePath'], true],
['cloud_status', 'Read CloudDrop log synchronization state.', 'read', {}, []],
['cloud_configure', 'Enable or disable CloudDrop log sync with the saved or supplied key.', 'settings', { enabled: boolean, apiKey: text(200) }, ['enabled']],
@@ -91,9 +103,9 @@ const definitions = [
const capabilities = { diagnostics: 'read', accounts: 'control', settings: 'control', backup: 'control', lifecycle: 'control', 'secrets.read': 'secrets', 'files.write': 'files' };
const MCP_TOOLS = Object.freeze(definitions.map(([name, description, scope, properties, required, destructive = false]) => Object.freeze({
name: 'mhu_' + name, description, scope: capabilities[scope] || scope, destructive, inputSchema: object(properties, required),
requiredScopes: [...new Set([capabilities[scope] || scope, ...(['queue_add', 'automation_update'].includes(name) ? ['files'] : []), ...(name === 'files_delete' ? ['control'] : [])])],
annotations: { readOnlyHint: ['read', 'diagnostics', 'files', 'secrets.read'].includes(scope), destructiveHint: destructive,
idempotentHint: ['read', 'diagnostics', 'files', 'secrets.read'].includes(scope), openWorldHint: /^(?:queue_(?:start|retry|resume)|accounts_(?:check|otp|request_otp)|automation_(?:start|reconcile)|cloud_|backup_|update_)/.test(name) }
requiredScopes: [...new Set([capabilities[scope] || scope, ...(['queue_add', 'automation_update'].includes(name) ? ['files'] : []), ...(name === 'files_delete' ? ['control'] : []), ...(name.startsWith('files_transfer_') ? ['read'] : [])])],
annotations: { readOnlyHint: ['read', 'diagnostics', 'files', 'secrets.read'].includes(scope) && !['files_transfer_open', 'files_transfer_close'].includes(name), destructiveHint: destructive,
idempotentHint: ['read', 'diagnostics', 'files', 'secrets.read'].includes(scope) && name !== 'files_transfer_open', openWorldHint: /^(?:queue_(?:start|retry|resume)|accounts_(?:check|otp|request_otp)|automation_(?:start|reconcile)|cloud_|backup_|update_)/.test(name) }
})));
function createMcpToolAdapter({ handlers, authorizeConfirmation, sanitize, audit = () => {}, getCapabilities, requiresConfirmation = () => false }) {
@@ -105,9 +117,7 @@ function createMcpToolAdapter({ handlers, authorizeConfirmation, sanitize, audit
const tool = byName.get(name);
if (!tool || typeof handlers[name] !== 'function') throw new McpAccessError('TOOL_UNAVAILABLE');
const allowed = () => getCapabilities ? getCapabilities() : context?.scopes;
const permitted = () => Array.isArray(allowed()) && allowed().includes(tool.scope)
&& (name !== 'mhu_files_delete' || allowed().includes('control'))
&& (!['mhu_queue_add', 'mhu_automation_update'].includes(name) || allowed().includes('files'));
const permitted = () => Array.isArray(allowed()) && tool.requiredScopes.every(scope => allowed().includes(scope));
if (!permitted()) throw new McpAccessError('SCOPE_DENIED');
validateInput(args, tool.inputSchema);
if (context.signal?.aborted || !Number.isFinite(context.deadline) || context.deadline <= Date.now()) throw new McpAccessError('REQUEST_EXPIRED');
@@ -133,8 +143,8 @@ function createMcpToolAdapter({ handlers, authorizeConfirmation, sanitize, audit
const value = await handlers[name](args, context);
if (context.signal?.aborted || Date.now() >= context.deadline) throw new McpAccessError('REQUEST_EXPIRED');
if (!permitted()) throw new McpAccessError('SCOPE_DENIED');
const result = name === 'mhu_secrets_read' ? value : await sanitize(value);
if (Buffer.byteLength(JSON.stringify(result) || 'null') > 1024 * 1024) throw new McpAccessError('RESULT_TOO_LARGE');
const result = name.startsWith('mhu_files_transfer_') ? sanitizeTransferResult(name, value, sanitize) : name === 'mhu_secrets_read' ? value : await sanitize(value);
if (Buffer.byteLength(JSON.stringify(result) || 'null') > (name === 'mhu_files_transfer_read' ? 2 : 1) * 1024 * 1024) throw new McpAccessError('RESULT_TOO_LARGE');
audit({ tool: name, event: 'completed', requestId: context.requestId, durationMs: Date.now() - startedAt });
return result;
};