Skip to content

Commit eca6e4e

Browse files
committed
AG-39385: remove local scriptlet data
Squashed commit of the following: commit 570cee7 Author: Dmitriy Seregin <[email protected]> Date: Wed Jan 29 14:20:11 2025 +0300 update changelog commit 0610d0f Author: Dmitriy Seregin <[email protected]> Date: Wed Jan 29 14:10:29 2025 +0300 undo changes in archive commit 8b31bf2 Author: Dmitriy Seregin <[email protected]> Date: Wed Jan 29 13:10:12 2025 +0300 update archive for tests commit c94a1eb Author: Slava Leleka <[email protected]> Date: Wed Jan 29 13:01:07 2025 +0300 AG-39385 export LocalScriptFunctionData type Squashed commit of the following: commit 3fdede0 Author: Slava Leleka <[email protected]> Date: Tue Jan 28 19:32:09 2025 -0500 Revert "update tgz files" This reverts commit 47d9d3f. commit 47d9d3f Author: Slava Leleka <[email protected]> Date: Tue Jan 28 19:26:47 2025 -0500 update tgz files commit 0abf271 Author: Slava Leleka <[email protected]> Date: Tue Jan 28 19:26:33 2025 -0500 export LocalScriptFunctionData commit decaaa0 Author: Dmitriy Seregin <[email protected]> Date: Mon Jan 27 22:20:47 2025 +0300 update test archive commit 788065b Author: Dmitriy Seregin <[email protected]> Date: Mon Jan 27 22:20:35 2025 +0300 small update for better build commit 1f02721 Author: Slava Leleka <[email protected]> Date: Mon Jan 27 12:17:46 2025 -0500 fix changelog commit 24aa3ed Author: Dmitriy Seregin <[email protected]> Date: Mon Jan 27 17:08:14 2025 +0300 update archive for tests commit 9554e9a Author: Dmitriy Seregin <[email protected]> Date: Mon Jan 27 17:08:01 2025 +0300 fix commit d199a35 Author: Dmitriy Seregin <[email protected]> Date: Mon Jan 27 16:56:23 2025 +0300 bump version && update changelog commit 0dd230f Author: Maxim Topciu <[email protected]> Date: Sun Jan 26 15:34:22 2025 +0300 AG-39385 remove local scriptlet data
1 parent 1810fc6 commit eca6e4e

File tree

9 files changed

+32
-113
lines changed

9 files changed

+32
-113
lines changed

packages/tswebextension/CHANGELOG.md

+9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717

1818
[AdguardBrowserExtension#3002]: https://github.com/AdguardTeam/AdguardBrowserExtension/issues/3002
1919

20+
## [2.4.0-alpha.11] - 2025-01-29
21+
22+
### Removed
23+
24+
- Local Scriptlet rules allowing. This is a reversion of `2.4.0-alpha.10` changes
25+
so there is no more limitation on Scriptlet rules execution.
26+
27+
[2.4.0-alpha.11]: https://github.com/AdguardTeam/tsurlfilter/releases/tag/tswebextension-v2.4.0-alpha.11
28+
2029
## [2.4.0-alpha.10] - 2025-01-17
2130

2231
### Changed

packages/tswebextension/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@adguard/tswebextension",
3-
"version": "2.4.0-alpha.10",
3+
"version": "2.4.0-alpha.11",
44
"description": "This is a TypeScript library that implements AdGuard's extension API",
55
"main": "dist/index.js",
66
"typings": "dist/types/src/lib/mv2/background/index.d.ts",

packages/tswebextension/src/lib/mv2/background/services/content-filtering/content-stream.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TextEncoder, TextDecoder } from 'text-encoding';
1+
import TextEncoding from 'text-encoding';
22
import { type WebRequest } from 'webextension-polyfill';
33
import { RequestType } from '@adguard/tsurlfilter';
44

@@ -16,6 +16,10 @@ import { type ContentStringFilterInterface } from './content-string-filter';
1616
import { FilteringEventType, type FilteringLogInterface } from '../../../../common/filtering-log';
1717
import { logger } from '../../../../common/utils/logger';
1818

19+
// Do not destruct inside import, because it somehow breaks build in browser
20+
// extension via "ReferenceError: TextDecoder is not defined".
21+
const { TextEncoder, TextDecoder } = TextEncoding;
22+
1923
/**
2024
* Content Stream Filter class.
2125
*

packages/tswebextension/src/lib/mv3/background/app.ts

-10
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import { getErrorMessage } from '../../common/error';
3737
import { ALLOWLIST_FILTER_ID, QUICK_FIXES_FILTER_ID, USER_FILTER_ID } from '../../common/constants';
3838
import {
3939
type LocalScriptFunctionData,
40-
type LocalScriptletRulesData,
4140
localScriptRulesService,
4241
} from './services/local-script-rules-service';
4342

@@ -392,15 +391,6 @@ export class TsWebExtension implements AppInterface<
392391
localScriptRulesService.setLocalScriptRules(localScriptRules);
393392
}
394393

395-
/**
396-
* Sets prebuild local **scriptlet** rules.
397-
*
398-
* @param localScriptletRules Object with pre-build Scriptlet rules. @see {@link LocalScriptRulesService}.
399-
*/
400-
public static setLocalScriptletRules(localScriptletRules: LocalScriptletRulesData): void {
401-
localScriptRulesService.setLocalScriptletRules(localScriptletRules);
402-
}
403-
404394
/**
405395
* Updates `hideReferrer` stealth config value without re-initialization of engine.
406396
*

packages/tswebextension/src/lib/mv3/background/cosmetic-api.ts

+9-33
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type CosmeticResult, type CosmeticRule } from '@adguard/tsurlfilter';
1+
import { type ScriptletData, type CosmeticResult, type CosmeticRule } from '@adguard/tsurlfilter';
22
import { CosmeticRuleType } from '@adguard/agtree';
33

44
import { appContext } from './app-context';
@@ -13,7 +13,6 @@ import { defaultFilteringLog, FilteringEventType } from '../../common/filtering-
1313
import { getDomain } from '../../common/utils/url';
1414
import { type ContentType } from '../../common/request-type';
1515
import { nanoid } from '../nanoid';
16-
import { type ScriptletRuleData } from '../tabs/frame';
1716
import { localScriptRulesService } from './services/local-script-rules-service';
1817

1918
export type ContentScriptCosmeticData = {
@@ -45,7 +44,7 @@ type ScriptsAndScriptletsData = {
4544
/**
4645
* List of scriptlet data objects.
4746
*/
48-
scriptletDataList: ScriptletRuleData[],
47+
scriptletDataList: ScriptletData[],
4948
};
5049

5150
/**
@@ -198,16 +197,12 @@ export class CosmeticApi extends CosmeticApiCommon {
198197
for (let i = 0; i < rules.length; i += 1) {
199198
const rule = rules[i];
200199
if (rule.isScriptlet) {
201-
const scriptletRunData = rule.getScriptletData();
200+
const scriptletData = rule.getScriptletData();
202201

203-
// it looks like `//scriptlet...` so it will be easily matched against localScriptletRules
204-
const scriptletRuleText = rule.getContent();
205-
206-
if (scriptletRunData && scriptletRuleText) {
207-
scriptletDataList.push({
208-
scriptletRunData,
209-
scriptletRuleText,
210-
});
202+
if (scriptletData) {
203+
scriptletDataList.push(
204+
scriptletData,
205+
);
211206
}
212207
} else {
213208
// TODO: Optimize script injection by checking if common scripts (e.g., AG_)
@@ -349,27 +344,11 @@ export class CosmeticApi extends CosmeticApiCommon {
349344

350345
try {
351346
await Promise.all(scriptletDataList.map((scriptletData) => {
352-
/**
353-
* It is possible to follow all places using this logic by searching JS_RULES_EXECUTION.
354-
*
355-
* This is STEP 4.2: Selecting only local scriptlet rules which were pre-built into the extension.
356-
*/
357-
358-
const { scriptletRunData, scriptletRuleText } = scriptletData;
359-
360-
/**
361-
* Here we check if the scriptlet rule is local to guarantee that we do not execute remote code.
362-
*/
363-
const isLocalScriptlet = localScriptRulesService.isLocalScriptlet(scriptletRuleText);
364-
if (!isLocalScriptlet) {
365-
return;
366-
}
367-
368347
// eslint-disable-next-line consistent-return
369348
return ScriptingApi.executeScriptlet({
370349
tabId,
371350
frameId,
372-
scriptletRunData,
351+
scriptletData,
373352
domainName: getDomain(frameContext.url),
374353
});
375354
}));
@@ -511,10 +490,7 @@ export class CosmeticApi extends CosmeticApiCommon {
511490

512491
// do not log rules if they are not local
513492
// which means that will not be applied
514-
if (
515-
!localScriptRulesService.isLocalScript(ruleText)
516-
&& !localScriptRulesService.isLocalScriptlet(ruleText)
517-
) {
493+
if (!localScriptRulesService.isLocalScript(ruleText)) {
518494
continue;
519495
}
520496

packages/tswebextension/src/lib/mv3/background/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,6 @@ export { BACKGROUND_TAB_ID, LF } from '../../common/constants';
7979
export { ContentType } from '../../common/request-type';
8080
export type { RequestData } from './request/events/request-event';
8181
export type { MessagesHandlerMV3 } from './messages-api';
82+
export type { LocalScriptFunctionData } from './services/local-script-rules-service';
8283

8384
export { TSWEBEXTENSION_VERSION, EXTENDED_CSS_VERSION } from '../../common/configuration';

packages/tswebextension/src/lib/mv3/background/scripting-api.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export type ExecuteScriptletParams = {
5151
/**
5252
* The scriptlet data to be executed.
5353
*/
54-
scriptletRunData: ScriptletData,
54+
scriptletData: ScriptletData,
5555

5656
/**
5757
* The domain name of the frame.
@@ -90,15 +90,15 @@ export class ScriptingApi {
9090
* @param params Parameters for executing the scriptlet.
9191
* @param params.tabId The ID of the tab.
9292
* @param params.frameId The ID of the frame.
93-
* @param params.scriptletRunData The scriptlet data to be executed.
93+
* @param params.scriptletData The scriptlet data to be executed.
9494
* @param params.domainName The domain name of the frame. Used for debugging.
9595
* @returns Promise that resolves when the script is executed.
9696
*/
9797
public static async executeScriptlet(
9898
{
9999
tabId,
100100
frameId,
101-
scriptletRunData,
101+
scriptletData,
102102
domainName,
103103
}: ExecuteScriptletParams,
104104
): Promise<void> {
@@ -108,18 +108,18 @@ export class ScriptingApi {
108108
}
109109

110110
const params: IConfiguration = {
111-
...scriptletRunData.params,
111+
...scriptletData.params,
112112
uniqueId: String(appContext.startTimeMs),
113113
verbose: appContext.configuration?.settings.debugScriptlets || false,
114114
domainName: domainName ?? undefined,
115115
};
116116

117117
await chrome.scripting.executeScript({
118118
target: { tabId, frameIds: [frameId] },
119-
func: scriptletRunData.func,
119+
func: scriptletData.func,
120120
injectImmediately: true,
121121
world: 'MAIN',
122-
args: [params, scriptletRunData.params.args],
122+
args: [params, scriptletData.params.args],
123123
});
124124
}
125125

packages/tswebextension/src/lib/mv3/background/services/local-script-rules-service.ts

-42
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,6 @@ export type LocalScriptFunctionData = {
1212
[key: string]: LocalScriptFunction;
1313
};
1414

15-
/**
16-
* An object containing local scriptlet rules where:
17-
* - key — scriptlet rule text
18-
* - value — boolean value indicating whether it is allowed to run (always true).
19-
*/
20-
export type LocalScriptletRulesData = {
21-
[key: string]: boolean;
22-
};
23-
2415
/**
2516
* It is possible to follow all places using this logic by searching JS_RULES_EXECUTION.
2617
*
@@ -38,12 +29,6 @@ export class LocalScriptRulesService {
3829
*/
3930
private localScripts: LocalScriptFunctionData | undefined;
4031

41-
/**
42-
* When {@link setLocalScriptletRules} is called, this holds a list of pre-built Scriptlets rules allowed to run.
43-
* If it is never called, this remains undefined.
44-
*/
45-
private localScriptlets: LocalScriptletRulesData | undefined;
46-
4732
/**
4833
* Stores prebuilt JS rules in memory for later use.
4934
*
@@ -53,16 +38,6 @@ export class LocalScriptRulesService {
5338
this.localScripts = localScriptRules;
5439
}
5540

56-
/**
57-
* Stores prebuilt Scriptlet rules in memory for later use.
58-
*
59-
* @param localScriptletRules A map of scriptlet rules as string
60-
* to a boolean value indicating whether it is allowed to run.
61-
*/
62-
public setLocalScriptletRules(localScriptletRules: LocalScriptletRulesData): void {
63-
this.localScriptlets = localScriptletRules;
64-
}
65-
6641
/**
6742
* Checks if the given script text is included in our prebuilt local scripts.
6843
*
@@ -80,23 +55,6 @@ export class LocalScriptRulesService {
8055
return this.localScripts[scriptText] !== undefined;
8156
}
8257

83-
/**
84-
* Checks if the given scriptlet rule is included in our prebuilt local scriptlets.
85-
*
86-
* This helper method is primarily for transparency during the Chrome Web Store review process.
87-
*
88-
* @param scriptletRuleText The scriptlet rule to verify.
89-
*
90-
* @returns True if the scriptlet rule is part of our local collection, false otherwise.
91-
*/
92-
public isLocalScriptlet(scriptletRuleText: string): boolean {
93-
if (this.localScriptlets === undefined) {
94-
return false;
95-
}
96-
97-
return this.localScriptlets[scriptletRuleText] !== undefined;
98-
}
99-
10058
/**
10159
* Retrieves the function associated with the specified script text
10260
* from our local scripts collection.

packages/tswebextension/src/lib/mv3/tabs/frame.ts

+1-20
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,6 @@ import {
55
type ScriptletData,
66
} from '@adguard/tsurlfilter';
77

8-
/**
9-
* Scriptlet rule data object which contains scriptlet data for execution
10-
* and scriptlet rule text (rule content) to match whether it is local or not.
11-
*/
12-
export type ScriptletRuleData = {
13-
/**
14-
* Scriptlet data for the execution.
15-
*/
16-
scriptletRunData: ScriptletData,
17-
18-
/**
19-
* Scriptlet rule text to match whether it is local or not.
20-
*
21-
* @example
22-
* `//scriptlet('set-constant', 'canRunAds', 'true')`
23-
*/
24-
scriptletRuleText: string,
25-
};
26-
278
/**
289
* Prepared cosmetic result.
2910
* This type represents the processed cosmetic data extracted from the initial cosmetic result.
@@ -37,7 +18,7 @@ type PreparedCosmeticResult = {
3718
/**
3819
* A list of scriptlet data extracted from the cosmetic result.
3920
*/
40-
scriptletDataList: ScriptletRuleData[];
21+
scriptletDataList: ScriptletData[];
4122

4223
/**
4324
* CSS styles extracted from the cosmetic result.

0 commit comments

Comments
 (0)