Skip to content

Commit 89c82b2

Browse files
mnenciaarmruleonardoce
committed
fix(validation): validate PostgreSQL parameter name shape (#11029)
PostgreSQL configuration parameter keys from `spec.postgresql.parameters` were rendered verbatim into `postgresql.conf` without any shape validation. A key containing a newline, such as `"#\narchive_command"`, produced two physical lines and injected an arbitrary directive, bypassing the fixed-parameter filter (an exact-key map lookup). Validate each parameter name against the PostgreSQL GUC naming format at the admission webhook so malformed keys are rejected before they reach the renderer. Because the same validator runs at the start of the instance manager reconcile loop through the admission guard, a Cluster that already has a malformed key persisted (created before this fix, or through a webhook bypass) will have its reconciliation stopped until the key is corrected. Closes #11028 Signed-off-by: Marco Nenciarini <[email protected]> Signed-off-by: Armando Ruocco <[email protected]> Signed-off-by: Leonardo Cecchi <[email protected]> Co-authored-by: Armando Ruocco <[email protected]> Co-authored-by: Leonardo Cecchi <[email protected]> (cherry picked from commit c38ec34) (cherry picked from commit 54fce0d)
1 parent 4b8d08b commit 89c82b2

2 files changed

Lines changed: 96 additions & 0 deletions

File tree

‎internal/webhook/v1/cluster_webhook.go‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"context"
2424
"encoding/json"
2525
"fmt"
26+
"regexp"
2627
"slices"
2728
"strconv"
2829
"strings"
@@ -1037,6 +1038,18 @@ func (v *ClusterCustomValidator) validateFailoverQuorum(r *apiv1.Cluster) field.
10371038
return result
10381039
}
10391040

1041+
// postgresParameterNameRegex matches a valid PostgreSQL configuration
1042+
// parameter name: a plain GUC name or a custom (namespaced) one with a
1043+
// single dot. Keys are rendered verbatim into postgresql.conf, so a
1044+
// newline or other unexpected character could inject an arbitrary
1045+
// directive.
1046+
//
1047+
// The pattern follows PostgreSQL's configuration-file lexer (guc-file.l),
1048+
// including the dollar sign it allows in identifiers. It is intentionally
1049+
// restricted to ASCII: the lexer also accepts high-bit bytes, but no real
1050+
// GUC relies on them.
1051+
var postgresParameterNameRegex = regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_$]*(\.[A-Za-z_][A-Za-z0-9_$]*)?$`)
1052+
10401053
// validateConfiguration determines whether a PostgreSQL configuration is valid
10411054
func (v *ClusterCustomValidator) validateConfiguration(r *apiv1.Cluster) field.ErrorList {
10421055
var result field.ErrorList
@@ -1077,6 +1090,17 @@ func (v *ClusterCustomValidator) validateConfiguration(r *apiv1.Cluster) field.E
10771090
sanitizedParameters := postgres.CreatePostgresqlConfiguration(info).GetConfigurationParameters()
10781091

10791092
for key, value := range r.Spec.PostgresConfiguration.Parameters {
1093+
if !postgresParameterNameRegex.MatchString(key) {
1094+
result = append(
1095+
result,
1096+
field.Invalid(
1097+
field.NewPath("spec", "postgresql", "parameters", key),
1098+
key,
1099+
"must be a valid PostgreSQL configuration parameter name "+
1100+
"(a GUC name, optionally namespaced with a single dot)"))
1101+
continue
1102+
}
1103+
10801104
_, isFixed := postgres.FixedConfigurationParameters[key]
10811105
sanitizedValue, presentInSanitizedConfiguration := sanitizedParameters[key]
10821106
if isFixed && (!presentInSanitizedConfiguration || value != sanitizedValue) {

‎internal/webhook/v1/cluster_webhook_test.go‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,78 @@ var _ = Describe("configuration change validation", func() {
514514
v = &ClusterCustomValidator{}
515515
})
516516

517+
It("accepts well-formed parameter names, including namespaced custom GUCs", func() {
518+
clusterNew := &apiv1.Cluster{
519+
Spec: apiv1.ClusterSpec{
520+
PostgresConfiguration: apiv1.PostgresConfiguration{
521+
Parameters: map[string]string{
522+
"work_mem": "16MB",
523+
"auto_explain.log_min_duration": "100ms",
524+
"some$ext.param$1": "value",
525+
},
526+
},
527+
StorageConfiguration: apiv1.StorageConfiguration{
528+
Size: "10Gi",
529+
},
530+
},
531+
}
532+
Expect(v.validateConfiguration(clusterNew)).To(BeEmpty())
533+
})
534+
535+
DescribeTable("rejects a parameter name that could inject a directive",
536+
func(key string) {
537+
clusterNew := &apiv1.Cluster{
538+
Spec: apiv1.ClusterSpec{
539+
PostgresConfiguration: apiv1.PostgresConfiguration{
540+
Parameters: map[string]string{
541+
key: "/bin/evil",
542+
},
543+
},
544+
StorageConfiguration: apiv1.StorageConfiguration{
545+
Size: "10Gi",
546+
},
547+
},
548+
}
549+
errs := v.validateConfiguration(clusterNew)
550+
Expect(errs).To(HaveLen(1))
551+
Expect(errs[0].Type).To(Equal(field.ErrorTypeInvalid))
552+
Expect(errs[0].Field).To(HavePrefix("spec.postgresql.parameters"))
553+
},
554+
Entry("leading comment then newline", "#\narchive_command"),
555+
Entry("valid name with a trailing newline injection", "archive_command\nrestart_after = 0"),
556+
Entry("carriage return", "archive_command\rrestart_after = 0"),
557+
)
558+
559+
DescribeTable("rejects a malformed parameter name",
560+
func(key string) {
561+
clusterNew := &apiv1.Cluster{
562+
Spec: apiv1.ClusterSpec{
563+
PostgresConfiguration: apiv1.PostgresConfiguration{
564+
Parameters: map[string]string{
565+
key: "value",
566+
},
567+
},
568+
StorageConfiguration: apiv1.StorageConfiguration{
569+
Size: "10Gi",
570+
},
571+
},
572+
}
573+
errs := v.validateConfiguration(clusterNew)
574+
Expect(errs).To(HaveLen(1))
575+
Expect(errs[0].Type).To(Equal(field.ErrorTypeInvalid))
576+
Expect(errs[0].Field).To(HavePrefix("spec.postgresql.parameters"))
577+
},
578+
// The end anchor in the name regex matches end-of-text, not end-of-line,
579+
// so a trailing newline is caught even though it carries no injected directive.
580+
Entry("trailing newline", "archive_command\n"),
581+
Entry("embedded NUL byte", "work_mem\x00"),
582+
Entry("equals sign", "work_mem=128MB"),
583+
Entry("leading whitespace", " work_mem"),
584+
Entry("space in the middle", "work mem"),
585+
Entry("hash character", "work#mem"),
586+
Entry("empty string", ""),
587+
)
588+
517589
It("produces no error when WAL size settings are correct", func() {
518590
clusterNew := &apiv1.Cluster{
519591
Spec: apiv1.ClusterSpec{

0 commit comments

Comments
 (0)