From b4f2f13ce31d08ce8e01cba6c3c46f3028f6a09b Mon Sep 17 00:00:00 2001 From: IvanHunters Date: Mon, 23 Nov 2026 16:31:35 +0100 Subject: [PATCH] feat(credentials): tell whether a legacy password survives login PostgreSQL applies SASLprep to the presented password at login, and ALTER ROLE ... PASSWORD did the same when it stored the old verifier, so a SCRAM verifier built from the raw bytes of a legacy password silently fails to authenticate any password SASLprep changes, such as one carrying a soft hyphen. CarriesOver tells the conversion which passwords it may keep, so the rest are left to their first mint. pg_saslprep returns every all-ASCII input unchanged through its pg_is_ascii fast path before the prohibited-character step runs, so an all-ASCII password, ASCII controls included, carries over. Every format other than scram-sha-256 stores or hashes the raw bytes and always carries over. Assisted-by: LLM Signed-off-by: IvanHunters Signed-off-by: Andrei Kvapil --- pkg/credentials/format/format.go | 24 ++++++++++++++++++++++++ pkg/credentials/format/format_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/pkg/credentials/format/format.go b/pkg/credentials/format/format.go index 7f7592cac..4c445c0f8 100644 --- a/pkg/credentials/format/format.go +++ b/pkg/credentials/format/format.go @@ -57,6 +57,30 @@ func Known(name string) bool { return false } +// CarriesOver reports whether a legacy plaintext password encoded under name +// still authenticates that same plaintext at login. For scram-sha-256, +// PostgreSQL runs the password through SASLprep (RFC 4013) before it builds or +// checks a SCRAM verifier, but pg_saslprep returns an all-ASCII input unchanged +// through an ASCII fast path (pg_is_ascii) before its mapping and +// prohibited-character steps run, so every all-ASCII password, ASCII controls +// included, is hashed over its raw bytes at both ALTER ROLE and login, and a +// verifier built here from the same bytes matches. A password with any +// non-ASCII byte reaches SASLprep's mapping and may come out as different bytes +// (a soft hyphen U+00AD, say, maps to nothing), so it is re-minted rather than +// stored as a verifier that would silently fail login. Every other format +// hashes or stores the raw bytes, which authenticate as they are. +func CarriesOver(name, password string) bool { + if name != ScramSHA256 { + return true + } + for i := 0; i < len(password); i++ { + if password[i] > 0x7f { + return false + } + } + return true +} + // GeneratePassword draws PasswordLength characters uniformly from the // alphabet with crypto/rand. Rejection sampling keeps the draw unbiased: a // byte is accepted only below the largest multiple of the alphabet size. diff --git a/pkg/credentials/format/format_test.go b/pkg/credentials/format/format_test.go index 63d76d253..a526093a4 100644 --- a/pkg/credentials/format/format_test.go +++ b/pkg/credentials/format/format_test.go @@ -107,6 +107,32 @@ func TestEncodeFixedVectors(t *testing.T) { } } +func TestCarriesOver(t *testing.T) { + tests := []struct { + name, format, password string + want bool + }{ + // PostgreSQL returns an all-ASCII password unchanged through pg_saslprep's + // ASCII fast path, so every all-ASCII password, ASCII controls included, + // carries over; a non-ASCII byte reaches SASLprep's mapping and may be + // rewritten, so it does not. + {"scram printable ASCII", ScramSHA256, "pa55-word_ABC xyz", true}, + {"scram space is printable", ScramSHA256, "two words", true}, + {"scram ASCII control is still ASCII", ScramSHA256, "tab\there", true}, + {"scram soft hyphen rewritten by SASLprep", ScramSHA256, "I­X", false}, + {"scram non-ASCII", ScramSHA256, "café", false}, + // Other formats hash or store the raw bytes, which authenticate as is. + {"sha256 non-ASCII", SHA256, "café", true}, + {"mysql-native soft hyphen", MySQLNative, "I­X", true}, + {"plaintext non-ASCII", Plaintext, "café", true}, + } + for _, tt := range tests { + if got := CarriesOver(tt.format, tt.password); got != tt.want { + t.Errorf("%s: CarriesOver(%q, %q) = %v, want %v", tt.name, tt.format, tt.password, got, tt.want) + } + } +} + func TestEncodeUnknownFormat(t *testing.T) { if _, err := Encode("apr1", "x"); err == nil { t.Fatal("expected an error for an unknown format")