Fix CIDRSliceCSV appending to the previous value instead of replacing it - #7831
Open
youdie006 wants to merge 2 commits into
Open
Fix CIDRSliceCSV appending to the previous value instead of replacing it#7831youdie006 wants to merge 2 commits into
youdie006 wants to merge 2 commits into
Conversation
CIDRSliceCSV.Set appended onto whatever the value already held, so a per-tenant override of alertmanager_receivers_firewall_block_cidr_networks was added on top of the operator defaults rather than replacing them. Limits.UnmarshalYAML copies the defaults and then unmarshals the tenant's YAML over them, so CIDRs a tenant removed from the list stayed blocked. StringSliceCSV.Set and SecretStringSliceCSV.Set both assign. Build into a fresh slice so the value replaces the previous one, and so a bad entry leaves the existing value untouched. Signed-off-by: manon <youdie006@users.noreply.github.com>
Signed-off-by: manon <youdie006@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this PR does:
CIDRSliceCSV.Set(pkg/util/flagext/cidr.go:48) did*c = append(*c, *cidr), accumulating onto whatever the value already held instead of replacing it.That matters because
Limits.UnmarshalYAML(pkg/util/validation/limits.go:489) sets the value to the defaults and then unmarshals the tenant's YAML on top — its own comment states the invariant: "We want to set l to the defaults and then overwrite it with the input." Foralertmanager_receivers_firewall_block_cidr_networksthe override was appended instead, so a tenant got operator defaults plus its own list, and CIDRs it removed from the list stayed blocked.Measured through
Overrides.AlertmanagerReceiversBlockCIDRNetworks(limits.go:1123), the value the Alertmanager receiver firewall dialer reads atpkg/alertmanager/alertmanager.go:668:This fails closed — it over-blocks rather than under-blocks — so it is a config-correctness bug, not a security issue.
The two siblings in the same package
Setstringslicecsv.go:15StringSliceCSV.Set*v = strings.Split(s, ",")— replacessecretstringslicecsv.go:23SecretStringSliceCSV.Setvalues, thenv.values = values— replacescidr.go:48CIDRSliceCSV.SetThe fix follows
SecretStringSliceCSVexactly: build into a fresh slice, assign at the end. That also makes a failed entry leave the existing value untouched, which the append version did not — it kept whatever it had already parsed before the error.Not changed:
Set("")still returnscidr: : invalid CIDR address:and leaves the value alone, exactly as on master. #7714 gaveStringSliceCSVan explicit empty-string case; whetherCIDRSliceCSVshould get one too is a separate question and I left it out of this change.Which issue(s) this PR fixes:
No existing issue; found by reading the three CSV slice types in
pkg/util/flagextagainst each other.Testing
Test_CIDRSliceCSV_SetReplacesPreviousValueandTest_CIDRSliceCSV_SetDoesNotMutateOnErrorincidr_test.go, plusTestAlertmanagerReceiversBlockCIDRNetworksPerTenantOverrideReplacesDefaultinlimits_test.go, which drives the real YAML →Overridespath.expected: []string{"172.16.0.0/12"}/actual: []string{"10.0.0.0/8", "192.168.0.0/16", "172.16.0.0/12"}.appendbrings all three failures back. Over-correcting by clearing eagerly (*c = nilup front) failsSetDoesNotMutateOnErrorwithactual: "", and committing partial values on the error path fails it withactual: "192.168.0.0/16"— so the atomicity side is pinned too.go test -tags "netgo slicelabels" -timeout 30m -race -count 1 ./pkg/util/... ./pkg/alertmanager/...passes.golangci-lint runreports 0 issues, run as v2.13.1 built with go1.27.0 insidequay.io/cortexproject/build-image:master-7bc8b2491bso it matches the Lint job exactly.modernize@v0.23.0clean,go vetclean,gofmtclean.Test_CIDRSliceCSV_YamlMarshallingvaries one axis — entry count — and every row starts fromTestStruct{}, so nothing covered a secondSeton a populated value.Checklist
CHANGELOG.mdupdated - the order of entries should be[CHANGE],[FEATURE],[ENHANCEMENT],[BUGFIX]docs/configuration/v1-guarantees.mdupdated if this PR introduces experimental flagsPer
GENAI_POLICY.md: AI usage disclosure — this patch was found and written with Claude Code. I have reviewed every line and can explain it without going back to the tool. All output quoted above is verbatim from running it against master and against this branch.