Skip to content

Conversation

milosgajdos
Copy link
Member

There was a small regression introduced in
#4349.

Specifically, if the certificate chain verification succeeds we should return immediately instead of following up with further verification checks.

This commit fixes that: we only follow up with further token verifications if the x5c header is missing.

We've also refactored this method so it's hopefully clearer:

  • removed named returns -- they're unnecessary and add unnecessary cognitive load when reading the code

Closes #4299

@milosgajdos
Copy link
Member Author

milosgajdos commented Jul 21, 2024

There is a regression in the latest release PTAL @thaJeztah @Jamstah 😬

Once this is merged in, I'll cut a patch beta release

}

return
Copy link
Member

@thaJeztah thaJeztah Jul 21, 2024

Choose a reason for hiding this comment

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

thanks 🙏 (I hate named output vars combined with naked returns)

if err != nil {
// NOTE(milosgajdos): if the x5c header is missing
// the token may have been signed by a JWKS.
if err == jose.ErrMissingX5cHeader {
Copy link
Member

Choose a reason for hiding this comment

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

we should probably use errors.Is here

Copy link
Member Author

Choose a reason for hiding this comment

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

I was considering that, too. Yeah, can change that.

if signingKey, ok := verifyOpts.TrustedKeys[header.KeyID]; ok {
return signingKey, nil
}
return nil, fmt.Errorf("token signed by untrusted key with ID: %q", header.KeyID)
Copy link
Member

Choose a reason for hiding this comment

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

Not new, but I notice we return a "ErrInvalidToken" in other branches; is that error special / used as a sentinel error? (and in that case, must all invalid errors wrap that one somewhere?)

Copy link
Member Author

Choose a reason for hiding this comment

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

ErrinvalidToken is a sentinel error that was in the original code (see v2.8 branch:

ErrInvalidToken = errors.New("invalid token")
)

I think in this specific code we could maybe wrap it, but I'm not sure it's that important, because the other switch branches return different errors, too 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

I am also wondering if we should ditch the switch clause here and replace it with if statements 🤔 might make the code a bit more readble

Copy link
Member

Choose a reason for hiding this comment

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

Nah, I think a switch is nice; with a switch, all conditions are upfront and visible, with if/else if/else, they get buried deeper down the line, which (IMO) usually makes it less readable.

Copy link
Collaborator

@davidspek davidspek left a comment

Choose a reason for hiding this comment

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

Change makes sense to me and looks good!

@milosgajdos milosgajdos force-pushed the fix-auth-verification-chain branch from c6f686b to eb469f1 Compare July 29, 2024 17:47
There was a small regression introduced in
distribution#4349.

Specifically, if the certificate chain verification succeeds we should
return immediately instead of following up with further token verification
checks.

This commit fixes that: we only follow up with further token
verifications if x5c header is missing.

We've also refactored this method so it's hopefully clearer.

Co-authored-by: Kyle Squizzato <ksquizz@gmail.com>
Signed-off-by: Milos Gajdos <milosthegajdos@gmail.com>
@milosgajdos milosgajdos force-pushed the fix-auth-verification-chain branch from eb469f1 to 70e0d88 Compare July 29, 2024 17:48
@milosgajdos milosgajdos merged commit f0bd0f6 into distribution:main Jul 29, 2024
16 checks passed
@milosgajdos milosgajdos deleted the fix-auth-verification-chain branch July 29, 2024 17:56
@calmh
Copy link

calmh commented Aug 17, 2024

Once this is merged in, I'll cut a patch beta release

This is not currently in any release (or even pushed image), right? I was trying out the 3.0.0-beta.1 release and ended up here after googling the problems we see

@milosgajdos
Copy link
Member Author

That's correct, @calmh. We will do another patch release before the stable, but no ETA.

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

Successfully merging this pull request may close these issues.

INFO[0101] failed to verify token: token signed by untrusted key with ID:
5 participants