Skip to content

Commit 7fe7f34

Browse files
cuonglmvishr
authored andcommitted
Basic scheme is case-insensitive (#1033)
1 parent b28538b commit 7fe7f34

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

middleware/basic_auth.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package middleware
33
import (
44
"encoding/base64"
55
"strconv"
6+
"strings"
67

78
"github.com/labstack/echo"
89
)
@@ -27,7 +28,7 @@ type (
2728
)
2829

2930
const (
30-
basic = "Basic"
31+
basic = "basic"
3132
defaultRealm = "Restricted"
3233
)
3334

@@ -72,7 +73,7 @@ func BasicAuthWithConfig(config BasicAuthConfig) echo.MiddlewareFunc {
7273
auth := c.Request().Header.Get(echo.HeaderAuthorization)
7374
l := len(basic)
7475

75-
if len(auth) > l+1 && auth[:l] == basic {
76+
if len(auth) > l+1 && strings.ToLower(auth[:l]) == basic {
7677
b, err := base64.StdEncoding.DecodeString(auth[l+1:])
7778
if err != nil {
7879
return err

middleware/basic_auth_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/base64"
55
"net/http"
66
"net/http/httptest"
7+
"strings"
78
"testing"
89

910
"github.com/labstack/echo"
@@ -30,6 +31,11 @@ func TestBasicAuth(t *testing.T) {
3031
req.Header.Set(echo.HeaderAuthorization, auth)
3132
assert.NoError(t, h(c))
3233

34+
// Case-insensitive header scheme
35+
auth = strings.ToUpper(basic) + " " + base64.StdEncoding.EncodeToString([]byte("joe:secret"))
36+
req.Header.Set(echo.HeaderAuthorization, auth)
37+
assert.NoError(t, h(c))
38+
3339
// Invalid credentials
3440
auth = basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:invalid-password"))
3541
req.Header.Set(echo.HeaderAuthorization, auth)

0 commit comments

Comments
 (0)