On Sun, Nov 29, 2026 at 2:43 AM Stepan Neretin <[email protected]> wrote:

> Hi,
>
> password is a USER MAPPING option because libpq marks it with
> dispchar '*'.  passfile is not marked that way, so postgres_fdw and
> dblink treat it as a FOREIGN SERVER option instead.
>
> That puts the password file path into srvoptions, which are broadly
> readable.  Attached makes passfile user mapping only and
> superuser only, and rejects a leftover server side passfile at
> connect time.  Same idea as keeping secrets out of srvoptions,
> in the spirit of the servicefile revert, just for placement.
>
> I left passfile in the libpqsrv proxy allowlist on purpose.  That
> safelist still blesses it (like sslcert), and Sehrope noted passfile
> does not have the /dev/zero hang that servicefile did [1].
> Docs deferred.
>
> Regards,
> Stepan Neretin
>
> [1]
> https://postgr.es/m/CAH7T-aq+U6GDPO8mr3Gga3KzxGpAE=bslb38x+r8fvwjzf-...@mail.gmail.com
>


Hi,

CFBot failed on kerberos/001_auth because that test still put
passfile on the foreign server.  Attached v1 moves it to the
user mapping.  No other change.

Regards,
Stepan Neretin
From 9e3a15146fc7b33a6a9608eafa3729722c69826f Mon Sep 17 00:00:00 2001
From: Stepan Neretin <[email protected]>
Date: Sun, 29 Nov 2026 02:23:12 +0700
Subject: [PATCH] Make postgres_fdw/dblink passfile a user mapping option

passfile was treated as a FOREIGN SERVER option because libpq does not
mark it with dispchar '*'. That put the password file path into
srvoptions. Accept it only on USER MAPPING, require superuser to set
it, and reject leftover server side values at connect time.
---
 contrib/dblink/dblink.c                       | 26 +++++++++++++++---
 contrib/dblink/expected/dblink.out            | 10 +++++++
 contrib/dblink/sql/dblink.sql                 |  8 ++++++
 contrib/postgres_fdw/connection.c             | 17 ++++++++++++
 .../postgres_fdw/expected/postgres_fdw.out    | 27 ++++++++++++++-----
 contrib/postgres_fdw/option.c                 | 23 +++++++++++-----
 contrib/postgres_fdw/sql/postgres_fdw.sql     | 14 ++++++++--
 src/test/kerberos/t/001_auth.pl               |  4 +--
 8 files changed, 108 insertions(+), 21 deletions(-)

diff --git a/contrib/dblink/dblink.c b/contrib/dblink/dblink.c
index 26e7746d808..2210888aef4 100644
--- a/contrib/dblink/dblink.c
+++ b/contrib/dblink/dblink.c
@@ -2139,6 +2139,14 @@ dblink_fdw_validator(PG_FUNCTION_ARGS)
 
 		if (strcmp(def->defname, "use_scram_passthrough") == 0)
 			(void) defGetBoolean(def);	/* accept only boolean values */
+
+		/* passfile on a user mapping is superuser-only */
+		if (strcmp(def->defname, "passfile") == 0 &&
+			context == UserMappingRelationId && !superuser())
+			ereport(ERROR,
+					(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
+					 errmsg("passfile is superuser-only"),
+					 errhint("User mappings with the passfile option set may only be created or modified by the superuser.")));
 	}
 
 	PG_RETURN_VOID();
@@ -3074,6 +3082,14 @@ get_connect_string(const char *servername)
 		{
 			DefElem    *def = lfirst(cell);
 
+			/* Reject a leftover passfile on the foreign server. */
+			if (strcmp(def->defname, "passfile") == 0)
+				ereport(ERROR,
+						(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+						 errmsg("passfile cannot be specified in options for foreign server \"%s\"",
+								foreign_server->servername),
+						 errhint("Specify passfile in a user mapping instead.")));
+
 			if (is_valid_dblink_option(options, def->defname, ForeignServerRelationId))
 				appendStringInfo(&buf, "%s='%s' ", def->defname,
 								 escape_param_str(strVal(def->arg)));
@@ -3235,10 +3251,14 @@ is_valid_dblink_option(const PQconninfoOption *options, const char *option,
 		return false;
 
 	/*
-	 * If the option is "user" or marked secure, it should be specified only
-	 * in USER MAPPING.  Others should be specified only in SERVER.
+	 * If the option is "user", "passfile", or marked secure, it should be
+	 * specified only in USER MAPPING.  Others should be specified only in
+	 * SERVER.  passfile is not marked '*' by libpq, but a password-file
+	 * path must not live in srvoptions.
 	 */
-	if (strcmp(opt->keyword, "user") == 0 || strchr(opt->dispchar, '*'))
+	if (strcmp(opt->keyword, "user") == 0 ||
+		strcmp(opt->keyword, "passfile") == 0 ||
+		strchr(opt->dispchar, '*'))
 	{
 		if (context != UserMappingRelationId)
 			return false;
diff --git a/contrib/dblink/expected/dblink.out b/contrib/dblink/expected/dblink.out
index a00d74fff1d..f1120d787c9 100644
--- a/contrib/dblink/expected/dblink.out
+++ b/contrib/dblink/expected/dblink.out
@@ -1088,9 +1088,19 @@ ERROR:  invalid option "sslkeylogfile"
 ALTER USER MAPPING FOR public SERVER fdtest
 	OPTIONS (ADD sslkeylogfile './noexist/log');
 ERROR:  invalid option "sslkeylogfile"
+-- passfile is user-mapping-only (not a server option)
+ALTER SERVER fdtest OPTIONS (ADD passfile '/no/such/pgpass');
+ERROR:  invalid option "passfile"
+ALTER USER MAPPING FOR public SERVER fdtest
+	OPTIONS (ADD passfile '/no/such/pgpass');
 GRANT USAGE ON FOREIGN SERVER fdtest TO regress_dblink_user;
 GRANT EXECUTE ON FUNCTION dblink_connect_u(text, text) TO regress_dblink_user;
 SET SESSION AUTHORIZATION regress_dblink_user;
+-- unpriv user cannot set passfile on their own mapping
+CREATE USER MAPPING FOR CURRENT_USER SERVER fdtest
+	OPTIONS (user :'USER', passfile '/no/such/pgpass');
+ERROR:  passfile is superuser-only
+HINT:  User mappings with the passfile option set may only be created or modified by the superuser.
 -- should fail (needs a password)
 SELECT dblink_connect('myconn', 'fdtest');
 ERROR:  password or GSSAPI delegated credentials required
diff --git a/contrib/dblink/sql/dblink.sql b/contrib/dblink/sql/dblink.sql
index bcedf834752..b823b7d6005 100644
--- a/contrib/dblink/sql/dblink.sql
+++ b/contrib/dblink/sql/dblink.sql
@@ -569,10 +569,18 @@ ALTER SERVER fdtest OPTIONS (ADD sslkeylogfile './noexist/log');
 ALTER USER MAPPING FOR public SERVER fdtest
 	OPTIONS (ADD sslkeylogfile './noexist/log');
 
+-- passfile is user-mapping-only (not a server option)
+ALTER SERVER fdtest OPTIONS (ADD passfile '/no/such/pgpass');
+ALTER USER MAPPING FOR public SERVER fdtest
+	OPTIONS (ADD passfile '/no/such/pgpass');
+
 GRANT USAGE ON FOREIGN SERVER fdtest TO regress_dblink_user;
 GRANT EXECUTE ON FUNCTION dblink_connect_u(text, text) TO regress_dblink_user;
 
 SET SESSION AUTHORIZATION regress_dblink_user;
+-- unpriv user cannot set passfile on their own mapping
+CREATE USER MAPPING FOR CURRENT_USER SERVER fdtest
+	OPTIONS (user :'USER', passfile '/no/such/pgpass');
 -- should fail (needs a password)
 SELECT dblink_connect('myconn', 'fdtest');
 -- should succeed
diff --git a/contrib/postgres_fdw/connection.c b/contrib/postgres_fdw/connection.c
index 70d3a93ab5b..c8a888c3cc1 100644
--- a/contrib/postgres_fdw/connection.c
+++ b/contrib/postgres_fdw/connection.c
@@ -513,6 +513,7 @@ construct_connection_params(ForeignServer *server, UserMapping *user,
 	const char **keywords;
 	const char **values;
 	char	   *appname = NULL;
+	ListCell   *cell;
 	int			n;
 
 	/*
@@ -524,6 +525,22 @@ construct_connection_params(ForeignServer *server, UserMapping *user,
 	keywords = palloc_array(const char *, n);
 	values = palloc_array(const char *, n);
 
+	/*
+	 * Reject a leftover passfile on the foreign server.  New DDL forbids it,
+	 * but older servers may still have it in srvoptions.
+	 */
+	foreach(cell, server->options)
+	{
+		DefElem    *def = (DefElem *) lfirst(cell);
+
+		if (strcmp(def->defname, "passfile") == 0)
+			ereport(ERROR,
+					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+					 errmsg("passfile cannot be specified in options for foreign server \"%s\"",
+							server->servername),
+					 errhint("Specify passfile in a user mapping instead.")));
+	}
+
 	n = 0;
 	n += ExtractConnectionOptions(server->options,
 								  keywords + n, values + n);
diff --git a/contrib/postgres_fdw/expected/postgres_fdw.out b/contrib/postgres_fdw/expected/postgres_fdw.out
index aea1ddda308..ee29f0fff95 100644
--- a/contrib/postgres_fdw/expected/postgres_fdw.out
+++ b/contrib/postgres_fdw/expected/postgres_fdw.out
@@ -189,6 +189,11 @@ ALTER USER MAPPING FOR public SERVER testserver1
 -- permitted to check validation.
 ALTER USER MAPPING FOR public SERVER testserver1
 	OPTIONS (ADD sslkey 'value', ADD sslcert 'value');
+-- passfile is user-mapping-only (not a server option)
+ALTER SERVER testserver1 OPTIONS (ADD passfile '/no/such/pgpass');
+ERROR:  invalid option "passfile"
+ALTER USER MAPPING FOR public SERVER testserver1
+	OPTIONS (ADD passfile '/no/such/pgpass');
 -- OAuth options are not allowed in either context
 ALTER SERVER testserver1 OPTIONS (ADD oauth_issuer 'https://example.com');
 ERROR:  invalid option "oauth_issuer"
@@ -11519,10 +11524,15 @@ SELECT 1 FROM ft1_nopw LIMIT 1;
 ERROR:  password or GSSAPI delegated credentials required
 DETAIL:  Non-superusers must delegate GSSAPI credentials, provide a password, or enable SCRAM pass-through in user mapping.
 -- If we add a password to the connstr it'll fail, because we don't allow passwords
--- in connstrs only in user mappings.
+-- in connstrs only in user mappings.  passfile is likewise mapping-only.
 ALTER SERVER loopback_nopw OPTIONS (ADD password 'dummypw');
 ERROR:  invalid option "password"
-HINT:  Perhaps you meant the option "passfile".
+ALTER SERVER loopback_nopw OPTIONS (ADD passfile '/no/such/pgpass');
+ERROR:  invalid option "passfile"
+-- unpriv user cannot set passfile on the user mapping
+ALTER USER MAPPING FOR CURRENT_USER SERVER loopback_nopw OPTIONS (ADD passfile '/no/such/pgpass');
+ERROR:  sslcert, sslkey and passfile are superuser-only
+HINT:  User mappings with the sslcert, sslkey or passfile options set may only be created or modified by the superuser.
 -- If we add a password for our user mapping instead, we should get a different
 -- error because the password wasn't actually *used* when we run with trust auth.
 --
@@ -11551,15 +11561,18 @@ SELECT 1 FROM ft1_nopw LIMIT 1;
         1
 (1 row)
 
--- unpriv user also cannot set sslcert / sslkey on the user mapping
+-- unpriv user also cannot set sslcert / sslkey / passfile on the user mapping
 -- first set password_required so we see the right error messages
 ALTER USER MAPPING FOR CURRENT_USER SERVER loopback_nopw OPTIONS (SET password_required 'true');
 ALTER USER MAPPING FOR CURRENT_USER SERVER loopback_nopw OPTIONS (ADD sslcert 'foo.crt');
-ERROR:  sslcert and sslkey are superuser-only
-HINT:  User mappings with the sslcert or sslkey options set may only be created or modified by the superuser.
+ERROR:  sslcert, sslkey and passfile are superuser-only
+HINT:  User mappings with the sslcert, sslkey or passfile options set may only be created or modified by the superuser.
 ALTER USER MAPPING FOR CURRENT_USER SERVER loopback_nopw OPTIONS (ADD sslkey 'foo.key');
-ERROR:  sslcert and sslkey are superuser-only
-HINT:  User mappings with the sslcert or sslkey options set may only be created or modified by the superuser.
+ERROR:  sslcert, sslkey and passfile are superuser-only
+HINT:  User mappings with the sslcert, sslkey or passfile options set may only be created or modified by the superuser.
+ALTER USER MAPPING FOR CURRENT_USER SERVER loopback_nopw OPTIONS (ADD passfile '/no/such/pgpass');
+ERROR:  sslcert, sslkey and passfile are superuser-only
+HINT:  User mappings with the sslcert, sslkey or passfile options set may only be created or modified by the superuser.
 -- We're done with the role named after a specific user and need to check the
 -- changes to the public mapping.
 DROP USER MAPPING FOR CURRENT_USER SERVER loopback_nopw;
diff --git a/contrib/postgres_fdw/option.c b/contrib/postgres_fdw/option.c
index b5d1d2217ba..0b4871807d3 100644
--- a/contrib/postgres_fdw/option.c
+++ b/contrib/postgres_fdw/option.c
@@ -198,14 +198,15 @@ postgres_fdw_validator(PG_FUNCTION_ARGS)
 						 errhint("User mappings with the password_required option set to false may only be created or modified by the superuser.")));
 		}
 		else if (strcmp(def->defname, "sslcert") == 0 ||
-				 strcmp(def->defname, "sslkey") == 0)
+				 strcmp(def->defname, "sslkey") == 0 ||
+				 strcmp(def->defname, "passfile") == 0)
 		{
-			/* similarly for sslcert / sslkey on user mapping */
+			/* similarly for sslcert / sslkey / passfile on user mapping */
 			if (catalog == UserMappingRelationId && !superuser())
 				ereport(ERROR,
 						(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
-						 errmsg("sslcert and sslkey are superuser-only"),
-						 errhint("User mappings with the sslcert or sslkey options set may only be created or modified by the superuser.")));
+						 errmsg("sslcert, sslkey and passfile are superuser-only"),
+						 errhint("User mappings with the sslcert, sslkey or passfile options set may only be created or modified by the superuser.")));
 		}
 		else if (strcmp(def->defname, "analyze_sampling") == 0)
 		{
@@ -288,6 +289,9 @@ InitPgFdwOptions(void)
 		 * sslcert and sslkey are in fact libpq options, but we repeat them
 		 * here to allow them to appear in both foreign server context (when
 		 * we generate libpq options) and user mapping context (from here).
+		 *
+		 * passfile is classified as user-mapping-only in the libpq loop
+		 * below; we intentionally do not keep a server-option copy.
 		 */
 		{"sslcert", UserMappingRelationId, true},
 		{"sslkey", UserMappingRelationId, true},
@@ -353,10 +357,15 @@ InitPgFdwOptions(void)
 											lopt->keyword);
 
 		/*
-		 * "user" and any secret options are allowed only on user mappings.
-		 * Everything else is a server option.
+		 * "user", passfile, and any secret options (dispchar '*') are allowed
+		 * only on user mappings.  Everything else is a server option.
+		 *
+		 * passfile is not marked '*' by libpq, but a password-file path is
+		 * still secret material and must not live in srvoptions.
 		 */
-		if (strcmp(lopt->keyword, "user") == 0 || strchr(lopt->dispchar, '*'))
+		if (strcmp(lopt->keyword, "user") == 0 ||
+			strcmp(lopt->keyword, "passfile") == 0 ||
+			strchr(lopt->dispchar, '*'))
 			popt->optcontext = UserMappingRelationId;
 		else
 			popt->optcontext = ForeignServerRelationId;
diff --git a/contrib/postgres_fdw/sql/postgres_fdw.sql b/contrib/postgres_fdw/sql/postgres_fdw.sql
index b71771ab8df..597dc84bbd2 100644
--- a/contrib/postgres_fdw/sql/postgres_fdw.sql
+++ b/contrib/postgres_fdw/sql/postgres_fdw.sql
@@ -206,6 +206,11 @@ ALTER USER MAPPING FOR public SERVER testserver1
 ALTER USER MAPPING FOR public SERVER testserver1
 	OPTIONS (ADD sslkey 'value', ADD sslcert 'value');
 
+-- passfile is user-mapping-only (not a server option)
+ALTER SERVER testserver1 OPTIONS (ADD passfile '/no/such/pgpass');
+ALTER USER MAPPING FOR public SERVER testserver1
+	OPTIONS (ADD passfile '/no/such/pgpass');
+
 -- OAuth options are not allowed in either context
 ALTER SERVER testserver1 OPTIONS (ADD oauth_issuer 'https://example.com');
 ALTER SERVER testserver1 OPTIONS (ADD oauth_client_id 'myID');
@@ -3785,9 +3790,13 @@ CREATE FOREIGN TABLE pg_temp.ft1_nopw (
 SELECT 1 FROM ft1_nopw LIMIT 1;
 
 -- If we add a password to the connstr it'll fail, because we don't allow passwords
--- in connstrs only in user mappings.
+-- in connstrs only in user mappings.  passfile is likewise mapping-only.
 
 ALTER SERVER loopback_nopw OPTIONS (ADD password 'dummypw');
+ALTER SERVER loopback_nopw OPTIONS (ADD passfile '/no/such/pgpass');
+
+-- unpriv user cannot set passfile on the user mapping
+ALTER USER MAPPING FOR CURRENT_USER SERVER loopback_nopw OPTIONS (ADD passfile '/no/such/pgpass');
 
 -- If we add a password for our user mapping instead, we should get a different
 -- error because the password wasn't actually *used* when we run with trust auth.
@@ -3814,11 +3823,12 @@ SET ROLE regress_nosuper;
 -- Should finally work now
 SELECT 1 FROM ft1_nopw LIMIT 1;
 
--- unpriv user also cannot set sslcert / sslkey on the user mapping
+-- unpriv user also cannot set sslcert / sslkey / passfile on the user mapping
 -- first set password_required so we see the right error messages
 ALTER USER MAPPING FOR CURRENT_USER SERVER loopback_nopw OPTIONS (SET password_required 'true');
 ALTER USER MAPPING FOR CURRENT_USER SERVER loopback_nopw OPTIONS (ADD sslcert 'foo.crt');
 ALTER USER MAPPING FOR CURRENT_USER SERVER loopback_nopw OPTIONS (ADD sslkey 'foo.key');
+ALTER USER MAPPING FOR CURRENT_USER SERVER loopback_nopw OPTIONS (ADD passfile '/no/such/pgpass');
 
 -- We're done with the role named after a specific user and need to check the
 -- changes to the public mapping.
diff --git a/src/test/kerberos/t/001_auth.pl b/src/test/kerberos/t/001_auth.pl
index d4018acde88..2544ba011db 100644
--- a/src/test/kerberos/t/001_auth.pl
+++ b/src/test/kerberos/t/001_auth.pl
@@ -82,7 +82,7 @@ $node->safe_psql('postgres',
 	"CREATE SERVER s1 FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host '$host', hostaddr '$hostaddr', port '$port', dbname 'postgres');"
 );
 $node->safe_psql('postgres',
-	"CREATE SERVER s2 FOREIGN DATA WRAPPER postgres_fdw OPTIONS (port '$port', dbname 'postgres', passfile '$pgpass');"
+	"CREATE SERVER s2 FOREIGN DATA WRAPPER postgres_fdw OPTIONS (port '$port', dbname 'postgres');"
 );
 
 $node->safe_psql('postgres', 'GRANT USAGE ON FOREIGN SERVER s1 TO test1;');
@@ -90,7 +90,7 @@ $node->safe_psql('postgres', 'GRANT USAGE ON FOREIGN SERVER s1 TO test1;');
 $node->safe_psql('postgres',
 	"CREATE USER MAPPING FOR test1 SERVER s1 OPTIONS (user 'test1');");
 $node->safe_psql('postgres',
-	"CREATE USER MAPPING FOR test1 SERVER s2 OPTIONS (user 'test2');");
+	"CREATE USER MAPPING FOR test1 SERVER s2 OPTIONS (user 'test2', passfile '$pgpass');");
 
 $node->safe_psql('postgres', "CREATE TABLE t1 (c1 int);");
 $node->safe_psql('postgres', "INSERT INTO t1 VALUES (1);");
-- 
2.56.1

Reply via email to