Skip to content

Commit 54fce0d

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)
1 parent 694c2ed commit 54fce0d

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
@@ -24,6 +24,7 @@ import (
2424
"encoding/json"
2525
"fmt"
2626
"path/filepath"
27+
"regexp"
2728
"slices"
2829
"strconv"
2930
"strings"
@@ -1016,6 +1017,18 @@ func (v *ClusterCustomValidator) validateFailoverQuorum(r *apiv1.Cluster) field.
10161017
return result
10171018
}
10181019

1020+
// postgresParameterNameRegex matches a valid PostgreSQL configuration
1021+
// parameter name: a plain GUC name or a custom (namespaced) one with a
1022+
// single dot. Keys are rendered verbatim into postgresql.conf, so a
1023+
// newline or other unexpected character could inject an arbitrary
1024+
// directive.
1025+
//
1026+
// The pattern follows PostgreSQL's configuration-file lexer (guc-file.l),
1027+
// including the dollar sign it allows in identifiers. It is intentionally
1028+
// restricted to ASCII: the lexer also accepts high-bit bytes, but no real
1029+
// GUC relies on them.
1030+
var postgresParameterNameRegex = regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_$]*(\.[A-Za-z_][A-Za-z0-9_$]*)?$`)
1031+
10191032
// validateConfiguration determines whether a PostgreSQL configuration is valid
10201033
func (v *ClusterCustomValidator) validateConfiguration(r *apiv1.Cluster) field.ErrorList {
10211034
var result field.ErrorList
@@ -1056,6 +1069,17 @@ func (v *ClusterCustomValidator) validateConfiguration(r *apiv1.Cluster) field.E
10561069
sanitizedParameters := postgres.CreatePostgresqlConfiguration(info).GetConfigurationParameters()
10571070

10581071
for key, value := range r.Spec.PostgresConfiguration.Parameters {
1072+
if !postgresParameterNameRegex.MatchString(key) {
1073+
result = append(
1074+
result,
1075+
field.Invalid(
1076+
field.NewPath("spec", "postgresql", "parameters", key),
1077+
key,
1078+
"must be a valid PostgreSQL configuration parameter name "+
1079+
"(a GUC name, optionally namespaced with a single dot)"))
1080+
continue
1081+
}
1082+
10591083
_, isFixed := postgres.FixedConfigurationParameters[key]
10601084
sanitizedValue, presentInSanitizedConfiguration := sanitizedParameters[key]
10611085
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)