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-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2017 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 /** @module requestBlocker */ | 18 /** @module requestBlocker */ |
19 | 19 |
20 "use strict"; | 20 "use strict"; |
21 | 21 |
22 const {Filter, RegExpFilter, BlockingFilter} = require("filterClasses"); | 22 const {Filter, RegExpFilter, BlockingFilter} = require("filterClasses"); |
23 const {Subscription} = require("subscriptionClasses"); | 23 const {Subscription} = require("subscriptionClasses"); |
24 const {defaultMatcher} = require("matcher"); | 24 const {defaultMatcher} = require("matcher"); |
25 const {FilterNotifier} = require("filterNotifier"); | 25 const {FilterNotifier} = require("filterNotifier"); |
26 const {Prefs} = require("prefs"); | 26 const {Prefs} = require("prefs"); |
27 const {checkWhitelisted, getKey} = require("whitelisting"); | 27 const {checkWhitelisted, getKey} = require("whitelisting"); |
28 const {stringifyURL, extractHostFromFrame, isThirdParty} = require("url"); | 28 const {stringifyURL, extractHostFromFrame, isThirdParty} = require("url"); |
29 const {port} = require("messaging"); | 29 const {port} = require("messaging"); |
30 const devtools = require("devtools"); | 30 const devtools = require("devtools"); |
31 | 31 |
32 // Chrome can't distinguish between OBJECT_SUBREQUEST and OBJECT requests. | 32 // Chrome and Firefox (WebExtensions) can't distinguish between |
| 33 // OBJECT_SUBREQUEST and OBJECT requests. |
33 RegExpFilter.typeMap.OBJECT_SUBREQUEST = RegExpFilter.typeMap.OBJECT; | 34 RegExpFilter.typeMap.OBJECT_SUBREQUEST = RegExpFilter.typeMap.OBJECT; |
34 | 35 |
| 36 // Map of content types reported by the browser to the respecitve content types |
| 37 // used by Adblock Plus. Other content types are simply mapped to OTHER. |
| 38 let resourceTypes = new Map(function*() |
| 39 { |
| 40 for (let type in RegExpFilter.typeMap) |
| 41 yield [type.toLowerCase(), type]; |
| 42 |
| 43 yield ["sub_frame", "SUBDOCUMENT"]; |
| 44 |
| 45 // Treat navigator.sendBeacon() the same as <a ping>, it's essentially the |
| 46 // same concept - merely generalized. |
| 47 yield ["beacon", "PING"]; |
| 48 |
| 49 // Treat <img srcset> and <picture> the same as other images. |
| 50 yield ["imageset", "IMAGE"]; |
| 51 }()); |
| 52 |
35 function onBeforeRequestAsync(page, url, type, docDomain, | 53 function onBeforeRequestAsync(page, url, type, docDomain, |
36 thirdParty, sitekey, | 54 thirdParty, sitekey, |
37 specificOnly, filter) | 55 specificOnly, filter) |
38 { | 56 { |
39 if (filter) | 57 if (filter) |
40 FilterNotifier.emit("filter.hitCount", filter, 0, 0, page); | 58 FilterNotifier.emit("filter.hitCount", filter, 0, 0, page); |
41 | 59 |
42 if (devtools) | 60 if (devtools) |
| 61 { |
43 devtools.logRequest( | 62 devtools.logRequest( |
44 page, url, type, docDomain, | 63 page, url, type, docDomain, |
45 thirdParty, sitekey, | 64 thirdParty, sitekey, |
46 specificOnly, filter | 65 specificOnly, filter |
47 ); | 66 ); |
| 67 } |
48 } | 68 } |
49 | 69 |
50 ext.webRequest.onBeforeRequest.addListener((url, type, page, frame) => | 70 ext.webRequest.onBeforeRequest.addListener((url, type, page, frame) => |
51 { | 71 { |
52 if (checkWhitelisted(page, frame)) | 72 if (checkWhitelisted(page, frame)) |
53 return true; | 73 return true; |
54 | 74 |
55 let urlString = stringifyURL(url); | 75 let urlString = stringifyURL(url); |
56 let docDomain = extractHostFromFrame(frame); | 76 let docDomain = extractHostFromFrame(frame); |
57 let thirdParty = isThirdParty(url, docDomain); | 77 let thirdParty = isThirdParty(url, docDomain); |
58 let sitekey = getKey(page, frame); | 78 let sitekey = getKey(page, frame); |
59 | 79 |
60 let specificOnly = !!checkWhitelisted( | 80 let specificOnly = !!checkWhitelisted( |
61 page, frame, RegExpFilter.typeMap.GENERICBLOCK | 81 page, frame, RegExpFilter.typeMap.GENERICBLOCK |
62 ); | 82 ); |
63 | 83 |
| 84 let mappedType = resourceTypes.get(type) || "OTHER"; |
| 85 |
64 let filter = defaultMatcher.matchesAny( | 86 let filter = defaultMatcher.matchesAny( |
65 urlString, RegExpFilter.typeMap[type], | 87 urlString, RegExpFilter.typeMap[mappedType], |
66 docDomain, thirdParty, sitekey, specificOnly | 88 docDomain, thirdParty, sitekey, specificOnly |
67 ); | 89 ); |
68 | 90 |
69 setTimeout(onBeforeRequestAsync, 0, page, urlString, | 91 setTimeout(onBeforeRequestAsync, 0, page, urlString, |
70 type, docDomain, | 92 mappedType, docDomain, |
71 thirdParty, sitekey, | 93 thirdParty, sitekey, |
72 specificOnly, filter); | 94 specificOnly, filter); |
73 | 95 |
74 return !(filter instanceof BlockingFilter); | 96 return !(filter instanceof BlockingFilter); |
75 }); | 97 }); |
76 | 98 |
77 port.on("filters.collapse", (message, sender) => | 99 port.on("filters.collapse", (message, sender) => |
78 { | 100 { |
79 if (checkWhitelisted(sender.page, sender.frame)) | 101 if (checkWhitelisted(sender.page, sender.frame)) |
80 return false; | 102 return false; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 | 167 |
146 FilterNotifier.on("subscription.added", onFilterChange); | 168 FilterNotifier.on("subscription.added", onFilterChange); |
147 FilterNotifier.on("subscription.removed", onFilterChange); | 169 FilterNotifier.on("subscription.removed", onFilterChange); |
148 FilterNotifier.on("subscription.updated", onFilterChange); | 170 FilterNotifier.on("subscription.updated", onFilterChange); |
149 FilterNotifier.on("subscription.disabled", arg => onFilterChange(arg, true)); | 171 FilterNotifier.on("subscription.disabled", arg => onFilterChange(arg, true)); |
150 FilterNotifier.on("filter.added", onFilterChange); | 172 FilterNotifier.on("filter.added", onFilterChange); |
151 FilterNotifier.on("filter.removed", onFilterChange); | 173 FilterNotifier.on("filter.removed", onFilterChange); |
152 FilterNotifier.on("filter.disabled", arg => onFilterChange(arg, true)); | 174 FilterNotifier.on("filter.disabled", arg => onFilterChange(arg, true)); |
153 FilterNotifier.on("load", onFilterChange); | 175 FilterNotifier.on("load", onFilterChange); |
154 | 176 |
155 port.on("request.websocket", (msg, sender) => | 177 port.on("request.blockedByWrapper", (msg, sender) => |
156 { | 178 { |
| 179 // Chrome 58 onwards directly supports WebSocket blocking, so we can ignore |
| 180 // messages from the wrapper here (see https://crbug.com/129353). Hopefully |
| 181 // WebRTC will be supported soon too (see https://crbug.com/707683). |
| 182 if (msg.requestType.toUpperCase() in chrome.webRequest.ResourceType) |
| 183 return false; |
| 184 |
157 return ext.webRequest.onBeforeRequest._dispatch( | 185 return ext.webRequest.onBeforeRequest._dispatch( |
158 new URL(msg.url), | 186 new URL(msg.url), |
159 "WEBSOCKET", | 187 msg.requestType, |
160 sender.page, | 188 sender.page, |
161 sender.frame | 189 sender.frame |
162 ).indexOf(false) != -1; | 190 ).includes(false); |
163 }); | 191 }); |
OLD | NEW |