Skip to content

Commit 93f4354

Browse files
authored
fix: rename ntp-tracker-stats.html to ntp-blocked-stats.html (#3658)
* fix: rename ntp-tracker-stats.html to ntp-blocked-stats.html * Restrict NTP stats redirect rules to duckduckgo.com requests
1 parent e1c8bff commit 93f4354

3 files changed

Lines changed: 31 additions & 23 deletions

File tree

‎shared/data/constants.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ module.exports = {
159159
},
160160
trackerStats: /** @type {const} */ ({
161161
allowedOrigin: 'https://duckduckgo.com',
162-
allowedPathname: 'ntp-tracker-stats.html',
162+
allowedPathname: 'ntp-blocked-stats.html',
163+
legacyPathname: 'ntp-tracker-stats.html',
163164
redirectTarget: 'html/tracker-stats.html',
164165
clientPortName: 'newtab-tracker-stats',
165166
/** @type {ReadonlyArray<string>} */

‎shared/js/background/dnr-utils.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export const ATB_PARAM_RULE_ID = 20003;
2323
export const ATB_EXTENSIONINSTALLED_RULE_ID = 20008;
2424
export const HOME_PAGE_RULE_ID = 20010;
2525
export const NEWTAB_TRACKER_STATS_REDIRECT_RULE_ID = 20006;
26+
export const NEWTAB_BLOCKED_STATS_REDIRECT_RULE_ID = 20012;
2627
export const SEARCH_REDIRECT_RULE_ID = 20009;
2728
export const NEWTAB_NO_AI_PARAM_RULE_ID = 20011;
2829

@@ -50,6 +51,7 @@ const RESERVED_DYNAMIC_RULE_IDS = [
5051
ATB_PARAM_RULE_ID,
5152
ATB_EXTENSIONINSTALLED_RULE_ID,
5253
NEWTAB_TRACKER_STATS_REDIRECT_RULE_ID,
54+
NEWTAB_BLOCKED_STATS_REDIRECT_RULE_ID,
5355
SEARCH_REDIRECT_RULE_ID,
5456
HOME_PAGE_RULE_ID,
5557
NEWTAB_NO_AI_PARAM_RULE_ID,

‎shared/js/background/newtab-tracker-stats.js‎

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import settings from './settings';
66
import { emitter, TrackerBlockedEvent } from './before-request.js';
77
import { generateDNRRule } from '@duckduckgo/ddg2dnr/lib/utils';
88
import { NEWTAB_TRACKER_STATS_REDIRECT_PRIORITY } from '@duckduckgo/ddg2dnr/lib/rulePriorities';
9-
import { NEWTAB_TRACKER_STATS_REDIRECT_RULE_ID } from './dnr-utils';
9+
import { NEWTAB_BLOCKED_STATS_REDIRECT_RULE_ID, NEWTAB_TRACKER_STATS_REDIRECT_RULE_ID } from './dnr-utils';
1010

1111
/**
1212
* @typedef {import('./settings.js')} Settings
@@ -373,11 +373,10 @@ export class NewTabTrackerStats {
373373
* Respond to requests for `tracker-stats.html` - if we determine
374374
* that the request was from an allowed origin, re-direct the
375375
* request to the web_accessible_resource file 'html/tracker-stats.html'
376-
*
377-
* @param details
378376
*/
379377
export function mv2Redirect() {
380-
const incomingUrl = new URL(constants.trackerStats.allowedPathname, constants.trackerStats.allowedOrigin);
378+
const { allowedOrigin, allowedPathname, legacyPathname, redirectTarget } = constants.trackerStats;
379+
const pathnames = [allowedPathname, legacyPathname];
381380
/**
382381
* This listener will redirect the request for tracker-stats.html
383382
* on the new tab page to our own HTML file under `web_accessible_resources`
@@ -387,39 +386,45 @@ export function mv2Redirect() {
387386
// Only do the redirect if we're being iframed into a known origin
388387
if (details.type === 'sub_frame') {
389388
const parsed = new URL(details.url);
390-
if (parsed.origin === constants.trackerStats.allowedOrigin) {
391-
if (parsed.pathname.includes(constants.trackerStats.allowedPathname)) {
389+
if (parsed.origin === allowedOrigin) {
390+
if (pathnames.some((pathname) => parsed.pathname.includes(pathname))) {
392391
return {
393-
redirectUrl: chrome.runtime.getURL(constants.trackerStats.redirectTarget),
392+
redirectUrl: chrome.runtime.getURL(redirectTarget),
394393
};
395394
}
396395
}
397396
}
398397
return undefined;
399398
},
400399
{
401-
urls: [incomingUrl.toString()],
400+
urls: pathnames.map((pathname) => new URL(pathname, allowedOrigin).toString()),
402401
types: ['sub_frame'],
403402
},
404403
['blocking'],
405404
);
406405
}
407406

408407
function mv3Redirect() {
409-
const targetUrl = chrome.runtime.getURL(constants.trackerStats.redirectTarget);
410-
const incomingUrl = new URL(constants.trackerStats.allowedPathname, constants.trackerStats.allowedOrigin);
411-
const redirectRule = generateDNRRule({
412-
id: NEWTAB_TRACKER_STATS_REDIRECT_RULE_ID,
413-
priority: NEWTAB_TRACKER_STATS_REDIRECT_PRIORITY,
414-
actionType: 'redirect',
415-
redirect: {
416-
url: targetUrl,
417-
},
418-
urlFilter: incomingUrl.toString(),
419-
resourceTypes: ['sub_frame'],
420-
});
408+
const { allowedOrigin, allowedPathname, legacyPathname, redirectTarget } = constants.trackerStats;
409+
const targetUrl = chrome.runtime.getURL(redirectTarget);
410+
const redirectRules = [
411+
{ id: NEWTAB_BLOCKED_STATS_REDIRECT_RULE_ID, pathname: allowedPathname },
412+
{ id: NEWTAB_TRACKER_STATS_REDIRECT_RULE_ID, pathname: legacyPathname },
413+
].map(({ id, pathname }) =>
414+
generateDNRRule({
415+
id,
416+
priority: NEWTAB_TRACKER_STATS_REDIRECT_PRIORITY,
417+
actionType: 'redirect',
418+
redirect: {
419+
url: targetUrl,
420+
},
421+
urlFilter: new URL(pathname, allowedOrigin).toString(),
422+
resourceTypes: ['sub_frame'],
423+
requestDomains: [new URL(allowedOrigin).hostname],
424+
}),
425+
);
421426
chrome.declarativeNetRequest.updateDynamicRules({
422-
removeRuleIds: [redirectRule.id],
423-
addRules: [redirectRule],
427+
removeRuleIds: redirectRules.map((rule) => rule.id),
428+
addRules: redirectRules,
424429
});
425430
}

0 commit comments

Comments
 (0)