mirror of
https://github.com/cozystack/cozystack.git
synced 2026-12-01 20:56:02 +09:00
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 <xorokhotnikov@gmail.com> Signed-off-by: Andrei Kvapil <kvapss@gmail.com>
This commit is contained in:
1 parent
c8ffc87717
commit
b4f2f13ce3
2 files changed
+50
No files matched your search
@@ -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.
|
||||
|
||||
@@ -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, "IX", 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, "IX", 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")
|
||||
|
||||
Reference in new issue
Block a user