Repository navigation
🐛 fix(collect): prevent latent panic on non-string helm release data (HELMDECODE-01) - #199
Merged
Merged
Conversation
…(HELMDECODE-01) secretReleaseData read data.release via unstructured.NestedMap, which deep-copies through runtime.DeepCopyJSONValue. That copier PANICS on any []byte value (it accepts only string/int64/float64/bool/nil/maps/slices), so a crafted unstructured object carrying data.release as raw []byte panicked inside NestedMap BEFORE the type switch ran. This made the switch's `case []byte` dead and masked the panic. The API server always encodes Secret data values as base64 STRINGS, so []byte is never produced on the live reconcile path -- but the branch was misleading and the panic was a latent defect on hostile/crafted input. Fix (option b): read the field with unstructured.NestedFieldNoCopy (no deep copy), so a []byte value reaches the type switch; remove the unreachable []byte case and reject any non-string concrete type with a wrapped error (no panic). A call-site comment records the DeepCopyJSONValue []byte constraint to prevent a future revert to NestedMap. Normal base64-string decode path is unchanged. RED-first: new test crafts data.release as raw []byte and asserts no panic; against the old code it captured `cannot deep copy []uint8`, and now returns a wrapped "unsupported type" error.
There was a problem hiding this comment.
Pull request overview
This PR hardens Helm Secret release decoding to avoid a latent panic when decoding crafted unstructured objects where data.release is a non-JSON type (notably []byte), while keeping the normal base64-string production path unchanged.
Changes:
- Switches Secret
dataaccess tounstructured.NestedFieldNoCopyto avoidruntime.DeepCopyJSONValuepanicking on[]byte. - Rejects non-string
data.releasevalues via a clean “unsupported type” error (removing the previously-dead[]bytebranch). - Adds a regression test that asserts crafted
[]byteinput never panics and returns an error containing “unsupported type”.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| internal/collect/helmdecode.go | Avoids deep-copy panic by using NestedFieldNoCopy and cleanly errors on unexpected data.release types. |
| internal/collect/helmdecode_branches_test.go | Adds a no-panic helper and a regression test covering the crafted []byte release case. |
Suppressed comments (1)
internal/collect/helmdecode.go:99
- This comment mentions returning a "wrapped error", but the code below returns a fresh error (no %w). Reword to avoid implying wrapping semantics.
// The API server always encodes Secret data values as base64 STRINGS, so
// any other concrete type (e.g. a crafted []byte) is unexpected. Reject it
// with a wrapped error rather than passing it through or panicking above.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+65
to
+68
| // string/int64/float64/bool/nil/maps/slices). A crafted unstructured object can | ||
| // carry a []byte at data.release; the no-copy accessor lets such a value reach | ||
| // the type switch below and be rejected with a wrapped error instead of | ||
| // panicking. Do NOT switch this back to NestedMap. |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



HELMDECODE-01 — latent panic hardening in helm release decoding (P3)
Root cause
internal/collect/helmdecode.gosecretReleaseDatareaddata.releaseviaunstructured.NestedMap(obj, "data").NestedMapdeep-copies the value throughruntime.DeepCopyJSONValue, which panics on any[]bytevalue — it acceptsonly
string/int64/float64/bool/nil/maps/slices(apimachinery converter.go:646,panic("cannot deep copy %T")). So a programmatically-crafted unstructured objectwhose
data.releaseis raw[]bytepanicked insideNestedMap, before thetype switch — making the switch's
case []byte: return v, nildead code thatmasked the panic.
Not a live reconcile-path crash: the API server / JSON decode always yields base64
strings for Secret
data, so[]bytenever occurs in production. But the deadbranch was misleading and the panic was a real latent defect on hostile/crafted
unstructured input.
Fix (option b — remove the dead branch, reject cleanly)
unstructured.NestedFieldNoCopy(no JSON deep copy), so a[]bytevalue now reaches the type switch instead of panicking upstream.case []byte; a non-string concrete type falls todefaultand returns a wrapped error (secret data.release has unsupported type %T) — no panic.DeepCopyJSONValue[]byteconstraint soa future refactor doesn't "tidy" it back to
NestedMapand reintroduce the panic.data-not-a-map is now handled by an explicit type assertion (still wrapped asread secret data: …), preserving existing error messages.Rationale for (b) over (a): the API server has no legitimate producer of
[]bytedata.release, so keeping a live[]bytebranch (option a) would leave a"when is this ever
[]byte?" question — the exact misleading-branch defect. (b)kills it outright while staying panic-free. The normal base64-string decode path is
unchanged.
RED-first proof
New
TestDecodeHelmReleaseSecret_rawBytesReleasecraftsdata.releaseas raw[]byteand runs decode through arecoverhelper.DecodeHelmReleaseSecret() panicked on crafted data.release: cannot deep copy []uint8(test FAILs).unsupported type(test PASSes). All pre-existing
helmdecodetests stay green.Gates
go build ./...— OKgo vet ./internal/collect/...— OKtask lint(golangci-lint v2.11.4 + go-arch-lint) — 0 issues, arch OKgo test ./internal/collect/... -count=1 -race(with KUBEBUILDER_ASSETS) — okgo test ./... -count=1(with KUBEBUILDER_ASSETS) — 39 packages ok, exit 0Scope
Touches only
internal/collect/helmdecode.goandinternal/collect/helmdecode_branches_test.go.