Skip to content

root: don't fail websocket connections with non-UTF-8 headers - #26688

Merged
BeryJu merged 2 commits into
goauthentik:mainfrom
ryanpesek:root/ws-logging-non-utf8-headers
Oct 5, 2026
Merged

BeryJu merged 2 commits into
goauthentik:mainfrom
ryanpesek:root/ws-logging-non-utf8-headers

Conversation

@ryanpesek

Copy link
Copy Markdown
Contributor

Details

What does this PR change?

ChannelsLoggingMiddleware.log() now decodes the User-Agent and X-Forwarded-For header values with errors="backslashreplace" instead of strict UTF-8.

ASGI header values are raw bytes, and HTTP doesn't require them to be UTF-8 (RFC 9110 allows obs-text). When a client sends any other byte, log() raises UnicodeDecodeError. log() runs before the middleware's try, so the exception goes up to the ASGI server and the websocket handshake fails with a 500:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa0 in position 129: invalid start byte

With backslashreplace, valid UTF-8 is logged as before, and other bytes are logged escaped (Meta\xa0AI), so the log line still shows what the client sent. errors="replace" would also stop the error but loses the byte, and latin-1 would garble valid non-ASCII UTF-8.

How was this tested?

  • New test test_non_utf8_headers in authentik/events/tests/test_ws_client.py. It opens /ws/client/ with 0xA0 in the User-Agent and 0xFF in X-Forwarded-For, checks that the connection is accepted, and checks that the scheme=ws log line has both values escaped. Without the fix, it fails with the UnicodeDecodeError above.
  • authentik/events/tests/test_ws_client.py, authentik/root/ and authentik/outposts/tests/: 41 passed.
  • We run the same change on a 2026.5 build, where the full test suite passes.
  • black and ruff are clean for the changed files.

Linked issues

None.

AI usage

I used an AI coding assistant to investigate the error, draft the change and its test, and write this description. I reviewed, tested, and edited all of it.


Checklist

  • The project has been linted, built, and tested (make all)
  • The documentation has been updated and formatted (make docs)
  • I have read the AI usage policy.

ChannelsLoggingMiddleware decoded the User-Agent and X-Forwarded-For
header values as strict UTF-8. Header values are raw bytes and HTTP
doesn't require them to be UTF-8, so a client sending any other byte
(for example the Meta AI in-app browser on iOS, whose User-Agent has a
Latin-1 non-breaking space, 0xA0) made log() raise UnicodeDecodeError.
log() runs before the middleware's try, so the handshake failed with a
500 and the client couldn't open a websocket at all.

Decode with errors="backslashreplace": valid UTF-8 is logged as before,
and other bytes are logged escaped (\xa0) instead of failing.
@ryanpesek
ryanpesek requested a review from a team as a code owner October 5, 2026 01:36
@netlify

netlify Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for authentik-docs ready!

Name Link
🔨 Latest commit fa0979e
🔍 Latest deploy log https://app.netlify.com/projects/authentik-docs/deploys/6ac38df59ae66f00086cc229
😎 Deploy Preview https://deploy-preview-26688--authentik-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@BeryJu BeryJu added backport/version-2026.5 Add this label to PRs to backport changes to version-2026.5 backport/version-2026.8 Add this label to PRs to backport changes to version-2026.8 labels Oct 5, 2026
@codecov

codecov Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.27%. Comparing base (51e9602) to head (fa0979e).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #26688      +/-   ##
==========================================
+ Coverage   91.76%   92.27%   +0.51%     
==========================================
  Files        1175     1175              
  Lines       77697    77708      +11     
  Branches     4129     4129              
==========================================
+ Hits        71297    71708     +411     
+ Misses       6334     5934     -400     
  Partials       66       66              
Flag Coverage Δ
conformance 32.95% <0.00%> (-0.01%) ⬇️
e2e 37.75% <0.00%> (?)
integration 29.25% <0.00%> (-0.49%) ⬇️
rust 46.45% <ø> (ø)
unit 93.73% <100.00%> (+<0.01%) ⬆️
unit-migrate 93.73% <100.00%> (+12.36%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@BeryJu
BeryJu merged commit eef0bfd into goauthentik:main Oct 5, 2026
138 of 139 checks passed
@authentik-cherry-pick

Copy link
Copy Markdown
Contributor

⚠️ Cherry-pick to version-2026.8 has conflicts: #26706

@authentik-cherry-pick

Copy link
Copy Markdown
Contributor

⚠️ Cherry-pick to version-2026.5 has conflicts: #26707

BeryJu added a commit that referenced this pull request Oct 7, 2026
…-pick #26688 to version-2026.8) (#26706)

* Cherry-pick #26688 to version-2026.8 (with conflicts)

This cherry-pick has conflicts that need manual resolution.

Original PR: #26688
Original commit: eef0bfd

* move test to root/tests/test_ws_client.py

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01HPATKAY2JKB2kinG1WrKj4

---------

Co-authored-by: Ryan Pesek <[email protected]>
Co-authored-by: Jens Langhammer <[email protected]>
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
BeryJu added a commit that referenced this pull request Oct 7, 2026
…-pick #26688 to version-2026.5) (#26707)

* Cherry-pick #26688 to version-2026.5 (with conflicts)

This cherry-pick has conflicts that need manual resolution.

Original PR: #26688
Original commit: eef0bfd

* fix?

---------

Co-authored-by: Ryan Pesek <[email protected]>
Co-authored-by: Jens Langhammer <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport/version-2026.5 Add this label to PRs to backport changes to version-2026.5 backport/version-2026.8 Add this label to PRs to backport changes to version-2026.8

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants