Skip to content

Commit

Permalink
Validate conversions when loading from database (uplift to 1.59.x) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
aseren authored Sep 27, 2023
1 parent b96e31a commit fcd07f9
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 0 deletions.
2 changes: 2 additions & 0 deletions components/brave_ads/core/internal/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,8 @@ static_library("internal") {
"conversions/queue/queue_item/conversion_queue_item_util.cc",
"conversions/queue/queue_item/conversion_queue_item_util.h",
"conversions/queue/queue_item/conversion_queue_item_util_constants.h",
"conversions/queue/queue_item/conversion_queue_item_validation_util.cc",
"conversions/queue/queue_item/conversion_queue_item_validation_util.h",
"conversions/resource/conversion_resource.cc",
"conversions/resource/conversion_resource.h",
"conversions/resource/conversion_resource_constants.h",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <utility>
#include <vector>

#include "base/debug/crash_logging.h"
#include "base/debug/dump_without_crashing.h"
#include "base/functional/bind.h"
#include "base/strings/string_util.h"
#include "brave/components/brave_ads/core/internal/client/ads_client_helper.h"
Expand All @@ -20,6 +22,7 @@
#include "brave/components/brave_ads/core/internal/conversions/actions/conversion_action_types.h"
#include "brave/components/brave_ads/core/internal/conversions/actions/conversion_action_types_constants.h"
#include "brave/components/brave_ads/core/internal/conversions/actions/conversion_action_types_util.h"
#include "brave/components/brave_ads/core/internal/conversions/queue/queue_item/conversion_queue_item_validation_util.h"
#include "brave/components/brave_ads/core/internal/conversions/types/verifiable_conversion/verifiable_conversion_info.h"
#include "brave/components/brave_ads/core/mojom/brave_ads.mojom.h"

Expand Down Expand Up @@ -139,6 +142,16 @@ void GetCallback(GetConversionQueueCallback callback,
for (const auto& record : command_response->result->get_records()) {
const ConversionQueueItemInfo conversion_queue_item =
GetFromRecord(&*record);
// TODO(https://github.com/brave/brave-browser/issues/33239): Validate all
// Brave Ads data when loading from database
if (!conversion_queue_item.IsValid()) {
SCOPED_CRASH_KEY_STRING256(
"BraveAdsConversion", "invalidFieldsNames",
GetConversionQueueItemInvalidFieldsNames(conversion_queue_item));
base::debug::DumpWithoutCrashing();
continue;
}

conversion_queue_items.push_back(conversion_queue_item);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* Copyright (c) 2023 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

#include "brave/components/brave_ads/core/internal/conversions/queue/queue_item/conversion_queue_item_validation_util.h"

#include <vector>

#include "base/strings/string_util.h"
#include "brave/components/brave_ads/core/internal/conversions/conversion/conversion_info.h"
#include "brave/components/brave_ads/core/internal/conversions/queue/queue_item/conversion_queue_item_info.h"

namespace brave_ads {

namespace {

constexpr char kAdTypeFieldName[] = "ad_type";
constexpr char kCreativeInstanceIdFieldName[] = "creative_instance_id";
constexpr char kCreativeSetIdFieldName[] = "creative_set_id";
constexpr char kCampaignIdFieldName[] = "campaign_id";
constexpr char kAdvertiserIdFieldName[] = "advertiser_id";
constexpr char kActionTypeFieldName[] = "action_type";
constexpr char kProcessAtFieldName[] = "process_at";
constexpr char kSeparator[] = ",";

std::vector<std::string> GetInvalidFieldsNamesList(
const ConversionInfo& conversion_item) {
std::vector<std::string> invalid_fields;

if (conversion_item.ad_type == AdType::kUndefined) {
invalid_fields.emplace_back(kAdTypeFieldName);
}

if (conversion_item.creative_instance_id.empty()) {
invalid_fields.emplace_back(kCreativeInstanceIdFieldName);
}

if (conversion_item.creative_set_id.empty()) {
invalid_fields.emplace_back(kCreativeSetIdFieldName);
}

if (conversion_item.campaign_id.empty()) {
invalid_fields.emplace_back(kCampaignIdFieldName);
}

if (conversion_item.advertiser_id.empty()) {
invalid_fields.emplace_back(kAdvertiserIdFieldName);
}

if (conversion_item.action_type == ConversionActionType::kUndefined) {
invalid_fields.emplace_back(kActionTypeFieldName);
}

return invalid_fields;
}

std::vector<std::string> GetInvalidFieldsNamesList(
const ConversionQueueItemInfo& conversion_queue_item) {
std::vector<std::string> invalid_fields =
GetInvalidFieldsNamesList(conversion_queue_item.conversion);
if (conversion_queue_item.process_at.is_null()) {
invalid_fields.emplace_back(kProcessAtFieldName);
}
return invalid_fields;
}

} // namespace

std::string GetConversionQueueItemInvalidFieldsNames(
const ConversionQueueItemInfo& conversion_queue_item) {
return base::JoinString(GetInvalidFieldsNamesList(conversion_queue_item),
kSeparator);
}

} // namespace brave_ads
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* Copyright (c) 2023 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_COMPONENTS_BRAVE_ADS_CORE_INTERNAL_CONVERSIONS_QUEUE_QUEUE_ITEM_CONVERSION_QUEUE_ITEM_VALIDATION_UTIL_H_
#define BRAVE_COMPONENTS_BRAVE_ADS_CORE_INTERNAL_CONVERSIONS_QUEUE_QUEUE_ITEM_CONVERSION_QUEUE_ITEM_VALIDATION_UTIL_H_

#include <string>

namespace brave_ads {

struct ConversionQueueItemInfo;

std::string GetConversionQueueItemInvalidFieldsNames(
const ConversionQueueItemInfo& conversion_queue_item);

} // namespace brave_ads

#endif // BRAVE_COMPONENTS_BRAVE_ADS_CORE_INTERNAL_CONVERSIONS_QUEUE_QUEUE_ITEM_CONVERSION_QUEUE_ITEM_VALIDATION_UTIL_H_
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* Copyright (c) 2023 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

#include "brave/components/brave_ads/core/internal/conversions/queue/queue_item/conversion_queue_item_validation_util.h"

#include "brave/components/brave_ads/core/internal/ads/ad_unittest_util.h"
#include "brave/components/brave_ads/core/internal/common/unittest/unittest_time_util.h"
#include "brave/components/brave_ads/core/internal/conversions/conversion/conversion_builder.h"
#include "brave/components/brave_ads/core/internal/conversions/conversion/conversion_info.h"
#include "brave/components/brave_ads/core/internal/conversions/queue/queue_item/conversion_queue_item_unittest_util.h"
#include "brave/components/brave_ads/core/internal/conversions/types/verifiable_conversion/verifiable_conversion_unittest_constants.h"
#include "brave/components/brave_ads/core/internal/user_interaction/ad_events/ad_event_builder.h"
#include "brave/components/brave_ads/core/internal/user_interaction/ad_events/ad_event_info.h"
#include "brave/components/brave_ads/core/public/ad_info.h"
#include "brave/components/brave_ads/core/public/ad_type.h"
#include "testing/gtest/include/gtest/gtest.h"

// npm run test -- brave_unit_tests --filter=BraveAds*

namespace brave_ads {

TEST(BraveAdsValidationUtilTest, InvalidConversionQueueItem) {
// Arrange
const AdInfo ad = BuildAdForTesting(AdType::kNotificationAd,
/*should_use_random_uuids*/ true);
ConversionInfo conversion = BuildConversion(
BuildAdEvent(ad, ConfirmationType::kViewed, /*created_at*/ Now()),
VerifiableConversionInfo{kVerifiableConversionId,
kVerifiableConversionAdvertiserPublicKey});
conversion.ad_type = AdType::kUndefined;

ConversionQueueItemList conversion_queue_items =
BuildConversionQueueItemsForTesting(conversion, /*count*/ 1);
conversion_queue_items[0].process_at = base::Time();

// Act

// Assert
EXPECT_EQ("ad_type,process_at", GetConversionQueueItemInvalidFieldsNames(
conversion_queue_items[0]));
}

TEST(BraveAdsValidationUtilTest, ValidConversionQueueItem) {
// Arrange
const AdInfo ad = BuildAdForTesting(AdType::kNotificationAd,
/*should_use_random_uuids*/ true);
const ConversionInfo conversion = BuildConversion(
BuildAdEvent(ad, ConfirmationType::kViewed, /*created_at*/ Now()),
VerifiableConversionInfo{kVerifiableConversionId,
kVerifiableConversionAdvertiserPublicKey});
const ConversionQueueItemList conversion_queue_items =
BuildConversionQueueItemsForTesting(conversion, /*count*/ 1);

// Act

// Assert
EXPECT_EQ(
"", GetConversionQueueItemInvalidFieldsNames(conversion_queue_items[0]));
}

} // namespace brave_ads
1 change: 1 addition & 0 deletions components/brave_ads/core/test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ source_set("brave_ads_unit_tests") {
"//brave/components/brave_ads/core/internal/conversions/queue/queue_item/conversion_queue_item_unittest_util.cc",
"//brave/components/brave_ads/core/internal/conversions/queue/queue_item/conversion_queue_item_unittest_util.h",
"//brave/components/brave_ads/core/internal/conversions/queue/queue_item/conversion_queue_item_util_unittest.cc",
"//brave/components/brave_ads/core/internal/conversions/queue/queue_item/conversion_queue_item_validation_util_unittest.cc",
"//brave/components/brave_ads/core/internal/conversions/resource/conversion_resource_unittest.cc",
"//brave/components/brave_ads/core/internal/conversions/types/default_conversion/creative_set_conversion_url_pattern/creative_set_conversion_url_pattern_util_unittest.cc",
"//brave/components/brave_ads/core/internal/conversions/types/verifiable_conversion/envelope/verifiable_conversion_envelope_unittest_util.cc",
Expand Down

0 comments on commit fcd07f9

Please sign in to comment.