Skip to content

fix: do not panic when a trap matches resources by namespace only - #61

Merged
mariotrace merged 2 commits into
dynatrace-oss:mainfrom
knQzx:fix/nil-selector-panic
Jul 31, 2026
Merged

mariotrace merged 2 commits into
dynatrace-oss:mainfrom
knQzx:fix/nil-selector-panic

Conversation

@knQzx

@knQzx knQzx commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Summary

MatchResources.Any[].selector is optional, and the README says a trap only needs one of namespaces or selector. both policy generators dereference the selector without checking it, so a trap that matches by namespace alone panics the reconcile loop

the timing makes it worse than a plain nil deref: reconcileDecoys runs before reconcileCaptors, so the honeytoken files are already written into the workload containers when the panic happens. the TracingPolicy or KivePolicy is never created, so the decoys sit there with nothing watching them, and the panic repeats on every reconcile

a minimal policy that triggers it:

traps:
  - filesystemHoneytoken:
      filePath: /run/secrets/token
      fileContent: some-token
    match:
      any:
        - resources:
            namespaces: [koney-demo]

Changes

skip resource filters that have no selector, which is what the decoy path in matching.go already does (lines 175 and 199). the three affected spots are generateTetragonTracingPolicy and both branches of generateKivePolicy

Testing

added a spec that generates both policy types from a namespace-only trap. it panics with invalid memory address or nil pointer dereference before the change and passes after

go test ./internal/... ./api/... is green. the internal/controller suite needs the envtest binaries, which I do not have locally, and it fails the same way on an unmodified checkout

knQzx and others added 2 commits July 30, 2026 16:24
MatchResources.Any[].selector is optional, and the README documents that a
trap may set only namespaces. Both policy generators dereferenced it
unconditionally, so such a trap panicked the reconcile loop after the decoys
had already been placed, leaving honeytokens without a captor policy.

Skip filters without a selector, as the decoy path in matching.go already
does.
Trap.IsValid() dereferenced value.Selector.MatchLabels whenever
Namespaces was empty, so an explicit `namespaces: []` with no
selector panicked the same way the policy generators did before
this PR, instead of returning the intended validation error.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@mariotrace

Copy link
Copy Markdown
Member

Excellent finding again, thank you very much! Will digging through the diff, I found another related issue in api/v1alpha1/dp_trap_types.go - already committed in f7e7923, waiting for checks to finish.

@mariotrace
mariotrace merged commit 5c2171a into dynatrace-oss:main Jul 31, 2026
7 checks passed
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