Hi hackers,
contrib/passwordcheck rejects clear-text passwords that contain the role
name, but the check uses a plain strstr(), so it is case-sensitive. A
different capitalization is enough to get around it. For example, for a
role named "alice":
ALTER ROLE alice PASSWORD 'Alice2024!';
is accepted, although the password obviously contains the role name.
The attached patch lowercases both the password and the role name with
str_tolower() before checking whether the password contains the role
name, so the check becomes case-insensitive.
Best regards
From 30c91ac175822aa08110b3f4038f6752dae51fae Mon Sep 17 00:00:00 2001
From: asotolongo <[email protected]>
Date: Tue, 24 Nov 2026 22:36:10 -0300
Subject: [PATCH v1] passwordcheck: Make the user name check case-insensitive
Previously, passwordcheck used strstr() to reject clear-text passwords
containing the role name, so a different capitalization of the role
name was enough to bypass the check. For example, for a role named
"alice", the password "Alice2024!" was accepted.
Lowercase both the password and the role name with str_tolower(),
using the database's default collation, before comparing them.
---
.../passwordcheck/expected/passwordcheck.out | 5 +++-
.../expected/passwordcheck_1.out | 5 +++-
contrib/passwordcheck/passwordcheck.c | 27 ++++++++++++++-----
contrib/passwordcheck/sql/passwordcheck.sql | 3 +++
4 files changed, 31 insertions(+), 9 deletions(-)
diff --git a/contrib/passwordcheck/expected/passwordcheck.out b/contrib/passwordcheck/expected/passwordcheck.out
index 9d02129..16ea2ca 100644
--- a/contrib/passwordcheck/expected/passwordcheck.out
+++ b/contrib/passwordcheck/expected/passwordcheck.out
@@ -6,13 +6,16 @@ ALTER USER regress_passwordcheck_user1 PASSWORD 'a_nice_long_password';
-- error: too short
ALTER USER regress_passwordcheck_user1 PASSWORD 'tooshrt';
ERROR: password is too short
-DETAIL: Password must be at least "passwordcheck.min_password_length" (8) bytes long.
+DETAIL: password must be at least "passwordcheck.min_password_length" (8) bytes long
-- ok
SET passwordcheck.min_password_length = 6;
ALTER USER regress_passwordcheck_user1 PASSWORD 'v_shrt';
-- error: contains user name
ALTER USER regress_passwordcheck_user1 PASSWORD 'xyzregress_passwordcheck_user1';
ERROR: password must not contain user name
+-- error: contains user name (different case)
+ALTER USER regress_passwordcheck_user1 PASSWORD 'xyzREGRESS_PasswordCheck_User1';
+ERROR: password must not contain user name
-- error: contains only letters
ALTER USER regress_passwordcheck_user1 PASSWORD 'alessnicelongpassword';
ERROR: password must contain both letters and nonletters
diff --git a/contrib/passwordcheck/expected/passwordcheck_1.out b/contrib/passwordcheck/expected/passwordcheck_1.out
index a334720..6d27d55 100644
--- a/contrib/passwordcheck/expected/passwordcheck_1.out
+++ b/contrib/passwordcheck/expected/passwordcheck_1.out
@@ -6,13 +6,16 @@ ALTER USER regress_passwordcheck_user1 PASSWORD 'a_nice_long_password';
-- error: too short
ALTER USER regress_passwordcheck_user1 PASSWORD 'tooshrt';
ERROR: password is too short
-DETAIL: Password must be at least "passwordcheck.min_password_length" (8) bytes long.
+DETAIL: password must be at least "passwordcheck.min_password_length" (8) bytes long
-- ok
SET passwordcheck.min_password_length = 6;
ALTER USER regress_passwordcheck_user1 PASSWORD 'v_shrt';
-- error: contains user name
ALTER USER regress_passwordcheck_user1 PASSWORD 'xyzregress_passwordcheck_user1';
ERROR: password must not contain user name
+-- error: contains user name (different case)
+ALTER USER regress_passwordcheck_user1 PASSWORD 'xyzREGRESS_PasswordCheck_User1';
+ERROR: password must not contain user name
-- error: contains only letters
ALTER USER regress_passwordcheck_user1 PASSWORD 'alessnicelongpassword';
ERROR: password must contain both letters and nonletters
diff --git a/contrib/passwordcheck/passwordcheck.c b/contrib/passwordcheck/passwordcheck.c
index b45187c..6ab65c2 100644
--- a/contrib/passwordcheck/passwordcheck.c
+++ b/contrib/passwordcheck/passwordcheck.c
@@ -3,7 +3,7 @@
* passwordcheck.c
*
*
- * Copyright (c) 2009-2026, PostgreSQL Global Development Group
+ * Copyright (c) 2009-2025, PostgreSQL Global Development Group
*
* Author: Laurenz Albe <[email protected]>
*
@@ -21,9 +21,11 @@
#include <crack.h>
#endif
+#include "catalog/pg_collation.h"
#include "commands/user.h"
#include "fmgr.h"
#include "libpq/crypt.h"
+#include "utils/formatting.h"
PG_MODULE_MAGIC_EXT(
.name = "passwordcheck",
@@ -101,14 +103,25 @@ check_password(const char *username,
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("password is too short"),
- errdetail("Password must be at least \"passwordcheck.min_password_length\" (%d) bytes long.",
+ errdetail("password must be at least \"passwordcheck.min_password_length\" (%d) bytes long",
min_password_length)));
- /* check if the password contains the username */
- if (strstr(password, username))
- ereport(ERROR,
- (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
- errmsg("password must not contain user name")));
+ /* check for username in password, case-insensitive (str_tolower) */
+ {
+ char *lower_pwd = str_tolower(password, pwdlen,
+ DEFAULT_COLLATION_OID);
+ char *lower_user = str_tolower(username, strlen(username),
+ DEFAULT_COLLATION_OID);
+ bool found = (strstr(lower_pwd, lower_user) != NULL);
+
+ pfree(lower_pwd);
+ pfree(lower_user);
+
+ if (found)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("password must not contain user name")));
+ }
/* check if the password contains both letters and non-letters */
pwd_has_letter = false;
diff --git a/contrib/passwordcheck/sql/passwordcheck.sql b/contrib/passwordcheck/sql/passwordcheck.sql
index 21ad8d4..4ca136e 100644
--- a/contrib/passwordcheck/sql/passwordcheck.sql
+++ b/contrib/passwordcheck/sql/passwordcheck.sql
@@ -16,6 +16,9 @@ ALTER USER regress_passwordcheck_user1 PASSWORD 'v_shrt';
-- error: contains user name
ALTER USER regress_passwordcheck_user1 PASSWORD 'xyzregress_passwordcheck_user1';
+-- error: contains user name (different case)
+ALTER USER regress_passwordcheck_user1 PASSWORD 'xyzREGRESS_PasswordCheck_User1';
+
-- error: contains only letters
ALTER USER regress_passwordcheck_user1 PASSWORD 'alessnicelongpassword';
--
2.34.1