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..1082119315e395d1b2501cd93020c7f56273409c 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,23 @@ 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; + } + + const auto wantAgent = request->GetWantAgent(); + const auto removalWantAgent = request->GetRemovalWantAgent(); + const auto isLocalWantAgent = (wantAgent != nullptr && wantAgent->IsLocal()) || + (removalWantAgent != nullptr && removalWantAgent->IsLocal()); + if (isLocalWantAgent && !AccessTokenHelper::IsSystemApp()) { + ANSR_LOGE("ReminderRequest 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..552dc8f104fbe53ce36afe4ef379dbab2fe3ec06 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