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

Create site hacks network delegate helper #68

Merged
merged 6 commits into from
Mar 28, 2018
Merged

Create site hacks network delegate helper #68

merged 6 commits into from
Mar 28, 2018

Conversation

bbondy
Copy link
Member

@bbondy bbondy commented Mar 26, 2018

Implements some of the needed site-hacks from browser-laptop.
Implements these site hacks as a new network delegate helper.

This partially implements the following issue: brave/brave-browser#84.

An alternate strategy for this task using an extension was considered and partially implemented in this branch: site-hacks-extension, but we aren't sure if we'll have support for extensions in Android yet, and it seemed easy enough to do these via C++.

In this PR:

  • Empty data URL redirect for sp1.nypost.com
  • Empty data URL redirect for sp.nasdaq.com
  • Google tag services stub for www.googletagservices.com/tag/js/gpt.js
  • Google tag manager stub for http://www.googletagmanager.com/gtm.js
  • Block https://www.lesechos.fr/xtcore.js
  • Block https://*.y8.com/js/sdkloader/outstream.js

In a follow up PRs:

  • Cookie exceptions
  • Referrer exceptions
  • Local storage exceptions
  • Forbes custom cookie
  • Twitter NoScript redirect
  • Brave UA whitelist

@bbondy
Copy link
Member Author

bbondy commented Mar 26, 2018

I'm still working on a test for the above, you can review before but I'll do the test before merging.

@bbondy
Copy link
Member Author

bbondy commented Mar 26, 2018

Test pushed, ready for review.

bbondy added 5 commits March 27, 2018 16:56
This re-uses the static network redirect helper class for both the profile network
delegate and the system network delegate.
Allows a list of hosts to be redirected to empty data URLs.

The list is initially:
- sp1.nypost.com
- sp.nasdaq.com
Matches requests for Google tag manager and Google tag services, and
uses our polyfill instead.
Creates a different network delegate helper so the system one is not
re-used.

This is for 2 reasons:
1) To avoid accidentally leaking things like geolocation redirects to
the profile network context.
2) To not need to slow down the system network context with other site hacks
meant for the profile network context.
Allows the SiteHacks delegate to block things.
These should eventually be moved to the ad-block lists, but for now I'm
just porting things 1:1 from browser-laptop.
garrettr
garrettr previously approved these changes Mar 27, 2018
Copy link
Contributor

@garrettr garrettr left a comment

Choose a reason for hiding this comment

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

As we discussed on Slack, this needed a rebase to pick up some of the recent rename-related changes. With the rebase, I am able to run yarn test brave_unit_tests successfully.

The code looks good and the new tests look great. LGTM, I mentioned some concerns/nits in line comments but they do not need to block merge.

str.append(": noopthisfn, updateCorrelator: noopfn }; var SizeMappingBuilder = function() { }; p = SizeMappingBuilder.prototype; p.addSize = noopthisfn; p.build = noopnullfn; var Slot = function() { }; p = Slot.prototype; p.addService = noopthisfn; p.clearCategoryExclusions = noopthisfn; p.clearTargeting = noopthisfn; p.defineSizeMapping = noopthisfn; p.get = noopnullfn; p.getAdUnitPath = nooparrayfn; p.getAttributeKeys = nooparrayfn; p.getCategoryExclusions = nooparrayfn; p.getDomId = noopstrfn; p.getSlot");
str.append("ElementId = noopstrfn; p.getSlotId = noopthisfn; p.getTargeting = nooparrayfn; p.getTargetingKeys = nooparrayfn; p.set = noopthisfn; p.setCategoryExclusion = noopthisfn; p.setClickUrl = noopthisfn; p.setCollapseEmptyDiv = noopthisfn; p.setTargeting = noopthisfn; var gpt = window.googletag || {}; window.googletag.destroySlots = function () { }; var cmd = gpt.cmd || []; gpt.apiReady = true; gpt.cmd = []; gpt.cmd.push = function(a) { try { a(); } catch (ex) { } return 1; }; gpt.companionAds = func");
str.append("tion() { return companionAdsService; }; gpt.content = function() { return contentService; }; gpt.defineOutOfPageSlot = function() { return new Slot(); }; gpt.defineSlot = function() { return new Slot(); }; gpt.disablePublisherConsole = noopfn; gpt.display = noopfn; gpt.enableServices = noopfn; gpt.getVersion = noopstrfn; gpt.pubads = function() { return pubAdsService; }; gpt.pubadsReady = true; gpt.sizeMapping = function() { return new SizeMappingBuilder(); }; window.googletag = gpt; while ( cm");
str.append("d.length !== 0 ) { gpt.cmd.push(cmd.shift()); } })();");
Copy link
Contributor

Choose a reason for hiding this comment

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

It's too bad C++ doesn't have something akin to Rust's include_str! (as far as I know). Perhaps GN has something that can help us out with including large strings like this. I think all these str.appends are fine for now, but may be difficult to maintain over time.

Copy link
Member

Choose a reason for hiding this comment

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

How about getting this script from file in resource pak?

Copy link
Member Author

Choose a reason for hiding this comment

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

That's a great idea. I won't block on it but I"ll do it in this new issue I just posted:
brave/brave-browser#128

str.append("d.length !== 0 ) { gpt.cmd.push(cmd.shift()); } })();");
base64_output.reserve(4668);
Base64UrlEncode(str, base::Base64UrlEncodePolicy::OMIT_PADDING, &base64_output);
base64_output= std::string(kJSDataURLPrefix) + base64_output;
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: space before =

Copy link
Member Author

Choose a reason for hiding this comment

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

will fix before landing, thx.

@bbondy
Copy link
Member Author

bbondy commented Mar 27, 2018

Thanks for the review.

As we discussed on Slack, this needed a rebase to pick up some of the recent rename-related changes. With the rebase, I am able to run yarn test brave_unit_tests successfully.

The old branch was also runnable on its own, but I rebased for ease of testing.
To get it to work before you'd have to npm run init and then build.

As long as it's buildable in general though on its own with init and build, rebases are not required because otherwise it'll take up everyone's time constantly rebasing with scale.

@bbondy
Copy link
Member Author

bbondy commented Mar 27, 2018

We probably have some more work to do on the tooling, because I think the sync that you did didn't reset things properly. Maybe because using the different repo name in DEPS, it probably doesn't have proper support for that.

#ifndef BRAVE_BROWSER_NET_BRAVE_SITE_HACKS_NETWORK_DELEGATE_H_
#define BRAVE_BROWSER_NET_BRAVE_SITE_HACKS_NETWORK_DELEGATE_H_

#include "chrome/browser/net/chrome_network_delegate.h"
Copy link
Member

Choose a reason for hiding this comment

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

nit: this header can be removed

#include "extensions/common/url_pattern.h"
#include "net/url_request/url_request.h"


Copy link
Member

Choose a reason for hiding this comment

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

nit: two blank lines
Or did you add one more line intentionally?

Copy link
Member Author

Choose a reason for hiding this comment

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

We need to introduce .cc linting soon. Ya I did it intentionally but I see now most chromium files do not do that. I'll update before merging this.

@bbondy bbondy merged commit fd1f430 into master Mar 28, 2018
@bsclifton bsclifton deleted the site-hacks branch June 18, 2018 06:29
bbondy pushed a commit that referenced this pull request Feb 18, 2019
Add scope to methods which modify content settings
fmarier pushed a commit that referenced this pull request Oct 29, 2019
petemill pushed a commit that referenced this pull request Jul 27, 2020
petemill pushed a commit that referenced this pull request Jul 28, 2020
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.

3 participants