@@ -39,20 +39,8 @@ import µb from './background.js';
39
39
40
40
/******************************************************************************/
41
41
42
- // Platform-specific behavior.
43
-
44
- // https://github.com/uBlockOrigin/uBlock-issues/issues/42
45
- // https://bugzilla.mozilla.org/show_bug.cgi?id=1376932
46
- // Add proper version number detection once issue is fixed in Firefox.
47
- let dontCacheResponseHeaders =
48
- vAPI . webextFlavor . soup . has ( 'firefox' ) ;
49
-
50
- // The real actual webextFlavor value may not be set in stone, so listen
51
- // for possible future changes.
52
- window . addEventListener ( 'webextFlavor' , function ( ) {
53
- dontCacheResponseHeaders =
54
- vAPI . webextFlavor . soup . has ( 'firefox' ) ;
55
- } , { once : true } ) ;
42
+ // For platform-specific behavior.
43
+ const isGecko = vAPI . webextFlavor . isGecko ;
56
44
57
45
/******************************************************************************/
58
46
@@ -64,7 +52,7 @@ const patchLocalRedirectURL = url => url.charCodeAt(0) === 0x2F /* '/' */
64
52
65
53
// Intercept and filter web requests.
66
54
67
- const onBeforeRequest = function ( details ) {
55
+ function onBeforeRequest ( details ) {
68
56
const fctxt = µb . filteringContext . fromWebrequestDetails ( details ) ;
69
57
70
58
// Special handling for root document.
@@ -248,7 +236,7 @@ const onBeforeRootFrameRequest = function(fctxt) {
248
236
) ;
249
237
250
238
return { cancel : true } ;
251
- } ;
239
+ }
252
240
253
241
/******************************************************************************/
254
242
@@ -278,7 +266,7 @@ const onBeforeRootFrameRequest = function(fctxt) {
278
266
// | 2 | rg | rg | rs | rs |
279
267
// --------+--------+--------+--------+--------+--------+
280
268
281
- const shouldStrictBlock = function ( fctxt , loggerEnabled ) {
269
+ function shouldStrictBlock ( fctxt , loggerEnabled ) {
282
270
const snfe = staticNetFilteringEngine ;
283
271
284
272
// Explicit filtering: `document` option
@@ -338,7 +326,7 @@ const shouldStrictBlock = function(fctxt, loggerEnabled) {
338
326
// | 2 | - | - | - | x |
339
327
// --------+--------+--------+--------+--------+--------+
340
328
return { result : rs , logData : lds } ;
341
- } ;
329
+ }
342
330
343
331
/******************************************************************************/
344
332
@@ -348,7 +336,7 @@ const shouldStrictBlock = function(fctxt, loggerEnabled) {
348
336
// Do not strict-block if the filter pattern does not contain at least one
349
337
// token character.
350
338
351
- const validateStrictBlock = function ( fctxt , logData ) {
339
+ function validateStrictBlock ( fctxt , logData ) {
352
340
if ( typeof logData . regex !== 'string' ) { return false ; }
353
341
if ( typeof logData . raw === 'string' && / \w / . test ( logData . raw ) === false ) {
354
342
return false ;
@@ -370,13 +358,13 @@ const validateStrictBlock = function(fctxt, logData) {
370
358
const end = match . index + match [ 0 ] . length - hnpos - hnlen ;
371
359
return end === 0 || end === 1 ||
372
360
end === 2 && url . charCodeAt ( hnpos + hnlen ) === 0x2E /* '.' */ ;
373
- } ;
361
+ }
374
362
375
363
/******************************************************************************/
376
364
377
365
// Intercept and filter behind-the-scene requests.
378
366
379
- const onBeforeBehindTheSceneRequest = function ( fctxt ) {
367
+ function onBeforeBehindTheSceneRequest ( fctxt ) {
380
368
const pageStore = µb . pageStoreFromTabId ( fctxt . tabId ) ;
381
369
if ( pageStore === null ) { return ; }
382
370
@@ -436,7 +424,7 @@ const onBeforeBehindTheSceneRequest = function(fctxt) {
436
424
if ( result === 1 ) {
437
425
return { cancel : true } ;
438
426
}
439
- } ;
427
+ }
440
428
441
429
// https://github.com/uBlockOrigin/uBlock-issues/issues/1204
442
430
// Report the tabless network requests to all page stores matching the
@@ -491,7 +479,7 @@ const onBeforeBehindTheSceneRequest = function(fctxt) {
491
479
// - HTML filtering (requires ability to modify response body)
492
480
// - CSP injection
493
481
494
- const onHeadersReceived = function ( details ) {
482
+ function onHeadersReceived ( details ) {
495
483
496
484
const fctxt = µb . filteringContext . fromWebrequestDetails ( details ) ;
497
485
const isRootDoc = fctxt . itype === fctxt . MAIN_FRAME ;
@@ -503,6 +491,12 @@ const onHeadersReceived = function(details) {
503
491
}
504
492
if ( pageStore . getNetFilteringSwitch ( fctxt ) === false ) { return ; }
505
493
494
+ // To enforce strict-blocking with ipaddress option
495
+ if ( isRootDoc && fctxt . ipaddress ) {
496
+ const r = onBeforeRootFrameRequest ( fctxt ) ;
497
+ if ( r ) { return ( r ) ; }
498
+ }
499
+
506
500
if ( ( fctxt . itype & foilLargeMediaElement . TYPE_BITS ) !== 0 ) {
507
501
const result = foilLargeMediaElement ( details , fctxt , pageStore ) ;
508
502
if ( result !== undefined ) { return result ; }
@@ -589,7 +583,7 @@ const onHeadersReceived = function(details) {
589
583
// https://github.com/uBlockOrigin/uBlock-issues/issues/229
590
584
// Use `no-cache` instead of `no-cache, no-store, must-revalidate`, this
591
585
// allows Firefox's offline mode to work as expected.
592
- if ( modifiedHeaders && dontCacheResponseHeaders ) {
586
+ if ( modifiedHeaders && isGecko ) {
593
587
const cacheControl = µb . hiddenSettings . cacheControlForFirefox1376932 ;
594
588
if ( cacheControl !== 'unset' ) {
595
589
let i = headerIndexFromName ( 'cache-control' , responseHeaders ) ;
@@ -605,7 +599,7 @@ const onHeadersReceived = function(details) {
605
599
if ( modifiedHeaders ) {
606
600
return { responseHeaders } ;
607
601
}
608
- } ;
602
+ }
609
603
610
604
const reMediaContentTypes = / ^ (?: a u d i o | i m a g e | v i d e o ) \/ | (?: \/ o g g ) $ / ;
611
605
@@ -1011,7 +1005,7 @@ const bodyFilterer = (( ) => {
1011
1005
1012
1006
/******************************************************************************/
1013
1007
1014
- const injectCSP = function ( fctxt , pageStore , responseHeaders ) {
1008
+ function injectCSP ( fctxt , pageStore , responseHeaders ) {
1015
1009
const cspSubsets = [ ] ;
1016
1010
const requestType = fctxt . type ;
1017
1011
@@ -1138,11 +1132,11 @@ const injectCSP = function(fctxt, pageStore, responseHeaders) {
1138
1132
} ) ;
1139
1133
1140
1134
return true ;
1141
- } ;
1135
+ }
1142
1136
1143
1137
/******************************************************************************/
1144
1138
1145
- const injectPP = function ( fctxt , pageStore , responseHeaders ) {
1139
+ function injectPP ( fctxt , pageStore , responseHeaders ) {
1146
1140
const permissions = [ ] ;
1147
1141
const directives = staticNetFilteringEngine . matchAndFetchModifiers ( fctxt , 'permissions' ) ;
1148
1142
if ( directives !== undefined ) {
@@ -1168,7 +1162,7 @@ const injectPP = function(fctxt, pageStore, responseHeaders) {
1168
1162
} ) ;
1169
1163
1170
1164
return true ;
1171
- } ;
1165
+ }
1172
1166
1173
1167
/******************************************************************************/
1174
1168
@@ -1179,7 +1173,7 @@ const injectPP = function(fctxt, pageStore, responseHeaders) {
1179
1173
// cache. This works only when the webext API supports the `fromCache`
1180
1174
// property (Firefox).
1181
1175
1182
- const foilLargeMediaElement = function ( details , fctxt , pageStore ) {
1176
+ function foilLargeMediaElement ( details , fctxt , pageStore ) {
1183
1177
if ( details . fromCache === true ) { return ; }
1184
1178
1185
1179
onDemandHeaders . setHeaders ( details . responseHeaders ) ;
@@ -1195,7 +1189,7 @@ const foilLargeMediaElement = function(details, fctxt, pageStore) {
1195
1189
}
1196
1190
1197
1191
return { cancel : true } ;
1198
- } ;
1192
+ }
1199
1193
1200
1194
foilLargeMediaElement . TYPE_BITS = fc . IMAGE | fc . MEDIA | fc . XMLHTTPREQUEST ;
1201
1195
@@ -1280,6 +1274,23 @@ const strictBlockBypasser = {
1280
1274
1281
1275
/******************************************************************************/
1282
1276
1277
+ function onResponseStarted ( details ) {
1278
+ if ( details . tabId === - 1 ) { return ; }
1279
+ const pageStore = µb . pageStoreFromTabId ( details . tabId ) ;
1280
+ if ( pageStore === null ) { return ; }
1281
+ if ( pageStore . getNetFilteringSwitch ( ) === false ) { return ; }
1282
+ // To enforce strict-blocking with ipaddress option
1283
+ if ( isGecko === false && details . type === 'main_frame' ) {
1284
+ const fctxt = µb . filteringContext . fromWebrequestDetails ( details ) ;
1285
+ const r = onBeforeRootFrameRequest ( fctxt ) ;
1286
+ if ( r ?. cancel ) { return ; }
1287
+ }
1288
+ details . ancestors = pageStore . getFrameAncestorDetails ( details . frameId ) ;
1289
+ scriptletFilteringEngine . injectNow ( details ) ;
1290
+ }
1291
+
1292
+ /******************************************************************************/
1293
+
1283
1294
// https://github.com/uBlockOrigin/uBlock-issues/issues/2350
1284
1295
// Added scriptlet injection attempt at onResponseStarted time as per
1285
1296
// https://github.com/AdguardTeam/AdguardBrowserExtension/issues/1029 and
@@ -1296,27 +1307,13 @@ const webRequest = {
1296
1307
1297
1308
return ( ) => {
1298
1309
vAPI . net . setSuspendableListener ( onBeforeRequest ) ;
1299
- vAPI . net . addListener (
1300
- 'onHeadersReceived' ,
1301
- onHeadersReceived ,
1302
- { urls : [ 'http://*/*' , 'https://*/*' ] } ,
1303
- [ 'blocking' , 'responseHeaders' ]
1304
- ) ;
1305
- vAPI . net . addListener (
1306
- 'onResponseStarted' ,
1307
- details => {
1308
- if ( details . tabId === - 1 ) { return ; }
1309
- const pageStore = µb . pageStoreFromTabId ( details . tabId ) ;
1310
- if ( pageStore === null ) { return ; }
1311
- if ( pageStore . getNetFilteringSwitch ( ) === false ) { return ; }
1312
- details . ancestors = pageStore . getFrameAncestorDetails ( details . frameId ) ;
1313
- scriptletFilteringEngine . injectNow ( details ) ;
1314
- } ,
1315
- {
1316
- types : [ 'main_frame' , 'sub_frame' ] ,
1317
- urls : [ 'http://*/*' , 'https://*/*' ]
1318
- }
1319
- ) ;
1310
+ vAPI . net . addListener ( 'onHeadersReceived' , onHeadersReceived , {
1311
+ urls : [ 'http://*/*' , 'https://*/*' ]
1312
+ } , [ 'blocking' , 'responseHeaders' ] ) ;
1313
+ vAPI . net . addListener ( 'onResponseStarted' , onResponseStarted , {
1314
+ types : [ 'main_frame' , 'sub_frame' ] ,
1315
+ urls : [ 'http://*/*' , 'https://*/*' ]
1316
+ } ) ;
1320
1317
vAPI . defer . once ( { sec : µb . hiddenSettings . toolbarWarningTimeout } ) . then ( ( ) => {
1321
1318
if ( vAPI . net . hasUnprocessedRequest ( ) === false ) { return ; }
1322
1319
vAPI . net . removeUnprocessedRequest ( ) ;
0 commit comments