| Left: | ||
| Right: |
| LEFT | RIGHT |
|---|---|
| 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("matcher"); | 20 const {defaultMatcher} = require("matcher"); |
| 21 const {BlockingFilter, RegExpFilter} = require("filterClasses"); | 21 const {RegExpFilter, WhitelistFilter} = require("filterClasses"); |
| 22 const {getDecodedHostname, stringifyURL, isThirdParty} = require("url"); | 22 const {extractHostFromFrame, getDecodedHostname, |
| 23 isThirdParty, stringifyURL} = require("url"); | |
| 23 const {checkWhitelisted} = require("whitelisting"); | 24 const {checkWhitelisted} = require("whitelisting"); |
| 24 const {FilterNotifier} = require("filterNotifier"); | 25 const {FilterNotifier} = require("filterNotifier"); |
| 25 const devtools = require("devtools"); | 26 const devtools = require("devtools"); |
| 26 | 27 |
| 27 const {typeMap} = RegExpFilter; | 28 const {typeMap} = RegExpFilter; |
| 28 | 29 |
| 29 browser.webRequest.onHeadersReceived.addListener(details => | 30 browser.webRequest.onHeadersReceived.addListener(details => |
| 30 { | 31 { |
| 31 let url = new URL(details.url); | 32 let url = new URL(details.url); |
| 32 let urlString = stringifyURL(url); | 33 let urlString = stringifyURL(url); |
| 33 | 34 let parentFrame = ext.getFrame(details.tabId, details.parentFrameId); |
| 34 let thirdParty = false; | 35 let hostname = extractHostFromFrame(parentFrame) || getDecodedHostname(url); |
| 35 let hostname; | 36 let thirdParty = isThirdParty(url, hostname); |
| 36 let parentFrame = details.parentFrameId != -1 && | |
| 37 ext.getFrame(details.tabId, details.parentFrameId); | |
| 38 if (parentFrame && parentFrame.url) | |
| 39 { | |
| 40 hostname = getDecodedHostname(parentFrame.url); | |
|
Sebastian Noack
2018/03/15 19:50:53
Please use extractHostFromFrame().
kzar
2018/03/16 12:54:59
Done.
| |
| 41 thirdParty = isThirdParty(url, hostname); | |
| 42 } | |
| 43 else | |
| 44 hostname = getDecodedHostname(url); | |
| 45 | 37 |
| 46 let cspMatch = defaultMatcher.matchesAny(urlString, typeMap.CSP, hostname, | 38 let cspMatch = defaultMatcher.matchesAny(urlString, typeMap.CSP, hostname, |
| 47 thirdParty, null, false); | 39 thirdParty, null, false); |
| 48 if (cspMatch) | 40 if (cspMatch) |
| 49 { | 41 { |
| 50 let page = new ext.Page({id: details.tabId, url: details.url}); | 42 let page = new ext.Page({id: details.tabId, url: details.url}); |
| 51 let frame = ext.getFrame(details.tabId, details.frameId); | 43 let frame = ext.getFrame(details.tabId, details.frameId); |
| 52 if (checkWhitelisted(page, frame, typeMap.DOCUMENT | typeMap.CSP)) | 44 |
| 45 if (checkWhitelisted(page, frame)) | |
| 53 return; | 46 return; |
| 54 | 47 |
| 55 // To avoid an extra matchesAny for the common case we assumed no | 48 // To avoid an extra matchesAny for the common case we assumed no |
| 56 // $genericblock filters applied when searching for a matching $csp filter. | 49 // $genericblock filters applied when searching for a matching $csp filter. |
| 57 // We must now pay the price by first checking for a $genericblock filter | 50 // We must now pay the price by first checking for a $genericblock filter |
| 58 // and if necessary that our $csp filter is specific. | 51 // and if necessary that our $csp filter is specific. |
| 59 let specificOnly = checkWhitelisted(page, frame, typeMap.GENERICBLOCK); | 52 let specificOnly = !!checkWhitelisted(page, frame, typeMap.GENERICBLOCK); |
| 60 if (specificOnly) | 53 if (specificOnly) |
| 61 { | 54 { |
| 62 cspMatch = defaultMatcher.matchesAny(urlString, typeMap.CSP, hostname, | 55 cspMatch = defaultMatcher.matchesAny(urlString, typeMap.CSP, hostname, |
| 63 thirdParty, null, specificOnly); | 56 thirdParty, null, specificOnly); |
| 64 if (!(cspMatch instanceof BlockingFilter)) | 57 if (!cspMatch) |
| 65 return; | 58 return; |
| 66 } | 59 } |
| 67 | 60 |
| 68 FilterNotifier.emit("filter.hitCount", cspMatch, 0, 0, page); | |
| 69 devtools.logRequest(page, urlString, "CSP", hostname, thirdParty, null, | 61 devtools.logRequest(page, urlString, "CSP", hostname, thirdParty, null, |
| 70 specificOnly, cspMatch); | 62 specificOnly, cspMatch); |
| 63 FilterNotifier.emit("filter.hitCount", cspMatch, 0, 0, page); | |
| 64 | |
| 65 if (cspMatch instanceof WhitelistFilter) | |
| 66 return; | |
| 71 | 67 |
| 72 details.responseHeaders.push({ | 68 details.responseHeaders.push({ |
| 73 name: "Content-Security-Policy", | 69 name: "Content-Security-Policy", |
| 74 value: cspMatch.csp | 70 value: cspMatch.csp |
| 75 }); | 71 }); |
| 76 | 72 |
| 77 return {responseHeaders: details.responseHeaders}; | 73 return {responseHeaders: details.responseHeaders}; |
| 78 } | 74 } |
| 79 }, { | 75 }, { |
| 80 urls: ["http://*/*", "https://*/*"], | 76 urls: ["http://*/*", "https://*/*"], |
| 81 types: ["main_frame", "sub_frame"] | 77 types: ["main_frame", "sub_frame"] |
| 82 }, ["blocking", "responseHeaders"]); | 78 }, ["blocking", "responseHeaders"]); |
| LEFT | RIGHT |