Multi-Hoster-Upload/gateway/test/e2e-verify.mjs
Administrator d69e5c39bf feat(diagnostics): MCP gateway + harden redaction so no secret ever leaves the box
Adds the connect-by-code side of remote diagnostics and closes two real
secret-leak vectors that an end-to-end gateway<->agent test surfaced.

Gateway (gateway/, local stdio MCP, Claude connects once):
- 14 read-only tools (server_health hub, read_log, list_logs, list_errors,
  get_queue_state, get_history, get_config_redacted, get_system_info,
  get_rotation_state, get_app_events + connect/disconnect/list/current).
- The HOST is always supplied by the operator, never taken from the code.
- TLS fingerprint pinning is enforced in the socket 'open' handler BEFORE the
  token is sent (wss opt-in); plain ws is loopback-only.
- registry.json (holds bearer tokens) is gitignored; only an empty example ships.

Security hardening (gates every off-box payload):
- redactLogText now scrubs opaque bearer/token-family secrets that are NOT
  stored config credentials (e.g. a session token a hoster returns inside an
  error string): bare token/auth_token/refresh_token/session_token + standalone
  "Bearer <opaque>". Benign "token bucket" prose is left intact.
- get_config_redacted deep-redacts every string leaf (JSON-safe, per-leaf, so
  the cookie/sess line patterns can't gobble across a compact-JSON field) and
  drops the history subtree (served by get_history with its own per-error
  redaction). This plugs leaks via globalSettings.pendingQueue[].error etc.

Bind-address safety:
- _safeDiagBindAddress() forces the diagnostic agent to 127.0.0.1/::1; the
  0.0.0.0 UI option is removed. Direct LAN/Internet bind stays disabled until
  encrypted transport (wss) exists — remote access goes through an SSH/VPN
  tunnel to loopback. (Never plaintext ws:// on all interfaces.)

Tests: end-to-end gateway<->agent gate (connect -> server_health/read_log/
get_config_redacted, asserts zero secret leakage, rejects doodstream log, path
traversal and write ops); + redaction regression tests in the main suite.
385 app tests + 9 gateway tests pass; lint 0 errors.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-19 17:39:31 +02:00

125 lines
5.3 KiB
JavaScript

import { createRequire } from 'node:module';
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
import { writeFileSync, mkdtempSync } from 'node:fs';
import { tmpdir } from 'node:os';
import assert from 'node:assert';
import { encode, decode } from '../code.js';
import { AgentClient } from '../agent-client.js';
const require = createRequire(import.meta.url);
const root = join(dirname(fileURLToPath(import.meta.url)), '..', '..');
const RemoteServer = require(join(root, 'lib', 'remote-server.js'));
const support = require(join(root, 'lib', 'support-bundle.js'));
const stats = require(join(root, 'lib', 'stats.js'));
const { createCollectors } = require(join(root, 'lib', 'diagnostics-collectors.js'));
const { createAgent } = require(join(root, 'lib', 'diagnostics-agent.js'));
const SECRET_API = 'SUPERSECRET_apikey_9f8e7d6c5b4a';
const SECRET_PW = 'hunter2_password_zxcv';
const SECRET_TOKEN = 'bearer_tok_qwerty12345';
const WEBHOOK = 'https://discord.com/api/webhooks/123456789012345678/abcDEF_secretWebhookToken-xyz';
const tmp = mkdtempSync(join(tmpdir(), 'mhu-e2e-'));
const debugLog = join(tmp, 'debug.log');
const dood = join(tmp, 'doodstream-debug.log');
writeFileSync(debugLog, [
`[2026-06-19T10:00:00Z] starting upload with apiKey=${SECRET_API}`,
`[2026-06-19T10:00:01Z] Authorization: Bearer ${SECRET_TOKEN}`,
`[2026-06-19T10:00:02Z] posting to ${WEBHOOK}`,
`[2026-06-19T10:00:03Z] password ${SECRET_PW} used`,
`[2026-06-19T10:00:04Z] normal benign line`,
].join('\n'));
writeFileSync(dood, `[doodstream] live apiKey=${SECRET_API}\n`);
const fakeConfig = {
hosters: { doodstream: [{ accountId: 'acc1', apiKey: SECRET_API, password: SECRET_PW }] },
hosterSettings: {},
globalSettings: {
webhookUrl: WEBHOOK,
diagnostics: { enabled: true, port: 9110, token: 'x'.repeat(64) },
pendingQueue: { savedAt: '2026-06-19T09:00:00Z', queueJobs: [{ file: 'a.mp4', fileName: 'a.mp4', hoster: 'doodstream', status: 'error', error: `failed with apiKey=${SECRET_API}` }], selectedUploadHosters: ['doodstream'], selectedFiles: ['a.mp4'] },
},
rotationCursors: { doodstream: 1 },
history: [{ timestamp: '2026-06-19T08:00:00Z', files: [{ name: 'a.mp4', results: [{ hoster: 'doodstream', status: 'error', error: `boom token=${SECRET_TOKEN}` }] }] }],
};
function getAllLogPaths() {
return { fileuploader: join(tmp, 'fileuploader.log'), debug: debugLog, accountRotation: join(tmp, 'rot.log'), doodstreamDebug: dood, crashLog: join(tmp, 'crash.log'), logDir: tmp };
}
const collectors = createCollectors({
loadConfig: () => fakeConfig,
getAllLogPaths,
support,
stats,
appInfo: () => ({ version: '9.9.9' }),
systemInfo: () => ({ platform: 'win32', hostname: 'TESTHOST' }),
agentInfo: () => ({ version: '9.9.9', port: 9110 }),
});
const agent = createAgent(collectors);
const TOKEN = 'z'.repeat(64);
const srv = new RemoteServer();
const SECRETS = [SECRET_API, SECRET_PW, SECRET_TOKEN, 'abcDEF_secretWebhookToken-xyz'];
function assertNoLeak(label, payload) {
const text = JSON.stringify(payload);
for (const s of SECRETS) {
assert.ok(!text.includes(s), `LEAK in ${label}: secret "${s.slice(0, 12)}…" appeared in response`);
}
}
(async () => {
await srv.start({
port: 0,
host: '127.0.0.1',
token: TOKEN,
diagnosticMode: true,
onDiagnosticRequest: (msg, _client, reply) => {
let r;
try { r = agent.handle(msg.op, msg.args); }
catch (e) { r = { ok: false, error: String(e && e.message || e) }; }
reply(r);
},
});
const port = srv.getPort();
const code = encode({ v: 1, port, token: TOKEN, label: 'e2e' });
const payload = decode(code);
assert.equal(payload.token, TOKEN);
const client = new AgentClient({ host: '127.0.0.1', port, token: payload.token });
await client.connect();
const health = await client.request('server_health', { errorLimit: 10 });
assert.equal(health.ok, true, 'server_health must succeed: ' + JSON.stringify(health));
assertNoLeak('server_health', health);
assert.ok(health.data.errors, 'server_health has errors section');
assert.ok(health.data.queue, 'server_health has queue section');
const log = await client.request('read_log', { name: 'debug', tailKb: 64 });
assert.equal(log.ok, true, 'read_log debug must succeed');
assert.ok(log.data.content.includes('normal benign line'), 'benign content preserved');
assertNoLeak('read_log:debug', log);
const dl = await client.request('read_log', { name: 'doodstream', tailKb: 64 });
assert.equal(dl.ok, false, 'doodstream log MUST NOT be readable (live api keys)');
const trav = await client.request('read_log', { name: '../../../etc/passwd' });
assert.equal(trav.ok, false, 'path traversal must be rejected');
const cfg = await client.request('get_config_redacted', { section: 'all' });
assert.equal(cfg.ok, true, 'get_config_redacted must succeed');
assertNoLeak('get_config_redacted', cfg);
const writeAttempt = await client.request('save_config', { x: 1 });
assert.equal(writeAttempt.ok, false, 'unknown/write op must be rejected by whitelist');
client.close();
srv.stop();
console.log('E2E PASS: connect → server_health/read_log/get_config_redacted succeeded; NO secrets leaked; doodstream + traversal + write rejected.');
process.exit(0);
})().catch((e) => { console.error('E2E FAIL:', e && e.stack || e); try { srv.stop(); } catch {} process.exit(1); });