Skip to content

Commit 27edc1a

Browse files
silverwind6543
andauthored
Fix panic in BasicAuthDecode (#14046)
* 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]>
1 parent e9cc613 commit 27edc1a

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
"os"
@@ -63,6 +64,11 @@ func BasicAuthDecode(encoded string) (string, string, error) {
6364
}
6465

6566
auth := strings.SplitN(string(s), ":", 2)
67+
68+
if len(auth) != 2 {
69+
return "", "", errors.New("invalid basic authentication")
70+
}
71+
6672
return auth[0], auth[1], nil
6773
}
6874

modules/base/tool_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ func TestBasicAuthDecode(t *testing.T) {
4343
assert.NoError(t, err)
4444
assert.Equal(t, "foo", user)
4545
assert.Equal(t, "bar", pass)
46+
47+
_, _, err = BasicAuthDecode("aW52YWxpZA==")
48+
assert.Error(t, err)
49+
50+
_, _, err = BasicAuthDecode("invalid")
51+
assert.Error(t, err)
4652
}
4753

4854
func TestBasicAuthEncode(t *testing.T) {

0 commit comments

Comments
 (0)