Repository navigation
fix(typescript): sort OAuth1 signature parameters in byte order - #45
Open
OsamaAnsar wants to merge 1 commit into
Open
OsamaAnsar wants to merge 1 commit into
OsamaAnsar wants to merge 1 commit into
Conversation
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.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
OAuth1._buildParamString(xdk-gen/templates/typescript/oauth1_auth.j2) sorts the signature parameters withObject.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).localeCompareis 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.jsonfor pairs whoselocaleCompareorder differs from byte order, including theoauth_*parameters):GET /2/media(media_keysvsmedia.fields)GET /2/spaces/by/creator_ids(user_idsvsuser.fields)GET /2/notes/search/posts_eligible_for_notes(post_selectionvspost.fields)(In this spec
/2/mediaand/2/notes/search/posts_eligible_for_noteslistUserToken(OAuth 1.0a);/2/spaces/by/creator_idslists 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(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,Aalso 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 emittedkey=valuestrings 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 ingenerator.rs) writestests/oauth1_signature.test.ts. It builds a request header, then recomputes the expected signature withnode:cryptoHMAC-SHA1 over a hand-written RFC-ordered base string (nonce/timestamp read from the header), for amedia.fields/media_keysURL 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 currentxdk-typescriptoutput (v0.6.6), compiled it withtsc, 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 --noEmitclean.The only Rust change is one added
renderline in thetests:list.