Skip to content

Commit

Permalink
Fix tab URL misattribution in learning listeners.
Browse files Browse the repository at this point in the history
By checking the request's document URL when available.
  • Loading branch information
ghostwords committed Oct 21, 2018
1 parent d8aedad commit 11c8da7
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 63 deletions.
8 changes: 8 additions & 0 deletions src/js/heuristicblocking.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ HeuristicBlocker.prototype = {

let tab_origin = tabOrigins[details.tabId];

// we may no longer be on the page the request is coming from
const request_doc_host = utils.getDocumentHostForRequest(details),
request_doc_origin = window.getBaseDomain(request_doc_host),
misattribution = request_doc_origin && request_doc_origin != tab_origin;
if (misattribution) {
tab_origin = request_doc_origin;
}

// ignore first-party requests
if (!tab_origin || request_origin == tab_origin) {
return {};
Expand Down
62 changes: 61 additions & 1 deletion src/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,15 +344,75 @@ function getHostFromDomainInput(input) {
return uri.host;
}

/**
* Gets the hostname for a given request's top-level document.
*
* The request's document may be different from the current top-level document
* loaded in tab as requests can come out of order:
*
* - "main_frame" requests usually but not always mark a boundary
* (navigating to another site while the current page is still loading)
* - sometimes there is no "main_frame" request
* (service worker pages in Firefox)
*
* @param {Object} details chrome.webRequest request details object
*
* @return {String} the hostname for the request's top-level document
*/
function getDocumentHostForRequest(details) {
let host, url;

// Firefox 54+
if (details.hasOwnProperty("documentUrl")) {
if (details.type == "main_frame") {
// the top-level document itself
url = details.url;
} else if (details.hasOwnProperty("frameAncestors")) {
// Firefox 58+
if (details.frameAncestors.length) {
// inside a frame
url = details.frameAncestors[details.frameAncestors.length - 1].url;
} else {
// inside the top-level document
url = details.documentUrl;
}
} else {
// TODO Firefox 54-57 or a service worker request
if (details.documentUrl.endsWith("/sw.js")) {
url = details.documentUrl;
}
}

// Chrome 63+
} else if (details.hasOwnProperty("initiator")) {
if (details.initiator && details.initiator != "null") {
if (details.type == "main_frame") {
url = details.url;
} else if (details.parentFrameId == -1 || details.type == "sub_frame" && details.parentFrameId === 0) {
// TODO can only rely on initiator for main frame resources:
// https://crbug.com/838242#c17
url = details.initiator + '/';
}
}
}

if (url) {
host = window.extractHostFromURL(url);
}

return host;
}

/************************************** exports */
var exports = {
arrayBufferToBase64,
estimateMaxEntropy,
explodeSubdomains,
getDocumentHostForRequest,
getHostFromDomainInput,
nDaysFromNow,
oneDayFromNow,
oneDay,
oneDayFromNow,
oneHour,
oneMinute,
oneSecond,
Expand Down
65 changes: 3 additions & 62 deletions src/js/webrequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function onBeforeRequest(details) {
// if we are no longer on the page the request is coming for,
// don't log in popup or attempt to replace social widgets
// but do block request/modify headers
const request_doc_host = getDocumentHostForRequest(details),
const request_doc_host = utils.getDocumentHostForRequest(details),
misattribution = request_doc_host && request_doc_host != tab_host;
if (misattribution) {
tab_host = request_doc_host;
Expand Down Expand Up @@ -178,7 +178,7 @@ function onBeforeSendHeaders(details) {
// if we are no longer on the page the request is coming for,
// don't log in popup or attempt to replace social widgets
// but do block request/modify headers
const request_doc_host = getDocumentHostForRequest(details),
const request_doc_host = utils.getDocumentHostForRequest(details),
misattribution = request_doc_host && request_doc_host != tab_host;
if (misattribution) {
tab_host = request_doc_host;
Expand Down Expand Up @@ -308,7 +308,7 @@ function onHeadersReceived(details) {
// if we are no longer on the page the request is coming for,
// don't log in popup or attempt to replace social widgets
// but do block request/modify headers
const request_doc_host = getDocumentHostForRequest(details),
const request_doc_host = utils.getDocumentHostForRequest(details),
misattribution = request_doc_host && request_doc_host != tab_host;
if (misattribution) {
tab_host = request_doc_host;
Expand Down Expand Up @@ -381,65 +381,6 @@ function isThirdPartyDomain(domain1, domain2) {
return false;
}

/**
* Gets the hostname for a given request's top-level document.
*
* The request's document may be different from the current top-level document
* loaded in tab as requests can come out of order:
*
* - "main_frame" requests usually but not always mark a boundary
* (navigating to another site while the current page is still loading)
* - sometimes there is no "main_frame" request
* (service worker pages in Firefox)
*
* @param {Object} details chrome.webRequest request details object
*
* @return {String} the hostname for the request's top-level document
*/
function getDocumentHostForRequest(details) {
let host, url;

// Firefox 54+
if (details.hasOwnProperty("documentUrl")) {
if (details.type == "main_frame") {
// the top-level document itself
url = details.url;
} else if (details.hasOwnProperty("frameAncestors")) {
// Firefox 58+
if (details.frameAncestors.length) {
// inside a frame
url = details.frameAncestors[details.frameAncestors.length - 1].url;
} else {
// inside the top-level document
url = details.documentUrl;
}
} else {
// TODO Firefox 54-57 or a service worker request
if (details.documentUrl.endsWith("/sw.js")) {
url = details.documentUrl;
}
}

// Chrome 63+
} else if (details.hasOwnProperty("initiator")) {
if (details.initiator && details.initiator != "null") {
if (details.type == "main_frame") {
url = details.url;
} else if (details.parentFrameId == -1 || details.type == "sub_frame" && details.parentFrameId === 0) {
// TODO can only rely on initiator for main frame resources:
// https://crbug.com/838242#c17
url = details.initiator + '/';
}
}
}

if (url) {
host = window.extractHostFromURL(url);
}

return host;
}

/**
* Gets the host name for a given tab id
* @param {Integer} tabId chrome tab id
Expand Down

0 comments on commit 11c8da7

Please sign in to comment.