Skip to content

fix(certificate): guard against nil GetCertificateContext response in Update#174

Merged
spbsoluble merged 2 commits into
fix/template-role-binding-paginationfrom
fix/certificate-update-nil-deref
Jun 30, 2026
Merged

fix(certificate): guard against nil GetCertificateContext response in Update#174
spbsoluble merged 2 commits into
fix/template-role-binding-paginationfrom
fix/certificate-update-nil-deref

Conversation

@spbsoluble

Copy link
Copy Markdown
Collaborator

Summary

Fixes a confirmed SIGSEGV nil-pointer-dereference crash in the keyfactor_certificate
resource's Update path. Reported against provider v2.8.1; the same unguarded code
path is present in v2.9.0 (released), v2.9.1-rc.0, and v2 — so this gates a clean
v2.9.1.

Crash

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x200 ...]
keyfactor.resourceCommandCertificate.Update(...)
    keyfactor/resource_keyfactor_certificate.go:1487

Triggered during an in-place update of an existing certificate (a certificate_format
change to PFX) when the context GET /Certificates/{id} fails or returns nil.

Root cause

In Update, hasAPIErrors logged a warning but did not return, after which the code
immediately dereferenced certGetResp.ContentBytes in parseLeafCert. When
GetCertificateContext returned (nil, err) — e.g. a transient/remote API error,
permission/key-recovery rejection, or an unretrievable cert — the nil deref crashed the
whole provider instead of surfacing the API error.

The Read path was hardened with certGetResp != nil guards in v2.9.0; the same
hardening was never applied to Update.

Fix

  • On hasAPIErrors (GET errored), log and returnhasAPIErrors already appended the
    error diagnostic.
  • Added a defensive certGetResp == nil guard that surfaces an explicit
    ERR_SUMMARY_CERTIFICATE_RESOURCE_READ diagnostic and returns (covers the (nil, nil)
    case).
  • Audited every certGetResp. access through the end of Update: all remaining accesses
    after the early return were already nil-guarded (checkCertDiags is internally nil-safe;
    the IsPendingRevocation, RevocationEffDate, and key-recovery blocks all use
    certGetResp != nil &&). No second landmine.

Regression test

TestUnitKeyfactorCertificateResource_UpdateNilGetResponse (unit, no lab) stands up an
httptest TLS server returning HTTP 500 on GET /Certificates/..., sets a PFX Plan over a
PEM State, and calls Update inside a deferred recover so any panic fails the test.
Asserts a clean diagnostic instead of a crash.

  • RED (without fix): FAIL — Update panicked (nil-deref regression): runtime error: invalid memory address or nil pointer dereference
  • GREEN (with fix): PASS
  • go build ./... clean; gofmt clean; full TestUnitKeyfactorCertificate* suite passes.

Notes

… Update

The Update path called GetCertificateContext and, when it returned
(nil, err), only logged a warning via hasAPIErrors before dereferencing
certGetResp.ContentBytes — causing a SIGSEGV nil-pointer dereference
during terraform apply (observed when flipping certificate_format to
PFX on an in-place update).

The Read path was hardened with certGetResp != nil guards in v2.9.0,
but Update was missed. Update now returns a clean diagnostic error and
bails out when the certificate GET fails or returns a nil response,
before any dereference. All subsequent certGetResp accesses in Update
were already nil-guarded.

Adds TestUnitKeyfactorCertificateResource_UpdateNilGetResponse, an
httptest-backed regression test that drives Update with a failing
certificate GET and asserts a diagnostic error is surfaced instead of
a panic.
…scoped diagnostic

Add ERR_SUMMARY_CERTIFICATE_RESOURCE_UPDATE and use it in the defensive
certGetResp == nil guard in Update, so the update path no longer borrows
the read-path diagnostic summary.

Correct the regression test's docstring/comments to describe what is
actually exercised (GET 500 -> hasAPIErrors early return, not the
format-change/parseLeafCert path) and note the certGetResp == nil guard
is belt-and-suspenders for a (nil, nil) response not reproducible via the
httptest harness. Trim the plan to only the fields load-bearing before
the early return.
@spbsoluble
spbsoluble merged commit 8ee68c8 into fix/template-role-binding-pagination Jun 30, 2026
6 of 8 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.

1 participant