Skip to content

Commit b5e5df6

Browse files
3point2k8s-publishing-bot
authored andcommitted
Fix SPDY proxy authentication with special chars
The username and password sent in the Proxy-Authorization header are not supposed to be percent escaped prior to being base64 encoded. Kubernetes-commit: bbb5513b3b4c956c486685886634c71ce7c31b9f
1 parent 553a2d6 commit b5e5df6

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

pkg/util/httpstream/spdy/roundtripper.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,10 @@ func (s *SpdyRoundTripper) proxyAuth(proxyURL *url.URL) string {
297297
if proxyURL == nil || proxyURL.User == nil {
298298
return ""
299299
}
300-
credentials := proxyURL.User.String()
301-
encodedAuth := base64.StdEncoding.EncodeToString([]byte(credentials))
302-
return fmt.Sprintf("Basic %s", encodedAuth)
300+
username := proxyURL.User.Username()
301+
password, _ := proxyURL.User.Password()
302+
auth := username + ":" + password
303+
return "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
303304
}
304305

305306
// RoundTrip executes the Request and upgrades it. After a successful upgrade,

pkg/util/httpstream/spdy/roundtripper_test.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"context"
2121
"crypto/tls"
2222
"crypto/x509"
23-
"encoding/base64"
2423
"io"
2524
"net"
2625
"net/http"
@@ -291,6 +290,16 @@ func TestRoundTripAndNewConnection(t *testing.T) {
291290
serverStatusCode: http.StatusSwitchingProtocols,
292291
shouldError: false,
293292
},
293+
"proxied valid https, proxy auth with chars that percent escape -> valid https": {
294+
serverFunc: httpsServerValidHostname(t),
295+
proxyServerFunc: httpsServerValidHostname(t),
296+
proxyAuth: url.UserPassword("proxy user", "proxypasswd%"),
297+
clientTLS: &tls.Config{RootCAs: localhostPool},
298+
serverConnectionHeader: "Upgrade",
299+
serverUpgradeHeader: "SPDY/3.1",
300+
serverStatusCode: http.StatusSwitchingProtocols,
301+
shouldError: false,
302+
},
294303
}
295304

296305
for k, testCase := range testCases {
@@ -400,18 +409,19 @@ func TestRoundTripAndNewConnection(t *testing.T) {
400409
}
401410
}
402411

403-
var expectedProxyAuth string
404412
if testCase.proxyAuth != nil {
405-
encodedCredentials := base64.StdEncoding.EncodeToString([]byte(testCase.proxyAuth.String()))
406-
expectedProxyAuth = "Basic " + encodedCredentials
407-
}
408-
if len(expectedProxyAuth) == 0 && proxyCalledWithAuth {
413+
expectedUsername := testCase.proxyAuth.Username()
414+
expectedPassword, _ := testCase.proxyAuth.Password()
415+
username, password, ok := (&http.Request{Header: http.Header{"Authorization": []string{proxyCalledWithAuthHeader}}}).BasicAuth()
416+
if !ok {
417+
t.Fatalf("invalid proxy auth header %s", proxyCalledWithAuthHeader)
418+
}
419+
if username != expectedUsername || password != expectedPassword {
420+
t.Fatalf("expected proxy auth \"%s:%s\", got \"%s:%s\"", expectedUsername, expectedPassword, username, password)
421+
}
422+
} else if proxyCalledWithAuth {
409423
t.Fatalf("proxy authorization unexpected, got %q", proxyCalledWithAuthHeader)
410424
}
411-
if proxyCalledWithAuthHeader != expectedProxyAuth {
412-
t.Fatalf("expected to see a call to the proxy with credentials %q, got %q", testCase.proxyAuth, proxyCalledWithAuthHeader)
413-
}
414-
415425
})
416426
}
417427
}

0 commit comments

Comments
 (0)