Skip to content

add protection to count token segments ahead of parsing#2233

Merged
bobcallaway merged 1 commit into
mainfrom
tacocat
Dec 4, 2025
Merged

add protection to count token segments ahead of parsing#2233
bobcallaway merged 1 commit into
mainfrom
tacocat

Conversation

@bobcallaway

Copy link
Copy Markdown
Member

Count number of separators before calling Split to minimize memory allocation risk

Fixes: GHSA-f83f-xpx7-ffpw

@bobcallaway bobcallaway requested a review from a team as a code owner December 4, 2025 16:53
Fixes: GHSA-f83f-xpx7-ffpw

Signed-off-by: Bob Callaway <bcallaway@google.com>
@bobcallaway bobcallaway enabled auto-merge (squash) December 4, 2025 16:56
@bobcallaway bobcallaway merged commit 765a0e5 into main Dec 4, 2025
17 checks passed
@bobcallaway bobcallaway deleted the tacocat branch December 4, 2025 17:00
return "", fmt.Errorf("oidc: malformed jwt, token must have 3 parts")
}

parts := strings.SplitN(token, ".", 3)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

No need to return a slice of strings when you need to work with the header of a JWT in this function.

header := token[:strings.Index(token, ".")], or say strings.Cut(token, ".") will do their job.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I also suggest using https://github.com/go-jose/go-jose/tree/main/json to parse the header, package github.com/coreos/go-oidc/v3 uses it through the go-jose package already in this project.

https://github.com/go-jose/go-jose/tree/main/json handles duplicate keys in the right way and doesn't do case insensitive matching (before you migrate to https://pkg.go.dev/encoding/json/v2).

parts := strings.Split(token, ".")
if len(parts) != 3 {
return "", fmt.Errorf("oidc: malformed jwt, expected 3 parts got %d", len(parts))
if strings.Count(token, ".") != 2 {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You might want to be a bit more robust and handle not only regular JWTs but also SD JWTs, which have recently been graduated to a proposed standard (RFC 9901).

An SD JWT has two "." or four "." in those cases when it includes a SD-JWT+KB proof (a cryptographic key binding proof).

@caruccio

caruccio commented Dec 16, 2025

Copy link
Copy Markdown

Any plans to back-port this fix to 1.7 and 1.6? Some projects could benefit from it, like kyverno
I could make the PRs.

@Hayden-IO

Copy link
Copy Markdown
Contributor

There are no breaking changes between 1.6 and 1.8 that would prohibit Kyverno from updating its indirect dependency, and I see that Kyverno is already using Go 1.25 so that shouldn't be a blocker either. Note that this impacts deployments only, not client code.

@caruccio

Copy link
Copy Markdown

You are right! Thanks for the feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants