diff --git a/src/Common/filesystemHelpers.cpp b/src/Common/filesystemHelpers.cpp index 730def23f61..ee2da75096b 100644 --- a/src/Common/filesystemHelpers.cpp +++ b/src/Common/filesystemHelpers.cpp @@ -130,22 +130,22 @@ bool pathStartsWith(const String & path, const String & prefix_path) return pathStartsWith(filesystem_path, filesystem_prefix_path); } -String SQLiteDatabaseValidatePath(const String & path, ContextPtr context) +String SQLiteDatabaseValidatePath(const String & path, const String & user_files_path) { - auto user_files_path = fs::canonical(context->getUserFilesPath()); + String canonical_user_files_path = fs::canonical(user_files_path); - fs::path canonical_path; + String canonical_path; std::error_code err; if (fs::path(path).is_relative()) - canonical_path = fs::canonical(user_files_path / path, err); + canonical_path = fs::canonical(fs::path(user_files_path) / path, err); else canonical_path = fs::canonical(path, err); if (err) throw Exception(ErrorCodes::PATH_ACCESS_DENIED, "SQLite database path '{}' is invalid. Error: {}", path, err.message()); - if (!canonical_path.string().starts_with(user_files_path.string())) + if (!canonical_path.starts_with(canonical_user_files_path)) throw Exception(ErrorCodes::PATH_ACCESS_DENIED, "SQLite database file path '{}' must be inside 'user_files' directory", path); diff --git a/src/Common/filesystemHelpers.h b/src/Common/filesystemHelpers.h index c53eb6ff65d..8d9965cb89e 100644 --- a/src/Common/filesystemHelpers.h +++ b/src/Common/filesystemHelpers.h @@ -2,7 +2,6 @@ #include #include -#include #include #include @@ -36,7 +35,7 @@ bool pathStartsWith(const std::filesystem::path & path, const std::filesystem::p /// Returns true if path starts with prefix path bool pathStartsWith(const String & path, const String & prefix_path); -String SQLiteDatabaseValidatePath(const String & path, ContextPtr context); +String SQLiteDatabaseValidatePath(const String & path, const String & user_files_path); } namespace FS diff --git a/src/Databases/SQLite/DatabaseSQLite.cpp b/src/Databases/SQLite/DatabaseSQLite.cpp index e9a44affbe7..2a98f3efaee 100644 --- a/src/Databases/SQLite/DatabaseSQLite.cpp +++ b/src/Databases/SQLite/DatabaseSQLite.cpp @@ -32,7 +32,7 @@ DatabaseSQLite::DatabaseSQLite( , database_engine_define(database_engine_define_->clone()) , log(&Poco::Logger::get("DatabaseSQLite")) { - auto db_path = SQLiteDatabaseValidatePath(database_path_, context_); + auto db_path = SQLiteDatabaseValidatePath(database_path_, context_->getUserFilesPath()); sqlite3 * tmp_sqlite_db = nullptr; int status = sqlite3_open(db_path.c_str(), &tmp_sqlite_db); diff --git a/src/Storages/StorageSQLite.cpp b/src/Storages/StorageSQLite.cpp index 40ecfbac7e9..4282f2a7ef3 100644 --- a/src/Storages/StorageSQLite.cpp +++ b/src/Storages/StorageSQLite.cpp @@ -159,7 +159,7 @@ void registerStorageSQLite(StorageFactory & factory) const auto database_path = engine_args[0]->as().value.safeGet(); const auto table_name = engine_args[1]->as().value.safeGet(); - auto db_path = SQLiteDatabaseValidatePath(database_path, args.getContext()); + auto db_path = SQLiteDatabaseValidatePath(database_path, args.getContext()->getUserFilesPath()); sqlite3 * tmp_sqlite_db = nullptr; int status = sqlite3_open(db_path.c_str(), &tmp_sqlite_db); diff --git a/src/TableFunctions/TableFunctionSQLite.cpp b/src/TableFunctions/TableFunctionSQLite.cpp index b8ceceb22d3..aafaa330f8f 100644 --- a/src/TableFunctions/TableFunctionSQLite.cpp +++ b/src/TableFunctions/TableFunctionSQLite.cpp @@ -76,7 +76,7 @@ void TableFunctionSQLite::parseArguments(const ASTPtr & ast_function, ContextPtr database_path = args[0]->as().value.safeGet(); remote_table_name = args[1]->as().value.safeGet(); - auto db_path = SQLiteDatabaseValidatePath(database_path, context); + auto db_path = SQLiteDatabaseValidatePath(database_path, context->getUserFilesPath()); sqlite3 * tmp_sqlite_db = nullptr; int status = sqlite3_open(db_path.c_str(), &tmp_sqlite_db); diff --git a/tests/queries/0_stateless/01889_sqlite_read_write.sh b/tests/queries/0_stateless/01889_sqlite_read_write.sh index b8ce8be8e29..73b106e9eb4 100755 --- a/tests/queries/0_stateless/01889_sqlite_read_write.sh +++ b/tests/queries/0_stateless/01889_sqlite_read_write.sh @@ -6,7 +6,6 @@ CUR_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) # See 01658_read_file_to_string_column.sh user_files_path=$(clickhouse-client --query "select _path,_file from file('nonexist.txt', 'CSV', 'val1 char')" 2>&1 | grep Exception | awk '{gsub("/nonexist.txt","",$9); print $9}') -user_files_path=/home/kssenii/ClickHouse/programs/server/user_files mkdir -p ${user_files_path}/ chmod 777 ${user_files_path}