Skip to content

backend: k8cache: percent-encode '+' in cache key fields to avoid delimiter collision#6127

Open
sid200727 wants to merge 1 commit into
kubernetes-sigs:mainfrom
sid200727:fix/k8cache-context-key-plus-delimiter
Open

backend: k8cache: percent-encode '+' in cache key fields to avoid delimiter collision#6127
sid200727 wants to merge 1 commit into
kubernetes-sigs:mainfrom
sid200727:fix/k8cache-context-key-plus-delimiter

Conversation

@sid200727

Copy link
Copy Markdown

Summary

This PR fixes a cache-key collision bug by percent-encoding any literal + character in the fields used to build k8cache keys, so the + delimiter can no longer be ambiguous with field content.

Related Issue

Fixes #6123

Changes

-> Updated GenerateKey in backend/pkg/k8cache/cacheStore.go to escape literal + to %2B in apiGroup, kind, namespace, and contextID before joining them with + to form the cache key
-> Added a regression test case to TestGenerateKey in backend/pkg/k8cache/cacheStore_test.go covering a context name containing +

Steps to Test

  1. Use a kubeconfig context whose name contains a +, e.g. prod+cluster
  2. Call GenerateKey with a request URL and that context name
  3. Before this fix: the resulting key has more than 4 +-delimited segments (e.g.apps+deployments+default+prod+cluster), which breaksDeleteKeys's positional namespace-stripping (keyPart[2] = "") andcan cause cache invalidation to target the wrong key
  4. After this fix: the + in the context name is escaped to %2B (apps+deployments+default+prod%2Bcluster), so the key always has exactly 4 segments and DeleteKeys parses it correctly
  5. Run go test ./pkg/k8cache/... - all existing tests pass unchanged, plus the new regression test

Screenshots (if applicable)

N/A (backend logic fix, no UI changes)

Notes for the Reviewer

-> DeleteKeys itself needs no changes, the fix is fully contained in GenerateKey, since it now guarantees the only + characters in a key are the delimiters
-> No existing test fixtures use + in any field, so this change is fully backward compatible
-> gofmt, go vet, and npm run backend:lint all pass clean

@kubernetes-prow kubernetes-prow Bot added the do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. label Jun 21, 2026
@kubernetes-prow

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: sid200727
Once this PR has been reviewed and has the lgtm label, please assign illume 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 requested review from ashu8912 and vyncent-t June 21, 2026 12:54
@kubernetes-prow

Copy link
Copy Markdown

Welcome @sid200727!

It looks like this is your first PR to kubernetes-sigs/headlamp 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/headlamp has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@kubernetes-prow kubernetes-prow Bot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Jun 21, 2026
…imiter collision

GenerateKey joins apiGroup, kind, namespace, and contextID with '+' as a
delimiter, and DeleteKeys later splits on '+' to strip the namespace via
positional indexing. Kubeconfig context names are not restricted by
Kubernetes naming rules and may legitimately contain '+', which produces
a key with more than 4 segments and breaks the positional namespace
stripping in DeleteKeys. This can cause cache invalidation to silently
target the wrong key, leaving stale cached API responses served after
a write.

Escape literal '+' to '%2B' in each field before joining so the only
'+' characters remaining in the key are the delimiters themselves.

Related to kubernetes-sigs#6123

Signed-off-by: Siddhi Khandelwal <siddhi.200727@gmail.com>
@sid200727 sid200727 force-pushed the fix/k8cache-context-key-plus-delimiter branch from 64f322a to 6086264 Compare June 21, 2026 13:00
@kubernetes-prow kubernetes-prow Bot removed the do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. label Jun 21, 2026
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/S Denotes a PR that changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: cache key generation breaks when kubeconfig context name contains +

1 participant