GioJSdocs
On this page

[security.websocket]

The Origin check on WebSocket upgrades, which stops other websites from opening sockets with your visitors' cookies.

gio.toml
[security.websocket]
check_origin = true      # the default

Browsers let any page open a WebSocket to any server and send that server's cookies with it (cross-site WebSocket hijacking). The upgrade is a GET, so CSRF protection alone would not cover it: GioJS judges every upgrade like an unsafe request instead.

Reference

KeyDefaultDescription
check_originbooleantrueCheck the Origin (and Sec-Fetch-Site) of every WebSocket upgrade with the rules of [security.csrf]: your own host, an origin in [security.csrf] trusted_origins, or a client with neither header (not a browser) is accepted; anything else gets 403 before the connection is upgraded.0 / false / empty: Upgrades from any website are accepted. Warns.

Behavior

  • Independent of [security.csrf] enabled: turning CSRF protection off because your forms carry tokens leaves this check on, since form tokens do nothing for WebSockets.
  • It shares the CSRF lists: trusted_origins are accepted here too, and exempt paths skip this check as well - exempt a public WebSocket API meant to be used from any site.
  • A refusal is a 403 with a plain-text body naming the setting to change (cross-site WebSocket upgrade blocked by CSRF protection - ...).

Startup warnings

WhenStartup warning
check_origin = false[security.websocket] check_origin = false: any website can open WebSockets to this server with your visitors' cookies - prefer listing origins in [security.csrf] trusted_origins, or public endpoints in [security.csrf] exempt

Examples

A public WebSocket API

Keep the check for the app's own sockets and open one path to every site:

gio.toml
[security.csrf]
exempt = ["/api/public-feed"]

A client on another origin

gio.toml
[security.csrf]
trusted_origins = ["https://dashboard.example.com"]

Good to know

  • A socket accepted here still has to pass its route's wsHandler, which can check the session and refuse it. See Authenticating connections.
  • With [websocket] enabled = false upgrades are answered 501. This check still runs before that, so a cross-site upgrade gets 403.

Version history

VersionChanges
v0.1.0-beta.8Introduced, on by default; turning it off logs a startup warning.