Skip to content

frontend: helpers: clusterSettings: Guard localStorage reads against corrupt data - #7266

Open
Kakumanu-Harshitha wants to merge 1 commit into
kubernetes-sigs:mainfrom
Kakumanu-Harshitha:fix-frontend-cluster-settings
Open

frontend: helpers: clusterSettings: Guard localStorage reads against corrupt data#7266
Kakumanu-Harshitha wants to merge 1 commit into
kubernetes-sigs:mainfrom
Kakumanu-Harshitha:fix-frontend-cluster-settings

Conversation

@Kakumanu-Harshitha

Copy link
Copy Markdown
Contributor

Summary

loadClusterSettings called JSON.parse without any error handling. Corrupted
localStorage entries (e.g. from browser extensions, quota truncation, or manual
edits) threw an uncaught SyntaxError that propagated to all callers
(NamespacesAutocomplete, GlobalSearch, NodeShellTerminal, PodDebugTerminal, etc.),
crashing the settings panel. Additionally, valid but non-object JSON payloads
(123, [], null) passed through silently, breaking downstream property accesses.

Related Issue

Fixes #7265

Changes

  • clusterSettings.ts: Wrap JSON.parse in try/catch; add strict type guard
    rejecting non-object payloads, falling back to {} with a logged error.
  • clusterSettings.ts: Wrap localStorage.setItem in try/catch so quota errors
    are logged instead of thrown.
  • clusterSettings.test.ts: Add tests for invalid JSON, primitive payloads
    (string, number, array, null), and storage quota errors; use try/finally for
    spy cleanup to prevent mock leakage.

Steps to Test

  1. Open DevTools → Application → Local Storage.
  2. Set cluster_settings.<cluster-name> to {not valid json.
  3. Navigate to the cluster settings page — verify no crash, error logged to console.
  4. Set it to 123 or ["a"] — verify loadClusterSettings returns {} gracefully.
  5. Set a valid settings object — verify it loads correctly as before.
  6. Run npm run frontend:test -- src/helpers/clusterSettings.test.ts — all tests pass.

Notes for the Reviewer

@kubernetes-prow kubernetes-prow Bot added needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Aug 15, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Kakumanu-Harshitha
Once this PR has been reviewed and has the lgtm label, please assign sniok 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

@kubernetes-prow kubernetes-prow Bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Aug 15, 2026
@kubernetes-prow
kubernetes-prow Bot requested review from illume and vyncent-t August 15, 2026 15:14
@Kakumanu-Harshitha
Kakumanu-Harshitha force-pushed the fix-frontend-cluster-settings branch from d4976b5 to 64f73af Compare August 15, 2026 15:29
@kubernetes-prow kubernetes-prow Bot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Aug 15, 2026
@illume
illume requested a balanced review from Copilot August 16, 2026 06:32

@illume illume left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the contribution.

Could you take a look at the commit messages in this PR? We follow a Linux kernel style for git commits — see the contributing guide and git log for examples.

Commits that need attention
  • build: update npm version and package-lock.json — Missing area: description prefix — e.g. frontend: HomeButton: Fix so it navigates to home or backend: config: Add enable-dynamic-clusters flag.
  • build: fix npm ci flag and regenerate package-lock.json — Missing area: description prefix — e.g. frontend: HomeButton: Fix so it navigates to home or backend: config: Add enable-dynamic-clusters flag.
  • build: use npm install in Dockerfile to fix lockfile sync issues — Missing area: description prefix — e.g. frontend: HomeButton: Fix so it navigates to home or backend: config: Add enable-dynamic-clusters flag.
  • build(frontend): pin @emnapi versions via overrides to fix lockfile sync — Missing area: description prefix — e.g. frontend: HomeButton: Fix so it navigates to home or backend: config: Add enable-dynamic-clusters flag.
  • build(frontend): add @emnapi to dependencies to ensure lockfile tracking — Missing area: description prefix — e.g. frontend: HomeButton: Fix so it navigates to home or backend: config: Add enable-dynamic-clusters flag.
Commit guidelines
  • Use atomic commits focused on a single change.
  • Use the title format <area>: <Description of changes> — description must start with a capital letter.
  • Keep the title under 72 characters (soft requirement).
  • Explain the intention and why the change is needed.
  • Make commit titles meaningful and describe what changed.
  • Do not add code that a later commit rewrites; squash or reorder commits instead.
  • Do not include Fixes #NN in commit messages.

Good examples:

  • frontend: HomeButton: Fix so it navigates to home
  • backend: config: Add enable-dynamic-clusters flag

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds defensive handling for cluster-setting writes, alongside broader npm and container-build changes. The read-path fix is already present on the base branch.

Changes:

  • Catches and tests localStorage.setItem failures.
  • Regenerates frontend dependencies and adds @emnapi packages.
  • Changes Docker npm installation behavior; plugin-example CI currently fails.

Reviewed changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
frontend/src/helpers/clusterSettings.ts Handles storage write failures.
frontend/src/helpers/clusterSettings.test.ts Tests write failures and improves cleanup.
frontend/package.json Adds and overrides @emnapi dependencies.
frontend/package-lock.json Regenerates dependency resolution.
Dockerfile Updates npm and frontend installation.
Dockerfile.plugins Updates npm for plugin builds.
Files not reviewed (1)
  • frontend/package-lock.json: Generated file

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +58 to +60
try {
localStorage.setItem(`cluster_settings.${clusterName}`, JSON.stringify(settings));
} catch (error) {
Comment thread frontend/package.json Outdated
Comment on lines +13 to +14
"@emnapi/core": "1.10.0",
"@emnapi/runtime": "1.10.0",
Comment thread Dockerfile Outdated
COPY frontend/package*.json /headlamp/frontend/
WORKDIR /headlamp
RUN cd ./frontend && npm ci --only=prod
RUN cd ./frontend && npm install --omit=dev --no-audit --no-fund
Comment thread Dockerfile Outdated

FROM --platform=${BUILDPLATFORM} node:22@sha256:5647be709086c696ff32edaaf1c70cd26d1da6ab2b39c32f3c7b4c4a31957e37 AS frontend-build
# Ensure npm >= 11 to satisfy engines in frontend/package.json
RUN npm install -g npm@^11.0.0
Comment thread Dockerfile.plugins Outdated
# Build the plugin
FROM node:22@sha256:5647be709086c696ff32edaaf1c70cd26d1da6ab2b39c32f3c7b4c4a31957e37 as builder
# Ensure npm >= 11 to satisfy engines in frontend/package.json
RUN npm install -g npm@^11.0.0
@Kakumanu-Harshitha
Kakumanu-Harshitha force-pushed the fix-frontend-cluster-settings branch from 1c1e364 to 756c69f Compare August 16, 2026 06:54
@kubernetes-prow kubernetes-prow Bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Aug 16, 2026
…orage failures

Wrap localStorage.setItem in storeClusterSettings with try/catch to
prevent an uncaught QuotaExceededError (or any storage exception) from
crashing the application. On failure, the error is logged to console
and the call becomes a safe no-op.

Add tests for the new error-handling path and harden existing test
spy teardown with try/finally blocks.

Signed-off-by: Kakumanu-Harshitha <harshithakakumanu2006@gmail.com>
@Kakumanu-Harshitha
Kakumanu-Harshitha force-pushed the fix-frontend-cluster-settings branch from 756c69f to 9721b93 Compare August 16, 2026 07:04

@illume illume left a comment

Copy link
Copy Markdown
Contributor

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.

@kubernetes-prow kubernetes-prow Bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Aug 16, 2026
@Kakumanu-Harshitha
Kakumanu-Harshitha force-pushed the fix-frontend-cluster-settings branch from f00969f to 9721b93 Compare August 16, 2026 17:22
@kubernetes-prow kubernetes-prow Bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Aug 16, 2026
@Kakumanu-Harshitha

Copy link
Copy Markdown
Contributor Author

hey @illume PTAL! when you are free?

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.

frontend: helpers: clusterSettings: loadClusterSettings crashes on corrupted localStorage payload

3 participants