!19446 merge refactor/cli into master

refactor: climgr code

Created-by: yangxuguang-huawei
Commit-by: yangxuguang-huawei
Merged-by: openharmony_ci
Description: **IssueNo**:
https://gitcode.com/openharmony/ability_ability_runtime/issues/15343

**Description**:

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

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

**TDD Result**:
已验证OK

**XTS Result**:
已验证OK

### 是否已执行L0用例
- [x] 已验证
- [ ] 不涉及。如不涉及,请写明理由


See merge request: openharmony/ability_ability_runtime!19446
This commit is contained in:
openharmony_ci
2026-05-27 15:06:25 +08:00
6 changed files with 60 additions and 8 deletions
@@ -67,6 +67,7 @@ void JsCliSessionEventCallbackImpl::FreeNativeReference(std::unique_ptr<NativeRe
uv_work_t *work = new (std::nothrow) uv_work_t;
if (work == nullptr) {
reference.reset();
return;
}
work->data = reinterpret_cast<void *>(reference.release());
@@ -140,4 +141,4 @@ void JsCliSessionEventCallbackImpl::OnToolEvent(const std::string &sessionId,
}
} // namespace CliTool
} // namespace OHOS
} // namespace OHOS
@@ -215,7 +215,7 @@ bool CliToolMGRClient::LoadCliToolMgrService()
return false;
}
sptr<CliMgrLoadCallback> loadCallback = new (std::nothrow) CliMgrLoadCallback();
sptr<CliMgrLoadCallback> loadCallback = sptr<CliMgrLoadCallback>::MakeSptr();
if (loadCallback == nullptr) {
TAG_LOGE(AAFwkTag::CLI_TOOL, "Create load callback failed");
return false;
@@ -206,11 +206,17 @@ void CliToolManagerService::Init()
EventDispatcher::GetInstance().DispatchInputReplyEvent(record->callerPid, record->callerUid, eventId,
result ? ERR_OK : ERR_CLI_SEND_MESSAGE);
});
ioMonitor_->SetSessionClosedCallback([this](const std::string &sessionId, bool isStdout) {
HandleOutputClosed(sessionId, isStdout);
ioMonitor_->SetSessionClosedCallback([](const std::string &sessionId, bool isStdout) {
auto service = CliToolManagerService::GetInstance();
if (service != nullptr) {
service->HandleOutputClosed(sessionId, isStdout);
}
});
ioMonitor_->SetSessionDrainedCallback([this](const std::string &sessionId) {
HandleOutputDrained(sessionId);
ioMonitor_->SetSessionDrainedCallback([](const std::string &sessionId) {
auto service = CliToolManagerService::GetInstance();
if (service != nullptr) {
service->HandleOutputDrained(sessionId);
}
});
ioMonitor_->Start();
}
@@ -64,7 +64,10 @@ bool IOMonitor::Start()
TAG_LOGE(AAFwkTag::CLI_TOOL, "epoll_create1 failed: %{public}s", strerror(errno));
return false;
}
monitorThread_ = std::thread(&IOMonitor::MonitorLoop, this);
auto monitor = shared_from_this();
monitorThread_ = std::thread([monitor]() {
monitor->MonitorLoop();
});
return true;
}
@@ -132,6 +132,10 @@ int32_t ToolUtil::ValidateInputSchemaProperties(const std::string &inputSchema,
// Validate type if specified in schema
auto &propertySchema = properties[key];
if (propertySchema.contains("type")) {
if (!propertySchema["type"].is_string()) {
TAG_LOGE(AAFwkTag::CLI_TOOL, "args key '%{public}s' has invalid schema type", key.c_str());
return ERR_INVALID_PARAM;
}
std::string expectedType = propertySchema["type"].get<std::string>();
if (!ValidateParamType(value, expectedType, propertySchema, key)) {
TAG_LOGE(AAFwkTag::CLI_TOOL, "args key '%{public}s' type mismatch, expected: %{public}s",
@@ -440,7 +444,14 @@ bool ToolUtil::ValidateArrayItems(sptr<AAFwk::IArray> arrayObj,
if (arrayObj->GetLength(arrayLength) != ERR_OK || arrayLength == 0) {
return true;
}
std::string itemType = itemsSchema.value("type", "");
if (!itemsSchema.contains("type")) {
return true;
}
if (!itemsSchema["type"].is_string()) {
TAG_LOGE(AAFwkTag::CLI_TOOL, "Array '%{public}s' has invalid item schema type", key.c_str());
return false;
}
std::string itemType = itemsSchema["type"].get<std::string>();
if (itemType.empty()) {
return true;
}
@@ -869,6 +869,20 @@ HWTEST_F(ToolUtilTest, ValidateInputSchemaProperties_TypeValidation_1100, TestSi
GTEST_LOG_(INFO) << "ToolUtil_ValidateInputSchemaProperties_TypeValidation_1100 end";
}
/**
* @tc.name: ToolUtil_ValidateInputSchemaProperties_TypeValidation_1200
* @tc.desc: Test ValidateInputSchemaProperties rejects a non-string schema type
* @tc.type: FUNC
*/
HWTEST_F(ToolUtilTest, ValidateInputSchemaProperties_TypeValidation_1200, TestSize.Level1)
{
AAFwk::WantParams args;
args.SetParam("target", AAFwk::String::Box("device"));
EXPECT_EQ(ToolUtil::ValidateInputSchemaProperties(
R"({"properties":{"target":{"type":123}}})", args), ERR_INVALID_PARAM);
}
/**
* @tc.name: ToolUtil_ValidateInputSchemaProperties_NestedObject_0100
* @tc.desc: Test ValidateInputSchemaProperties with nested object validation
@@ -1120,6 +1134,23 @@ HWTEST_F(ToolUtilTest, ValidateInputSchemaProperties_ArrayItems_0300, TestSize.L
GTEST_LOG_(INFO) << "ToolUtil_ValidateInputSchemaProperties_ArrayItems_0300 end";
}
/**
* @tc.name: ToolUtil_ValidateInputSchemaProperties_ArrayItems_0400
* @tc.desc: Test array schema rejects a non-string item type
* @tc.type: FUNC
*/
HWTEST_F(ToolUtilTest, ValidateInputSchemaProperties_ArrayItems_0400, TestSize.Level1)
{
AAFwk::WantParams args;
sptr<AAFwk::IArray> array = sptr<AAFwk::Array>::MakeSptr(1, AAFwk::g_IID_IString);
ASSERT_NE(array, nullptr);
array->Set(0, AAFwk::String::Box("value").GetRefPtr());
args.SetParam("values", array);
EXPECT_EQ(ToolUtil::ValidateInputSchemaProperties(
R"({"properties":{"values":{"type":"array","items":{"type":123}}}})", args), ERR_INVALID_PARAM);
}
/**
* @tc.name: ToolUtil_ValidateInputSchemaProperties_1100
* @tc.desc: Test ValidateInputSchemaProperties with non-empty args and empty schema