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

Improve prevent-fetch — set response.status to 0 if responseType is set to opaque #364

Closed
AdamWr opened this issue Oct 5, 2023 · 1 comment

Comments

@AdamWr
Copy link
Member

AdamWr commented Oct 5, 2023

Related to - AdguardTeam/AdguardFilters#163224

Steps to reproduce:

  1. Add this rule:
example.org#%#//scriptlet('prevent-fetch', 'pagead2.googlesyndication.com', '', 'opaque')
  1. Go to - https://example.org/
  2. Run in console:
const request = new Request('https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js', {
  method: 'HEAD',
  mode: 'no-cors'
});
const response = await fetch(request);
if (response.status !== 0) {
  alert('adblock');
}

With scriptlet enabled response.status is 200 so alert is displayed, but as far as I understand, if response.type is opaque, then response.status should be 0:

An opaque filtered response is a filtered response whose type is "opaque", URL list is « », status is 0, status message is the empty byte sequence, header list is « », body is null, and body info is a new response body info.

https://fetch.spec.whatwg.org/#concept-filtered-response-opaque

So maybe we could set status basing on responseType:

const response = new Response(responseBody, {
status: 200,
statusText: 'OK',
});

Perhaps also response.statusText should be set to '' and response.body to null, if responseType is set to opaque.

@AdamWr
Copy link
Member Author

AdamWr commented Nov 17, 2023

Also allow to set responseBody to empty string '':

let strResponseBody;
if (responseBody === '' || responseBody === 'emptyObj') {
strResponseBody = '{}';
} else if (responseBody === 'emptyArr') {
strResponseBody = '[]';
} else {
logMessage(source, `Invalid responseBody parameter: '${responseBody}'`);
return;
}

Related to this - AdguardTeam/AdguardFilters#166464

It looks like that website is checking if content of the response is ''.
Something like (just simple example):

const fetchTest = await fetch('/test');
const content = await fetchTest.text();
const expectedResponse = '';
if(content !== expectedResponse) {
  adblock
}

adguard pushed a commit that referenced this issue Nov 30, 2023
…Type is set to opaque. #364

Squashed commit of the following:

commit 78b5e74
Author: Adam Wróblewski <adam@adguard.com>
Date:   Thu Nov 30 11:30:02 2023 +0100

    Add emptyStr as value for responseBody
    Set response body to null, status to 0 and statusText "", if responseType is set to opaque
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