Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]

Expand Down
31 changes: 31 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="<html>authenticated homepage</html>")
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={})
Expand Down
12 changes: 11 additions & 1 deletion twitter_cli/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1103,16 +1103,26 @@ 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:
raise ValueError("Failed to extract ondemand file URL from homepage")
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,
Expand Down
8 changes: 4 additions & 4 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.