Commit eed79f46 authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

net/http: ignore case of basic auth scheme in Request.BasicAuth

RFC 2617, Section 1.2: "It uses an extensible, case-insensitive
token to identify the authentication scheme"

RFC 7617, Section 2: "Note that both scheme and parameter names are
matched case-insensitively."

Fixes #22736

Change-Id: I825d6dbd4fef0f1c6add89f0cbdb56a03eae9443
Reviewed-on: https://go-review.googlesource.com/111516Reviewed-by: 's avatarDmitri Shuralyov <dmitri@shuralyov.com>
Reviewed-by: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 73e38303
...@@ -858,7 +858,8 @@ func (r *Request) BasicAuth() (username, password string, ok bool) { ...@@ -858,7 +858,8 @@ func (r *Request) BasicAuth() (username, password string, ok bool) {
// "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" returns ("Aladdin", "open sesame", true). // "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" returns ("Aladdin", "open sesame", true).
func parseBasicAuth(auth string) (username, password string, ok bool) { func parseBasicAuth(auth string) (username, password string, ok bool) {
const prefix = "Basic " const prefix = "Basic "
if !strings.HasPrefix(auth, prefix) { // Case insensitive prefix match. See Issue 22736.
if len(auth) < len(prefix) || !strings.EqualFold(auth[:len(prefix)], prefix) {
return return
} }
c, err := base64.StdEncoding.DecodeString(auth[len(prefix):]) c, err := base64.StdEncoding.DecodeString(auth[len(prefix):])
......
...@@ -607,6 +607,11 @@ var parseBasicAuthTests = []struct { ...@@ -607,6 +607,11 @@ var parseBasicAuthTests = []struct {
ok bool ok bool
}{ }{
{"Basic " + base64.StdEncoding.EncodeToString([]byte("Aladdin:open sesame")), "Aladdin", "open sesame", true}, {"Basic " + base64.StdEncoding.EncodeToString([]byte("Aladdin:open sesame")), "Aladdin", "open sesame", true},
// Case doesn't matter:
{"BASIC " + base64.StdEncoding.EncodeToString([]byte("Aladdin:open sesame")), "Aladdin", "open sesame", true},
{"basic " + base64.StdEncoding.EncodeToString([]byte("Aladdin:open sesame")), "Aladdin", "open sesame", true},
{"Basic " + base64.StdEncoding.EncodeToString([]byte("Aladdin:open:sesame")), "Aladdin", "open:sesame", true}, {"Basic " + base64.StdEncoding.EncodeToString([]byte("Aladdin:open:sesame")), "Aladdin", "open:sesame", true},
{"Basic " + base64.StdEncoding.EncodeToString([]byte(":")), "", "", true}, {"Basic " + base64.StdEncoding.EncodeToString([]byte(":")), "", "", true},
{"Basic" + base64.StdEncoding.EncodeToString([]byte("Aladdin:open sesame")), "", "", false}, {"Basic" + base64.StdEncoding.EncodeToString([]byte("Aladdin:open sesame")), "", "", false},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment