Skip to content

fix: refuse GraphQL writes when x-client-transaction-id is unavailable - #101

Open
oxkage wants to merge 1 commit into
jackwener:mainfrom
oxkage:fix/fail-closed-unsigned-writes
Open

oxkage wants to merge 1 commit into
jackwener:mainfrom
oxkage:fix/fail-closed-unsigned-writes

Conversation

@oxkage

@oxkage oxkage commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

Problem

When the ClientTransaction bootstrap fails (the cases in #73 / #78 / #88: the
homepage fetch, ondemand.s no longer being in the page, the escaped-quote hash
regex), _build_headers() silently omits X-Client-Transaction-Id and every
GraphQL POST goes out as a bare API client.

The failure mode is not a clean error, and that is the dangerous part:

  • writes come back as 344 "You have reached your daily limit" even on accounts
    that have never posted that day
    — it is not a quota, it is X's
    anti-automation response;
  • writes that do go through are followed by the account being suspended a day
    or two later
    (64 Your account is suspended... on the next attempt).

We hit this in production running a daily posting loop on 0.8.5: the
Failed to init ClientTransaction warning was logged on every command, every
other call kept working, so nothing looked broken — and the accounts doing the
posting were suspended. The library gave us no way to notice that a write had
gone out unsigned.

Change

Fail closed in _build_headers():

  • compute the transaction id as before;
  • if no id is available and the request is a POST, raise TwitterAPIError
    with an explicit message instead of sending an unsigned write;
  • GET is unaffected — those endpoints do not need the header;
  • TWITTER_ALLOW_UNSIGNED=1 keeps the old behaviour for anyone who wants it;
  • the init-failure log line now states that the header is unavailable and that
    writes will be refused.

This is deliberately independent of the bootstrap fixes (#98, #99, #89, ...):
whichever of those lands, a future regression in the bootstrap will surface as a
loud error instead of a silent ban.

Tests

New TestUnsignedWriteGuard:

  • POST without a transaction id → TwitterAPIError
  • POST with TWITTER_ALLOW_UNSIGNED=1 → allowed, no header
  • GET without a transaction id → allowed
  • POST with a working generator → header present
  • POST when the generator raises → refused

The media-upload fixture now supplies a transaction id, since INIT / APPEND /
FINALIZE are POSTs too.

uv run --extra dev pytest → 247 passed, 6 deselected.

When ClientTransaction fails to initialise, _build_headers() silently omits
the x-client-transaction-id header and every GraphQL POST goes out as a bare
API client. X answers those with a misleading `344 "You have reached your
daily limit"` even on accounts that have never posted, and accounts whose
writes do get through are suspended shortly afterwards.

Fail closed instead: if no transaction id could be produced, refuse POST
requests with an explicit error, and make the init failure loud in the log.
GET requests are unaffected (they do not require the header). Escape hatch for
deliberate unsigned writes: TWITTER_ALLOW_UNSIGNED=1.

The media-upload test fixture now supplies a transaction id, since INIT/APPEND
/FINALIZE are POSTs too.
axsddlr added a commit to axsddlr/twitter-cli that referenced this pull request Oct 31, 2026
Two advisory fixes for issues that cannot be resolved by code alone.

jackwener#28 / jackwener#66 / jackwener#46 — Windows cookie extraction
`Unable to get key for cookie decryption` on Windows is almost always
Chrome/Edge 127+ **App-Bound Encryption**, which browser_cookie3 cannot unwrap.
The old hint listed DPAPI causes and suggested running as admin, which sends
people down the wrong path. Detect the decrypt diagnostic and give the real
options: use Firefox (still readable), or export auth_token/ct0 from DevTools
or a cookie extension and set them as env vars. The existing DPAPI/VSS-copy
advice is retained as a secondary cause, since it is still valid when the
failure is not ABC.

Not a full fix: decrypting ABC would require a Windows-only key extractor.
That is out of scope here; the issue becomes actionable instead of cryptic.

jackwener#72 — account flagged as a bot
X treats an unsigned write as automation. Writes now log a prominent warning
when x-client-transaction-id is unavailable. Deliberately a warning, not a
refusal: the change may still land, and hard-failing writes (as proposed in
upstream jackwener#101) turns a transient bootstrap failure into a broken command.
Reads stay silent — they usually just 404, so a warning there is noise.

Tests cover both branches, including that a signed write and an unsigned read
do not warn, and that the write still returns headers rather than raising.
axsddlr added a commit to axsddlr/twitter-cli that referenced this pull request Oct 31, 2026
Two advisory fixes for issues that cannot be resolved by code alone.

jackwener#28 / jackwener#66 / jackwener#46 — Windows cookie extraction
`Unable to get key for cookie decryption` on Windows is almost always
Chrome/Edge 127+ **App-Bound Encryption**, which browser_cookie3 cannot unwrap.
The old hint listed DPAPI causes and suggested running as admin, which sends
people down the wrong path. Detect the decrypt diagnostic and give the real
options: use Firefox (still readable), or export auth_token/ct0 from DevTools
or a cookie extension and set them as env vars. The existing DPAPI/VSS-copy
advice is retained as a secondary cause, since it is still valid when the
failure is not ABC.

Not a full fix: decrypting ABC would require a Windows-only key extractor.
That is out of scope here; the issue becomes actionable instead of cryptic.

jackwener#72 — account flagged as a bot
X treats an unsigned write as automation. Writes now log a prominent warning
when x-client-transaction-id is unavailable. Deliberately a warning, not a
refusal: the change may still land, and hard-failing writes (as proposed in
upstream jackwener#101) turns a transient bootstrap failure into a broken command.
Reads stay silent — they usually just 404, so a warning there is noise.

Tests cover both branches, including that a signed write and an unsigned read
do not warn, and that the write still returns headers rather than raising.

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