Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

security(logs): mask the password in a connection URL, not just a query-string key

nirholasb5d7aa6cda29
diff --git a/api/_lib/http.js b/api/_lib/http.js
index 54a448ca5..65026f81f 100644
--- a/api/_lib/http.js
+++ b/api/_lib/http.js
@@ -198,7 +198,12 @@ export function reportServerError(err, { code = 'internal_error', status = 500,
// their network errors, so an on-chain failure would otherwise print the keyed
// RPC endpoint (HELIUS_API_KEY) into logs. Applied here, at the single shared
// sink, so every handler is covered rather than each remembering to redact.
- const detail = redactUrlSecrets(err?.message || String(err ?? 'unknown error'));
+ const rawDetail = err?.message || String(err ?? 'unknown error');
+ const detail = redactUrlSecrets(rawDetail);
+ // Whether anything was actually masked. Compared against the RAW string, not
+ // against err.message, so an Error with an empty message (which falls back to
+ // String(err)) is not misread as "redacted" and needlessly replaced below.
+ const redacted = detail !== rawDetail;
// A DB outage is infrastructure, not a code fault: throttle the log, skip the
// Sentry capture, and collapse to the single shared `db:unavailable` alert so a
// missing DATABASE_URL degrades quietly instead of flooding error tracking.
@@ -218,8 +223,8 @@ export function reportServerError(err, { code = 'internal_error', status = 500,
// message carried no credential; otherwise send a redacted stand-in that
// keeps the stack but not the key.
let captured;
- if (err instanceof Error && err.message === detail) {
- captured = err; // nothing was redacted, so send the original untouched
+ if (err instanceof Error && !redacted) {
+ captured = err; // nothing was masked, so send the original untouched
} else {
captured = new Error(detail);
// A stack's first line repeats the message, so redact it too rather than
diff --git a/api/_lib/scrub-secrets.js b/api/_lib/scrub-secrets.js
index 19c5249f2..1e202bd3d 100644
--- a/api/_lib/scrub-secrets.js
+++ b/api/_lib/scrub-secrets.js
@@ -50,18 +50,28 @@ export function scrubSecrets(value, seen = new WeakSet()) {
return out;
}
-// Credentials carried in a URL QUERY STRING, which scrubSecrets cannot reach:
-// it redacts by KEY name on an object, and a message like
+// Credentials carried in a URL, which scrubSecrets cannot reach: it redacts by
+// KEY name on an object, and a message like
// `FetchError: request to https://rpc.example/?api-key=SECRET failed` is a plain
// string with no key to match. Solana web3.js embeds the full RPC URL in its
// network errors, so any path that logs a raw `err.message` from an on-chain
// call would otherwise spill HELIUS_API_KEY into the log sink.
-const URL_CREDENTIAL_RE = /([?&](?:api[-_]?key|access[-_]?token|token|secret|key|auth)=)[^&\s"'`]+/gi;
+//
+// Two shapes carry a secret, and both occur here:
+// 1. a query parameter https://rpc.host/?api-key=SECRET
+// 2. userinfo postgres://user:PASSWORD@host/db
+// The second matters just as much: a Neon/Postgres connection failure puts the
+// whole DATABASE_URL, password included, into its error message.
+const URL_QUERY_CREDENTIAL_RE = /([?&](?:api[-_]?key|access[-_]?token|token|secret|key|auth)=)[^&\s"'`]+/gi;
+// `//user:` then anything up to the `@`. The username is kept (it is useful for
+// debugging and is not the secret); only the password is masked.
+const URL_USERINFO_RE = /(\/\/[^/@\s:]+:)[^/@\s]+@/g;
/**
* Mask credential VALUES embedded in a URL inside a free-form string (typically
* an error message) while keeping the rest of the text intact for debugging.
- * Only the value after a known credential parameter is replaced.
+ * Covers both a known credential query parameter and a `user:password@` userinfo
+ * segment; everything else is left readable.
*
* Use this on any string bound for a log sink; use `scrubSecrets` for objects.
*
@@ -69,5 +79,7 @@ const URL_CREDENTIAL_RE = /([?&](?:api[-_]?key|access[-_]?token|token|secret|key
* @returns {string}
*/
export function redactUrlSecrets(text) {
- return String(text ?? '').replace(URL_CREDENTIAL_RE, '$1REDACTED');
+ return String(text ?? '')
+ .replace(URL_QUERY_CREDENTIAL_RE, '$1REDACTED')
+ .replace(URL_USERINFO_RE, '$1REDACTED@');
}
diff --git a/tests/scrub-secrets.test.js b/tests/scrub-secrets.test.js
index e8ce07358..d2e08fb1c 100644
--- a/tests/scrub-secrets.test.js
+++ b/tests/scrub-secrets.test.js
@@ -88,6 +88,34 @@ describe('redactUrlSecrets', () => {
);
});
+ it('masks the password in a postgres/redis connection URL', () => {
+ // The shape a Neon or Upstash connection failure carries: the whole
+ // DATABASE_URL, password included, lands in err.message and then in the log.
+ const pg = redactUrlSecrets('connect ECONNREFUSED postgres://neondb_owner:[email protected]/neondb');
+ expect(pg).not.toContain('npg_S3cr3tPw');
+ expect(pg).toBe('connect ECONNREFUSED postgres://neondb_owner:[email protected]/neondb');
+
+ const redis = redactUrlSecrets('redis://default:[email protected]:6379 timed out');
+ expect(redis).not.toContain('AX9zSECRET');
+ expect(redis).toContain('redis://default:[email protected]:6379');
+ });
+
+ it('keeps the username and host readable, masking only the password', () => {
+ // Redaction has to leave enough behind to debug with: which database, which
+ // host, which role. Only the secret goes.
+ const out = redactUrlSecrets('postgres://app_user:[email protected]:5432/prod');
+ expect(out).toContain('app_user');
+ expect(out).toContain('db.internal:5432/prod');
+ expect(out).not.toContain('hunter2');
+ });
+
+ it('does not mangle a URL with no password', () => {
+ expect(redactUrlSecrets('https://user@host/path')).toBe('https://user@host/path');
+ expect(redactUrlSecrets('https://example.com/a/b?x=1')).toBe('https://example.com/a/b?x=1');
+ // A bare "scheme:host" with no // userinfo must not be treated as credentials.
+ expect(redactUrlSecrets('see mailto:[email protected]')).toBe('see mailto:[email protected]');
+ });
+
it('coerces non-strings instead of throwing', () => {
expect(redactUrlSecrets(null)).toBe('');
expect(redactUrlSecrets(undefined)).toBe('');