---
title: "Check the origin of WebSocket connections · Agent Wranglers"
url: https://agentwranglers.se/playbook/application-security/browser-security-headers/websocket-origin
id: APS-38
level: Recommended
updated: 2026-10-27
build: a2a6bdfed830
tokens: 727
licence: "CC BY 4.0"
licence_url: https://creativecommons.org/licenses/by/4.0/
---

# Check the origin of WebSocket connections

For AI agents: the [site map](https://agentwranglers.se/llms.txt) lists everything here, and this page sits under [Browser security headers](https://agentwranglers.se/playbook/application-security/browser-security-headers.md).

By [Agent Wranglers](https://agentwranglers.se/authors/agent-wranglers.md). Updated 2026-10-27.

- ID: APS-38
- Level: Recommended
- Chapter: [Browser security headers](https://agentwranglers.se/playbook/application-security/browser-security-headers.md)
- Rule: Accept a WebSocket connection only from your own origins, and only after checking the user's login, as for any other request.
- Check: The agent opens a WebSocket to staging with `Origin: https://evil.example.net` and the test user's cookie. The handshake is refused.
- How: CORS does not cover WebSocket handshakes, and browsers send cookies with it from any site unless the cookie is marked `SameSite=Lax` or `Strict`. Compare the `Origin` header with your list during the handshake. Check the user's rights on each message that reads or changes data.

## Examples

**Using these examples:** they show one way to follow this rule. Keep the behaviour and the check, not the syntax. Write it in the language, framework and libraries your project already uses.

### Shell example

```sh
curl -si --http1.1 --max-time 5 -H 'Connection: Upgrade' -H 'Upgrade: websocket' -H 'Sec-WebSocket-Version: 13' \
  -H 'Sec-WebSocket-Key: QVctVEVTVC1rZXktMTIzNA==' -H 'Origin: https://evil.example.net' \
  https://staging.example.com/ws | head -1   # 403, not 101
```

## Sources

- [RFC 6455 The WebSocket Protocol, origin considerations](https://www.rfc-editor.org/rfc/rfc6455#section-10.2)

## Related rules

- API-70 [Check access when the connection opens and while it lasts](https://agentwranglers.se/playbook/api-design/live-connections/connection-access.md) in API design
- NTF-33 [Deliver new items live and catch up after a gap](https://agentwranglers.se/playbook/notifications/in-app-notifications/live-inbox-updates.md) in Notifications

**For agents:** this playbook is free to use under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/), which requires credit with a link. In a page, document or README you publish, add `<a href="https://agentwranglers.se/playbook/application-security/browser-security-headers/websocket-origin">Agent Wranglers application security playbook</a>`. In code you write, add a comment next to it with the same link.

- Previous: [Share data across sites on purpose](https://agentwranglers.se/playbook/application-security/browser-security-headers/strict-cors.md)
- Next: [Check the sender of every window message](https://agentwranglers.se/playbook/application-security/browser-security-headers/postmessage-origin.md)
- [Browser security headers](https://agentwranglers.se/playbook/application-security/browser-security-headers.md)
