Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes Brave Ads crash on iOS for ads with an invalid target URL #5614

Merged
merged 1 commit into from
May 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion vendor/bat-native-ads/src/bat/ads/bundle_state.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Result BundleState::FromJson(

info.title = creative["title"].GetString();
info.body = creative["body"].GetString();
info.target_url = GetUrlWithScheme(creative["targetUrl"].GetString());
info.target_url = creative["targetUrl"].GetString();
info.creative_instance_id = creative["creativeInstanceId"].GetString();

const std::string category = creative_ad_notification.name.GetString();
Expand Down
2 changes: 1 addition & 1 deletion vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1181,7 +1181,7 @@ bool AdsImpl::ShowAdNotification(
ad_notification->category = info.category;
ad_notification->title = info.title;
ad_notification->body = info.body;
ad_notification->target_url = GetUrlWithScheme(info.target_url);
ad_notification->target_url = info.target_url;
ad_notification->geo_target = info.geo_targets.at(0);

BLOG(1, "Ad notification shown:\n"
Expand Down
8 changes: 8 additions & 0 deletions vendor/bat-native-ads/src/bat/ads/internal/catalog_state.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#include "bat/ads/internal/catalog_state.h"
#include "bat/ads/internal/json_helper.h"
#include "bat/ads/internal/static_values.h"
#include "bat/ads/internal/logging.h"

#include "url/gurl.h"

namespace ads {

Expand Down Expand Up @@ -157,6 +160,11 @@ Result CatalogState::FromJson(
creative_info.payload.body = payload["body"].GetString();
creative_info.payload.title = payload["title"].GetString();
creative_info.payload.target_url = payload["targetUrl"].GetString();
if (!GURL(creative_info.payload.target_url).is_valid()) {
BLOG(1, "Invalid target URL for creative instance id "
<< creative_instance_id);
continue;
}

creative_set_info.creative_ad_notifications.push_back(creative_info);
} else {
Expand Down
13 changes: 0 additions & 13 deletions vendor/bat-native-ads/src/bat/ads/internal/url_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,6 @@

namespace ads {

std::string GetUrlWithScheme(
const std::string& url) {
if (!base::StartsWith(url, url::kHttpScheme,
base::CompareCase::INSENSITIVE_ASCII) &&
!base::StartsWith(url, url::kHttpsScheme,
base::CompareCase::INSENSITIVE_ASCII)) {
return base::StringPrintf("%s%s%s", url::kHttpsScheme,
url::kStandardSchemeSeparator, url.c_str());
}

return url;
}

bool UrlMatchesPattern(
const std::string& url,
const std::string& pattern) {
Expand Down
3 changes: 0 additions & 3 deletions vendor/bat-native-ads/src/bat/ads/internal/url_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@

namespace ads {

std::string GetUrlWithScheme(
const std::string& url);

bool UrlMatchesPattern(
const std::string& url,
const std::string& pattern);
Expand Down
39 changes: 0 additions & 39 deletions vendor/bat-native-ads/src/bat/ads/internal/url_util_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,45 +37,6 @@ class BraveAdsUrlUtilTest : public ::testing::Test {
// Objects declared here can be used by all tests in the test case
};

TEST_F(BraveAdsUrlUtilTest,
GetUrlForUrlMissingScheme) {
// Arrange
const std::string url = "www.foobar.com";

// Act
const std::string url_with_schema = GetUrlWithScheme(url);

// Assert
const std::string expected_url_with_scheme = "https://www.foobar.com";
EXPECT_EQ(expected_url_with_scheme, url_with_schema);
}

TEST_F(BraveAdsUrlUtilTest,
GetUrlForUrlWithHttpScheme) {
// Arrange
const std::string url = "http://www.foobar.com";

// Act
const std::string url_with_schema = GetUrlWithScheme(url);

// Assert
const std::string expected_url_with_scheme = "http://www.foobar.com";
EXPECT_EQ(expected_url_with_scheme, url_with_schema);
}

TEST_F(BraveAdsUrlUtilTest,
GetUrlForUrlWithHttpsScheme) {
// Arrange
const std::string url = "https://www.foobar.com";

// Act
const std::string url_with_schema = GetUrlWithScheme(url);

// Assert
const std::string expected_url_with_scheme = "https://www.foobar.com";
EXPECT_EQ(expected_url_with_scheme, url_with_schema);
}

TEST_F(BraveAdsUrlUtilTest,
UrlMatchesPatternWithNoWildcards) {
// Arrange
Expand Down
4 changes: 2 additions & 2 deletions vendor/brave-ios/Ads/BATAdNotification.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ NS_SWIFT_NAME(AdsNotification)
@property (nonatomic, readonly, copy) NSString *category;
@property (nonatomic, readonly, copy) NSString *title;
@property (nonatomic, readonly, copy) NSString *body;
@property (nonatomic, readonly, copy) NSURL *targetURL;
@property (nonatomic, readonly, copy) NSString *targetURL;
@property (nonatomic, readonly, copy) NSString *geoTarget;
@end

@interface BATAdNotification (MyFirstAd)
+ (instancetype)customAdWithTitle:(NSString *)title
body:(NSString *)body
url:(NSURL *)url NS_SWIFT_NAME(customAd(title:body:url:));
url:(NSString *)url NS_SWIFT_NAME(customAd(title:body:url:));
@end

NS_ASSUME_NONNULL_END
6 changes: 3 additions & 3 deletions vendor/brave-ios/Ads/BATAdNotification.mm
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ @interface BATAdNotification ()
@property (nonatomic, copy) NSString *category;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *body;
@property (nonatomic, copy) NSURL *targetURL;
@property (nonatomic, copy) NSString *targetURL;
@property (nonatomic, copy) NSString *geoTarget;
@end

Expand All @@ -30,7 +30,7 @@ - (instancetype)initWithNotificationInfo:(const ads::AdNotificationInfo&)info
self.category = [NSString stringWithUTF8String:info.category.c_str()];
self.title = [NSString stringWithUTF8String:info.title.c_str()];
self.body = [NSString stringWithUTF8String:info.body.c_str()];
self.targetURL = [NSURL URLWithString:[NSString stringWithUTF8String:info.target_url.c_str()]];
self.targetURL = [NSString stringWithUTF8String:info.target_url.c_str()];
self.geoTarget = [NSString stringWithUTF8String:info.geo_target.c_str()];
}
return self;
Expand All @@ -39,7 +39,7 @@ - (instancetype)initWithNotificationInfo:(const ads::AdNotificationInfo&)info
@end

@implementation BATAdNotification (MyFirstAd)
+ (instancetype)customAdWithTitle:(NSString *)title body:(NSString *)body url:(NSURL *)url
+ (instancetype)customAdWithTitle:(NSString *)title body:(NSString *)body url:(NSString *)url
{
BATAdNotification *notification = [[BATAdNotification alloc] init];
notification.title = title;
Expand Down