Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 32 additions & 12 deletions crypto/dpop/dpop.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"net/http"
"net/url"
"slices"
"strings"
"time"

"github.com/lestrrat-go/jwx/v2/jwa"
Expand Down Expand Up @@ -156,10 +155,20 @@ func Parse(s string) (*DPoP, error) {
if token.IssuedAt().IsZero() {
return nil, fmt.Errorf("%w: missing iat claim", ErrInvalidDPoP)
}
if v, ok := token.Get(HTUKey); !ok || v == "" {
if v, ok := token.Get(HTUKey); !ok {
return nil, fmt.Errorf("%w: missing htu claim", ErrInvalidDPoP)
} else if htu, ok := v.(string); !ok {
return nil, fmt.Errorf("%w: invalid htu claim", ErrInvalidDPoP)
} else if htu == "" {
return nil, fmt.Errorf("%w: missing htu claim", ErrInvalidDPoP)
} else if _, err := url.Parse(htu); err != nil {
return nil, fmt.Errorf("%w: invalid htu claim: %w", ErrInvalidDPoP, err)
}
if v, ok := token.Get(HTMKey); !ok || v == "" {
if v, ok := token.Get(HTMKey); !ok {
return nil, fmt.Errorf("%w: missing htm claim", ErrInvalidDPoP)
} else if htm, ok := v.(string); !ok {
return nil, fmt.Errorf("%w: invalid htm claim", ErrInvalidDPoP)
} else if htm == "" {
return nil, fmt.Errorf("%w: missing htm claim", ErrInvalidDPoP)
}
if token.JwtID() == "" {
Expand Down Expand Up @@ -220,22 +229,33 @@ func (t DPoP) Match(jkt string, method string, url string) (bool, error) {
if method != t.HTM() {
return false, fmt.Errorf("method mismatch, token: %s, given: %s", t.HTM(), method)
}
urlLeft := strip(t.HTU())
urlRight := strip(url)
urlLeft, err := strip(t.HTU())
if err != nil {
return false, fmt.Errorf("invalid htu claim: %w", err)
}
urlRight, err := strip(url)
if err != nil {
return false, fmt.Errorf("invalid url: %w", err)
}
if urlLeft != urlRight {
return false, fmt.Errorf("url mismatch, token: %s, given: %s", urlLeft, urlRight)
}

return true, nil
}

func strip(raw string) string {
url, _ := url.Parse(raw)
url.Scheme = "https"
url.Host = strings.Split(url.Host, ":")[0]
url.RawQuery = ""
url.Fragment = ""
return url.String()
func strip(raw string) (string, error) {
u, err := url.Parse(raw)
if err != nil {
return "", err
}
u.Scheme = "https"
if host := u.Hostname(); host != "" {
u.Host = host
}
u.RawQuery = ""
u.Fragment = ""
return u.String(), nil
}

func (t DPoP) MarshalJSON() ([]byte, error) {
Expand Down
34 changes: 34 additions & 0 deletions crypto/dpop/dpop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,40 @@ func TestParseDPoP(t *testing.T) {
require.Error(t, err)
assert.EqualError(t, err, "invalid DPoP token: jti claim too long")
})
t.Run("invalid htu claim URL", func(t *testing.T) {
// Regression test: url.Parse returns (nil, error) for invalid percent-encoded
// sequences. Without the fix, strip() dereferenced the nil pointer, panicking
// the server when Match() was called on a token with htu="%zz".
dpopToken := New(*request)
_ = dpopToken.Token.Set(HTUKey, "%zz")
dpopString, _ := dpopToken.Sign("kid", keyPair, alg)

_, err := Parse(dpopString)

require.Error(t, err)
assert.ErrorIs(t, err, ErrInvalidDPoP)
assert.Contains(t, err.Error(), "invalid htu claim")
})
t.Run("non-string htu claim", func(t *testing.T) {
dpopToken := New(*request)
_ = dpopToken.Token.Set(HTUKey, 42)
dpopString, _ := dpopToken.Sign("kid", keyPair, alg)

_, err := Parse(dpopString)

require.Error(t, err)
assert.EqualError(t, err, "invalid DPoP token: invalid htu claim")
})
t.Run("non-string htm claim", func(t *testing.T) {
dpopToken := New(*request)
_ = dpopToken.Token.Set(HTMKey, 42)
dpopString, _ := dpopToken.Sign("kid", keyPair, alg)

_, err := Parse(dpopString)

require.Error(t, err)
assert.EqualError(t, err, "invalid DPoP token: invalid htm claim")
})
}

func TestDPoP_Match(t *testing.T) {
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,6 @@ github.com/nuts-foundation/crypto-ecies v0.0.0-20211207143025-5b84f9efce2b h1:80
github.com/nuts-foundation/crypto-ecies v0.0.0-20211207143025-5b84f9efce2b/go.mod h1:6YUioYirD6/8IahZkoS4Ypc8xbeJW76Xdk1QKcziNTM=
github.com/nuts-foundation/go-did v0.18.0 h1:IB0X8PrzDulpR1zAgDpaHfwoSjJpIhx5u1Tg8I2nnb8=
github.com/nuts-foundation/go-did v0.18.0/go.mod h1:4od1gAmCi9HjHTQGEvHC8pLeuXdXACxidAcdA52YScc=
github.com/nuts-foundation/go-leia/v4 v4.2.0 h1:o/bgYVCyTgsfgtaKmlrcUaJ2z4NwetERC98SUWwYajM=
github.com/nuts-foundation/go-leia/v4 v4.2.0/go.mod h1:Gw6bXqJLOAmHSiXJJYbVoj+Mowp/PoBRywO0ZPsVzA0=
github.com/nuts-foundation/go-leia/v4 v4.3.0 h1:R0qGISIeg2q/PCQTC9cuoBtA6cFu4WBV2DbmSOWKZyM=
github.com/nuts-foundation/go-leia/v4 v4.3.0/go.mod h1:Gw6bXqJLOAmHSiXJJYbVoj+Mowp/PoBRywO0ZPsVzA0=
github.com/nuts-foundation/go-stoabs v1.11.0 h1:q18jVruPdFcVhodDrnKuhq/24i0pUC/YXgzJS0glKUU=
Expand Down
Loading