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
15 changes: 14 additions & 1 deletion packages/adblocker/src/filters/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -725,14 +725,27 @@ 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);
for (const part of value.split('|')) {
if (option === 'to') {
if (part.charCodeAt(0) === 126 /* '~' */) {
denyallowEntities.add(part.slice(1));
} else {
denyallowEntities.add(`~${part}`);
}
} else {
denyallowEntities.add(part);
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this would be best handled inside of Domains directly. Maybe with a flag?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second argument of "Domain" is "debug = false". I don't know if it's still straightforward to have a third argument after this. There's no reason not to do that but need to find how to do that cleanly.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding an argument is only one way to go about it that does not change the main comment. You can also use static method, etc.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is duplicate of #4525 which is already being handled. I don't know which will be merged first but I believe we can put this off for a while.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found that we depend on another variable called denyallowEntities which is Set<string>. Alternative could be using string[] and use Set at the end to pull duplicates out. However, I don't feel it'll impose a low complexity.

denyallow = Domains.parse(Array.from(denyallowEntities), debug);
break;
}
case 'domain':
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