Repository navigation
fix(plugins): redact API keys from rejected websocket handshakes - #7032
Conversation
WSServerHandshakeError subclasses ClientResponseError, so it is neither a ClientConnectorError nor an asyncio.TimeoutError. Six credentialed ws_connect sites caught only those two, letting a 401/403 upgrade escape carrying RequestInfo, whose headers hold the key. Apply the shape used for cartesia in livekit#6740: handle the response error separately and re-raise with 'from None', surfacing only the status and the exception type. Affects deepgram (stt, stt_v2), simplismart, smallestai and xai (stt, tts). Fixes livekit#7031
There was a problem hiding this comment.
Note
This report is out of date. Scroll down for Devin Review's latest report on this PR.
🔍 Devin Review: 1 flag
Not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)
…logging The catch-all added for credential redaction also covered the statements that run after a successful connect, including a read of the private ws._response attribute. An AttributeError there would have been converted into a retryable APIConnectionError, masking the bug and spinning the reconnect loop. Narrow each try to the connect call, matching the scope cartesia guards in livekit#6740.
There was a problem hiding this comment.
Devin Review found 1 new potential issue.
1 flag not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)
| ws_headers = { | ||
| k: v for k, v in ws._response.headers.items() if k.startswith("dg-") or k == "Date" | ||
| } | ||
| logger.debug( | ||
| "Established new Deepgram STT WebSocket connection:", | ||
| extra={"headers": ws_headers}, |
|
Pushed faf17dd after a second look at the scope of the catch-all. The first commit kept each plugin's existing Two things I checked while I was in here, in case they come up in review.
Redaction and lint are unchanged after the narrowing: 0 unguarded credentialed |
|
On the header-logging flag at The convention point looks right to me though. Leaving it out to keep this PR to the credential path. Happy to open a separate one for the |
livekit/agents#7032 was approved and merged by davidzhao on 2026-08-30, so the API-key redaction fix moves into the merged table - five merged PRs now. Adds n8n#37359 to the hidden in-review list.
Summary
WSServerHandshakeErrorsubclassesaiohttp.ClientResponseError, so it is neither anaiohttp.ClientConnectorErrornor anasyncio.TimeoutError. Six credentialedws_connectsites still wrapped the connect inexcept (aiohttp.ClientConnectorError, asyncio.TimeoutError), so a rejected upgrade matched neither handler and escaped uncaught, carryingRequestInfoand therefore the auth header.Same failure as #6739, which #6740 fixed for cartesia. That reporter asked whether it was "the cartesia plugin, or more g[enerally]"; these are the sites where it was still reachable.
stt.py,stt_v2.pystt.pystt.pystt.py,tts.pyFixes #7031
Approach
The shape from #6740, applied unchanged: handle
asyncio.TimeoutErrorandaiohttp.ClientResponseErrorseparately, re-raise withfrom None, and surface only the status andtype(e).__name__rather thanstr(e)orrepr(e).I scoped this by parsing every
tryblock containing a credentialedws_connectand reading which exception types its handlers actually name, rather than grepping. My first grep pass wrongly cleareddeepgram/stt.py, which is why the count is from the AST walk. The other ten credentialed sites already catch the response error or re-raise withfrom None, and are untouched here.Verification
A local server refusing the upgrade with 401, called with deepgram's exact call shape.
Before:
After, the same request raises
APIStatusError: Invalid response status, status_code 401,__cause__None, and the key appears in neitherstr()norrepr().Checked what actually reaches a log, since that was the reported symptom:
One thing worth stating plainly:
raise ... from Nonesuppresses__cause__and sets__suppress_context__, so tracebacks andlogging.exceptionomit the original error, which is the #6739 symptom. The original exception object does remain on__context__, so an error reporter that walks the context chain and ignores the suppress flag could still reach it. That is equally true of the cartesia fix in #6740; I have kept the same shape rather than diverge. Happy to go further if you would rather these paths scrubRequestInfoexplicitly.Tests
tests/test_ws_handshake_credential_redaction.py, mirroringtests/test_plugin_cartesia_tts.pyfrom #6740: construct aWSServerHandshakeErrorwhoseRequestInfoheaders carry the key, drive the real_connect_ws, and assertAPIStatusErrorwith the status preserved, the key absent fromstrandrepr, and__cause__None. Marked per plugin so they run under the existing--pluginfiltering.ruff checkandruff formatclean on all seven files.