Skip to content

Commit

Permalink
Adds check for public key beaing empty for promotions
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed May 10, 2020
1 parent 433fd6c commit c411ce4
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 23 deletions.
27 changes: 25 additions & 2 deletions components/brave_rewards/browser/rewards_service_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ namespace brave_test_resp {
std::string registrarVK_;
std::string verification_;
std::string promotions_;
std::string promotion_empty_key_;
std::string promotion_claim_;
std::string creds_tokens_;
std::string creds_tokens_prod_;
Expand Down Expand Up @@ -380,7 +381,11 @@ class BraveRewardsBrowserTest
}
} else if (URLMatches(url, "/promotions?", PREFIX_V1,
ServerTypes::kPromotion)) {
*response = brave_test_resp::promotions_;
if (promotion_empty_key_) {
*response = brave_test_resp::promotion_empty_key_;
} else {
*response = brave_test_resp::promotions_;
}
} else if (URLMatches(url, "/promotions/", PREFIX_V1,
ServerTypes::kPromotion)) {
if (url.find("claims") != std::string::npos) {
Expand Down Expand Up @@ -679,6 +684,10 @@ class BraveRewardsBrowserTest
ASSERT_TRUE(base::ReadFileToString(path.AppendASCII("promotions_resp.json"),
&brave_test_resp::promotions_));

ASSERT_TRUE(base::ReadFileToString(
path.AppendASCII("promotion_empty_key_resp.json"),
&brave_test_resp::promotion_empty_key_));

ASSERT_TRUE(base::ReadFileToString(path.AppendASCII("captcha_resp.json"),
&brave_test_resp::captcha_));
ASSERT_TRUE(
Expand Down Expand Up @@ -1436,7 +1445,8 @@ class BraveRewardsBrowserTest
double reconciled_tip_total_ = 0;
double pending_balance_ = 0;
double external_balance_ = 0;
double verified_wallet_ = false;
bool verified_wallet_ = false;
bool promotion_empty_key_ = false;
const std::string external_wallet_address_ =
"abe5f454-fedd-4ea9-9203-470ae7315bb3";
};
Expand Down Expand Up @@ -2786,3 +2796,16 @@ IN_PROC_BROWSER_TEST_F(
"[color=contribute]",
"-50.0BAT");
}

IN_PROC_BROWSER_TEST_F(
BraveRewardsBrowserTest,
PromotionHasEmptyPublicKey) {
promotion_empty_key_ = true;
EnableRewards();

WaitForPromotionInitialization();
rewards_service_browsertest_utils::WaitForElementToAppear(
OpenRewardsPopup(),
"[data-test-id=notification-close]",
false);
}
Original file line number Diff line number Diff line change
Expand Up @@ -394,4 +394,6 @@ void IsMediaTipsInjected(content::WebContents* context, bool should_appear) {
WaitForElementToAppear(context, ".action-brave-tip", should_appear);
}



} // namespace rewards_service_browsertest_utils
16 changes: 16 additions & 0 deletions test/data/rewards-data/promotion_empty_key_resp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"promotions": [
{
"id": "6820f6a4-c6ef-481d-879c-d2c30c8928c3",
"createdAt": "2019-10-30T08:07:07.092836Z",
"expiresAt": "2025-02-29T08:07:07.092836Z",
"version": 5,
"suggestionsPerGrant": 120,
"approximateValue": "30.0",
"type": "ugp",
"available": true,
"platform": "",
"publicKeys": []
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ enum Result {
AC_TABLE_EMPTY = 14,
NOT_ENOUGH_FUNDS = 15,
TIP_ERROR = 16,
CORRUPTED_WALLET = 17,
CORRUPTED_DATA = 17,
GRANT_ALREADY_CLAIMED = 18,

CONTRIBUTION_AMOUNT_TOO_LOW = 19,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,34 @@ void CredentialsCommon::GetSignedCredsFromResponse(
return;
}

auto* signed_creds = parsed_response.FindListKey("signed_creds");
if (!signed_creds || signed_creds->GetList().empty()) {
BLOG(ledger_, ledger::LogLevel::LOG_ERROR) <<
"Failed to parse signed creds";
callback(ledger::Result::RETRY);
return;
}

auto* public_key = parsed_response.FindStringKey("public_key");
if (!public_key || public_key->empty()) {
BLOG(ledger_, ledger::LogLevel::LOG_ERROR) << "Public key is empty";
callback(ledger::Result::RETRY);
return;
}

auto* batch_proof = parsed_response.FindStringKey("batch_proof");
if (!batch_proof || batch_proof->empty()) {
BLOG(ledger_, ledger::LogLevel::LOG_ERROR) << "Batch proof is empty";
callback(ledger::Result::RETRY);
return;
}

auto creds_batch = ledger::CredsBatch::New();
creds_batch->trigger_id = trigger.id;
creds_batch->trigger_type = trigger.type;
base::JSONWriter::Write(
*parsed_response.FindListKey("signed_creds"),
&creds_batch->signed_creds);
creds_batch->public_key = *parsed_response.FindStringKey("public_key");
creds_batch->batch_proof = *parsed_response.FindStringKey("batch_proof");
base::JSONWriter::Write(*signed_creds, &creds_batch->signed_creds);
creds_batch->public_key = *public_key;
creds_batch->batch_proof = *batch_proof;

ledger_->SaveSignedCreds(std::move(creds_batch), callback);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ void LedgerImpl::OnPublisherStateLoaded(
}

if (GetPaymentId().empty() || GetWalletPassphrase().empty()) {
callback(ledger::Result::CORRUPTED_WALLET);
callback(ledger::Result::CORRUPTED_DATA);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ void Promotion::Fetch(ledger::FetchPromotionCallback callback) {
const std::string& passphrase = ledger_->GetWalletPassphrase();
if (wallet_payment_id.empty() || passphrase.empty()) {
ledger::PromotionList empty_list;
callback(ledger::Result::CORRUPTED_WALLET, std::move(empty_list));
callback(ledger::Result::CORRUPTED_DATA, std::move(empty_list));
ledger::WalletProperties properties;
ledger_->OnWalletProperties(ledger::Result::CORRUPTED_WALLET, properties);
ledger_->OnWalletProperties(ledger::Result::CORRUPTED_DATA, properties);
return;
}

Expand Down Expand Up @@ -182,9 +182,9 @@ void Promotion::OnGetAllPromotions(
HandleExpiredPromotions(ledger_, &promotions);

ledger::PromotionList list;
bool success = ParseFetchResponse(response, &list);
ledger::Result success = ParseFetchResponse(response, &list);

if (!success) {
if (success == ledger::Result::LEDGER_ERROR) {
BLOG(ledger_, ledger::LogLevel::LOG_ERROR) << "Failed to parse promotions";
ProcessFetchedPromotions(
ledger::Result::LEDGER_ERROR,
Expand All @@ -193,6 +193,13 @@ void Promotion::OnGetAllPromotions(
return;
}

// even though that some promotions are corrupted
// we should display non corrupted ones either way
if (success == ledger::Result::CORRUPTED_DATA) {
BLOG(ledger_, ledger::LogLevel::LOG_ERROR) <<
"Some promotions are not formatted correctly";
}

for (auto & item : list) {
auto it = promotions.find(item->id);
if (it != promotions.end() &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,21 @@ ledger::ReportType ConvertPromotionTypeToReportType(
}
}

bool ParseFetchResponse(
ledger::Result ParseFetchResponse(
const std::string& response,
ledger::PromotionList* list) {
if (!list) {
return false;
return ledger::Result::LEDGER_ERROR;
}

base::Optional<base::Value> value = base::JSONReader::Read(response);
if (!value || !value->is_dict()) {
return false;
return ledger::Result::LEDGER_ERROR;
}

base::DictionaryValue* dictionary = nullptr;
if (!value->GetAsDictionary(&dictionary)) {
return false;
return ledger::Result::LEDGER_ERROR;
}

auto* promotions = dictionary->FindListKey("promotions");
Expand Down Expand Up @@ -160,7 +160,7 @@ bool ParseFetchResponse(
}

auto* public_keys = item.FindListKey("publicKeys");
if (!public_keys) {
if (!public_keys || public_keys->GetList().empty()) {
continue;
}

Expand All @@ -175,11 +175,11 @@ bool ParseFetchResponse(
}

if (promotion_size != list->size()) {
return false;
return ledger::Result::CORRUPTED_DATA;
}
}

return true;
return ledger::Result::LEDGER_OK;
}

std::vector<ledger::PromotionType> GetEligiblePromotions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ ledger::PromotionType ConvertStringToPromotionType(const std::string& type);
ledger::ReportType ConvertPromotionTypeToReportType(
const ledger::PromotionType type);

bool ParseFetchResponse(
ledger::Result ParseFetchResponse(
const std::string& response,
ledger::PromotionList* list);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void Wallet::GetWalletProperties(

if (payment_id.empty() || passphrase.empty()) {
ledger::WalletProperties properties;
callback(ledger::Result::CORRUPTED_WALLET,
callback(ledger::Result::CORRUPTED_DATA,
WalletPropertiesToWalletInfo(properties));
return;
}
Expand Down Expand Up @@ -469,7 +469,7 @@ void Wallet::GetAnonWalletStatus(ledger::ResultCallback callback) {
}

if (payment_id.empty() || passphrase.empty()) {
callback(ledger::Result::CORRUPTED_WALLET);
callback(ledger::Result::CORRUPTED_DATA);
return;
}

Expand Down

0 comments on commit c411ce4

Please sign in to comment.