Multi-Hoster-Upload/gateway/test/code.test.js
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

63 lines
1.9 KiB
JavaScript

import { test } from 'node:test';
import assert from 'node:assert/strict';
import { encode, decode } from '../code.js';
test('decode(encode(x)) round-trips a full payload', () => {
const payload = {
v: 1,
port: 9110,
token: 'deadbeefcafe1234',
label: 'prod-3',
fp: 'AB:CD:EF:01:23:45:67:89',
};
const code = encode(payload);
assert.ok(code.startsWith('mhu1_'));
assert.deepEqual(decode(code), payload);
});
test('decode(encode(x)) round-trips a payload without fp (ws://)', () => {
const payload = { v: 1, port: 9110, token: 'token-abc', label: 'localhost' };
const code = encode(payload);
assert.deepEqual(decode(code), payload);
});
test('decode rejects a string without the mhu1_ prefix', () => {
assert.throws(() => decode('hello-world'), /missing "mhu1_" prefix/);
});
test('decode rejects a wrong-version payload', () => {
const bad = 'mhu1_' + Buffer.from(
JSON.stringify({ v: 2, port: 9110, token: 'x', label: 'l' }),
'utf8',
).toString('base64url');
assert.throws(() => decode(bad), /unsupported version/);
});
test('decode rejects garbage after the prefix', () => {
assert.throws(() => decode('mhu1_!!!not-base64-or-json!!!'), /Invalid code/);
});
test('decode rejects an empty payload', () => {
assert.throws(() => decode('mhu1_'), /empty payload/);
});
test('decode rejects a non-string input', () => {
assert.throws(() => decode(null), /expected a string/);
});
test('decode rejects a missing token', () => {
const bad = 'mhu1_' + Buffer.from(
JSON.stringify({ v: 1, port: 9110, label: 'l' }),
'utf8',
).toString('base64url');
assert.throws(() => decode(bad), /token/);
});
test('decode rejects a non-number port', () => {
const bad = 'mhu1_' + Buffer.from(
JSON.stringify({ v: 1, port: 'nope', token: 'x', label: 'l' }),
'utf8',
).toString('base64url');
assert.throws(() => decode(bad), /port/);
});