diff --git a/pyproject.toml b/pyproject.toml index de8f3dc..94fd3fa 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,7 +25,7 @@ dependencies = [ "rich>=13.0", "PyYAML>=6.0", "curl_cffi>=0.7", - "xclienttransaction>=1.0.1", + "xclienttransaction>=1.0.3", "beautifulsoup4>=4.12", ] diff --git a/tests/test_client.py b/tests/test_client.py index c1393d3..53e297c 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -265,6 +265,37 @@ def test_handles_malformed_html(self): # ── TwitterClient._build_headers ───────────────────────────────────────── +class TestClientTransactionInit: + @pytest.mark.parametrize("cookie_string", [None, "auth_token=token; ct0=csrf; lang=en"]) + def test_homepage_uses_session_cookies_but_cdn_does_not(self, cookie_string): + session = MagicMock() + home = MagicMock(text="authenticated homepage") + home.content = home.text.encode() + script = MagicMock(text="transaction script") + session.get.side_effect = [home, script] + script_url = "https://abs.twimg.com/responsive-web/client-web/ondemand.s.test.js" + + with ( + patch.object(TwitterClient, "_load_ct_cache", return_value=False), + patch.object(TwitterClient, "_save_ct_cache") as save_cache, + patch("twitter_cli.client._get_cffi_session", return_value=session), + patch("twitter_cli.client.get_ondemand_file_url", return_value=script_url), + patch("twitter_cli.client.ClientTransaction") as transaction, + patch("twitter_cli.client._update_features_from_html"), + ): + client = TwitterClient("token", "csrf", cookie_string=cookie_string) + + homepage_call, cdn_call = session.get.call_args_list + assert homepage_call.args == ("https://x.com",) + assert homepage_call.kwargs["headers"]["Cookie"] == ( + cookie_string or "auth_token=token; ct0=csrf" + ) + assert cdn_call.args == (script_url,) + assert "Cookie" not in cdn_call.kwargs["headers"] + assert client._client_transaction is transaction.return_value + save_cache.assert_called_once_with(home.text, script.text) + + class TestBuildHeaders: @patch("twitter_cli.client._get_cffi_session") @patch("twitter_cli.client._gen_ct_headers", return_value={}) diff --git a/twitter_cli/client.py b/twitter_cli/client.py index 0436c8e..95b49c0 100644 --- a/twitter_cli/client.py +++ b/twitter_cli/client.py @@ -1103,9 +1103,18 @@ 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() + # The logged-out homepage omits the ondemand script needed for + # transaction IDs. Send session cookies only to x.com, not its CDN. home_page = cffi_session.get( - "https://x.com", headers=ct_headers, timeout=10, + "https://x.com", + headers={ + **ct_headers, + "Cookie": self._cookie_string + or "auth_token=%s; ct0=%s" % (self._auth_token, self._ct0), + }, + timeout=10, ) + home_page.raise_for_status() home_page_response = bs4.BeautifulSoup(home_page.content, "html.parser") ondemand_url = get_ondemand_file_url(response=home_page_response) if not ondemand_url: @@ -1113,6 +1122,7 @@ def _ensure_client_transaction(self): ondemand_file = cffi_session.get( ondemand_url, headers=ct_headers, timeout=10, ) + ondemand_file.raise_for_status() self._client_transaction = ClientTransaction( home_page_response=home_page_response, ondemand_file_response=ondemand_file.text, diff --git a/uv.lock b/uv.lock index 5c2ba17..6db82b2 100644 --- a/uv.lock +++ b/uv.lock @@ -641,7 +641,7 @@ requires-dist = [ { name = "rich", specifier = ">=13.0" }, { name = "ruff", marker = "extra == 'dev'", specifier = ">=0.8" }, { name = "types-pyyaml", marker = "extra == 'dev'", specifier = ">=6.0.12" }, - { name = "xclienttransaction", specifier = ">=1.0.1" }, + { name = "xclienttransaction", specifier = ">=1.0.3" }, ] provides-extras = ["dev"] @@ -677,12 +677,12 @@ wheels = [ [[package]] name = "xclienttransaction" -version = "1.0.1" +version = "1.0.3" source = { registry = "https://pypi.org/simple" } dependencies = [ { name = "beautifulsoup4" }, ] -sdist = { url = "https://files.pythonhosted.org/packages/eb/c7/c841afe6759567cf29e36196dff65c935682b69ae38e986bd14e7decf74e/xclienttransaction-1.0.1.tar.gz", hash = "sha256:e4e032ff27e76ab927c0a7f57ad3630e06b7100c113bdf22b03cdde9584756f3", size = 9062, upload-time = "2025-10-09T10:38:31.485Z" } +sdist = { url = "https://files.pythonhosted.org/packages/1b/11/6f5a55df6d464fe6f839fe96c3863fa2338005ed8e83e89b0a9db5716b5a/xclienttransaction-1.0.3.tar.gz", hash = "sha256:1eeb5d48cec6a224189bbd8a6021c40486fa3aff650478672bccf82b3436cedc", size = 10609, upload-time = "2026-06-26T10:47:39.664Z" } wheels = [ - { url = "https://files.pythonhosted.org/packages/fe/b8/b953828e2f0f658f7f2d6354525728c9569c83937ebe4903f2391f02a702/xclienttransaction-1.0.1-py3-none-any.whl", hash = "sha256:0280e021129a38f8ee2ebede6406d4c065a599430bd4c8aa0c122f2e42f9f0f0", size = 10724, upload-time = "2025-10-09T10:38:30.098Z" }, + { url = "https://files.pythonhosted.org/packages/fa/a1/be735112ebe49916065e6f80017b93e92fc8880789c3f4f17380db7f531a/xclienttransaction-1.0.3-py3-none-any.whl", hash = "sha256:f73dd2a4b6856f98fa45f0cd26c9a0fdc0b2ceba0e79b5523cdf8aac7efc59fe", size = 10778, upload-time = "2026-06-26T10:47:38.401Z" }, ]