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

Hardcode custom headers #9157

Merged
merged 5 commits into from
Jun 23, 2021
Merged

Hardcode custom headers #9157

merged 5 commits into from
Jun 23, 2021

Conversation

kkuehlz
Copy link
Contributor

@kkuehlz kkuehlz commented Jun 17, 2021

Resolves brave/brave-browser#16455

Submitter Checklist:

  • I confirm that no security/privacy review is needed, or that I have requested one
  • There is a ticket for my issue
  • Used Github auto-closing keywords in the PR description above
  • Wrote a good PR/commit description
  • Added appropriate labels (QA/Yes or QA/No; release-notes/include or release-notes/exclude; OS/...) to the associated issue
  • Checked the PR locally: npm run test -- brave_browser_tests, npm run test -- brave_unit_tests, npm run lint, npm run gn_check, npm run tslint
  • Ran git rebase master (if needed)

Reviewer Checklist:

  • A security review is not needed, or a link to one is included in the PR description
  • New files have MPL-2.0 license header
  • Adequate test coverage exists to prevent regressions
  • Major classes, functions and non-trivial code blocks are well-commented
  • Changes in component dependencies are properly reflected in gn
  • Code follows the style guide
  • Test plan is specified in PR before merging

After-merge Checklist:

Test Plan:

@kkuehlz kkuehlz requested a review from simonhong as a code owner June 17, 2021 01:33
@kkuehlz kkuehlz changed the title Issues/16455 Hardcode custom headers Jun 17, 2021
Copy link
Member

@simonhong simonhong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++ with one nit. I can't find more commnet
This is rubber stamp to prevent blocking merge after getting sufficient approve from others :)

Copy link
Member

@bsclifton bsclifton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a note for @fmarier but LGTM! ++

Copy link
Member

@fmarier fmarier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking this so promptly up @keur !

CreateReferralHeader(kPartnerUpholdName, kPartnerUpholdDomains));
headers.Append(
CreateReferralHeader(kPartnerGrammarlyName, kPartnerGrammarlyDomains));
pref_service_->Set(kReferralHeaders, headers);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't we just hardcode the whole value as a string and then parse the base::Value from json in one line?

Copy link
Contributor Author

@kkuehlz kkuehlz Jun 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a style thing, and i prefer what i have. parsing invalid JSON will not be covered by tests. at least with this we get a little more safety with compile-time type checking

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so we can remove fake header data from the test and do real checks for real hardcoded domains. This will increase safety and reduce boilerplate.

@iefremov
Copy link
Contributor

After looking at the code I've found out that these headers are only used in OnBeforeStartTransaction_ReferralsWork, so we can erase even more stuff: BraveRequestHandler::OnReferralHeadersChanged, the field in brave::BraveRequestInfo, the preference. Just check the hardcoded list in OnBeforeStartTransaction_ReferralsWork

Copy link
Contributor

@iefremov iefremov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems we can drop even more legacy code

Minimize network requests by hardcoding the referral headers as opposed
to fetching them from the network. These headers are deprecated and
won't change anymore.

Resolves brave/brave-browser#16455
Kevin Kuehler added 3 commits June 22, 2021 14:30
  * Set the referrals in a singleton. Delete all the boilerplate code in
    the network delegate helper
  * Simplify the tests to reflect real referrer headers we use
    We can delete a lot more dead code now.
@@ -743,8 +694,8 @@ std::string BraveReferralsService::FormatExtraHeaders(
return std::string();

const base::DictionaryValue* request_headers_dict = nullptr;
if (!GetMatchingReferralHeaders(*referral_headers_list, &request_headers_dict,
url))
if (!BraveReferralsHeaders::GetInstance()->GetMatchingReferralHeaders(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, this is interesting. Do we serve any headers from /promo/initialize/nonua"? @fmarier

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch. no we don't (i authored the current version of laptop-updates). going to remove this too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

confirmed with @aekeus this was good to remove

std::string GetAPIKey();
class BraveReferralsHeaders {
public:
static BraveReferralsHeaders* GetInstance() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is really no need to write the singleton boilerplate. Just a function that internally uses a static vector (or even better, flat_map) with hardcoded values would be enough.

domains.Append(header);
headers_dict.SetKey("domains", std::move(domains));

constexpr double expiration_ms = 31536000000.0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it used at all?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

constexpr char kPartnerUpholdName[] = "uphold";
constexpr char kPartnerGrammarlyName[] = "grammarly";

referral_headers_.push_back(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

personally, I would make global

constexpr auto kHeaders = base::MakeFixedFlatMap<base::StringPiece, base::StringPiece> {
 {"eaff.com", "eaff}, {"stg.eaff.com", "eaff"} , {"uphold.com", "uphold}, ...
}

then I would iterate over keys and match the domain and then get the header value in one line (if this is everything we need).

This is no longer available server-side.
@kkuehlz kkuehlz merged commit b50d69f into master Jun 23, 2021
@kkuehlz kkuehlz deleted the issues/16455 branch June 23, 2021 19:34
return std::string();

const base::DictionaryValue* request_headers_dict = nullptr;
if (!GetMatchingReferralHeaders(*referral_headers_list, &request_headers_dict,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that means we don't need GetMatchingReferralHeaders overloads?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The custom header list should be hard-coded
5 participants