From 3eef0601e3404a9b1238f24f0f2deab571e4a17c Mon Sep 17 00:00:00 2001 From: santrancisco Date: Tue, 21 Nov 2023 16:29:40 +1100 Subject: [PATCH 1/5] Fix file path validation for DatabaseFileSystem --- src/Databases/DatabaseFilesystem.cpp | 6 ++++-- .../02921_database_filesystem_path_check.reference | 2 ++ .../02921_database_filesystem_path_check.sh | 13 +++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 tests/queries/0_stateless/02921_database_filesystem_path_check.reference create mode 100755 tests/queries/0_stateless/02921_database_filesystem_path_check.sh diff --git a/src/Databases/DatabaseFilesystem.cpp b/src/Databases/DatabaseFilesystem.cpp index 49f260034db..b6a5f95a5f7 100644 --- a/src/Databases/DatabaseFilesystem.cpp +++ b/src/Databases/DatabaseFilesystem.cpp @@ -40,13 +40,15 @@ DatabaseFilesystem::DatabaseFilesystem(const String & name_, const String & path { path = user_files_path / path; } - else if (!is_local && !pathStartsWith(fs::path(path), user_files_path)) + + path = fs::absolute(path).lexically_normal(); + + if (!is_local && !pathStartsWith(fs::path(path), user_files_path)) { throw Exception(ErrorCodes::BAD_ARGUMENTS, "Path must be inside user-files path: {}", user_files_path.string()); } - path = fs::absolute(path).lexically_normal(); if (!fs::exists(path)) throw Exception(ErrorCodes::BAD_ARGUMENTS, "Path does not exist: {}", path); } diff --git a/tests/queries/0_stateless/02921_database_filesystem_path_check.reference b/tests/queries/0_stateless/02921_database_filesystem_path_check.reference new file mode 100644 index 00000000000..21ebc92b43f --- /dev/null +++ b/tests/queries/0_stateless/02921_database_filesystem_path_check.reference @@ -0,0 +1,2 @@ +Path must be inside user-files path +Path must be inside user-files path \ No newline at end of file diff --git a/tests/queries/0_stateless/02921_database_filesystem_path_check.sh b/tests/queries/0_stateless/02921_database_filesystem_path_check.sh new file mode 100755 index 00000000000..79315d89f07 --- /dev/null +++ b/tests/queries/0_stateless/02921_database_filesystem_path_check.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +CUR_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +# shellcheck source=../shell_config.sh +. "$CUR_DIR"/../shell_config.sh + +function get_exception_message() +{ + $CLICKHOUSE_CLIENT --query "$1" |& grep -o 'Path must.*path' +} + +get_exception_message "create database db_filesystem ENGINE=Filesystem('/etc');" +get_exception_message "create database db_filesystem ENGINE=Filesystem('../../../../../../../../etc')';" \ No newline at end of file From f4d936fc554ae8af7a2cf01721995aa8e5b143a7 Mon Sep 17 00:00:00 2001 From: santrancisco Date: Tue, 21 Nov 2023 17:39:12 +1100 Subject: [PATCH 2/5] Fix style --- src/Databases/DatabaseFilesystem.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Databases/DatabaseFilesystem.cpp b/src/Databases/DatabaseFilesystem.cpp index b6a5f95a5f7..ca1b5b27a59 100644 --- a/src/Databases/DatabaseFilesystem.cpp +++ b/src/Databases/DatabaseFilesystem.cpp @@ -40,9 +40,9 @@ DatabaseFilesystem::DatabaseFilesystem(const String & name_, const String & path { path = user_files_path / path; } - + path = fs::absolute(path).lexically_normal(); - + if (!is_local && !pathStartsWith(fs::path(path), user_files_path)) { throw Exception(ErrorCodes::BAD_ARGUMENTS, From 0828447a5872e521f1346e25d0b6b5f4d52c2019 Mon Sep 17 00:00:00 2001 From: santrancisco Date: Tue, 21 Nov 2023 21:19:42 +1100 Subject: [PATCH 3/5] Fix fast check grep --- .../queries/0_stateless/02921_database_filesystem_path_check.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/queries/0_stateless/02921_database_filesystem_path_check.sh b/tests/queries/0_stateless/02921_database_filesystem_path_check.sh index 79315d89f07..8b60acbb47e 100755 --- a/tests/queries/0_stateless/02921_database_filesystem_path_check.sh +++ b/tests/queries/0_stateless/02921_database_filesystem_path_check.sh @@ -6,7 +6,7 @@ CUR_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) function get_exception_message() { - $CLICKHOUSE_CLIENT --query "$1" |& grep -o 'Path must.*path' + $CLICKHOUSE_CLIENT --query "$1" |& grep -o 'Path must be inside user-files path' } get_exception_message "create database db_filesystem ENGINE=Filesystem('/etc');" From e9f6c398cf6bd1052a3cd194252580736e97c561 Mon Sep 17 00:00:00 2001 From: santrancisco Date: Tue, 21 Nov 2023 22:26:34 +1100 Subject: [PATCH 4/5] fixing test --- .../02921_database_filesystem_path_check.reference | 2 -- .../02921_database_filesystem_path_check.sh | 13 ------------- .../02921_database_filesystem_path_check.sql | 2 ++ 3 files changed, 2 insertions(+), 15 deletions(-) delete mode 100755 tests/queries/0_stateless/02921_database_filesystem_path_check.sh create mode 100755 tests/queries/0_stateless/02921_database_filesystem_path_check.sql diff --git a/tests/queries/0_stateless/02921_database_filesystem_path_check.reference b/tests/queries/0_stateless/02921_database_filesystem_path_check.reference index 21ebc92b43f..e69de29bb2d 100644 --- a/tests/queries/0_stateless/02921_database_filesystem_path_check.reference +++ b/tests/queries/0_stateless/02921_database_filesystem_path_check.reference @@ -1,2 +0,0 @@ -Path must be inside user-files path -Path must be inside user-files path \ No newline at end of file diff --git a/tests/queries/0_stateless/02921_database_filesystem_path_check.sh b/tests/queries/0_stateless/02921_database_filesystem_path_check.sh deleted file mode 100755 index 8b60acbb47e..00000000000 --- a/tests/queries/0_stateless/02921_database_filesystem_path_check.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env bash - -CUR_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) -# shellcheck source=../shell_config.sh -. "$CUR_DIR"/../shell_config.sh - -function get_exception_message() -{ - $CLICKHOUSE_CLIENT --query "$1" |& grep -o 'Path must be inside user-files path' -} - -get_exception_message "create database db_filesystem ENGINE=Filesystem('/etc');" -get_exception_message "create database db_filesystem ENGINE=Filesystem('../../../../../../../../etc')';" \ No newline at end of file diff --git a/tests/queries/0_stateless/02921_database_filesystem_path_check.sql b/tests/queries/0_stateless/02921_database_filesystem_path_check.sql new file mode 100755 index 00000000000..d62b629df7b --- /dev/null +++ b/tests/queries/0_stateless/02921_database_filesystem_path_check.sql @@ -0,0 +1,2 @@ +create database db_filesystem ENGINE=Filesystem('/etc'); -- { serverError BAD_ARGUMENTS } +create database db_filesystem ENGINE=Filesystem('../../../../../../../../etc'); -- { serverError BAD_ARGUMENTS } \ No newline at end of file From 844125b5de5eb8b4210a89bb63e9b8a3ded085c1 Mon Sep 17 00:00:00 2001 From: santrancisco Date: Tue, 21 Nov 2023 22:43:34 +1100 Subject: [PATCH 5/5] Fix executable flag for styling --- .../queries/0_stateless/02921_database_filesystem_path_check.sql | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 tests/queries/0_stateless/02921_database_filesystem_path_check.sql diff --git a/tests/queries/0_stateless/02921_database_filesystem_path_check.sql b/tests/queries/0_stateless/02921_database_filesystem_path_check.sql old mode 100755 new mode 100644