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

fix: network filter ending with right hat recognized as regex #3871

Closed
Closed
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
20 changes: 19 additions & 1 deletion packages/adblocker/src/filters/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
HASH_INTERNAL_MULT,
} from '../utils';
import IFilter from './interface';
import { parse } from 'tldts-experimental';

const HTTP_HASH = fastHash('http');
const HTTPS_HASH = fastHash('https');
Expand Down Expand Up @@ -838,7 +839,6 @@ export default class NetworkFilter implements IFilter {

// TODO
// - ignore hostname anchor is not hostname provided

if (hostname !== undefined) {
hostname = hostname.toLowerCase();
if (hasUnicode(hostname)) {
Expand Down Expand Up @@ -1535,11 +1535,29 @@ function setNetworkMask(mask: number, m: number, value: boolean): number {
return clearBit(mask, m);
}

function checkIsHostname(filter: string, start: number, end: number): boolean {
const range = filter.slice(start, end);

return (
parse(range, {
extractHostname: false,
}).hostname ===
parse(range, {
extractHostname: true,
}).hostname
);
}

/**
* Check if the sub-string contained between the indices start and end is a
* regex filter (it contains a '*' or '^' char).
*/
function checkIsRegex(filter: string, start: number, end: number): boolean {
// Check if the filter has a trailing hat without pathes (mostly for hostname) and then trim it
if (filter.charCodeAt(end - 1) === 94 /* '^' */ && checkIsHostname(filter, start, end - 1)) {
return false;
}

const indexOfSeparator = filter.indexOf('^', start);
if (indexOfSeparator !== -1 && indexOfSeparator < end) {
return true;
Expand Down
12 changes: 7 additions & 5 deletions packages/adblocker/test/parsing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ describe('Network filters', () => {
checkToString('@@||foo.com|', '@@||foo.com^');
checkToString('|foo.com|', '|foo.com|');
checkToString('foo.com|', 'foo.com|');
checkToString('foo.com^', 'foo.com^');
});

it('pprint domain', () => {
Expand Down Expand Up @@ -219,6 +220,12 @@ describe('Network filters', () => {
filter: 'foo.com/ads',
isImportant: true,
});

network('*bar^', {
...base,
filter: 'bar^',
hostname: '',
});
});

it('parses ||pattern', () => {
Expand Down Expand Up @@ -345,11 +352,6 @@ describe('Network filters', () => {
isRegex: true,
};

network('*bar^', {
...base,
filter: 'bar^',
hostname: '',
});
network('foo.com/*bar^', {
...base,
filter: 'foo.com/*bar^',
Expand Down
Loading