Skip to content

Commit bd46cce

Browse files
committed
Protect dblink from invalid options when using postgres_fdw server
When dblink uses a postgres_fdw server name for its connection, it is possible for the connection to have options that are invalid with dblink (e.g. "updatable"). The recommended way to avoid this problem is to use dblink_fdw servers instead. However there are use cases for using postgres_fdw, and possibly other FDWs, for dblink connection options, therefore protect against trying to use any options that do not apply by using is_valid_dblink_option() when building the connection string from the options. Back-patch to 9.3. Although 9.2 supports FDWs for connection info, is_valid_dblink_option() did not yet exist, and neither did postgres_fdw, at least in the postgres source tree. Given the lack of previous complaints, fixing that seems too invasive/not worth it. Author: Corey Huinker Reviewed-By: Joe Conway Discussion: https://postgr.es/m/CADkLM%3DfWyXVEyYcqbcRnxcHutkP45UHU9WD7XpdZaMfe7S%3DRwA%40mail.gmail.com
1 parent 1df8b3f commit bd46cce

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

contrib/dblink/dblink.c

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "access/reloptions.h"
4141
#include "catalog/indexing.h"
4242
#include "catalog/namespace.h"
43+
#include "catalog/pg_foreign_data_wrapper.h"
4344
#include "catalog/pg_foreign_server.h"
4445
#include "catalog/pg_type.h"
4546
#include "catalog/pg_user_mapping.h"
@@ -2732,6 +2733,25 @@ get_connect_string(const char *servername)
27322733
AclResult aclresult;
27332734
char *srvname;
27342735

2736+
static const PQconninfoOption *options = NULL;
2737+
2738+
/*
2739+
* Get list of valid libpq options.
2740+
*
2741+
* To avoid unnecessary work, we get the list once and use it throughout
2742+
* the lifetime of this backend process. We don't need to care about
2743+
* memory context issues, because PQconndefaults allocates with malloc.
2744+
*/
2745+
if (!options)
2746+
{
2747+
options = PQconndefaults();
2748+
if (!options) /* assume reason for failure is OOM */
2749+
ereport(ERROR,
2750+
(errcode(ERRCODE_FDW_OUT_OF_MEMORY),
2751+
errmsg("out of memory"),
2752+
errdetail("could not get libpq's default connection options")));
2753+
}
2754+
27352755
/* first gather the server connstr options */
27362756
srvname = pstrdup(servername);
27372757
truncate_identifier(srvname, strlen(srvname), false);
@@ -2755,25 +2775,28 @@ get_connect_string(const char *servername)
27552775
{
27562776
DefElem *def = lfirst(cell);
27572777

2758-
appendStringInfo(buf, "%s='%s' ", def->defname,
2759-
escape_param_str(strVal(def->arg)));
2778+
if (is_valid_dblink_option(options, def->defname, ForeignDataWrapperRelationId))
2779+
appendStringInfo(buf, "%s='%s' ", def->defname,
2780+
escape_param_str(strVal(def->arg)));
27602781
}
27612782

27622783
foreach(cell, foreign_server->options)
27632784
{
27642785
DefElem *def = lfirst(cell);
27652786

2766-
appendStringInfo(buf, "%s='%s' ", def->defname,
2767-
escape_param_str(strVal(def->arg)));
2787+
if (is_valid_dblink_option(options, def->defname, ForeignServerRelationId))
2788+
appendStringInfo(buf, "%s='%s' ", def->defname,
2789+
escape_param_str(strVal(def->arg)));
27682790
}
27692791

27702792
foreach(cell, user_mapping->options)
27712793
{
27722794

27732795
DefElem *def = lfirst(cell);
27742796

2775-
appendStringInfo(buf, "%s='%s' ", def->defname,
2776-
escape_param_str(strVal(def->arg)));
2797+
if (is_valid_dblink_option(options, def->defname, UserMappingRelationId))
2798+
appendStringInfo(buf, "%s='%s' ", def->defname,
2799+
escape_param_str(strVal(def->arg)));
27772800
}
27782801

27792802
return buf->data;

0 commit comments

Comments
 (0)