Skip to content

Commit 55d7e53

Browse files
silverwind6543zeripath
authored
Fix panic in BasicAuthDecode (#14046) (#14048)
* Fix panic in BasicAuthDecode If the string does not contain ":" that function would run into an `index out of range [1] with length 1` error. prevent that. * Update BasicAuthDecode() Co-authored-by: 6543 <[email protected]> Co-authored-by: 6543 <[email protected]> Co-authored-by: zeripath <[email protected]>
1 parent 96d4128 commit 55d7e53

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

modules/base/tool.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"crypto/sha256"
1111
"encoding/base64"
1212
"encoding/hex"
13+
"errors"
1314
"fmt"
1415
"net/http"
1516
"net/url"
@@ -65,6 +66,11 @@ func BasicAuthDecode(encoded string) (string, string, error) {
6566
}
6667

6768
auth := strings.SplitN(string(s), ":", 2)
69+
70+
if len(auth) != 2 {
71+
return "", "", errors.New("invalid basic authentication")
72+
}
73+
6874
return auth[0], auth[1], nil
6975
}
7076

modules/base/tool_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ func TestBasicAuthDecode(t *testing.T) {
4646
assert.NoError(t, err)
4747
assert.Equal(t, "foo", user)
4848
assert.Equal(t, "bar", pass)
49+
50+
_, _, err = BasicAuthDecode("aW52YWxpZA==")
51+
assert.Error(t, err)
52+
53+
_, _, err = BasicAuthDecode("invalid")
54+
assert.Error(t, err)
4955
}
5056

5157
func TestBasicAuthEncode(t *testing.T) {

0 commit comments

Comments
 (0)