From 7718b761b605ce859f7c9c6167af7c3be1a91ae8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Luba=C5=84ski?= Date: Tue, 13 Feb 2024 15:33:46 +0100 Subject: [PATCH] Fix source url check for updating stats on Firefox (#1424) --- extension-manifest-v3/src/background/stats.js | 3 +- .../src/background/utils/request.js | 56 +++++++++++++------ 2 files changed, 40 insertions(+), 19 deletions(-) diff --git a/extension-manifest-v3/src/background/stats.js b/extension-manifest-v3/src/background/stats.js index ad85ed16c..af9234b7b 100644 --- a/extension-manifest-v3/src/background/stats.js +++ b/extension-manifest-v3/src/background/stats.js @@ -129,7 +129,7 @@ export function updateTabStats(tabId, requests) { // Filter out requests that are not related to the current page // (e.g. requests on trailing edge when navigation to a new page is in progress) - requests = requests.filter((request) => request.isFromOriginUrl(stats.url)); + requests = requests.filter((request) => request.isFromDomain(stats.domain)); for (const request of requests) { const pattern = await trackerDb.getMetadata(request, { @@ -241,7 +241,6 @@ function setupTabStats(tabId, request) { if (request.isHttp || request.isHttps) { tabStats.set(tabId, { - url: request.url, domain: request.domain || request.hostname, trackers: [], }); diff --git a/extension-manifest-v3/src/background/utils/request.js b/extension-manifest-v3/src/background/utils/request.js index c94c5f6fc..43c90b63e 100644 --- a/extension-manifest-v3/src/background/utils/request.js +++ b/extension-manifest-v3/src/background/utils/request.js @@ -12,6 +12,24 @@ import { Request } from '@cliqz/adblocker'; import { parse } from 'tldts-experimental'; +const PARSE_CACHE_LIMIT = 1000; +const parseCache = new Map(); + +function parseWithCache(url) { + if (parseCache.has(url)) { + return parseCache.get(url); + } + + if (parseCache.size > PARSE_CACHE_LIMIT) { + parseCache.clear(); + } + + const parsed = parse(url); + parseCache.set(url, parsed); + + return parsed; +} + export default class ExtendedRequest extends Request { static fromRequestDetails(details) { const isMainFrame = details.type === 'main_frame'; @@ -19,8 +37,8 @@ export default class ExtendedRequest extends Request { ? details.url : details.originUrl || details.documentUrl || ''; - const parsedUrl = parse(details.url); - const parsedSourceUrl = isMainFrame ? parsedUrl : parse(sourceUrl); + const parsedUrl = parseWithCache(details.url); + const parsedSourceUrl = isMainFrame ? parsedUrl : parseWithCache(sourceUrl); return new ExtendedRequest({ requestId: details.requestId, @@ -53,27 +71,31 @@ export default class ExtendedRequest extends Request { this.sourceHostname = data.sourceHostname; } - isFromOriginUrl(url) { + isFromDomain(domain) { const { frameAncestors } = this._originalRequestDetails; - /* Firefox APIs */ + let url = ''; + /* Firefox APIs */ if (frameAncestors && frameAncestors.length > 0) { - return url === frameAncestors[frameAncestors.length - 1].url; + url = frameAncestors[frameAncestors.length - 1].url; + } else if (this.sourceUrl) { + url = this.sourceUrl; + } else { + /* Chrome APIs */ + + const { frameType, initiator } = this._originalRequestDetails; + + // For frameType 'sub_frame', we can't determine the origin URL + // as it might be the iframe itself or any of its ancestors + if (frameType === 'outermost_frame' && initiator) { + url = initiator; + } } - if (this.sourceUrl) { - return url === this.sourceUrl; - } - - /* Chrome APIs */ - - const { frameType, initiator } = this._originalRequestDetails; - - // For frameType 'sub_frame', we can't determine the origin URL - // as it might be the iframe itself or any of its ancestors - if (frameType === 'outermost_frame' && initiator) { - return url.startsWith(initiator); + if (url) { + const parsedUrl = parseWithCache(url); + return parsedUrl.domain === domain || parsedUrl.hostname === domain; } // As a fallback, we assume that the request is from the origin URL