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: check domains validity when parsing network filters #4525

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
8 changes: 7 additions & 1 deletion packages/adblocker/src/engine/domains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ import { binLookup, hasUnicode, HASH_INTERNAL_MULT } from '../utils.js';

export class Domains {
public static parse(parts: string[], debug: boolean = false): Domains | undefined {
if (parts.length === 0) {
if (
parts.length === 0 ||
// We accept parts after splitting a string by `|`
// Empty first and last entry indicates it's invalid
parts[0].length === 0 ||
parts[parts.length - 1].length === 0
) {
return undefined;
}

Expand Down
16 changes: 7 additions & 9 deletions packages/adblocker/src/filters/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -733,19 +733,17 @@ export default class NetworkFilter implements IFilter {
switch (option) {
case 'denyallow': {
denyallow = Domains.parse(value.split('|'), debug);
if (denyallow === undefined) {
return null;
}
break;
}
case 'domain':
case 'from': {
// domain list starting or ending with '|' is invalid
if (
value.charCodeAt(0) === 124 /* '|' */ ||
value.charCodeAt(value.length - 1) === 124 /* '|' */
) {
domains = Domains.parse(value.split('|'), debug);
if (domains === undefined) {
return null;
}

domains = Domains.parse(value.split('|'), debug);
break;
}
case 'badfilter':
Expand Down Expand Up @@ -1517,15 +1515,15 @@ export default class NetworkFilter implements IFilter {

if (this.domains !== undefined) {
if (this.domains.parts !== undefined) {
options.push(`domain=${this.domains.parts}`);
options.push(`domain=${this.domains.parts.replace(/,/g, '|')}`);
} else {
options.push('domain=<hashed>');
}
}

if (this.denyallow !== undefined) {
if (this.denyallow.parts !== undefined) {
options.push(`denyallow=${this.denyallow.parts}`);
options.push(`denyallow=${this.denyallow.parts.replace(/,/g, '|')}`);
} else {
options.push('denyallow=<hashed>');
}
Expand Down
Loading