Skip to content

Commit 3479ee4

Browse files
giorgos-f3thaJeztah
authored andcommitted
Fix security vulnerability
Fixes a security vulnerability where a jwt token could potentially be validated having invalid string characters. (cherry picked from commit a211650c6ae1cff6d7347d3e24070d65dcfb1122) form3tech-oss/jwt-go#14 Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 860640e commit 3479ee4

File tree

2 files changed

+59
-13
lines changed

2 files changed

+59
-13
lines changed

map_claims.go

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,38 @@ func (m MapClaims) VerifyAudience(cmp string, req bool) bool {
3434
// Compares the exp claim against cmp.
3535
// If required is false, this method will return true if the value matches or is unset
3636
func (m MapClaims) VerifyExpiresAt(cmp int64, req bool) bool {
37-
switch exp := m["exp"].(type) {
37+
exp, ok := m["exp"]
38+
if !ok {
39+
return !req
40+
}
41+
switch expType := exp.(type) {
3842
case float64:
39-
return verifyExp(int64(exp), cmp, req)
43+
return verifyExp(int64(expType), cmp, req)
4044
case json.Number:
41-
v, _ := exp.Int64()
45+
v, _ := expType.Int64()
4246
return verifyExp(v, cmp, req)
4347
}
44-
return !req
48+
return false
4549
}
4650

4751
// Compares the iat claim against cmp.
4852
// If required is false, this method will return true if the value matches or is unset
4953
func (m MapClaims) VerifyIssuedAt(cmp int64, req bool) bool {
50-
switch iat := m["iat"].(type) {
54+
iat, ok := m["iat"]
55+
if !ok {
56+
return !req
57+
}
58+
switch iatType := iat.(type) {
5159
case float64:
52-
return verifyIat(int64(iat), cmp, req)
60+
return verifyIat(int64(iatType), cmp, req)
5361
case json.Number:
54-
v, _ := iat.Int64()
62+
v, _ := iatType.Int64()
5563
return verifyIat(v, cmp, req)
5664
}
57-
return !req
65+
return false
5866
}
5967

60-
// Compares the iss claim against cmp.
68+
// Compares the iss claim against cmp.``
6169
// If required is false, this method will return true if the value matches or is unset
6270
func (m MapClaims) VerifyIssuer(cmp string, req bool) bool {
6371
iss, _ := m["iss"].(string)
@@ -67,14 +75,18 @@ func (m MapClaims) VerifyIssuer(cmp string, req bool) bool {
6775
// Compares the nbf claim against cmp.
6876
// If required is false, this method will return true if the value matches or is unset
6977
func (m MapClaims) VerifyNotBefore(cmp int64, req bool) bool {
70-
switch nbf := m["nbf"].(type) {
78+
nbf, ok := m["nbf"]
79+
if !ok {
80+
return !req
81+
}
82+
switch nbfType := nbf.(type) {
7183
case float64:
72-
return verifyNbf(int64(nbf), cmp, req)
84+
return verifyNbf(int64(nbfType), cmp, req)
7385
case json.Number:
74-
v, _ := nbf.Int64()
86+
v, _ := nbfType.Int64()
7587
return verifyNbf(v, cmp, req)
7688
}
77-
return !req
89+
return false
7890
}
7991

8092
// Validates time based claims "exp, iat, nbf".

map_claims_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,37 @@ func TestVerifyAud(t *testing.T) {
6666
})
6767
}
6868
}
69+
70+
func Test_mapclaims_verify_issued_at_invalid_type_string(t *testing.T) {
71+
mapClaims := MapClaims{
72+
"iat": "foo",
73+
}
74+
want := false
75+
got := mapClaims.VerifyIssuedAt(0, false)
76+
if want != got {
77+
t.Fatalf("Failed to verify claims, wanted: %v got %v", want, got)
78+
}
79+
}
80+
81+
func Test_mapclaims_verify_not_before_invalid_type_string(t *testing.T) {
82+
mapClaims := MapClaims{
83+
"nbf": "foo",
84+
}
85+
want := false
86+
got := mapClaims.VerifyNotBefore(0, false)
87+
if want != got {
88+
t.Fatalf("Failed to verify claims, wanted: %v got %v", want, got)
89+
}
90+
}
91+
92+
func Test_mapclaims_verify_expires_at_invalid_type_string(t *testing.T) {
93+
mapClaims := MapClaims{
94+
"exp": "foo",
95+
}
96+
want := false
97+
got := mapClaims.VerifyExpiresAt(0, false)
98+
99+
if want != got {
100+
t.Fatalf("Failed to verify claims, wanted: %v got %v", want, got)
101+
}
102+
}

0 commit comments

Comments
 (0)