Skip to content

Commit

Permalink
fix: client JWKS validator
Browse files Browse the repository at this point in the history
  • Loading branch information
alnr committed Aug 10, 2023
1 parent 621e56d commit fa39be5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
4 changes: 2 additions & 2 deletions client/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ type createOAuth2Client struct {
func (h *Handler) createOAuth2Client(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
c, err := h.CreateClient(r, h.r.ClientValidator().Validate, false)
if err != nil {
h.r.Writer().WriteError(w, r, errorsx.WithStack(err))
h.r.Writer().WriteError(w, r, err)
return
}

Expand Down Expand Up @@ -164,7 +164,7 @@ func (h *Handler) createOidcDynamicClient(w http.ResponseWriter, r *http.Request
func (h *Handler) CreateClient(r *http.Request, validator func(context.Context, *Client) error, isDynamic bool) (*Client, error) {
var c Client
if err := json.NewDecoder(r.Body).Decode(&c); err != nil {
return nil, err
return nil, errorsx.WithStack(herodot.ErrBadRequest.WithReasonf("Unable to decode the request body: %s", err))
}

if isDynamic {
Expand Down
8 changes: 8 additions & 0 deletions client/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ func (v *Validator) Validate(ctx context.Context, c *Client) error {
return errorsx.WithStack(ErrInvalidClientMetadata.WithHint("Fields jwks and jwks_uri can not both be set, you must choose one."))
}

if c.JSONWebKeys != nil {
for _, k := range c.JSONWebKeys.Keys {
if !k.Valid() {
return errorsx.WithStack(ErrInvalidClientMetadata.WithHint("Invalid JSON web key in set."))
}
}
}

if v.r.Config().ClientHTTPNoPrivateIPRanges() {
values := map[string]string{
"jwks_uri": c.JSONWebKeysURI,
Expand Down
8 changes: 5 additions & 3 deletions jwk/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"golang.org/x/sync/errgroup"

"github.com/ory/herodot"
"github.com/ory/x/httprouterx"

"github.com/gofrs/uuid"
Expand Down Expand Up @@ -288,7 +289,8 @@ func (h *Handler) createJsonWebKeySet(w http.ResponseWriter, r *http.Request, ps
var set = ps.ByName("set")

if err := json.NewDecoder(r.Body).Decode(&keyRequest); err != nil {
h.r.Writer().WriteError(w, r, errorsx.WithStack(err))
h.r.Writer().WriteError(w, r, errorsx.WithStack(herodot.ErrBadRequest.WithReasonf("Unable to decode the request body: %s", err)))
return
}

if keys, err := h.r.KeyManager().GenerateAndPersistKeySet(r.Context(), set, keyRequest.KeyID, keyRequest.Algorithm, keyRequest.Use); err == nil {
Expand Down Expand Up @@ -339,7 +341,7 @@ func (h *Handler) setJsonWebKeySet(w http.ResponseWriter, r *http.Request, ps ht
var set = ps.ByName("set")

if err := json.NewDecoder(r.Body).Decode(&keySet); err != nil {
h.r.Writer().WriteError(w, r, errorsx.WithStack(err))
h.r.Writer().WriteError(w, r, errorsx.WithStack(herodot.ErrBadRequest.WithReasonf("Unable to decode the request body: %s", err)))
return
}

Expand Down Expand Up @@ -397,7 +399,7 @@ func (h *Handler) adminUpdateJsonWebKey(w http.ResponseWriter, r *http.Request,
var set = ps.ByName("set")

if err := json.NewDecoder(r.Body).Decode(&key); err != nil {
h.r.Writer().WriteError(w, r, errorsx.WithStack(err))
h.r.Writer().WriteError(w, r, errorsx.WithStack(herodot.ErrBadRequest.WithReasonf("Unable to decode the request body: %s", err)))
return
}

Expand Down

0 comments on commit fa39be5

Please sign in to comment.