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

Add new scriptlet — 'trusted-replace-outbound-text' #410

Closed
Yuki2718 opened this issue Mar 4, 2024 · 1 comment
Closed

Add new scriptlet — 'trusted-replace-outbound-text' #410

Yuki2718 opened this issue Mar 4, 2024 · 1 comment

Comments

@Yuki2718
Copy link

Yuki2718 commented Mar 4, 2024

Add a new scriptlet to intercept atob() call and prune its text output

Background: YT checks if anti-adblock warning (auxiliaryUi.messageRenderers.bkaEnforcementMessageViewModel) is removed by blocker by checking playerResponse.responseContext.mainAppWebResponseContext.trackingParam which includes encoded info about the warning if it is sent. If we remove this encoded info together with the warning, we can safely remove the warning. This is currently done with $replace rules removing parts of trackingParam but this approach is risky. The encoded info is finally decoded with atob, and if we prune the info from the output of atob, we can safely remove the info. If you want to check by yourself, search for 282054944_a in /desktop_polymer.vflset script in the debugger and put a break point before this, and follow the processing step by step. Here's the moment decoding was done when anti-adb is not sent. If sent, the output will be longer and include \n|\nGauxiliaryUi.messageRenderers.bkaEnforcementMessageViewModel.displayType\x121ENFORCEMENT_MESSAGE_VIEW_MODEL_DISPLAY_TYPE_POPUP\nM\nEauxiliaryUi.messageRenderers.bkaEnforcementMessageViewModel.isVisible\x12\x04true.

Screenshot 1

mea

Actual rules working on Chrome as of writing:

www.youtube.com#%#(()=>{const wrapper=(target,thisArg,args)=>{let result = Reflect.apply(target,thisArg,args);console.debug('Result atob:',result);if(result.includes('bkaEnforcementMessage')){const modifiedContent=result.replace('\n|\nGauxiliaryUi.messageRenderers.bkaEnforcementMessageViewModel.displayType\x121ENFORCEMENT_MESSAGE_VIEW_MODEL_DISPLAY_TYPE_POPUP\nM\nEauxiliaryUi.messageRenderers.bkaEnforcementMessageViewModel.isVisible\x12\x04true','');console.debug('Modified atob:',modifiedContent);return modifiedContent;}return result;};const handler={apply:wrapper};window.atob=new Proxy(window.atob,handler);})();
www.youtube.com#%#//scriptlet('set-constant', 'ytInitialPlayerResponse.auxiliaryUi.messageRenderers.bkaEnforcementMessageViewModel', 'undefined')
www.youtube.com#@%#//scriptlet('trusted-replace-fetch-response', '/("trackingParam":"kx_fmPxhoPZR)[-_0-9A-Za-z]{150}[-_0-9A-Za-z]+?([-_0-9A-Za-z]{55}lLKPQ-SS"\})/', '$1$2', 'player?')
www.youtube.com#@%#//scriptlet('trusted-replace-fetch-response', '/("trackingParam":"k5DfmPxhoXpR)[-_0-9A-Za-z]{150}[-_0-9A-Za-z]+?([-_0-9A-Za-z]{151}lLKPQGiS"\})/', '$1$2', 'player?')
www.youtube.com#%#//scriptlet('json-prune', 'playerResponse.adPlacements playerResponse.playerAds playerResponse.adSlots adPlacements playerAds adSlots')

Screenshot 2

atob

The first line is what should be the new scriptlet. The next line overwrites auxiliaryUi.messageRenderers.bkaEnforcementMessageViewModel on Chromium at the first load of yt video. The third and forth rules disable the current rules doing the same on raw trackingParam, as we want to see the test rule works by itself. The last rule is to intentionally trigger the warning if the pruning failed.

May possibly be more generic scriptlet like trusted-prune-text-output with target function as an argument (in this case atob), 0-based position of tareget argument in the function, condition for the pruning to happen (e.g. input/output includes specified string or regex), and stack trace.

@Yuki2718
Copy link
Author

Now yt no longer uses atob or any other global function if yt.config_.EXPERIMENT_FLAGS.ab_sig_bit_dea is true. Probably we have to intercept base64-encoded intermediate representation of trackingParam, decode it, replace its text, encode again, and return. Here two possible filters working for the original case of auxiliaryUi.messageRenderers.bkaEnforcementMessageViewModel warning (not the ssap issue we're currently facing):

www.youtube.com#%#(()=>{const wrapper=(target,thisArg,args)=>{let result=Reflect.apply(target,thisArg,args);console.debug('Result Array join:',result);try{const decoded=atob(result);console.debug('Decoded:',decoded);if(decoded.includes('bkaEnforcementMessage')){const modifiedContent=result.replace(/\n.\n.auxiliaryUi\.messageRenderers\.bkaEnforcementMessageViewModel\.displayType.\dENFORCEMENT_MESSAGE_VIEW_MODEL_DISPLAY_TYPE_[A-Z]+\n.\n.auxiliaryUi\.messageRenderers\.bkaEnforcementMessageViewModel\.isVisible.{2}(?:tru|fals)e/,'');console.debug('Modified content:',modifiedContent);const encodeToBase64=btoa(modifiedContent);return encodeToBase64}}catch(e){} return result};const handler={apply:wrapper};window.Array.prototype.join=new Proxy(window.Array.prototype.join,handler)})()
www.youtube.com#%#(()=>{const wrapper=(target,argumentsList,newTarget)=>{const result=Reflect.construct(target,argumentsList,newTarget);try{const decoded=new TextDecoder().decode(result);console.debug('Decoded:',decoded);if(decoded.includes('bkaEnforcementMessage')){const modifiedContent=decoded.replace(/\n.\n.auxiliaryUi\.messageRenderers\.bkaEnforcementMessageViewModel\.displayType.\dENFORCEMENT_MESSAGE_VIEW_MODEL_DISPLAY_TYPE_[A-Z]+\n.\n.auxiliaryUi\.messageRenderers\.bkaEnforcementMessageViewModel\.isVisible.{2}(?:tru|fals)e/,'');const modifiedUint8Array=new TextEncoder().encode(modifiedContent);return modifiedUint8Array}}catch(e){} return result};const handler={construct:wrapper,};window.Uint8Array=new Proxy(Uint8Array,handler)})()

The first rule targets Array.prototype.join and the second Uint8Array. I propose adding an option to decode/encode input and output, otherwise the scriptlet here can trivially be circumvented like this.

adguard pushed a commit that referenced this issue May 21, 2024
Squashed commit of the following:

commit e7a0a2d
Merge: a6f85a7 45a42cc
Author: Adam Wróblewski <[email protected]>
Date:   Mon May 20 17:28:17 2024 +0200

    Merge branch 'master' into feature/AG-30879

commit a6f85a7
Author: Adam Wróblewski <[email protected]>
Date:   Mon May 20 18:26:45 2024 +0300

    AG-30879 Add ability to decode text in trusted-replace-outbound-text scriptlet

    Squashed commit of the following:

    commit fc7fcdc
    Author: Adam Wróblewski <[email protected]>
    Date:   Tue May 14 11:51:48 2024 +0200

        Rename to isValidBase64

    commit e87edc4
    Author: Adam Wróblewski <[email protected]>
    Date:   Tue May 7 10:53:14 2024 +0200

        Add logging for invalid base64 encoded strings

    commit 788f70b
    Author: Adam Wróblewski <[email protected]>
    Date:   Mon May 6 17:54:10 2024 +0200

        Add variable for logging decoded content
        Improve checkIfValidBase64 and add more tests

    commit f0f931e
    Author: Adam Wróblewski <[email protected]>
    Date:   Fri May 3 17:08:17 2024 +0200

        trusted-replace-outbound-text - add ability to decode base64 text content

commit ff3e186
Author: Adam Wróblewski <[email protected]>
Date:   Fri Apr 26 11:03:30 2024 +0200

    Update decription

commit 0693ec6
Author: Adam Wróblewski <[email protected]>
Date:   Fri Apr 26 10:25:39 2024 +0200

    Update description

commit 76c8016
Author: Adam Wróblewski <[email protected]>
Date:   Fri Apr 26 10:00:07 2024 +0200

    Log information that content is not a string

commit c88ce94
Author: Adam Wróblewski <[email protected]>
Date:   Fri Apr 26 09:34:46 2024 +0200

    Fix typo in function name

commit 7ebb300
Author: Adam Wróblewski <[email protected]>
Date:   Thu Apr 25 12:18:59 2024 +0200

    Log information about not modified content
    Add a test

commit 48545bf
Author: Adam Wróblewski <[email protected]>
Date:   Tue Apr 23 12:48:51 2024 +0200

    Set default parameters
    Update test

commit 2fcc905
Author: Adam Wróblewski <[email protected]>
Date:   Mon Apr 22 20:06:00 2024 +0200

    Add trusted-replace-outbound-text scriptlet
@adguard-bot adguard-bot changed the title Add a new scriptlet to intercept atob() call and prune its text output Add new scriptlet — 'trusted-replace-outbound-text' Jun 13, 2024
@slavaleleka slavaleleka added the Feature request Adding new feature label Aug 2, 2024
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

4 participants