Skip to content

Commit

Permalink
AG-37186 $removeparam fails to match encoded URL params in MV2
Browse files Browse the repository at this point in the history
Merge in ADGUARD-FILTERS/tsurlfilter from fix/AG-37186 to master

Squashed commit of the following:

commit 26819b3
Author: Kurbanali Ruslan <r.kurbanali@adguard.com>
Date:   Mon Nov 18 16:26:58 2024 +0500

    updated changelog and bump version

commit 63b23c9
Author: Kurbanali Ruslan <r.kurbanali@adguard.com>
Date:   Mon Nov 18 16:21:55 2024 +0500

    reverted tgz

commit 3c6dd35
Author: Kurbanali Ruslan <r.kurbanali@adguard.com>
Date:   Thu Nov 14 10:47:37 2024 +0500

    updated comment

commit b28aee1
Author: Kurbanali Ruslan <r.kurbanali@adguard.com>
Date:   Wed Nov 13 17:04:01 2024 +0500

    updated tgz

commit 9febff1
Author: Kurbanali Ruslan <r.kurbanali@adguard.com>
Date:   Wed Nov 13 16:49:34 2024 +0500

    added changelog

commit 6afa503
Author: Kurbanali Ruslan <r.kurbanali@adguard.com>
Date:   Wed Nov 13 16:48:58 2024 +0500

    added comment

commit e747993
Author: Kurbanali Ruslan <r.kurbanali@adguard.com>
Date:   Wed Nov 13 16:35:48 2024 +0500

    added test cases

commit 48c5f0e
Author: Kurbanali Ruslan <r.kurbanali@adguard.com>
Date:   Wed Nov 13 16:35:43 2024 +0500

    fixed remove param not matching
  • Loading branch information
kurrx committed Nov 19, 2024
1 parent c7ade46 commit 466a62d
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 7 deletions.
9 changes: 9 additions & 0 deletions packages/tsurlfilter/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
<!-- TODO: manually add compare links for version changes -->
<!-- e.g. [1.0.77]: https://github.com/AdguardTeam/tsurlfilter/compare/tsurlfilter-v1.0.76...tsurlfilter-v1.0.77 -->

## [3.0.7] - 2024-11-18

### Fixed

- `$removeparam` fails to match encoded URL params in MV2 [AdguardBrowserExtension#3015].

[AdguardBrowserExtension#3015]: https://github.com/AdguardTeam/AdguardBrowserExtension/issues/3015
[3.0.7]: https://github.com/AdguardTeam/tsurlfilter/releases/tag/tsurlfilter-v3.0.7

## [3.0.6] - 2024-11-02

### Changed
Expand Down
2 changes: 1 addition & 1 deletion packages/tsurlfilter/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@adguard/tsurlfilter",
"version": "3.0.6",
"version": "3.0.7",
"description": "This is a TypeScript library that implements AdGuard's content blocking rules",
"main": "dist/es/index.js",
"module": "dist/es/index.js",
Expand Down
10 changes: 7 additions & 3 deletions packages/tsurlfilter/src/utils/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,23 @@ export function cleanUrlParamByRegExp(url: string, regExp: RegExp, invert = fals

const split = splitUrl(url);

/**
* We are checking both regular param and decoded param, in case if regexp
* contains decoded params and url contains encoded params:
* https://github.com/AdguardTeam/AdguardBrowserExtension/issues/3015
*/
let modifiedQuery;
if (invert) {
modifiedQuery = split.query
.split('&')
.filter((x) => x)
.filter((x) => x && x.match(regExp))
.filter((x) => x && (x.match(regExp) || decodeURIComponent(x).match(regExp)))
.join('&');
} else {
modifiedQuery = split.query
.split('&')
.filter((x) => {
const test = x.includes('=') ? x : `${x}=`;
return !test.match(regExp);
return !test.match(regExp) && !decodeURIComponent(test).match(regExp);
})
.join('&');
}
Expand Down
6 changes: 4 additions & 2 deletions packages/tsurlfilter/test/modifiers/advanced-modifier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ describe('NetworkRule - removeparam rules', () => {
});

it('works if query parameters are correctly filtered with regexp', () => {
const rule = createNetworkRule('$removeparam=/p1|p2|p3|p4_.*/i', 0);
const rule = createNetworkRule('$removeparam=/p1|p2|p3|p4_|\\$p5.*/i', 0);
const modifier = rule.getAdvancedModifier() as RemoveParamModifier;

const comPage = 'http://example.com/page';
Expand All @@ -366,6 +366,7 @@ describe('NetworkRule - removeparam rules', () => {
expect(modifier.removeParameters(`${comPage}?p0=0&p4_=4`)).toBe(`${comPage}?p0=0`);
expect(modifier.removeParameters(`${comPage}?p0=0&p4=4`)).toBe(`${comPage}?p0=0&p4=4`);
expect(modifier.removeParameters(`${comPage}?p0=0&p4_1=4`)).toBe(`${comPage}?p0=0`);
expect(modifier.removeParameters(`${comPage}?p0=0&%24p5=0&$p5=0&p5=0`)).toBe(`${comPage}?p0=0&p5=0`);
});

it('works if query parameters are removed by naked rule', () => {
Expand All @@ -390,14 +391,15 @@ describe('NetworkRule - removeparam rules', () => {
});

it('works if inverted regex removeparam is applied correctly', () => {
const rule = createNetworkRule('$removeparam=~/p0.*/', 0);
const rule = createNetworkRule('$removeparam=~/p0|\\$p4.*/', 0);
const modifier = rule.getAdvancedModifier() as RemoveParamModifier;

const comPage = 'http://example.com/page';
expect(modifier.removeParameters(`${comPage}`)).toBe(`${comPage}`);
expect(modifier.removeParameters(`${comPage}?p0=0`)).toBe(`${comPage}?p0=0`);
expect(modifier.removeParameters(`${comPage}?p0=0&p1=1`)).toBe(`${comPage}?p0=0`);
expect(modifier.removeParameters(`${comPage}?p01=0&p1=1&p2=2&p3=3`)).toBe(`${comPage}?p01=0`);
expect(modifier.removeParameters(`${comPage}?p0=0&p1=2&p2=2&p3=3&%24p4=0&$p4=0&p4=0`)).toBe(`${comPage}?p0=0&%24p4=0&$p4=0`);
});

it('does not remove unmatched parameters', () => {
Expand Down
7 changes: 7 additions & 0 deletions packages/tsurlfilter/test/utils/url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,11 @@ describe('Query parameters', () => {
expect(utils.cleanUrlParamByRegExp('http://example.com?test=1&test=2', /test.*/, true)).toEqual('http://example.com?test=1&test=2');
expect(utils.cleanUrlParamByRegExp('http://example.com?test=1&test=2', /test=1/, true)).toEqual('http://example.com?test=1');
});

it('handles properly encoded params', () => {
expect(utils.cleanUrlParamByRegExp('http://example.com?$test', /\$test.*/)).toEqual('http://example.com');
expect(utils.cleanUrlParamByRegExp('http://example.com?%24test', /\$test.*/)).toEqual('http://example.com');
expect(utils.cleanUrlParamByRegExp('http://example.com?$test', /\$test.*/, true)).toEqual('http://example.com?$test');
expect(utils.cleanUrlParamByRegExp('http://example.com?%24test', /\$test.*/, true)).toEqual('http://example.com?%24test');
});
});
6 changes: 6 additions & 0 deletions packages/tswebextension/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
<!-- TODO: manually add compare links for version changes -->
<!-- e.g. [0.1.2]: https://github.com/AdguardTeam/tsurlfilter/compare/tswebextension-v0.1.1...tswebextension-v0.1.2 -->

## [2.0.6] - 2024-11-18

- Updated `@adguard/tsurlfilter` to `v3.0.7`.

[2.0.6]: https://github.com/AdguardTeam/tsurlfilter/releases/tag/tswebextension-v2.0.6

## [2.0.5] - 2024-11-02

### Changed
Expand Down
2 changes: 1 addition & 1 deletion packages/tswebextension/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@adguard/tswebextension",
"version": "2.0.5",
"version": "2.0.6",
"description": "This is a TypeScript library that implements AdGuard's extension API",
"main": "dist/index.js",
"typings": "dist/types/src/lib/mv2/background/index.d.ts",
Expand Down

0 comments on commit 466a62d

Please sign in to comment.