From f3eec6bc50f80884b8d19a8ea7b84a28877f5ef5 Mon Sep 17 00:00:00 2001 From: "Chao Li (Evan)" Date: Tue, 1 Dec 2026 10:44:16 +0800 Subject: [PATCH v7-REL-18 2/2] Warn about relative elements in extension_control_path The available-extension views could list extensions found through relative paths, while CREATE EXTENSION rejected those paths when its search reached them. Move the absolute-path check to get_extension_control_directories() and warn and skip relative elements for all callers. Extensions found only through relative elements are neither listed nor loaded, while valid absolute elements remain usable. Unknown-language hint lookups also retain their original errors and can find hints in the remaining directories. Use WARNING rather than the ERROR used for ordinary lookups on master to avoid breaking configurations that previously found extensions in an earlier absolute directory, while notifying users of the mistake. 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 behavior and add TAP coverage for warnings, relative-only extensions, continued lookups through absolute directories, and unknown-language errors and hints. 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 | 45 ++++++----- .../t/001_extension_control_path.pl | 75 +++++++++++++++++++ 3 files changed, 97 insertions(+), 24 deletions(-) diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml index 142a312230d..8c222306293 100644 --- a/doc/src/sgml/config.sgml +++ b/doc/src/sgml/config.sgml @@ -11226,6 +11226,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 generate a warning and are + ignored. Relative path elements also generate a warning and are ignored. diff --git a/src/backend/commands/extension.c b/src/backend/commands/extension.c index 59f0e64ca57..e278b71d348 100644 --- a/src/backend/commands/extension.c +++ b/src/backend/commands/extension.c @@ -177,8 +177,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); /* * get_extension_oid - given an extension name, look up the OID @@ -470,8 +469,9 @@ is_extension_script_filename(const char *filename) /* * Return a list of directories declared on 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) @@ -484,6 +484,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) { @@ -531,7 +532,16 @@ get_extension_control_directories(int elevel) /* Canonicalize the path based on the OS and add to the list */ canonicalize_path(mangled); - paths = lappend(paths, mangled); + 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 + paths = lappend(paths, mangled); } /* Break if ecp is empty or move to the next path on ecp */ @@ -2767,7 +2777,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(char, location, locations) @@ -4170,40 +4180,27 @@ new_ExtensionControlFile(const char *extname) } /* - * Work in a very similar way with find_in_path but it receives an already - * parsed List of paths to search the basename and it do not support macro - * replacement or custom error messages (for simplicity). - * - * By "already parsed List of paths" this function expected 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; foreach(cell, paths) { - char *path = lfirst(cell); + const char *path = lfirst(cell); 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 27eeb31a7a9..59dcaf60e8f 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 @@ -152,6 +152,81 @@ 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); +{ + my ($code, $stdout, $stderr) = $node->psql( + 'postgres', + "set extension_control_path = '\$system${sep}relative'; + select count(*) from pg_available_extensions where name = '$relative_ext'"); + is($code, 0, 'relative element does not prevent listing extensions'); + is($stdout, '0', 'extension in a relative directory is not listed'); + like($stderr, + qr/WARNING: component in parameter "extension_control_path" is not an absolute path/, + 'warning identifies the relative path element during listing'); + + ($code, $stdout, $stderr) = $node->psql( + 'postgres', + "set extension_control_path = '\$system${sep}relative'; + create extension $relative_ext"); + is($code, 3, 'extension in a relative directory cannot be loaded'); + like($stderr, + qr/WARNING: component in parameter "extension_control_path" is not an absolute path/, + 'warning identifies the relative path element during creation'); + like($stderr, + qr/ERROR: extension "\Q$relative_ext\E" is not available/, + 'relative directory is excluded from the extension search'); +} + +# The same extension can be found and loaded through an absolute path, even +# with relative elements before or after that path. +create_extension($relative_ext, $ext_dir); +for my $path ( + "relative$sep$ext_dir_canonicalized", + "$ext_dir_canonicalized${sep}relative") +{ + my ($code, $stdout, $stderr) = $node->psql( + 'postgres', + "set extension_control_path = '$path'; + select count(*) from pg_available_extensions where name = '$relative_ext'"); + is($code, 0, "relative element in path '$path' does not prevent listing extensions"); + is($stdout, '1', 'extension is listed from the absolute directory'); + like($stderr, + qr/WARNING: component in parameter "extension_control_path" is not an absolute path/, + 'warning identifies the relative path element during listing'); + ($code, $stdout, $stderr) = $node->psql( + 'postgres', + "set extension_control_path = '$path'; create extension $relative_ext"); + is($code, 0, "extension can be loaded using path '$path'"); + like($stderr, + qr/WARNING: component in parameter "extension_control_path" is not an absolute path/, + 'warning identifies the relative path element during creation'); + $node->safe_psql('postgres', "drop extension $relative_ext"); +} + # Test with an extension that does not exists my ($code, $stdout, $stderr) = $node->psql('postgres', "CREATE EXTENSION invalid"); -- 2.50.1 (Apple Git-155)