/* Copyright (c) 2026 Code Infinity SPDX-License-Identifier: MPL-2.0 This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/. */ /** * GHSA-h2mr-m3v3-cq8c - a connection URL's password never reaches output, * whatever shape the URL has. * * Driven by test/fixtures/db_url_redaction_cases.json, the case list shared by * all four frameworks: every password x every template x every scheme each * parser supports, through the REAL code: * * redact redactCredentials() - the single redaction primitive, and the * function the websocket backplane's "connected to" line uses * parse DatabaseUrl, GraphUrl, Mqtt.parseUrl, parseAmqpUrl * connect initDatabase (pg, mysql2, tedious, node-firebird, mongodb), * GraphDatabase + query (ultipa, neo4j-driver, arangojs), Mqtt.connect, * RabbitMQBackend.push, the Mongo DocStore, the Redis websocket * backplane - every one against a CLOSED 127.0.0.1 port, so each * fails for real * boot real startServer() children with TINA4_DATABASE_URL set (the * "Database could not be reached" boot line) for every template x * three passwords, plus the dev admin's GET /__dev/api/connections * for every case * * No mocks. Process output is CAPTURED (stdout/stderr writes) while each * framework call runs, and every captured byte is scanned too. * * "No output contains a secret fragment" means: the return value, the parsed * non-secret fields (host, user, database, the safe/JSON/inspect renders), * error.message, error.stack, util.inspect(error) - which walks own properties * such as Node's TypeError `.input` - and the whole `cause` chain, plus all * captured stdout/stderr. * * A FAILURE NEVER PRINTS A SECRET. Every case is named by an id - scheme, * template #N, password #N (their positions in the fixture) - and a leak is * reported as WHICH fragment (#N in secret_fragments) turned up on WHICH * surface (redacted form, a parsed field, error.message, cause[1].stack, the * captured output, the boot log, ...). The produced text itself is never * logged: a test that prints the value it caught leaking is a leak of its own. * * KNOWN LIMITS (documented, asserted to be the ONLY exclusions): * - A host-less URL whose password is all digits (postgres://u:123/x) is a * valid URL with port 123. Not in the case list. * - `{scheme}://u:Pq1@Rs2` (the host-less template with a password holding an * unencoded `@`) is byte-for-byte a valid URL whose HOST is `Rs2`. Nothing * can tell it from `postgres://u:pw@dbhost`, so the "host" is shown and a * connect looks it up. Skipped on every surface, and counted, so the * exclusion cannot silently grow. * * sqlite and odbc are not swept: a sqlite URL is a file path with no userinfo, * and odbc carries PWD= (covered by test/databaseUrlRedaction.test.ts). * * Run with: npx tsx test/dbUrlRedaction.test.ts */ import { inspect } from "node:util"; import { readFileSync, mkdtempSync, mkdirSync, writeFileSync, rmSync } from "node:fs"; import { spawn, type ChildProcess } from "node:child_process"; import { tmpdir } from "node:os"; import { join, resolve } from "node:path"; import http from "node:http"; import { pathToFileURL } from "node:url"; import { freePort } from "./freePort.ts"; // Bound every connect so a run stays fast; a refused port answers in ms anyway. process.env.TINA4_DATABASE_CONNECT_TIMEOUT = "2"; process.env.TINA4_GRAPH_CONNECT_TIMEOUT = "2"; delete process.env.TINA4_DB_POOL; delete process.env.TINA4_DATABASE_USERNAME; delete process.env.TINA4_DATABASE_PASSWORD; const { DatabaseUrl, redactCredentials } = await import("../packages/orm/src/databaseUrl.ts"); const { initDatabase } = await import("../packages/orm/src/database.ts"); const { GraphUrl } = await import("../packages/orm/src/graph/graphUrl.ts"); const { GraphDatabase } = await import("../packages/orm/src/graph/graphDatabase.ts"); const { Mqtt } = await import("../packages/core/src/mqtt.ts"); const { parseAmqpUrl, RabbitMQBackend } = await import("../packages/core/src/queueBackends/rabbitmqBackend.ts"); const { getCollection, closeDocStore } = await import("../packages/orm/src/docstore.ts"); interface MustParse { url: string; host: string; port?: number; user: string; password: string; database: string } const CASES = JSON.parse( readFileSync(join(import.meta.dirname, "fixtures", "db_url_redaction_cases.json"), "utf8"), ) as { secret_fragments: string[]; passwords: string[]; templates: string[]; must_still_parse: MustParse[] }; const FRAGMENTS = CASES.secret_fragments; // ── bookkeeping ──────────────────────────────────────────────────────────── let pass = 0; let fail = 0; const failsBySurface = new Map(); const groupPass = new Map(); /** * One assertion. PASS lines are folded per group (thousands of cases); every * FAIL prints. `name` and `why` are secret-free BY CONSTRUCTION: case ids, * surface labels and fragment numbers - never produced text. */ function check(group: string, name: string, ok: boolean, why = ""): void { if (ok) { pass++; groupPass.set(group, (groupPass.get(group) ?? 0) + 1); return; } fail++; const surface = group.split(" ")[0]; failsBySurface.set(surface, (failsBySurface.get(surface) ?? 0) + 1); console.log(` \x1b[31mFAIL\x1b[0m [${group}] ${name}${why ? ` - ${why}` : ""}`); } function flushGroup(group: string): void { const n = groupPass.get(group) ?? 0; if (n > 0) console.log(` \x1b[32mPASS\x1b[0m [${group}] ${n} case(s)`); groupPass.delete(group); } /** Which secret fragment `text` carries, by NAME ("fragment #1"), or null. */ function leakIn(text: string): string | null { const index = FRAGMENTS.findIndex((f) => text.includes(f)); return index === -1 ? null : `fragment #${index + 1}`; } /** A labelled piece of output, so a leak can be reported by WHERE it was. */ interface Surface { label: string; text: string } /** The first surface carrying a fragment, as "fragment #N in