Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scriptlets are injected too late on website reload/navigation #2855

Open
3 tasks done
kodiakhub opened this issue Jun 27, 2024 · 11 comments
Open
3 tasks done

Scriptlets are injected too late on website reload/navigation #2855

kodiakhub opened this issue Jun 27, 2024 · 11 comments

Comments

@kodiakhub
Copy link

kodiakhub commented Jun 27, 2024

Please answer the following questions for yourself before submitting an issue

  • Filters were updated before reproducing an issue
  • I checked the knowledge base and found no answer
  • I checked to make sure that this issue has not already been filed

AdGuard Extension version

4.3.53

Browser version

Chrome 126.0.6478.127

OS version

Windows 10

Ad Blocking

AdGuard Base filter

Privacy

AdGuard Tracking Protection filter, AdGuard URL Tracking filter

Social

AdGuard Social Media filter

Annoyances

AdGuard Annoyances filter

Security

No response

Other

No response

Language-specific

AdGuard Turkish filter

What Stealth Mode options do you have enabled?

No response

Issue Details

First of all, the issue was that pop-ups were appearing on different sites when they were first opened or in certain situations and when I wanted to verify the issue again at same time, I could not reproduce it, so I did a test.

During the testing phase, I only used AdGuard filters in the uBO extension. There seems to be a issue with the AdGuard extension. It can also be noticed that the uBO extension functions faster and more fluidly than the AdGuard extension.

Steps to reproduce:

  1. Watch compare videos and reproduce the issue.

Expected Behavior

The AdGuard extension does not leak pop-ups.

Actual Behavior

The AdGuard extension leaks pop-ups.

Videos

AdGuard

AdGuard.P.mp4

uBO

uBO.P.mp4

Additional Information

No response

@AdamWr
Copy link
Member

AdamWr commented Jun 29, 2024

I guess that the problem might be like mentioned here - AdguardTeam/AdguardFilters#182479 (comment)
It looks like that on website reload/navigation sometimes scriptlets are injected too late.

Steps to reproduce:

  1. Add these rules:
fiddle.jshell.net#%#//scriptlet('set-constant', 'alert', 'noopFunc')
fiddle.jshell.net#%#debugger;

2 Go to - https://jsfiddle.net/1tmha0g8/

Code:
<script>
debugger;
alert(1);
</script>

Alert dialog should be prevented by scriptlet.

On "hard reload", scriptlet is injected in time, but when I click on "Run" button or reload website then almost always rules are injected too late.

Video
Screen.Recording.2024-06-29.121904.mp4

I have checked old version 4.2.110 and it doesn't occur in this version (I have checked this version, because it was easy to download it from - https://github.com/AdguardTeam/AdguardBrowserExtension/releases, newer versions require to build them).

@kodiakhub
Copy link
Author

kodiakhub commented Jun 30, 2024

website reload/navigation

Also happens on first visit too.


Another issue: AdguardTeam/AdguardFilters#182571

Refresh the page (no hard refresh) anti-adblock appears and some rare popups.

Update: This issue a bit different (may not related), take a look this video:

Video

Trigger.mp4

Update 2: After some test, yes it's related. See the video:

Video 2

trigger.2.mp4

Update 3: On this comment, somehow issue has been fixed, see: AdguardTeam/AdguardFilters#182902

@kodiakhub kodiakhub changed the title Pop-ups leaking in some situation. Race condition issue in some situation. Jun 30, 2024
@adguard-bot adguard-bot changed the title Race condition issue in some situation. Scriptlets are injected too late on website reload/navigation Jul 2, 2024
@adguard-bot adguard-bot assigned maximtop and unassigned alexx7311 Jul 2, 2024
@kodiakhub
Copy link
Author

Possibility another one: AdguardTeam/AdguardFilters#182802

@kodiakhub
Copy link
Author

Possibility another one: AdguardTeam/AdguardFilters#182802

Just tested again 4.1.55 vs 4.3.53 see the video:

Video

uzay.manga.mp4

This is a serious issue as end user.

@kodiakhub
Copy link
Author

Another issue: AdguardTeam/AdguardFilters#182795

Tested AdGuard extension v4.1.55 vs v4.3.53 see the video:

Video

dizikorea.vip.mp4

I think different sites and different tests are enough. 😄

@zloyden
Copy link

zloyden commented Jul 10, 2024

Another issue AdguardTeam/AdguardFilters#183315.
The rules do not work in the extension 4.3.53. Perhaps they apply too late on the site. However, they work fine in the app.

cety.app#%#//scriptlet('prevent-addEventListener', 'click', 'openPopup')
cety.app#%#//scriptlet('set-constant', 'openPopup', 'noopFunc')
cety.app#%#//scriptlet('remove-node-text', 'script', 'openPopup')

To reproduce this, you should add the exception cety.app#@%#//scriptlet('abort-on-stack-trace', 'document.createElement', 'openPopup').

@Alex-302
Copy link
Member

AdguardTeam/AdguardFilters#183713

javball.com#%#//scriptlet('prevent-fetch', 'pagead2.googlesyndication.com')
javball.com#%#//scriptlet("abort-current-inline-script", "detectAdBlock")
javball.com#%#//scriptlet('abort-on-property-write', 'detectAdBlock')

@ameshkov
Copy link
Member

ameshkov commented Jul 19, 2024

This is a valid issue. We were trying to be too smart about injecting scriptlets.

Here's what we do now:

  1. Compute cosmeticResult in the blocking handler (onBeforeRequested started).
  2. Try injecting it in onResponseStarted (usually, it fails).
  3. Then go through every lifetime phase of a request and retry injecting.
  4. Clean up on onErrorOccurred / onComplete.

We made this too complicated while the proper solution is much, much easier.
There's absolutely no need for listening for every lifetime phase, etc.

Just do this:

        const injectDoTalogo = (tabId, injectDetails, attemptNumber = 0) => {
            console.log(`injecting to ${tabId}, attempt ${attemptNumber}`);

            // Use some reasonably big number, 100 or 1000.
            if (attemptNumber >= 100) {
                console.log('Stop trying, something is very wrong');
            }

            chrome.tabs.executeScript(tabId, injectDetails, () => {
                if (chrome.runtime.lastError) {
                    injectDoTalogo(tabId, injectDetails, attemptNumber + 1);
                } else {
                    console.log('Now we need to clean up');
                }
            });
        };

        chrome.webRequest.onResponseStarted.addListener((details) => {
            if (details.type !== 'main_frame') {
                return;
            }

            injectDoTalogo(details.tabId, {
                code: 'console.log(new Date().getTime() + " executing scripts!")',
                frameId: details.frameId,
                runAt: 'document_start',
                matchAboutBlank: true,
            });
        }, { urls: ['<all_urls>'] });

My testing shows that it guarantees the fastest injection.

It also simplifies the code a lot.

You can test it here:
https://ameshkov.w3spaces.com/saved-from-Tryit-2024-07-19-ohc1y.html

Add this rule to AG to see the difference: ameshkov.w3spaces.com#%#console.log(${new Date().getTime()} adguard injection);

image

@kodiakhub
Copy link
Author

This issue causes many problems. Like in reporting and even solving the problem.

See: AdguardTeam/AdguardFilters@516850f

Screenshot

image

I hope it gets fixed as soon as possible. 😄

@Alex-302
Copy link
Member

Same problem from time to time
AdguardTeam/AdguardFilters#188617

@Alex-302
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants