nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: lestrrat-go/jwx
Pull request #604 (2022-03-04 12:52:24)

Fix jwa.KeyAlgorithmFor()


Patch:
diff --git a/jwa/jwa.go b/jwa/jwa.go
index 3fb1e13d..6d2311c7 100644
--- a/jwa/jwa.go
+++ b/jwa/jwa.go
@@ -41,7 +41,7 @@ func KeyAlgorithmFrom(v interface{}) KeyAlgorithm {
 	switch v := v.(type) {
 	case SignatureAlgorithm:
 		return v
-	case ContentEncryptionAlgorithm:
+	case KeyEncryptionAlgorithm:
 		return v
 	case string:
 		var salg SignatureAlgorithm


Test patch:
diff --git a/jwa/jwa_test.go b/jwa/jwa_test.go
index f77c7ec3..74bb07d8 100644
--- a/jwa/jwa_test.go
+++ b/jwa/jwa_test.go
@@ -1,5 +1,13 @@
 package jwa_test
 
+import (
+	"fmt"
+	"testing"
+
+	"github.com/lestrrat-go/jwx/v2/jwa"
+	"github.com/stretchr/testify/assert"
+)
+
 type stringer struct {
 	src string
 }
@@ -7,3 +15,54 @@ type stringer struct {
 func (s stringer) String() string {
 	return s.src
 }
+
+func TestSanity(t *testing.T) {
+	var k1 jwa.KeyAlgorithm = jwa.RS256
+	if _, ok := k1.(jwa.SignatureAlgorithm); !assert.True(t, ok, `converting k1 to jws.SignatureAlgorithm should succeed`) {
+		return
+	}
+	if _, ok := k1.(jwa.KeyEncryptionAlgorithm); !assert.False(t, ok, `converting k1 to jws.KeyEncryptionAlgorithm should fail`) {
+		return
+	}
+	var k2 jwa.KeyAlgorithm = jwa.DIRECT
+	if _, ok := k2.(jwa.SignatureAlgorithm); !assert.False(t, ok, `converting k2 to jws.SignatureAlgorithm should fail`) {
+		return
+	}
+	if _, ok := k2.(jwa.KeyEncryptionAlgorithm); !assert.True(t, ok, `converting k2 to jws.KeyEncryptionAlgorithm should succeed`) {
+		return
+	}
+}
+
+func TestKeyAlgorithmFrom(t *testing.T) {
+	testcases := []struct {
+		Input interface{}
+		Error bool
+	}{
+		{
+			Input: jwa.RS256,
+		},
+		{
+			Input: jwa.DIRECT,
+		},
+		{
+			Input: jwa.A128CBC_HS256,
+			Error: true,
+		},
+	}
+
+	for _, tc := range testcases {
+		tc := tc
+		t.Run(fmt.Sprintf("%T", tc.Input), func(t *testing.T) {
+			alg := jwa.KeyAlgorithmFrom(tc.Input)
+			if tc.Error {
+				if !assert.IsType(t, alg, jwa.InvalidKeyAlgorithm(""), `key should be invalid`) {
+					return
+				}
+			} else {
+				if !assert.Equal(t, alg, tc.Input, `key should be valid`) {
+					return
+				}
+			}
+		})
+	}
+}
Repository: lestrrat-go/jwx
Pull request #714 (2022-04-23 23:42:50)

Add "missing required claims" error


Patch:
diff --git a/jwt/validate.go b/jwt/validate.go
index d8148d5e..f4a4aa67 100644
--- a/jwt/validate.go
+++ b/jwt/validate.go
@@ -159,6 +159,22 @@ func (err *validationError) Unwrap() error {
 	return err.error
 }
 
+type missingRequiredClaimError struct {
+	claim string
+}
+
+func (err *missingRequiredClaimError) Error() string {
+	return fmt.Sprintf("%q not satisfied: required claim not found", err.claim)
+}
+
+func (err *missingRequiredClaimError) Is(target error) bool {
+	_, ok := target.(*missingRequiredClaimError)
+	return ok
+}
+
+func (err *missingRequiredClaimError) isValidationError() {}
+func (*missingRequiredClaimError) Unwrap() error          { return nil }
+
 var errTokenExpired = NewValidationError(fmt.Errorf(`"exp" not satisfied`))
 var errInvalidIssuedAt = NewValidationError(fmt.Errorf(`"iat" not satisfied`))
 var errTokenNotYetValid = NewValidationError(fmt.Errorf(`"nbf" not satisfied`))
@@ -179,6 +195,11 @@ func ErrTokenNotYetValid() ValidationError {
 	return errTokenNotYetValid
 }
 
+// ErrMissingRequiredClaim creates a new error for missing required claims.
+func ErrMissingRequiredClaim(name string) ValidationError {
+	return &missingRequiredClaimError{claim: name}
+}
+
 // Validator describes interface to validate a Token.
 type Validator interface {
 	// Validate should return an error if a required conditions is not met.
@@ -377,9 +398,10 @@ func IsRequired(name string) Validator {
 type isRequired string
 
 func (ir isRequired) Validate(_ context.Context, t Token) ValidationError {
-	_, ok := t.Get(string(ir))
+	name := string(ir)
+	_, ok := t.Get(name)
 	if !ok {
-		return NewValidationError(fmt.Errorf(`%q not satisfied: required claim not found`, string(ir)))
+		return ErrMissingRequiredClaim(name)
 	}
 	return nil
 }


Test patch:
diff --git a/jwt/jwt_test.go b/jwt/jwt_test.go
index c02d8f9c..eff8cdd3 100644
--- a/jwt/jwt_test.go
+++ b/jwt/jwt_test.go
@@ -1291,6 +1291,13 @@ func TestGH430(t *testing.T) {
 	}
 }
 
+func TestGH706(t *testing.T) {
+	tok := jwt.New()
+	if !assert.ErrorIs(t, jwt.ErrMissingRequiredClaim(""), jwt.Validate(tok, jwt.WithRequiredClaim("foo")), `jwt.Validate should fail`) {
+		return
+	}
+}
+
 func TestBenHigginsByPassRegression(t *testing.T) {
 	key, err := rsa.GenerateKey(rand.Reader, 2048)
 	if err != nil {
Repository: lestrrat-go/jwx
Pull request #949 (2023-06-22 22:16:10)

Adam korcz fix 1
closes #947, adding tests


Patch:
diff --git a/jwk/okp.go b/jwk/okp.go
index 2686ba51..2bf79012 100644
--- a/jwk/okp.go
+++ b/jwk/okp.go
@@ -81,6 +81,9 @@ func (k *okpPublicKey) Raw(v interface{}) error {
 }
 
 func buildOKPPrivateKey(alg jwa.EllipticCurveAlgorithm, xbuf []byte, dbuf []byte) (interface{}, error) {
+	if len(dbuf) == 0 {
+		return nil, fmt.Errorf(`cannot use empty seed`)
+	}
 	switch alg {
 	case jwa.Ed25519:
 		ret := ed25519.NewKeyFromSeed(dbuf)


Test patch:
diff --git a/jwk/jwk_test.go b/jwk/jwk_test.go
index 19d28dcf..af7dfd4d 100644
--- a/jwk/jwk_test.go
+++ b/jwk/jwk_test.go
@@ -2137,7 +2137,7 @@ func TestECDSAPEM(t *testing.T) {
 // forcing all of our users to use `go.uber.org/goleak` is prudent.
 //
 // However, we are leaving this test here so that users can learn how this function
-// can be used. This is meant to show you the boilerplate code for when you/ want to make
+// can be used. This is meant to show you the boilerplate code for when you want to make
 // absolutely sure that no goroutine is left when you finish your program.
 //
 // For example, if you are writing an app, you can follow the pattern in the
@@ -2176,3 +2176,12 @@ func TestGH928(t *testing.T) {
 	goleak.VerifyNone(t)
 }
 */
+
+func TestGH947(t *testing.T) {
+	// AS OP described it. Below case will panic if the problem exists,
+	raw := []byte(`{"crv":"Ed25519","d":"","x":"","kty":"OKP"}`)
+	k, err := jwk.ParseKey(raw)
+	require.NoError(t, err, `jwk.ParseKey should succeed`)
+	var exported []byte
+	require.Error(t, k.Raw(&exported), `(okpkey).Raw with 0-length OKP key should fail`)
+}