File tree Expand file tree Collapse file tree 2 files changed +11
-1
lines changed Expand file tree Collapse file tree 2 files changed +11
-1
lines changed Original file line number Diff line number Diff line change 4
4
"encoding/base64"
5
5
"strconv"
6
6
"strings"
7
+ "net/http"
7
8
8
9
"github.com/labstack/echo/v4"
9
10
)
@@ -74,10 +75,13 @@ func BasicAuthWithConfig(config BasicAuthConfig) echo.MiddlewareFunc {
74
75
l := len (basic )
75
76
76
77
if len (auth ) > l + 1 && strings .EqualFold (auth [:l ], basic ) {
78
+ // Invalid base64 shouldn't be treated as error
79
+ // instead should be treated as invalid client input
77
80
b , err := base64 .StdEncoding .DecodeString (auth [l + 1 :])
78
81
if err != nil {
79
- return err
82
+ return echo . NewHTTPError ( http . StatusBadRequest ). SetInternal ( err )
80
83
}
84
+
81
85
cred := string (b )
82
86
for i := 0 ; i < len (cred ); i ++ {
83
87
if cred [i ] == ':' {
Original file line number Diff line number Diff line change @@ -58,6 +58,12 @@ func TestBasicAuth(t *testing.T) {
58
58
assert .Equal (http .StatusUnauthorized , he .Code )
59
59
assert .Equal (basic + ` realm="someRealm"` , res .Header ().Get (echo .HeaderWWWAuthenticate ))
60
60
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 .StatusBadRequest , he .Code )
66
+
61
67
// Missing Authorization header
62
68
req .Header .Del (echo .HeaderAuthorization )
63
69
he = h (c ).(* echo.HTTPError )
You can’t perform that action at this time.
0 commit comments