Skip to content

feat: to network modifier #4524

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

Merged
merged 15 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from 5 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
26 changes: 23 additions & 3 deletions packages/adblocker/src/filters/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export const enum NETWORK_FILTER_MASK {
isHostnameAnchor = 1 << 28,
isRedirectRule = 1 << 29,
isRedirect = 1 << 30,
isTo = 1 << 31,
// IMPORTANT: the mask is now full, no more options can be added
// Consider creating a separate fitler type for isReplace if a new
// network filter option is needed.
Expand Down Expand Up @@ -731,8 +732,19 @@ export default class NetworkFilter implements IFilter {
const value = rawOption[1];

switch (option) {
case 'to':
case 'denyallow': {
denyallow = Domains.parse(value.split('|'), debug);
if (denyallow !== undefined) {
return null;
}
let parts = value.split('|');
if (option === 'to') {
mask = setBit(mask, NETWORK_FILTER_MASK.isTo);
parts = parts.map((part) =>
part.charCodeAt(0) === 126 /* '~' */ ? part.slice(1) : `~${part}`,
);
}
denyallow = Domains.parse(parts, debug);
break;
}
case 'domain':
Expand Down Expand Up @@ -1524,10 +1536,14 @@ export default class NetworkFilter implements IFilter {
}

if (this.denyallow !== undefined) {
const optionName = this.isTo() ? 'to' : 'denyallow';
if (this.denyallow.parts !== undefined) {
options.push(`denyallow=${this.denyallow.parts}`);
const parts = this.denyallow.parts.split(',');
options.push(
`${optionName}=${this.isTo() ? parts.map((part) => (part.charCodeAt(0) === 126 /* '~' */ ? part.slice(1) : `~${part}`)).join('|') : parts.join('|')}`,
);
} else {
options.push('denyallow=<hashed>');
options.push(`${optionName}=<hashed>`);
}
}

Expand Down Expand Up @@ -1818,6 +1834,10 @@ export default class NetworkFilter implements IFilter {
return getBit(this.mask, NETWORK_FILTER_MASK.isBadFilter);
}

public isTo() {
return getBit(this.mask, NETWORK_FILTER_MASK.isTo);
}

public isUnicode() {
return getBit(this.mask, NETWORK_FILTER_MASK.isUnicode);
}
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
19 changes: 19 additions & 0 deletions packages/adblocker/test/parsing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,25 @@ describe('Network filters', () => {
denyallow: undefined,
});
});

context('to', () => {
it('fails when both denyallow and to used', () => {
network('||foo.com$denyallow=foo.com,to=bar.com', null);
network('||foo.com$to=foo.com,denyallow=bar.com', null);
});

it('reverses domains condition', () => {
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