From e65d156eac1895527c7bc96dacafeed1eba8163e Mon Sep 17 00:00:00 2001 From: Mckgare0717 Date: Sun, 13 Sep 2026 10:22:46 +0100 Subject: [PATCH] Fix ClientTransaction init crash by sending auth cookies 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 Claude-Session: https://claude.ai/code/session_01WTsyPvyhvsNrHiWBxuucUG --- tests/test_client.py | 53 +++++++++++++++++++++++++++++++++++++++++++ twitter_cli/client.py | 8 +++++++ 2 files changed, 61 insertions(+) diff --git a/tests/test_client.py b/tests/test_client.py index c1393d3..8942b21 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -332,6 +332,59 @@ def test_cookie_string_used_when_available( assert headers["sec-ch-ua-platform-version"] == '""' +# ── TwitterClient._ensure_client_transaction ───────────────────────────── + +class TestEnsureClientTransaction: + """x.com serves a stripped logged-out shell (no ondemand.s bundle + reference) to unauthenticated requests, which crashes + x_client_transaction's homepage parsing. The homepage fetch used to + init ClientTransaction must carry the session's auth cookies so x.com + returns the full authenticated app shell instead. + """ + + @patch("twitter_cli.client.TwitterClient._load_ct_cache", return_value=False) + @patch("twitter_cli.client._get_cffi_session") + @patch("twitter_cli.client._gen_ct_headers", return_value={}) + def test_homepage_fetch_includes_auth_cookie(self, mock_ct_headers, mock_session, mock_load_cache): + mock_session.return_value = MagicMock() + # Fail fast right after the homepage fetch so we don't need to mock + # the rest of the ClientTransaction init chain. + mock_session.return_value.get = MagicMock(side_effect=Exception("stop after homepage fetch")) + + client = TwitterClient.__new__(TwitterClient) + client._auth_token = "test_token" + client._ct0 = "test_ct0" + client._cookie_string = None + client._client_transaction = None + client._ct_init_attempted = False + + client._ensure_client_transaction() + + homepage_call = mock_session.return_value.get.call_args + assert homepage_call.args[0] == "https://x.com" + sent_headers = homepage_call.kwargs["headers"] + assert sent_headers["Cookie"] == "auth_token=test_token; ct0=test_ct0" + + @patch("twitter_cli.client.TwitterClient._load_ct_cache", return_value=False) + @patch("twitter_cli.client._get_cffi_session") + @patch("twitter_cli.client._gen_ct_headers", return_value={}) + def test_homepage_fetch_prefers_full_cookie_string(self, mock_ct_headers, mock_session, mock_load_cache): + mock_session.return_value = MagicMock() + mock_session.return_value.get = MagicMock(side_effect=Exception("stop after homepage fetch")) + + client = TwitterClient.__new__(TwitterClient) + client._auth_token = "test_token" + client._ct0 = "test_ct0" + client._cookie_string = "auth_token=x; ct0=y; other=z" + client._client_transaction = None + client._ct_init_attempted = False + + client._ensure_client_transaction() + + sent_headers = mock_session.return_value.get.call_args.kwargs["headers"] + assert sent_headers["Cookie"] == "auth_token=x; ct0=y; other=z" + + class TestPaginationBehavior: def test_fetch_timeline_can_include_promoted_content(self): client = TwitterClient.__new__(TwitterClient) diff --git a/twitter_cli/client.py b/twitter_cli/client.py index 0436c8e..9b342d3 100644 --- a/twitter_cli/client.py +++ b/twitter_cli/client.py @@ -1103,6 +1103,14 @@ def _ensure_client_transaction(self): # a different TLS fingerprint on the same IP — a detection vector. cffi_session = _get_cffi_session() ct_headers = _gen_ct_headers() + # x.com serves a stripped-down logged-out shell (no ondemand.s + # JS bundle reference) to unauthenticated requests, which makes + # x_client_transaction's homepage regex match nothing and crash + # with "'NoneType' object has no attribute 'group'" inside + # get_ondemand_file_url. Attach the same session cookies used + # for API calls so we get the full authenticated app shell. + ct_headers["Cookie"] = self._cookie_string or "auth_token=%s; ct0=%s" % ( + self._auth_token, self._ct0) home_page = cffi_session.get( "https://x.com", headers=ct_headers, timeout=10, )