Skip to content

fix(typescript): sort OAuth1 signature parameters in byte order - #45

Open
OsamaAnsar wants to merge 1 commit into
xdevplatform:mainfrom
OsamaAnsar:fix/typescript-oauth1-param-sort
Open

OsamaAnsar wants to merge 1 commit into
xdevplatform:mainfrom
OsamaAnsar:fix/typescript-oauth1-param-sort

Conversation

@OsamaAnsar

Copy link
Copy Markdown

Problem

OAuth1._buildParamString (xdk-gen/templates/typescript/oauth1_auth.j2) sorts the signature parameters with Object.entries(allParams).sort(([a],[b]) => a.localeCompare(b)). RFC 5849 section 3.4.1.3.2 says to sort the percent-encoded names in ascending byte order (then by value). localeCompare is locale-aware and orders punctuation differently from byte order, so whenever two parameter names differ at a . vs _ (or upper vs lower case), the SDK builds a different signature base string than the spec.

The X API spec has query-parameter pairs where this changes the order on these endpoints (found by scanning specs/openapi.json for pairs whose localeCompare order differs from byte order, including the oauth_* parameters):

  • GET /2/media (media_keys vs media.fields)
  • GET /2/spaces/by/creator_ids (user_ids vs user.fields)
  • GET /2/notes/search/posts_eligible_for_notes (post_selection vs post.fields)

(In this spec /2/media and /2/notes/search/posts_eligible_for_notes list UserToken (OAuth 1.0a); /2/spaces/by/creator_ids lists only OAuth2UserToken and BearerToken, so for that one this only matters if someone uses OAuth1 with it anyway.) I have not called the live API, so I can't say whether the server rejects the old signatures; what I can show is that they differ from an independent RFC reference.

Repro (against oauthlib 4.0.0 as an independent reference, same nonce and timestamp)

GET https://api.x.com/2/media?media_keys=3_1&media.fields=url

before: MISMATCH  sdk oe0J8xzROgI1CD+1pyMk7iwp4/0=   ref xmb/Gh5V2aT/u9DvQ+g/SZ9JQFs=
after:  MATCH     sdk GhWxXQeOXcz8C2WVHMBp5p/M7Fs=   ref GhWxXQeOXcz8C2WVHMBp5p/M7Fs=

(Nonce and timestamp are random per run, so the values differ between the two runs; within each run SDK and reference use the same nonce/timestamp.) The other two endpoints above and a form-encoded POST body with a_b, a.c, a-d, A also go from MISMATCH to MATCH; parameter sets with no ordering difference (/2/users/by?usernames=...&user.fields=..., /2/tweets?ids=...&tweet.fields=...&expansions=...) match both before and after.

Fix

Sort by the percent-encoded name, then the percent-encoded value, comparing code units (a < b ? -1 : a > b ? 1 : 0); the encoded strings are ASCII so this is byte order. This changes only the sort order. The emitted key=value strings and everything else are unchanged. Not an auth/confidentiality change: it only makes the signature input match the RFC.

Tests

New test template test_oauth1_signature.j2 (registered in generator.rs) writes tests/oauth1_signature.test.ts. It builds a request header, then recomputes the expected signature with node:crypto HMAC-SHA1 over a hand-written RFC-ordered base string (nonce/timestamp read from the header), for a media.fields/media_keys URL and for mixed-case names.

How this was verified

I do not have a Rust toolchain, so I did not run the generator or cargo test / cargo fmt / cargo clippy; CI will regenerate the SDK. I applied the identical change by hand to a copy of the current xdk-typescript output (v0.6.6), compiled it with tsc, and compared signatures against oauthlib (above). The new test file, run there with jest: before the fix 2 failed / 2 total, after 2 passed / 2 total. Full jest suite on the patched copy: 1155 passing, tsc --noEmit clean.

The only Rust change is one added render line in the tests: list.

The OAuth1 signature base string sorted parameters with localeCompare on
the raw names. RFC 5849 section 3.4.1.3.2 requires the percent-encoded
names to be sorted in ascending byte order (then by value). localeCompare
is locale-aware and orders punctuation differently, so for names such as
`media_keys` and `media.fields` the SDK built a different base string
than the RFC reference and therefore a different signature.

Compare the encoded name, then the encoded value, by code unit instead.
Adds a jest test that checks the header signature against an HMAC
computed over a hand-written, RFC-ordered base string.
@CLAassistant

CLAassistant commented Oct 21, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

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.

2 participants