[GH-ISSUE #8954] generic-oauth: non-ASCII error_description causes TypeError on redirect #11245

Open
opened 2026-04-13 07:35:32 -05:00 by GiteaMirror · 0 comments
Owner

Originally created by @RainyPixel on GitHub (Apr 4, 2026).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/8954

Description

When an OAuth provider returns error_description containing non-ASCII characters (e.g., Cyrillic from Yandex OAuth), the generic-oauth callback handler crashes with a TypeError because the description is interpolated directly into the redirect URL without encoding.

Steps to reproduce

  1. Configure a generic OAuth provider that returns error responses with non-ASCII error_description (e.g., Yandex OAuth returns Russian text)
  2. Trigger an OAuth error (e.g., unauthorized_client)
  3. The callback handler crashes instead of redirecting to the error page

Error

TypeError: Header 'location' has invalid value:
'https://example.com/auth/error?error=unauthorized_client&error_description=Запрещено получать токены с данными правами для данного приложения'

Root cause

In plugins/generic-oauth/routes.mjs, the redirect URL is built via template literal without encoding:

throw ctx.redirect(
  `${defaultErrorURL}?error=${ctx.query.error || "oAuth_code_missing"}&error_description=${ctx.query.error_description}`
);

Non-ASCII characters in HTTP Location header are invalid per RFC 7230.

Suggested fix

Wrap query parameters with encodeURIComponent():

throw ctx.redirect(
  `${defaultErrorURL}?error=${encodeURIComponent(ctx.query.error || "oAuth_code_missing")}&error_description=${encodeURIComponent(ctx.query.error_description || "")}`
);

Same issue exists in redirectOnError function in the same file.

Environment

  • better-auth: 1.5.6
  • Runtime: Bun
  • OAuth provider: Yandex (returns Cyrillic error_description)
Originally created by @RainyPixel on GitHub (Apr 4, 2026). Original GitHub issue: https://github.com/better-auth/better-auth/issues/8954 ## Description When an OAuth provider returns `error_description` containing non-ASCII characters (e.g., Cyrillic from Yandex OAuth), the generic-oauth callback handler crashes with a `TypeError` because the description is interpolated directly into the redirect URL without encoding. ## Steps to reproduce 1. Configure a generic OAuth provider that returns error responses with non-ASCII `error_description` (e.g., Yandex OAuth returns Russian text) 2. Trigger an OAuth error (e.g., `unauthorized_client`) 3. The callback handler crashes instead of redirecting to the error page ## Error ``` TypeError: Header 'location' has invalid value: 'https://example.com/auth/error?error=unauthorized_client&error_description=Запрещено получать токены с данными правами для данного приложения' ``` ## Root cause In `plugins/generic-oauth/routes.mjs`, the redirect URL is built via template literal without encoding: ```js throw ctx.redirect( `${defaultErrorURL}?error=${ctx.query.error || "oAuth_code_missing"}&error_description=${ctx.query.error_description}` ); ``` Non-ASCII characters in HTTP `Location` header are invalid per RFC 7230. ## Suggested fix Wrap query parameters with `encodeURIComponent()`: ```js throw ctx.redirect( `${defaultErrorURL}?error=${encodeURIComponent(ctx.query.error || "oAuth_code_missing")}&error_description=${encodeURIComponent(ctx.query.error_description || "")}` ); ``` Same issue exists in `redirectOnError` function in the same file. ## Environment - better-auth: 1.5.6 - Runtime: Bun - OAuth provider: Yandex (returns Cyrillic error_description)
GiteaMirror added the bug label 2026-04-13 07:35:32 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#11245