@@ -646,3 +646,57 @@ func TestNewAccessControllerPemBlock(t *testing.T) {
646646 t .Fatal ("accessController has the wrong number of certificates" )
647647 }
648648}
649+
650+ // This test makes sure the untrusted key can not be used in token verification.
651+ func TestVerifyJWKWithTrustedKey (t * testing.T ) {
652+ // Generate a test key pair
653+ privKey , err := ecdsa .GenerateKey (elliptic .P256 (), rand .Reader )
654+ if err != nil {
655+ t .Fatal (err )
656+ }
657+ pubKey := privKey .Public ()
658+
659+ // Create a JWK with no certificates
660+ jwk := & jose.JSONWebKey {
661+ Key : privKey ,
662+ KeyID : "test-key-id" ,
663+ Use : "sig" ,
664+ Algorithm : string (jose .ES256 ),
665+ }
666+
667+ // Create verify options with our public key as trusted
668+ verifyOpts := VerifyOptions {
669+ TrustedKeys : map [string ]crypto.PublicKey {
670+ "test-key-id" : pubKey ,
671+ },
672+ }
673+
674+ // Create test header
675+ header := jose.Header {
676+ JSONWebKey : jwk ,
677+ }
678+
679+ // Test the verifyJWK function
680+ returnedKey , err := verifyJWK (header , verifyOpts )
681+ if err != nil {
682+ t .Fatalf ("Expected no error, got: %v" , err )
683+ }
684+
685+ // Verify the returned key matches our trusted key
686+ if returnedKey != pubKey {
687+ t .Error ("Returned key does not match the trusted key" )
688+ }
689+
690+ // Test with untrusted key
691+ verifyOpts .TrustedKeys = map [string ]crypto.PublicKey {
692+ "different-key-id" : pubKey ,
693+ }
694+
695+ _ , err = verifyJWK (header , verifyOpts )
696+ if err == nil {
697+ t .Error ("Expected error for untrusted key, got none" )
698+ }
699+ if err .Error () != "untrusted JWK with no certificate chain" {
700+ t .Errorf ("Expected 'untrusted JWK with no certificate chain' error, got: %v" , err )
701+ }
702+ }
0 commit comments