Skip to content

Commit 95d092e

Browse files
authored
fix: relax collection path restriction (#340)
1 parent 37cd226 commit 95d092e

5 files changed

Lines changed: 29 additions & 31 deletions

File tree

python/tests/detail/test_collection_create_and_open.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,11 @@ def check_collection_full(coll: Collection):
127127
valid_path_list = [
128128
"/tmp/nonexistent/directory/test_collection",
129129
"test/collection/with/slashes",
130+
"test/collection/with/slashes/哈哈",
130131
]
131132
invalid_path_list = [
132-
"invalid:path",
133+
"invalid\0path",
133134
"",
134-
"test_collection_with_spaces ",
135-
"test@#$%collection",
136135
]
137136

138137

src/db/collection.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,9 +1725,9 @@ Status CollectionImpl::create() {
17251725
if (path_.empty()) {
17261726
return Status::InvalidArgument("path validate failed: path is empty");
17271727
}
1728-
if (!std::regex_match(path_, COLLECTION_PATH_REGEX)) {
1728+
if (!FileHelper::PathSimpleValidation(path_)) {
17291729
return Status::InvalidArgument("path validate failed: path[", path_,
1730-
"] cannot pass the regex verification");
1730+
"] is not a valid path");
17311731
}
17321732
if (ailego::FileHelper::IsExist(path_.c_str())) {
17331733
return Status::InvalidArgument("path validate failed: path[", path_,

src/db/common/constants.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ const std::regex FIELD_NAME_REGEX("^[a-zA-Z0-9_-]{1,32}$");
4646

4747
const std::regex DOC_PK_REGEX("^[a-zA-Z0-9_!@#$%+=.-]{1,64}$");
4848

49-
const std::regex COLLECTION_PATH_REGEX(
50-
R"(^(?:[a-zA-Z]:)?[/\\]?(?:[a-zA-Z0-9_.\-]+[/\\])*[a-zA-Z0-9_.\-]+$)");
51-
5249
constexpr uint32_t kMaxDenseDimSize = 20000;
5350

5451
constexpr uint32_t kMaxScalarFieldSize = 1024;

src/db/common/file_helper.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,22 @@ class FileHelper {
231231
return ailego::FileHelper::FileSize(file_path.c_str());
232232
}
233233

234+
//! Perform a lightweight sanity check on the path string.
235+
//! This only catches obvious invalid input and does NOT guarantee the path
236+
//! is usable.
237+
static bool PathSimpleValidation(const std::string &path) {
238+
if (path.empty()) return false;
239+
240+
if (path.find('\0') != std::string::npos) return false;
241+
242+
#ifdef _WIN32
243+
// Characters forbidden in Windows path components.
244+
if (path.find_first_of("<>\"|?*") != std::string::npos) return false;
245+
#endif
246+
247+
return true;
248+
}
249+
234250
//! Copy file
235251
//! src_file_path and dst_file_path must be the full path
236252
//! dst_file_path/.. must exist

tests/db/collection_test.cc

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ TEST_F(CollectionTest, Feature_CreateAndOpen_PathValidate) {
224224
".hidden",
225225
"file.txt",
226226
"abs_test/nested/path",
227+
"abs test/nested/path",
227228
"nested/a/b/c",
228229
"_",
229230
"-",
@@ -242,30 +243,15 @@ TEST_F(CollectionTest, Feature_CreateAndOpen_PathValidate) {
242243
}
243244

244245
{
245-
std::vector<std::string> inalid_paths = {
246-
" ", "",
247-
"file name", // space
248-
"file$name", // $
249-
"a&b", // &
250-
"a|b", // |
251-
"a<b", // <
252-
"a>b", // >
253-
"a\"b", // "
254-
"a'b", // '
255-
"a;b", // ;
256-
"a?b", // ?
257-
"a*b", // *
258-
"a[b]", // []
259-
"a{b}", // {}
260-
"a~b", // ~
261-
"a#b", // #
262-
"a\tb", // tab
263-
"a\nb", // newline
264-
"a\rb", // carriage return
246+
using std::string_literals::operator""s;
247+
std::vector<std::string> invalid_paths = {
248+
"",
249+
"v\0v"s, // NUL
250+
#if _WIN32
251+
"v?v"s,
252+
#endif
265253
};
266-
for (auto path : inalid_paths) {
267-
ailego::FileHelper::RemoveDirectory(path.c_str());
268-
254+
for (auto path : invalid_paths) {
269255
auto result = Collection::CreateAndOpen(path, *schema, options);
270256
if (!result.has_value()) {
271257
std::cout << result.error().message() << std::endl;

0 commit comments

Comments
 (0)