diff --git a/native/cocos/platform/apple/CCFileUtils-apple.mm b/native/cocos/platform/apple/CCFileUtils-apple.mm index 39f5c2e859..73d5d3bd73 100644 --- a/native/cocos/platform/apple/CCFileUtils-apple.mm +++ b/native/cocos/platform/apple/CCFileUtils-apple.mm @@ -314,9 +314,24 @@ std::string FileUtilsApple::getFullPathForDirectoryAndFilename(const std::string { if (directory[0] != '/') { + NSString* dirStr = [NSString stringWithUTF8String:directory.c_str()]; + // The following logic is used for remove the "../" in the directory + // Because method "pathForResource" will return nil if the directory contains "../". + auto theIdx = directory.find(".."); + if (theIdx != std::string::npos && theIdx > 0) { + NSArray* pathComps = [dirStr pathComponents]; + NSUInteger idx = [pathComps indexOfObject:@".."]; + while (idx != NSNotFound && idx > 0) { // if found ".." & it's not at the beginning of the string + [pathComps removeObjectAtIndex: idx]; // remove the item ".." + [pathComps removeObjectAtIndex: idx - 1]; // remove the item before ".." + idx = [pathComps indexOfObject:@".."]; // find ".." again + } + dirStr = [NSString pathWithComponents:pathComps]; + } + NSString* fullpath = [pimpl_->getBundle() pathForResource:[NSString stringWithUTF8String:filename.c_str()] ofType:nil - inDirectory:[NSString stringWithUTF8String:directory.c_str()]]; + inDirectory:dirStr]; if (fullpath != nil) { return [fullpath UTF8String]; }