mirror of
https://github.com/gorhill/uBlock.git
synced 2024-09-03 17:49:39 +02:00
Apply response filtering according to mime type
Related issue: https://github.com/uBlockOrigin/uBlock-issues/issues/2833
This commit is contained in:
parent
971f36c78e
commit
6417f54299
@ -480,13 +480,6 @@ const onBeforeBehindTheSceneRequest = function(fctxt) {
|
|||||||
// - CSP injection
|
// - CSP injection
|
||||||
|
|
||||||
const onHeadersReceived = function(details) {
|
const onHeadersReceived = function(details) {
|
||||||
// https://github.com/uBlockOrigin/uBlock-issues/issues/610
|
|
||||||
// Process behind-the-scene requests in a special way.
|
|
||||||
if ( details.tabId < 0 ) {
|
|
||||||
if ( normalizeBehindTheSceneResponseHeaders(details) === false ) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const fctxt = µb.filteringContext.fromWebrequestDetails(details);
|
const fctxt = µb.filteringContext.fromWebrequestDetails(details);
|
||||||
const isRootDoc = fctxt.itype === fctxt.MAIN_FRAME;
|
const isRootDoc = fctxt.itype === fctxt.MAIN_FRAME;
|
||||||
@ -522,13 +515,14 @@ const onHeadersReceived = function(details) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const mime = mimeFromHeaders(responseHeaders);
|
||||||
|
|
||||||
// https://github.com/gorhill/uBlock/issues/2813
|
// https://github.com/gorhill/uBlock/issues/2813
|
||||||
// Disable the blocking of large media elements if the document is itself
|
// Disable the blocking of large media elements if the document is itself
|
||||||
// a media element: the resource was not prevented from loading so no
|
// a media element: the resource was not prevented from loading so no
|
||||||
// point to further block large media elements for the current document.
|
// point to further block large media elements for the current document.
|
||||||
if ( isRootDoc ) {
|
if ( isRootDoc ) {
|
||||||
const contentType = headerValueFromName('content-type', responseHeaders);
|
if ( reMediaContentTypes.test(mime) ) {
|
||||||
if ( reMediaContentTypes.test(contentType) ) {
|
|
||||||
pageStore.allowLargeMediaElementsUntil = 0;
|
pageStore.allowLargeMediaElementsUntil = 0;
|
||||||
// Fall-through: this could be an SVG document, which supports
|
// Fall-through: this could be an SVG document, which supports
|
||||||
// script tags.
|
// script tags.
|
||||||
@ -547,7 +541,7 @@ const onHeadersReceived = function(details) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
// html filtering
|
// html filtering
|
||||||
if ( isRootDoc || fctxt.itype === fctxt.SUB_FRAME ) {
|
if ( mime === 'text/html' || mime === 'application/xhtml+xml' ) {
|
||||||
const selectors = htmlFilteringEngine.retrieve(fctxt);
|
const selectors = htmlFilteringEngine.retrieve(fctxt);
|
||||||
if ( selectors ) {
|
if ( selectors ) {
|
||||||
jobs.push({
|
jobs.push({
|
||||||
@ -600,23 +594,19 @@ const reMediaContentTypes = /^(?:audio|image|video)\//;
|
|||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
// https://github.com/uBlockOrigin/uBlock-issues/issues/610
|
const mimeFromHeaders = headers => {
|
||||||
|
if ( Array.isArray(headers) === false ) { return ''; }
|
||||||
const normalizeBehindTheSceneResponseHeaders = function(details) {
|
return mimeFromContentType(headerValueFromName('content-type', headers));
|
||||||
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;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const mimeFromContentType = contentType => {
|
||||||
|
const match = reContentTypeMime.exec(contentType);
|
||||||
|
if ( match === null ) { return ''; }
|
||||||
|
return match[1].toLowerCase();
|
||||||
|
};
|
||||||
|
|
||||||
|
const reContentTypeMime = /^([^;]+)(?:;|$)/i;
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
function textResponseFilterer(session, directives) {
|
function textResponseFilterer(session, directives) {
|
||||||
@ -683,7 +673,6 @@ htmlResponseFilterer.xmlSerializer = null;
|
|||||||
|
|
||||||
const bodyFilterer = (( ) => {
|
const bodyFilterer = (( ) => {
|
||||||
const sessions = new Map();
|
const sessions = new Map();
|
||||||
const reContentTypeDocument = /^([^;]+)(?:;|$)/i;
|
|
||||||
const reContentTypeCharset = /charset=['"]?([^'" ]+)/i;
|
const reContentTypeCharset = /charset=['"]?([^'" ]+)/i;
|
||||||
const otherValidMimes = new Set([
|
const otherValidMimes = new Set([
|
||||||
'application/javascript',
|
'application/javascript',
|
||||||
@ -712,12 +701,6 @@ const bodyFilterer = (( ) => {
|
|||||||
return '';
|
return '';
|
||||||
};
|
};
|
||||||
|
|
||||||
const mimeFromContentType = contentType => {
|
|
||||||
const match = reContentTypeDocument.exec(contentType);
|
|
||||||
if ( match === null ) { return; }
|
|
||||||
return match[1].toLowerCase();
|
|
||||||
};
|
|
||||||
|
|
||||||
const charsetFromContentType = contentType => {
|
const charsetFromContentType = contentType => {
|
||||||
const match = reContentTypeCharset.exec(contentType);
|
const match = reContentTypeCharset.exec(contentType);
|
||||||
if ( match === null ) { return; }
|
if ( match === null ) { return; }
|
||||||
@ -932,6 +915,9 @@ const bodyFilterer = (( ) => {
|
|||||||
if ( contentType !== '' ) {
|
if ( contentType !== '' ) {
|
||||||
mime = mimeFromContentType(contentType);
|
mime = mimeFromContentType(contentType);
|
||||||
if ( mime === undefined ) { return; }
|
if ( mime === undefined ) { return; }
|
||||||
|
if ( mime.startsWith('text/') === false ) {
|
||||||
|
if ( otherValidMimes.has(mime) === false ) { return; }
|
||||||
|
}
|
||||||
charset = charsetFromContentType(contentType);
|
charset = charsetFromContentType(contentType);
|
||||||
if ( charset !== undefined ) {
|
if ( charset !== undefined ) {
|
||||||
charset = textEncode.normalizeCharset(charset);
|
charset = textEncode.normalizeCharset(charset);
|
||||||
@ -941,10 +927,6 @@ const bodyFilterer = (( ) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( mime.startsWith('text/') === false ) {
|
|
||||||
if ( otherValidMimes.has(mime) === false ) { return; }
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user