This repository has been archived by the owner on Dec 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 974
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow first party URLs from www.youtube.com
Auditors: - @bsclifton: For code review, also please pull into 0.14.0 if that's ok. - @ukemulks: For verification that ads are being blocked on YouTube Test cases: - Make sure automated tests are passing - `npm run test -- --grep=isThirdPartyHost` - `npm run test -- --grep=adBlockUtil` - Make sure things that used to be blocked are still being blocked. - Make sure you don't see ads on YouTube. Refactored code to be more easily unit tested and added some as well Fix #7432
- Loading branch information
Showing
8 changed files
with
153 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
const {siteHacks} = require('../../../js/data/siteHacks') | ||
const {FilterOptions} = require('ad-block') | ||
const isThirdPartyHost = require('../isThirdPartyHost') | ||
|
||
const whitelistHosts = ['disqus.com', 'a.disquscdn.com'] | ||
|
||
/** | ||
* Maps filtering request resourceTypes to ones that our adBlock library understands | ||
*/ | ||
const mapFilterType = { | ||
mainFrame: FilterOptions.document, | ||
subFrame: FilterOptions.subdocument, | ||
stylesheet: FilterOptions.stylesheet, | ||
script: FilterOptions.script, | ||
image: FilterOptions.image, | ||
object: FilterOptions.object, | ||
xhr: FilterOptions.xmlHttpRequest, | ||
other: FilterOptions.other | ||
} | ||
|
||
/** | ||
* @param resourceType {string} The resource type from the web request API | ||
* @param firstPartyUrl {Url} The parsed URL of the main frame URL loading the url | ||
* @param url {Url} The parsed URL of the resource for consideration | ||
*/ | ||
const shouldDoAdBlockCheck = (resourceType, firstPartyUrl, url) => | ||
firstPartyUrl.protocol && | ||
// By default first party hosts are allowed, but enable the check if a flag is specified in siteHacks | ||
(isThirdPartyHost(firstPartyUrl.hostname || '', url.hostname) || | ||
siteHacks[firstPartyUrl.hostname] && siteHacks[firstPartyUrl.hostname].allowFirstPartyAdblockChecks) && | ||
// Only check http and https for now | ||
firstPartyUrl.protocol.startsWith('http') && | ||
// Only do adblock if the host isn't in the whitelist | ||
!whitelistHosts.find((whitelistHost) => whitelistHost === url.hostname || url.hostname.endsWith('.' + whitelistHost)) && | ||
// Make sure there's a valid resource type before trying to use adblock | ||
mapFilterType[resourceType] !== undefined | ||
|
||
module.exports = { | ||
mapFilterType, | ||
shouldDoAdBlockCheck | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
const getBaseDomain = require('../../js/lib/baseDomain').getBaseDomain | ||
|
||
/** | ||
* baseContextHost {string} - The base host to check against | ||
* testHost {string} - The host to check | ||
*/ | ||
const isThirdPartyHost = (baseContextHost, testHost) => { | ||
// TODO: Always return true if these are IP addresses that aren't the same | ||
if (!testHost || !baseContextHost) { | ||
return true | ||
} | ||
const documentDomain = getBaseDomain(baseContextHost) | ||
if (testHost.length > documentDomain.length) { | ||
return (testHost.substr(testHost.length - documentDomain.length - 1) !== '.' + documentDomain) | ||
} else { | ||
return (testHost !== documentDomain) | ||
} | ||
} | ||
|
||
module.exports = isThirdPartyHost |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* global describe, it */ | ||
|
||
require('../../../braveUnit') | ||
const assert = require('assert') | ||
const {URL} = require('url') | ||
|
||
const site = new URL('https://www.brave.com') | ||
const thirdPartyResource = new URL('https://www.coffee.com/logo.png') | ||
const firstPartyResource = new URL('https://www.brave.com/logo.png') | ||
const {shouldDoAdBlockCheck} = require('../../../../../app/browser/ads/adBlockUtil') | ||
|
||
describe('adBlockUtil test', function () { | ||
describe('shouldDoAdBlockCheck', function () { | ||
it('http protocol allows ad block checks', function () { | ||
assert.ok(shouldDoAdBlockCheck('script', new URL('https://www.brave.com'), thirdPartyResource)) | ||
}) | ||
it('https protocol allows ad block checks', function () { | ||
assert.ok(shouldDoAdBlockCheck('script', new URL('https://www.brave.com'), thirdPartyResource)) | ||
}) | ||
it('ftp protocol does not allow ad block checks', function () { | ||
assert.ok(!shouldDoAdBlockCheck('script', new URL('ftp://www.brave.com'), thirdPartyResource)) | ||
}) | ||
it('should check third party urls', function () { | ||
assert.ok(shouldDoAdBlockCheck('script', site, thirdPartyResource)) | ||
}) | ||
it('should NOT check first party urls', function () { | ||
assert.ok(!shouldDoAdBlockCheck('script', site, firstPartyResource)) | ||
}) | ||
it('Avoid checks with unknown resource types', function () { | ||
// This test is valid just as long as we don't start handling beefaroni resource types in the ad block lib!!! | ||
assert.ok(!shouldDoAdBlockCheck('beefaroni', site, new URL('https://disqus.com/test'))) | ||
}) | ||
it('should check first party hosts on youtube', function () { | ||
assert.ok(shouldDoAdBlockCheck('script', new URL('https://www.youtube.com'), new URL('https://www.youtube.com/script.js'))) | ||
}) | ||
it('diqus is allowed as third party, for now', function () { | ||
assert.ok(!shouldDoAdBlockCheck('script', site, new URL('https://disqus.com/test'))) | ||
assert.ok(!shouldDoAdBlockCheck('script', site, new URL('https://hello.disqus.com/test'))) | ||
assert.ok(!shouldDoAdBlockCheck('script', site, new URL('https://a.disquscdn.com/test'))) | ||
assert.ok(!shouldDoAdBlockCheck('script', site, new URL('https://b.a.disquscdn.com/test'))) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* global describe, it */ | ||
|
||
require('../../braveUnit') | ||
const assert = require('assert') | ||
const isThirdPartyHost = require('../../../../app/browser/isThirdPartyHost') | ||
|
||
const braveHost = 'brave.com' | ||
|
||
describe('isThirdPartyHost test', function () { | ||
it('A URL should not be third party to itself', function () { | ||
assert.ok(!isThirdPartyHost(braveHost, braveHost)) | ||
}) | ||
it('A subdomain URL should not be third party', function () { | ||
assert.ok(!isThirdPartyHost(braveHost, 'ragu.brave.com')) | ||
}) | ||
it('Unrelated URLs should be third party', function () { | ||
assert.ok(isThirdPartyHost(braveHost, 'ragu.com')) | ||
}) | ||
it('Checks subdomains properly', function () { | ||
assert.ok(isThirdPartyHost(braveHost, 'brave.ragu.com')) | ||
}) | ||
}) |
85464b9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Commit title should probably read:
Allow first party URL checks from www.youtube.com.
Sorry for the confusion.
85464b9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
++ looks great 😄 Thanks for the unit tests!
I moved issue to 0.14.0 and copied the steps there 😄
85464b9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bbondy
G2G!
This is good to push from my end, any anomalies can be captured and worked thru as they come in, but this is fantastic for Youtube.com. Thanks again!