Skip to content

fix(captive-portal): check the Origin header on the portal WebSocket - #597

Open
hhvrc wants to merge 1 commit into
developfrom
fix/portal-ws-origin-check
Open

hhvrc wants to merge 1 commit into
developfrom
fix/portal-ws-origin-check

Conversation

@hhvrc

@hhvrc hhvrc commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

What does this change?

The portal's /ws pre-handshake callback only rejected requests whose Host header is a name (RFC8908::IsForeignHost) and never looked at the Origin header. Browsers do not apply the same-origin policy to WebSocket connections, so any web page loaded on a client joined to the setup AP (for example a phone that also has cellular data) could open ws://<AP IP>/ws and send ShockerCommandList messages or read hub state.

The handshake now also checks the Origin header. It is accepted when:

  • there is no Origin header (not a browser; browsers always send one on a WebSocket handshake),
  • the Origin is http:// plus the request's own Host (the portal page itself; the Host check already confines Host to an IP literal, so DNS rebinding cannot match), or
  • the Origin host is localhost or 127.0.0.1 on any port (the frontend dev server, which getDeviceHostname() points at 4.3.2.1).

Any other Origin, including null, https:// and anything longer than an IPv4 literal with a port, gets 403 Forbidden and the connection is closed.

Resolves code scanning alert #8 (CWE-1385).

This touches device control and authentication: it gates who can reach the local command path. The portal page itself connects exactly as before.

How was it tested?

  • Formatted with clang-format 21 (the CI version).
  • Not yet built or run on hardware. To check: the portal at http://4.3.2.1/ still connects; the frontend dev server on localhost still connects; a WebSocket opened from another origin (e.g. a browser console on another site while joined to the AP) is refused with 403.

Checklist

  • The change is focused on one thing.
  • Tests, formatter and linter pass locally.
  • Documentation is updated where behaviour changed.
  • No secrets, credentials or personal data are included.
  • If this touches device control, limits, sharing, authentication or firmware updates, the description says so and explains how it was tested.

Browsers do not apply the same-origin policy to WebSocket connections, so
any page loaded on a client joined to the setup AP could open /ws and send
shocker commands. The pre-handshake callback only checked the Host header.

Accept a handshake only with no Origin (not a browser), the page's own
origin (http:// plus the Host, already confined to an IP literal) or a
loopback origin (the frontend dev server). Others get 403.

Resolves code scanning alert #8 (CWE-1385).
@coderabbitai

coderabbitai Bot commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 59 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 4cfd5e27-a940-4b4d-863b-bb7a8d932695
Commits

Reviewing files that changed from the base of the PR and between d416e19 and 0e4dc80.

Files selected for processing: 2

  • .changes/portal-ws-origin-check.md
  • components/captive_portal/src/captiveportal/WebSocketEndpoint.cpp
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@github-actions

github-actions Bot commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

Change file check

✅ OK: 1 valid change file(s) added in this PR.

Version impact: patch bump (fix). The next release stays 2.0.0 (latest stable: 1.6.0).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

1 participant