Disallow "=" in names of reloptions and foreign-data options.
authorTom Lane <[email protected]>
Mon, 2 Jun 2025 19:22:45 +0000 (15:22 -0400)
committerTom Lane <[email protected]>
Mon, 2 Jun 2025 19:22:45 +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 <[email protected]>
Author: Tom Lane <[email protected]>
Reviewed-by: Chapman Flack <[email protected]>
Discussion: https://postgr.es/m/6830EB30.8090904@acm.org
Backpatch-through: 13

contrib/file_fdw/input/file_fdw.source
contrib/file_fdw/output/file_fdw.source
src/backend/access/common/reloptions.c
src/backend/commands/foreigncmds.c

index 45b728eeb3dfead53e1ecbdd00f0a816c6d86bf2..d8101d49840f8664047321da0935560484659db3 100644 (file)
@@ -36,6 +36,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', header 'true');      -- ERROR
 CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (format 'text', quote ':');          -- ERROR
index 52b4d5f1df7fcc5fe0d2f8b734aad8690a63a85f..dfcc833d80b71c85c4cb4d59a3f2af675eae5624 100644 (file)
@@ -31,6 +31,11 @@ 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"
+HINT:  Valid options in this context are: filename, program, format, header, delimiter, quote, escape, null, encoding
+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', header 'true');      -- ERROR
index 39e0cd1fca68921bb78fa4e46f0c410c871011c8..7717eb3733f210859d3f55fbe747e92a89cd349b 100644 (file)
@@ -1226,8 +1226,9 @@ transformRelOptions(Datum oldOptions, List *defList, const char *namspace,
        }
        else
        {
-           text       *t;
+           const char *name;
            const char *value;
+           text       *t;
            Size        len;
 
            /*
@@ -1274,11 +1275,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 =
@@ -1286,7 +1295,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,
@@ -1296,11 +1305,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 901b8bc89b6b136501fa0eb25e5412321759bdd9..1899d36c3b44cbef1b79dfc03ef2c1485ac44f5d 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,