| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
# Test password normalization in SCRAM. |
| 5 |
# |
| 6 |
# This test can only run with Unix-domain sockets. |
| 7 |
|
| 8 |
use strict; |
| 9 |
use warnings FATAL => 'all'; |
| 10 |
use PostgreSQL::Test::Cluster; |
| 11 |
use PostgreSQL::Test::Utils; |
| 12 |
use Test::More; |
| 13 |
if (!$use_unix_sockets) |
| 14 |
{ |
| 15 |
plan skip_all => |
| 16 |
"authentication tests cannot run without Unix-domain sockets"; |
| 17 |
} |
| 18 |
|
| 19 |
# Delete pg_hba.conf from the given node, add a new entry to it |
| 20 |
# and then execute a reload to refresh it. |
| 21 |
sub reset_pg_hba |
| 22 |
{ |
| 23 |
my $node = shift; |
| 24 |
my $hba_method = shift; |
| 25 |
|
| 26 |
unlink($node->data_dir . '/pg_hba.conf'); |
| 27 |
$node->append_conf('pg_hba.conf', "local all all $hba_method"); |
| 28 |
$node->reload; |
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
# Test access for a single role, useful to wrap all tests into one. |
| 33 |
sub test_login |
| 34 |
{ |
| 35 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 36 |
|
| 37 |
my $node = shift; |
| 38 |
my $role = shift; |
| 39 |
my $password = shift; |
| 40 |
my $expected_res = shift; |
| 41 |
my $status_string = 'failed'; |
| 42 |
|
| 43 |
$status_string = 'success' if ($expected_res eq 0); |
| 44 |
|
| 45 |
my $connstr = "user=$role"; |
| 46 |
my $testname = |
| 47 |
"authentication $status_string for role $role with password $password"; |
| 48 |
|
| 49 |
$ENV{"PGPASSWORD"} = $password; |
| 50 |
if ($expected_res eq 0) |
| 51 |
{ |
| 52 |
$node->connect_ok($connstr, $testname); |
| 53 |
} |
| 54 |
else |
| 55 |
{ |
| 56 |
# No checks of the error message, only the status code. |
| 57 |
$node->connect_fails($connstr, $testname); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
# Initialize primary node. Force UTF-8 encoding, so that we can use non-ASCII |
| 62 |
# characters in the passwords below. |
| 63 |
my $node = PostgreSQL::Test::Cluster->new('primary'); |
| 64 |
$node->init(extra => [ '--locale=C', '--encoding=UTF8' ]); |
| 65 |
$node->start; |
| 66 |
|
| 67 |
# These tests are based on the example strings from RFC4013.txt, |
| 68 |
# Section "3. Examples": |
| 69 |
# |
| 70 |
# # Input Output Comments |
| 71 |
# - ----- ------ -------- |
| 72 |
# 1 I<U+00AD>X IX SOFT HYPHEN mapped to nothing |
| 73 |
# 2 user user no transformation |
| 74 |
# 3 USER USER case preserved, will not match #2 |
| 75 |
# 4 <U+00AA> a output is NFKC, input in ISO 8859-1 |
| 76 |
# 5 <U+2168> IX output is NFKC, will match #1 |
| 77 |
# 6 <U+0007> Error - prohibited character |
| 78 |
# 7 <U+0627><U+0031> Error - bidirectional check |
| 79 |
|
| 80 |
# Create test roles. |
| 81 |
$node->safe_psql( |
| 82 |
'postgres', |
| 83 |
"SET password_encryption='scram-sha-256'; |
| 84 |
SET client_encoding='utf8'; |
| 85 |
CREATE ROLE saslpreptest1_role LOGIN PASSWORD 'IX'; |
| 86 |
CREATE ROLE saslpreptest4a_role LOGIN PASSWORD 'a'; |
| 87 |
CREATE ROLE saslpreptest4b_role LOGIN PASSWORD E'\\xc2\\xaa'; |
| 88 |
CREATE ROLE saslpreptest6_role LOGIN PASSWORD E'foo\\x07bar'; |
| 89 |
CREATE ROLE saslpreptest7_role LOGIN PASSWORD E'foo\\u0627\\u0031bar'; |
| 90 |
"); |
| 91 |
|
| 92 |
# Require password from now on. |
| 93 |
reset_pg_hba($node, 'scram-sha-256'); |
| 94 |
|
| 95 |
# Check that #1 and #5 are treated the same as just 'IX' |
| 96 |
test_login($node, 'saslpreptest1_role', "I\xc2\xadX", 0); |
| 97 |
test_login($node, 'saslpreptest1_role', "\xe2\x85\xa8", 0); |
| 98 |
|
| 99 |
# but different from lower case 'ix' |
| 100 |
test_login($node, 'saslpreptest1_role', "ix", 2); |
| 101 |
|
| 102 |
# Check #4 |
| 103 |
test_login($node, 'saslpreptest4a_role', "a", 0); |
| 104 |
test_login($node, 'saslpreptest4a_role', "\xc2\xaa", 0); |
| 105 |
test_login($node, 'saslpreptest4b_role', "a", 0); |
| 106 |
test_login($node, 'saslpreptest4b_role', "\xc2\xaa", 0); |
| 107 |
|
| 108 |
# Check #6 and #7 - In PostgreSQL, contrary to the spec, if the password |
| 109 |
# contains prohibited characters, we use it as is, without normalization. |
| 110 |
test_login($node, 'saslpreptest6_role', "foo\x07bar", 0); |
| 111 |
test_login($node, 'saslpreptest6_role', "foobar", 2); |
| 112 |
|
| 113 |
test_login($node, 'saslpreptest7_role', "foo\xd8\xa71bar", 0); |
| 114 |
test_login($node, 'saslpreptest7_role', "foo1\xd8\xa7bar", 2); |
| 115 |
test_login($node, 'saslpreptest7_role', "foobar", 2); |
| 116 |
|
| 117 |
done_testing(); |