Skip to content

fix(twitter-app): escape the state param reflected on the OAuth callback error page - #14352

Merged
aryanorastar merged 4 commits into
BasedHardware:mainfrom
Suyann:fix/twitter-app-callback-xss
Sep 22, 2026
Merged

aryanorastar merged 4 commits into
BasedHardware:mainfrom
Suyann:fix/twitter-app-callback-xss

Conversation

@Suyann

@Suyann Suyann commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

What

The Twitter voice-posting plugin's OAuth callback error page (plugins/omi-twitter-app/main_simple.py, GET /auth/callback) now percent-encodes and HTML-escapes the state query param before building its "Try again" link, instead of reflecting the raw value into an href attribute. Fixes #14350.

Why

Any token-exchange failure renders the error page, and its link was f'<a href="/auth?uid={error_uid}">' with error_uid = state, where state is Query(None) straight from the request. On current main:

GET /auth/callback?code=x&state=%22%3E%3Cscript%3Ealert(1)%3C/script%3E
->  <p><a href="/auth?uid="><script>alert(1)</script>">Try again</a></p>

The quote closes the attribute and the script executes on the plugin's origin (reflected XSS). The branch is reachable without any session: a state with no stored OAuth handler makes get_access_token() raise OAuth session not found, which is the path that renders the page. A benign & or # in state corrupted the retry query the same way.

The value is now encoded at both boundaries it crosses — urllib.parse.quote(value, safe="") for the query, html.escape(..., quote=True) for the attribute — so the link still carries the original value (it round-trips through parse_qs) but can no longer break out of the href.

Changes

  • plugins/omi-twitter-app/main_simple.py: import html and urllib.parse.quote; the callback error page interpolates html.escape(quote(state, safe=""), quote=True).
  • Second commit (leave it better, same file): the setup page at GET /?uid= built its "Connect Twitter Account" link as f"/auth?uid={uid}" inside an href — the same raw reflection. Encoded the same way. omi-twitter-app is not among the six apps the pattern-wide PR fix(plugins): encode reflected uid in setup/auth pages across six apps #13658 covers; no overlap with that diff. Open PR fix(plugins): sanitize sensitive logging of tokens, credentials, and user data (#13777) #13782 also edits main_simple.py, but only its logging lines (hunks at main lines 222-231, 638-649, 665-671 and 697-752); this branch touches the import block, root() (main_simple.py:29-35) and the auth_callback error branch (:523-536), so the two do not overlap textually either.
  • plugins/omi-twitter-app/test_callback_error_escaping.py (new): hermetic regression test — stubs FastAPI, dotenv and the sibling modules, drives the real auth_callback handler down its error branch and the real root handler, parses the page with html.parser the way a browser would and pins that no <script> element opens, the href is percent-encoded, &/# cannot corrupt the query, and the uid round-trips; plain and missing state keep their links unchanged.
  • .github/checks-manifest.yaml: registers twitter-app-callback-error-escaping-tests (local + ci lanes, triggered by plugins/omi-twitter-app/**).

Tests

PYTHONUTF8=1 python3 plugins/omi-twitter-app/test_callback_error_escaping.py
PASS test_script_in_state_is_not_reflected_raw
PASS test_try_again_link_round_trips_state
PASS test_plain_state_link_is_unchanged
PASS test_missing_state_falls_back_to_unknown
PASS test_setup_page_encodes_uid_in_connect_link
5 tests passed

Same test against main's main_simple.py fails as intended (control cases pass):

FAIL test_script_in_state_is_not_reflected_raw: ... <a href="/auth?uid="><script>alert(1)</script>">Try again</a> ...
FAIL test_try_again_link_round_trips_state: ('"><script>alert(1)</script>', '/auth?uid=')
PASS test_plain_state_link_is_unchanged
PASS test_missing_state_falls_back_to_unknown
FAIL test_setup_page_encodes_uid_in_connect_link: ['/auth?uid=']

Also run: python3 plugins/oauth/test_client_log_safety.py (OK, 5 tests), git diff --check main..fix/twitter-app-callback-xss (clean), bash scripts/pr-preflight --pr-body-file <this body> (passed). The live path (a real Twitter OAuth round trip on a deployed instance with client credentials) needs a Railway deployment and X app keys I do not have; the handlers were exercised through the hermetic harness above, which calls the real functions with only FastAPI and the network-bound siblings stubbed.

Failure-Class

Failure-Class: none

(scripts/failure-class prepare validates the declaration (OK: prepare, patch operation=none) and none of the 253 registry definitions describes client input reflected unescaped into HTML; the advisory candidates it surfaced for this path — nonexecuting-verification-command, runtime-image-boundary, secret-printed-to-plugin-logs — do not apply.)

Invariants

scripts/pr-preflight --suggest reports no affected product invariants for this diff (plugin-only change).

Bounty

Claims the US$25 proposed in #14350; payout details will be sent to [email protected] after merge as the Contribution guide asks.

Authored with Claude Code on behalf of @Suyann; every command listed was actually run on this branch.

Review in cubic

…ack error page

GET /auth/callback?code=x&state=<value> rendered its error page with
f'<a href="/auth?uid={state}">', so the attacker-controlled state query
param landed raw inside an HTML attribute. A state of
"><script>alert(1)</script> closed the attribute and executed as script
(reflected XSS), and & or # in the value corrupted the retry query.

The value is now percent-encoded for the query (urllib.parse.quote with
safe="") and HTML-escaped for the attribute, so the Try again link still
carries the original value but can no longer break out of the href.

Adds a hermetic regression test that stubs FastAPI and the sibling
modules, drives the real auth_callback handler down its error branch and
pins that no <script> element opens, the href is percent-encoded and the
uid round-trips; registered in .github/checks-manifest.yaml.

Verification:
- PYTHONUTF8=1 python3 plugins/omi-twitter-app/test_callback_error_escaping.py -> 4 tests passed
- same test against the previous main_simple.py -> 2 FAIL
  (href="/auth?uid="><script>alert(1)</script>" reaches the page raw)

Failure-Class: none
…t link

GET /?uid=<value> built the "Connect Twitter Account" link as
f"/auth?uid={uid}" inside an href attribute, the same raw reflection the
callback error page had: a quote in uid broke out of the attribute (HTML
injection) and & or # corrupted the query. Percent-encode the value for
the query and escape it for the attribute, mirroring the callback fix.

Covered by a fifth case in test_callback_error_escaping.py that renders
the real root handler and pins the encoded, round-trippable href.

Verification:
- PYTHONUTF8=1 python3 plugins/omi-twitter-app/test_callback_error_escaping.py -> 5 tests passed
- same test against the previous commit -> FAIL test_setup_page_encodes_uid_in_connect_link: ['/auth?uid=']

Failure-Class: none
@Git-on-my-level

Copy link
Copy Markdown
Member

Thanks @Suyann — verified end to end; this is a clean fix for a real reflected XSS.

The bug is real on main. Rendering the callback error page with the pre-fix main_simple.py, GET /auth/callback?code=x&state="><script>alert(1)</script> produced <a href="/auth?uid="><script>alert(1)</script>"> — the quote closes the attribute and the script runs on the plugin's origin. The path is reachable without a session: an unknown state makes get_access_token() raise OAuth session not found, landing in the except that renders the page.

Per file:

  • plugins/omi-twitter-app/main_simple.py — both reflections are now encoded at each boundary they cross: quote(value, safe="") for the query plus html.escape(..., quote=True) for the attribute, in auth_callback's Try-again link and in root's Connect link (second commit). Correct construction: after quote(safe="") only unreserved characters and %XX remain, so the value can't terminate the attribute or smuggle &/#, and it still round-trips through parse_qs. The retry target /auth itself only issues a RedirectResponse to Tweepy's authorize URL, so there's no second reflection hop.
  • plugins/omi-twitter-app/test_callback_error_escaping.py — genuinely hermetic (stdlib only; FastAPI/dotenv/sibling modules stubbed, no network or secrets). I ran it: 5/5 pass on this branch, and against the pre-fix main_simple.py the script-reflection, round-trip, and setup-page tests fail with the raw reflection showing — so the suite pins the fix rather than passing vacuously. Parsing the rendered page with HTMLParser and counting opened <script> elements is the right browser-like assertion.
  • .github/checks-manifest.yaml — the new twitter-app-callback-error-escaping-tests entry sits under checks: with lanes: [local, ci] and triggers scoped to the plugin directory, matching the established sibling hermetic-suite pattern (e.g. twitter-chat-tools-max-results-tests). Parses cleanly and the reason text is accurate.

The description's scoping claims also check out: #13658's six-app sweep doesn't include omi-twitter-app, and open #13782 only touches logging lines elsewhere in this file, so no real conflict.

Left for human maintainer review: this changes rendering inside the OAuth callback of a user-facing auth flow — worth a maintainer's sign-off before merge even though the fix itself looks correct.

Automated maintainer-assistant feedback; final merge decisions rest with the human maintainers.


by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.

@Git-on-my-level Git-on-my-level added positive-signal Automation verified a genuine fix/quality contribution security-review Touches auth, provider routing, secrets, or security-sensitive surfaces python labels Sep 17, 2026
…ts entry in a stable section

Moves the checks-manifest.yaml entry out of the tail of the checks list, where
several concurrent PRs insert at the same spot and every merge conflicts, into
the stable section after desktop-backend-drift-fixtures. No behaviour change;
the check still runs in both lanes with the same triggers.
@Suyann

Suyann commented Sep 18, 2026

Copy link
Copy Markdown
Contributor Author

Resolved the merge conflict: the only conflicting file was .github/checks-manifest.yaml (several PRs append at the same spot before exempt:). The entry is now registered in a stable section of the checks list (after desktop-backend-drift-fixtures) with the same command, triggers and lanes; no code change. The branch merges cleanly against current main (git merge-tree), the regression test passes, and pr-preflight passes locally.

@Git-on-my-level

Copy link
Copy Markdown
Member

Follow-up verification on the current head (bfcaf7d):

No blocking concerns from my side; CI has not reported on this head, so the local run above is the execution evidence. Since this modifies an OAuth callback error path, leaving the merge sign-off to a maintainer.


by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.

@Suyann

Suyann commented Sep 19, 2026

Copy link
Copy Markdown
Contributor Author

Conflict-free again: another PR landed at the manifest spot I had moved to, so the checks-manifest.yaml entry now sits next to the firestore checks (untouched on main). No code change; git merge-tree against current main is clean, the 5 hermetic tests pass, pr-preflight passes locally. Ready for the human sign-off flagged in the review.

@Suyann
Suyann force-pushed the fix/twitter-app-callback-xss branch from 34b0694 to b9cd46d Compare September 19, 2026 16:42
@Suyann

Suyann commented Sep 19, 2026

Copy link
Copy Markdown
Contributor Author

Correction to my previous note: that push still conflicted (the manifest section I had moved to changed on main in the meantime). The entry is now placed in a section verified untouched on main; git merge-tree against current main reports no conflict, tests 5/5, pr-preflight passes locally. Sorry for the noise.

…ntry after diff-hygiene

The previous locations (tail of the list, then after desktop-backend-drift-fixtures)
collided with other PRs inserting at the same spots; this section is untouched on
main. Same command, triggers and lanes; no behaviour change.
@Suyann
Suyann force-pushed the fix/twitter-app-callback-xss branch from b9cd46d to 569c0f3 Compare September 19, 2026 16:43

@aryanorastar aryanorastar left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on 569c0f393f4c.

The OAuth callback error page percent-encodes state and HTML-escapes the Try-again href; the setup Connect link does the same for uid. twitter-app-callback-error-escaping-tests is registered. I ran that suite against this tip: 5/5 OK.

I am not merging.

@aryanorastar
aryanorastar merged commit 9cbf40e into BasedHardware:main Sep 22, 2026
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

positive-signal Automation verified a genuine fix/quality contribution python security-review Touches auth, provider routing, secrets, or security-sensitive surfaces

Projects

None yet

Development

Successfully merging this pull request may close these issues.

omi-twitter-app: reflected XSS — /auth/callback error page interpolates the raw state query param into its HTML ($25 bounty proposed)

3 participants