Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 20 additions & 9 deletions crypto/dpop/dpop.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ func Parse(s string) (*DPoP, error) {
}
if v, ok := token.Get(HTUKey); !ok || v == "" {
return nil, fmt.Errorf("%w: missing htu claim", ErrInvalidDPoP)
} else if _, err := url.Parse(v.(string)); err != nil {
return nil, fmt.Errorf("%w: invalid htu claim: %w", ErrInvalidDPoP, err)
Comment thread
reinkrul marked this conversation as resolved.
}
if v, ok := token.Get(HTMKey); !ok || v == "" {
return nil, fmt.Errorf("%w: missing htm claim", ErrInvalidDPoP)
Comment thread
reinkrul marked this conversation as resolved.
Outdated
Expand Down Expand Up @@ -220,22 +222,31 @@ 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)
}
Comment thread
reinkrul marked this conversation as resolved.
Comment thread
reinkrul marked this conversation as resolved.
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"
u.Host = strings.Split(u.Host, ":")[0]
Comment thread
reinkrul marked this conversation as resolved.
Outdated
u.RawQuery = ""
u.Fragment = ""
return u.String(), nil
}

func (t DPoP) MarshalJSON() ([]byte, error) {
Expand Down
14 changes: 14 additions & 0 deletions crypto/dpop/dpop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,20 @@ 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")
})
}

func TestDPoP_Match(t *testing.T) {
Expand Down
Loading