Skip to content

fix(diff): avoid panic when expected and actual value types differ - #2777

Open
amalsaidov wants to merge 1 commit into
kyverno:mainfrom
amalsaidov:fix/diff-prune-type-mismatch-panic
Open

amalsaidov wants to merge 1 commit into
kyverno:mainfrom
amalsaidov:fix/diff-prune-type-mismatch-panic

Conversation

@amalsaidov

Copy link
Copy Markdown

Explanation

This is a bug fix.

PrettyDiff (pkg/utils/diff) renders the diff that Chainsaw prints when a resource assertion fails. Today, if the expected manifest holds a scalar at a key where the live resource holds a map, Chainsaw panics instead of printing that diff, so the whole run crashes on what should be an ordinary assertion failure.

Related issue

No existing issue. Found while reading the assertion/diff code. Happy to open an issue first if a maintainer prefers.

Proposed Changes

The prune, pruneMetadata and pruneRoot helpers walk the actual object and, whenever a key holds a nested map, recurse into the same key of the expected object using an unchecked type assertion:

if v, ok := v.(map[string]any); ok {
    prune(expected[k].(map[string]any), v)
}

When expected[k] is present but is not a map (for example a ConfigMap asserted with data: foo while the real object has a data map), that assertion panics:

panic: interface conversion: interface {} is string, not map[string]interface {}

This path is reachable from resourceError.Error() (pkg/engine/operations/errors/resource.go), i.e. the normal failure-reporting path, so a slightly malformed assertion crashes the run rather than showing a helpful diff.

The fix guards the expected lookup with the comma-ok form and only recurses when the expected value is also a map. On a type mismatch the differing values simply show up in the rendered diff, which is the useful behaviour.

Added unit tests in pkg/utils/diff/diff_test.go covering a scalar-vs-map mismatch at the root and under metadata (both panic without this change, pass with it). No user-facing behaviour changes beyond no longer crashing, so no proof manifests.

Checklist

  • I have read the contributing guidelines.
  • This is a bug fix and I have added unit tests that prove my fix is effective.

AI-assisted; directed, reviewed and tested by the author.

PrettyDiff is used to render the diff shown when a resource assertion
fails. The prune helpers walk the actual object and, whenever a key holds
a nested map, recurse into the matching key of the expected object with an
unchecked type assertion (expected[k].(map[string]any)).

When the expected manifest holds a scalar at a key where the live resource
holds a map (for example a ConfigMap asserted with "data: foo" while the
real object has a "data" map), that assertion panics with
"interface conversion: interface {} is string, not map[string]interface {}"
and crashes the run instead of printing the diff that explains the failure.

Guard the expected lookup with the comma-ok form and only recurse when the
expected value is a map too, so a type mismatch simply shows up in the diff.

AI-assisted; directed, reviewed and tested by the author.

Signed-off-by: Amal Saidov <amalsaidov@users.noreply.github.com>
@amalsaidov
amalsaidov requested a review from a team as a code owner November 17, 2026 12:30

This branch has not been deployed

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

1 participant