Skip to content

Fix ClientTransaction init crash (search returns 404) - #90

Open
Mckgare0717 wants to merge 1 commit into
jackwener:mainfrom
Mckgare0717:fix/x-client-transaction-cookie
Open

Mckgare0717 wants to merge 1 commit into
jackwener:mainfrom
Mckgare0717:fix/x-client-transaction-cookie

Conversation

@Mckgare0717

Copy link
Copy Markdown
Contributor

Summary

  • search (and any other endpoint that requires x-client-transaction-id) currently fails with a bare HTTP 404, because ClientTransaction silently fails to initialize on every run.
  • Root cause: _ensure_client_transaction fetches https://x.com without the session's auth cookies to scrape the ondemand.s JS bundle reference. x.com now serves a stripped-down logged-out shell (~32KB, no bundle reference) to unauthenticated requests, so x_client_transaction's regex in get_ondemand_file_url matches nothing and crashes with 'NoneType' object has no attribute 'group'. That exception is caught and logged as a warning, so the client silently continues without the transaction header — working fine for endpoints that don't require it (feed, status, whoami), and hitting a 404 on ones that do (search).
  • Fix: attach the client's own auth_token/ct0 cookies to that homepage fetch, exactly like every other authenticated request already does. With cookies, x.com returns the full ~300KB+ app shell and the bundle regex matches again.

Verified locally against live x.com: before the fix, twitter search "..." reliably logs Failed to init ClientTransaction: 'NoneType' object has no attribute 'group' and returns a 404; after the fix, no warning, ClientTransaction initializes, and search returns real results.

Test plan

  • Added TestEnsureClientTransaction in tests/test_client.py — two tests confirming the homepage fetch carries the Cookie header (both from discrete auth_token/ct0 and from cookie_string). Verified both fail with KeyError: 'Cookie' against the pre-fix code and pass after.
  • Full suite: 241 passed, same 3 pre-existing failures as on main (test_iter_chrome_cookie_files_* in tests/test_auth.py — unrelated Chrome-profile path detection tests that don't pass on Windows; unaffected by this change).
  • Live end-to-end check: twitter search, twitter status, twitter -c feed all working post-fix.

x.com serves a stripped-down logged-out shell (no ondemand.s JS bundle
reference) to unauthenticated requests. x_client_transaction's homepage
parser (get_ondemand_file_url) expects that bundle reference and crashes
with "'NoneType' object has no attribute 'group'" when its regex finds
nothing, so ClientTransaction silently fails to initialize.

_ensure_client_transaction already swallows that exception and continues
without the x-client-transaction-id header, so most endpoints (feed,
status, whoami) keep working. Twitter's search endpoint enforces the
header and returns a bare 404 once it's missing, which is where this
first surfaced.

Fix: attach the client's own session cookies to the homepage fetch used
for ClientTransaction init, same as every other authenticated request.
With cookies, x.com returns the full app shell and the bundle regex
matches again.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WTsyPvyhvsNrHiWBxuucUG

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant