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

for #26: refactor (some) filterRequest code into modules and test #75

Merged
merged 3 commits into from
Aug 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
*.bundle.js
web-ext-artifacts/*.zip
css/foundation.min.css
.nyc_output
83 changes: 31 additions & 52 deletions js/background.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
var {allHosts, canonicalizeHost} = require('./canonicalize')
const {loadLists} = require('./lists')
var {canonicalizeHost} = require('./canonicalize')
const {loadLists, hostInBlocklist, hostInEntity} = require('./lists')
const {allowRequest} = require('./requests')
const {log} = require('./log')

var TESTPILOT_TELEMETRY_CHANNEL = 'testpilot-telemetry'
var testpilotPingChannel = new BroadcastChannel(TESTPILOT_TELEMETRY_CHANNEL)

// HACK: Start with active tab id = 1 when browser starts
Copy link
Member

Choose a reason for hiding this comment

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

@groovecoder This inline comment sounds interesting, may I ask you some additional details about this?
(e.g. some additional info about the scenario and the issue)

var currentActiveTabID = 1
var currentOriginDisabledIndex = -1
Expand Down Expand Up @@ -46,43 +44,32 @@ function blockTrackerRequests (blocklist, allowedHosts, entityList) {

// undefined origins are browser internals (e.g., about:newtab)
if (typeof requestDetails.originUrl === 'undefined') {
totalExecTime[requestTabID] += Date.now() - blockTrackerRequestsStart
return {}
return allowRequest(requestTabID, totalExecTime, blockTrackerRequestsStart)
}

// Determine all origin flags
originTopHost = canonicalizeHost(new URL(requestDetails.originUrl).host)
currentActiveOrigin = originTopHost
currentOriginDisabledIndex = allowedHosts.indexOf(currentActiveOrigin)

currentOriginDisabled = currentOriginDisabledIndex > -1
if (requestDetails.frameId === 0) {
mainFrameOriginTopHosts[requestTabID] = originTopHost
}

currentOriginDisabled = currentOriginDisabledIndex > -1
// Allow request originating from Firefox and/or new tab/window origins
firefoxOrigin = (typeof originTopHost !== 'undefined' && originTopHost.includes('moz-nullprincipal'))
newOrigin = originTopHost === ''

// Allow request originating from Firefox and/or new tab/window origins
if (firefoxOrigin || newOrigin) {
totalExecTime[requestTabID] += Date.now() - blockTrackerRequestsStart
return {}
return allowRequest(requestTabID, totalExecTime, blockTrackerRequestsStart)
}

requestTopHost = canonicalizeHost(new URL(requestDetails.url).host)
// check if any host from lowest-level to top-level is in the blocklist
var allRequestHosts = allHosts(requestTopHost)
for (let requestHost of allRequestHosts) {
requestHostInBlocklist = blocklist.has(requestHost)
if (requestHostInBlocklist) {
break
}
}

requestHostInBlocklist = hostInBlocklist(blocklist, requestTopHost)

// Allow requests to 3rd-party domains NOT in the block-list
if (!requestHostInBlocklist) {
totalExecTime[requestTabID] += Date.now() - blockTrackerRequestsStart
return {}
return allowRequest(requestTabID, totalExecTime, blockTrackerRequestsStart)
}

requestIsThirdParty = requestTopHost !== originTopHost
Expand All @@ -91,8 +78,7 @@ function blockTrackerRequests (blocklist, allowedHosts, entityList) {
// Allow all requests to the main frame origin domain from child frames' pages
requestHostMatchesMainFrame = (requestDetails.frameId > 0 && requestTopHost === mainFrameOriginTopHosts[requestTabID])
if (requestHostMatchesMainFrame) {
totalExecTime[requestTabID] += Date.now() - blockTrackerRequestsStart
return {}
return allowRequest(requestTabID, totalExecTime, blockTrackerRequestsStart)
}
log(`requestTopHost: ${requestTopHost} does not match originTopHost: ${originTopHost}...`)

Expand All @@ -102,31 +88,18 @@ function blockTrackerRequests (blocklist, allowedHosts, entityList) {
var originIsEntityProperty = false
var mainFrameOriginIsEntityProperty = false

for (let requestHost of allHosts(requestTopHost)) {
requestIsEntityResource = entity.resources.indexOf(requestHost) > -1
if (requestIsEntityResource) {
requestEntityName = entityName
break
}
}
for (let originHost of allHosts(originTopHost)) {
originIsEntityProperty = entity.properties.indexOf(originHost) > -1
if (originIsEntityProperty) {
break
}
requestIsEntityResource = hostInEntity(entity.resources, requestTopHost)
if (requestIsEntityResource) {
requestEntityName = entityName
}

for (let mainFrameOriginHost of allHosts(mainFrameOriginTopHosts[requestTabID])) {
mainFrameOriginIsEntityProperty = entity.properties.indexOf(mainFrameOriginHost) > -1
if (mainFrameOriginIsEntityProperty) {
break
}
}
originIsEntityProperty = hostInEntity(entity.properties, originTopHost)

mainFrameOriginIsEntityProperty = hostInEntity(entity.properties, mainFrameOriginTopHosts[requestTabID])

if ((originIsEntityProperty || mainFrameOriginIsEntityProperty) && requestIsEntityResource) {
log(`originTopHost ${originTopHost} and resource requestTopHost ${requestTopHost} belong to the same entity: ${entityName}; allowing request`)
totalExecTime[requestTabID] += Date.now() - blockTrackerRequestsStart
return {}
return allowRequest(requestTabID, totalExecTime, blockTrackerRequestsStart)
}
}

Expand All @@ -144,7 +117,7 @@ function blockTrackerRequests (blocklist, allowedHosts, entityList) {
if (allowedEntities[requestTabID].indexOf(requestEntityName) === -1) {
allowedEntities[requestTabID].push(requestEntityName)
}
return {}
return allowRequest(requestTabID, totalExecTime, blockTrackerRequestsStart)
}

blockedRequests[requestTabID].push(requestTopHost)
Expand All @@ -162,12 +135,11 @@ function blockTrackerRequests (blocklist, allowedHosts, entityList) {
}

// none of the above checks matched, so default to allowing the request
totalExecTime[requestTabID] += Date.now() - blockTrackerRequestsStart
return {}
return allowRequest(requestTabID, totalExecTime, blockTrackerRequestsStart)
}
}

function startListeners ({blocklist, allowedHosts, entityList}) {
function startListeners ({blocklist, allowedHosts, entityList}, testPilotPingChannel) {
browser.webRequest.onBeforeRequest.addListener(
blockTrackerRequests(blocklist, allowedHosts, entityList),
{urls: ['*://*/*']},
Expand Down Expand Up @@ -208,7 +180,7 @@ function startListeners ({blocklist, allowedHosts, entityList}) {
feedback: message.feedback
}
log('telemetry ping payload: ' + JSON.stringify(testPilotPingMessage))
testpilotPingChannel.postMessage(testPilotPingMessage)
testPilotPingChannel.postMessage(testPilotPingMessage)
log('mainFrameOriginTopHosts[currentActiveTabID]: ' + mainFrameOriginTopHosts[currentActiveTabID])
browser.tabs.sendMessage(currentActiveTabID, {
'feedback': message.feedback,
Expand All @@ -223,7 +195,7 @@ function startListeners ({blocklist, allowedHosts, entityList}) {
notes: message.notes
}
log('telemetry ping payload: ' + JSON.stringify(testPilotPingMessage))
testpilotPingChannel.postMessage(testPilotPingMessage)
testPilotPingChannel.postMessage(testPilotPingMessage)
browser.tabs.sendMessage(currentActiveTabID, message)
}
if (message === 'close-feedback') {
Expand All @@ -238,6 +210,13 @@ const state = {
entityList: {}
}

function initTestPilotPingChannel ({BroadcastChannel}) {
let TESTPILOT_TELEMETRY_CHANNEL = 'testpilot-telemetry'
let testPilotPingChannel = new BroadcastChannel(TESTPILOT_TELEMETRY_CHANNEL)
return testPilotPingChannel
}

loadLists(state).then(() => {
startListeners(state)
let testPilotPingChannel = initTestPilotPingChannel(window)
startListeners(state, testPilotPingChannel)
}, console.error.bind(console))
11 changes: 0 additions & 11 deletions js/canonicalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,7 @@ function canonicalizeHost (host) {
return canonicalizedHost
}

function allHosts (host) {
const allHosts = []
const hostParts = host.split('.')
while (hostParts.length > 1) {
allHosts.push(hostParts.join('.'))
hostParts.splice(0, 1)
}
return allHosts
}

module.exports = {
allHosts,
canonicalizeHost,
trim
}
40 changes: 39 additions & 1 deletion js/lists.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
function allHosts (host) {
const allHosts = []
const hostParts = host.split('.')
while (hostParts.length > 1) {
allHosts.push(hostParts.join('.'))
hostParts.splice(0, 1)
}
return allHosts
}

function loadLists (state) {
const blockListPromise = loadJSON('disconnect-blocklist.json').then((data) => {
state.blocklist = processBlockListJSON(data)
Expand Down Expand Up @@ -69,7 +79,35 @@ function getAllowedHostsList () {
})
}

// check if any host from lowest-level to top-level is in the blocklist
function hostInBlocklist (blocklist, host) {
let requestHostInBlocklist = false
var allHostVariants = allHosts(host)
for (let hostVariant of allHostVariants) {
requestHostInBlocklist = blocklist.has(hostVariant)
if (requestHostInBlocklist) {
return true
}
}
return false
}

// check if any host from lowest-level to top-level is in the entitylist
function hostInEntity (entityHosts, host) {
let entityHost = false
for (let hostVariant of allHosts(host)) {
entityHost = entityHosts.indexOf(hostVariant) > -1
if (entityHost) {
return true
}
}
return false
}

module.exports = {
allHosts,
loadLists,
processBlockListJSON
processBlockListJSON,
hostInBlocklist,
hostInEntity
}
8 changes: 8 additions & 0 deletions js/requests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function allowRequest (tabID, totalExecTime, startDateTime) {
totalExecTime[tabID] += Date.now() - startDateTime
return {}
}

module.exports = {
allowRequest
}
Loading