Skip to content

Commit

Permalink
Pull request #716: AG-13420 Re-add wildcard in AllowList
Browse files Browse the repository at this point in the history
Merge in ADGUARD-FILTERS/tsurlfilter from feature/AG-13420 to master

Squashed commit of the following:

commit 802a427
Author: Vladimir Zhelvis <[email protected]>
Date:   Wed Nov 15 11:33:08 2023 +0300

    update changelog

commit cdab6ce
Merge: 07c9dc9 85e0fcf
Author: Vladimir Zhelvis <[email protected]>
Date:   Wed Nov 15 11:30:21 2023 +0300

    Merge branch 'master' into feature/AG-13420

commit 07c9dc9
Author: Vladimir Zhelvis <[email protected]>
Date:   Wed Nov 15 11:22:46 2023 +0300

    delete tsurlfilter diff that not related to the task

commit 76aff71
Author: Vladimir Zhelvis <[email protected]>
Date:   Wed Nov 15 11:18:10 2023 +0300

    add allowlist wildcard support

commit 6dc7b02
Merge: 636ed0a 865ff8a
Author: Vladimir Zhelvis <[email protected]>
Date:   Mon Nov 13 13:03:55 2023 +0300

    Merge branch 'master' into feature/AG-13420

commit 636ed0a
Author: Vladimir Zhelvis <[email protected]>
Date:   Fri Nov 3 16:02:38 2023 +0300

    fix one bug, add another

commit 3cdb90f
Author: Vladimir Zhelvis <[email protected]>
Date:   Fri Nov 3 16:00:38 2023 +0300

    fix one bug, add another

commit 6b6080c
Author: Dmitriy Seregin <[email protected]>
Date:   Fri Nov 3 16:15:19 2023 +0400

    AG-13420: support wildcard in allowlist
  • Loading branch information
zhelvis committed Nov 15, 2023
1 parent 85e0fcf commit 2b9aa9b
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 13 deletions.
6 changes: 6 additions & 0 deletions packages/tswebextension/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
<!-- e.g. [0.1.2]: https://github.com/AdguardTeam/tsurlfilter/compare/tswebextension-v0.1.1...tswebextension-v0.1.2 -->


## [Unreleased]

### Added
- Allowlist wildcard support [#2020](https://github.com/AdguardTeam/AdguardBrowserExtension/issues/2020).


## [0.4.4] - 2023-11-13

### Changed
Expand Down
29 changes: 26 additions & 3 deletions packages/tswebextension/src/lib/mv2/background/allowlist.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NetworkRule, StringRuleList, SimpleRegex } from '@adguard/tsurlfilter';
import { NetworkRule, StringRuleList } from '@adguard/tsurlfilter';

import { ALLOWLIST_FILTER_ID } from '../../common/constants';
import type { Configuration } from '../../common/configuration';
Expand Down Expand Up @@ -82,10 +82,33 @@ export class Allowlist {
* Creates rule string based on specified domain.
*
* @param domain Allowlisted domain.
*
* @returns Allowlist rule string.
*/
private static createAllowlistRuleString(domain: string): string {
const escapedDomain = SimpleRegex.escapeRegexSpecials(domain);
return String.raw`@@///(www\.)?${escapedDomain}/$document,important`;
// Special case for wildcard tld + n domain.
if (domain.startsWith('*.')) {
return String.raw`@@||${domain.slice(2)}$document,important`;
}

// In other cases we use regexp to match domain and it`s 'www' subdomain strictly.
let regexp = '';

// transform allowlist domain special characters
for (let i = 0; i < domain.length; i += 1) {
const char = domain[i];

// transform wildcard to regexp equivalent
if (char === '*') {
regexp += '.*';
// escape domain separator
} else if (char === '.') {
regexp += String.raw`\.`;
} else {
regexp += char;
}
}

return String.raw`@@///(www\.)?${regexp}/$document,important`;
}
}
63 changes: 53 additions & 10 deletions packages/tswebextension/test/lib/mv2/background/allowlist.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { StringRuleList, NetworkRule } from '@adguard/tsurlfilter';
import { StringRuleList } from '@adguard/tsurlfilter';
import { Allowlist } from '@lib/mv2/background/allowlist';
import { ALLOWLIST_FILTER_ID } from '@lib/common/constants';
import { getConfigurationMv2Fixture } from './fixtures/configuration';
Expand Down Expand Up @@ -79,17 +79,60 @@ describe('Allowlist Api', () => {
});

describe('static createAllowlistRule method', () => {
it('should return allowlist rule, when domain is specified', () => {
expect(Allowlist.createAllowlistRule('example.com')).toStrictEqual(
new NetworkRule(
String.raw`@@///(www\.)?example\.com/$document,important`,
ALLOWLIST_FILTER_ID,
),
);
});

it('should return null, when domain is empty', () => {
expect(Allowlist.createAllowlistRule('')).toBeNull();
});

describe('should return network rule for valid allowlist domain', () => {
const testCases = [
{
domain: 'example.com',
expected: String.raw`@@///(www\.)?example\.com/$document,important`,
},
{
domain: 'sub.example.com',
expected: String.raw`@@///(www\.)?sub\.example\.com/$document,important`,
},
{
domain: 'example.*',
expected: String.raw`@@///(www\.)?example\..*/$document,important`,
},
{
domain: 'example.*.com',
expected: String.raw`@@///(www\.)?example\..*\.com/$document,important`,
},
{
domain: '*.example.com',
expected: String.raw`@@||example.com$document,important`,
},
{
domain: '*.sub.example.*',
expected: String.raw`@@||sub.example.*$document,important`,
},
{
domain: '*example.com',
expected: String.raw`@@///(www\.)?.*example\.com/$document,important`,
},
{
domain: 'example*.com',
expected: String.raw`@@///(www\.)?example.*\.com/$document,important`,
},
{
domain: 'exam*ple.com',
expected: String.raw`@@///(www\.)?exam.*ple\.com/$document,important`,
},
{
domain: '*.example*.com',
expected: String.raw`@@||example*.com$document,important`,
},
];

it.each(testCases)('should return $expected rule for $domain', ({ domain, expected }) => {
const rule = Allowlist.createAllowlistRule(domain)!;

expect(rule.getText()).toBe(expected);
expect(rule.getFilterListId()).toBe(ALLOWLIST_FILTER_ID);
});
});
});
});

0 comments on commit 2b9aa9b

Please sign in to comment.