Files
ability_ability_runtime/services/abilitymgr/include/utils/uri_utils.h
T
openharmony_ci 20a104f317 !18736 merge master0321 into master
feat: add URI permission verification mechanism for content URIs

Created-by: dsz2025
Commit-by: duansizhao
Merged-by: openharmony_ci
Description: **IssueNo**: #14797

**Description**:

Add URI permission verification mechanism for content URIs to enhance security and proper authorization checks.

- Add VerifyUriPermission interface in IAbilityManagerCollaborator
- Add VerifyContentUriPermission in UriPermissionManagerStubImpl
- Mark content URIs as authorized for broker calls
- Add ContentUriPermission verification in CheckProxyUriPermission
- Update mock classes for unit testing

**稳定性自检:**
| 自检项 | 自检结果 |
| ------------------------------------------------------------ | -------- |
| 涉及跨进程调用的相关操作需要抛至主线程或加锁防止并发 | 待确认 | 修改了敏感文件: *proxy.cpp, *stub.cpp
| 成员变量进行赋值或创建需要排查并发 | 不涉及 |
| 谨慎在lambda表达式中使用引用捕获 | 待确认 | 修改了敏感文件: *.cpp
| 谨慎在未经拷贝的情况下使用外部传入的string、C字符串 | 待确认 | 修改了敏感文件: *.cpp, *.h
| map\vector\list\set等stl模板类使用时需要排查并发 | 待确认 | 修改了敏感文件: *.cpp, *.h
| 谨慎考虑加锁范围 | 待确认 | 修改了敏感文件: *.cpp
| 在IPC通信中谨慎使用同步通信方式 | 不涉及 |
| 禁止传递this指针至其他模块或线程(特别是eventhandler任务) | 不涉及 |
| 禁止将外部传入的裸指针在内部直接构造智能指针 | 不涉及 |
| 禁止多个独立创建的智能指针管理同一地址 | 不涉及 |
| 禁止在析构函数中抛异步任务 | 不涉及 |
| 禁止js对象在非js线程(例如在IPC线程)创建、使用或销毁 | 不涉及 |
| 禁止在对外接口中未经判空直接使用外部传入的指针 | 不涉及 |
| 禁止接口返回局部变量引用 | 不涉及 |
| 禁止在信号函数中加锁 | 待确认 | 修改了敏感文件: *.cpp
| 禁止在关键流程(SA启动、应用启动等主流程)执行耗时的操作 | 不涉及 |
| 禁止将同一个cpp编译在不同的so中 | 不涉及 |

**安全编码自检:**
| 自检项 | 自检结果 |
| -------------------------------------------------------------- | -------- |
| 裸指针避免通过隐式转换构造为sptr | 待确认 | 修改了敏感文件: *.cpp
| json对象在取值之前必须先判断类型,避免类型不匹配 | 待确认 | 修改了敏感文件: *.cpp
| 序列化时必须对传入的数组大小进行校验,避免出现超大数组 | 待确认 | 修改了敏感文件: *.cpp
| 避免使用未明确位宽的整型,选择使用int8_t、uint8_t等类型 | 不涉及 |
| 外部传入的路径要做规范化校验,对路径中的.、..、../等特殊字符严格校验 | 待确认 | 修改了敏感文件: *.cpp
| 指针变量、表示资源描述符的变量、bool变量必须赋初值 | 不涉及 |
| readParcelable获取的对象使用前需要判空 | 待确认 | 修改了敏感文件: *.cpp
| 分配和释放内存的函数需要成对出现 | 不涉及 |
| 申请内存后异常退出前需要及时进行内存释放 | 不涉及 |
| 内存申请前必须对内存大小进行合法性校验 | 不涉及 |
| 内存分配后必须判断是否成功 | 不涉及 |
| 禁止使用realloc、alloca函数 | 不涉及 |
| 禁止打印文件路径、口令等敏感信息,如有需要,使用private修饰 | 待确认 | 修改了敏感文件: *.cpp
| 禁止打印内存地址 | 待确认 | 修改了敏感文件: *.cpp
| 整数之间运算时必须严格检查,确保不会出现溢出、反转、除0 | 不涉及 |
| 禁止对有符号整数进行位操作符运算 | 待确认 | 修改了敏感文件: *.cpp
| 禁止对指针进行逻辑或位运算 | 不涉及 |
| 循环次数如果收外部数据控制,需要检验其合法性 | 不涉及 |
| 禁止使用内存操作类危险函数,需要使用安全函数 | 不涉及 |
| 谨慎使用不可重入函数 | 不涉及 |
| 必须检查安全函数的返回值,并进行正确处理 | 不涉及 |
| 禁止仅通过TokenType类型判断绕过权限校验 | 不涉及 |

**TDD Result**:
待确认

**XTS Result**:
待确认

### 是否已执行L0用例
- [ ] 待确认


See merge request: openharmony/ability_ability_runtime!18736
2026-03-27 15:00:36 +08:00

279 lines
11 KiB
C++

/*
* Copyright (c) 2024-2026 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 OHOS_ABILITY_RUNTIME_URI_UTILS_H
#define OHOS_ABILITY_RUNTIME_URI_UTILS_H
#include <string>
#include <vector>
#include "ability_record.h"
#include "check_result.h"
#include "nocopyable.h"
#include "uri.h"
#include "want.h"
namespace OHOS {
namespace AAFwk {
struct GrantUriPermissionInfo {
std::string targetBundleName;
std::string targetAbilityName = "";
uint32_t callerTokenId = 0;
uint32_t flag = 0;
int32_t appIndex = 0;
int32_t userId = -1;
int32_t targetUid = -1;
int32_t callerUid = -1;
int32_t collaboratorType = 0;
bool isSandboxApp = false;
bool isNotifyCollaborator = false;
};
class UriUtils {
public:
static UriUtils &GetInstance();
/**
* @brief Get list of URIs with granted permissions
* @param uriVec Input vector of URI strings
* @param checkResults Vector indicating permission check results
* @param callerTokenId Token ID of the caller
* @param targetBundleName Bundle name of the target application
* @param want The Want object containing URI information
* @return Vector of Uri objects with granted permissions
*/
std::vector<Uri> GetPermissionedUriList(const std::vector<std::string> &uriVec,
const std::vector<CheckResult> &checkResults, uint32_t callerTokenId,
const std::string &targetBundleName, Want &want);
/**
* @brief Extract URI list from Want object
* @param want The Want object containing URI information
* @param uriVec Output vector to store extracted URIs
* @return true if URIs were successfully extracted, false otherwise
*/
bool GetUriListFromWant(Want &want, std::vector<std::string> &uriVec);
#ifdef SUPPORT_UPMS
/**
* @brief Check if URI permission flag is set in Want
* @param flag The flag for grant uri permission
* @return true if URI permission flag is set, false otherwise
*/
bool IsGrantUriPermissionFlag(uint32_t flag);
#endif // SUPPORT_UPMS
/**
* @brief Check if extension type is service extension
* @param extensionAbilityType The extension ability type to check
* @return true if the type is service extension, false otherwise
*/
bool IsServiceExtensionType(AppExecFwk::ExtensionAbilityType extensionAbilityType);
#ifdef SUPPORT_UPMS
/**
* @brief Grant URI permission
* @param want The Want object containing URI information
* @param callerTokenId Token ID of the caller
* @param targetBundleName Bundle name of the target application
* @param appIndex Application index
*/
bool GrantDmsUriPermission(Want &want, uint32_t callerTokenId, std::string targetBundleName, int32_t appIndex);
/**
* @brief Grant URI permission for service extension ability
* @param abilityRequest The ability request containing URI information
* @return true if permission was successfully granted, false otherwise
*/
bool GrantUriPermissionForServiceExtension(const AbilityRequest &abilityRequest);
/**
* @brief Grant URI permission for UI or service extension ability
* @param abilityRequest The ability request containing URI information
* @return true if permission was successfully granted, false otherwise
*/
bool GrantUriPermissionForUIOrServiceExtension(const AbilityRequest &abilityRequest);
/**
* @brief Grant URI permission with detailed parameters
* @param want The Want object containing URI information
* @param grantInfo The param for grant uri permission
*/
bool GrantUriPermission(Want &want, const GrantUriPermissionInfo &grantInfo);
/**
* @brief Check URI permission for the caller
* @param callerTokenId Token ID of the caller
* @param want The Want object containing URI information
*/
void CheckUriPermission(uint32_t callerTokenId, Want &want);
/**
* @brief Grant URI permission for a list of URIs
* @param uriVec Vector of URI strings
* @param flag Permission flag
* @param targetBundleName Bundle name of the target application
* @param appIndex Application index
* @param initiatorTokenId Token ID of the initiator
*/
void GrantUriPermission(const std::vector<std::string> &uriVec, int32_t flag,
const std::string &targetBundleName, int32_t appIndex, uint32_t initiatorTokenId);
#endif // SUPPORT_UPMS
private:
UriUtils();
~UriUtils();
#ifdef SUPPORT_UPMS
/**
* @brief Grant URI permission for shell process
* @param strUriVec Vector of URI strings
* @param flag Permission flag
* @param targetPkg Package name of the target
* @param appIndex Application index
* @return true if permission was successfully granted, false otherwise
*/
bool GrantShellUriPermission(const std::vector<std::string> &strUriVec, uint32_t flag,
const std::string &targetPkg, int32_t appIndex);
/**
* @brief Internal implementation of URI permission granting
* @param uriVec Vector of URI strings
* @param grantInfo The param for grant uri permission
* @param want Want object containing URI information
* @param isBrokerCall Whether the call is from broker
* @return true if permission was successfully granted, false otherwise
* @note When isBrokerCall is true, content URIs are automatically marked as authorized
*/
bool GrantUriPermissionInner(const std::vector<std::string> &uriVec, const GrantUriPermissionInfo &grantInfo,
Want &want, bool isBrokerCall);
#endif // SUPPORT_UPMS
/**
* @brief Check if the call is from DMS (Distributed Mission Service)
* @param fromTokenId Token ID of the caller
* @return true if the call is from DMS, false otherwise
*/
bool IsDmsCall(uint32_t fromTokenId);
/**
* @brief Check if the application is a sandbox application
* @param tokenId Token ID of the application
* @return true if the application is a sandbox app, false otherwise
*/
bool IsSandboxApp(uint32_t tokenId);
/**
* @brief Get URI list from Want for DMS
* @param want The Want object containing URI information
* @return Vector of Uri objects
*/
std::vector<Uri> GetUriListFromWantDms(Want &want);
/**
* @brief Publish file open event
* @param want The Want object containing file information
* @param grantInfo The param for grant uri permission
*/
void PublishFileOpenEvent(const Want &want, const GrantUriPermissionInfo &grantInfo);
/**
* @brief Check if bundle is in Anco app identifier
* @param bundleName Bundle name to check
* @return true if the bundle is in Anco app identifier, false otherwise
*/
bool IsInAncoAppIdentifier(const std::string &bundleName);
/**
* @brief Check if identifier matches bundle name in Anco context
* @param identifier Identifier to check
* @param bundleName Bundle name to match
* @return true if identifier matches bundle name, false otherwise
*/
bool CheckIsInAncoAppIdentifier(const std::string &identifier, const std::string &bundleName);
/**
* @brief Process UDMF key in Want object
* @param want The Want object to process
*/
void ProcessUDMFKey(Want &want);
/**
* @brief Process URI in Want object
* @param checkResult Result of permission check
* @param apiVersion API version of the caller
* @param want The Want object to process
* @param permissionedUris Output vector for processed URIs
* @return true if processing was successful, false otherwise
*/
bool ProcessWantUri(bool checkResult, int32_t apiVersion, Want &want, std::vector<Uri> &permissionedUris);
/**
* @brief Get caller name and API version
* @param tokenId Token ID of the caller
* @param callerName Output parameter for caller name
* @param apiVersion Output parameter for API version
* @return true if information was successfully retrieved, false otherwise
*/
bool GetCallerNameAndApiVersion(uint32_t tokenId, std::string &callerName, int32_t &apiVersion);
/**
* @brief Send URI permission grant event
* @param callerBundleName Bundle name of the caller
* @param targetBundleName Bundle name of the target
* @param oriUri Original URI string
* @param apiVersion API version of the caller
* @param eventType Type of the event
* @return true if event was successfully sent, false otherwise
*/
bool SendGrantUriPermissionEvent(const std::string &callerBundleName, const std::string &targetBundleName,
const std::string &oriUri, int32_t apiVersion, const std::string &eventType);
/**
* @brief Notify collaborator grant uri permission started.
* @param isNotifyCollaborator Need notify collaborator.
* @param uris The uri list to grant permission.
* @param flag Want::FLAG_AUTH_READ_URI_PERMISSION or Want::FLAG_AUTH_WRITE_URI_PERMISSION.
* @param userId The user id of target application.
* @return result of notify collaborator.
*/
bool NotifyGrantUriPermissionStart(bool isNotifyCollaborator, const std::vector<std::string> &uris,
uint32_t flag, int32_t userId);
/**
* @brief Notify collaborator grant uri Permission finished.
* @param isNotifyCollaborator Need notify collaborator.
* @param uris The uri list to grant permission.
* @param flag Want::FLAG_AUTH_READ_URI_PERMISSION or Want::FLAG_AUTH_WRITE_URI_PERMISSION.
* @param userId The user id of target application.
* @param checkResults The result of check uri permission.
* @return result of notify collaborator.
*/
bool NotifyGrantUriPermissionEnd(bool isNotifyCollaborator, const std::vector<std::string> &uris,
uint32_t flag, int32_t userId, const std::vector<bool> &checkResults);
/**
* @brief Mark content URIs as authorized for broker call
* @param uriVec Vector of URI strings
* @param checkResults Check results to update
* @param isBrokerCall Whether the call is from broker
*/
void MarkContentUriAuthorizedForBroker(const std::vector<std::string> &uriVec,
std::vector<CheckResult> &checkResults, bool isBrokerCall);
DISALLOW_COPY_AND_MOVE(UriUtils);
};
} // namespace AAFwk
} // namespace OHOS
#endif // OHOS_ABILITY_RUNTIME_URI_UTILS_H