Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: lib/requestBlocker.js

Issue 29713631: Issue 5760 - Use relative require paths (Closed)
Left Patch Set: Created March 3, 2018, 4:09 a.m.
Right Patch Set: Address PS4 comments, rebase Created April 5, 2018, 11:09 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « lib/prefs.js ('k') | lib/stats.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 /** @module requestBlocker */ 18 /** @module requestBlocker */
19 19
20 "use strict"; 20 "use strict";
21 21
22 const {Filter, RegExpFilter, BlockingFilter} = require( 22 const {Filter, RegExpFilter, BlockingFilter} =
23 "../adblockpluscore/lib/filterClasses"); 23 require("../adblockpluscore/lib/filterClasses");
24 const {Subscription} = require("../adblockpluscore/lib/subscriptionClasses"); 24 const {Subscription} = require("../adblockpluscore/lib/subscriptionClasses");
25 const {defaultMatcher} = require("../adblockpluscore/lib/matcher"); 25 const {defaultMatcher} = require("../adblockpluscore/lib/matcher");
26 const {FilterNotifier} = require("../adblockpluscore/lib/filterNotifier"); 26 const {FilterNotifier} = require("../adblockpluscore/lib/filterNotifier");
27 const {Prefs} = require("./prefs"); 27 const {Prefs} = require("./prefs");
28 const {checkWhitelisted, getKey} = require("./whitelisting"); 28 const {checkWhitelisted, getKey} = require("./whitelisting");
29 const {stringifyURL, extractHostFromFrame, isThirdParty} = require("./url"); 29 const {stringifyURL, extractHostFromFrame, isThirdParty} = require("./url");
30 const {port} = require("./messaging"); 30 const {port} = require("./messaging");
31 const devtools = require("./devtools"); 31 const devtools = require("./devtools");
32 32
33 // Chrome can't distinguish between OBJECT_SUBREQUEST and OBJECT requests. 33 // Chrome can't distinguish between OBJECT_SUBREQUEST and OBJECT requests.
(...skipping 23 matching lines...) Expand all
57 exports.filterTypes = new Set(function*() 57 exports.filterTypes = new Set(function*()
58 { 58 {
59 // Microsoft Edge does not have webRequest.ResourceType or the devtools panel. 59 // Microsoft Edge does not have webRequest.ResourceType or the devtools panel.
60 // Since filterTypes is only used by devtools, we can just bail out here. 60 // Since filterTypes is only used by devtools, we can just bail out here.
61 if (!(browser.webRequest.ResourceType)) 61 if (!(browser.webRequest.ResourceType))
62 return; 62 return;
63 63
64 for (let type in browser.webRequest.ResourceType) 64 for (let type in browser.webRequest.ResourceType)
65 yield resourceTypes.get(browser.webRequest.ResourceType[type]) || "OTHER"; 65 yield resourceTypes.get(browser.webRequest.ResourceType[type]) || "OTHER";
66 66
67 // WEBSOCKET and WEBRTC get addressed through workarounds, even if the 67 // WEBRTC gets addressed through a workaround, even if the webRequest API is
68 // webRequest API is lacking support to block these kind of requests. 68 // lacking support to block this kind of a request.
69 yield "WEBSOCKET";
70 yield "WEBRTC"; 69 yield "WEBRTC";
71 70
72 // POPUP and ELEMHIDE filters aren't blocked on the request level but by other 71 // POPUP, CSP and ELEMHIDE filters aren't mapped to resource types.
73 // means. They don't have a corresponding value in webRequest.ResourceType.
74 yield "POPUP"; 72 yield "POPUP";
75 yield "ELEMHIDE"; 73 yield "ELEMHIDE";
74 yield "CSP";
76 }()); 75 }());
77 76
78 function onBeforeRequestAsync(page, url, type, docDomain, 77 function getRelatedTabIds(details)
79 thirdParty, sitekey, 78 {
80 specificOnly, filter) 79 // This is the common case, the request is associated with a single tab.
81 { 80 // If tabId is -1, its not (e.g. the request was sent by
82 if (filter) 81 // a Service/Shared Worker) and we have to identify the related tabs.
83 FilterNotifier.emit("filter.hitCount", filter, 0, 0, page); 82 if (details.tabId != -1)
84 83 return Promise.resolve([details.tabId]);
85 if (devtools) 84
86 { 85 let url; // Firefox provides "originUrl" indicating the
86 if (details.originUrl) // URL of the tab that caused this request.
87 url = details.originUrl; // In case of Service/Shared Worker, this is the
88 // URL of the tab that caused the worker to spawn.
89
90 else if (details.initiator) // Chromium >=63 provides "intiator" which
91 url = details.initiator + "/*"; // is equivalent to "originUrl" on Firefox
92 // except that its not a full URL but just
93 // an origin (proto + host).
94 else
95 return Promise.resolve([]);
96
97 return browser.tabs.query({url}).then(tabs => tabs.map(tab => tab.id));
98 }
99
100 function logRequest(details, url, type, docDomain, thirdParty,
101 sitekey, specificOnly, filter)
102 {
103 getRelatedTabIds(details).then(tabIds =>
104 {
105 if (filter)
106 FilterNotifier.emit("filter.hitCount", filter, 0, 0, tabIds);
107
87 devtools.logRequest( 108 devtools.logRequest(
88 page, url, type, docDomain, 109 tabIds, url, type, docDomain,
89 thirdParty, sitekey, 110 thirdParty, sitekey,
90 specificOnly, filter 111 specificOnly, filter
91 ); 112 );
113 });
114 }
115
116 browser.webRequest.onBeforeRequest.addListener(details =>
117 {
118 // Never block top-level documents.
119 if (details.type == "main_frame")
120 return;
121
122 // Filter out requests from non web protocols. Ideally, we'd explicitly
123 // specify the protocols we are interested in (i.e. http://, https://,
124 // ws:// and wss://) with the url patterns, given below, when adding this
125 // listener. But unfortunately, Chrome <=57 doesn't support the WebSocket
126 // protocol and is causing an error if it is given.
127 let url = new URL(details.url);
128 if (url.protocol != "http:" && url.protocol != "https:" &&
129 url.protocol != "ws:" && url.protocol != "wss:")
130 return;
131
132 let originUrl = null;
133 if (details.originUrl)
134 {
135 originUrl = new URL(details.originUrl);
136
137 // Firefox (only) allows to intercept requests sent by the browser
138 // and other extensions. We don't want to block these.
139 if (originUrl.protocol == "chrome:" ||
140 originUrl.protocol == "moz-extension:")
141 return;
92 } 142 }
93 } 143 // Fallback to "initiator" on Chrome >=63. It doesn't include the
94 144 // path (unlike "originUrl" on Firefox), but is still good enough
95 ext.webRequest.onBeforeRequest.addListener((url, type, page, frame) => 145 // (in case the tab/frame is unknown) for the $domain filter option
96 { 146 // and most document exception rules which only match the domain part.
97 let docDomain = null; 147 else if (details.initiator)
98 let sitekey = null; 148 originUrl = new URL(details.initiator);
99 let specificOnly = false; 149
100 let thirdParty = false; 150 let page = null;
151 let frame = null;
152 if (details.tabId != -1)
153 {
154 page = new ext.Page({id: details.tabId});
155 frame = ext.getFrame(
156 details.tabId,
157 // We are looking for the frame that contains the element which
158 // has triggered this request. For most requests (e.g. images) we
159 // can just use the request's frame ID, but for subdocument requests
160 // (e.g. iframes) we must instead use the request's parent frame ID.
161 details.type == "sub_frame" ? details.parentFrameId : details.frameId
162 );
163 }
164
165 if (checkWhitelisted(page, frame, originUrl))
166 return;
167
101 let urlString = stringifyURL(url); 168 let urlString = stringifyURL(url);
102 169 let type = resourceTypes.get(details.type) || "OTHER";
103 170 let docDomain = extractHostFromFrame(frame, originUrl);
104 if (frame && page) 171 let thirdParty = isThirdParty(url, docDomain);
105 { 172 let sitekey = getKey(page, frame, originUrl);
106 if (checkWhitelisted(page, frame)) 173 let specificOnly = !!checkWhitelisted(page, frame, originUrl,
107 return true; 174 RegExpFilter.typeMap.GENERICBLOCK);
108
109 docDomain = extractHostFromFrame(frame);
110 sitekey = getKey(page, frame);
111 thirdParty = isThirdParty(url, docDomain);
112 specificOnly = !!checkWhitelisted(page, frame,
113 RegExpFilter.typeMap.GENERICBLOCK);
114 }
115
116 let mappedType = resourceTypes.get(type) || "OTHER";
117 175
118 let filter = defaultMatcher.matchesAny( 176 let filter = defaultMatcher.matchesAny(
119 urlString, RegExpFilter.typeMap[mappedType], 177 urlString, RegExpFilter.typeMap[type],
120 docDomain, thirdParty, sitekey, specificOnly 178 docDomain, thirdParty, sitekey, specificOnly
121 ); 179 );
122 180
123 setTimeout(onBeforeRequestAsync, 0, page, urlString, 181 logRequest(details, urlString, type, docDomain,
124 mappedType, docDomain, 182 thirdParty, sitekey, specificOnly, filter);
125 thirdParty, sitekey, 183
126 specificOnly, filter); 184 if (filter instanceof BlockingFilter)
127 185 return {cancel: true};
128 return !(filter instanceof BlockingFilter); 186 }, {urls: ["<all_urls>"]}, ["blocking"]);
129 });
130 187
131 port.on("filters.collapse", (message, sender) => 188 port.on("filters.collapse", (message, sender) =>
132 { 189 {
133 if (checkWhitelisted(sender.page, sender.frame)) 190 if (checkWhitelisted(sender.page, sender.frame))
134 return false; 191 return false;
135 192
136 let typeMask = RegExpFilter.typeMap[message.mediatype]; 193 let typeMask = RegExpFilter.typeMap[message.mediatype];
137 let documentHost = extractHostFromFrame(sender.frame); 194 let documentHost = extractHostFromFrame(sender.frame);
138 let sitekey = getKey(sender.page, sender.frame); 195 let sitekey = getKey(sender.page, sender.frame);
139 let blocked = false; 196 let blocked = false;
140 197
141 let specificOnly = checkWhitelisted( 198 let specificOnly = checkWhitelisted(
142 sender.page, sender.frame, 199 sender.page, sender.frame, null,
143 RegExpFilter.typeMap.GENERICBLOCK 200 RegExpFilter.typeMap.GENERICBLOCK
144 ); 201 );
145 202
146 for (let url of message.urls) 203 for (let url of message.urls)
147 { 204 {
148 let urlObj = new URL(url, message.baseURL); 205 let urlObj = new URL(url, message.baseURL);
149 let filter = defaultMatcher.matchesAny( 206 let filter = defaultMatcher.matchesAny(
150 stringifyURL(urlObj), 207 stringifyURL(urlObj),
151 typeMask, documentHost, 208 typeMask, documentHost,
152 isThirdParty(urlObj, documentHost), 209 isThirdParty(urlObj, documentHost),
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
199 256
200 FilterNotifier.on("subscription.added", onFilterChange); 257 FilterNotifier.on("subscription.added", onFilterChange);
201 FilterNotifier.on("subscription.removed", onFilterChange); 258 FilterNotifier.on("subscription.removed", onFilterChange);
202 FilterNotifier.on("subscription.updated", onFilterChange); 259 FilterNotifier.on("subscription.updated", onFilterChange);
203 FilterNotifier.on("subscription.disabled", arg => onFilterChange(arg, true)); 260 FilterNotifier.on("subscription.disabled", arg => onFilterChange(arg, true));
204 FilterNotifier.on("filter.added", onFilterChange); 261 FilterNotifier.on("filter.added", onFilterChange);
205 FilterNotifier.on("filter.removed", onFilterChange); 262 FilterNotifier.on("filter.removed", onFilterChange);
206 FilterNotifier.on("filter.disabled", arg => onFilterChange(arg, true)); 263 FilterNotifier.on("filter.disabled", arg => onFilterChange(arg, true));
207 FilterNotifier.on("load", onFilterChange); 264 FilterNotifier.on("load", onFilterChange);
208 265
209 port.on("request.blockedByWrapper", (msg, sender) => 266 port.on("request.blockedByRTCWrapper", (msg, sender) =>
210 { 267 {
211 // Chrome 58 onwards directly supports WebSocket blocking, so we can ignore
212 // messages from the wrapper here (see https://crbug.com/129353). Hopefully
213 // WebRTC will be supported soon too (see https://crbug.com/707683).
214 // Edge supports neither webRequest.ResourceType nor WebSocket blocking yet:
215 // https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/102973 76/
216 if (browser.webRequest.ResourceType &&
217 (msg.requestType.toUpperCase() in browser.webRequest.ResourceType))
218 {
219 return false;
220 }
221
222 return ext.webRequest.onBeforeRequest._dispatch( 268 return ext.webRequest.onBeforeRequest._dispatch(
223 new URL(msg.url), 269 new URL(msg.url),
224 msg.requestType, 270 "webrtc",
225 sender.page, 271 sender.page,
226 sender.frame 272 sender.frame
227 ).includes(false); 273 ).includes(false);
228 }); 274 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld