Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .changes/portal-ws-origin-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
kind: security
---
Reject portal WebSocket connections from other websites

## Release Note
Other websites can no longer control the hub through the setup portal

A web page open on a phone or computer joined to the hub's setup network could connect to the portal and send shocker commands. The portal now only accepts connections from its own page.

- The /ws handshake checks the Origin header: allowed are no Origin (non-browser clients), the portal's own origin and a loopback origin (the frontend dev server)
- Other origins get 403 Forbidden
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ const char* const TAG = "CaptivePortalWs";
#include "message_handlers/WebSocket.h"
#include "rfc8908/RFC8908Handler.h"
#include "serialization/WSLocal.h"
#include "StringHelpers.h"
#include "wifi/WiFiManager.h"
#include "wifi/WiFiScanManager.h"

#include <string_view>
#include <utility>

// Without it httpd_uri_t has no ws_pre_handshake_cb. sdkconfig.defaults enables it, but an existing sdkconfig wins over
Expand Down Expand Up @@ -144,6 +146,40 @@ void CaptivePortal::WebSocketEndpoint::wsSessCtxFree(void* ctx)
delete c;
}

// Browsers let any page open a WebSocket to any host, so the Origin header is the only thing telling the portal's own
// page apart from another site loaded on the same client. Allowed: no Origin (not a browser), the page's own origin
// (http:// plus the Host it asked for, which IsForeignHost has already confined to an IP literal), and a loopback origin
// (the frontend dev server, which talks to the hub from localhost).
static bool IsAllowedOrigin(httpd_req_t* req)
{
char origin[sizeof("http://255.255.255.255:65535")];
esp_err_t err = httpd_req_get_hdr_value_str(req, "Origin", origin, sizeof(origin));
if (err == ESP_ERR_NOT_FOUND) {
return true;
}
if (err != ESP_OK) {
return false; // Too long to be one we allow
}

std::string_view originView(origin);
if (!StringHasPrefix(originView, "http://")) {
return false;
}
std::string_view originHost = originView.substr(sizeof("http://") - 1);

std::string_view originName = StringBeforeFirst(originHost, ':');
if (originName == "localhost" || originName == "127.0.0.1") {
return true;
}

char host[sizeof("255.255.255.255:65535")];
if (httpd_req_get_hdr_value_str(req, "Host", host, sizeof(host)) != ESP_OK) {
return false;
}

return originHost == std::string_view(host);
}

// httpd does not call the URI handler for the handshake (esp_http_server's httpd_uri.c), only this callback, right
// after it has switched the socket to WebSocket: allocate an id, arm the disconnect hook and greet the client.
// The /ws route is registered directly (it needs the websocket fields), so it does its own Host check, here before
Expand All @@ -155,6 +191,11 @@ esp_err_t CaptivePortal::WebSocketEndpoint::wsPreHandshake(httpd_req_t* req)
RFC8908::EmitRedirect(req);
return ESP_FAIL;
}
if (!IsAllowedOrigin(req)) {
OS_LOGW(TAG, "Rejecting WebSocket from another origin on fd %d", httpd_req_to_sockfd(req));
httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, nullptr);
return ESP_FAIL;
}
return ESP_OK;
}

Expand Down
Loading