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 URL-embedded credentials before any error sink

nirholase628677a3f5d
diff --git a/api/_lib/http.js b/api/_lib/http.js
index 9482697cd..ee00735f3 100644
--- a/api/_lib/http.js
+++ b/api/_lib/http.js
@@ -7,6 +7,7 @@ import { captureException } from './sentry.js';
import { sendOpsAlert } from './alerts.js';
import { instrument as zauthInstrument, drain as zauthDrain } from './zauth.js';
import { isDbUnavailableError, isDbCapacityError, isStoragePressured } from './db.js';
+import { redactUrlSecrets } from './scrub-secrets.js';
// Secure-by-default caching: emit `no-store` UNLESS the handler already set a
// Cache-Control header (e.g. `res.setHeader('cache-control', 'public, s-maxage=…')`
@@ -192,7 +193,12 @@ function logDbUnavailableOnce(scope, msg) {
// (callers redact URLs via redactUrl() before passing them here).
export function reportServerError(err, { code = 'internal_error', status = 500, context = {} } = {}) {
const ref = correlationId();
- const detail = err?.message || String(err ?? 'unknown error');
+ // Mask URL-embedded credentials before the message reaches ANY sink (console,
+ // Sentry, ops alert). Solana web3.js and fetch put the full request URL in
+ // 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'));
// 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.
@@ -208,7 +214,19 @@ export function reportServerError(err, { code = 'internal_error', status = 500,
}
console.error(`[server-error ${ref}] ${code} (${status}): ${detail}`);
try {
- captureException(err instanceof Error ? err : new Error(detail), { ref, code, status, ...context });
+ // Capture the original Error (preserving its stack + class) only when its
+ // 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 — send the original untouched
+ } else {
+ captured = new Error(detail);
+ // A stack's first line repeats the message, so redact it too rather than
+ // closing the front door and leaving the stack open.
+ if (err instanceof Error && err.stack) captured.stack = redactUrlSecrets(err.stack);
+ }
+ captureException(captured, { ref, code, status, ...context });
// Fire-and-forget like captureException; deduped per error class+message
// (ref excluded from the signature so each occurrence doesn't re-alert).
sendOpsAlert(`${status} ${code}`, `${detail}\nref ${ref}`, {
diff --git a/api/_lib/scrub-secrets.js b/api/_lib/scrub-secrets.js
index fd537e50e..19c5249f2 100644
--- a/api/_lib/scrub-secrets.js
+++ b/api/_lib/scrub-secrets.js
@@ -49,3 +49,25 @@ 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
+// `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;
+
+/**
+ * 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.
+ *
+ * Use this on any string bound for a log sink; use `scrubSecrets` for objects.
+ *
+ * @param {*} text
+ * @returns {string}
+ */
+export function redactUrlSecrets(text) {
+ return String(text ?? '').replace(URL_CREDENTIAL_RE, '$1REDACTED');
+}
diff --git a/tests/scrub-secrets.test.js b/tests/scrub-secrets.test.js
index e22c8f0fd..9ad053dc4 100644
--- a/tests/scrub-secrets.test.js
+++ b/tests/scrub-secrets.test.js
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest';
-import { scrubSecrets } from '../api/_lib/scrub-secrets.js';
+import { scrubSecrets, redactUrlSecrets } from '../api/_lib/scrub-secrets.js';
describe('scrubSecrets', () => {
it('redacts secret-bearing keys at the top level', () => {
@@ -46,4 +46,51 @@ describe('scrubSecrets', () => {
scrubSecrets(input);
expect(input.secret).toBe('keep-me-in-original');
});
+
+ it('does NOT reach credentials inside a plain string (that is redactUrlSecrets job)', () => {
+ // Pinning the division of labour: scrubSecrets matches on object KEYS, so a
+ // bare error message is invisible to it. If someone ever "fixes" that by
+ // making it walk strings, this test says where the real tool lives.
+ const msg = 'FetchError: request to https://rpc.example/?api-key=SECRET failed';
+ expect(scrubSecrets(msg)).toBe(msg);
+ });
+});
+
+describe('redactUrlSecrets', () => {
+ it('masks a keyed RPC URL inside a network error message', () => {
+ // The exact shape Solana web3.js produces when the RPC call fails — the leak
+ // this exists to stop (HELIUS_API_KEY into console / Sentry / ops alerts).
+ const out = redactUrlSecrets('FetchError: request to https://mainnet.helius-rpc.com/?api-key=abc123secret failed');
+ expect(out).not.toContain('abc123secret');
+ expect(out).toContain('api-key=REDACTED');
+ // The rest of the message must survive, or the log is useless for debugging.
+ expect(out).toContain('FetchError');
+ expect(out).toContain('mainnet.helius-rpc.com');
+ });
+
+ it('masks every credential parameter spelling and multiple occurrences', () => {
+ const out = redactUrlSecrets(
+ 'a=https://x/?api_key=A1 b=https://y/?access-token=B2 c=https://z/?secret=C3&token=D4&auth=E5',
+ );
+ for (const leaked of ['A1', 'B2', 'C3', 'D4', 'E5']) expect(out).not.toContain(leaked);
+ });
+
+ it('stops at the parameter boundary, keeping following params readable', () => {
+ const out = redactUrlSecrets('https://rpc/?api-key=SECRET&cluster=devnet');
+ expect(out).toBe('https://rpc/?api-key=REDACTED&cluster=devnet');
+ });
+
+ it('leaves a clean message and non-credential params untouched', () => {
+ const clean = 'Transaction simulation failed: insufficient lamports for rent';
+ expect(redactUrlSecrets(clean)).toBe(clean);
+ expect(redactUrlSecrets('https://rpc/?cluster=devnet&commitment=confirmed')).toBe(
+ 'https://rpc/?cluster=devnet&commitment=confirmed',
+ );
+ });
+
+ it('coerces non-strings instead of throwing', () => {
+ expect(redactUrlSecrets(null)).toBe('');
+ expect(redactUrlSecrets(undefined)).toBe('');
+ expect(redactUrlSecrets(42)).toBe('42');
+ });
});