diff --git a/xdk-gen/src/typescript/generator.rs b/xdk-gen/src/typescript/generator.rs index 5fbd10b0..eea8b8a4 100644 --- a/xdk-gen/src/typescript/generator.rs +++ b/xdk-gen/src/typescript/generator.rs @@ -85,6 +85,7 @@ language! { render "test_contracts" => "tests/{}/test_contracts.test.ts", render "test_pagination" => "tests/{}/test_pagination.test.ts" }, + render "test_oauth1_signature" => "tests/oauth1_signature.test.ts", render "test_client_errors" => "tests/client_errors.test.ts", render "jest.config" => "jest.config.cjs" ] diff --git a/xdk-gen/templates/typescript/oauth1_auth.j2 b/xdk-gen/templates/typescript/oauth1_auth.j2 index 2a67ee85..7f0fcd1b 100644 --- a/xdk-gen/templates/typescript/oauth1_auth.j2 +++ b/xdk-gen/templates/typescript/oauth1_auth.j2 @@ -239,8 +239,14 @@ export class OAuth1 { // If body is JSON, we don't include it in the signature (per OAuth1 spec) } - // Sort parameters alphabetically - const sortedParams = Object.entries(allParams).sort(([a], [b]) => a.localeCompare(b)); + // Sort by percent-encoded name, then value, in byte order (RFC 5849 section 3.4.1.3.2). + // localeCompare is locale-aware and orders punctuation differently (e.g. '.' vs '_'). + const compareBytes = (x: string, y: string): number => (x < y ? -1 : x > y ? 1 : 0); + const sortedParams = Object.entries(allParams).sort( + ([aKey, aValue], [bKey, bValue]) => + compareBytes(this._encode(aKey), this._encode(bKey)) || + compareBytes(this._encode(aValue), this._encode(bValue)) + ); return sortedParams .map(([key, value]) => `${this._encode(key)}=${this._encode(value)}`) diff --git a/xdk-gen/templates/typescript/test_oauth1_signature.j2 b/xdk-gen/templates/typescript/test_oauth1_signature.j2 new file mode 100644 index 00000000..f23e8223 --- /dev/null +++ b/xdk-gen/templates/typescript/test_oauth1_signature.j2 @@ -0,0 +1,82 @@ +/** + * AUTO-GENERATED FILE - DO NOT EDIT + * This file was automatically generated by the XDK build tool. + * Any manual changes will be overwritten on the next generation. + * + * Auto-generated tests for the OAuth1 signature base string. The expected + * signature is computed independently (node:crypto over a hand-written base + * string) so the test pins the parameter ordering required by RFC 5849 + * section 3.4.1.3.2: percent-encoded names sorted in byte order. + * + * Generated automatically - do not edit manually. + */ + +import { describe, it, expect } from '@jest/globals'; +import { createHmac } from 'node:crypto'; +import { OAuth1 } from '../src/oauth1_auth.js'; + +const oauth1 = new OAuth1({ + apiKey: 'ck', + apiSecret: 'cs', + callback: 'oob', + accessToken: 'at', + accessTokenSecret: 'ats', +}); + +/** Reads a (percent-decoded) parameter from an OAuth Authorization header. */ +const headerParam = (header: string, name: string): string => { + const match = header.match(new RegExp(`${name}="([^"]*)"`)); + if (!match) { + throw new Error(`${name} missing from header: ${header}`); + } + return decodeURIComponent(match[1]); +}; + +/** HMAC-SHA1 signature of a signature base string, using the test secrets. */ +const sign = (baseString: string): string => + createHmac('sha1', 'cs&ats').update(baseString).digest('base64'); + +describe('OAuth1 signature base string ordering', () => { + it('sorts "." before "_" in query parameter names (byte order, not locale order)', async () => { + const header = await oauth1.buildRequestHeader( + 'GET', + 'https://api.x.com/2/media?media_keys=3_1&media.fields=url' + ); + const nonce = headerParam(header, 'oauth_nonce'); + const timestamp = headerParam(header, 'oauth_timestamp'); + + // media.fields ('.' = 0x2E) sorts before media_keys ('_' = 0x5F). + const baseString = + 'GET&https%3A%2F%2Fapi.x.com%2F2%2Fmedia&' + + 'media.fields%3Durl%26media_keys%3D3_1' + + '%26oauth_consumer_key%3Dck' + + `%26oauth_nonce%3D${nonce}` + + '%26oauth_signature_method%3DHMAC-SHA1' + + `%26oauth_timestamp%3D${timestamp}` + + '%26oauth_token%3Dat' + + '%26oauth_version%3D1.0'; + + expect(headerParam(header, 'oauth_signature')).toBe(sign(baseString)); + }); + + it('orders names with uppercase letters before lowercase ones', async () => { + const header = await oauth1.buildRequestHeader( + 'GET', + 'https://api.x.com/2/tweets?b=2&a=1&B=3&A=4' + ); + const nonce = headerParam(header, 'oauth_nonce'); + const timestamp = headerParam(header, 'oauth_timestamp'); + + const baseString = + 'GET&https%3A%2F%2Fapi.x.com%2F2%2Ftweets&' + + 'A%3D4%26B%3D3%26a%3D1%26b%3D2' + + '%26oauth_consumer_key%3Dck' + + `%26oauth_nonce%3D${nonce}` + + '%26oauth_signature_method%3DHMAC-SHA1' + + `%26oauth_timestamp%3D${timestamp}` + + '%26oauth_token%3Dat' + + '%26oauth_version%3D1.0'; + + expect(headerParam(header, 'oauth_signature')).toBe(sign(baseString)); + }); +});