Tighten WebSocket auth close codes#7664
Conversation
Greptile SummaryThis PR differentiates WebSocket authentication close codes by mapping Firebase auth errors to 4001 (token refresh needed), 4004 (re-login required), and 1008 (generic failure), and adds matching telemetry labels on the Dart client side.
Confidence Score: 3/5Safe for the happy-path and string-fallback cases, but a real CertificateFetchError silently returns 1008 instead of the intended 4001, so clients will not know to refresh their token in that scenario. The core logic routing ExpiredIdTokenError to 4001 and RevokedIdTokenError to 4004 is correct and tested. However, CertificateFetchError inherits from UnknownError not InvalidIdTokenError, making its isinstance branch dead code. The test that appears to cover this path exercises a completely different branch via string matching, masking the gap. backend/utils/other/endpoints.py — the _verify_ws_auth and _get_ws_auth_close functions need attention for the unreachable CertificateFetchError branch. Important Files Changed
Sequence DiagramsequenceDiagram
participant Client as Flutter Client
participant Auth as _verify_ws_auth
participant Close as _get_ws_auth_close
participant FB as firebase_admin
Client->>Auth: WS connect (Authorization header)
alt No or malformed header
Auth-->>Client: close 1008
else Valid format
Auth->>FB: verify_token(token)
alt RevokedIdTokenError (subclass of InvalidIdTokenError)
FB-->>Auth: raises RevokedIdTokenError
Auth->>Close: _get_ws_auth_close(e)
Close-->>Auth: 4004
Auth-->>Client: close 4004 (re-login required)
else ExpiredIdTokenError (subclass of InvalidIdTokenError)
FB-->>Auth: raises ExpiredIdTokenError
Auth->>Close: _get_ws_auth_close(e)
Close-->>Auth: 4001
Auth-->>Client: close 4001 (token refresh)
else Generic InvalidIdTokenError
FB-->>Auth: raises InvalidIdTokenError
Auth->>Close: _get_ws_auth_close(e)
Close-->>Auth: 1008 or 4001 via string match
Auth-->>Client: close 1008 or 4001
else CertificateFetchError (NOT subclass of InvalidIdTokenError)
FB-->>Auth: raises CertificateFetchError
Note over Auth: Caught by except Exception, not InvalidIdTokenError
Auth-->>Client: close 1008 instead of intended 4001
else Success
FB-->>Auth: uid string
Auth-->>Client: connection accepted
end
end
|
kodjima33
left a comment
There was a problem hiding this comment.
Differentiated WebSocket auth close codes — mixed backend+app/dart, approve only per policy.
9270432 to
8d9e219
Compare
|
Rebased and force-pushed onto current Greptile follow-up addressed in the current branch:
Backend verification passes: |
kodjima33
left a comment
There was a problem hiding this comment.
re-approved on new sha: backend+dart-client (distinct WS auth close codes + close-code labels) — approve only per policy
8d9e219 to
53e0b16
Compare
kodjima33
left a comment
There was a problem hiding this comment.
Re-approved on new commits — backend (approve-only area).
53e0b16 to
d708831
Compare
d708831 to
41859b1
Compare
kodjima33
left a comment
There was a problem hiding this comment.
Tighten WebSocket auth close codes — approve only, auth area (re-approve)
Summary
utils.other.endpointsCurrent status
mainat1a5824403b68ce47c3b0909577cadc1242ba0d3f41859b19a479831be67953776b910d770e4a3dd2Verification
D:\codex-omi-work\.venvs\omi-backend-vad-refresh\Scripts\python.exe -m pytest backend\tests\unit\test_ws_auth_handshake.py -q --tb=shortD:\codex-omi-work\.venvs\omi-backend-vad-refresh\Scripts\python.exe -m py_compile backend\utils\other\endpoints.py backend\tests\unit\test_ws_auth_handshake.pyD:\codex-omi-work\.venvs\omi-backend-vad-refresh\Scripts\python.exe -m black --check backend\utils\other\endpoints.py backend\tests\unit\test_ws_auth_handshake.pyD:\codex-omi-work\.tools\dart-sdk\bin\dart.exe format --set-exit-if-changed app\lib\services\sockets\pure_socket.dartflutter_lintspackage-resolution warning because app package dependencies are not installed in this Windows worktree.git diff --check origin/main...HEADscripts/pre-commitwith the backend Windows venv and local Dart SDK onPATH