Skip to content

Commit

Permalink
fix popup rule other types blocking. AG-30639
Browse files Browse the repository at this point in the history
AdguardTeam/AdguardBrowserExtension#2723

Squashed commit of the following:

commit 45d3a1b
Merge: b9e65f8 1eecaee
Author: Slava Leleka <[email protected]>
Date:   Thu Feb 22 17:20:53 2024 +0200

    Merge branch 'fix/AG-30639' of ssh://bit.int.agrd.dev:7999/adguard-filters/tsurlfilter into fix/AG-30639

commit b9e65f8
Author: Slava Leleka <[email protected]>
Date:   Thu Feb 22 17:20:26 2024 +0200

    beautify blocking page return

commit 1eecaee
Author: Slava Leleka <[email protected]>
Date:   Thu Feb 22 18:01:01 2024 +0300

    packages/tswebextension/test/lib/mv2/background/request/request-blocking-api.test.ts edited online with Bitbucket

commit fb27db5
Author: Slava Leleka <[email protected]>
Date:   Thu Feb 22 16:13:06 2024 +0200

    handle basic blocking rules with document modifier

commit 8b7caa1
Author: Slava Leleka <[email protected]>
Date:   Thu Feb 22 15:34:15 2024 +0200

    fix basic rules

commit 8e0ad59
Author: Slava Leleka <[email protected]>
Date:   Thu Feb 22 13:30:56 2024 +0200

    Revert "Revert "Revert "cleanup"""

    This reverts commit f9b8795.

commit f9b8795
Author: Slava Leleka <[email protected]>
Date:   Thu Feb 22 13:17:02 2024 +0200

    Revert "Revert "cleanup""

    This reverts commit bfebf14.

commit bfebf14
Author: Slava Leleka <[email protected]>
Date:   Thu Feb 22 13:00:51 2024 +0200

    Revert "cleanup"

    This reverts commit 91c02d6.

commit 4dc540c
Author: Slava Leleka <[email protected]>
Date:   Thu Feb 22 13:00:48 2024 +0200

    fix basic blocking rules hanfling

commit 91c02d6
Author: Slava Leleka <[email protected]>
Date:   Wed Feb 21 22:38:06 2024 +0200

    cleanup

commit 5cb3592
Author: Slava Leleka <[email protected]>
Date:   Wed Feb 21 22:34:40 2024 +0200

    update changelog

commit 5d93481
Author: Slava Leleka <[email protected]>
Date:   Wed Feb 21 22:32:04 2024 +0200

    add tests for getBlockingResponse

commit 589b1f0
Author: Slava Leleka <[email protected]>
Date:   Wed Feb 21 22:28:59 2024 +0200

    fix getBlockingResponse()

commit d455031
Author: Slava Leleka <[email protected]>
Date:   Wed Feb 21 22:26:59 2024 +0200

    bump version
  • Loading branch information
slavaleleka committed Feb 22, 2024
1 parent 90353f7 commit b4c87f5
Show file tree
Hide file tree
Showing 5 changed files with 367 additions and 42 deletions.
2 changes: 2 additions & 0 deletions packages/tsurlfilter/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
In addition to that, it solves the problem of leaking links to the original leaking strings
that in turn was leading to higher memory usage.

[2.2.14]: https://github.com/AdguardTeam/tsurlfilter/releases/tag/tsurlfilter-v2.2.14

## [2.2.13] - 2024-02-13

### Changed
Expand Down
11 changes: 11 additions & 0 deletions packages/tswebextension/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,24 @@ 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 -->

## [1.0.15] - 2024-02-22

### Fixed

- `$popup` modifier block other types of resources [#2723].

[1.0.15]: https://github.com/AdguardTeam/tsurlfilter/releases/tag/tswebextension-v1.0.15
[#2723]: https://github.com/AdguardTeam/AdguardBrowserExtension/issues/2723

## [1.0.14] - 2024-02-13

### Changed

- Filtering engine now uses the new `BufferRuleList` provided by `@adguard/tsurlfilter`
to improve performance and memory usage.

[1.0.14]: https://github.com/AdguardTeam/tsurlfilter/releases/tag/tswebextension-v1.0.14

## [1.0.13] - 2024-02-13

### Added
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": "1.0.14",
"version": "1.0.15",
"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
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { ContentType } from '../../../common/request-type';
/**
* Params for {@link RequestBlockingApi.getBlockingResponse}.
*/
type GetBlockingResponseParams = {
export type GetBlockingResponseParams = {
tabId: number,
eventId: string,
rule: NetworkRule | null,
Expand Down Expand Up @@ -104,45 +104,38 @@ export class RequestBlockingApi {
return undefined;
}

// If the request is a document request.
if (requestType === RequestType.Document) {
// Blocking rule can be with $popup modifier - in this case we need
// to close the tab as soon as possible.
// https://adguard.com/kb/ru/general/ad-filtering/create-own-filters/#popup-modifier
if (rule.isOptionEnabled(NetworkRuleOption.Popup)) {
const isNewTab = tabsApi.isNewPopupTab(tabId);

if (isNewTab) {
// the tab is considered as a popup and should be closed
RequestBlockingApi.logRuleApplying(data);
browser.tabs.remove(tabId);
return { cancel: true };
}

// $popup modifier can be used as a single modifier in the rule
// so there should be no document type, and:
// 1. new tab should be handled as a popup earlier (isNewTab check)
// 2. tab loading on direct url navigation should not be blocked
// https://github.com/AdguardTeam/AdguardBrowserExtension/issues/2449
//
// Note: for both rules `||example.com^$document,popup` and `||example.com^$all`
// there will be document type set (so we can safely return undefined here)
// and this function execution shall proceed to the final return statement
// and blocking page should be shown
if ((rule.getPermittedRequestTypes() & RequestType.Document) !== RequestType.Document) {
return undefined;
}
// Blocking rule can be with $popup modifier - in this case we need
// to close the tab as soon as possible.
// https://adguard.com/kb/ru/general/ad-filtering/create-own-filters/#popup-modifier
if (rule.isOptionEnabled(NetworkRuleOption.Popup)) {
const isNewTab = tabsApi.isNewPopupTab(tabId);

if (isNewTab) {
// the tab is considered as a popup and should be closed
RequestBlockingApi.logRuleApplying(data);
browser.tabs.remove(tabId);
return { cancel: true };
}

// For all other blocking rules, we return our dummy page with the
// option to temporarily disable blocking for the specified domain.
return documentBlockingService.getDocumentBlockingResponse({
eventId,
requestUrl,
referrerUrl,
rule,
tabId,
});
// $popup modifier can be used as a single modifier in the rule
// so there should be no document type, and:
// 1. new tab should be handled as a popup earlier (isNewTab check)
// 2. tab loading on direct url navigation should not be blocked
// https://github.com/AdguardTeam/AdguardBrowserExtension/issues/2449
//
// Note: for both rules `||example.com^$document,popup` and `||example.com^$all`
// there will be document type set so blocking page should be shown
if ((rule.getPermittedRequestTypes() & RequestType.Document) === RequestType.Document) {
return documentBlockingService.getDocumentBlockingResponse({
eventId,
requestUrl,
referrerUrl,
rule,
tabId,
});
}

return undefined;
}

if (rule.isOptionEnabled(NetworkRuleOption.Redirect)) {
Expand All @@ -157,6 +150,25 @@ export class RequestBlockingApi {
}
}

// Basic rules for blocking requests are applied only to sub-requests
// so `||example.com^` will not block the main page
// https://adguard.com/kb/general/ad-filtering/create-own-filters/#basic-rules
if (requestType === RequestType.Document) {
// but if the blocking rule has $document modifier, blocking page should be shown
// e.g. `||example.com^$document`
if (rule.getPermittedRequestTypes() === RequestType.Document) {
return documentBlockingService.getDocumentBlockingResponse({
eventId,
requestUrl,
referrerUrl,
rule,
tabId,
});
}

return undefined;
}

RequestBlockingApi.logRuleApplying(data);
return { cancel: true };
}
Expand Down
Loading

0 comments on commit b4c87f5

Please sign in to comment.