Skip to content

backend: OIDC: Return error on state generation failure#5643

Open
harrshita123 wants to merge 1 commit into
kubernetes-sigs:mainfrom
harrshita123:fix-5630-oidc-state-generation
Open

backend: OIDC: Return error on state generation failure#5643
harrshita123 wants to merge 1 commit into
kubernetes-sigs:mainfrom
harrshita123:fix-5630-oidc-state-generation

Conversation

@harrshita123
Copy link
Copy Markdown
Contributor

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

  • Added a small helper to generate the OIDC state value and return an error on failure
  • Updated the OIDC login handler in backend/cmd/headlamp.go to log and return 500 Internal Server Error if state generation fails
  • Added a regression test for successful and failing state generation paths in backend/cmd/headlamp_test.go

Steps to Test

  1. Run the backend command package tests.
  2. Verify the new TestGenerateOidcState regression test passes.
  3. Confirm the OIDC login path no longer uses 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.

@k8s-ci-robot k8s-ci-robot requested review from skoeva and sniok May 14, 2026 13:35
@k8s-ci-robot k8s-ci-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label May 14, 2026
@k8s-ci-robot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: harrshita123
Once this PR has been reviewed and has the lgtm label, please assign joaquimrocha for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label May 14, 2026
@harrshita123 harrshita123 force-pushed the fix-5630-oidc-state-generation branch from 0c97387 to 1adca74 Compare May 14, 2026 15:08
@illume illume requested a review from Copilot May 14, 2026 15:11
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 /oidc handler 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.

Comment thread backend/cmd/headlamp_test.go Outdated
@illume illume requested a review from Copilot May 15, 2026 06:23
Copy link
Copy Markdown
Contributor

@illume illume left a comment

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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

  • generateOidcState ignores the byte count returned by randRead. Since Read is allowed to return n < len(b) with err == nil, this can produce a state value with partially zero-filled bytes. Treat short reads as an error (e.g., check n != len(b) and return io.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
	}

Comment thread backend/cmd/headlamp.go Outdated
Comment thread backend/cmd/headlamp_test.go Outdated
@illume illume requested a review from Copilot May 15, 2026 12:40
Copy link
Copy Markdown
Contributor

@illume illume left a comment

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

@illume illume marked this pull request as draft May 15, 2026 18:36
@k8s-ci-robot k8s-ci-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label May 15, 2026
Copy link
Copy Markdown
Contributor

@illume illume left a comment

Choose a reason for hiding this comment

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

Thanks for these changes.

Can you please address the open review comments? Once you've resolved each one, please mark it as resolved.

@harrshita123 harrshita123 force-pushed the fix-5630-oidc-state-generation branch 2 times, most recently from b9a4a95 to 69474f0 Compare May 15, 2026 20:57
@harrshita123
Copy link
Copy Markdown
Contributor Author

Thanks for these changes.

Can you please address the open review comments? Once you've resolved each one, please mark it as resolved.

@illume
I resolved copilot comment .

@harrshita123 harrshita123 marked this pull request as ready for review May 17, 2026 09:02
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label May 17, 2026
@illume illume requested a review from Copilot May 17, 2026 10:33
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

@harrshita123 harrshita123 force-pushed the fix-5630-oidc-state-generation branch from de85233 to 43e78cc Compare May 17, 2026 11:13
@illume illume requested a review from Copilot May 17, 2026 18:58
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

Comment thread backend/cmd/headlamp.go
Comment on lines +1151 to +1164
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
}
Comment thread backend/cmd/headlamp.go
Comment on lines +1151 to +1162
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
}

Comment on lines +1410 to +1429
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)
Copy link
Copy Markdown
Contributor

@illume illume left a comment

Choose a reason for hiding this comment

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

Thanks for these changes.

Can you please address the open review comments? Once you've resolved each one, please mark it as resolved.

Copy link
Copy Markdown
Contributor

@illume illume left a comment

Choose a reason for hiding this comment

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

Thanks for working on this.

Would you mind addressing the open Copilot review comments? Please mark each comment as resolved after addressing it.

Copy link
Copy Markdown
Contributor

@illume illume left a comment

Choose a reason for hiding this comment

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

Thanks for working on this.

Would you mind addressing the open Copilot review comments? Please mark each comment as resolved after addressing it.

Copy link
Copy Markdown
Contributor

@illume illume left a comment

Choose a reason for hiding this comment

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

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.

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label May 19, 2026
@k8s-ci-robot
Copy link
Copy Markdown
Contributor

PR needs rebase.

Details

Instructions 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.

Copy link
Copy Markdown
Contributor

@illume illume left a comment

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor

@illume illume left a comment

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor

@illume illume left a comment

Choose a reason for hiding this comment

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

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.

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

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

backend: OIDC login should not panic if state generation fails

4 participants