Fix WebSocket auth rejecting API keys with URL-special characters#964
Open
chrisyoung2005 wants to merge 1 commit intoRightNow-AI:mainfrom
Open
Fix WebSocket auth rejecting API keys with URL-special characters#964chrisyoung2005 wants to merge 1 commit intoRightNow-AI:mainfrom
chrisyoung2005 wants to merge 1 commit intoRightNow-AI:mainfrom
Conversation
The ?token= query parameter was extracted with split/strip_prefix and compared raw against the configured API key. Browsers encode the token with encodeURIComponent before appending it to the WebSocket URL, so characters common in base64-derived keys (+, /, =) arrive percent-encoded (%2B, %2F, %3D) and the comparison always fails. Switch to url::form_urlencoded::parse (an existing workspace dep) to decode the query string before comparison. No new transitive dependencies are added. Fixes RightNow-AI#962
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Fixes #962.
The WebSocket upgrade handler authenticates browser clients via a
?token=query parameter (browsers cannot set custom headers on WebSocket connections). The handler was extracting the token withsplit('&')/strip_prefix("token=")and comparing it directly against the configured API key using constant-time comparison.The problem: browsers call
encodeURIComponent()on the token before appending it to the WebSocket URL (api.js:226). Characters common in base64-derived keys —+,/,=— are percent-encoded to%2B,%2F,%3D. The server received the encoded string and compared it character-for-character against the raw key. They never match, so every WebSocket upgrade returned 401.The result: the fallback in
chat.jssilently switched to HTTP polling mode. Users with base64-derived API keys (e.g. generated withopenssl rand -base64 32) could never get streaming — without any clear error pointing to the root cause.How
Switch to
url::form_urlencoded::parse()to decode the query string before comparison. This is an existing workspace dependency (url = "2"in rootCargo.toml) — no new transitive dependencies are added.Before (
ws.rs):After:
Verification
cargo clippy -p openfang-api --all-targets -- -D warnings: zero warningscargo test -p openfang-api: 7/7 tests passWorkaround (until merged)
Generate an API key using only URL-safe characters: