Skip to content

fix: handle CORS preflight OPTIONS requests in browser relay#23962

Closed
miloudbelarebia wants to merge 1 commit intoopenclaw:mainfrom
miloudbelarebia:fix/relay-cors-preflight
Closed

fix: handle CORS preflight OPTIONS requests in browser relay#23962
miloudbelarebia wants to merge 1 commit intoopenclaw:mainfrom
miloudbelarebia:fix/relay-cors-preflight

Conversation

@miloudbelarebia
Copy link
Contributor

@miloudbelarebia miloudbelarebia commented Feb 22, 2026

Summary

  • Add OPTIONS handler in browser relay HTTP server for CORS preflight
  • Return Access-Control-Allow-Origin, Access-Control-Allow-Methods, Access-Control-Allow-Headers with the relay auth header name

Test plan

  • Verify Chrome extension can make cross-origin requests to relay
  • Verify OPTIONS preflight returns 204 with correct headers

Fixes #23842

Greptile Summary

Added CORS preflight (OPTIONS) handler to the browser relay HTTP server. The implementation returns 204 status with Access-Control-Allow-Origin (echoing the request origin or *), Access-Control-Allow-Methods, Access-Control-Allow-Headers (including the relay auth header), and Access-Control-Max-Age headers. This enables Chrome extensions to make cross-origin requests to the relay server.

Confidence Score: 4/5

  • Safe to merge with minor consideration for origin validation
  • The CORS preflight implementation is correctly positioned and follows standard patterns (204 response, proper headers). However, the OPTIONS handler accepts any origin without validation, unlike the WebSocket upgrade handler which validates chrome-extension:// origins on line 510. While this may be intentional for flexibility, it creates an inconsistency in security posture.
  • No files require special attention

Last reviewed commit: b3ef610

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 file reviewed, 1 comment

Edit Code Review Agent Settings | Greptile


// Handle CORS preflight requests from the browser extension.
if (req.method === "OPTIONS") {
const origin = getHeader(req, "origin") ?? "*";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OPTIONS handler accepts any origin, but WebSocket upgrade (line 510) validates chrome-extension:// prefix - consider same validation here for consistency

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/browser/extension-relay.ts
Line: 369

Comment:
OPTIONS handler accepts any origin, but WebSocket upgrade (line 510) validates `chrome-extension://` prefix - consider same validation here for consistency

<sub>Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!</sub>

How can I resolve this? If you propose a fix, please make it concise.

Add OPTIONS handler with proper CORS headers for the browser
extension relay. Validate the Origin header against the
chrome-extension:// prefix, consistent with the WebSocket
upgrade path, to reject cross-origin preflight from
non-extension origins.

Fixes openclaw#23842
@miloudbelarebia miloudbelarebia force-pushed the fix/relay-cors-preflight branch from b3ef610 to 9a77fe4 Compare February 23, 2026 08:41
steipete added a commit that referenced this pull request Feb 26, 2026
Reworks browser relay CORS handling for extension-origin preflight and JSON responses, adds regression tests, and updates changelog.
Landed from contributor @miloudbelarebia (PR #23962).

Co-authored-by: Miloud Belarebia <miloudbelarebia@users.noreply.github.com>
@steipete
Copy link
Contributor

Landed on main via 4c75eca58.

What I changed while landing:

  • kept your preflight/auth ordering fix intent
  • expanded CORS handling to include extension-origin CORS headers on relay HTTP JSON responses
  • added regression tests for extension-origin OPTIONS preflight + non-extension rejection + response CORS header behavior
  • added changelog entry referencing PR fix: handle CORS preflight OPTIONS requests in browser relay #23962

SHA mapping:

  • original PR commit: 9a77fe4f3137e11f129bc1fee7d9e11daeda2428
  • landed commit: 4c75eca58

Thanks for the fix, @miloudbelarebia.

@steipete steipete closed this Feb 26, 2026
wanjizheng pushed a commit to wanjizheng/openclaw that referenced this pull request Feb 27, 2026
Reworks browser relay CORS handling for extension-origin preflight and JSON responses, adds regression tests, and updates changelog.
Landed from contributor @miloudbelarebia (PR openclaw#23962).

Co-authored-by: Miloud Belarebia <miloudbelarebia@users.noreply.github.com>
wanjizheng pushed a commit to wanjizheng/openclaw that referenced this pull request Feb 27, 2026
Reworks browser relay CORS handling for extension-origin preflight and JSON responses, adds regression tests, and updates changelog.
Landed from contributor @miloudbelarebia (PR openclaw#23962).

Co-authored-by: Miloud Belarebia <miloudbelarebia@users.noreply.github.com>
(cherry picked from commit 58b1edf)
wanjizheng pushed a commit to wanjizheng/openclaw that referenced this pull request Feb 27, 2026
Reworks browser relay CORS handling for extension-origin preflight and JSON responses, adds regression tests, and updates changelog.
Landed from contributor @miloudbelarebia (PR openclaw#23962).

Co-authored-by: Miloud Belarebia <miloudbelarebia@users.noreply.github.com>
(cherry picked from commit 58b1edf)
wanjizheng pushed a commit to wanjizheng/openclaw that referenced this pull request Feb 27, 2026
Reworks browser relay CORS handling for extension-origin preflight and JSON responses, adds regression tests, and updates changelog.
Landed from contributor @miloudbelarebia (PR openclaw#23962).

Co-authored-by: Miloud Belarebia <miloudbelarebia@users.noreply.github.com>
(cherry picked from commit 58b1edf)
wanjizheng pushed a commit to wanjizheng/openclaw that referenced this pull request Feb 27, 2026
Reworks browser relay CORS handling for extension-origin preflight and JSON responses, adds regression tests, and updates changelog.
Landed from contributor @miloudbelarebia (PR openclaw#23962).

Co-authored-by: Miloud Belarebia <miloudbelarebia@users.noreply.github.com>
(cherry picked from commit 58b1edf)
wanjizheng pushed a commit to wanjizheng/openclaw that referenced this pull request Feb 27, 2026
Reworks browser relay CORS handling for extension-origin preflight and JSON responses, adds regression tests, and updates changelog.
Landed from contributor @miloudbelarebia (PR openclaw#23962).

Co-authored-by: Miloud Belarebia <miloudbelarebia@users.noreply.github.com>
(cherry picked from commit 58b1edf)
execute008 pushed a commit to execute008/openclaw that referenced this pull request Feb 27, 2026
Reworks browser relay CORS handling for extension-origin preflight and JSON responses, adds regression tests, and updates changelog.
Landed from contributor @miloudbelarebia (PR openclaw#23962).

Co-authored-by: Miloud Belarebia <miloudbelarebia@users.noreply.github.com>
r4jiv007 pushed a commit to r4jiv007/openclaw that referenced this pull request Feb 28, 2026
Reworks browser relay CORS handling for extension-origin preflight and JSON responses, adds regression tests, and updates changelog.
Landed from contributor @miloudbelarebia (PR openclaw#23962).

Co-authored-by: Miloud Belarebia <miloudbelarebia@users.noreply.github.com>
mylukin pushed a commit to mylukin/openclaw that referenced this pull request Feb 28, 2026
Reworks browser relay CORS handling for extension-origin preflight and JSON responses, adds regression tests, and updates changelog.
Landed from contributor @miloudbelarebia (PR openclaw#23962).

Co-authored-by: Miloud Belarebia <miloudbelarebia@users.noreply.github.com>
wanjizheng pushed a commit to wanjizheng/openclaw that referenced this pull request Feb 28, 2026
Reworks browser relay CORS handling for extension-origin preflight and JSON responses, adds regression tests, and updates changelog.
Landed from contributor @miloudbelarebia (PR openclaw#23962).

Co-authored-by: Miloud Belarebia <miloudbelarebia@users.noreply.github.com>
(cherry picked from commit 58b1edf)
wanjizheng pushed a commit to wanjizheng/openclaw that referenced this pull request Feb 28, 2026
Reworks browser relay CORS handling for extension-origin preflight and JSON responses, adds regression tests, and updates changelog.
Landed from contributor @miloudbelarebia (PR openclaw#23962).

Co-authored-by: Miloud Belarebia <miloudbelarebia@users.noreply.github.com>
(cherry picked from commit 58b1edf)
wanjizheng pushed a commit to wanjizheng/openclaw that referenced this pull request Feb 28, 2026
Reworks browser relay CORS handling for extension-origin preflight and JSON responses, adds regression tests, and updates changelog.
Landed from contributor @miloudbelarebia (PR openclaw#23962).

Co-authored-by: Miloud Belarebia <miloudbelarebia@users.noreply.github.com>
(cherry picked from commit 58b1edf)
wanjizheng pushed a commit to wanjizheng/openclaw that referenced this pull request Feb 28, 2026
Reworks browser relay CORS handling for extension-origin preflight and JSON responses, adds regression tests, and updates changelog.
Landed from contributor @miloudbelarebia (PR openclaw#23962).

Co-authored-by: Miloud Belarebia <miloudbelarebia@users.noreply.github.com>
(cherry picked from commit 58b1edf)
vincentkoc pushed a commit to Sid-Qin/openclaw that referenced this pull request Feb 28, 2026
Reworks browser relay CORS handling for extension-origin preflight and JSON responses, adds regression tests, and updates changelog.
Landed from contributor @miloudbelarebia (PR openclaw#23962).

Co-authored-by: Miloud Belarebia <miloudbelarebia@users.noreply.github.com>
vincentkoc pushed a commit to rylena/rylen-openclaw that referenced this pull request Feb 28, 2026
Reworks browser relay CORS handling for extension-origin preflight and JSON responses, adds regression tests, and updates changelog.
Landed from contributor @miloudbelarebia (PR openclaw#23962).

Co-authored-by: Miloud Belarebia <miloudbelarebia@users.noreply.github.com>
@miloudbelarebia
Copy link
Contributor Author

Thanks @steipete — glad the fix was useful. The expanded CORS handling + regression tests look solid. Appreciate the changelog reference too.

steipete added a commit to Sid-Qin/openclaw that referenced this pull request Mar 2, 2026
Reworks browser relay CORS handling for extension-origin preflight and JSON responses, adds regression tests, and updates changelog.
Landed from contributor @miloudbelarebia (PR openclaw#23962).

Co-authored-by: Miloud Belarebia <miloudbelarebia@users.noreply.github.com>
robertchang-ga pushed a commit to robertchang-ga/openclaw that referenced this pull request Mar 2, 2026
Reworks browser relay CORS handling for extension-origin preflight and JSON responses, adds regression tests, and updates changelog.
Landed from contributor @miloudbelarebia (PR openclaw#23962).

Co-authored-by: Miloud Belarebia <miloudbelarebia@users.noreply.github.com>
dorgonman pushed a commit to kanohorizonia/openclaw that referenced this pull request Mar 3, 2026
Reworks browser relay CORS handling for extension-origin preflight and JSON responses, adds regression tests, and updates changelog.
Landed from contributor @miloudbelarebia (PR openclaw#23962).

Co-authored-by: Miloud Belarebia <miloudbelarebia@users.noreply.github.com>
zooqueen pushed a commit to hanzoai/bot that referenced this pull request Mar 6, 2026
Reworks browser relay CORS handling for extension-origin preflight and JSON responses, adds regression tests, and updates changelog.
Landed from contributor @miloudbelarebia (PR openclaw#23962).

Co-authored-by: Miloud Belarebia <miloudbelarebia@users.noreply.github.com>
thebenjaminlee pushed a commit to escape-velocity-ventures/openclaw that referenced this pull request Mar 7, 2026
Reworks browser relay CORS handling for extension-origin preflight and JSON responses, adds regression tests, and updates changelog.
Landed from contributor @miloudbelarebia (PR openclaw#23962).

Co-authored-by: Miloud Belarebia <miloudbelarebia@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Browser relay (port 18792) rejects CORS preflight OPTIONS requests — Chrome extension can't authenticate

2 participants