Apply response filtering according to mime type · gorhill/uBlock@6417f54 · GitHub | Latest TMZ Celebrity News & Gossip | Watch TMZ Live
Skip to content

Commit 6417f54

Browse files
committed
Apply response filtering according to mime type
Related issue: uBlockOrigin/uBlock-issues#2833
1 parent 971f36c commit 6417f54

File tree

1 file changed

+18
-36
lines changed

1 file changed

+18
-36
lines changed

src/js/traffic.js

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -480,13 +480,6 @@ const onBeforeBehindTheSceneRequest = function(fctxt) {
480480
// - CSP injection
481481

482482
const onHeadersReceived = function(details) {
483-
// https://github.com/uBlockOrigin/uBlock-issues/issues/610
484-
// Process behind-the-scene requests in a special way.
485-
if ( details.tabId < 0 ) {
486-
if ( normalizeBehindTheSceneResponseHeaders(details) === false ) {
487-
return;
488-
}
489-
}
490483

491484
const fctxt = µb.filteringContext.fromWebrequestDetails(details);
492485
const isRootDoc = fctxt.itype === fctxt.MAIN_FRAME;
@@ -522,13 +515,14 @@ const onHeadersReceived = function(details) {
522515
}
523516
}
524517

518+
const mime = mimeFromHeaders(responseHeaders);
519+
525520
// https://github.com/gorhill/uBlock/issues/2813
526521
// Disable the blocking of large media elements if the document is itself
527522
// a media element: the resource was not prevented from loading so no
528523
// point to further block large media elements for the current document.
529524
if ( isRootDoc ) {
530-
const contentType = headerValueFromName('content-type', responseHeaders);
531-
if ( reMediaContentTypes.test(contentType) ) {
525+
if ( reMediaContentTypes.test(mime) ) {
532526
pageStore.allowLargeMediaElementsUntil = 0;
533527
// Fall-through: this could be an SVG document, which supports
534528
// script tags.
@@ -547,7 +541,7 @@ const onHeadersReceived = function(details) {
547541
});
548542
}
549543
// html filtering
550-
if ( isRootDoc || fctxt.itype === fctxt.SUB_FRAME ) {
544+
if ( mime === 'text/html' || mime === 'application/xhtml+xml' ) {
551545
const selectors = htmlFilteringEngine.retrieve(fctxt);
552546
if ( selectors ) {
553547
jobs.push({
@@ -600,23 +594,19 @@ const reMediaContentTypes = /^(?:audio|image|video)\//;
600594

601595
/******************************************************************************/
602596

603-
// https://github.com/uBlockOrigin/uBlock-issues/issues/610
604-
605-
const normalizeBehindTheSceneResponseHeaders = function(details) {
606-
if ( details.type !== 'xmlhttprequest' ) { return false; }
607-
const headers = details.responseHeaders;
608-
if ( Array.isArray(headers) === false ) { return false; }
609-
const contentType = headerValueFromName('content-type', headers);
610-
if ( contentType === '' ) { return false; }
611-
if ( reMediaContentTypes.test(contentType) === false ) { return false; }
612-
if ( contentType.startsWith('image') ) {
613-
details.type = 'image';
614-
} else {
615-
details.type = 'media';
616-
}
617-
return true;
597+
const mimeFromHeaders = headers => {
598+
if ( Array.isArray(headers) === false ) { return ''; }
599+
return mimeFromContentType(headerValueFromName('content-type', headers));
600+
};
601+
602+
const mimeFromContentType = contentType => {
603+
const match = reContentTypeMime.exec(contentType);
604+
if ( match === null ) { return ''; }
605+
return match[1].toLowerCase();
618606
};
619607

608+
const reContentTypeMime = /^([^;]+)(?:;|$)/i;
609+
620610
/******************************************************************************/
621611

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

684674
const bodyFilterer = (( ) => {
685675
const sessions = new Map();
686-
const reContentTypeDocument = /^([^;]+)(?:;|$)/i;
687676
const reContentTypeCharset = /charset=['"]?([^'" ]+)/i;
688677
const otherValidMimes = new Set([
689678
'application/javascript',
@@ -712,12 +701,6 @@ const bodyFilterer = (( ) => {
712701
return '';
713702
};
714703

715-
const mimeFromContentType = contentType => {
716-
const match = reContentTypeDocument.exec(contentType);
717-
if ( match === null ) { return; }
718-
return match[1].toLowerCase();
719-
};
720-
721704
const charsetFromContentType = contentType => {
722705
const match = reContentTypeCharset.exec(contentType);
723706
if ( match === null ) { return; }
@@ -932,6 +915,9 @@ const bodyFilterer = (( ) => {
932915
if ( contentType !== '' ) {
933916
mime = mimeFromContentType(contentType);
934917
if ( mime === undefined ) { return; }
918+
if ( mime.startsWith('text/') === false ) {
919+
if ( otherValidMimes.has(mime) === false ) { return; }
920+
}
935921
charset = charsetFromContentType(contentType);
936922
if ( charset !== undefined ) {
937923
charset = textEncode.normalizeCharset(charset);
@@ -941,10 +927,6 @@ const bodyFilterer = (( ) => {
941927
}
942928
}
943929

944-
if ( mime.startsWith('text/') === false ) {
945-
if ( otherValidMimes.has(mime) === false ) { return; }
946-
}
947-
948930
return true;
949931
}
950932
};

0 commit comments

Comments
 (0)

TMZ Celebrity News – Breaking Stories, Videos & Gossip

Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.

Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.

🎥 Watch TMZ Live

TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beat—watch now and see what’s trending in Hollywood.