Skip to content

Commit 1ed89e7

Browse files
author
Welling Guzman
committed
fix: basic auth invalid base64 string
fixes #2170
1 parent d5f8837 commit 1ed89e7

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

middleware/basic_auth.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,9 @@ func BasicAuthWithConfig(config BasicAuthConfig) echo.MiddlewareFunc {
7474
l := len(basic)
7575

7676
if len(auth) > l+1 && strings.EqualFold(auth[:l], basic) {
77-
b, err := base64.StdEncoding.DecodeString(auth[l+1:])
78-
if err != nil {
79-
return err
80-
}
77+
// Invalid base64 shouldn't be treated as error
78+
// instead should be treated as invalid client input
79+
b, _ := base64.StdEncoding.DecodeString(auth[l+1:])
8180
cred := string(b)
8281
for i := 0; i < len(cred); i++ {
8382
if cred[i] == ':' {

middleware/basic_auth_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ func TestBasicAuth(t *testing.T) {
5858
assert.Equal(http.StatusUnauthorized, he.Code)
5959
assert.Equal(basic+` realm="someRealm"`, res.Header().Get(echo.HeaderWWWAuthenticate))
6060

61+
// Invalid base64 string
62+
auth = basic + " invalidString"
63+
req.Header.Set(echo.HeaderAuthorization, auth)
64+
he = h(c).(*echo.HTTPError)
65+
assert.Equal(http.StatusUnauthorized, he.Code)
66+
assert.Equal(basic+` realm="someRealm"`, res.Header().Get(echo.HeaderWWWAuthenticate))
67+
6168
// Missing Authorization header
6269
req.Header.Del(echo.HeaderAuthorization)
6370
he = h(c).(*echo.HTTPError)

0 commit comments

Comments
 (0)