Skip to content
Open
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
137 changes: 137 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1545,3 +1545,140 @@ def mock_post(operation_name, variables, features=None):

assert captured.get("product") == "Latest"
assert captured.get("querySource") == "typed_query"


# ── ClientTransaction bootstrap (x-client-transaction-id) ───────────────

# Synthetic x.com/home page in the layout X ships since Sept 2026:
# - webpack numeric module map ("59924:\"ondemand.s\"")
# - separate numeric index -> hash map
# - twitter-site-verification meta + loading-x-anim frames
# The key is a valid base64 blob (16 bytes) so get_key_bytes() can decode it.
_CT_HOME_HTML = (
'<html><head>'
'<meta name="twitter-site-verification" content="MTIzNDU2Nzg5MGFiY2RlZmI="/>'
'</head><body>'
'<script>window.__M={0:"unused",59924:"ondemand.s",60041:"i18n"}</script>'
'<script>window.__H={0:"0000000000000000",59924:"38289b77fa31a72c",60041:"deadbeef12345678"}</script>'
'<svg id="loading-x-anim-0"><g><path d="M0 0 C 10 20, 30 40, 50 60 70 80, 90 100, 110 120"/></g></svg>'
'<svg id="loading-x-anim-1"><g><path d="M0 0 C 5 10, 15 25, 25 35 45 45, 55 55, 65 65"/></g></svg>'
'<svg id="loading-x-anim-2"><g><path d="M1 1 C 2 2, 3 3, 4 4 5 5, 6 6, 7 7"/></g></svg>'
'<svg id="loading-x-anim-3"><g><path d="M2 2 C 3 3, 4 4, 5 5 6 6, 7 7, 8 8"/></g></svg>'
'</body></html>'
)

# The current ondemand.s bundle content shape (fetched 2026-09-29). The
# INDICES pattern the library extracts from it is embedded here so the test
# runs without a network call. Note the required space after each comma —
# the INDICES_REGEX is `(x[<1-2 digits>], 16)`-shaped. The real bundle yields
# >=2 matches (row index + frame-time bytes), so this fixture does too.
_CT_ONDEMAND_JS = (
"var a=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,"
"26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45];"
"var b=function(c){return(a[(c[0], 16)],(a[(c[1], 17)]),(a[(c[2], 18)]),"
"(a[(c[3], 19)]),(a[(c[4], 20)]));};var d=b(a);"
)


class TestClientTransactionBootstrap:
"""Regression: CT must bootstrap from /home (with the session cookie),
not from the stripped root page, and must send the cookie on the
bootstrap request."""

def _make_client(self, cookie_string=None):
client = TwitterClient.__new__(TwitterClient)
client._auth_token = "auth_tok"
client._ct0 = "ct0_val"
client._cookie_string = cookie_string
client._client_transaction = None
client._ct_init_attempted = False
client._load_ct_cache = MagicMock(return_value=False)
client._save_ct_cache = MagicMock()
return client

@patch("twitter_cli.client._update_features_from_html")
@patch("twitter_cli.client.ClientTransaction")
@patch("twitter_cli.client._gen_ct_headers", return_value={"Referer": "https://x.com"})
@patch("twitter_cli.client._get_cffi_session")
def test_bootstraps_from_home_with_cookie(
self, mock_session, mock_ct_headers, mock_tx, mock_features
):
"""First request must be x.com/home carrying the session cookie, and
the second must be the resolved ondemand.s bundle URL."""
import bs4

home_page = MagicMock(
content=_CT_HOME_HTML.encode("utf-8"), text=_CT_HOME_HTML
)
ondemand_page = MagicMock(text=_CT_ONDEMAND_JS)

mock_session.return_value.get.side_effect = [home_page, ondemand_page]

client = self._make_client(cookie_string="auth_token=auth_tok; ct0=ct0_val")
client._ensure_client_transaction()

first = mock_session.return_value.get.call_args_list[0]
second = mock_session.return_value.get.call_args_list[1]
# 1. The bootstrap hit the responsive /home route, not the root shell.
assert first.args[0] == "https://x.com/home"
# 2. The session cookie was sent on the bootstrap request.
headers = first.kwargs.get("headers", {})
assert headers.get("Cookie") == "auth_token=auth_tok; ct0=ct0_val"
# 3. The ondemand bundle URL was resolved from the /home page
# (only possible because the module + hash maps are present).
assert second.args[0] == (
"https://abs.twimg.com/responsive-web/client-web/"
"ondemand.s.38289b77fa31a72ca.js"
)
# 4. The ClientTransaction object was constructed from the /home page.
mock_tx.assert_called_once()
constructed_page = mock_tx.call_args.kwargs["home_page_response"]
assert isinstance(constructed_page, bs4.BeautifulSoup)

@patch("twitter_cli.client._update_features_from_html")
@patch("twitter_cli.client.ClientTransaction")
@patch("twitter_cli.client._gen_ct_headers", return_value={"Referer": "https://x.com"})
@patch("twitter_cli.client._get_cffi_session")
def test_falls_back_to_auth_token_cookie(
self, mock_session, mock_ct_headers, mock_tx, mock_features
):
"""When no full browser cookie string exists, the bootstrap still
sends auth_token+ct0 so the /home page is not stripped."""
home_page = MagicMock(
content=_CT_HOME_HTML.encode("utf-8"), text=_CT_HOME_HTML
)
home_page.content = _CT_HOME_HTML.encode("utf-8")
ondemand_page = MagicMock(text=_CT_ONDEMAND_JS)
mock_session.return_value.get.side_effect = [home_page, ondemand_page]

client = self._make_client(cookie_string=None)
client._ensure_client_transaction()

headers = mock_session.return_value.get.call_args_list[0].kwargs.get("headers", {})
assert headers.get("Cookie") == "auth_token=auth_tok; ct0=ct0_val"

@patch("twitter_cli.client._update_features_from_html")
@patch("twitter_cli.client.ClientTransaction")
@patch("twitter_cli.client._gen_ct_headers", return_value={"Referer": "https://x.com"})
@patch("twitter_cli.client._get_cffi_session")
def test_real_parser_resolves_ondemand_from_current_layout(
self, mock_session, mock_ct_headers, mock_tx, mock_features
):
"""The real (unmocked) x_client_transaction parser must resolve the
ondemand.s URL from a page in the current numeric-module layout.

This is the exact call that raised AttributeError under the pinned
xclienttransaction 1.0.1 (its regex expects the old
'"ondemand.s": "<hash>"' name->hash map, not the numeric index
layout X ships since Sept 2026). The floor bump in pyproject.toml
(>=1.0.3) is what makes this pass.
"""
from x_client_transaction.utils import get_ondemand_file_url
import bs4

soup = bs4.BeautifulSoup(_CT_HOME_HTML, "html.parser")
url = get_ondemand_file_url(response=soup)
assert url == (
"https://abs.twimg.com/responsive-web/client-web/"
"ondemand.s.38289b77fa31a72ca.js"
)
22 changes: 22 additions & 0 deletions tests/test_smoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,25 @@ def test_feed(self):
result, payload = _invoke("feed", "--max", "3")
assert result.exit_code == 0, f"feed failed: {result.output}"
assert payload["ok"] is True


@smoke
class TestCTGatedReads:
"""Endpoints that 404 without the x-client-transaction-id header.

These were broken (HTTP 404 on SearchTimeline/Followers/Following/Likes)
because the CT bootstrap fetched the stripped root page. They are the
live regression guard for the /home+cookie bootstrap.
"""

def test_followers(self):
result, payload = _invoke("followers", "elonmusk", "--max", "3")
assert result.exit_code == 0, f"followers failed: {result.output}"
assert payload["ok"] is True
assert len(payload["data"]) >= 1

def test_following(self):
result, payload = _invoke("following", "elonmusk", "--max", "3")
assert result.exit_code == 0, f"following failed: {result.output}"
assert payload["ok"] is True
assert len(payload["data"]) >= 1
14 changes: 13 additions & 1 deletion twitter_cli/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1103,8 +1103,20 @@ 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 root path serves a lightweight logged-out shell without the
# ondemand.s bundle. The responsive /home route exposes it, but
# only when a session cookie is sent (verified 2026-09-29:
# /home without a cookie is a 17KB stripped shell, with a cookie
# it is a 305KB page containing the module + hash maps).
# The generated transaction id is derived from public page data,
# so sending the cookie here does not weaken it — it is only the
# difference between a header that works and a 404 on every
# header-gated GET (SearchTimeline, Followers, Following, Likes).
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,
"https://x.com/home", headers=ct_headers, timeout=10,
)
home_page_response = bs4.BeautifulSoup(home_page.content, "html.parser")
ondemand_url = get_ondemand_file_url(response=home_page_response)
Expand Down
24 changes: 23 additions & 1 deletion twitter_cli/graphql.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,35 @@ def _scan_bundles(url_fetch_fn):
try:
from .constants import get_user_agent
html = url_fetch_fn("https://x.com", {"user-agent": get_user_agent()})
# Match both the legacy responsive-web/client-web layout and the new
# x-web/x-web layout (X moved the web build to /x-web/ in 2026).
script_pattern = re.compile(
r'(?:src|href)=["\']'
r'(https://abs\.twimg\.com/responsive-web/client-web[^"\']+'
r'(https://abs\.twimg\.com/(?:responsive-web/client-web|x-web/x-web)[^"\']+'
r'\.js)'
r'["\']'
)
script_urls = script_pattern.findall(html)
# The x-web entry bundle references its code as relative ./assets/*.js
# chunks — follow those so any queryId/operationName pairs stay
# reachable as the build layout evolves.
if script_urls:
try:
from curl_cffi import requests as _cc_requests

_sess: Any = _cc_requests.Session(impersonate="chrome")
for script_url in list(script_urls):
if "/x-web/x-web/" not in script_url:
continue
entry = _sess.get(script_url, timeout=20).text
for rel in re.findall(
r'["\']\./assets/([a-zA-Z0-9._\-]+\.js)["\']', entry
):
full = script_url.rsplit("/", 1)[0] + "/" + rel
if full not in script_urls:
script_urls.append(full)
except Exception as _exc:
logger.debug("x-web asset follow-up scan failed: %s", _exc)
except Exception as exc: # pragma: no cover - network-dependent branch
logger.warning("Failed to scan JS bundles: %s", exc)
return
Expand Down
10 changes: 5 additions & 5 deletions uv.lock

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