| OLD | NEW | 
|---|
| 1 /* | 1 /* | 
| 2  * This file is part of Adblock Plus <https://adblockplus.org/>, | 2  * This file is part of Adblock Plus <https://adblockplus.org/>, | 
| 3  * Copyright (C) 2006-present eyeo GmbH | 3  * Copyright (C) 2006-present eyeo GmbH | 
| 4  * | 4  * | 
| 5  * Adblock Plus is free software: you can redistribute it and/or modify | 5  * Adblock Plus is free software: you can redistribute it and/or modify | 
| 6  * it under the terms of the GNU General Public License version 3 as | 6  * it under the terms of the GNU General Public License version 3 as | 
| 7  * published by the Free Software Foundation. | 7  * published by the Free Software Foundation. | 
| 8  * | 8  * | 
| 9  * Adblock Plus is distributed in the hope that it will be useful, | 9  * Adblock Plus is distributed in the hope that it will be useful, | 
| 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
| 12  * GNU General Public License for more details. | 12  * GNU General Public License for more details. | 
| 13  * | 13  * | 
| 14  * You should have received a copy of the GNU General Public License | 14  * You should have received a copy of the GNU General Public License | 
| 15  * along with Adblock Plus.  If not, see <http://www.gnu.org/licenses/>. | 15  * along with Adblock Plus.  If not, see <http://www.gnu.org/licenses/>. | 
| 16  */ | 16  */ | 
| 17 | 17 | 
| 18 "use strict"; | 18 "use strict"; | 
| 19 | 19 | 
| 20 const {defaultMatcher} = require("../adblockpluscore/lib/matcher"); | 20 const {defaultMatcher} = require("../adblockpluscore/lib/matcher"); | 
| 21 const {RegExpFilter, WhitelistFilter} = | 21 const {RegExpFilter, WhitelistFilter} = | 
| 22   require("../adblockpluscore/lib/filterClasses"); | 22   require("../adblockpluscore/lib/filterClasses"); | 
| 23 const {isThirdParty} = require("../adblockpluscore/lib/domain"); |  | 
| 24 const {extractHostFromFrame} = require("./url"); | 23 const {extractHostFromFrame} = require("./url"); | 
| 25 const {checkWhitelisted} = require("./whitelisting"); | 24 const {checkWhitelisted} = require("./whitelisting"); | 
| 26 const {filterNotifier} = require("filterNotifier"); | 25 const {filterNotifier} = require("filterNotifier"); | 
| 27 const {logRequest} = require("./hitLogger"); | 26 const {logRequest} = require("./hitLogger"); | 
| 28 | 27 | 
| 29 const {typeMap} = RegExpFilter; | 28 const {typeMap} = RegExpFilter; | 
| 30 | 29 | 
| 31 browser.webRequest.onHeadersReceived.addListener(details => | 30 browser.webRequest.onHeadersReceived.addListener(details => | 
| 32 { | 31 { | 
| 33   let url = new URL(details.url); | 32   let url = new URL(details.url); | 
| 34   let parentFrame = ext.getFrame(details.tabId, details.parentFrameId); | 33   let parentFrame = ext.getFrame(details.tabId, details.parentFrameId); | 
| 35   let hostname = extractHostFromFrame(parentFrame) || url.hostname; | 34   let hostname = extractHostFromFrame(parentFrame) || url.hostname; | 
| 36   let thirdParty = isThirdParty(url, hostname); |  | 
| 37 | 35 | 
| 38   let cspMatch = defaultMatcher.matchesAny(details.url, typeMap.CSP, hostname, | 36   let cspMatch = defaultMatcher.matchesAny(url, typeMap.CSP, hostname, | 
| 39                                            thirdParty, null, false); | 37                                            null, false); | 
| 40   if (cspMatch) | 38   if (cspMatch) | 
| 41   { | 39   { | 
| 42     let page = new ext.Page({id: details.tabId, url: details.url}); | 40     let page = new ext.Page({id: details.tabId, url: details.url}); | 
| 43     let frame = ext.getFrame(details.tabId, details.frameId); | 41     let frame = ext.getFrame(details.tabId, details.frameId); | 
| 44 | 42 | 
| 45     if (checkWhitelisted(page, frame)) | 43     if (checkWhitelisted(page, frame)) | 
| 46       return; | 44       return; | 
| 47 | 45 | 
| 48     // To avoid an extra matchesAny for the common case we assumed no | 46     // To avoid an extra matchesAny for the common case we assumed no | 
| 49     // $genericblock filters applied when searching for a matching $csp filter. | 47     // $genericblock filters applied when searching for a matching $csp filter. | 
| 50     // We must now pay the price by first checking for a $genericblock filter | 48     // We must now pay the price by first checking for a $genericblock filter | 
| 51     // and if necessary that our $csp filter is specific. | 49     // and if necessary that our $csp filter is specific. | 
| 52     let specificOnly = !!checkWhitelisted(page, frame, null, | 50     let specificOnly = !!checkWhitelisted(page, frame, null, | 
| 53                                           typeMap.GENERICBLOCK); | 51                                           typeMap.GENERICBLOCK); | 
| 54     if (specificOnly && !(cspMatch instanceof WhitelistFilter)) | 52     if (specificOnly && !(cspMatch instanceof WhitelistFilter)) | 
| 55     { | 53     { | 
| 56       cspMatch = defaultMatcher.matchesAny(details.url, typeMap.CSP, hostname, | 54       cspMatch = defaultMatcher.matchesAny(url, typeMap.CSP, hostname, | 
| 57                                            thirdParty, null, specificOnly); | 55                                            null, specificOnly); | 
| 58       if (!cspMatch) | 56       if (!cspMatch) | 
| 59         return; | 57         return; | 
| 60     } | 58     } | 
| 61 | 59 | 
| 62     if (cspMatch instanceof WhitelistFilter) | 60     if (cspMatch instanceof WhitelistFilter) | 
| 63     { | 61     { | 
| 64       logRequest([details.tabId], { | 62       logRequest( | 
| 65         url: details.url, type: "CSP", docDomain: hostname, | 63         [details.tabId], | 
| 66         thirdParty, specificOnly | 64         {url: details.url, type: "CSP", docDomain: hostname, specificOnly}, | 
| 67       }, cspMatch); | 65         cspMatch | 
|  | 66       ); | 
| 68       filterNotifier.emit("filter.hitCount", cspMatch, 0, 0, [details.tabId]); | 67       filterNotifier.emit("filter.hitCount", cspMatch, 0, 0, [details.tabId]); | 
| 69       return; | 68       return; | 
| 70     } | 69     } | 
| 71 | 70 | 
| 72     let {blocking} = defaultMatcher.search(details.url, typeMap.CSP, hostname, | 71     let {blocking} = defaultMatcher.search(url, typeMap.CSP, hostname, null, | 
| 73                                            thirdParty, null, specificOnly, | 72                                            specificOnly, "blocking"); | 
| 74                                            "blocking"); |  | 
| 75     for (cspMatch of blocking) | 73     for (cspMatch of blocking) | 
| 76     { | 74     { | 
| 77       logRequest([details.tabId], { | 75       logRequest( | 
| 78         url: details.url, type: "CSP", docDomain: hostname, | 76         [details.tabId], | 
| 79         thirdParty, specificOnly | 77         {url: details.url, type: "CSP", docDomain: hostname, specificOnly}, | 
| 80       }, cspMatch); | 78         cspMatch | 
|  | 79       ); | 
| 81       filterNotifier.emit("filter.hitCount", cspMatch, 0, 0, [details.tabId]); | 80       filterNotifier.emit("filter.hitCount", cspMatch, 0, 0, [details.tabId]); | 
| 82 | 81 | 
| 83       details.responseHeaders.push({ | 82       details.responseHeaders.push({ | 
| 84         name: "Content-Security-Policy", | 83         name: "Content-Security-Policy", | 
| 85         value: cspMatch.csp | 84         value: cspMatch.csp | 
| 86       }); | 85       }); | 
| 87     } | 86     } | 
| 88 | 87 | 
| 89     return {responseHeaders: details.responseHeaders}; | 88     return {responseHeaders: details.responseHeaders}; | 
| 90   } | 89   } | 
| 91 }, { | 90 }, { | 
| 92   urls: ["http://*/*", "https://*/*"], | 91   urls: ["http://*/*", "https://*/*"], | 
| 93   types: ["main_frame", "sub_frame"] | 92   types: ["main_frame", "sub_frame"] | 
| 94 }, ["blocking", "responseHeaders"]); | 93 }, ["blocking", "responseHeaders"]); | 
| OLD | NEW | 
|---|