From f40032dc000bbb535992d7a904dd963efae442d6 Mon Sep 17 00:00:00 2001 From: Bennett Cyphers Date: Fri, 7 Sep 2018 14:11:46 -0700 Subject: [PATCH 1/2] Strip UTM parameters from third-party requests ...and all main-frame requests. Leave first-party background requests and private or social widget requests alone. --- src/js/webrequest.js | 55 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/src/js/webrequest.js b/src/js/webrequest.js index 291b29cd4a..baad292f8f 100644 --- a/src/js/webrequest.js +++ b/src/js/webrequest.js @@ -51,12 +51,18 @@ function onBeforeRequest(details) { type = details.type, url = details.url; + let noUtm = stripUtmParams(url); + if (type == "main_frame") { forgetTab(tab_id); badger.recordFrame(tab_id, frame_id, details.parentFrameId, url); - return {}; + if (noUtm) { + return {redirectUrl: noUtm}; + } else { + return {}; + } } if (type == "sub_frame") { @@ -82,6 +88,9 @@ function onBeforeRequest(details) { } var requestAction = checkAction(tab_id, requestDomain, frame_id); + + // if requestAction is false, this is a private domain OR the domain/frame has + // been unblocked if (!requestAction) { return {}; } @@ -93,7 +102,11 @@ function onBeforeRequest(details) { } if (requestAction != constants.BLOCK && requestAction != constants.USER_BLOCK) { - return {}; + if (noUtm) { + return {redirectUrl: noUtm}; + } else { + return {}; + } } if (type == 'script') { @@ -601,6 +614,44 @@ function unblockSocialWidgetOnTab(tabId, socialWidgetUrls) { } } +/** + * Strip Urchin Tracking Module (UTM) parameters from a url + * Adapted from au-revoir-utm (https://github.com/rik/au-revoir-utm), 2018-09-06 + * + * @param {String} url The URL of the outgoing request + */ +function stripUtmParams(url) { + const UTM_PREFIX = 'utm_'; + + // short-circuit if this isn't an HTTPS link, + // or if there aren't any utm parameters + if (!url.startsWith('https://') || !url.includes(UTM_PREFIX)) { + return; + } + + // remove utm from normal url parameters + let parsedURL = new URL(url) + for (let param of [...parsedURL.searchParams.keys()]) { + if (param.startsWith(UTM_PREFIX)) { + parsedURL.searchParams.delete(param); + } + } + + // remove utm from parameters after the hash in the url + let parsedFragment = new URLSearchParams(parsedURL.hash.substring(1)) + for (let param of [...parsedFragment.keys()]) { + if (param.startsWith(UTM_PREFIX)) { + parsedFragment.delete(param); + } + } + parsedURL.hash = parsedFragment.toString(); + + // if the url has changed, return it. Otherwise return null. + if (parsedUrl != url) { + return parsedURL.toString(); + } +} + /** * sender.tab is available for content script (not popup) messages only */ From c3fb88517418d34bb1243101ac142bbaa9436017 Mon Sep 17 00:00:00 2001 From: Bennett Cyphers Date: Fri, 7 Sep 2018 14:41:49 -0700 Subject: [PATCH 2/2] Fix lint issues --- src/js/webrequest.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/js/webrequest.js b/src/js/webrequest.js index baad292f8f..c0671cc173 100644 --- a/src/js/webrequest.js +++ b/src/js/webrequest.js @@ -630,7 +630,7 @@ function stripUtmParams(url) { } // remove utm from normal url parameters - let parsedURL = new URL(url) + let parsedURL = new URL(url); for (let param of [...parsedURL.searchParams.keys()]) { if (param.startsWith(UTM_PREFIX)) { parsedURL.searchParams.delete(param); @@ -638,7 +638,7 @@ function stripUtmParams(url) { } // remove utm from parameters after the hash in the url - let parsedFragment = new URLSearchParams(parsedURL.hash.substring(1)) + let parsedFragment = new URLSearchParams(parsedURL.hash.substring(1)); for (let param of [...parsedFragment.keys()]) { if (param.startsWith(UTM_PREFIX)) { parsedFragment.delete(param); @@ -647,7 +647,7 @@ function stripUtmParams(url) { parsedURL.hash = parsedFragment.toString(); // if the url has changed, return it. Otherwise return null. - if (parsedUrl != url) { + if (parsedURL != url) { return parsedURL.toString(); } }