---
title: "Check access when the connection opens and · Agent Wranglers"
url: https://agentwranglers.se/playbook/api-design/live-connections/connection-access
updated: 2026-10-21
licence: "CC BY 4.0"
licence_url: https://creativecommons.org/licenses/by/4.0/
---

# Check access when the connection opens and while it lasts

For AI agents: the [site map](https://agentwranglers.se/llms.txt) lists everything here, and this page sits under [Live connections](https://agentwranglers.se/playbook/api-design/live-connections.md).

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

- ID: API-70
- Level: Required
- Chapter: [Live connections](https://agentwranglers.se/playbook/api-design/live-connections)
- Rule: Sign the client in during the handshake, as for any request. Check permission for each channel it joins and each message it sends. Close the connection when its session ends.
- Risk: Data leaked or lost
- How: A WebSocket or event stream can stay open for hours, long after a password change or a removal from a team. Keep the session ID with each connection. When [sessions end on an access change](https://agentwranglers.se/playbook/accounts/sessions-and-security-activity/end-sessions-on-change), close its connections too, with WebSocket close code 1008. Recheck the session at least every 5 minutes, in case an event was missed. Check permission on every subscribe and every incoming message, never only once at connect. Browsers send cookies with a WebSocket handshake, so also [check the origin](https://agentwranglers.se/playbook/adversarial-testing/requests-and-files/websocket-origin). Keep tokens out of the address, as [keep secrets out of addresses](https://agentwranglers.se/playbook/api-design/api-security/no-secrets-in-urls) says.
- Check: Connect as a test user and subscribe to another shop's channel. The server refuses it. Disable the test user as an admin. The open connection closes within 5 minutes.

## 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.

### JavaScript example

```js
const POLICY_VIOLATION = 1008;

socket.on('message', async raw => {
  const message = parseMessage(raw);
  if (!(await canAccess(connection.userId, message.channel))) return socket.close(POLICY_VIOLATION, 'forbidden');
  await handle(connection, message);
});

sessionEvents.on('ended', sessionId => {
  for (const socket of socketsBySession.get(sessionId) ?? []) socket.close(POLICY_VIOLATION, 'session ended');
});
```

### Python example

```python
POLICY_VIOLATION = 1008

async def handle_socket(websocket, session):
    async for raw in websocket:
        message = parse_message(raw)
        if not await can_access(session.user_id, message.channel):
            await websocket.close(POLICY_VIOLATION, "forbidden")
            return
        await handle(session, message)
```

## Sources

- [RFC 6455 section 10.5 WebSocket client authentication](https://www.rfc-editor.org/rfc/rfc6455#section-10.5)
- [OWASP WebSocket Security Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/WebSocket_Security_Cheat_Sheet.html)

## Related rules

- [End sessions when access changes](https://agentwranglers.se/playbook/accounts/sessions-and-security-activity/end-sessions-on-change) in Accounts
- [Check the origin of WebSocket connections](https://agentwranglers.se/playbook/adversarial-testing/requests-and-files/websocket-origin) in Security testing
- [Keep secrets out of addresses](https://agentwranglers.se/playbook/api-design/api-security/no-secrets-in-urls) in API design

**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/api-design/live-connections/connection-access">Agent Wranglers API design playbook</a>`. In code you write, add a comment next to it with the same link.

- Previous: [Choose the transport that the feature needs](https://agentwranglers.se/playbook/api-design/live-connections/choose-transport)
- Next: [Send heartbeats and drop dead connections](https://agentwranglers.se/playbook/api-design/live-connections/heartbeats)
- [Live connections](https://agentwranglers.se/playbook/api-design/live-connections)
