Skip to content

🐛 fix(collect): prevent latent panic on non-string helm release data (HELMDECODE-01) - #199

Merged
konih merged 1 commit into
mainfrom
fix/helmdecode-01-bytes-panic
Aug 3, 2026
Merged

konih merged 1 commit into
mainfrom
fix/helmdecode-01-bytes-panic

Conversation

@konih

@konih konih commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

HELMDECODE-01 — latent panic hardening in helm release decoding (P3)

Root cause

internal/collect/helmdecode.go secretReleaseData read data.release via
unstructured.NestedMap(obj, "data"). NestedMap deep-copies the value through
runtime.DeepCopyJSONValue, which panics on any []byte value — it accepts
only string/int64/float64/bool/nil/maps/slices (apimachinery converter.go:646,
panic("cannot deep copy %T")). So a programmatically-crafted unstructured object
whose data.release is raw []byte panicked inside NestedMap, before the
type switch — making the switch's case []byte: return v, nil dead code that
masked the panic.

Not a live reconcile-path crash: the API server / JSON decode always yields base64
strings for Secret data, so []byte never occurs in production. But the dead
branch was misleading and the panic was a real latent defect on hostile/crafted
unstructured input.

Fix (option b — remove the dead branch, reject cleanly)

  • Read the field with unstructured.NestedFieldNoCopy (no JSON deep copy), so a
    []byte value now reaches the type switch instead of panicking upstream.
  • Remove the unreachable case []byte; a non-string concrete type falls to
    default and returns a wrapped error (secret data.release has unsupported type %T) — no panic.
  • Add a call-site comment documenting the DeepCopyJSONValue []byte constraint so
    a future refactor doesn't "tidy" it back to NestedMap and reintroduce the panic.
  • data-not-a-map is now handled by an explicit type assertion (still wrapped as
    read secret data: …), preserving existing error messages.

Rationale for (b) over (a): the API server has no legitimate producer of []byte
data.release, so keeping a live []byte branch (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_rawBytesRelease crafts data.release as raw
[]byte and runs decode through a recover helper.

  • Against the unmodified code: DecodeHelmReleaseSecret() panicked on crafted data.release: cannot deep copy []uint8 (test FAILs).
  • After the fix: no panic, returns a wrapped error containing unsupported type
    (test PASSes). All pre-existing helmdecode tests stay green.

Gates

  • go build ./... — OK
  • go vet ./internal/collect/... — OK
  • task lint (golangci-lint v2.11.4 + go-arch-lint) — 0 issues, arch OK
  • go test ./internal/collect/... -count=1 -race (with KUBEBUILDER_ASSETS) — ok
  • go test ./... -count=1 (with KUBEBUILDER_ASSETS) — 39 packages ok, exit 0

Scope

Touches only internal/collect/helmdecode.go and
internal/collect/helmdecode_branches_test.go.

…(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.
Copilot AI review requested due to automatic review settings August 3, 2026 23:49

Copilot AI left a comment

Copy link
Copy Markdown

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 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 data access to unstructured.NestedFieldNoCopy to avoid runtime.DeepCopyJSONValue panicking on []byte.
  • Rejects non-string data.release values via a clean “unsupported type” error (removing the previously-dead []byte branch).
  • Adds a regression test that asserts crafted []byte input 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.
@konih
konih merged commit 06e6325 into main Aug 3, 2026
27 of 28 checks passed
@konih
konih deleted the fix/helmdecode-01-bytes-panic branch August 3, 2026 23:55
@codecov

codecov Bot commented Aug 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@sonarqubecloud

sonarqubecloud Bot commented Aug 3, 2026

Copy link
Copy Markdown

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants