Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go: ['1.22', '1.23']
go: ['1.23', '1.24']
name: Linux Go ${{ matrix.go }}
steps:
- uses: actions/checkout@v4
Expand Down
56 changes: 21 additions & 35 deletions verify.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package oidc

import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -211,12 +209,29 @@ func (v *IDTokenVerifier) Verify(ctx context.Context, rawIDToken string) (*IDTok
return nil, fmt.Errorf("oidc: malformed jwt: %v", err)
}

// Throw out tokens with invalid claims before trying to verify the token. This lets
// us do cheap checks before possibly re-syncing keys.
payload, err := parseJWT(rawIDToken)
switch len(jws.Signatures) {
case 0:
return nil, fmt.Errorf("oidc: id token not signed")
case 1:
default:
return nil, fmt.Errorf("oidc: multiple signatures on id token not supported")
}

sig := jws.Signatures[0]
supportedSigAlgs := v.config.SupportedSigningAlgs
if len(supportedSigAlgs) == 0 {
supportedSigAlgs = []string{RS256}
}

if !contains(supportedSigAlgs, sig.Header.Algorithm) {
return nil, fmt.Errorf("oidc: id token signed with unsupported algorithm, expected %q got %q", supportedSigAlgs, sig.Header.Algorithm)
}

payload, err := v.keySet.VerifySignature(ctx, rawIDToken)
Copy link
Copy Markdown

@liggitt liggitt Jan 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how did moving this first help the DOS? this still calls:

VerifySignature → jose.ParseSigned → parseSignedCompact

which still calls parts := strings.Split(input, ".")

If this was trying to catch spurious tokens with lots of . characters, something like the count check in https://go.dev/cl/652155 would seem more effective

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey! The v3 version of this package depends on a go-jose version that doesn't do that.

github.com/go-jose/go-jose/v4 v4.1.3

https://github.com/go-jose/go-jose/blob/v4.1.3/jws.go#L367

For v2, maybe we should do our own check before this to throw out invalid ID tokens early

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For v2, maybe we should do our own check before this to throw out invalid ID tokens early

Yeah, that's exactly what I'd recommend

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(also, sorry for the random 6-month-later drive-by ... I just got here while reviewing kubernetes/kubernetes#136162 (comment))

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lol, no worries. Appreciate you actually checking my work.

Sent the change and tagged a release:

if err != nil {
return nil, fmt.Errorf("oidc: malformed jwt: %v", err)
return nil, fmt.Errorf("failed to verify signature: %v", err)
}

var token idToken
if err := json.Unmarshal(payload, &token); err != nil {
return nil, fmt.Errorf("oidc: failed to unmarshal claims: %v", err)
Expand Down Expand Up @@ -296,36 +311,7 @@ func (v *IDTokenVerifier) Verify(ctx context.Context, rawIDToken string) (*IDTok
}
}

switch len(jws.Signatures) {
case 0:
return nil, fmt.Errorf("oidc: id token not signed")
case 1:
default:
return nil, fmt.Errorf("oidc: multiple signatures on id token not supported")
}

sig := jws.Signatures[0]
supportedSigAlgs := v.config.SupportedSigningAlgs
if len(supportedSigAlgs) == 0 {
supportedSigAlgs = []string{RS256}
}

if !contains(supportedSigAlgs, sig.Header.Algorithm) {
return nil, fmt.Errorf("oidc: id token signed with unsupported algorithm, expected %q got %q", supportedSigAlgs, sig.Header.Algorithm)
}

t.sigAlgorithm = sig.Header.Algorithm

gotPayload, err := v.keySet.VerifySignature(ctx, rawIDToken)
if err != nil {
return nil, fmt.Errorf("failed to verify signature: %v", err)
}

// Ensure that the payload returned by the square actually matches the payload parsed earlier.
if !bytes.Equal(gotPayload, payload) {
return nil, errors.New("oidc: internal error, payload parsed did not match previous payload")
}

return t, nil
}

Expand Down
Loading