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

Protect from Link Tracking on Tumblr #2733

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
36 changes: 36 additions & 0 deletions src/js/firstparties/tumblr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// only observed links with this format out in the wild
let tumblr_links = "a[href^='https://t.umblr.com/redirect?']";
Copy link
Member

Choose a reason for hiding this comment

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

This file is almost identical to https://github.com/EFForg/privacybadger/blob/master/src/js/firstparties/google-static.js. What if we reuse google-static.js, conditionally setting the DOM selector variable (based on document.domain maybe)?


// reassigns the href and scrubs link of all unnecessary attributes
function unwrapLink(a) {
let href = new URL(a.href).searchParams.get('z');
if (!window.isURL(href)) {
return;
}

for (let attr of a.attributes) {
if (!['target', 'class', 'style'].includes(attr.name)) {
Copy link
Member

Choose a reason for hiding this comment

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

Should we also keep ARIA attributes?

a.removeAttribute(attr.name);
}
}

a.rel = "noreferrer";
a.href = href;
}

// main handler to target each link on page and launder them through the unwrapLink func
function unwrapAll() {
document.querySelectorAll(tumblr_links).forEach((a) => {
unwrapLink(a);
});
}

chrome.runtime.sendMessage({
type: "checkEnabled"
}, function (enabled) {
if (!enabled) {
return;
}
unwrapAll();
setInterval(unwrapAll, 2000);
});
14 changes: 14 additions & 0 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,20 @@
"all_frames": true,
"run_at": "document_idle"
},
{
"js": [
"js/firstparties/lib/utils.js",
"js/firstparties/tumblr.js"
],
"matches": [
"https://tumblr.com/*",
"http://tumblr.com/*",
"https://*.tumblr.com/*",
"http://*.tumblr.com/*"
],
"all_frames": true,
"run_at": "document_idle"
},
{
"js": [
"js/contentscripts/utils.js",
Expand Down
35 changes: 35 additions & 0 deletions src/tests/tests/firstparties.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ let fb_wrap = 'https://facebook.com/l.php?u=' + destination;
let fb_xss = 'https://facebook.com/l.php?u=javascript://bad.site/%250Aalert(1)';
let g_wrap = 'https://www.google.com/url?q=' + destination;
let g_ping = '/url?url=' + destination;
let tumblr_wrap = 'https://t.umblr.com/redirect?z=' + destination;

function makeLink(href) {
let element = document.createElement('a');
Expand Down Expand Up @@ -164,4 +165,38 @@ QUnit.test('google search de-instrumentation', (assert) => {
fixture.appendChild(util_script);
});

QUnit.test('tumblr link unwrapping', (assert) => {
Copy link
Member

Choose a reason for hiding this comment

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

We probably don't need a unit test for this PR, especially if we reuse google-static.js for Tumblr.

const NUM_CHECKS = 2,
done = assert.async();
assert.expect(NUM_CHECKS);

let fixture = document.getElementById('qunit-fixture');
let tumblr_link = makeLink(tumblr_wrap);

// create first-party utility script
let util_script = document.createElement('script');
util_script.src = '../js/firstparties/lib/utils.js';

// create the content script
let tumblr_script = document.createElement('script');
tumblr_script.src = '../js/firstparties/tumblr.js';
tumblr_script.onload = function() {
assert.equal(tumblr_link.href, destination, 'unwrapped tumblr link');
assert.ok(tumblr_link.rel.includes('noreferrer'),
'added noreferrer to tumblr link');

unstub();
done();
};

// after the utility script has finished loading, add the content script
util_script.onload = function() {
fixture.append(tumblr_script);
};

stub([tumblr_link], '/url?');
fixture.appendChild(tumblr_link);
fixture.appendChild(util_script);
});

}());