Skip to content

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

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

backend: OIDC: Return error on state generation failure#5643
harrshita123 wants to merge 3 commits 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
@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
Cluster string // cluster context name this is associated with
}

var randRead = rand.Read
Comment on lines +1353 to +1358
func TestGenerateOidcState(t *testing.T) {
originalRandRead := randRead

t.Cleanup(func() {
randRead = originalRandRead
})
@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.

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