Skip to content

Gateway: enforce origin checks for browser-context WS clients#23352

Closed
bmendonca3 wants to merge 2 commits into
openclaw:mainfrom
bmendonca3:pr-01-browser-origin-enforcement
Closed

Gateway: enforce origin checks for browser-context WS clients#23352
bmendonca3 wants to merge 2 commits into
openclaw:mainfrom
bmendonca3:pr-01-browser-origin-enforcement

Conversation

@bmendonca3

@bmendonca3 bmendonca3 commented Feb 22, 2026

Copy link
Copy Markdown
Contributor

Summary

  • enforce browser origin validation for any websocket client that either sends an Origin header or declares a browser client mode (ui/webchat)
  • keep the Control UI-specific remediation message while returning a generic origin not allowed error for other clients
  • add unit coverage for the new origin-enforcement gate logic
  • add auth e2e coverage for non-Control-UI browser-mode clients across Origin missing, null, mismatched, and matching-host cases

Testing

  • pnpm test src/gateway/origin-check.test.ts
  • pnpm test:e2e src/gateway/server.auth.e2e.test.ts
  • pnpm check

Greptile Summary

This PR enhances browser origin validation for WebSocket clients by introducing a new shouldCheckBrowserOrigin gate that enforces origin checks for any client that either sends an Origin header OR declares a browser client mode (ui/webchat). The implementation correctly separates the decision logic (when to check) from the validation logic (what to check), making the security boundary more explicit and testable.

Key security improvements:

  • Extends origin validation beyond Control UI to all browser-context clients (any client with mode: ui or mode: webchat)
  • Enforces checks for any WebSocket client that sends an Origin header, regardless of declared mode
  • Maintains Control UI-specific error messaging while returning generic "origin not allowed" for other clients
  • Adds comprehensive e2e test coverage for non-Control-UI browser clients across missing, null, mismatched, and matching origin cases

Platform security hardening:

  • Android: Requires TLS for non-loopback gateway connections with clear status errors for manual plaintext attempts; excludes openclaw/identity from cloud backup and device transfer to prevent identity material leakage
  • iOS: Refactors shouldForceTLSshouldRequireTLS and adds resolveManualUseTLS wrapper for clearer TLS enforcement logic
  • Web UI: Validates gateway socket URLs before connection attempts using the shared getGatewaySocketUrlSecurityError helper

The changes align well with the codebase's existing security patterns and maintain backward compatibility for legitimate use cases.

Confidence Score: 4/5

  • This PR is safe to merge with one minor test coverage improvement recommended
  • The implementation is well-designed with clear separation of concerns, comprehensive e2e test coverage, and consistent security hardening across platforms. The only gap is a missing unit test for WEBCHAT mode in shouldCheckBrowserOrigin, which is a minor issue since the e2e tests cover Control UI (which uses WEBCHAT mode) extensively. The security logic is sound and follows the repository's established patterns.
  • No files require special attention - the implementation is solid across all modified files

Last reviewed commit: 3577aef

@bmendonca3

Copy link
Copy Markdown
Contributor Author

Closing temporarily per request for state reset.

@bmendonca3

Copy link
Copy Markdown
Contributor Author

Reopening per request after validation.

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation channel: googlechat Channel integration: googlechat channel: nextcloud-talk Channel integration: nextcloud-talk channel: voice-call Channel integration: voice-call app: android App: android app: ios App: ios app: macos App: macos app: web-ui App: web-ui gateway Gateway runtime cli CLI command changes size: XL labels Feb 22, 2026

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

60 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

Comment on lines +72 to +77
it("enforces origin checks for UI mode without Origin", () => {
const result = shouldCheckBrowserOrigin({
clientMode: GATEWAY_CLIENT_MODES.UI,
});
expect(result).toBe(true);
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

missing test for WEBCHAT mode

shouldCheckBrowserOrigin enforces checks for both UI and WEBCHAT modes (origin-check.ts:63), but only UI mode is tested here. Add test coverage for WEBCHAT mode enforcement.

Suggested change
it("enforces origin checks for UI mode without Origin", () => {
const result = shouldCheckBrowserOrigin({
clientMode: GATEWAY_CLIENT_MODES.UI,
});
expect(result).toBe(true);
});
it("enforces origin checks for UI mode without Origin", () => {
const result = shouldCheckBrowserOrigin({
clientMode: GATEWAY_CLIENT_MODES.UI,
});
expect(result).toBe(true);
});
it("enforces origin checks for WEBCHAT mode without Origin", () => {
const result = shouldCheckBrowserOrigin({
clientMode: GATEWAY_CLIENT_MODES.WEBCHAT,
});
expect(result).toBe(true);
});
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/gateway/origin-check.test.ts
Line: 72-77

Comment:
missing test for `WEBCHAT` mode

`shouldCheckBrowserOrigin` enforces checks for both `UI` and `WEBCHAT` modes (origin-check.ts:63), but only `UI` mode is tested here. Add test coverage for `WEBCHAT` mode enforcement.

```suggestion
  it("enforces origin checks for UI mode without Origin", () => {
    const result = shouldCheckBrowserOrigin({
      clientMode: GATEWAY_CLIENT_MODES.UI,
    });
    expect(result).toBe(true);
  });

  it("enforces origin checks for WEBCHAT mode without Origin", () => {
    const result = shouldCheckBrowserOrigin({
      clientMode: GATEWAY_CLIENT_MODES.WEBCHAT,
    });
    expect(result).toBe(true);
  });
```

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

@openclaw-barnacle openclaw-barnacle Bot added size: S and removed docs Improvements or additions to documentation channel: googlechat Channel integration: googlechat channel: nextcloud-talk Channel integration: nextcloud-talk channel: voice-call Channel integration: voice-call app: android App: android app: ios App: ios app: macos App: macos app: web-ui App: web-ui cli CLI command changes size: XL labels Feb 22, 2026
@vincentkoc

Copy link
Copy Markdown
Member

you have been detected be spamming with unwarranted prs and issues and your issues and prs have been automatically closed.

@vincentkoc vincentkoc closed this Feb 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gateway Gateway runtime size: S