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

address review (codex): match provenance on the originating frame, not any frame

[._.]/ Adam Eivy1ff6ac56de57
diff --git a/client/src/lib/extensionErrors.js b/client/src/lib/extensionErrors.js
index 22eec6eadb..cc6fcdd7c5 100644
--- a/client/src/lib/extensionErrors.js
+++ b/client/src/lib/extensionErrors.js
@@ -13,10 +13,22 @@ const EXTENSION_MESSAGE_RE = [
/\bMetaMask\b/i,
];
+// Only the throw site proves provenance — an extension that wraps or invokes
+// our code leaves its frames below ours. Handles V8 (`at fn (url:1:1)`) and
+// Firefox/Safari (`fn@url:1:1`) stacks.
+function originatingFrame(stack) {
+ for (const raw of stack.split('\n')) {
+ const line = raw.trim();
+ if (!line) continue;
+ if (/^at\s/.test(line) || line.includes('@')) return line;
+ }
+ return '';
+}
+
/**
* True when an error report originated from a browser extension rather than
- * PortOS itself. Checks `source` / `stack` / `message`; never `url` (the page
- * location is ours even when an extension throws on top of it).
+ * PortOS itself. Checks `source` / the stack's originating frame / `message`;
+ * never `url` (the page location is ours even when an extension throws on it).
*/
export function isExtensionError(payload) {
if (!payload || typeof payload !== 'object') return false;
@@ -27,7 +39,7 @@ export function isExtensionError(payload) {
const message = str(payload.message);
if (EXTENSION_SCHEME_RE.test(source)) return true;
- if (EXTENSION_SCHEME_RE.test(stack)) return true;
+ if (EXTENSION_SCHEME_RE.test(originatingFrame(stack))) return true;
if (EXTENSION_SCHEME_RE.test(message)) return true;
return EXTENSION_MESSAGE_RE.some(re => re.test(message));
diff --git a/docs/features/error-handling.md b/docs/features/error-handling.md
index caeee51e01..dad0e44400 100644
--- a/docs/features/error-handling.md
+++ b/docs/features/error-handling.md
@@ -75,20 +75,26 @@ control and not actionable — the user cannot fix MetaMask by changing PortOS
Detection is **provenance-first**: an extension URL scheme (`chrome-extension://`,
`moz-extension://`, `safari-web-extension://`, `webkit-masked-url://`, …) in the script
-`source` or the `stack` proves the frame is not ours. This does the real work — every
-extension error observed so far carries such a frame. A single message pattern
-(`MetaMask`) backstops the one case provenance cannot see: a wallet rejecting with a bare
-string, which carries no frames at all.
+`source` or at the stack's **originating frame** proves the throw site is not ours. This
+does the real work — every extension error observed so far carries such a frame. A single
+message pattern (`MetaMask`) backstops the one case provenance cannot see: a wallet
+rejecting with a bare string, which carries no frames at all.
-Four constraints worth preserving when touching this:
+Five constraints worth preserving when touching this:
- **Filter before the throttle, on both ends.** The client reporter and the server
aggregator each gate on a 1/sec throttle. An extension error that takes the slot drops a
genuine PortOS error arriving <1s behind it. Filtering early keeps the budget for errors
we can act on — it is a correctness rule, not an optimization.
-- **Filter before sanitize (server).** `sanitize()` caps the stack at 4000 chars, which can
- cut off the extension frame that is the only proof of provenance. Detection runs on the
- raw payload.
+- **Only the originating (top) frame counts — never "any frame in the stack".** An
+ extension that wraps or synchronously invokes our code (a patched `fetch`, an injected
+ provider, a dispatched event) leaves its frames *below* ours, so a genuine PortOS error
+ that merely passed through extension code carries an extension URL in its stack. Matching
+ any frame would silently drop it forever. `originatingFrame()` handles both stack
+ dialects — V8 (`at fn (url:1:1)`, after a `Type: message` line) and Firefox/Safari
+ (`fn@url:1:1`).
+- **Detect on the raw payload (server).** `sanitize()` caps the stack at 4000 chars, so a
+ long first line can push the originating frame past the cut and hide the provenance.
- **Never match on `url`.** That field is the *page* location, which is ours even when an
extension throws on top of it. Only `source` / `stack` / `message` are evidence.
- **Keep the message list near-empty.** Each pattern is a permanent silent-drop rule, and
diff --git a/server/lib/extensionErrors.js b/server/lib/extensionErrors.js
index 53b0e2f616..7bcc8866bd 100644
--- a/server/lib/extensionErrors.js
+++ b/server/lib/extensionErrors.js
@@ -44,13 +44,36 @@ const EXTENSION_MESSAGE_RE = [
/\bMetaMask\b/i,
];
+/**
+ * The originating (top) frame of a stack — the throw site.
+ *
+ * Only this frame proves provenance. An extension that wraps or synchronously
+ * invokes our code (a patched `fetch`, an injected provider, a dispatched
+ * event) leaves its frames BELOW ours, so a genuine PortOS error that merely
+ * passed *through* extension code has an extension URL in its stack while
+ * being entirely ours. Matching any frame would silently drop it forever —
+ * the worst failure this module can have.
+ *
+ * Handles both stack dialects: V8 (` at fn (url:1:1)`, preceded by a
+ * `Type: message` line) and Firefox/Safari (`fn@url:1:1`, no message line).
+ */
+function originatingFrame(stack) {
+ for (const raw of stack.split('\n')) {
+ const line = raw.trim();
+ if (!line) continue;
+ if (/^at\s/.test(line) || line.includes('@')) return line;
+ }
+ return '';
+}
+
/**
* True when an error report originated from a browser extension rather than
* PortOS itself.
*
- * Checks `source` (the script URL), `stack`, and `message`. Deliberately does
- * NOT check `url` — that is the *page* location (`window.location.href`),
- * which is always ours even when an extension throws on top of it.
+ * Checks `source` (the script URL), the stack's originating frame, and
+ * `message`. Deliberately does NOT check `url` — that is the *page* location
+ * (`window.location.href`), which is always ours even when an extension
+ * throws on top of it.
*
* Safe to call on a raw, unsanitized payload; every field is treated as
* untrusted and non-string values are ignored.
@@ -63,11 +86,12 @@ export function isExtensionError(payload) {
const stack = str(payload.stack);
const message = str(payload.message);
- // Provenance: an extension scheme anywhere in the script URL or the stack.
- // The message is checked too — a failed dynamic import or fetch names the
- // offending extension URL in its message with no stack attached.
+ // `source` is the script the error came from (an `error` event's `filename`),
+ // so it is already the originating script — no frame extraction needed.
if (EXTENSION_SCHEME_RE.test(source)) return true;
- if (EXTENSION_SCHEME_RE.test(stack)) return true;
+ if (EXTENSION_SCHEME_RE.test(originatingFrame(stack))) return true;
+ // A failed dynamic import or fetch names the offending extension URL in its
+ // message with no stack attached.
if (EXTENSION_SCHEME_RE.test(message)) return true;
return EXTENSION_MESSAGE_RE.some(re => re.test(message));
diff --git a/server/lib/extensionErrors.mirror.test.js b/server/lib/extensionErrors.mirror.test.js
index c6a96fa6a9..56da22186f 100644
--- a/server/lib/extensionErrors.mirror.test.js
+++ b/server/lib/extensionErrors.mirror.test.js
@@ -21,7 +21,7 @@ const __dirname = dirname(fileURLToPath(import.meta.url));
const SERVER_PATH = resolve(__dirname, 'extensionErrors.js');
const CLIENT_PATH = resolve(__dirname, '../../client/src/lib/extensionErrors.js');
-const MIRRORED_NAMES = ['EXTENSION_SCHEME_RE', 'EXTENSION_MESSAGE_RE', 'isExtensionError'];
+const MIRRORED_NAMES = ['EXTENSION_SCHEME_RE', 'EXTENSION_MESSAGE_RE', 'originatingFrame', 'isExtensionError'];
describe('extensionErrors server↔client mirror parity', () => {
const serverSrc = readFileSync(SERVER_PATH, 'utf8');
diff --git a/server/lib/extensionErrors.test.js b/server/lib/extensionErrors.test.js
index a8ec522de2..75a9152f85 100644
--- a/server/lib/extensionErrors.test.js
+++ b/server/lib/extensionErrors.test.js
@@ -33,6 +33,49 @@ describe('isExtensionError', () => {
})).toBe(true);
});
+ it('flags a Firefox/Safari-dialect stack (`fn@url`, no `at ` prefix)', () => {
+ expect(isExtensionError({
+ message: 'boom',
+ stack: 'inject@moz-extension://abcd-1234/content.js:1:1\nfoo@https://portos/assets/index.js:2:2',
+ })).toBe(true);
+ });
+ });
+
+ describe('provenance is the ORIGINATING frame, not any frame', () => {
+ // An extension that wraps or synchronously invokes our code (a patched
+ // fetch, an injected provider, a dispatched event) leaves its frames BELOW
+ // ours. Treating any frame as proof would silently drop a real PortOS bug.
+ it('does NOT flag a PortOS error merely invoked from extension code', () => {
+ expect(isExtensionError({
+ message: "Cannot read properties of undefined (reading 'id')",
+ stack: [
+ "TypeError: Cannot read properties of undefined (reading 'id')",
+ ' at renderRow (https://portos/assets/index-abc.js:10:5)',
+ ' at wrappedFetch (chrome-extension://examplewalletextensionid00000000/inpage.js:1:1)',
+ ].join('\n'),
+ })).toBe(false);
+ });
+
+ it('still flags it when the extension frame IS the throw site', () => {
+ expect(isExtensionError({
+ message: "Cannot read properties of null (reading 'ethereum')",
+ stack: [
+ "TypeError: Cannot read properties of null (reading 'ethereum')",
+ ' at inject (chrome-extension://examplewalletextensionid00000000/inpage.js:1:1)',
+ ' at dispatch (https://portos/assets/index-abc.js:10:5)',
+ ].join('\n'),
+ })).toBe(true);
+ });
+
+ it('does not mistake the V8 message line for the originating frame', () => {
+ // The `Type: message` line precedes the frames and must be skipped, or a
+ // message mentioning an extension URL would be read as the throw site.
+ expect(isExtensionError({
+ message: 'boom',
+ stack: 'Error: boom\n at f (https://portos/assets/index.js:1:1)',
+ })).toBe(false);
+ });
+
it('flags an extension URL named in the message with no stack', () => {
expect(isExtensionError({
message: 'Failed to fetch dynamically imported module: chrome-extension://abc/x.js',
diff --git a/server/services/clientErrors.js b/server/services/clientErrors.js
index ccd8aaaf8f..1c3af73414 100644
--- a/server/services/clientErrors.js
+++ b/server/services/clientErrors.js
@@ -130,8 +130,9 @@ export async function recordClientError(rawPayload) {
const now = Date.now();
// Before the throttle: an extension error taking the 1/sec slot would drop a
- // real PortOS error arriving behind it. Before sanitize: MAX_STACK_CHARS
- // would truncate away the extension frame that proves provenance.
+ // real PortOS error arriving behind it. On the raw payload: MAX_STACK_CHARS
+ // truncation can cut the originating frame off a stack with a long first
+ // line, hiding the proof of provenance.
// Re-checked here even though current clients filter at the source — a
// long-lived tab keeps running an old bundle.
if (isExtensionError(rawPayload)) {
diff --git a/server/services/clientErrors.test.js b/server/services/clientErrors.test.js
index 5a49a280da..82a0f3f0a8 100644
--- a/server/services/clientErrors.test.js
+++ b/server/services/clientErrors.test.js
@@ -216,23 +216,39 @@ describe('recordClientError', () => {
expect(review.createItem.mock.calls[0][0].title).toContain('Genuine PortOS failure');
});
- it('still reports an extension frame that sits past the stack truncation limit', async () => {
- // Detection runs on the RAW payload for this reason — sanitize() caps
- // the stack at 4000 chars, which would cut off the only proof of
- // provenance and let the error through as if it were ours.
- const deepStack = [
- 'TypeError: boom',
- ...Array.from({ length: 200 }, (_, i) => ` at frame${i} (https://portos/assets/index.js:${i}:1)`),
- ' at inject (chrome-extension://abcdefghijklmnop/inpage.js:1:1)',
+ it('still detects the frame when truncation would have cut it off', async () => {
+ // Why detection runs on the RAW payload: sanitize() caps the stack at
+ // MAX_STACK_CHARS (4000), so an unusually long first line pushes the
+ // originating frame past the cut and a post-sanitize check would let the
+ // extension error through as if it were ours.
+ const stack = [
+ `TypeError: ${'x'.repeat(5000)}`,
+ ' at inject (chrome-extension://examplewalletextensionid00000000/inpage.js:1:1)',
].join('\n');
- expect(deepStack.length).toBeGreaterThan(4000);
- const result = await recordClientError({ type: 'error', message: 'boom', stack: deepStack });
+ const result = await recordClientError({ type: 'error', message: 'boom', stack });
expect(result.reason).toBe('extension');
expect(review.createItem).not.toHaveBeenCalled();
});
+ it('does NOT drop a real PortOS error that merely ran through extension code', async () => {
+ // The extension frame is below ours, so PortOS threw it — it must reach
+ // the Review Hub rather than be silently swallowed.
+ const result = await recordClientError({
+ type: 'error',
+ message: "Cannot read properties of undefined (reading 'id')",
+ stack: [
+ "TypeError: Cannot read properties of undefined (reading 'id')",
+ ' at renderRow (https://portos/assets/index-abc.js:10:5)',
+ ' at wrappedFetch (chrome-extension://examplewalletextensionid00000000/inpage.js:1:1)',
+ ].join('\n'),
+ });
+
+ expect(result.accepted).toBe(true);
+ expect(review.createItem).toHaveBeenCalledTimes(1);
+ });
+
it('does NOT filter `crypto.randomUUID is not a function` — that is our own bug', async () => {
// Insecure-origin crash from PortOS's own code (see client/src/lib/uuid.js).
// It superficially resembles extension noise; filtering it would hide a