Skip to content

Commit

Permalink
Merge branch 'master' into fix/AG-24533
Browse files Browse the repository at this point in the history
  • Loading branch information
zhelvis committed Sep 18, 2023
2 parents 89ee2fa + aaa0e4d commit ad0e93a
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 49 deletions.
3 changes: 2 additions & 1 deletion packages/tsurlfilter/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased

### Fixed
- Filtering log clearing on `$removeparam` rule application[#2442](https://github.com/AdguardTeam/AdguardBrowserExtension/issues/2442)
- Scriptlets not being logged when filtering log is open [#2442](https://github.com/AdguardTeam/AdguardBrowserExtension/issues/2481)
- Filtering log clearing on `$removeparam` rule application [#2442](https://github.com/AdguardTeam/AdguardBrowserExtension/issues/2442)


## [2.1.11] - 2023-08-25
Expand Down
20 changes: 10 additions & 10 deletions packages/tsurlfilter/src/rules/cosmetic-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { config } from '../configuration';
*/
interface InitScriptParams {
debug?: boolean,
request?: Request,
frameUrl?: string
}

/**
Expand All @@ -33,7 +33,7 @@ export type ScriptletData = {
type ScriptData = {
code: string | null,
debug?: boolean,
domain?: string
frameUrl?: string
};

/**
Expand Down Expand Up @@ -295,19 +295,20 @@ export class CosmeticRule implements rule.IRule {
/**
* Returns script ready to execute or null
* Rebuilds scriptlet script if debug or domain params change
* @param options
* @param options script options
* @returns script code or null
*/
getScript(options: InitScriptParams = {}): string | null {
const { debug = false, request = null } = options;
const { debug = false, frameUrl } = options;
const { scriptData } = this;

if (scriptData && !this.isScriptlet) {
return scriptData.code;
}

if (scriptData && scriptData.debug === debug) {
if (request) {
if (request.domain === scriptData.domain) {
if (frameUrl) {
if (frameUrl === scriptData.frameUrl) {
return scriptData.code;
}
} else {
Expand Down Expand Up @@ -666,8 +667,7 @@ export class CosmeticRule implements rule.IRule {
* @param options
*/
initScript(options: InitScriptParams = {}) {
const { debug = false, request = null } = options;

const { debug = false, frameUrl } = options;
const ruleContent = this.getContent();
if (!this.isScriptlet) {
this.scriptData = {
Expand All @@ -685,14 +685,14 @@ export class CosmeticRule implements rule.IRule {
name: scriptletParams.name,
ruleText: this.getText(),
verbose: debug,
domainName: request?.domain,
domainName: frameUrl,
version: config.version || '',
};

this.scriptData = {
code: scriptlets.invoke(params) ?? null,
debug,
domain: request?.domain,
frameUrl,
};

this.scriptletData = {
Expand Down
22 changes: 11 additions & 11 deletions packages/tsurlfilter/test/rules/cosmetic-rule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -692,12 +692,12 @@ describe('Javascript rules', () => {
const jsRule = `example.org#%#${jsRuleContent}`;
const rule = new CosmeticRule(jsRule, 0);
const verbose = true;
const domainName = 'example.com';
const frameUrl = 'https://example.com';

const script = rule.getScript({ debug: verbose, request: new Request(`https://${domainName}`, null, RequestType.Document) })!;
const script = rule.getScript({ debug: verbose, frameUrl })!;
const params = parseParamsFromScript(script)!;
expect(params.ruleText).toBe(jsRule);
expect(params.domainName).toBe(domainName);
expect(params.domainName).toBe(frameUrl);
expect(params.verbose).toBe(verbose);
});

Expand Down Expand Up @@ -739,29 +739,29 @@ describe('Javascript rules', () => {
const jsRule = `example.org#%#${jsRuleContent}`;
const rule = new CosmeticRule(jsRule, 0);
const verbose = false;
let domainName = 'example.com';
let frameUrl = 'https://example.com';

jest.spyOn(rule, 'initScript');

expect(rule.initScript).toBeCalledTimes(0);

// after first get script call
let script = rule.getScript({ debug: verbose, request: new Request(`https://${domainName}`, null, RequestType.Document) })!;
let script = rule.getScript({ debug: verbose, frameUrl })!;
let params = parseParamsFromScript(script)!;
expect(params.domainName).toBe(domainName);
expect(params.domainName).toBe(frameUrl);
expect(rule.initScript).toBeCalledTimes(1);

// after second get script call
script = rule.getScript({ debug: verbose, request: new Request(`https://${domainName}`, null, RequestType.Document) })!;
script = rule.getScript({ debug: verbose, frameUrl })!;
params = parseParamsFromScript(script)!;
expect(params.domainName).toBe(domainName);
expect(params.domainName).toBe(frameUrl);
expect(rule.initScript).toBeCalledTimes(1);

// after third get script call with other domain name
domainName = 'example.org';
script = rule.getScript({ debug: verbose, request: new Request(`https://${domainName}`, null, RequestType.Document) })!;
frameUrl = 'https://example.org';
script = rule.getScript({ debug: verbose, frameUrl })!;
params = parseParamsFromScript(script)!;
expect(params.domainName).toBe(domainName);
expect(params.domainName).toBe(frameUrl);
expect(rule.initScript).toBeCalledTimes(2);
});

Expand Down
6 changes: 6 additions & 0 deletions packages/tswebextension/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
<!-- TODO: manually add compare links for version to the end of the file -->
<!-- e.g. [0.1.2]: https://github.com/AdguardTeam/tsurlfilter/compare/tswebextension-v0.1.1...tswebextension-v0.1.2 -->

## Unreleased

### Fixed

- `$csp`, `$removeparam` and `$removeheader` allowlist rules not being published as filtering log events


## [0.3.18] - 2023-09-13

Expand Down
26 changes: 23 additions & 3 deletions packages/tswebextension/src/lib/mv2/background/cosmetic-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type { ContentType } from '../../common/request-type';
export type ApplyCosmeticRulesParams = {
tabId: number,
frameId: number,
url?: string,
cosmeticResult: CosmeticResult,
};

Expand Down Expand Up @@ -165,17 +166,34 @@ export class CosmeticApi {
* Builds scripts from cosmetic rules.
*
* @param rules Cosmetic rules.
* @param frameUrl Frame url.
* @returns Scripts or undefined.
*/
public static getScriptText(rules: CosmeticRule[]): string | undefined {
public static getScriptText(rules: CosmeticRule[], frameUrl?: string): string | undefined {
if (rules.length === 0) {
return undefined;
}

const permittedRules = CosmeticApi.sanitizeScriptRules(rules);

let debug = false;
const { configuration } = appContext;
if (configuration) {
const { settings } = configuration;
if (settings) {
if (settings.collectStats) {
debug = true;
}
}
}

const scriptParams = {
debug,
frameUrl,
};

const scriptText = permittedRules
.map((rule) => rule.getScript())
.map((rule) => rule.getScript(scriptParams))
.join('\n');

if (!scriptText) {
Expand Down Expand Up @@ -274,11 +292,12 @@ export class CosmeticApi {
tabId,
frameId,
cosmeticResult,
url,
} = params;

const scriptRules = cosmeticResult.getScriptRules();

let scriptText = CosmeticApi.getScriptText(scriptRules);
let scriptText = CosmeticApi.getScriptText(scriptRules, url);
scriptText += stealthApi.getSetDomSignalScript();

if (scriptText) {
Expand Down Expand Up @@ -389,6 +408,7 @@ export class CosmeticApi {
await injector({
frameId,
tabId,
url: frame.url,
cosmeticResult: frame.cosmeticResult,
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { nanoid } from 'nanoid';

import { ContentType } from '../../../common/request-type';
import { defaultFilteringLog, FilteringEventType } from '../../../common/filtering-log';
import { FilteringLogInterface } from '../../../common';
import {
type RequestContext,
requestContextStorage,
Expand All @@ -15,14 +16,25 @@ const CSP_HEADER_NAME = 'Content-Security-Policy';
* Content Security Policy Headers filtering service module.
*/
export class CspService {
private filteringLog: FilteringLogInterface;

/**
* Constructor.
*
* @param filteringLog Filtering log.
*/
constructor(filteringLog: FilteringLogInterface) {
this.filteringLog = filteringLog;
}

/**
* Applies CSP rules to response headers and returns modified headers.
* It is applied when webRequest.onHeadersReceived event is fired.
*
* @param context Request context.
* @returns True if headers were modified.
*/
public static onHeadersReceived(context: RequestContext): boolean {
public onHeadersReceived(context: RequestContext): boolean {
const {
matchingResult,
responseHeaders,
Expand All @@ -42,6 +54,7 @@ export class CspService {

for (let i = 0; i < cspRules.length; i += 1) {
const rule = cspRules[i];

// Don't forget: getCspRules returns all $csp rules, we must directly check that the rule is blocking.
if (RequestBlockingApi.isRequestBlockedByRule(rule)) {
const cspHeaderValue = rule.getAdvancedModifierValue();
Expand All @@ -51,22 +64,22 @@ export class CspService {
name: CSP_HEADER_NAME,
value: cspHeaderValue,
});

defaultFilteringLog.publishEvent({
type: FilteringEventType.ApplyCspRule,
data: {
tabId,
eventId: nanoid(),
requestUrl,
frameUrl: referrerUrl,
frameDomain: getDomain(referrerUrl) as string,
requestType: ContentType.Csp,
rule,
timestamp: Date.now(),
},
});
}
}

this.filteringLog.publishEvent({
type: FilteringEventType.ApplyCspRule,
data: {
tabId,
eventId: nanoid(),
requestUrl,
frameUrl: referrerUrl,
frameDomain: getDomain(referrerUrl) as string,
requestType: ContentType.Csp,
rule,
timestamp: Date.now(),
},
});
}

if (cspHeaders.length > 0) {
Expand All @@ -80,3 +93,5 @@ export class CspService {
return false;
}
}

export const cspService = new CspService(defaultFilteringLog);
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ export class HeadersService {

let isModified = false;
rules.forEach((rule) => {
if (HeadersService.applyRule(requestHeaders, rule, true)) {
isModified = true;
isModified = HeadersService.applyRule(requestHeaders, rule, true);
if (isModified || rule.isAllowlist()) {
this.filteringLog.publishEvent({
type: FilteringEventType.RemoveHeader,
data: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,29 @@ export class ParamsService {

const purgedUrl = removeParamRules.reduce((url: string, rule: NetworkRule): string => {
if (rule.isAllowlist()) {
this.filteringLog.publishEvent({
type: FilteringEventType.RemoveParam,
data: {
removeParam: true,
eventId: nanoid(),
tabId: context.tabId,
requestUrl: url,
frameUrl: url,
frameDomain: getDomain(url) as string,
requestType: contentType,
rule,
timestamp,
},
});
return url;
}

const modifier = rule.getAdvancedModifier() as RemoveParamModifier;

const modifiedUrl = modifier.removeParameters(url);
const hasUrlChanged = modifiedUrl !== url;

if (url !== modifiedUrl) {
if (hasUrlChanged) {
context.isRemoveparamRedirect = true;
this.filteringLog.publishEvent({
type: FilteringEventType.RemoveParam,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ import { headersService } from './services/headers-service';
import { paramsService } from './services/params-service';
import { cookieFiltering } from './services/cookie-filtering/cookie-filtering';
import { ContentFiltering } from './services/content-filtering/content-filtering';
import { CspService } from './services/csp-service';
import { cspService } from './services/csp-service';
import {
hideRequestInitiatorElement,
RequestEvents,
Expand Down Expand Up @@ -479,7 +479,7 @@ export class WebRequestApi {
let responseHeadersModified = false;

if (requestUrl && (requestType === RequestType.Document || requestType === RequestType.SubDocument)) {
if (CspService.onHeadersReceived(context)) {
if (cspService.onHeadersReceived(context)) {
responseHeadersModified = true;
}
}
Expand Down Expand Up @@ -744,6 +744,7 @@ export class WebRequestApi {
tabId,
frameId,
cosmeticResult,
url,
})
.catch(logger.debug);
}
Expand Down
Loading

0 comments on commit ad0e93a

Please sign in to comment.