Skip to content

Commit 9f9e607

Browse files
authored
Merge pull request ClickHouse#87479 from ClickHouse/backport/25.8/87181
Backport ClickHouse#87181 to 25.8: Make pathStartsWith only match paths under the prefix
2 parents 269fa2e + fed36c3 commit 9f9e607

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

src/Common/filesystemHelpers.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,8 @@ String getFilesystemName([[maybe_unused]] const String & mount_point)
218218

219219
bool pathStartsWith(const std::filesystem::path & path, const std::filesystem::path & prefix_path)
220220
{
221-
String absolute_path = std::filesystem::weakly_canonical(path);
222-
String absolute_prefix_path = std::filesystem::weakly_canonical(prefix_path);
223-
return absolute_path.starts_with(absolute_prefix_path);
221+
auto rel = fs::relative(path, prefix_path);
222+
return (!rel.empty() && (rel.native() == "." || rel.native()[0] != '.'));
224223
}
225224

226225
static bool fileOrSymlinkPathStartsWith(const std::filesystem::path & path, const std::filesystem::path & prefix_path)
@@ -230,11 +229,8 @@ static bool fileOrSymlinkPathStartsWith(const std::filesystem::path & path, cons
230229
/// `.` and `..` and extra `/`. Path is not canonized because otherwise path will
231230
/// not be a path of a symlink itself.
232231

233-
String absolute_path = std::filesystem::absolute(path);
234-
absolute_path = fs::path(absolute_path).lexically_normal(); /// Normalize path.
235-
String absolute_prefix_path = std::filesystem::absolute(prefix_path);
236-
absolute_prefix_path = fs::path(absolute_prefix_path).lexically_normal(); /// Normalize path.
237-
return absolute_path.starts_with(absolute_prefix_path);
232+
auto rel = fs::absolute(path).lexically_normal().lexically_relative(fs::absolute(prefix_path).lexically_normal());
233+
return (!rel.empty() && (rel.native() == "." || rel.native()[0] != '.'));
238234
}
239235

240236
bool pathStartsWith(const String & path, const String & prefix_path)

tests/queries/0_stateless/03624_proper_path_starts_with.reference

Whitespace-only changes.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bash
2+
3+
CUR_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
4+
# shellcheck source=../shell_config.sh
5+
. "$CUR_DIR"/../shell_config.sh
6+
7+
BAD_PATH_PARENT=$(echo "${USER_FILES_PATH}" | sed 's:/*$::')/../BAD
8+
# Remove the trailing slashes and add extra characters to the folder name
9+
BAD_PATH_EXTRA=$(echo "${USER_FILES_PATH}" | sed 's:/*$::')_BAD
10+
11+
$CLICKHOUSE_CLIENT --query "CREATE TABLE t (a UInt64) ENGINE=File('CSV', '${BAD_PATH_PARENT}') -- { serverError DATABASE_ACCESS_DENIED }"
12+
$CLICKHOUSE_CLIENT --query "CREATE TABLE t (a UInt64) ENGINE=File('CSV', '${BAD_PATH_EXTRA}') -- { serverError DATABASE_ACCESS_DENIED }"

0 commit comments

Comments
 (0)