Skip to content

Commit

Permalink
do not inject cosmetic rules into an assistant frame. AG-9829
Browse files Browse the repository at this point in the history
AdguardTeam/AdguardBrowserExtension#1848

Squashed commit of the following:

commit 7dd337d
Author: Slava Leleka <[email protected]>
Date:   Mon Mar 25 15:50:34 2024 +0300

    packages/tswebextension/CHANGELOG.md edited online with Bitbucket

commit d572159
Author: Slava Leleka <[email protected]>
Date:   Fri Mar 22 19:19:23 2024 +0200

    decrease Assistant.FRAME_CREATION_LIMIT_MS to 100

commit 60ef472
Author: Slava Leleka <[email protected]>
Date:   Fri Mar 22 19:13:18 2024 +0200

    add url checking

commit 0f3c5c1
Author: Slava Leleka <[email protected]>
Date:   Fri Mar 22 17:50:33 2024 +0200

    make isAssistantFrame() a static method of Assistant

commit c1b3ebf
Author: Slava Leleka <[email protected]>
Date:   Fri Mar 22 16:31:08 2024 +0200

    fix setAssistantInitTimestamp()

commit bfed0f7
Author: Slava Leleka <[email protected]>
Date:   Thu Mar 21 21:25:18 2024 +0200

    do not inject cosmetic rules into an assistant frame
  • Loading branch information
slavaleleka committed Mar 25, 2024
1 parent 4276bdf commit b43c3ea
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 3 deletions.
10 changes: 10 additions & 0 deletions packages/tswebextension/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ 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.17] - 2024-03-25

### Fixed

- Assistant iframe styles are affected by cosmetic rules specific for websites [AdguardBrowserExtension#1848].

[1.0.17]: https://github.com/AdguardTeam/tsurlfilter/releases/tag/tswebextension-v1.0.17
[AdguardBrowserExtension#1848]: https://github.com/AdguardTeam/AdguardBrowserExtension/issues/1848


## [1.0.16] - 2024-03-01

### Changed
Expand Down
2 changes: 2 additions & 0 deletions packages/tswebextension/src/lib/common/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export interface AppInterface<

/**
* Launches assistant in the current tab.
*
* @param tabId Id of the tab to launch assistant in.
*/
openAssistant: (tabId: number) => void;

Expand Down
2 changes: 2 additions & 0 deletions packages/tswebextension/src/lib/mv2/background/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ MessageHandlerMV2
* @param tabId Tab id where assistant will be opened.
*/
public async openAssistant(tabId: number): Promise<void> {
this.tabsApi.setAssistantInitTimestamp(tabId);
await Assistant.openAssistant(tabId);
}

Expand All @@ -221,6 +222,7 @@ MessageHandlerMV2
* @param tabId Tab id.
*/
public async closeAssistant(tabId: number): Promise<void> {
this.tabsApi.resetAssistantInitTimestamp(tabId);
await Assistant.closeAssistant(tabId);
}

Expand Down
52 changes: 50 additions & 2 deletions packages/tswebextension/src/lib/mv2/background/assistant.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import browser from 'webextension-polyfill';
import browser, { WebNavigation } from 'webextension-polyfill';
import { EventChannel, MessageType } from '../../common';
import { messagesApi } from './api';
import { MAIN_FRAME_ID, TabContext } from './tabs';

/**
* Event channel wrapper for sending messages to assistant.
* Event channel wrapper for sending messages to the assistant.
*/
export class Assistant {
/**
* Maximum time delay in milliseconds between the assistant frame creation and the assistant initialization.
*/
private static FRAME_CREATION_LIMIT_MS = 100;

/**
* Assistant frame URL.
*/
private static FRAME_URL = 'about:blank';

public static onCreateRule = new EventChannel<string>();

/**
Expand Down Expand Up @@ -43,4 +54,41 @@ export class Assistant {
type: MessageType.CloseAssistant,
});
}

/**
* Checks whether the frame is an assistant frame. Two conditions must be met:
* 1. The frame was created less than {@link Assistant.FRAME_CREATION_LIMIT_MS} milliseconds
* after the assistant initialization.
* 2. The frame is a child of the main frame.
*
* @see {@link https://github.com/AdguardTeam/AdguardBrowserExtension/issues/1848}
*
* @param details Frame details of the onDomContentLoaded event.
* @param tabContext Tab context.
*
* @returns True if the frame is an assistant frame, false otherwise.
*/
public static async isAssistantFrame(
details: WebNavigation.OnDOMContentLoadedDetailsType,
tabContext: TabContext | undefined,
): Promise<boolean> {
if (!tabContext || !tabContext?.assistantInitTimestamp) {
return false;
}

const {
tabId,
url,
frameId,
timeStamp,
} = details;

const newFrameData = await browser.webNavigation.getFrame({ tabId, frameId });

const timeSinceFrameCreatedMs = timeStamp - tabContext.assistantInitTimestamp;

return timeSinceFrameCreatedMs < Assistant.FRAME_CREATION_LIMIT_MS
&& url === Assistant.FRAME_URL
&& newFrameData?.parentFrameId === MAIN_FRAME_ID;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ export class TabContext {
*/
public isDocumentRequestCached = false;

/**
* Timestamp of the assistant initialization.
*
* Needed to avoid cosmetic rules injection into the assistant frame.
*
* @see {@link https://github.com/AdguardTeam/AdguardBrowserExtension/issues/1848}
*/
public assistantInitTimestamp?: number | null = null;

/**
* Context constructor.
*
Expand Down
32 changes: 32 additions & 0 deletions packages/tswebextension/src/lib/mv2/background/tabs/tabs-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,38 @@ export class TabsApi {
return this.context.get(tabId);
}

/**
* Sets a current timestamp as `assistantInitTimestamp` of the tab context.
*
* Needed to determine later if a newly created frame is an assistant frame.
*
* @param tabId Tab id.
*/
public setAssistantInitTimestamp(tabId: number): void {
const tabContext = this.context.get(tabId);

if (!tabContext) {
return;
}

tabContext.assistantInitTimestamp = Date.now();
}

/**
* Resets tab context's `assistantInitTimestamp` to null.
*
* @param tabId Tab id.
*/
public resetAssistantInitTimestamp(tabId: number): void {
const tabContext = this.context.get(tabId);

if (!tabContext) {
return;
}

tabContext.assistantInitTimestamp = null;
}

/**
* Checks whether the tab with the specified ID is open in incognito mode
* or not.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ import { logger } from '../../common/utils/logger';
import { defaultFilteringLog, FilteringEventType } from '../../common/filtering-log';
import { FRAME_DELETION_TIMEOUT } from '../../common/constants';

import { Assistant } from './assistant';
import { CosmeticApi } from './cosmetic-api';
import { headersService } from './services/headers-service';
import { paramsService } from './services/params-service';
Expand Down Expand Up @@ -717,7 +718,7 @@ export class WebRequestApi {
* @see https://github.com/AdguardTeam/AdguardBrowserExtension/issues/1046
* @param details Event details.
*/
private static onDomContentLoaded(details: WebNavigation.OnDOMContentLoadedDetailsType): void {
private static async onDomContentLoaded(details: WebNavigation.OnDOMContentLoadedDetailsType): Promise<void> {
const {
tabId,
frameId,
Expand All @@ -732,6 +733,13 @@ export class WebRequestApi {
return;
}

const isAssistant = await Assistant.isAssistantFrame(details, tabContext);

// do not inject cosmetic rules into the assistant frame
if (isAssistant) {
return;
}

const cosmeticResult = engineApi.matchCosmetic({
requestUrl: mainFrameUrl,
frameUrl: mainFrameUrl,
Expand Down

0 comments on commit b43c3ea

Please sign in to comment.