@@ -159,30 +159,32 @@ Red::DynArray<Red::Handle<AsyncFile>> FileSystemStorage::get_async_files() {
159159
160160std::filesystem::path FileSystemStorage::restrict_path (
161161 const std::string& p_path, std::error_code& p_error) const {
162- std::filesystem::path path = storage_path / p_path;
162+ std::filesystem::path file_path = storage_path / p_path;
163163
164164 // NOTE: See issue regarding usage of `std::weakly_canonical` with MO2:
165165 // https://github.com/ModOrganizer2/modorganizer/issues/2039
166166 if (FileSystem::is_mo2_detected ()) {
167167 if (p_path.find (' /' ) != std::string::npos ||
168168 p_path.find (' \\ ' ) != std::string::npos) {
169+ FileSystem::debug (" Unsafe path detected: \" {}\" " , file_path.string ().c_str ());
169170 p_error = std::make_error_code (std::errc::permission_denied);
170171 }
171- return path ;
172+ return file_path ;
172173 }
173174
174- const std::filesystem::path real_path = std::filesystem::weakly_canonical (path , p_error);
175+ const std::filesystem::path absolute_path = std::filesystem::weakly_canonical (file_path , p_error);
175176 if (p_error) {
176177 FileSystem::debug (" Failed to get canonical path for \" {}\" : {}" , p_path.c_str (), p_error.message ().c_str ());
177- return real_path ;
178+ return absolute_path ;
178179 }
179180
180- if (real_path.string ().find (storage_path.string () + " \\ " ) != 0 ) {
181- FileSystem::debug (" Accessing: \" {}\" " , p_path.c_str ());
182- FileSystem::debug (" Resolving: \" {}\" " , real_path.string ().c_str ());
181+ const auto path = to_lower (absolute_path.string ());
182+ const auto root_path = to_lower (storage_path.string ());
183+ if (path.find (root_path + " \\ " ) != 0 && path.find (root_path + ' /' ) != 0 ) {
184+ FileSystem::debug (" Unsafe path detected: \" {}\" " , absolute_path.string ().c_str ());
183185 p_error = std::make_error_code (std::errc::permission_denied);
184186 }
185- return real_path ;
187+ return absolute_path ;
186188}
187189
188190SharedMutex FileSystemStorage::get_mutex (const std::filesystem::path& p_path) {
0 commit comments