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 // The webRequest API doesn't support WebSocket connection blocking in Microsoft | 20 const {defaultMatcher} = require("../adblockpluscore/lib/matcher"); |
21 // Edge and versions of Chrome before 58. Therefore for those we inject CSP | 21 const {RegExpFilter, WhitelistFilter} = |
22 // headers below as a workaround. See https://crbug.com/129353 and | 22 require("../adblockpluscore/lib/filterClasses"); |
23 // https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/10297376
/ | 23 const {extractHostFromFrame, getDecodedHostname, |
24 if (!browser.webRequest.ResourceType || | 24 isThirdParty, stringifyURL} = require("./url"); |
25 !("WEBSOCKET" in browser.webRequest.ResourceType)) | 25 const {checkWhitelisted} = require("./whitelisting"); |
| 26 const {FilterNotifier} = require("filterNotifier"); |
| 27 const devtools = require("./devtools"); |
| 28 |
| 29 const {typeMap} = RegExpFilter; |
| 30 |
| 31 browser.webRequest.onHeadersReceived.addListener(details => |
26 { | 32 { |
27 const {defaultMatcher} = require("../adblockpluscore/lib/matcher"); | 33 let url = new URL(details.url); |
28 const {BlockingFilter, RegExpFilter} = require( | 34 let urlString = stringifyURL(url); |
29 "../adblockpluscore/lib/filterClasses"); | 35 let parentFrame = ext.getFrame(details.tabId, details.parentFrameId); |
30 const {getDecodedHostname} = require("./url"); | 36 let hostname = extractHostFromFrame(parentFrame) || getDecodedHostname(url); |
31 const {checkWhitelisted} = require("./whitelisting"); | 37 let thirdParty = isThirdParty(url, hostname); |
32 | 38 |
33 browser.webRequest.onHeadersReceived.addListener(details => | 39 let cspMatch = defaultMatcher.matchesAny(urlString, typeMap.CSP, hostname, |
| 40 thirdParty, null, false); |
| 41 if (cspMatch) |
34 { | 42 { |
35 let hostname = getDecodedHostname(new URL(details.url)); | 43 let page = new ext.Page({id: details.tabId, url: details.url}); |
36 let match = defaultMatcher.matchesAny("", RegExpFilter.typeMap.WEBSOCKET, | 44 let frame = ext.getFrame(details.tabId, details.frameId); |
37 hostname, false, null, true); | 45 |
38 if (match instanceof BlockingFilter && | 46 if (checkWhitelisted(page, frame)) |
39 !checkWhitelisted(new ext.Page({id: details.tabId}), | 47 return; |
40 ext.getFrame(details.tabId, details.frameId))) | 48 |
| 49 // To avoid an extra matchesAny for the common case we assumed no |
| 50 // $genericblock filters applied when searching for a matching $csp filter. |
| 51 // We must now pay the price by first checking for a $genericblock filter |
| 52 // and if necessary that our $csp filter is specific. |
| 53 let specificOnly = !!checkWhitelisted(page, frame, null, |
| 54 typeMap.GENERICBLOCK); |
| 55 if (specificOnly) |
41 { | 56 { |
42 details.responseHeaders.push({ | 57 cspMatch = defaultMatcher.matchesAny(urlString, typeMap.CSP, hostname, |
43 name: "Content-Security-Policy", | 58 thirdParty, null, specificOnly); |
44 // We're blocking WebSockets here by adding a connect-src restriction | 59 if (!cspMatch) |
45 // since the Chrome extension API does not allow us to intercept them. | 60 return; |
46 // https://bugs.chromium.org/p/chromium/issues/detail?id=129353 | |
47 // | |
48 // We also need the frame-src and object-src restrictions since CSPs | |
49 // are not inherited from the parent for documents with data: and blob: | |
50 // URLs, see https://crbug.com/513860. | |
51 // | |
52 // We must use the deprecated child-src directive instead of worker-src | |
53 // since that's not supported yet (as of Chrome 56.) | |
54 // | |
55 // "http:" also includes "https:" implictly. | |
56 // https://www.chromestatus.com/feature/6653486812889088 | |
57 value: "connect-src http:; child-src http:; " + | |
58 "frame-src http:; object-src http:" | |
59 }); | |
60 return {responseHeaders: details.responseHeaders}; | |
61 } | 61 } |
62 }, { | 62 |
63 urls: ["http://*/*", "https://*/*"], | 63 devtools.logRequest([details.tabId], urlString, "CSP", hostname, |
64 // We must also intercept script requests since otherwise Web Workers can | 64 thirdParty, null, specificOnly, cspMatch); |
65 // be abused to execute scripts for which our Content Security Policy | 65 FilterNotifier.emit("filter.hitCount", cspMatch, 0, 0, [details.tabId]); |
66 // won't be injected. | 66 |
67 // https://github.com/gorhill/uBO-Extra/issues/19 | 67 if (cspMatch instanceof WhitelistFilter) |
68 types: ["main_frame", "sub_frame", "script"] | 68 return; |
69 }, ["blocking", "responseHeaders"]); | 69 |
70 } | 70 details.responseHeaders.push({ |
| 71 name: "Content-Security-Policy", |
| 72 value: cspMatch.csp |
| 73 }); |
| 74 |
| 75 return {responseHeaders: details.responseHeaders}; |
| 76 } |
| 77 }, { |
| 78 urls: ["http://*/*", "https://*/*"], |
| 79 types: ["main_frame", "sub_frame"] |
| 80 }, ["blocking", "responseHeaders"]); |
LEFT | RIGHT |