diff --git a/frameworks/js/napi/src/common_convert_request.cpp b/frameworks/js/napi/src/common_convert_request.cpp index 75905fe085ab5b171d7643fe628fbcfb97df822f..26a06dba431e6a427f18c1f397946b801e4653c5 100644 --- a/frameworks/js/napi/src/common_convert_request.cpp +++ b/frameworks/js/napi/src/common_convert_request.cpp @@ -1266,9 +1266,16 @@ napi_value Common::GetNotificationRemovalWantAgent( } std::shared_ptr removeWantAgent = std::make_shared(*wantAgent); - if ((uint32_t)removeWantAgent->GetPendingWant()->GetType( - removeWantAgent->GetPendingWant()->GetTarget()) >= OPERATION_MAX_TYPE) { - request.SetRemovalWantAgent(removeWantAgent); + if (removeWantAgent->IsLocal()) { + const auto localPendingWant = removeWantAgent->GetLocalPendingWant(); + if (localPendingWant != nullptr && localPendingWant->GetType() >= OPERATION_MAX_TYPE) { + request.SetRemovalWantAgent(removeWantAgent); + } + } else { + if ((uint32_t)removeWantAgent->GetPendingWant()->GetType( + removeWantAgent->GetPendingWant()->GetTarget()) >= OPERATION_MAX_TYPE) { + request.SetRemovalWantAgent(removeWantAgent); + } } } diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index 1263310fce9122a300d1d17946b76b2124c1e571..dcd72551009d1133e9938937aa813640219517cd 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -1671,6 +1671,8 @@ private: void SetChainIdToExtraInfo(const sptr &request, OHOS::HiviewDFX::HiTraceId traceId); + ErrCode CheckNotificationRequest(const sptr &request); + private: static sptr instance_; static std::mutex instanceMutex_; diff --git a/services/ans/src/advanced_notification_manager/advanced_notification_publish.cpp b/services/ans/src/advanced_notification_manager/advanced_notification_publish.cpp index 6ae602d310ef0d88137f63bb051c557112b4e306..12eb14237473cacab061e688ae2f97f81d3b73e4 100644 --- a/services/ans/src/advanced_notification_manager/advanced_notification_publish.cpp +++ b/services/ans/src/advanced_notification_manager/advanced_notification_publish.cpp @@ -52,9 +52,9 @@ ErrCode AdvancedNotificationService::Publish(const std::string &label, const spt OHOS::HiviewDFX::HiTraceId traceId = OHOS::HiviewDFX::HiTraceChain::GetId(); ANS_LOGD("%{public}s", __FUNCTION__); - if (!request) { - ANSR_LOGE("ReminderRequest object is nullptr"); - return ERR_ANS_INVALID_PARAM; + const auto checkResult = CheckNotificationRequest(request); + if (checkResult != ERR_OK) { + return checkResult; } SetChainIdToExtraInfo(request, traceId); @@ -374,5 +374,29 @@ ErrCode AdvancedNotificationService::UpdateNotificationTimerByUid(const int32_t return ERR_OK; } +ErrCode AdvancedNotificationService::CheckNotificationRequest(const sptr &request) +{ + if (!request) { + ANSR_LOGE("ReminderRequest object is nullptr"); + return ERR_ANS_INVALID_PARAM; + } + auto tokenCaller = IPCSkeleton::GetCallingTokenID(); + bool isSystemApp = AccessTokenHelper::IsSystemApp(); + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(tokenCaller); + bool isAgentController = AccessTokenHelper::VerifyCallerPermission(tokenCaller, + OHOS_PERMISSION_NOTIFICATION_AGENT_CONTROLLER); + + const auto wantAgent = request->GetWantAgent(); + const auto removalWantAgent = request->GetRemovalWantAgent(); + const auto isLocalWantAgent = (wantAgent != nullptr && wantAgent->IsLocal()) || + (removalWantAgent != nullptr && removalWantAgent->IsLocal()); + bool isSpecifiedAccess = (isSystemApp || isSubsystem) && isAgentController; + if (isLocalWantAgent && !isSpecifiedAccess) { + ANSR_LOGE("Local wantAgent does not support non system app"); + return ERR_ANS_NON_SYSTEM_APP; + } + return ERR_OK; +} + } // Notification } // OHOS \ No newline at end of file diff --git a/services/ans/test/unittest/advanced_notification_service_test/advanced_notification_service_publish_test.cpp b/services/ans/test/unittest/advanced_notification_service_test/advanced_notification_service_publish_test.cpp index 80701d5e42ec52021e09c05af29b1f79b9120e0b..e8268fc7f2160cb774ca7c103ebb42a9427f3f5b 100644 --- a/services/ans/test/unittest/advanced_notification_service_test/advanced_notification_service_publish_test.cpp +++ b/services/ans/test/unittest/advanced_notification_service_test/advanced_notification_service_publish_test.cpp @@ -582,6 +582,28 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_01300, SleepForFC(); } +/** + * @tc.number : ANS_Publish_01400 + * @tc.name : ANSPublish01400 + * @tc.desc : When an non system app wants to publish an notification with local wantAgent, the + * notification publishing interface returns ERR_ANS_NON_SYSTEM_APP; + */ +HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_01400, Function | SmallTest | Level1) +{ + TestAddSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION); + sptr req = new NotificationRequest(); + EXPECT_NE(req, nullptr); + std::shared_ptr want = std::make_shared(); + std::shared_ptr localPendingWant = + std::make_shared("TestBundleName", want, 0); + std::shared_ptr wantAgent = + std::make_shared(localPendingWant); + req->SetWantAgent(wantAgent); + MockIsSystemApp(false); + std::string label = "publish's label"; + ASSERT_EQ(advancedNotificationService_->Publish(label, req), (int)ERR_ANS_NON_SYSTEM_APP); + SleepForFC(); +} /** * @tc.number : AdvancedNotificationServiceTest_04600 @@ -1739,5 +1761,95 @@ HWTEST_F(AdvancedNotificationServiceTest, OnReceiveEvent_1300, Function | SmallT advancedNotificationService.systemEventObserver_->OnReceiveEvent(data); ASSERT_EQ(advancedNotificationService.localScreenOn_, false); } + +/** + * @tc.number : CheckNotificationRequest_0100 + * @tc.name : CheckNotificationRequest_0100 + * @tc.desc : Test CheckNotificationRequest method when request is null + * @tc.require : I5TIQR + */ +HWTEST_F(AdvancedNotificationServiceTest, CheckNotificationRequest_0100, Level1) +{ + AdvancedNotificationService advancedNotificationService; + + ASSERT_EQ(advancedNotificationService.CheckNotificationRequest(nullptr), ERR_ANS_INVALID_PARAM); +} + +/** + * @tc.number : CheckNotificationRequest_0200 + * @tc.name : CheckNotificationRequest_0200 + * @tc.desc : Test CheckNotificationRequest method when request has no wantAgent + * @tc.require : I5TIQR + */ +HWTEST_F(AdvancedNotificationServiceTest, CheckNotificationRequest_0200, Level1) +{ + AdvancedNotificationService advancedNotificationService; + auto request = new NotificationRequest(); + + ASSERT_EQ(advancedNotificationService.CheckNotificationRequest(request), ERR_OK); +} + +/** + * @tc.number : CheckNotificationRequest_0300 + * @tc.name : CheckNotificationRequest_0300 + * @tc.desc : Test CheckNotificationRequest method when request has no wantAgent + * @tc.require : I5TIQR + */ +HWTEST_F(AdvancedNotificationServiceTest, CheckNotificationRequest_0300, Level1) +{ + AdvancedNotificationService advancedNotificationService; + auto request = new NotificationRequest(); + std::shared_ptr want = std::make_shared(); + std::shared_ptr localPendingWant = + std::make_shared("TestBundleName", want, 0); + std::shared_ptr wantAgent = + std::make_shared(localPendingWant); + request->SetWantAgent(wantAgent); + MockSystemApp(); + + ASSERT_EQ(advancedNotificationService.CheckNotificationRequest(request), ERR_OK); +} + +/** + * @tc.number : CheckNotificationRequest_0300 + * @tc.name : CheckNotificationRequest_0300 + * @tc.desc : Test CheckNotificationRequest method when request has no wantAgent + * @tc.require : I5TIQR + */ +HWTEST_F(AdvancedNotificationServiceTest, CheckNotificationRequest_0400, Level1) +{ + AdvancedNotificationService advancedNotificationService; + auto request = new NotificationRequest(); + std::shared_ptr want = std::make_shared(); + std::shared_ptr localPendingWant = + std::make_shared("TestBundleName", want, 0); + std::shared_ptr wantAgent = + std::make_shared(localPendingWant); + request->SetRemovalWantAgent(wantAgent); + MockSystemApp(); + + ASSERT_EQ(advancedNotificationService.CheckNotificationRequest(request), ERR_OK); +} + +/** + * @tc.number : CheckNotificationRequest_0300 + * @tc.name : CheckNotificationRequest_0300 + * @tc.desc : Test CheckNotificationRequest method when request has no wantAgent + * @tc.require : I5TIQR + */ +HWTEST_F(AdvancedNotificationServiceTest, CheckNotificationRequest_0500, Level1) +{ + AdvancedNotificationService advancedNotificationService; + auto request = new NotificationRequest(); + std::shared_ptr want = std::make_shared(); + std::shared_ptr localPendingWant = + std::make_shared("TestBundleName", want, 0); + std::shared_ptr wantAgent = + std::make_shared(localPendingWant); + request->SetRemovalWantAgent(wantAgent); + MockIsSystemApp(false); + + ASSERT_EQ(advancedNotificationService.CheckNotificationRequest(request), ERR_OK); +} } // namespace Notification } // namespace OHOS