@@ -200,6 +200,68 @@ def test_hmac_prepare_key_rejects_jwk_json(self, jwk_file: str) -> None:
200200 with pytest .raises (InvalidKeyError , match = "looks like a JWK" ):
201201 algo .prepare_key (keyfile .read ())
202202
203+ @pytest .mark .parametrize (
204+ "container" , ("jwks" , "array" , "nested-array" , "bom-jwks" )
205+ )
206+ def test_hmac_prepare_key_rejects_public_jwk_containers (
207+ self , container : str
208+ ) -> None :
209+ algo = HMACAlgorithm (HMACAlgorithm .SHA256 )
210+
211+ with open (key_path ("jwk_rsa_pub.json" )) as keyfile :
212+ public_jwk = json .load (keyfile )
213+
214+ if container == "jwks" :
215+ key : Union [str , bytes ] = json .dumps ({"keys" : [public_jwk ]})
216+ elif container == "array" :
217+ key = json .dumps ([public_jwk ])
218+ elif container == "nested-array" :
219+ key = json .dumps ([[public_jwk ]])
220+ else :
221+ key = b"\xef \xbb \xbf " + json .dumps (
222+ {"keys" : [public_jwk ]}
223+ ).encode ()
224+
225+ with pytest .raises (InvalidKeyError , match = "looks like a JWK" ):
226+ algo .prepare_key (key )
227+
228+ def test_hmac_prepare_key_rejects_deep_public_jwk_array (self ) -> None :
229+ algo = HMACAlgorithm (HMACAlgorithm .SHA256 )
230+ depth = 20000
231+ key = b"[" * depth + b'{"kty":"RSA"}' + b"]" * depth
232+
233+ with pytest .raises (InvalidKeyError , match = "looks like a JWK" ):
234+ algo .prepare_key (key )
235+
236+ def test_hmac_prepare_key_rejects_deep_public_jwk_array_with_escaped_kty (
237+ self ,
238+ ) -> None :
239+ algo = HMACAlgorithm (HMACAlgorithm .SHA256 )
240+ depth = 20000
241+ key = b"[" * depth + b'{"\\ u006bty":"RSA"}' + b"]" * depth
242+
243+ with pytest .raises (InvalidKeyError , match = "looks like a JWK" ):
244+ algo .prepare_key (key )
245+
246+ def test_hmac_prepare_key_accepts_deep_array_secret_with_kty_string (
247+ self ,
248+ ) -> None :
249+ algo = HMACAlgorithm (HMACAlgorithm .SHA256 )
250+ depth = 20000
251+ key = b'["kty",' + b"[" * depth + b"0" + b"]" * depth + b"]"
252+
253+ assert algo .prepare_key (key ) == key
254+
255+ def test_hmac_prepare_key_rejects_jwks_with_oversized_integer (self ) -> None :
256+ algo = HMACAlgorithm (HMACAlgorithm .SHA256 )
257+ with open (key_path ("jwk_rsa_pub.json" )) as keyfile :
258+ public_jwk = json .load (keyfile )
259+ key = json .dumps ({"keys" : [public_jwk ], "extra" : 0 })
260+ key = key .replace ('"extra": 0' , '"extra": ' + "1" * 5000 )
261+
262+ with pytest .raises (InvalidKeyError , match = "looks like a JWK" ):
263+ algo .prepare_key (key )
264+
203265 @pytest .mark .parametrize (
204266 "encoding" ,
205267 [
0 commit comments