Skip to content
This repository was archived by the owner on Apr 22, 2024. It is now read-only.

Commit c9dd026

Browse files
author
Vasily Kolobkov
committed
Check the token to have 3 parts present upon decoding
1 parent ca1404e commit c9dd026

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

algorithms.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ func (a *Algorithm) Encode(payload *Claims) (string, error) {
8787
// Decode returns a map representing the token's claims. DOESN'T validate the claims though.
8888
func (a *Algorithm) Decode(encoded string) (*Claims, error) {
8989
encryptedComponents := strings.Split(encoded, ".")
90+
if len(encryptedComponents) != 3 {
91+
return nil, errors.New("malformed token")
92+
}
9093

9194
b64Payload := encryptedComponents[1]
9295

jwt_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,15 @@ func TestVerifyTokenNbf(t *testing.T) {
108108
}
109109
})
110110
}
111+
112+
func TestDecodeMalformedToken(t *testing.T) {
113+
RunTest(t, func(algorithm Algorithm) {
114+
bogusTokens := []string{"", "abc", "czwmS6hE.NZLElvuy"}
115+
116+
for _, bogusToken := range bogusTokens {
117+
if _, err := algorithm.Decode(bogusToken); err == nil {
118+
t.Fatalf("no error returned upon decoding malformed token '%s'", bogusToken)
119+
}
120+
}
121+
})
122+
}

0 commit comments

Comments
 (0)