diff --git a/README_zh.md b/README_zh.md index 3377dd09b57744f987799a16e02c9a2d63f0b445..8bf8ddceede47bd112e83edcfb3fd01820e4cde2 100644 --- a/README_zh.md +++ b/README_zh.md @@ -41,7 +41,7 @@ [媒体库服务](https://gitee.com/openharmony/multimedia_medialibrary_standard) [存储管理服务](https://gitee.com/openharmony/filemanagement_storage_service) [文件访问接口](https://gitee.com/openharmony/filemanagement_file_api) -[多用户管理](http:////gitee.com/openharmony/account_os_account) +[多用户管理](https://gitee.com/openharmony/account_os_account) [**公共文件访问框架**](https://gitee.com/openharmony/filemanagement_user_file_service) diff --git a/interfaces/inner_api/file_access/src/uri_ext.cpp b/interfaces/inner_api/file_access/src/uri_ext.cpp index f9994f23578a56abe6726b6cada9d243ca3815e6..a96dde768bb9bf6ada5efa3cce4923d9ebc74bfa 100644 --- a/interfaces/inner_api/file_access/src/uri_ext.cpp +++ b/interfaces/inner_api/file_access/src/uri_ext.cpp @@ -57,7 +57,7 @@ Urie::Urie(const std::string& uriString) : OHOS::Uri(uriString) if (!CheckScheme()) { uriString_ = EMPTY; - HILOG_IMPL(LOG_CORE, LOG_DEBUG, 0xD001305, "URIe", "URI Scheme wrong"); + HILOG_IMPL(LOG_CORE, LOG_DEBUG, 0xD00430A, "URIe", "URI Scheme wrong"); } } @@ -489,7 +489,7 @@ bool Urie::Marshalling(Parcel& parcel) const return parcel.WriteString16(Str8ToStr16(uriString_)); } - HILOG_IMPL(LOG_CORE, LOG_ERROR, 0xD001305, "URIe", "uriString_ is not ascii string"); + HILOG_IMPL(LOG_CORE, LOG_ERROR, 0xD00430A, "URIe", "uriString_ is not ascii string"); return false; } diff --git a/interfaces/kits/native/recent/recent_n_exporter.cpp b/interfaces/kits/native/recent/recent_n_exporter.cpp index 18ee5c835505d51fe89b1bef70fac627002c14b6..78952ec309637cc6f5a877c40e1e6b4a81ebbc43 100644 --- a/interfaces/kits/native/recent/recent_n_exporter.cpp +++ b/interfaces/kits/native/recent/recent_n_exporter.cpp @@ -23,6 +23,7 @@ #include "accesstoken_kit.h" #include "file_uri.h" #include "file_utils.h" +#include "file_uri_check.h" #include "hilog_wrapper.h" #include "ipc_skeleton.h" #include "tokenid_kit.h" @@ -94,6 +95,11 @@ napi_value RecentNExporter::AddRecentFile(napi_env env, napi_callback_info cbinf } auto [succ, uri, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); FileUri fileUri(string(uri.get())); + if (!IsFilePathValid(fileUri.ToString().c_str())) { + HILOG_ERROR ("uri is invalid"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } auto filePath = fileUri.GetRealPath(); struct stat statBuf; if (stat(filePath.c_str(), &statBuf) < 0) { @@ -142,6 +148,11 @@ napi_value RecentNExporter::RemoveRecentFile(napi_env env, napi_callback_info cb } auto [succ, uri, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); FileUri fileUri(string(uri.get())); + if (!IsFilePathValid(fileUri.ToString().c_str())) { + HILOG_ERROR ("uri is invalid"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } auto filePath = fileUri.GetPath(); struct stat statBuf; if (stat(filePath.c_str(), &statBuf) < 0) { diff --git a/interfaces/kits/native/trash/src/file_trash_n_exporter.cpp b/interfaces/kits/native/trash/src/file_trash_n_exporter.cpp index 6c58c19a08cd16289e2516532a138540636f464d..e31c2276b0b867c42a8cf806206a81b85b75600b 100644 --- a/interfaces/kits/native/trash/src/file_trash_n_exporter.cpp +++ b/interfaces/kits/native/trash/src/file_trash_n_exporter.cpp @@ -23,6 +23,7 @@ #include "file_info.h" #include "file_uri.h" #include "file_util.h" +#include "file_uri_check.h" #include "ipc_skeleton.h" #include "rust_file.h" #include "tokenid_kit.h" @@ -602,6 +603,11 @@ napi_value FileTrashNExporter::Recover(napi_env env, napi_callback_info info) return nullptr; } string uriStr = uriPtr.get(); + if (!IsFilePathValid(uriStr.c_str())) { + HILOG_ERROR ("uri is invalid"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } HILOG_DEBUG("Recover: uriPtr get end."); // 获取沙箱目录地址 @@ -635,6 +641,24 @@ napi_value FileTrashNExporter::Recover(napi_env env, napi_callback_info info) return RecoverDir(env, path); } +static string CheckPath(string uriStr) +{ + HILOG_INFO("CompletelyDelete: check path start"); + if (!IsFilePathValid(uriStr.c_str())) { + HILOG_ERROR ("uri is invalid"); + return ""; + } + // 获取沙箱目录地址 + AppFileService::ModuleFileUri::FileUri fileUri(uriStr); + string path = fileUri.GetPath(); + // 判断绝对路径 + if (!GetRealPath(path)) { + HILOG_ERROR("Recover: Invalid Path"); + return ""; + } + return path; +} + napi_value FileTrashNExporter::CompletelyDelete(napi_env env, napi_callback_info info) { if (!CheckSystemAppAndPermission(FILE_ACCESS_PERMISSION, env)) { @@ -659,14 +683,10 @@ napi_value FileTrashNExporter::CompletelyDelete(napi_env env, napi_callback_info string uriStr = uriPtr.get(); HILOG_DEBUG("Recover: uriPtr get end."); - - // 获取沙箱目录地址 - AppFileService::ModuleFileUri::FileUri fileUri(uriStr); - string path = fileUri.GetPath(); - // 判断绝对路径 - if (!GetRealPath(path)) { + string path = CheckPath(uriStr); + if (path.empty()) { + HILOG_ERROR("Invalid Path"); NError(EINVAL).ThrowErr(env); - HILOG_ERROR("Recover: Invalid Path"); return nullptr; } HILOG_DEBUG("Recover path is trash dir start."); diff --git a/utils/file_uri_check.h b/utils/file_uri_check.h new file mode 100644 index 0000000000000000000000000000000000000000..02e0b5c9c8b011cbba290b9e1f335599ee03447a --- /dev/null +++ b/utils/file_uri_check.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FILE_URI_CHECK_H +#define FILE_URI_CHECK_H + +#include "hilog_wrapper.h" + +namespace OHOS { +namespace FileAccessFwk { + +static const std::string PATH_INVALID_FLAG1 = "../"; +static const std::string PATH_INVALID_FLAG2 = "/.."; +static const uint32_t PATH_INVALID_FLAG_LEN = 3; +static const char FILE_SEPARATOR_CHAR = '/'; + +static bool IsFilePathValid(const std::string &filePath) +{ + size_t pos = filePath.find(PATH_INVALID_FLAG1); + while (pos != std::string::npos) { + if (pos == 0 || filePath[pos - 1] == FILE_SEPARATOR_CHAR) { + HILOG_ERROR("Relative path is not allowed, path contain ../"); + return false; + } + pos = filePath.find(PATH_INVALID_FLAG1, pos + PATH_INVALID_FLAG_LEN); + } + pos = filePath.rfind(PATH_INVALID_FLAG2); + if ((pos != std::string::npos) && (filePath.size() - pos == PATH_INVALID_FLAG_LEN)) { + HILOG_ERROR("Relative path is not allowed, path tail is /.."); + return false; + } + return true; +} +} // FileAccessFwk +} // OHOS +#endif // FILE_URI_CHECK_H