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

feat: to network modifier #4524

Merged
merged 15 commits into from
Jan 24, 2025
Merged
13 changes: 11 additions & 2 deletions packages/adblocker/src/filters/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -725,14 +725,23 @@ export default class NetworkFilter implements IFilter {
// --------------------------------------------------------------------- //
// parseOptions
// --------------------------------------------------------------------- //
const denyallowEntities: Set<string> = new Set();
for (const rawOption of getFilterOptions(line, optionsIndex + 1, line.length)) {
const negation = rawOption[0].charCodeAt(0) === 126; /* '~' */
const option = negation === true ? rawOption[0].slice(1) : rawOption[0];
const value = rawOption[1];

switch (option) {
case 'to':
case 'denyallow': {
denyallow = Domains.parse(value.split('|'), debug);
let parts = value.split('|');
if (option === 'to') {
parts = parts.map((part) =>
part.charCodeAt(0) === 126 /* '~' */ ? part.slice(1) : `~${part}`,
seia-soto marked this conversation as resolved.
Show resolved Hide resolved
);
}
parts.forEach((part) => denyallowEntities.add(part));
seia-soto marked this conversation as resolved.
Show resolved Hide resolved
denyallow = Domains.parse(Array.from(denyallowEntities), debug);
break;
}
case 'domain':
Expand Down Expand Up @@ -1525,7 +1534,7 @@ export default class NetworkFilter implements IFilter {

if (this.denyallow !== undefined) {
if (this.denyallow.parts !== undefined) {
options.push(`denyallow=${this.denyallow.parts}`);
options.push(`denyallow=${this.denyallow.parts.replace(/,/g, '|')}`);
seia-soto marked this conversation as resolved.
Show resolved Hide resolved
} else {
options.push('denyallow=<hashed>');
}
Expand Down
17 changes: 17 additions & 0 deletions packages/adblocker/test/matching.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,23 @@ describe('#NetworkFilter.match', () => {
url: 'https://sub.y.com/bar',
type: 'script',
});

// to
expect(f`*$3p,from=a.com,to=b.com`).to.matchRequest({
sourceUrl: 'https://a.com',
url: 'https://b.com/bar',
type: 'script',
});
expect(f`*$frame,3p,from=a.com|b.com,to=~c.com`).to.not.matchRequest({
sourceUrl: 'https://a.com',
url: 'https://c.com/bar',
type: 'sub_frame',
});
expect(f`$frame,csp=non-relevant,to=~safe.com,from=c.com|d.com`).to.not.matchRequest({
sourceUrl: 'https://c.com',
url: 'https://safe.com/foo',
type: 'sub_frame',
});
});
});

Expand Down
23 changes: 23 additions & 0 deletions packages/adblocker/test/parsing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,29 @@ describe('Network filters', () => {
denyallow: undefined,
});
});

context('to', () => {
it('reverses domains condition', () => {
network('||foo.com$to=foo.com|~bar.com,denyallow=bar.com|~foo.com', {
denyallow: {
hostnames: h(['bar.com']),
entities: undefined,
notHostnames: h(['foo.com']),
notEntities: undefined,
parts: undefined,
},
});
network('||foo.com$to=bar.com|baz.com', {
denyallow: {
hostnames: undefined,
entities: undefined,
notHostnames: h(['bar.com', 'baz.com']),
notEntities: undefined,
parts: undefined,
},
});
});
});
});

describe('redirect', () => {
Expand Down
Loading