Skip to content

Commit

Permalink
support permissions modifier for compiler validation
Browse files Browse the repository at this point in the history
  • Loading branch information
slavaleleka committed Nov 8, 2023
1 parent 43659dc commit 32b115a
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 19 deletions.
9 changes: 9 additions & 0 deletions packages/tsurlfilter/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
<!-- e.g. [1.0.77]: https://github.com/AdguardTeam/tsurlfilter/compare/tsurlfilter-v1.0.76...tsurlfilter-v1.0.77 -->


## [2.2.4] - 2023-11-08

### Added

- Support of `$permissions` modifier for compiler validation
[#66](https://github.com/AdguardTeam/tsurlfilter/issues/66)


## [2.2.3] - 2023-11-03

### Added

- Support of `$referrerpolicy` modifier for compiler validation
[#191](https://github.com/AdguardTeam/FiltersCompiler/issues/191)


## [2.2.2] - 2023-10-17
Expand Down
1 change: 1 addition & 0 deletions packages/tsurlfilter/src/rules/network-rule-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export const NETWORK_RULE_OPTIONS = {
CTAG: 'ctag',
METHOD: 'method',
TO: 'to',
PERMISSIONS: 'permissions',
ALL: 'all',
};

Expand Down
50 changes: 31 additions & 19 deletions packages/tsurlfilter/src/rules/network-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,41 +73,44 @@ export enum NetworkRuleOption {
// Other modifiers

/** $popup modifier */
Popup = 1 << 14,
Popup = 1 << 12,
/** $csp modifier */
Csp = 1 << 15,
Csp = 1 << 13,
/** $replace modifier */
Replace = 1 << 16,
Replace = 1 << 14,
/** $cookie modifier */
Cookie = 1 << 17,
Cookie = 1 << 15,
/** $redirect modifier */
Redirect = 1 << 18,
Redirect = 1 << 16,
/** $badfilter modifier */
Badfilter = 1 << 19,
Badfilter = 1 << 17,
/** $removeparam modifier */
RemoveParam = 1 << 20,
RemoveParam = 1 << 18,
/** $removeheader modifier */
RemoveHeader = 1 << 21,
RemoveHeader = 1 << 19,
/** $jsonprune modifier */
JsonPrune = 1 << 22,
JsonPrune = 1 << 20,
/** $hls modifier */
Hls = 1 << 23,
Hls = 1 << 21,

// Compatibility dependent
/** $network modifier */
Network = 1 << 24,
Network = 1 << 22,

/** dns modifiers */
Client = 1 << 25,
DnsRewrite = 1 << 26,
DnsType = 1 << 27,
Ctag = 1 << 28,
Client = 1 << 23,
DnsRewrite = 1 << 24,
DnsType = 1 << 25,
Ctag = 1 << 26,

// $method modifier
Method = 1 << 30,
/* $method modifier */
Method = 1 << 27,

// $to modifier
To = 1 << 31,
/* $to modifier */
To = 1 << 28,

/* $permissions modifier */
Permissions = 1 << 29,

// Groups (for validation)

Expand Down Expand Up @@ -1460,6 +1463,15 @@ export class NetworkRule implements rule.IRule {
this.setOptionEnabled(NetworkRuleOption.RemoveHeader, true);
this.advancedModifier = new RemoveHeaderModifier(optionValue, this.isAllowlist());
break;
// $permissions
case OPTIONS.PERMISSIONS:
// simple validation of permissions rules for compiler.
// should be fully supported in tsurlfilter v2.3 and the browser extension v4.4. AG-17467
if (isCompatibleWith(CompatibilityTypes.Extension)) {
throw new SyntaxError('Extension does not support $permissions modifier yet');
}
this.setOptionEnabled(NetworkRuleOption.Permissions, true);
break;
// $jsonprune
// simple validation of jsonprune rules for compiler
// https://github.com/AdguardTeam/FiltersCompiler/issues/168
Expand Down
6 changes: 6 additions & 0 deletions packages/tsurlfilter/test/rules/rule-converter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,12 @@ describe('Options', () => {
checkConversionResult(rule, rule);
});

it('converts permissions option properly', () => {
const rule = '||example.org^$permissions=geolocation=()';
// converted rule should be the same as original
checkConversionResult(rule, rule);
});

it('converts hls option properly', () => {
const rule = String.raw`||example.org^$hls=/#UPLYNK-SEGMENT:.*\,ad/t`;
// converted rule should be the same as original
Expand Down
8 changes: 8 additions & 0 deletions packages/tsurlfilter/test/rules/rule-validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ describe('RuleValidator', () => {
expect(RuleValidator.validate(ruleText).valid).toBeTruthy();
});

it('validates permissions modifier', () => {
// TODO: remove following comment and setConfiguration() after the implementation
// $permissions is not implemented for extension yet
setConfiguration({ compatibility: CompatibilityTypes.CoreLibs });
const ruleText = '||example.org^$permissions=geolocation=()';
expect(RuleValidator.validate(ruleText).valid).toBeTruthy();
});

it('validates referrerpolicy modifier', () => {
// $referrerpolicy is not supported by the extension
setConfiguration({ compatibility: CompatibilityTypes.CoreLibs });
Expand Down

0 comments on commit 32b115a

Please sign in to comment.