From e9e591f9c34b61e5c5dfd9540b62a63017a7050d Mon Sep 17 00:00:00 2001 From: "Chao Li (Evan)" Date: Mon, 9 Nov 2026 16:15:01 +0800 Subject: [PATCH v7 2/2] Reject relative elements in extension_control_path The available-extension views could list extensions found through relative paths, while CREATE EXTENSION rejected those paths only when its search reached them. Move the absolute-path check to get_extension_control_directories() so ordinary extension lookups consistently reject relative elements, even when an earlier directory contains the requested extension. When checking for an extension to provide an unknown-language hint, warn and skip relative elements. This preserves the original CREATE FUNCTION or DO error while allowing a hint to be found using the remaining directories. Canonicalize system_dir in get_extension_control_directories() so all returned paths are canonicalized. Make find_in_paths() static and remove its redundant path copying and canonicalization, retaining an assertion that paths are absolute. Document the restriction and add TAP coverage for relative elements and unknown-language errors and hints. Backpatch with WARNING instead of ERROR for ordinary extension lookups, skipping relative elements so valid directories remain usable. Reported-by: Surya Poondla Author: Chao Li Reviewed-by: Surya Poondla Reviewed-by: Matheus Alcantara Reviewed-by: Andres Freund Reviewed-by: Peter Eisentraut Discussion: https://postgr.es/m/DC2D9FA5-45C4-494F-99FC-F9B95AD4FF55@gmail.com --- doc/src/sgml/config.sgml | 1 + src/backend/commands/extension.c | 61 +++++++++---------- .../t/001_extension_control_path.pl | 59 ++++++++++++++++++ 3 files changed, 90 insertions(+), 31 deletions(-) diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml index 48a8953b16b..77a10467523 100644 --- a/doc/src/sgml/config.sgml +++ b/doc/src/sgml/config.sgml @@ -11807,6 +11807,7 @@ extension_control_path = 'C:\tools\postgresql;H:\my_project\share;$system' '$system'. If the value is set to an empty string, the default '$system' is also assumed. Empty elements within a nonempty value are not allowed. + Relative path elements are not allowed. diff --git a/src/backend/commands/extension.c b/src/backend/commands/extension.c index 3cff5155889..36de781707c 100644 --- a/src/backend/commands/extension.c +++ b/src/backend/commands/extension.c @@ -195,8 +195,7 @@ static void ExecAlterExtensionContentsRecurse(AlterExtensionContentsStmt *stmt, ObjectAddress object); static char *read_whole_file(const char *filename, int *length); static ExtensionControlFile *new_ExtensionControlFile(const char *extname); - -char *find_in_paths(const char *basename, List *paths); +static char *find_in_paths(const char *basename, List *paths); /* * Return the extension location. If the current user doesn't have sufficient @@ -511,8 +510,9 @@ is_extension_script_filename(const char *filename) /* * Return a list of directories declared in the extension_control_path GUC. - * Report empty elements at elevel. If elevel is less than ERROR, skip them - * and continue searching the remaining directories. + * All returned directory paths are absolute and canonicalized. + * Report empty and relative elements at elevel. If elevel is less than ERROR, + * skip them and continue searching the remaining directories. */ static List * get_extension_control_directories(int elevel) @@ -526,6 +526,7 @@ get_extension_control_directories(int elevel) get_share_path(my_exec_path, sharepath); system_dir = psprintf("%s/extension", sharepath); + canonicalize_path(system_dir); if (strlen(Extension_control_path) == 0) { @@ -559,7 +560,7 @@ get_extension_control_directories(int elevel) else { char *mangled; - ExtensionLocation *location = palloc_object(ExtensionLocation); + bool is_system; /* Copy the next path found on ecp */ piece = palloc(len + 1); @@ -569,22 +570,31 @@ get_extension_control_directories(int elevel) * Substitute the path macro if needed or append "extension" * suffix if it is a custom extension control path. */ - if (strcmp(piece, EXTENSION_SYSTEM_MACRO) == 0) - { - location->macro = pstrdup(piece); + is_system = strcmp(piece, EXTENSION_SYSTEM_MACRO) == 0; + if (is_system) mangled = substitute_path_macro(piece, EXTENSION_SYSTEM_MACRO, system_dir); - } else - { - location->macro = NULL; mangled = psprintf("%s/extension", piece); - } pfree(piece); /* Canonicalize the path based on the OS and add to the list */ canonicalize_path(mangled); - location->loc = mangled; - paths = lappend(paths, location); + if (!is_absolute_path(mangled)) + { + ereport(elevel, + errcode(ERRCODE_INVALID_NAME), + errmsg("component in parameter \"%s\" is not an absolute path", + "extension_control_path")); + pfree(mangled); + } + else + { + ExtensionLocation *location = palloc_object(ExtensionLocation); + + location->macro = is_system ? pstrdup(EXTENSION_SYSTEM_MACRO) : NULL; + location->loc = mangled; + paths = lappend(paths, location); + } } /* Break if ecp is empty or move to the next path on ecp */ @@ -2832,7 +2842,7 @@ extension_file_exists(const char *extensionName) DIR *dir; struct dirent *de; - /* Empty path elements must not replace the original error. */ + /* Empty or relative path elements must not replace the original error. */ locations = get_extension_control_directories(WARNING); foreach_ptr(ExtensionLocation, location, locations) @@ -4235,12 +4245,10 @@ new_ExtensionControlFile(const char *extname) } /* - * Search for the basename in the list of paths. - * - * Similar to find_in_path but for simplicity does not support custom error - * messages and expects that paths already have all macros replaced. + * Search for the basename in the list of absolute, canonicalized paths + * returned by get_extension_control_directories(). */ -char * +static char * find_in_paths(const char *basename, List *paths) { ListCell *cell; @@ -4248,26 +4256,17 @@ find_in_paths(const char *basename, List *paths) foreach(cell, paths) { ExtensionLocation *location = lfirst(cell); - char *path = location->loc; + const char *path = location->loc; char *full; Assert(path != NULL); - - path = pstrdup(path); - canonicalize_path(path); - - /* only absolute paths */ - if (!is_absolute_path(path)) - ereport(ERROR, - errcode(ERRCODE_INVALID_NAME), - errmsg("component in parameter \"%s\" is not an absolute path", "extension_control_path")); + Assert(is_absolute_path(path)); full = psprintf("%s/%s", path, basename); if (pg_file_exists(full)) return full; - pfree(path); pfree(full); } diff --git a/src/test/modules/test_extensions/t/001_extension_control_path.pl b/src/test/modules/test_extensions/t/001_extension_control_path.pl index f42d76b7ecf..da774b798a5 100644 --- a/src/test/modules/test_extensions/t/001_extension_control_path.pl +++ b/src/test/modules/test_extensions/t/001_extension_control_path.pl @@ -169,6 +169,65 @@ for my $sql ( 'no extension hint is provided without search directories'); } +# Relative elements must not replace an unknown-language error or prevent a hint +# from being found using the remaining directories. +for my $sql ( + "create function test_invalid_language() returns int language $ext_name as 'x'", + "do language $ext_name 'x'") +{ + my ($code, $stdout, $stderr) = $node->psql( + 'postgres', + "set extension_control_path = 'relative$sep$ext_dir_canonicalized${sep}relative'; $sql"); + is($code, 3, "unknown language is rejected by $sql"); + like($stderr, + qr/WARNING: component in parameter "extension_control_path" is not an absolute path/, + 'warning identifies the relative path element during hint lookup'); + like($stderr, qr/ERROR: language "\Q$ext_name\E" does not exist/, + "invalid path does not replace the unknown-language error"); + like($stderr, + qr/HINT: Use CREATE EXTENSION to load the language into the database\./, + "remaining directory provides the extension hint"); +} + +# An extension found only through a relative path must not be listed or loaded. +my $relative_dir = $node->data_dir . '/relative'; +mkpath("$relative_dir/extension"); +my $relative_ext = 'test_relative_path'; +create_extension($relative_ext, $relative_dir); +for my $sql ( + "select count(*) from pg_available_extensions where name = '$relative_ext'", + "create extension $relative_ext") +{ + my ($code, $stdout, $stderr) = $node->psql( + 'postgres', + "set extension_control_path = '\$system${sep}relative'; $sql"); + is($code, 3, "relative directory is rejected by $sql"); + like($stderr, + qr/ERROR: component in parameter "extension_control_path" is not an absolute path/, + 'error identifies the relative path element'); +} + +# Relative elements are rejected even when an absolute directory contains the +# extension, whether the relative element appears before or after it. +create_extension($relative_ext, $ext_dir); +for my $path ( + "relative$sep$ext_dir_canonicalized", + "$ext_dir_canonicalized${sep}relative") +{ + for my $sql ( + "select count(*) from pg_available_extensions where name = '$relative_ext'", + "create extension $relative_ext") + { + my ($code, $stdout, $stderr) = $node->psql( + 'postgres', + "set extension_control_path = '$path'; $sql"); + is($code, 3, "relative element in path '$path' is rejected by $sql"); + like($stderr, + qr/ERROR: component in parameter "extension_control_path" is not an absolute path/, + 'error identifies the relative path element'); + } +} + # Test with an extension that does not exists my ($code, $stdout, $stderr) = $node->psql('postgres', "CREATE EXTENSION invalid"); -- 2.50.1 (Apple Git-155)