Skip to content

Commit 9bd1c7a

Browse files
author
Welling Guzman
committed
fix: basic auth returns 400 on invalid base64 string
1 parent 1ed89e7 commit 9bd1c7a

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

middleware/basic_auth.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/base64"
55
"strconv"
66
"strings"
7+
"net/http"
78

89
"github.com/labstack/echo/v4"
910
)
@@ -76,7 +77,11 @@ func BasicAuthWithConfig(config BasicAuthConfig) echo.MiddlewareFunc {
7677
if len(auth) > l+1 && strings.EqualFold(auth[:l], basic) {
7778
// Invalid base64 shouldn't be treated as error
7879
// instead should be treated as invalid client input
79-
b, _ := base64.StdEncoding.DecodeString(auth[l+1:])
80+
b, err := base64.StdEncoding.DecodeString(auth[l+1:])
81+
if err != nil {
82+
return echo.NewHTTPError(http.StatusBadRequest).SetInternal(err)
83+
}
84+
8085
cred := string(b)
8186
for i := 0; i < len(cred); i++ {
8287
if cred[i] == ':' {

middleware/basic_auth_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ func TestBasicAuth(t *testing.T) {
6262
auth = basic + " invalidString"
6363
req.Header.Set(echo.HeaderAuthorization, auth)
6464
he = h(c).(*echo.HTTPError)
65-
assert.Equal(http.StatusUnauthorized, he.Code)
66-
assert.Equal(basic+` realm="someRealm"`, res.Header().Get(echo.HeaderWWWAuthenticate))
65+
assert.Equal(http.StatusBadRequest, he.Code)
6766

6867
// Missing Authorization header
6968
req.Header.Del(echo.HeaderAuthorization)

0 commit comments

Comments
 (0)