Skip to content

Commit

Permalink
Apply response filtering according to mime type
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed Nov 6, 2023
1 parent 971f36c commit 6417f54
Showing 1 changed file with 18 additions and 36 deletions.
54 changes: 18 additions & 36 deletions src/js/traffic.js
Original file line number Diff line number Diff line change
Expand Up @@ -480,13 +480,6 @@ const onBeforeBehindTheSceneRequest = function(fctxt) {
// - CSP injection

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 isRootDoc = fctxt.itype === fctxt.MAIN_FRAME;
Expand Down Expand Up @@ -522,13 +515,14 @@ const onHeadersReceived = function(details) {
}
}

const mime = mimeFromHeaders(responseHeaders);

// https://github.com/gorhill/uBlock/issues/2813
// Disable the blocking of large media elements if the document is itself
// a media element: the resource was not prevented from loading so no
// point to further block large media elements for the current document.
if ( isRootDoc ) {
const contentType = headerValueFromName('content-type', responseHeaders);
if ( reMediaContentTypes.test(contentType) ) {
if ( reMediaContentTypes.test(mime) ) {
pageStore.allowLargeMediaElementsUntil = 0;
// Fall-through: this could be an SVG document, which supports
// script tags.
Expand All @@ -547,7 +541,7 @@ const onHeadersReceived = function(details) {
});
}
// html filtering
if ( isRootDoc || fctxt.itype === fctxt.SUB_FRAME ) {
if ( mime === 'text/html' || mime === 'application/xhtml+xml' ) {
const selectors = htmlFilteringEngine.retrieve(fctxt);
if ( selectors ) {
jobs.push({
Expand Down Expand Up @@ -600,23 +594,19 @@ 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;
const mimeFromHeaders = headers => {
if ( Array.isArray(headers) === false ) { return ''; }
return mimeFromContentType(headerValueFromName('content-type', headers));
};

const mimeFromContentType = contentType => {
const match = reContentTypeMime.exec(contentType);
if ( match === null ) { return ''; }
return match[1].toLowerCase();
};

const reContentTypeMime = /^([^;]+)(?:;|$)/i;

/******************************************************************************/

function textResponseFilterer(session, directives) {
Expand Down Expand Up @@ -683,7 +673,6 @@ htmlResponseFilterer.xmlSerializer = null;

const bodyFilterer = (( ) => {
const sessions = new Map();
const reContentTypeDocument = /^([^;]+)(?:;|$)/i;
const reContentTypeCharset = /charset=['"]?([^'" ]+)/i;
const otherValidMimes = new Set([
'application/javascript',
Expand Down Expand Up @@ -712,12 +701,6 @@ const bodyFilterer = (( ) => {
return '';
};

const mimeFromContentType = contentType => {
const match = reContentTypeDocument.exec(contentType);
if ( match === null ) { return; }
return match[1].toLowerCase();
};

const charsetFromContentType = contentType => {
const match = reContentTypeCharset.exec(contentType);
if ( match === null ) { return; }
Expand Down Expand Up @@ -932,6 +915,9 @@ const bodyFilterer = (( ) => {
if ( contentType !== '' ) {
mime = mimeFromContentType(contentType);
if ( mime === undefined ) { return; }
if ( mime.startsWith('text/') === false ) {
if ( otherValidMimes.has(mime) === false ) { return; }
}
charset = charsetFromContentType(contentType);
if ( charset !== undefined ) {
charset = textEncode.normalizeCharset(charset);
Expand All @@ -941,10 +927,6 @@ const bodyFilterer = (( ) => {
}
}

if ( mime.startsWith('text/') === false ) {
if ( otherValidMimes.has(mime) === false ) { return; }
}

return true;
}
};
Expand Down

0 comments on commit 6417f54

Please sign in to comment.