From f5be73f32ee2b54fa388f71e5c949b535330a95f Mon Sep 17 00:00:00 2001 From: Wade Fletcher Date: Wed, 25 Nov 2026 19:43:25 +0000 Subject: [PATCH] fix(auth): percent-encode OAuth 1.0a parameters per RFC 5849 encode() used url.QueryEscape, which writes a space as + instead of %20, so every OAuth 1.0a request whose query contained a space, such as a multi-word search, was signed over a different base string than X computes and failed with 401. X also rejects a signed request whose query carries a raw reserved character, such as the colon in query=from:XDevelopers, so send the query in the same encoding the signature covers. Amp-Thread-ID: https://ampcode.com/threads/T-7e242faa-c06f-4a65-a920-3fcacb7f8810 Co-authored-by: Wade Fletcher --- api/client.go | 9 ++++++++- api/client_test.go | 8 ++++++++ auth/auth.go | 4 +++- auth/auth_test.go | 24 +++++++++++++++++++++++- 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/api/client.go b/api/client.go index fd34ee4..7fff9fd 100644 --- a/api/client.go +++ b/api/client.go @@ -301,6 +301,13 @@ func (c *ApiClient) buildBaseRequest(method, endpoint string, body io.Reader, co return nil, xurlErrors.NewHTTPError(err) } + // Send the query string in the encoding the OAuth 1.0a signature covers. X + // rejects a signed request whose query carries a raw reserved character, + // such as the colon in query=from:XDevelopers. + if req.URL.RawQuery != "" { + req.URL.RawQuery = strings.ReplaceAll(req.URL.Query().Encode(), "+", "%20") + } + // Add headers for _, header := range headers { parts := strings.SplitN(header, ":", 2) @@ -320,7 +327,7 @@ func (c *ApiClient) buildBaseRequest(method, endpoint string, body io.Reader, co // opts into unauthenticated requests (allowUnauthenticated, set only by // library/test constructors), where we proceed and let the server decide. if req.Header.Get("Authorization") == "" { - authHeader, err := c.getAuthHeader(httpMethod, url, authType, username) + authHeader, err := c.getAuthHeader(httpMethod, req.URL.String(), authType, username) if err != nil { if !c.allowUnauthenticated { return nil, err diff --git a/api/client_test.go b/api/client_test.go index b8bd67a..e1f1678 100644 --- a/api/client_test.go +++ b/api/client_test.go @@ -122,6 +122,14 @@ func TestBuildRequest(t *testing.T) { wantURL: "https://api.x.com/2/tweets", wantErr: false, }, + { + name: "Query is sent percent-encoded as it is signed", + method: "GET", + endpoint: "/2/tweets/search/recent?query=from:XDevelopers+-is:retweet&max_results=10", + authType: "app", + wantMethod: "GET", + wantURL: "https://api.x.com/2/tweets/search/recent?max_results=10&query=from%3AXDevelopers%20-is%3Aretweet", + }, { name: "Absolute URL", method: "GET", diff --git a/auth/auth.go b/auth/auth.go index 32a6eaa..0551a47 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -659,8 +659,10 @@ func generateTimestamp() string { return fmt.Sprintf("%d", time.Now().Unix()) } +// encode percent-encodes s as OAuth 1.0a requires (RFC 5849, section 3.6): +// every byte except the unreserved characters, with a space as %20, not +. func encode(s string) string { - return url.QueryEscape(s) + return strings.ReplaceAll(url.QueryEscape(s), "+", "%20") } func generateCodeVerifierAndChallenge() (string, string, error) { diff --git a/auth/auth_test.go b/auth/auth_test.go index ef1aedf..ed99b28 100644 --- a/auth/auth_test.go +++ b/auth/auth_test.go @@ -124,7 +124,8 @@ func TestEncode(t *testing.T) { expected string }{ {"abc", "abc"}, - {"a b c", "a+b+c"}, + {"a b c", "a%20b%20c"}, + {"from:XDevelopers", "from%3AXDevelopers"}, {"a+b+c", "a%2Bb%2Bc"}, {"a/b/c", "a%2Fb%2Fc"}, {"a?b=c", "a%3Fb%3Dc"}, @@ -139,6 +140,27 @@ func TestEncode(t *testing.T) { } } +// TestGenerateSignature checks the worked example in X's "Creating a +// signature" guide, whose status contains spaces. +func TestGenerateSignature(t *testing.T) { + params := map[string]string{ + "status": "Hello Ladies + Gentlemen, a signed OAuth request!", + "include_entities": "true", + "oauth_consumer_key": "xvz1evFS4wEEPTGEFPHBog", + "oauth_nonce": "kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg", + "oauth_signature_method": "HMAC-SHA1", + "oauth_timestamp": "1318622958", + "oauth_token": "370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb", + "oauth_version": "1.0", + } + + signature, err := generateSignature("POST", "https://api.twitter.com/1.1/statuses/update.json", params, + "kAcSOqF21Fu85e7zjz7ZN2U4ZRhfV3WpwPAoE3Z7kBw", "LswwdoUaIvS8ltyTt5jkRh4J50vUPVVHtR2YPi5kE") + + require.NoError(t, err) + assert.Equal(t, "hCtSmYh+iHYCEqBWrE7C7hYmtUk=", signature) +} + func TestGenerateCodeVerifierAndChallenge(t *testing.T) { verifier, challenge, err := generateCodeVerifierAndChallenge()