Skip to content

Commit

Permalink
Fix anonymous-credentials calls after cxx api change, misc feedback c…
Browse files Browse the repository at this point in the history
…hanges
  • Loading branch information
DJAndries committed Jul 4, 2024
1 parent fad93bf commit 072fdfc
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 13 deletions.
37 changes: 29 additions & 8 deletions components/web_discovery/browser/credential_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,25 @@ std::optional<std::string> FinishJoin(
std::vector<const uint8_t> group_pub_key,
std::vector<const uint8_t> gsk,
std::vector<const uint8_t> join_resp_bytes) {
auto finish_res = anonymous_credential_manager->finish_join(
rust::Slice(group_pub_key.data(), group_pub_key.size()),
rust::Slice(gsk.data(), gsk.size()),
auto pub_key_result = anonymous_credentials::load_group_public_key(
rust::Slice(group_pub_key.data(), group_pub_key.size()));
auto gsk_result = anonymous_credentials::load_credential_big(
rust::Slice(gsk.data(), gsk.size()));
auto join_resp_result = anonymous_credentials::load_join_response(
rust::Slice(join_resp_bytes.data(), join_resp_bytes.size()));
if (!pub_key_result.error_message.empty() ||
!gsk_result.error_message.empty() ||
!join_resp_result.error_message.empty()) {
VLOG(1) << "Failed to finish credential join due to deserialization error "
"with group pub key, gsk, or join response: "
<< pub_key_result.error_message.c_str()
<< gsk_result.error_message.c_str()
<< join_resp_result.error_message.c_str();
return std::nullopt;
}
auto finish_res = anonymous_credential_manager->finish_join(
*pub_key_result.value, *gsk_result.value,
std::move(join_resp_result.value));
if (!finish_res.error_message.empty()) {
VLOG(1) << "Failed to finish credential join for " << date << ": "
<< finish_res.error_message.c_str();
Expand All @@ -106,16 +121,22 @@ std::optional<std::vector<const uint8_t>> PerformSign(
std::optional<std::vector<uint8_t>> gsk_bytes,
std::optional<std::vector<uint8_t>> credential_bytes) {
if (gsk_bytes && credential_bytes) {
auto set_res = anonymous_credential_manager->set_gsk_and_credentials(
auto gsk_result = anonymous_credentials::load_credential_big(
rust::Slice(reinterpret_cast<const uint8_t*>(gsk_bytes->data()),
gsk_bytes->size()),
gsk_bytes->size()));
auto credential_result = anonymous_credentials::load_user_credentials(
rust::Slice(reinterpret_cast<const uint8_t*>(credential_bytes->data()),
credential_bytes->size()));
if (!set_res.error_message.empty()) {
VLOG(1) << "Failed to sign due to credential set failure: "
<< set_res.error_message.c_str();
if (!gsk_result.error_message.empty() ||
!credential_result.error_message.empty()) {
VLOG(1) << "Failed to sign due to deserialization error with gsk, or "
"user credential: "
<< gsk_result.error_message.c_str()
<< credential_result.error_message.c_str();
return std::nullopt;
}
anonymous_credential_manager->set_gsk_and_credentials(
std::move(gsk_result.value), std::move(credential_result.value));
}
auto sig_res = anonymous_credential_manager->sign(
rust::Slice(msg.data(), msg.size()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use kuchikiki::{
traits::TendrilSink,
};

#[allow(unsafe_op_in_unsafe_fn)]
#[cxx::bridge(namespace = "rust_document_extractor")]
mod ffi {
pub struct SelectAttributeRequest {
Expand Down
2 changes: 1 addition & 1 deletion components/web_discovery/browser/double_fetcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ constexpr net::NetworkTrafficAnnotationTag kFetchNetworkTrafficAnnotation =
semantics {
sender: "Brave Web Discovery Double Fetch"
description:
"Retrieves a page of interest without session cookies for
"Retrieves a page of interest without cookies for
scraping and reporting via Web Discovery."
trigger:
"Requests are sent minutes after the original
Expand Down
6 changes: 6 additions & 0 deletions components/web_discovery/browser/pref_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@ namespace web_discovery {
// Profile prefs
inline constexpr char kWebDiscoveryNativeEnabled[] =
"brave.web_discovery.wdp_native_enabled";

// The following pref values are used for generating
// anonymous signatures for user submissions.
// Since they are not used for encrypting sensitive data,
// they do not require secure storage.
inline constexpr char kCredentialRSAPrivateKey[] =
"brave.web_discovery.rsa_priv_key";
inline constexpr char kCredentialRSAPublicKey[] =
"brave.web_discovery.rsa_pub_key";
inline constexpr char kAnonymousCredentialsDict[] =
"brave.web_discovery.anon_creds";

inline constexpr char kScheduledDoubleFetches[] =
"brave.web_discovery.scheduled_double_fetches";
inline constexpr char kScheduledReports[] =
Expand Down
6 changes: 3 additions & 3 deletions components/web_discovery/browser/server_config_loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ constexpr char kPatternsFilename[] = "wdp_patterns.json";
constexpr char kOmittedLocationValue[] = "--";
constexpr auto kAllowedReportLocations =
base::MakeFixedFlatSet<std::string_view>(
{"de", "at", "ch", "es", "us", "fr", "nl", "gb", "it", "be",
"se", "dk", "fi", "cz", "gr", "hu", "ro", "no", "ca", "au",
"ru", "ua", "in", "pl", "jp", "br", "mx", "cn", "ar"});
{"ar", "at", "au", "be", "br", "ca", "ch", "cn", "cz", "de",
"dk", "es", "fi", "fr", "gb", "gr", "hu", "in", "it", "jp",
"mx", "nl", "no", "pl", "ro", "ru", "se", "ua", "us"});

KeyMap ParseKeys(const base::Value::Dict& encoded_keys) {
KeyMap map;
Expand Down
9 changes: 9 additions & 0 deletions components/web_discovery/browser/wdp_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ void WDPService::Stop() {
content_scraper_ = nullptr;
server_config_loader_ = nullptr;
credential_manager_ = nullptr;

profile_prefs_->ClearPref(kWebDiscoveryNativeEnabled);
profile_prefs_->ClearPref(kAnonymousCredentialsDict);
profile_prefs_->ClearPref(kCredentialRSAPrivateKey);
profile_prefs_->ClearPref(kCredentialRSAPublicKey);
profile_prefs_->ClearPref(kScheduledDoubleFetches);
profile_prefs_->ClearPref(kScheduledReports);
profile_prefs_->ClearPref(kUsedBasenameCounts);
profile_prefs_->ClearPref(kPageCounts);
}

void WDPService::OnEnabledChange() {
Expand Down

0 comments on commit 072fdfc

Please sign in to comment.