Skip to content

Commit

Permalink
AG-35716 Implement convertToUbo method for network rules in AGTree
Browse files Browse the repository at this point in the history
Merge in ADGUARD-FILTERS/tsurlfilter from fix/AG-35716 to master

Squashed commit of the following:

commit 7c43513
Merge: 1e7d9c2 0f34994
Author: scripthunter7 <[email protected]>
Date:   Fri Sep 6 13:33:11 2024 +0200

    Merge branch 'master' into fix/AG-35716

commit 1e7d9c2
Author: scripthunter7 <[email protected]>
Date:   Fri Sep 6 13:22:07 2024 +0200

    bump version to 2.0.1

commit 215e3c7
Author: scripthunter7 <[email protected]>
Date:   Thu Sep 5 17:36:21 2024 +0200

    change to const

commit 19832dc
Author: scripthunter7 <[email protected]>
Date:   Thu Sep 5 09:12:16 2024 +0200

    make more tolerant

commit a111601
Author: scripthunter7 <[email protected]>
Date:   Thu Sep 5 09:07:50 2024 +0200

    update jsdoc

commit e7df1af
Author: scripthunter7 <[email protected]>
Date:   Thu Sep 5 09:05:36 2024 +0200

    update exports

commit bb6d859
Author: scripthunter7 <[email protected]>
Date:   Thu Sep 5 09:05:25 2024 +0200

    small improve

commit 930cbef
Author: scripthunter7 <[email protected]>
Date:   Thu Sep 5 09:05:04 2024 +0200

    fix nit

commit 2f18b64
Author: scripthunter7 <[email protected]>
Date:   Thu Sep 5 08:59:50 2024 +0200

    update changelog

commit edea4be
Author: scripthunter7 <[email protected]>
Date:   Thu Sep 5 08:58:24 2024 +0200

    improvements

commit 80607af
Author: scripthunter7 <[email protected]>
Date:   Wed Sep 4 19:04:13 2024 +0200

    add reference

commit e9a4813
Author: scripthunter7 <[email protected]>
Date:   Wed Sep 4 18:59:33 2024 +0200

    add comment

commit 379bf41
Author: scripthunter7 <[email protected]>
Date:   Wed Sep 4 18:58:00 2024 +0200

    basic network rule conversion to ubo syntax

commit 8834807
Author: scripthunter7 <[email protected]>
Date:   Wed Sep 4 18:55:58 2024 +0200

    handle redirect resource types in compatibility tables api

commit 5a35930
Author: scripthunter7 <[email protected]>
Date:   Wed Sep 4 18:55:14 2024 +0200

    add resource types to ubo redirects
  • Loading branch information
scripthunter7 committed Sep 6, 2024
1 parent 0f34994 commit 5f9820b
Show file tree
Hide file tree
Showing 50 changed files with 582 additions and 6 deletions.
6 changes: 5 additions & 1 deletion packages/agtree/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,23 @@ The format is based on [Keep a Changelog][keepachangelog], and this project adhe
[keepachangelog]: https://keepachangelog.com/en/1.0.0/
[semver]: https://semver.org/spec/v2.0.0.html

## Unreleased
## [2.0.1] - 2024-09-06

### Added

- Support for uBO's legacy `script:inject` mask.
- Support for uBO's redirect priority in the compatibility table name normalization.
- `resource_types` field to the redirects compatibility table.
- Support for converting network rules to uBO syntax.

### Fixed

- Conversion for uBO's `rc` and `ra` scriptlets.
- Converter now unescape scriptlet separators from the previous rule, if needed (e.g. it unescape commas when converting
from uBO to AG syntax).

[2.0.1]: https://github.com/AdguardTeam/tsurlfilter/releases/tag/agtree-v2.0.1

## [2.0.0] - 2024-08-15

### Added
Expand Down
2 changes: 1 addition & 1 deletion packages/agtree/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@adguard/agtree",
"version": "2.0.0",
"version": "2.0.1",
"description": "Universal adblock filter list parser.",
"keywords": [
"adblock",
Expand Down
18 changes: 16 additions & 2 deletions packages/agtree/src/compatibility-tables/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Table of contents:
- [Available compatibility table instances](#available-compatibility-table-instances)
- [Platform flags](#platform-flags)
- [Platform expressions parser](#platform-expressions-parser)
- [Specific API](#specific-api)
- [Redirects compatibility table](#redirects-compatibility-table)
- [Examples](#examples)
- [Check if a modifier is supported by any adblocker](#check-if-a-modifier-is-supported-by-any-adblocker)
- [Check if a modifier is supported by a specific / generic platform](#check-if-a-modifier-is-supported-by-a-specific--generic-platform)
Expand Down Expand Up @@ -163,10 +165,10 @@ All of them provides the following methods for their respective compatibility da
| `getRow(name: string): T[]` | Returns all compatibility data records for the specified name. |
| `getRowsByProduct(): RowsByProduct<T>` | Returns all compatibility data records grouped by product. |

All methods based on the name of the entity and the platform.

<!-- markdownlint-enable MD013 -->

All methods based on the name of the entity and the platform.

#### Platform flags

The `SpecificPlatform` and `GenericPlatform` enums are used to specify the platform in the API methods.
Expand All @@ -186,6 +188,18 @@ import { parseRawPlatforms } from '@adguard/agtree';
parseRawPlatforms('adg_os_windows|ubo_ext_chrome');
```

### Specific API

#### Redirects compatibility table

<!-- markdownlint-disable MD013 -->

| Method name | Description |
| --- | --- |
| `getResourceTypeModifiers(redirect: string \| RedirectDataSchema platform: SpecificPlatform \| GenericPlatform)` | Get all supported resource type modifiers for a specific redirect. |

<!-- markdownlint-enable MD013 -->

### Examples

In this section, we provide examples of how to use the compatibility table API.
Expand Down
7 changes: 7 additions & 0 deletions packages/agtree/src/compatibility-tables/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,10 @@ export {
getPlatformId,
getSpecificPlatformName,
} from './utils/platform-helpers';
export {
ResourceType,
} from './schemas/resource-type';
export {
getResourceTypeModifier,
isValidResourceType,
} from './utils/resource-type-helpers';
43 changes: 43 additions & 0 deletions packages/agtree/src/compatibility-tables/redirects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import { redirectsCompatibilityTableData } from './compatibility-table-data';
import { type CompatibilityTable } from './types';
import { deepFreeze } from '../utils/deep-freeze';
import { COLON } from '../utils/constants';
import { type GenericPlatform, type SpecificPlatform } from './platforms';
import { getResourceTypeModifier } from './utils/resource-type-helpers';
import { isNull, isString, isUndefined } from '../utils/type-guards';

/**
* Prefix for resource redirection names.
Expand Down Expand Up @@ -57,6 +60,46 @@ class RedirectsCompatibilityTable extends CompatibilityTableBase<RedirectDataSch
constructor(data: CompatibilityTable<RedirectDataSchema>) {
super(data, redirectNameNormalizer);
}

/**
* Gets the resource type adblock modifiers for the redirect for the given platform
* based on the `resourceTypes` field.
*
* @param redirect Redirect name or redirect data.
* @param platform Platform to get the modifiers for.
*
* @returns Set of resource type modifiers or an empty set if the redirect is not found or has no resource types.
*/
public getResourceTypeModifiers(
redirect: string | RedirectDataSchema,
platform: SpecificPlatform | GenericPlatform,
): Set<string> {
let redirectData: RedirectDataSchema | null = null;

if (isString(redirect)) {
redirectData = this.getFirst(redirect, platform);
} else {
redirectData = redirect;
}

const modifierNames = new Set<string>();

if (isNull(redirectData) || isUndefined(redirectData.resourceTypes)) {
return modifierNames;
}

for (const resourceType of redirectData.resourceTypes) {
const modifierName = getResourceTypeModifier(resourceType, platform);

if (isNull(modifierName)) {
continue;
}

modifierNames.add(modifierName);
}

return modifierNames;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ ubo_any:
name: 1x1.gif
description: Redirects to a 1x1 transparent GIF.
docs: https://github.com/gorhill/uBlock/wiki/Resources-Library#available-empty-redirect-resources
resource_types:
- image

abp_any:
name: 1x1-transparent-gif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ ubo_any:
name: 2x2.png
description: Redirects to a 2x2 transparent PNG.
docs: https://github.com/gorhill/uBlock/wiki/Resources-Library#available-empty-redirect-resources
resource_types:
- image

abp_any:
name: 2x2-transparent-png
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ ubo_any:
name: 32x32.png
description: Redirects to a 32x32 transparent PNG.
docs: https://github.com/gorhill/uBlock/wiki/Resources-Library#available-empty-redirect-resources
resource_types:
- image

abp_any:
name: 32x32-transparent-png
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ ubo_any:
name: 3x2.png
description: Redirects to a 3x2 transparent PNG.
docs: https://github.com/gorhill/uBlock/wiki/Resources-Library#available-empty-redirect-resources
resource_types:
- image

abp_any:
name: 3x2-transparent-png
Expand Down
2 changes: 2 additions & 0 deletions packages/agtree/src/compatibility-tables/redirects/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ with the following fields:
| `removed` | Describes whether the redirect is removed; for *already removed* features. | `boolean` | `false` |
| `removal_message` | Message that describes why the redirect is removed. If not specified or it's value is `null`, then the message is not available. It's value is omitted if the redirect is not marked as deprecated. | `string\|null` | `null` |
| `is_blocking` | Whether the redirect is blocking. | `boolean` | `false` |
| `resource_types` | List of [resource types][resource-types] that the redirect can be applied to. Only the values ​​listed in the documentation can be used! | `string[]` | `[]` |

<!-- markdownlint-enable MD013 -->

[resource-types]: https://developer.chrome.com/docs/extensions/reference/declarativeNetRequest/#type-ResourceType
\*: The field is required.
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ ubo_any:
name: amazon_apstag.js
description: Mocks Amazon's apstag.js
docs: https://github.com/gorhill/uBlock/wiki/Resources-Library#amazon_apstagjs-
resource_types:
- script
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# TODO: add description and documentation reference
ubo_any:
name: amazon_ads.js
resource_types:
- script
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# TODO: add description and documentation reference
ubo_any:
name: ampproject_v0.js
resource_types:
- script

Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# TODO: add description and documentation reference
ubo_any:
name: chartbeat.js
resource_types:
- script
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ adg_os_any|adg_ext_any:
ubo_any:
name: fingerprint2.js
description: Mocks FingerprintJS2 script.
resource_types:
- script

Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ adg_os_any|adg_ext_any:
ubo_any:
name: fingerprint3.js
description: Mocks FingerprintJS v3.
resource_types:
- script

Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ adg_os_any|adg_ext_any:
ubo_any:
name: google-analytics_ga.js
description: Mocks old Google Analytics API.
resource_types:
- script
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ adg_os_any|adg_ext_any:
ubo_any:
name: google-analytics_analytics.js
description: Mocks Google's Analytics and Tag Manager APIs.
resource_types:
- script
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# TODO: add description and documentation reference
ubo_any:
name: google-analytics_cx_api.js
resource_types:
- script

Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# TODO: add description and documentation reference
ubo_any:
name: google-analytics_inpage_linkid.js
resource_types:
- script
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ adg_os_any|adg_ext_any:
ubo_any:
name: google-ima.js
description: Mocks the IMA SDK of Google.
resource_types:
- script
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ ubo_any:
aliases:
- googlesyndication.com/adsbygoogle.js
description: Mocks Google AdSense API.
resource_types:
- script
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ adg_os_any|adg_ext_any:
ubo_any:
name: googletagservices_gpt.js
description: Mocks Google Publisher Tag API.
resource_types:
- script
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# TODO: add description and documentation reference
ubo_any:
name: hd-main.js
resource_types:
- script
2 changes: 2 additions & 0 deletions packages/agtree/src/compatibility-tables/redirects/noeval.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ adg_os_any|adg_ext_any:
ubo_any:
name: noeval.js
description: Prevents page to use eval.
resource_types:
- script
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
ubo_any:
name: noop-0.5s.mp3
description: Redirects to an empty MP3 file with a duration of 0.5 seconds.
resource_types:
- media
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ adg_os_any|adg_ext_any:
ubo_any:
name: noop.css
description: Redirects to an empty CSS file.
resource_types:
- stylesheet

abp_any:
name: blank-css
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ adg_os_any|adg_ext_any:
ubo_any:
name: noop.html
description: Redirects to an empty HTML file.
resource_types:
- sub_frame

abp_any:
name: blank-html
Expand Down
2 changes: 2 additions & 0 deletions packages/agtree/src/compatibility-tables/redirects/noopjs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ adg_os_any|adg_ext_any:
ubo_any:
name: noop.js
description: Redirects to an empty JavaScript file.
resource_types:
- script

abp_any:
name: blank-js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ adg_os_any|adg_ext_any:
ubo_any:
name: noop-0.1s.mp3
description: Redirects to an empty MP3 file with a duration of 0.1 seconds.
resource_types:
- media

abp_any:
name: blank-mp3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ adg_os_any|adg_ext_any:
ubo_any:
name: noop-1s.mp4
description: Redirects to an empty MP4 file with a duration of 1 second.
resource_types:
- media

abp_any:
name: blank-mp4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ adg_os_any|adg_ext_any:
ubo_any:
name: noop.txt
description: Redirects to an empty text file.
resource_types:
- image
- media
- sub_frame
- stylesheet
- script
- xmlhttprequest
- other

abp_any:
name: blank-text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ adg_os_any|adg_ext_any:
ubo_any:
name: noop-vmap1.0.xml
description: Redirects to an empty VMAP 1.0 XML file.
resource_types:
- media
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ adg_os_any|adg_ext_any:
ubo_any:
name: nowebrtc.js
description: Disables WebRTC by overriding `RTCPeerConnection`.
resource_types:
- other
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# TODO: add description and documentation reference
ubo_any:
name: outbrain-widget.js
resource_types:
- script
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ adg_os_any|adg_ext_any:
ubo_any:
name: prebid-ads.js
description: Mocks prebid-ads.js script.
resource_types:
- script
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ adg_os_any|adg_ext_any:
ubo_any:
name: nobab.js
description: Prevents BlockAdblock script from detecting an ad blocker.
resource_types:
- script
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ adg_os_any|adg_ext_any:
ubo_any:
name: nobab2.js
description: Prevents BlockAdblock script from detecting an ad blocker.
resource_types:
- script
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ ubo_any:
- fuckadblock.js-3.2.0
- fuckadblock.js-3.2.0.js
description: Mocks FAB script v3.2.0.
resource_types:
- script
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ adg_os_any|adg_ext_any:
ubo_any:
name: popads.js
description: Aborts on property write (PopAds, popns), throws reference error with random id.
resource_types:
- script
Loading

0 comments on commit 5f9820b

Please sign in to comment.