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

60 lines
1.7 KiB
JavaScript

const PREFIX = 'mhu1_';
export function encode(payload) {
if (!payload || typeof payload !== 'object') {
throw new Error('encode: payload must be an object');
}
const json = JSON.stringify(payload);
const b64 = Buffer.from(json, 'utf8').toString('base64url');
return PREFIX + b64;
}
export function decode(code) {
if (typeof code !== 'string') {
throw new Error('Invalid code: expected a string');
}
const trimmed = code.trim();
if (!trimmed.startsWith(PREFIX)) {
throw new Error('Invalid code: missing "mhu1_" prefix');
}
const b64 = trimmed.slice(PREFIX.length);
if (!b64) {
throw new Error('Invalid code: empty payload');
}
let json;
try {
json = Buffer.from(b64, 'base64url').toString('utf8');
} catch {
throw new Error('Invalid code: not valid base64url');
}
let payload;
try {
payload = JSON.parse(json);
} catch {
throw new Error('Invalid code: payload is not valid JSON');
}
if (!payload || typeof payload !== 'object') {
throw new Error('Invalid code: payload is not an object');
}
if (payload.v !== 1) {
throw new Error(`Invalid code: unsupported version (expected v=1, got ${payload.v})`);
}
if (typeof payload.port !== 'number' || !Number.isFinite(payload.port)) {
throw new Error('Invalid code: "port" must be a number');
}
if (typeof payload.token !== 'string' || payload.token.length === 0) {
throw new Error('Invalid code: "token" must be a non-empty string');
}
if (typeof payload.label !== 'string') {
throw new Error('Invalid code: "label" must be a string');
}
if (payload.fp !== undefined && typeof payload.fp !== 'string') {
throw new Error('Invalid code: "fp" must be a string when present');
}
return payload;
}