Skip to content

fix: prevent panic on invalid regex in reload annotation - #1193

Merged
msafwankarim merged 2 commits into
stakater:masterfrom
nikolauspschuetz:fix/reload-annotation-invalid-regex-panic
Aug 10, 2026
Merged

msafwankarim merged 2 commits into
stakater:masterfrom
nikolauspschuetz:fix/reload-annotation-invalid-regex-panic

Conversation

@nikolauspschuetz

Copy link
Copy Markdown
Contributor

Problem

ShouldReload compiles each comma-separated value of a named reload annotation with regexp.MustCompile:

// pkg/common/common.go
re := regexp.MustCompile("^" + value + "$")

The value comes directly from a user-set annotation on a watched workload (e.g. secret.reloader.stakater.com/reload / configmap.reloader.stakater.com/reload, which support regex). regexp.MustCompile panics on an invalid pattern, and the queue worker (wait.Until(c.runWorker, …)) has no recover(). So a single malformed annotation on any workload in any watched namespace — e.g.:

metadata:
  annotations:
    secret.reloader.stakater.com/reload: "app-config["   # unbalanced bracket

crashes the Reloader process and stops reloads cluster-wide — a low-privilege, cross-namespace availability issue.

Fix

Use regexp.Compile and, on error, log and skip that pattern instead of panicking. Valid patterns behave exactly as before; an invalid one is logged and simply doesn't match.

Test

Added TestShouldReload_InvalidRegexAnnotation_DoesNotPanic in pkg/common/common_test.go. It panics before this change and passes after (verified with go test ./pkg/common/...).

ShouldReload compiled each comma-separated value of a named reload
annotation (e.g. secret.reloader.stakater.com/reload) with
regexp.MustCompile, which panics on an invalid pattern. The value comes
straight from a user-set annotation on a watched workload, and the queue
worker has no recover(), so a single malformed annotation (e.g.
"app-config[") on any workload in any watched namespace crashes Reloader
and stops reloads cluster-wide.

Use regexp.Compile and, on error, log and skip that pattern instead of
panicking.
@nikolauspschuetz
nikolauspschuetz marked this pull request as ready for review July 22, 2026 21:40
Comment thread pkg/common/common.go Resolved
Comment thread pkg/common/common_test.go Resolved
Per review: instead of logging the skipped pattern inside ShouldReload
(which lacks workload identity), collect the compile errors on
ReloadCheckResult.Errors and log them at the upgrade call site, where the
resource name, type, and namespace are known. Add a test for the
multi-value case where one comma-separated pattern is malformed and a
valid one still matches.
@msafwankarim
msafwankarim merged commit b6a6d8c into stakater:master Aug 10, 2026
5 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