Skip to content

fix: bypass CORS for site adapters via about:blank execution#159

Open
SamCuipogobongo wants to merge 1 commit intoepiral:mainfrom
SamCuipogobongo:fix/cors-bypass-about-blank
Open

fix: bypass CORS for site adapters via about:blank execution#159
SamCuipogobongo wants to merge 1 commit intoepiral:mainfrom
SamCuipogobongo:fix/cors-bypass-about-blank

Conversation

@SamCuipogobongo
Copy link
Copy Markdown

Summary

Site adapters that make cross-origin API calls fail with TypeError: Failed to fetch because Runtime.evaluate runs in page context where browser CORS restrictions apply. This affects ~50% of adapters including hackernews/top, bbc/news, arxiv/search, stackoverflow/search, etc.

Root cause: After #132 removed the Extension and unified the daemon, the maintainer noted "CDP 层面处理了跨域 fetch", but Runtime.evaluate still executes JS in page context, which is subject to CORS. The issue was never actually fixed.

Changes

Two files, two complementary fixes:

1. cdp-discovery.ts — Add --disable-web-security to managed browser (1 line)

The managed browser is a dedicated Chrome instance with its own --user-data-dir, completely isolated from the user's daily browser. Adding --disable-web-security eliminates CORS enforcement on about:blank tabs (null origin), enabling cross-origin fetch() calls.

2. site.ts — Smart tab routing for adapters (51 lines)

  • Primary path: Adapters with capabilities: ["network"] (and without "cookie") are routed to an about:blank tab instead of the target domain tab. Combined with --disable-web-security, cross-origin fetch succeeds.
  • Fallback path: If any adapter fails with "Failed to fetch" on a domain tab, automatically retry on about:blank. This catches adapters that make cross-origin calls but don't declare capabilities: ["network"] (e.g. bbc/news, arxiv/search).
  • Cookie-dependent adapters (same-origin) continue to run on the domain tab as before — no behavior change.

Why not the other approaches?

Approach Issue
PR #15 (Page.setBypassCSP) Only bypasses CSP, not CORS. Also never restored, leaving user tabs permanently vulnerable
PR #45 (new Function() in Node) "network" capability is ambiguous — some adapters declare it but depend on document.cookie. Also exposes process/require/fs to adapter code
This PR (about:blank + --disable-web-security) Runs in browser sandbox (safe), no ambiguity (blank tab = no origin = no CORS), fallback catches unlabeled adapters

Test plan

Tested on macOS with Chrome 146 + bb-browser 0.11.2:

  • bb-browser site hackernews/top 3 — ✅ (was: Failed to fetch)
  • bb-browser site bbc/news — ✅ (was: Failed to fetch)
  • bb-browser site arxiv/search "LLM agent" — ✅ (was: Failed to fetch)
  • bb-browser site stackoverflow/search "async await" — ✅ (was: Failed to fetch)
  • bb-browser site wikipedia/summary "Python" — ✅ (was: Failed to fetch)
  • bb-browser eval "document.title" — ✅ regression check
  • bb-browser site zhihu/hot — needs login (expected, cookie-dependent adapter)

Fixes #41
Partially addresses #110 (cross-origin fetch portion)
Related: #104, #45

🤖 Generated with Claude Code

…ble-web-security

Site adapters that make cross-origin API calls (e.g. hackernews/top fetching
from firebaseio.com, bbc/news fetching from feeds.bbci.co.uk) fail with
"TypeError: Failed to fetch" because Runtime.evaluate runs in page context
where browser CORS restrictions apply.

Two complementary fixes:

1. Add --disable-web-security to managed browser launch args. This flag only
   affects the dedicated bb-browser Chrome instance (separate user-data-dir),
   not the user's daily browser. On about:blank tabs (null origin), this
   completely eliminates CORS enforcement.

2. Route adapters with capabilities: ["network"] to an about:blank tab instead
   of the target domain tab. Combined with (1), cross-origin fetch() calls
   succeed without CORS restrictions. Adapters requiring cookies (same-origin)
   continue to run on the domain tab as before.

3. Auto-retry fallback: if any adapter fails with "Failed to fetch" on a
   domain tab, automatically retry on an about:blank tab. This catches
   adapters that make cross-origin calls but don't declare capabilities:
   ["network"].

Tested: hackernews/top, bbc/news, arxiv/search, stackoverflow/search,
wikipedia/summary — all return valid JSON after this fix.

Fixes epiral#41
Closes epiral#110 (跨域 fetch 部分)
Related: epiral#104, epiral#45

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Site adapters fail with 'Failed to fetch' while eval works fine

1 participant