Skip to content

Commit

Permalink
fix: return HTTP 400 if key unmarshal fails
Browse files Browse the repository at this point in the history
  • Loading branch information
nipsufn committed Oct 25, 2023
1 parent 6a0a914 commit 31115ff
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
14 changes: 14 additions & 0 deletions session/stub/jwk.es512.broken.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"keys": [
{
"use": "sig",
"kty": "EC",
"kid": "bc7f7afc-6742-427c-bb9e-164fe0f8b6a7",
"crv": "P-521",
"alg": "ES512",
"x": "ASj36HQOpsWiaGyzK1F0GkxXRt37R01M-OCWFk8rFqH8UnFBk0qnCmVYWv3pwVPPsN0CfFiaXTrV1gUSapkkDgWY",
"y": "ALf5bqXExUq6FzQNQg01hDhR2lOKzkrC02Bc6Alld8Zji3-echbimNZltoOi4MhXbSJeWHpU8wzb3v9XAAW4eovn",
"d": "ALP0Sf7cmcELc9CQ2bWd6Qs-YxMu0N9EYZhDmR6qbYdGnvv-lcGy_ySoEJD0vPMKagA8PHDvFhC7ORwP-sBIJ4O_"
}
]

3 changes: 3 additions & 0 deletions session/tokenizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package session
import (
"context"
"encoding/json"
"strings"
"time"

"go.opentelemetry.io/otel/trace"
Expand Down Expand Up @@ -70,6 +71,8 @@ func (s *Tokenizer) TokenizeSession(ctx context.Context, template string, sessio
if err != nil {
if errors.Is(err, jwksx.ErrUnableToFindKeyID) {
return errors.WithStack(herodot.ErrBadRequest.WithReasonf("Could not find key a suitable key for tokenization in the JWKS url."))
} else if strings.Contains(err.Error(), "failed to unmarshal JWK set: ") {
return errors.WithStack(herodot.ErrBadRequest.WithReasonf("%v", err.Error()))
}
return err
}
Expand Down
9 changes: 9 additions & 0 deletions session/tokenizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"testing"
"time"

"github.com/ory/herodot"

"github.com/gofrs/uuid"
"github.com/golang-jwt/jwt/v5"
"github.com/lestrrat-go/jwx/jwk"
Expand Down Expand Up @@ -115,4 +117,11 @@ func TestTokenizer(t *testing.T) {

snapshotx.SnapshotT(t, token.Claims, snapshotx.ExceptPaths("jti"))
})

t.Run("case=rs512-with-broken-keyfile", func(t *testing.T) {
tid := "rs512-template"
setTokenizeConfig(conf, tid, "jwk.es512.broken.json", "file://stub/rs512-template.jsonnet")
err := tkn.TokenizeSession(ctx, tid, s)
require.ErrorIs(t, err, herodot.ErrBadRequest)
})
}

0 comments on commit 31115ff

Please sign in to comment.