Skip to content

fix(integrations): decode base64url oauth state in the callback - #1654

Merged
sung17 merged 1 commit into
mainfrom
fix/tiktok-oauth-state-base64url
Nov 7, 2026
Merged

sung17 merged 1 commit into
mainfrom
fix/tiktok-oauth-state-base64url

Conversation

@sung17

@sung17 sung17 commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator

Summary

Fixes the TikTok connect flow returning 404 on the OAuth callback, reported in #1572 (comment).

  • TikTok encodes the OAuth state as base64url (integrations/tiktok/src/apis/auth.ts), which uses - and _.
  • The integrations callback decoded it with atob, which rejects - and _ ("Invalid character") → notFound().
  • With a UUID workspace id and the .../settings/channels?channel=tiktok referer, the encoded state contains those characters practically every time (2000/2000 in a local run), so connecting TikTok failed for most workspaces.

Change

  • New decodeOAuthState() in apps/builder/src/lib/oauth-state.ts: decodes with Buffer.from(raw, "base64url"), which accepts both base64url (TikTok) and the standard base64 the other integrations produce with btoa (Zalo, Google Sheets/Calendar, Instagram, WhatsApp).
  • The callback uses it instead of atob. Connect-session states ({sessionId}.{nonce}) are unaffected — they are dispatched before this parse.

Test plan

  • apps/builder/__tests__/oauth-state-decode.test.ts: base64url state with -/_, standard base64 state, percent-encoded state, invalid state throws
  • Existing callback tests (oauth-callback-connect-session, oauth-reconnect-callback) pass
  • ultracite check on changed files, check:invariants, pnpm check-types --filter builder

🤖 Generated with Claude Code

TikTok encodes the OAuth state as base64url, but the integrations
callback decoded it with atob, which rejects "-" and "_". Whenever the
encoded state contained them the callback returned 404, so connecting
TikTok failed for most workspaces. Decode with Node's base64url decoder,
which accepts both base64url and the standard base64 the other
integrations produce with btoa.

Reported in #1572 (comment 6650507443).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the bug Something isn't working as expected label Nov 5, 2026
@sung17
sung17 merged commit 9aab29b into main Nov 7, 2026
15 checks passed
hello-jackytruong referenced this pull request in hello-jackytruong/ChatWorkAI Nov 22, 2026
TikTok encodes the OAuth state as base64url, but the integrations
callback decoded it with atob, which rejects "-" and "_". Whenever the
encoded state contained them the callback returned 404, so connecting
TikTok failed for most workspaces. Decode with Node's base64url decoder,
which accepts both base64url and the standard base64 the other
integrations produce with btoa.

Reported in #1572 (comment 6650507443).

Co-authored-by: Deathgiver <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant