Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix([codex5-client-error-reporter]): harden never-throws + strip url fragments

[._.]/ Adam Eivycebd56038fa0
diff --git a/client/src/lib/clientErrorReporter.js b/client/src/lib/clientErrorReporter.js
index ac29038263..374a725f4a 100644
--- a/client/src/lib/clientErrorReporter.js
+++ b/client/src/lib/clientErrorReporter.js
@@ -59,12 +59,30 @@ function safeToString(value) {
try { return String(value); } catch { return '[unstringifiable]'; }
}
+// Read a property from an unknown reason without trusting it not to throw —
+// a Proxy or hostile getter can throw on `.message` / `.stack` access.
+function safeGet(obj, key) {
+ try { return obj[key]; } catch { return undefined; }
+}
+
+function coerceStack(stack) {
+ if (stack == null) return undefined;
+ return typeof stack === 'string' ? stack : safeToString(stack);
+}
+
function extractFromReason(reason) {
if (reason instanceof Error) {
- return { message: reason.message || safeToString(reason), stack: reason.stack };
+ return {
+ message: safeGet(reason, 'message') || safeToString(reason),
+ stack: coerceStack(safeGet(reason, 'stack')),
+ };
}
if (reason && typeof reason === 'object') {
- return { message: safeToString(reason.message ?? safeStringify(reason)), stack: reason.stack };
+ const m = safeGet(reason, 'message');
+ return {
+ message: safeToString(m ?? safeStringify(reason)),
+ stack: coerceStack(safeGet(reason, 'stack')),
+ };
}
return { message: safeToString(reason ?? 'Unknown'), stack: undefined };
}
@@ -108,29 +126,38 @@ export function buildPayload(input) {
/**
* Report a client-side error. Resolves to:
* - `{ sent: true }` — sent to the server.
- * - `{ sent: false, reason: 'duplicate' | 'rate-limited' | 'no-fetch' | 'transport-error' }`
+ * - `{ sent: false, reason: 'duplicate' | 'rate-limited' | 'no-fetch' | 'transport-error' | 'caught' | 'empty' }`
*
- * Never throws.
+ * Never throws — any synchronous exception while building the payload or any
+ * fetch failure resolves to `{ sent: false, reason }`. This is critical because
+ * the reporter is wired from `unhandledrejection`, where any uncaught throw
+ * would itself become an unhandled rejection (infinite recursion).
*/
export async function reportClientError(input) {
if (typeof fetch !== 'function') return { sent: false, reason: 'no-fetch' };
- const payload = buildPayload(input);
- if (!payload.message) return { sent: false, reason: 'empty' };
-
+ let payload;
+ let hash;
const now = Date.now();
- if (now - lastSentAt < MIN_SEND_INTERVAL_MS) {
- return { sent: false, reason: 'rate-limited' };
- }
- const hash = hashError(payload.message, payload.stack, payload.source);
- if (recentHashes.has(hash)) {
- return { sent: false, reason: 'duplicate' };
+ try {
+ payload = buildPayload(input);
+ if (!payload.message) return { sent: false, reason: 'empty' };
+
+ if (now - lastSentAt < MIN_SEND_INTERVAL_MS) {
+ return { sent: false, reason: 'rate-limited' };
+ }
+ hash = hashError(payload.message, payload.stack, payload.source);
+ if (recentHashes.has(hash)) {
+ return { sent: false, reason: 'duplicate' };
+ }
+
+ recentHashes.set(hash, now);
+ lastSentAt = now;
+ pruneRecent(now);
+ } catch {
+ return { sent: false, reason: 'caught' };
}
- recentHashes.set(hash, now);
- lastSentAt = now;
- pruneRecent(now);
-
// Raw `fetch` (not the shared `request()` helper) because we need
// `keepalive: true` so the POST survives a tab unload triggered by the
// crash itself, and we must never throw — an exception inside the
diff --git a/client/src/lib/clientErrorReporter.test.js b/client/src/lib/clientErrorReporter.test.js
index 98fbace280..d985cac85a 100644
--- a/client/src/lib/clientErrorReporter.test.js
+++ b/client/src/lib/clientErrorReporter.test.js
@@ -34,6 +34,20 @@ describe('buildPayload', () => {
expect(() => buildPayload({ type: 'unhandledrejection', reason: hostile })).not.toThrow();
});
+ it('does not throw when the reason has a throwing `message` or `stack` getter', () => {
+ const hostile = {};
+ Object.defineProperty(hostile, 'message', { get() { throw new Error('msg'); } });
+ Object.defineProperty(hostile, 'stack', { get() { throw new Error('stk'); } });
+ expect(() => buildPayload({ type: 'unhandledrejection', reason: hostile })).not.toThrow();
+ });
+
+ it('coerces a non-string `stack` field to a string', () => {
+ const reason = new Error('oops');
+ reason.stack = { weird: true };
+ const out = buildPayload({ type: 'unhandledrejection', reason });
+ expect(typeof out.stack).toBe('string');
+ });
+
it('extracts message + stack + filename/lineno/colno from an error event', () => {
const err = new Error('boom');
err.stack = 'Error: boom\n at foo (foo.js:1:1)';
diff --git a/client/src/main.jsx b/client/src/main.jsx
index dd1f32f80a..5050a84a13 100644
--- a/client/src/main.jsx
+++ b/client/src/main.jsx
@@ -24,8 +24,11 @@ window.addEventListener('unhandledrejection', (event) => {
event.preventDefault();
return;
}
- console.error(`❌ Unhandled Promise Rejection: ${event.reason}`);
+ // Report first so a hostile `event.reason` (throwing toString / circular)
+ // can't take down the handler before `reportClientError` runs. Pass the
+ // reason as a separate console argument — no implicit String() coercion.
reportClientError({ type: 'unhandledrejection', reason: event.reason });
+ console.error('❌ Unhandled Promise Rejection:', event.reason);
event.preventDefault();
});
diff --git a/server/lib/errorHandler.js b/server/lib/errorHandler.js
index a53b58e886..2565e850ca 100644
--- a/server/lib/errorHandler.js
+++ b/server/lib/errorHandler.js
@@ -16,14 +16,22 @@ const causeSuffix = (error) =>
error.context?.causeChain ? ` ← ${error.context.causeChain}` : '';
/**
- * Strip the `?query` (and everything after) from a URL or request path so
- * tokens like `?access_token=…` don't leak into server logs or stored error
- * reports. Returns the input unchanged when there is no query string.
+ * Strip the `?query` and `#fragment` (whichever appears first, plus everything
+ * after) from a URL or request path so tokens like `?access_token=…` or
+ * `#access_token=…` from an OAuth implicit-grant callback don't leak into
+ * server logs or stored error reports. Returns the input unchanged when
+ * neither separator is present. Server-side `req.url` never carries a
+ * fragment (browsers don't send them), so the fragment strip is a no-op
+ * for the `routePath` caller — it's there for the client-error sanitizer.
*/
export function stripQueryString(url) {
if (typeof url !== 'string') return url;
const qIndex = url.indexOf('?');
- return qIndex === -1 ? url : url.slice(0, qIndex);
+ const hIndex = url.indexOf('#');
+ const cut = (qIndex === -1)
+ ? hIndex
+ : (hIndex === -1 ? qIndex : Math.min(qIndex, hIndex));
+ return cut === -1 ? url : url.slice(0, cut);
}
/**
diff --git a/server/services/clientErrors.test.js b/server/services/clientErrors.test.js
index 7f0dc72e63..fb061b3450 100644
--- a/server/services/clientErrors.test.js
+++ b/server/services/clientErrors.test.js
@@ -151,4 +151,15 @@ describe('recordClientError', () => {
expect(arg.description).not.toContain('token=keepout');
expect(arg.metadata.source).toBe('/assets/index-abc.js');
});
+
+ it('strips a `#fragment` from URL fields too (OAuth implicit-grant tokens)', async () => {
+ await recordClientError({
+ type: 'error',
+ message: 'boom',
+ url: 'https://portos/callback#access_token=keepout',
+ });
+ const arg = review.createItem.mock.calls[0][0];
+ expect(arg.description).toContain('URL: https://portos/callback');
+ expect(arg.description).not.toContain('access_token');
+ });
});