Skip to content

Commit 0f017e9

Browse files
authored
Revert #26 (ignore unsupported JWKs in Sets) (#131)
This reverts commit 3e2bbef, "Unmarshal jwk keys with unsupported key type or algorithm into empty ... (#26)" I accidentally merged that PR into v3, but per our README, the v3 branch is getting security updates but not functionality updates.
1 parent 3e2bbef commit 0f017e9

File tree

2 files changed

+5
-73
lines changed

2 files changed

+5
-73
lines changed

Diff for: jwk.go

+5-47
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,6 @@ func (k JSONWebKey) MarshalJSON() ([]byte, error) {
174174
return json.Marshal(raw)
175175
}
176176

177-
var errUnsupportedJWK = errors.New("go-jose/go-jose: unsupported json web key")
178-
179177
// UnmarshalJSON reads a key from its JSON representation.
180178
func (k *JSONWebKey) UnmarshalJSON(data []byte) (err error) {
181179
var raw rawJSONWebKey
@@ -230,7 +228,7 @@ func (k *JSONWebKey) UnmarshalJSON(data []byte) (err error) {
230228
}
231229
key, err = raw.symmetricKey()
232230
case "OKP":
233-
if raw.Crv == "Ed25519" {
231+
if raw.Crv == "Ed25519" && raw.X != nil {
234232
if raw.D != nil {
235233
key, err = raw.edPrivateKey()
236234
if err == nil {
@@ -240,29 +238,17 @@ func (k *JSONWebKey) UnmarshalJSON(data []byte) (err error) {
240238
key, err = raw.edPublicKey()
241239
keyPub = key
242240
}
241+
} else {
242+
err = fmt.Errorf("go-jose/go-jose: unknown curve %s'", raw.Crv)
243243
}
244-
case "":
245-
// kty MUST be present
246-
err = fmt.Errorf("go-jose/go-jose: missing json web key type")
244+
default:
245+
err = fmt.Errorf("go-jose/go-jose: unknown json web key type '%s'", raw.Kty)
247246
}
248247

249248
if err != nil {
250249
return
251250
}
252251

253-
if key == nil {
254-
// RFC 7517:
255-
// 5. JWK Set Format
256-
// ...
257-
// Implementations SHOULD ignore JWKs within a JWK Set that use "kty"
258-
// (key type) values that are not understood by them, that are missing
259-
// required members, or for which values are out of the supported
260-
// ranges.
261-
262-
// Fail unmarshal with errUnsupportedJWK
263-
return errUnsupportedJWK
264-
}
265-
266252
if certPub != nil && keyPub != nil {
267253
if !reflect.DeepEqual(certPub, keyPub) {
268254
return errors.New("go-jose/go-jose: invalid JWK, public keys in key and x5c fields do not match")
@@ -362,34 +348,6 @@ func (s *JSONWebKeySet) Key(kid string) []JSONWebKey {
362348
return keys
363349
}
364350

365-
func (s *JSONWebKeySet) UnmarshalJSON(data []byte) (err error) {
366-
type rawJSONWebKeySet struct {
367-
Keys []json.RawMessage `json:"keys"`
368-
}
369-
370-
var rs rawJSONWebKeySet
371-
err = json.Unmarshal(data, &rs)
372-
if err != nil {
373-
return err
374-
}
375-
376-
for _, rk := range rs.Keys {
377-
var k JSONWebKey
378-
err = json.Unmarshal(rk, &k)
379-
if err != nil {
380-
// Skip key and continue unmarshalling the key set if key unmarshal
381-
// failed because of unsupported key type or parameters.
382-
if !errors.Is(err, errUnsupportedJWK) {
383-
return err
384-
}
385-
} else {
386-
s.Keys = append(s.Keys, k)
387-
}
388-
}
389-
390-
return nil
391-
}
392-
393351
const rsaThumbprintTemplate = `{"e":"%s","kty":"RSA","n":"%s"}`
394352
const ecThumbprintTemplate = `{"crv":"%s","kty":"EC","x":"%s","y":"%s"}`
395353
const edThumbprintTemplate = `{"crv":"%s","kty":"OKP","x":"%s"}`

Diff for: jwk_test.go

-26
Original file line numberDiff line numberDiff line change
@@ -703,12 +703,6 @@ func TestWebKeyVectorsInvalid(t *testing.T) {
703703
`{"kty":"EC","crv":"P-256","d":"XXX"}`,
704704
`{"kty":"EC","crv":"ABC","d":"dGVzdA","x":"dGVzdA"}`,
705705
`{"kty":"EC","crv":"P-256","d":"dGVzdA","x":"dGVzdA"}`,
706-
// Invalid oct key
707-
`{"kty":"oct"}`,
708-
`{"kty":"oct","k":"%not-base64url-encoded*"}`,
709-
// Invalid OKP key
710-
`{"kty":"OKP","crv":"Ed25519"}`,
711-
`{"kty":"OKP","crv":"Ed25519","x":"%not-base64url-encoded*"}`,
712706
}
713707

714708
for _, key := range keys {
@@ -720,26 +714,6 @@ func TestWebKeyVectorsInvalid(t *testing.T) {
720714
}
721715
}
722716

723-
func TestWebKeyVectorsUnsupported(t *testing.T) {
724-
keys := []string{
725-
// Unknown kty
726-
`{"kty": "XXX"}`,
727-
// Unsupported OKP curve
728-
`{"kty": "OKP", "crv": "X25519", "x": "89abcdef"}`,
729-
}
730-
731-
for _, key := range keys {
732-
var jwk2 JSONWebKey
733-
err := jwk2.UnmarshalJSON([]byte(key))
734-
if err != nil {
735-
t.Error("failed to parse key with unsupported type or algorithm:", key)
736-
}
737-
if jwk2.Valid() {
738-
t.Error("unsupported key type or algorithm parsed into a valid key:", key)
739-
}
740-
}
741-
}
742-
743717
// Test vectors from RFC 7520
744718
var cookbookJWKs = []string{
745719
// EC Public

0 commit comments

Comments
 (0)