Normalize tabless xhr to image/media in onHeadersReceived()

Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/610

The service worker-related issue affects both
Chromium/Firefox: the type of resources fetched
from a service worker are uniformly set to
`xmlhttprequest`, hence losing a key piece of
information for the purpose of accurate content
filtering.
This commit is contained in:
Raymond Hill 2019-05-31 09:02:07 -04:00
parent 7904bebffb
commit 27e8c8d468
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
1 changed files with 36 additions and 6 deletions

View File

@ -418,13 +418,18 @@ const onBeforeMaybeSpuriousCSPReport = (function() {
// - CSP injection
const onHeadersReceived = function(details) {
const fctxt = µBlock.filteringContext.fromWebrequestDetails(details);
// Do not interfere with behind-the-scene requests.
if ( fctxt.tabId < 0 ) { return; }
// https://github.com/uBlockOrigin/uBlock-issues/issues/610
// Process behind-the-scene requests in a special way.
if (
details.tabId < 0 &&
normalizeBehindTheSceneResponseHeaders(details) === false
) {
return;
}
const µb = µBlock;
const requestType = details.type;
const fctxt = µb.filteringContext.fromWebrequestDetails(details);
const requestType = fctxt.type;
const isRootDoc = requestType === 'main_frame';
const isDoc = isRootDoc || requestType === 'sub_frame';
@ -493,6 +498,25 @@ const onHeadersReceived = function(details) {
const reMediaContentTypes = /^(?:audio|image|video)\//;
/******************************************************************************/
// https://github.com/uBlockOrigin/uBlock-issues/issues/610
const normalizeBehindTheSceneResponseHeaders = function(details) {
if ( details.type !== 'xmlhttprequest' ) { return false; }
const headers = details.responseHeaders;
if ( Array.isArray(headers) === false ) { return false; }
const contentType = headerValueFromName('content-type', headers);
if ( contentType === '' ) { return false; }
if ( reMediaContentTypes.test(contentType) === false ) { return false; }
if ( contentType.startsWith('image') ) {
details.type = 'image';
} else {
details.type = 'media';
}
return true;
};
/*******************************************************************************
The response body filterer is responsible for:
@ -1028,7 +1052,13 @@ return {
'onHeadersReceived',
onHeadersReceived,
{
types: [ 'main_frame', 'sub_frame', 'image', 'media' ],
types: [
'main_frame',
'sub_frame',
'image',
'media',
'xmlhttprequest',
],
urls: [ 'http://*/*', 'https://*/*' ],
},
[ 'blocking', 'responseHeaders' ]