1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
From: Mostafa El Katerji <mostafakaterji@gmail.com>
Date: Tue, 14 Aug 2018 12:20:33 +0300
Subject: CVE-2020-26160
| jwt-go before 4.0.0-preview1 allows attackers to bypass intended
| access restrictions in situations with []string{} for m["aud"] (which
| is allowed by the specification). Because the type assertion fails, ""
| is the value of aud. This is a security problem if the JWT token is
| presented to a service that lacks its own audience check.
https://github.com/dgrijalva/jwt-go/issues/428
https://github.com/dgrijalva/jwt-go/pull/286
Author: https://github.com/katerji92
---
claims.go | 45 +++++++++++++++-------
claims_test.go | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++++
map_claims.go | 14 ++++++-
map_claims_tests.go | 46 +++++++++++++++++++++++
4 files changed, 195 insertions(+), 15 deletions(-)
create mode 100644 claims_test.go
create mode 100644 map_claims_tests.go
diff --git a/claims.go b/claims.go
index f0228f0..b2d4e56 100644
--- a/claims.go
+++ b/claims.go
@@ -16,13 +16,13 @@ type Claims interface {
// https://tools.ietf.org/html/rfc7519#section-4.1
// See examples for how to use this with your own claim types
type StandardClaims struct {
- Audience string `json:"aud,omitempty"`
- ExpiresAt int64 `json:"exp,omitempty"`
- Id string `json:"jti,omitempty"`
- IssuedAt int64 `json:"iat,omitempty"`
- Issuer string `json:"iss,omitempty"`
- NotBefore int64 `json:"nbf,omitempty"`
- Subject string `json:"sub,omitempty"`
+ Audience interface{} `json:"aud,omitempty"`
+ ExpiresAt int64 `json:"exp,omitempty"`
+ Id string `json:"jti,omitempty"`
+ IssuedAt int64 `json:"iat,omitempty"`
+ Issuer string `json:"iss,omitempty"`
+ NotBefore int64 `json:"nbf,omitempty"`
+ Subject string `json:"sub,omitempty"`
}
// Validates time based claims "exp, iat, nbf".
@@ -58,10 +58,27 @@ func (c StandardClaims) Valid() error {
return vErr
}
+// Extracts an array of audience values from the aud field.
+func ExtractAudience(c *StandardClaims) []string {
+ switch c.Audience.(type) {
+ case []interface{}:
+ auds := make([]string, len(c.Audience.([]interface{})))
+ for i, value := range c.Audience.([]interface{}) {
+ auds[i] = value.(string)
+ }
+ return auds
+ case []string:
+ return c.Audience.([]string)
+ default:
+ return []string{c.Audience.(string)}
+ }
+}
+
// Compares the aud claim against cmp.
// If required is false, this method will return true if the value matches or is unset
func (c *StandardClaims) VerifyAudience(cmp string, req bool) bool {
- return verifyAud(c.Audience, cmp, req)
+ audiences := ExtractAudience(c)
+ return verifyAud(audiences, cmp, req)
}
// Compares the exp claim against cmp.
@@ -90,13 +107,15 @@ func (c *StandardClaims) VerifyNotBefore(cmp int64, req bool) bool {
// ----- helpers
-func verifyAud(aud string, cmp string, required bool) bool {
- if aud == "" {
+func verifyAud(auds []string, cmp string, required bool) bool {
+ if len(auds) == 0 {
return !required
- }
- if subtle.ConstantTimeCompare([]byte(aud), []byte(cmp)) != 0 {
- return true
} else {
+ for _, aud := range auds {
+ if len(aud) == len(cmp) && subtle.ConstantTimeCompare([]byte(aud), []byte(cmp)) != 0 {
+ return true
+ }
+ }
return false
}
}
diff --git a/claims_test.go b/claims_test.go
new file mode 100644
index 0000000..e8ec982
--- /dev/null
+++ b/claims_test.go
@@ -0,0 +1,105 @@
+package jwt
+
+import (
+ "testing"
+)
+
+// Test StandardClaims instances with an audience value populated in a string, []string and []interface{}
+var audienceValue = "Aud"
+var unmatchedAudienceValue = audienceValue + "Test"
+var claimWithAudience = []StandardClaims{
+ {
+ audienceValue,
+ 123123,
+ "Id",
+ 12312,
+ "Issuer",
+ 12312,
+ "Subject",
+ },
+ {
+ []string{audienceValue, unmatchedAudienceValue},
+ 123123,
+ "Id",
+ 12312,
+ "Issuer",
+ 12312,
+ "Subject",
+ },
+ {
+ []interface{}{audienceValue, unmatchedAudienceValue},
+ 123123,
+ "Id",
+ 12312,
+ "Issuer",
+ 12312,
+ "Subject",
+ },
+}
+
+// Test StandardClaims instances with no aduences within empty []string and []interface{} collections.
+var claimWithoutAudience = []StandardClaims{
+ {
+ []string{},
+ 123123,
+ "Id",
+ 12312,
+ "Issuer",
+ 12312,
+ "Subject",
+ },
+ {
+ []interface{}{},
+ 123123,
+ "Id",
+ 12312,
+ "Issuer",
+ 12312,
+ "Subject",
+ },
+}
+
+func TestExtractAudienceWithAudienceValues(t *testing.T) {
+ for _, data := range claimWithAudience {
+ var aud = ExtractAudience(&data)
+ if len(aud) == 0 || aud[0] != audienceValue {
+ t.Errorf("The audience value was not extracted properly")
+ }
+ }
+}
+
+func TestExtractAudience_WithoutAudienceValues(t *testing.T) {
+ for _, data := range claimWithoutAudience {
+ var aud = ExtractAudience(&data)
+ if len(aud) != 0 {
+ t.Errorf("An audience value should not have been extracted")
+ }
+ }
+}
+
+var audWithValues = [][]string{
+ []string{audienceValue},
+ []string{"Aud1", "Aud2", audienceValue},
+}
+
+var audWithLackingOriginalValue = [][]string{
+ []string{},
+ []string{audienceValue + "1"},
+ []string{"Aud1", "Aud2", audienceValue + "1"},
+}
+
+func TestVerifyAud_ShouldVerifyExists(t *testing.T) {
+ for _, data := range audWithValues {
+ if !verifyAud(data, audienceValue, true) {
+ t.Errorf("The audience value was not verified properly")
+ }
+ }
+}
+
+func TestVerifyAud_ShouldVerifyDoesNotExist(t *testing.T) {
+ for _, data := range audWithValues {
+ if !verifyAud(data, audienceValue, true) {
+ t.Errorf("The audience value was not verified properly")
+ }
+ }
+}
diff --git a/map_claims.go b/map_claims.go
index 291213c..643e18f 100644
--- a/map_claims.go
+++ b/map_claims.go
@@ -13,8 +13,18 @@ type MapClaims map[string]interface{}
// Compares the aud claim against cmp.
// If required is false, this method will return true if the value matches or is unset
func (m MapClaims) VerifyAudience(cmp string, req bool) bool {
- aud, _ := m["aud"].(string)
- return verifyAud(aud, cmp, req)
+ switch aud := m["aud"].(type) {
+ case []string:
+ return verifyAud(aud, cmp, req)
+ case []interface{}:
+ auds := make([]string, len(aud))
+ for i, value := range aud {
+ auds[i] = value.(string)
+ }
+ return verifyAud(auds, cmp, req)
+ default:
+ return verifyAud([]string{aud.(string)}, cmp, req)
+ }
}
// Compares the exp claim against cmp.
diff --git a/map_claims_tests.go b/map_claims_tests.go
new file mode 100644
index 0000000..39ad450
--- /dev/null
+++ b/map_claims_tests.go
@@ -0,0 +1,46 @@
+package jwt
+
+import (
+ "testing"
+)
+
+var audFixedValue = "Aud"
+var audClaimsMapsWithValues = []MapClaims{
+ {
+ "aud": audFixedValue,
+ },
+ {
+ "aud": []string{audFixedValue},
+ },
+ {
+ "aud": []interface{}{audFixedValue},
+ },
+}
+
+var audClaimsMapsWithoutValues = []MapClaims{
+ {},
+ {
+ "aud": []string{},
+ },
+ {
+ "aud": []interface{}{},
+ },
+}
+
+// Verifies that for every form of the "aud" field, the audFixedValue is always verifiable
+func TestVerifyAudienceWithVerifiableValues(t *testing.T) {
+ for _, data := range audClaimsMapsWithValues {
+ if !data.VerifyAudience(audFixedValue, true) {
+ t.Errorf("The audience value was not extracted properly")
+ }
+ }
+}
+
+// Verifies that for every empty form of the "aud" field, the audFixedValue cannot be verified
+func TestVerifyAudienceWithoutVerifiableValues(t *testing.T) {
+ for _, data := range audClaimsMapsWithoutValues {
+ if data.VerifyAudience(audFixedValue, true) {
+ t.Errorf("The audience should not verify")
+ }
+ }
+}
|