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 // The webRequest API doesn't support WebSocket connection blocking in Microsoft | 20 const {defaultMatcher} = require("matcher"); |
21 // Edge and versions of Chrome before 58. Therefore for those we inject CSP | 21 const {BlockingFilter, |
22 // headers below as a workaround. See https://crbug.com/129353 and | 22 RegExpFilter, |
23 // https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/10297376
/ | 23 WhitelistFilter} = require("filterClasses"); |
24 if (!browser.webRequest.ResourceType || | 24 const {getDecodedHostname, 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("matcher"); | 33 // Chrome seems to sometimes give the response type of "xmlhttprequest" |
28 const {BlockingFilter, RegExpFilter} = require("filterClasses"); | 34 // instead of "main_frame", but when it does the tabId is always -1 and |
29 const {getDecodedHostname} = require("url"); | 35 // the URL matches the initiator's URL[1]. |
30 const {checkWhitelisted} = require("whitelisting"); | 36 // Chrome also seems to not normalise the initiator URL[2]. |
| 37 // [1] - https://bugs.chromium.org/p/chromium/issues/detail?id=805649 |
| 38 // [2] - https://bugs.chromium.org/p/chromium/issues/detail?id=821042 |
| 39 if (details.type == "xmlhttprequest" && |
| 40 (details.tabId > -1 || details.initiator && |
| 41 details.url != new URL(details.initiator).toString())) |
| 42 { |
| 43 return; |
| 44 } |
31 | 45 |
32 browser.webRequest.onHeadersReceived.addListener(details => | 46 // To avoid an extra matchesAny for the common case let's assume no |
| 47 // $genericblock filters apply when searching for a matching $csp filter, |
| 48 // we can double check later if necessary. |
| 49 let specificOnly = false; |
| 50 |
| 51 let url = new URL(details.url); |
| 52 let hostname = getDecodedHostname(url); |
| 53 let cspMatch = defaultMatcher.matchesAny(details.url, typeMap.CSP, hostname, |
| 54 false, null, specificOnly); |
| 55 let page = null; |
| 56 |
| 57 if (cspMatch) |
33 { | 58 { |
34 let hostname = getDecodedHostname(new URL(details.url)); | 59 if (details.tabId > -1) |
35 let match = defaultMatcher.matchesAny("", RegExpFilter.typeMap.WEBSOCKET, | |
36 hostname, false, null, true); | |
37 if (match instanceof BlockingFilter && | |
38 !checkWhitelisted(new ext.Page({id: details.tabId}), | |
39 ext.getFrame(details.tabId, details.frameId))) | |
40 { | 60 { |
41 details.responseHeaders.push({ | 61 page = new ext.Page({id: details.tabId, url: details.url}); |
42 name: "Content-Security-Policy", | 62 |
43 // We're blocking WebSockets here by adding a connect-src restriction | 63 if (cspMatch instanceof WhitelistFilter) |
44 // since the Chrome extension API does not allow us to intercept them. | 64 { |
45 // https://bugs.chromium.org/p/chromium/issues/detail?id=129353 | 65 if (devtools) |
46 // | 66 { |
47 // We also need the frame-src and object-src restrictions since CSPs | 67 devtools.logWhitelistedDocument(page, details.url, typeMap.CSP, |
48 // are not inherited from the parent for documents with data: and blob: | 68 hostname, cspMatch); |
49 // URLs, see https://crbug.com/513860. | 69 } |
50 // | 70 return; |
51 // We must use the deprecated child-src directive instead of worker-src | 71 } |
52 // since that's not supported yet (as of Chrome 56.) | 72 |
53 // | 73 let frame = ext.getFrame(details.tabId, details.frameId); |
54 // "http:" also includes "https:" implictly. | 74 |
55 // https://www.chromestatus.com/feature/6653486812889088 | 75 if (checkWhitelisted(page, frame, typeMap.DOCUMENT | typeMap.CSP)) |
56 value: "connect-src http:; child-src http:; " + | 76 return; |
57 "frame-src http:; object-src http:" | 77 |
58 }); | 78 // We pay the price now for skipping the specificOnly check earlier, we |
59 return {responseHeaders: details.responseHeaders}; | 79 // must check again for a CSP filter, this time making sure it's specific. |
| 80 specificOnly = !!checkWhitelisted(page, frame, typeMap.GENERICBLOCK); |
| 81 if (specificOnly) |
| 82 { |
| 83 cspMatch = defaultMatcher.matchesAny(details.url, typeMap.CSP, hostname, |
| 84 false, null, specificOnly); |
| 85 if (!(cspMatch instanceof BlockingFilter)) |
| 86 return; |
| 87 } |
| 88 |
| 89 FilterNotifier.emit("filter.hitCount", cspMatch, 0, 0, page); |
60 } | 90 } |
61 }, { | 91 // If we don't know the associated tab we'll have to check that if the URL |
62 urls: ["http://*/*", "https://*/*"], | 92 // is whitelisted manually. Things like $sitekey whitelisting and filter hit |
63 // We must also intercept script requests since otherwise Web Workers can | 93 // logging won't work, but it's the best we can do. |
64 // be abused to execute scripts for which our Content Security Policy | 94 else if (cspMatch instanceof WhitelistFilter || |
65 // won't be injected. | 95 defaultMatcher.whitelist.matchesAny(stringifyURL(url), |
66 // https://github.com/gorhill/uBO-Extra/issues/19 | 96 typeMap.DOCUMENT | typeMap.CSP, |
67 types: ["main_frame", "sub_frame", "script"] | 97 hostname)) |
68 }, ["blocking", "responseHeaders"]); | 98 { |
69 } | 99 return; |
| 100 } |
| 101 |
| 102 if (devtools) |
| 103 { |
| 104 devtools.logRequest(page, details.url, "CSP", hostname, |
| 105 false, null, specificOnly, cspMatch); |
| 106 } |
| 107 |
| 108 details.responseHeaders.push({ |
| 109 name: "Content-Security-Policy", |
| 110 value: cspMatch.csp |
| 111 }); |
| 112 |
| 113 return {responseHeaders: details.responseHeaders}; |
| 114 } |
| 115 }, { |
| 116 urls: ["http://*/*", "https://*/*"], |
| 117 types: ["main_frame", "sub_frame", "xmlhttprequest"] |
| 118 }, ["blocking", "responseHeaders"]); |
OLD | NEW |