Repository navigation
fix(oauth): include error_description in authorization callback errors - #626
Merged
Merged
Conversation
The local callback server only read `error` from the redirect and dropped `error_description`, so an unknown/placeholder client_id surfaced as a bare `OAuth authorization failed: internal_server_error`. Append the description (RFC 6749 §4.1.2.1) to the error, and HTML-escape the text shown on the browser callback page since it comes from the query string. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
# Conflicts: # CHANGELOG.md
…k errors - the callback handler checks state for every redirect, error redirects included; a mismatched or missing state gets an error page and is ignored (the flow keeps waiting), so another page can neither abort a pending authorization nor inject an error text. CsrfTokenMismatch is no longer returned by the flow - redirect-supplied error text has control characters (ANSI escapes, CR/LF) replaced and is capped at 512 characters before reaching the error, logs and the page (which still HTML-escapes it) - tests: escape/format/sanitize units; flows tests for forged callbacks being ignored and for error_description + page escaping; existing flows send the correct state - CHANGELOG: entries for the description, the XSS fix and the state check
… the CsrfTokenMismatch change Review of 99c98b6: - the page for a callback with a mismatched state no longer says "Authorization Failed": the SDK keeps waiting, so it tells the user to finish in the most recently opened window - sanitize also replaces line/paragraph separators, zero-width characters and bidi overrides, not only control characters - error code tables (Rust, C header) mark code 8 as no longer returned - CHANGELOG: a mismatched redirect now ends in AuthorizationTimeout after 5 minutes instead of CsrfTokenMismatch; the 512-character cap applies to error and error_description each
…ror-description # Conflicts: # CHANGELOG.md
Two claims did not match the code. Control characters become spaces, but an ANSI escape sequence is not 'replaced': only its leading ESC is a control character, so the rest survives as literal text — which defuses the sequence without removing it, as the crate's own test asserts. And a truncated value is 512 characters plus an appended '…', not 512. Fixed in the CHANGELOG and in sanitize's own doc comment, which carried the same two errors.
hogan-yuan
added a commit
to longbridge/openapi-go
that referenced
this pull request
Nov 17, 2026
Ports the correction from longbridge/openapi#626. Two claims did not match the code, in both the CHANGELOG and the function's own comment. Control characters become spaces, but an ANSI escape sequence is not replaced: unicode.IsControl matches only its leading ESC, so the rest survives as literal text — which defuses the sequence without removing it. And a truncated value is 512 characters plus an appended "…", not 512.
…ror-description # Conflicts: # CHANGELOG.md
hogan-yuan
added a commit
to longbridge/openapi-go
that referenced
this pull request
Nov 19, 2026
Documentation only — no behaviour change.
The CHANGELOG entry and the `sanitizeCallbackText` doc comment both
listed "control characters **including ANSI escapes**" among what gets
replaced with spaces. That is not what the function does, and the
wording mattered because it is the security note a reader relies on.
## What it actually does
```go
func isUnsafeRune(r rune) bool {
return unicode.IsControl(r) || ...
}
```
Only the **leading ESC** of an ANSI sequence is a control character. The
rest — `[31m` — is ordinary printable text and survives verbatim. The
sequence is defused, not removed.
The second inaccuracy: both places said the fields are "capped at 512
characters" without mentioning that `…` is appended, so the cap is
effectively 512 + 1.
## Verified by running it
```
ANSI in="\x1b[31mRED\x1b[0m"
out=" [31mRED [0m" <- ESC -> space, "[31m" left as text
TRUNC inLen=562 outRunes=513 endsWithEllipsis=true
EXACT inLen=512 outRunes=512 endsWithEllipsis=false
```
So the defusing works, the escape text does remain, and `…` is added
only when something was actually cut.
## Note
The same two inaccuracies were in the Rust SDK's CHANGELOG and were
corrected there (longbridge/openapi#626). This brings the Go wording in
line.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
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.
Reopens #619, which was closed by mistake. The branch had been deleted, so GitHub would not let the PR be reopened; this is the same work, with
mainmerged in.Summary
errorfrom the redirect and droppederror_description. An unknown or placeholderclient_id(e.g.your-client-idfrom the examples) surfaced as a bareOAuth authorization failed: invalid_client, with nothing saying which client id was wrong.error_description(RFC 6749 §4.1.2.1) is now appended:OAuth authorization failed: invalid_client: unknown client_id: <id>. The same text appears on the browser callback page.localhostorigin, reachable by any web page while an authorization was pending. It is now HTML-escaped.stateis checked on every callback, including error redirects. A callback with a mismatched or missingstategets a 400 page and is ignored, and the flow keeps waiting for the real redirect. Previously any web page could end a pending authorization with an error text of its choosing.erroranderror_descriptionare each capped at 512 characters before reaching the error, the logs or the terminal.Behaviour change worth noting
CsrfTokenMismatch(error code 8) is no longer returned by the authorization flow. The variant is kept for compatibility and marked as such in the code tables. A redirect with a wrong or missingstatenow leaves the flow waiting, so without a valid redirect it ends inAuthorizationTimeoutafter 5 minutes. Rust code matchingCsrfTokenMismatchfrom the flow should handleAuthorizationTimeoutinstead.Other language layers pass
AuthorizationFailed { message }through unchanged, so no binding changes were needed.Testing
Unit and flow tests: escaping, message format, sanitization; forged callbacks (wrong
state, carrying either a code or an attacker-written error) are ignored and the real callback still completes the flow;error_descriptioncontaining<script>is reported in the error and escaped on the page.cargo test -p longbridge-oauth: 52 passed, 1 ignored.cargo +nightly fmt --all --checkclean;cargo clippy --all --all-featuresunchanged against main's baseline.Downstream
cargo check --all-targetson longbridge-terminal and longbridge-mcp with the workspace patched to this branch: the CLI drives the browser flow viaOAuthBuilder::buildand needs no change; the MCP only usesOAuth::from_tokenand is unaffected.End-to-end against the live gateways
Driven with a non-existent
client_id, relaying the gateway's real error redirect into the local callback server. Production (openapi.longbridge.com) and staging (openapi-global.longbridge.xyz) behave identically:Security behaviour, verified against a live flow (the real error text contains no special characters, so these were injected into the callback directly):
statestatestate+<script>alert(1)</script><script>on the page;<escaped to<Note for reviewers
An earlier version of this description said the server returns
internal_server_errorrather thaninvalid_clientfor an unknown client, and called that an out-of-scope backend issue. That is no longer the case: both environments now returninvalid_clientwith an accurateunknown client_id: <id>description. Nothing in this PR depends on which code the server sends — the fix is that whatever description it sends is now surfaced, escaped and sanitized.