backend: OIDC: Return error on state generation failure#5643
backend: OIDC: Return error on state generation failure#5643harrshita123 wants to merge 1 commit into
Conversation
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: harrshita123 The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
0c97387 to
1adca74
Compare
There was a problem hiding this comment.
Pull request overview
This PR updates the backend OIDC login flow to avoid panicking when OAuth state generation fails, instead returning a controlled 500 Internal Server Error and logging the underlying failure. It also introduces a small helper to make state generation testable.
Changes:
- Added
generateOidcState()and a test seam (randRead) to return(state, error)instead of panicking on entropy read failures. - Updated the
/oidchandler to log and return HTTP 500 when state generation fails. - Added a regression test covering success/failure paths for state generation.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| backend/cmd/headlamp.go | Replaces panic-based state generation with an error-returning helper and graceful HTTP 500 handling in the OIDC login route. |
| backend/cmd/headlamp_test.go | Adds a regression test for generateOidcState() using an overridable randomness function. |
illume
left a comment
There was a problem hiding this comment.
Thanks for this PR.
the PR has a merge-main commit; please rebase against main to keep the history clean.
Why this matters
Merge commits from main make the PR history harder to review. Please rebase your branch on top of the latest main instead, then update the PR with the rebased commits.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
Comments suppressed due to low confidence (1)
backend/cmd/headlamp.go:1129
generateOidcStateignores the byte count returned byrandRead. SinceReadis allowed to returnn < len(b)witherr == nil, this can produce a state value with partially zero-filled bytes. Treat short reads as an error (e.g., checkn != len(b)and returnio.ErrUnexpectedEOF) and add a test for the partial-read case.
func generateOidcState() (string, error) {
b := make([]byte, 32)
if _, err := randRead(b); err != nil {
return "", err
}
illume
left a comment
There was a problem hiding this comment.
Thanks for this PR.
the PR has a merge-main commit; please rebase against main to keep the history clean.
Why this matters
Merge commits from main make the PR history harder to review. Please rebase your branch on top of the latest main instead, then update the PR with the rebased commits.
illume
left a comment
There was a problem hiding this comment.
Thanks for these changes.
Can you please address the open review comments? Once you've resolved each one, please mark it as resolved.
b9a4a95 to
69474f0
Compare
@illume |
de85233 to
43e78cc
Compare
| func generateOidcState(read func([]byte) (int, error)) (string, error) { | ||
| b := make([]byte, 32) | ||
|
|
||
| n, err := read(b) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| if n != len(b) { | ||
| return "", io.ErrUnexpectedEOF | ||
| } | ||
|
|
||
| return base64.RawURLEncoding.EncodeToString(b), nil | ||
| } |
| func generateOidcState(read func([]byte) (int, error)) (string, error) { | ||
| b := make([]byte, 32) | ||
|
|
||
| n, err := read(b) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| if n != len(b) { | ||
| return "", io.ErrUnexpectedEOF | ||
| } | ||
|
|
| func TestGenerateOidcState(t *testing.T) { | ||
| t.Run("success", func(t *testing.T) { | ||
| state, err := generateOidcState(func(b []byte) (int, error) { | ||
| for i := range b { | ||
| b[i] = byte(i) | ||
| } | ||
|
|
||
| return len(b), nil | ||
| }) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8", state) | ||
| }) | ||
|
|
||
| t.Run("failure", func(t *testing.T) { | ||
| state, err := generateOidcState(func([]byte) (int, error) { | ||
| return 0, errors.New("rand failure") | ||
| }) | ||
|
|
||
| require.Error(t, err) | ||
| assert.Empty(t, state) |
illume
left a comment
There was a problem hiding this comment.
Thanks for these changes.
Can you please address the open review comments? Once you've resolved each one, please mark it as resolved.
illume
left a comment
There was a problem hiding this comment.
Thanks for working on this.
Would you mind addressing the open Copilot review comments? Please mark each comment as resolved after addressing it.
illume
left a comment
There was a problem hiding this comment.
Thanks for working on this.
Would you mind addressing the open Copilot review comments? Please mark each comment as resolved after addressing it.
illume
left a comment
There was a problem hiding this comment.
Thanks for this PR.
The open review comments from Copilot still need attention — can you have a look? Once addressed, please mark them as resolved.
|
PR needs rebase. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
illume
left a comment
There was a problem hiding this comment.
Thanks for this PR.
There are git conflicts in this PR that need to be resolved.
How to resolve conflicts
Rebase or merge the latest main into your branch, resolve the conflicts, and push the updated branch.
The open review comments from Copilot still need attention — can you have a look? Once addressed, please mark them as resolved.
illume
left a comment
There was a problem hiding this comment.
Thanks for the contribution.
There are some open Copilot review comments — could you take a look at them? Please mark each one as resolved once you've addressed it.
illume
left a comment
There was a problem hiding this comment.
Thanks for the contribution.
There are some open Copilot review comments — could you take a look at them? Please mark each one as resolved once you've addressed it.
Summary
This PR fixes the OIDC login path so Headlamp returns a normal error when OAuth state generation fails instead of panicking the backend handler.
Related Issue
Fixes #5630
Changes
backend/cmd/headlamp.goto log and return500 Internal Server Errorif state generation failsbackend/cmd/headlamp_test.goSteps to Test
TestGenerateOidcStateregression test passes.panic(err)for state generation failure.Screenshots
Not applicable.
Notes for the Reviewer
This change is intentionally small and isolated to the OIDC login path. It replaces the panic path with explicit error handling and keeps the rest of the login flow unchanged.