postgres.git / summary / log / commit / refs
commit 8e54a414570010afed0b8b1ed10882afba380166
Author: Daniel Gustafsson <dgustafsson@postgresql.org>
Date: Thu Nov 05 13:14:01 2026 +0000
First released in: 18.7, 19.0
oauth: Recheck oauth_validator_libraries during authentication
The validator allowlist is only enforced while parsing pg_hba.conf.
If changing oauth_validator_libraries makes an existing oauth line
invalid, the HBA reload fails and the old lines stay in use, so a
removed validator keeps authenticating users. In particular, when
setting the list to the empty string, the server doesn't refuse an
OAuth connection, as documented.
Backpatch down to v18 where OAuth was introduced.
Author: Zsolt Parragi <zsolt.parragi@percona.com>
Reviewed-by: Daniel Gustafsson <daniel@yesql.se>
Reviewed-by: shihao zhong <zhong950419@gmail.com>
Discussion: https://postgr.es/m/CAN4CZFNbWXwC=s4t9Xu1kitLsQvTe-EwpVtFCjixWCHBxRTraQ@mail.gmail.com
Backpatch-through: 18
src/backend/libpq/auth-oauth.c | 14 ++++++++++++++
src/test/modules/oauth_validator/t/001_server.pl | 16 ++++++++++++++++
2 files changed, 30 insertions(+)
diff --git a/src/backend/libpq/auth-oauth.c b/src/backend/libpq/auth-oauth.c
index 16bd6640de0..1703d230fc0 100644
--- a/src/backend/libpq/auth-oauth.c
+++ b/src/backend/libpq/auth-oauth.c
@@ -102,6 +102,7 @@ static void *
oauth_init(Port *port, const char *selected_mech, const char *shadow_pass)
{
struct oauth_ctx *ctx;
+ char *err_msg;
if (strcmp(selected_mech, OAUTHBEARER_NAME) != 0)
ereport(ERROR,
@@ -117,6 +118,19 @@ oauth_init(Port *port, const char *selected_mech, const char *shadow_pass)
ctx->issuer = port->hba->oauth_issuer;
ctx->scope = port->hba->oauth_scope;
+ /*
+ * The allowlist is otherwise only enforced when pg_hba.conf is parsed. If
+ * a change to oauth_validator_libraries caused the last reload to fail,
+ * we're still using the old HBA lines, and their validator may no longer
+ * be permitted. Check again before loading it. The details go to the
+ * server log only; the client just sees the usual authentication failure.
+ */
+ if (!check_oauth_validator(port->hba, COMMERROR, &err_msg))
+ ereport(ERROR,
+ errcode(ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION),
+ errmsg("OAuth bearer authentication failed for user \"%s\"",
+ port->user_name));
+
load_validator_library(port->hba->oauth_validator);
return ctx;
diff --git a/src/test/modules/oauth_validator/t/001_server.pl b/src/test/modules/oauth_validator/t/001_server.pl
index 71ad1ce1845..38cd1ebb490 100644
--- a/src/test/modules/oauth_validator/t/001_server.pl
+++ b/src/test/modules/oauth_validator/t/001_server.pl
@@ -105,6 +105,22 @@ $log_start = $node->wait_for_log(
$log_start);
$bgconn->query_safe('SELECT 1');
+# The failed reload leaves the old HBA lines in place, but their validator is
+# no longer allowed.
+SKIP:
+{
+ skip "EXEC_BACKEND builds do not use already loaded configuration", 1
+ if ($PostgreSQL::Test::Utils::windows_os);
+ $node->connect_fails(
+ "user=test dbname=postgres oauth_issuer=$issuer oauth_client_id=f02c6361-0635",
+ "validator removed from oauth_validator_libraries is not used",
+ expected_stderr =>
+ qr/OAuth bearer authentication failed for user "test"/,
+ log_like => [
+ qr/oauth_validator_libraries must be set for authentication method oauth/
+ ]);
+}
+
$node->append_conf('postgresql.conf',
"oauth_validator_libraries = 'validator'");
$node->reload;
[parent: 41b4b8e8bf1a]