]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Disallow "=" in names of reloptions and foreign-data options.
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 2 Jun 2025 19:22:44 +0000 (15:22 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 2 Jun 2025 19:22:44 +0000 (15:22 -0400)
We store values for these options as array elements with the syntax
"name=value", hence a name containing "=" confuses matters when
it's time to read the array back in.  Since validation of the
options is often done (long) after this conversion to array format,
that leads to confusing and off-point error messages.  We can
improve matters by rejecting names containing "=" up-front.

(Probably a better design would have involved pairs of array
elements, but it's too late now --- and anyway, there's no
evident use-case for option names like this.  We already
reject such names in some other contexts such as GUCs.)

Reported-by: Chapman Flack <jcflack@acm.org>
Author: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: Chapman Flack <jcflack@acm.org>
Discussion: https://postgr.es/m/6830EB30.8090904@acm.org
Backpatch-through: 13

contrib/file_fdw/expected/file_fdw.out
contrib/file_fdw/sql/file_fdw.sql
src/backend/access/common/reloptions.c
src/backend/commands/foreigncmds.c

index 72304e0ff321ba2e2c29563162e23b2402f57908..6ae956f88018041f4d028329f0f0342afd7122db 100644 (file)
@@ -48,6 +48,10 @@ SET ROLE regress_file_fdw_superuser;
 CREATE USER MAPPING FOR regress_file_fdw_superuser SERVER file_server;
 CREATE USER MAPPING FOR regress_no_priv_user SERVER file_server;
 -- validator tests
+CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (foo 'bar');  -- ERROR
+ERROR:  invalid option "foo"
+CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS ("a=b" 'true');  -- ERROR
+ERROR:  invalid option name "a=b": must not contain "="
 CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (format 'xml');  -- ERROR
 ERROR:  COPY format "xml" not recognized
 CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (format 'text', quote ':');          -- ERROR
index f0548e14e18452789cd83b4fa8f6d88a6b9284a9..e91b9799c4293e46e4268d8ccddeaa3fff8917c6 100644 (file)
@@ -55,6 +55,8 @@ CREATE USER MAPPING FOR regress_file_fdw_superuser SERVER file_server;
 CREATE USER MAPPING FOR regress_no_priv_user SERVER file_server;
 
 -- validator tests
+CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (foo 'bar');  -- ERROR
+CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS ("a=b" 'true');  -- ERROR
 CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (format 'xml');  -- ERROR
 CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (format 'text', quote ':');          -- ERROR
 CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (format 'text', escape ':');         -- ERROR
index 469de9bb49f604694fe04eb6931591f6cbe141b0..9648a7695801b59dd2d7b7c4e003402112891a21 100644 (file)
@@ -1234,8 +1234,9 @@ transformRelOptions(Datum oldOptions, List *defList, const char *namspace,
                }
                else
                {
-                       text       *t;
+                       const char *name;
                        const char *value;
+                       text       *t;
                        Size            len;
 
                        /*
@@ -1282,11 +1283,19 @@ transformRelOptions(Datum oldOptions, List *defList, const char *namspace,
                         * have just "name", assume "name=true" is meant.  Note: the
                         * namespace is not output.
                         */
+                       name = def->defname;
                        if (def->arg != NULL)
                                value = defGetString(def);
                        else
                                value = "true";
 
+                       /* Insist that name not contain "=", else "a=b=c" is ambiguous */
+                       if (strchr(name, '=') != NULL)
+                               ereport(ERROR,
+                                               (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+                                                errmsg("invalid option name \"%s\": must not contain \"=\"",
+                                                               name)));
+
                        /*
                         * This is not a great place for this test, but there's no other
                         * convenient place to filter the option out. As WITH (oids =
@@ -1294,7 +1303,7 @@ transformRelOptions(Datum oldOptions, List *defList, const char *namspace,
                         * amount of ugly.
                         */
                        if (acceptOidsOff && def->defnamespace == NULL &&
-                               strcmp(def->defname, "oids") == 0)
+                               strcmp(name, "oids") == 0)
                        {
                                if (defGetBoolean(def))
                                        ereport(ERROR,
@@ -1304,11 +1313,11 @@ transformRelOptions(Datum oldOptions, List *defList, const char *namspace,
                                continue;
                        }
 
-                       len = VARHDRSZ + strlen(def->defname) + 1 + strlen(value);
+                       len = VARHDRSZ + strlen(name) + 1 + strlen(value);
                        /* +1 leaves room for sprintf's trailing null */
                        t = (text *) palloc(len + 1);
                        SET_VARSIZE(t, len);
-                       sprintf(VARDATA(t), "%s=%s", def->defname, value);
+                       sprintf(VARDATA(t), "%s=%s", name, value);
 
                        astate = accumArrayResult(astate, PointerGetDatum(t),
                                                                          false, TEXTOID,
index 0ecff545a9e42f2f151705713fcfb350fce27df7..6baf80cf09c70a72a9aecdcd57e8511fe319332e 100644 (file)
@@ -71,15 +71,26 @@ optionListToArray(List *options)
        foreach(cell, options)
        {
                DefElem    *def = lfirst(cell);
+               const char *name;
                const char *value;
                Size            len;
                text       *t;
 
+               name = def->defname;
                value = defGetString(def);
-               len = VARHDRSZ + strlen(def->defname) + 1 + strlen(value);
+
+               /* Insist that name not contain "=", else "a=b=c" is ambiguous */
+               if (strchr(name, '=') != NULL)
+                       ereport(ERROR,
+                                       (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+                                        errmsg("invalid option name \"%s\": must not contain \"=\"",
+                                                       name)));
+
+               len = VARHDRSZ + strlen(name) + 1 + strlen(value);
+               /* +1 leaves room for sprintf's trailing null */
                t = palloc(len + 1);
                SET_VARSIZE(t, len);
-               sprintf(VARDATA(t), "%s=%s", def->defname, value);
+               sprintf(VARDATA(t), "%s=%s", name, value);
 
                astate = accumArrayResult(astate, PointerGetDatum(t),
                                                                  false, TEXTOID,