Skip to content

Commit

Permalink
Fix source url check for updating stats on Firefox (#1424)
Browse files Browse the repository at this point in the history
  • Loading branch information
smalluban authored Feb 13, 2024
1 parent 4d0d4a5 commit 7718b76
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 19 deletions.
3 changes: 1 addition & 2 deletions extension-manifest-v3/src/background/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down Expand Up @@ -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: [],
});
Expand Down
56 changes: 39 additions & 17 deletions extension-manifest-v3/src/background/utils/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,33 @@
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';
const sourceUrl = isMainFrame
? 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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7718b76

Please sign in to comment.