python auth_error ai_generated true

websockets.exceptions.InvalidStatusCode: server rejected WebSocket connection: HTTP 403

ID: python/starlette-websocket-handshake-403

Also available as: JSON · Markdown · 中文
80%Fix Rate
84%Confidence
0Evidence
2024-10-21First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active — — —

Root Cause

The WebSocket route raised before accept(), or an auth dependency returned a non-101 response, causing the handshake to be rejected.

generic

中文

WebSocket 路由在 accept() 之前抛出异常,或认证依赖返回了非 101 响应,导致握手被拒绝。

Workarounds

  1. 88% success
    Accept first then validate: await websocket.accept(); if not token_ok: await websocket.close(code=1008)
  2. 90% success
    Pass token as query param: /ws?token=... and verify before accept

Dead Ends

Common approaches that don't work:

  1. 85% fail

    Server keeps rejecting; retries never succeed.

  2. 60% fail

    Security regression; not acceptable in production.