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

Delta Between Two Patch Sets: lib/requestBlocker.js

Issue 29760707: Issue 6622 - Implement $rewrite filter option (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Left Patch Set: No longer have a "REWRITE" type in devtools. Log the rewritten to URL. Created May 4, 2018, 6:25 p.m.
Right Patch Set: Updated proper revision for adblockpluscore. No dependency change for ui Created May 18, 2018, 3:27 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 | « dependencies ('k') | no next file » | 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} = 22 const {Filter, RegExpFilter, BlockingFilter} =
23 require("../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 {extractHostFromFrame, isThirdParty} = require("./url");
30 const {port} = require("./messaging"); 30 const {port} = require("./messaging");
31 const devtools = require("./devtools"); 31 const {logRequest: hitLoggerLogRequest} = require("./hitLogger");
32 32
33 const extensionProtocol = new URL(browser.extension.getURL("")).protocol; 33 const extensionProtocol = new URL(browser.extension.getURL("")).protocol;
34 34
35 // Chrome can't distinguish between OBJECT_SUBREQUEST and OBJECT requests. 35 // Chrome can't distinguish between OBJECT_SUBREQUEST and OBJECT requests.
36 if (!browser.webRequest.ResourceType || 36 if (!browser.webRequest.ResourceType ||
37 !("OBJECT_SUBREQUEST" in browser.webRequest.ResourceType)) 37 !("OBJECT_SUBREQUEST" in browser.webRequest.ResourceType))
38 { 38 {
39 RegExpFilter.typeMap.OBJECT_SUBREQUEST = RegExpFilter.typeMap.OBJECT; 39 RegExpFilter.typeMap.OBJECT_SUBREQUEST = RegExpFilter.typeMap.OBJECT;
40 } 40 }
41 41
(...skipping 21 matching lines...) Expand all
63 if (!(browser.webRequest.ResourceType)) 63 if (!(browser.webRequest.ResourceType))
64 return; 64 return;
65 65
66 for (let type in browser.webRequest.ResourceType) 66 for (let type in browser.webRequest.ResourceType)
67 yield resourceTypes.get(browser.webRequest.ResourceType[type]) || "OTHER"; 67 yield resourceTypes.get(browser.webRequest.ResourceType[type]) || "OTHER";
68 68
69 // WEBRTC gets addressed through a workaround, even if the webRequest API is 69 // WEBRTC gets addressed through a workaround, even if the webRequest API is
70 // lacking support to block this kind of a request. 70 // lacking support to block this kind of a request.
71 yield "WEBRTC"; 71 yield "WEBRTC";
72 72
73 // These filter types aren't mapped to resource types. 73 // POPUP, CSP and ELEMHIDE filters aren't mapped to resource types.
74 yield "POPUP"; 74 yield "POPUP";
75 yield "ELEMHIDE"; 75 yield "ELEMHIDE";
76 yield "CSP"; 76 yield "CSP";
77 }()); 77 }());
78 78
79 function getDocumentInfo(page, frame, originUrl) 79 function getDocumentInfo(page, frame, originUrl)
80 { 80 {
81 return [ 81 return [
82 extractHostFromFrame(frame, originUrl), 82 extractHostFromFrame(frame, originUrl),
83 getKey(page, frame, originUrl), 83 getKey(page, frame, originUrl),
84 !!checkWhitelisted(page, frame, originUrl, 84 !!checkWhitelisted(page, frame, originUrl,
85 RegExpFilter.typeMap.GENERICBLOCK) 85 RegExpFilter.typeMap.GENERICBLOCK)
86 ]; 86 ];
87 } 87 }
88 88
89 function matchRequest(url, type, docDomain, sitekey, specificOnly) 89 function matchRequest(url, type, docDomain, sitekey, specificOnly)
90 { 90 {
91 let urlString = stringifyURL(url);
92 let thirdParty = isThirdParty(url, docDomain); 91 let thirdParty = isThirdParty(url, docDomain);
93 92 let filter = defaultMatcher.matchesAny(url.href, RegExpFilter.typeMap[type],
94 return [ 93 docDomain, thirdParty,
95 defaultMatcher.matchesAny(urlString, RegExpFilter.typeMap[type], 94 sitekey, specificOnly);
96 docDomain, thirdParty, sitekey, specificOnly), 95 return [filter, thirdParty];
97 urlString,
98 thirdParty
99 ];
100 } 96 }
101 97
102 function getRelatedTabIds(details) 98 function getRelatedTabIds(details)
103 { 99 {
104 // This is the common case, the request is associated with a single tab. 100 // This is the common case, the request is associated with a single tab.
105 // If tabId is -1, its not (e.g. the request was sent by 101 // If tabId is -1, its not (e.g. the request was sent by
106 // a Service/Shared Worker) and we have to identify the related tabs. 102 // a Service/Shared Worker) and we have to identify the related tabs.
107 if (details.tabId != -1) 103 if (details.tabId != -1)
108 return Promise.resolve([details.tabId]); 104 return Promise.resolve([details.tabId]);
109 105
110 let url; // Firefox provides "originUrl" indicating the 106 let url; // Firefox provides "originUrl" indicating the
111 if (details.originUrl) // URL of the tab that caused this request. 107 if (details.originUrl) // URL of the tab that caused this request.
112 url = details.originUrl; // In case of Service/Shared Worker, this is the 108 url = details.originUrl; // In case of Service/Shared Worker, this is the
113 // URL of the tab that caused the worker to spawn. 109 // URL of the tab that caused the worker to spawn.
114 110
115 else if (details.initiator) // Chromium >=63 provides "intiator" which 111 else if (details.initiator && details.initiator != "null")
116 url = details.initiator + "/*"; // is equivalent to "originUrl" on Firefox 112 url = details.initiator + "/*"; // Chromium >=63 provides "intiator" which
113 // is equivalent to "originUrl" on Firefox
117 // except that its not a full URL but just 114 // except that its not a full URL but just
118 // an origin (proto + host). 115 // an origin (proto + host).
119 else 116 else
120 return Promise.resolve([]); 117 return Promise.resolve([]);
121 118
122 return browser.tabs.query({url}).then(tabs => tabs.map(tab => tab.id)); 119 return browser.tabs.query({url}).then(tabs => tabs.map(tab => tab.id));
123 } 120 }
124 121
125 function logRequest(tabIds, url, type, docDomain, thirdParty, 122 function logRequest(tabIds, request, filter)
126 sitekey, specificOnly, filter, rewrittenTo)
127 { 123 {
128 if (filter) 124 if (filter)
129 FilterNotifier.emit("filter.hitCount", filter, 0, 0, tabIds); 125 FilterNotifier.emit("filter.hitCount", filter, 0, 0, tabIds);
130 126
131 devtools.logRequest( 127 hitLoggerLogRequest(tabIds, request, filter);
132 tabIds, url, type, docDomain,
133 thirdParty, sitekey,
134 specificOnly, filter, rewrittenTo
135 );
136 } 128 }
137 129
138 browser.webRequest.onBeforeRequest.addListener(details => 130 browser.webRequest.onBeforeRequest.addListener(details =>
139 { 131 {
140 // Never block top-level documents. 132 // Never block top-level documents.
141 if (details.type == "main_frame") 133 if (details.type == "main_frame")
142 return; 134 return;
143 135
144 // Filter out requests from non web protocols. Ideally, we'd explicitly 136 // Filter out requests from non web protocols. Ideally, we'd explicitly
145 // specify the protocols we are interested in (i.e. http://, https://, 137 // specify the protocols we are interested in (i.e. http://, https://,
146 // ws:// and wss://) with the url patterns, given below, when adding this 138 // ws:// and wss://) with the url patterns, given below, when adding this
147 // listener. But unfortunately, Chrome <=57 doesn't support the WebSocket 139 // listener. But unfortunately, Chrome <=57 doesn't support the WebSocket
148 // protocol and is causing an error if it is given. 140 // protocol and is causing an error if it is given.
149 let url = new URL(details.url); 141 let url = new URL(details.url);
150 if (url.protocol != "http:" && url.protocol != "https:" && 142 if (url.protocol != "http:" && url.protocol != "https:" &&
151 url.protocol != "ws:" && url.protocol != "wss:") 143 url.protocol != "ws:" && url.protocol != "wss:")
152 return; 144 return;
153 145
154 // Firefox provides us with the full origin URL, while Chromium (>=63) 146 // Firefox provides us with the full origin URL, while Chromium (>=63)
155 // provides only the protocol + host of the (top-level) document which 147 // provides only the protocol + host of the (top-level) document which
156 // the request originates from through the "initiator" property. 148 // the request originates from through the "initiator" property.
157 let originUrl = details.originUrl ? new URL(details.originUrl) : 149 let originUrl = null;
158 details.initiator ? new URL(details.initiator) : null; 150 if (details.originUrl)
151 originUrl = new URL(details.originUrl);
152 else if (details.initiator && details.initiator != "null")
153 originUrl = new URL(details.initiator);
159 154
160 // Ignore requests sent by extensions or by Firefox itself: 155 // Ignore requests sent by extensions or by Firefox itself:
161 // * Firefox intercepts requests sent by any extensions, indicated with 156 // * Firefox intercepts requests sent by any extensions, indicated with
162 // an "originURL" starting with "moz-extension:". 157 // an "originURL" starting with "moz-extension:".
163 // * Chromium intercepts requests sent by this extension only, indicated 158 // * Chromium intercepts requests sent by this extension only, indicated
164 // on Chromium >=63 with an "initiator" starting with "chrome-extension:". 159 // on Chromium >=63 with an "initiator" starting with "chrome-extension:".
165 // * On Firefox, requests that don't relate to any document or extension are 160 // * On Firefox, requests that don't relate to any document or extension are
166 // indicated with an "originUrl" starting with "chrome:". 161 // indicated with an "originUrl" starting with "chrome:".
167 if (originUrl && (originUrl.protocol == extensionProtocol || 162 if (originUrl && (originUrl.protocol == extensionProtocol ||
168 originUrl.protocol == "chrome:")) 163 originUrl.protocol == "chrome:"))
169 return; 164 return;
170 165
171 let page = new ext.Page({id: details.tabId}); 166 let page = new ext.Page({id: details.tabId});
172 let frame = ext.getFrame( 167 let frame = ext.getFrame(
173 details.tabId, 168 details.tabId,
174 // We are looking for the frame that contains the element which 169 // We are looking for the frame that contains the element which
175 // has triggered this request. For most requests (e.g. images) we 170 // has triggered this request. For most requests (e.g. images) we
176 // can just use the request's frame ID, but for subdocument requests 171 // can just use the request's frame ID, but for subdocument requests
177 // (e.g. iframes) we must instead use the request's parent frame ID. 172 // (e.g. iframes) we must instead use the request's parent frame ID.
178 details.type == "sub_frame" ? details.parentFrameId : details.frameId 173 details.type == "sub_frame" ? details.parentFrameId : details.frameId
179 ); 174 );
180 175
181 // On Chromium >= 63, if both the frame is unknown and we haven't get 176 // On Chromium >= 63, if both the frame is unknown and we haven't get
182 // an "initator", this implies a request sent by the browser itself 177 // an "initiator", this implies a request sent by the browser itself
183 // (on older versions of Chromium, due to the lack of "initator", 178 // (on older versions of Chromium, due to the lack of "initiator",
184 // this can also indicate a request sent by a Shared/Service Worker). 179 // this can also indicate a request sent by a Shared/Service Worker).
185 if (!frame && !originUrl) 180 if (!frame && !originUrl)
186 return; 181 return;
187 182
188 if (checkWhitelisted(page, frame, originUrl)) 183 if (checkWhitelisted(page, frame, originUrl))
189 return; 184 return;
190 185
191 let type = resourceTypes.get(details.type) || "OTHER"; 186 let type = resourceTypes.get(details.type) || "OTHER";
192 let [docDomain, sitekey, specificOnly] = getDocumentInfo(page, frame, 187 let [docDomain, sitekey, specificOnly] = getDocumentInfo(page, frame,
193 originUrl); 188 originUrl);
194 let [filter, urlString, thirdParty] = matchRequest(url, type, docDomain, 189 let [filter, thirdParty] = matchRequest(url, type, docDomain,
195 sitekey, specificOnly); 190 sitekey, specificOnly);
196 191
197 let result; 192 let result;
198 let rewrittenTo; 193 let rewrittenUrl;
199 194
200 if (filter instanceof BlockingFilter) 195 if (filter instanceof BlockingFilter)
201 { 196 {
202 if (filter.rewrite) 197 if (filter.rewrite)
203 { 198 {
204 let rewritten = filter.rewriteUrl(urlString); 199 rewrittenUrl = filter.rewriteUrl(details.url);
205 if (rewritten == urlString) 200 // If no rewrite happened (error, different origin), we'll
206 // we couldn't do the rewrite, so just let it through. 201 // return undefined in order to avoid an "infinite" loop.
207 return; 202 if (rewrittenUrl != details.url)
208 203 result = {redirectUrl: rewrittenUrl};
209 rewrittenTo = rewritten;
210 result = {redirectUrl: rewritten};
211 } 204 }
212 else 205 else
213 result = {cancel: true}; 206 result = {cancel: true};
214 } 207 }
215 208
216 getRelatedTabIds(details).then(tabIds => 209 getRelatedTabIds(details).then(tabIds =>
217 { 210 {
218 logRequest(tabIds, urlString, type, docDomain, 211 logRequest(
219 thirdParty, sitekey, specificOnly, filter, rewrittenTo); 212 tabIds,
213 {
214 url: details.url, type, docDomain, thirdParty,
215 sitekey, specificOnly, rewrittenUrl
216 },
217 filter
218 );
220 }); 219 });
221 220
222 return result; 221 return result;
223 }, {urls: ["<all_urls>"]}, ["blocking"]); 222 }, {urls: ["<all_urls>"]}, ["blocking"]);
224 223
225 port.on("filters.collapse", (message, sender) => 224 port.on("filters.collapse", (message, sender) =>
226 { 225 {
227 let {page, frame} = sender; 226 let {page, frame} = sender;
228 227
229 if (checkWhitelisted(page, frame)) 228 if (checkWhitelisted(page, frame))
(...skipping 19 matching lines...) Expand all
249 return blocked && Prefs.hidePlaceholders; 248 return blocked && Prefs.hidePlaceholders;
250 }); 249 });
251 250
252 port.on("request.blockedByRTCWrapper", (msg, sender) => 251 port.on("request.blockedByRTCWrapper", (msg, sender) =>
253 { 252 {
254 let {page, frame} = sender; 253 let {page, frame} = sender;
255 254
256 if (checkWhitelisted(page, frame)) 255 if (checkWhitelisted(page, frame))
257 return false; 256 return false;
258 257
258 let {url} = msg;
259 let [docDomain, sitekey, specificOnly] = getDocumentInfo(page, frame); 259 let [docDomain, sitekey, specificOnly] = getDocumentInfo(page, frame);
260 let [filter, url, thirdParty] = matchRequest(new URL(msg.url), 260 let [filter, thirdParty] = matchRequest(new URL(url), "WEBRTC", docDomain,
261 "WEBRTC", docDomain, 261 sitekey, specificOnly);
262 sitekey, specificOnly); 262 logRequest(
263 263 [sender.page.id],
264 logRequest([sender.page.id], url, "WEBRTC", docDomain, 264 {url, type: "WEBRTC", docDomain, thirdParty, sitekey, specificOnly},
265 thirdParty, sitekey, specificOnly, filter); 265 filter
266 );
266 267
267 return filter instanceof BlockingFilter; 268 return filter instanceof BlockingFilter;
268 }); 269 });
269 270
270 let ignoreFilterNotifications = false; 271 let ignoreFilterNotifications = false;
271 let handlerBehaviorChangedQuota = 272 let handlerBehaviorChangedQuota =
272 browser.webRequest.MAX_HANDLER_BEHAVIOR_CHANGED_CALLS_PER_10_MINUTES; 273 browser.webRequest.MAX_HANDLER_BEHAVIOR_CHANGED_CALLS_PER_10_MINUTES;
273 274
274 function propagateHandlerBehaviorChange() 275 function propagateHandlerBehaviorChange()
275 { 276 {
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
327 } 328 }
328 329
329 FilterNotifier.on("subscription.added", onFilterChange); 330 FilterNotifier.on("subscription.added", onFilterChange);
330 FilterNotifier.on("subscription.removed", onFilterChange); 331 FilterNotifier.on("subscription.removed", onFilterChange);
331 FilterNotifier.on("subscription.updated", onFilterChange); 332 FilterNotifier.on("subscription.updated", onFilterChange);
332 FilterNotifier.on("subscription.disabled", arg => onFilterChange(arg, true)); 333 FilterNotifier.on("subscription.disabled", arg => onFilterChange(arg, true));
333 FilterNotifier.on("filter.added", onFilterChange); 334 FilterNotifier.on("filter.added", onFilterChange);
334 FilterNotifier.on("filter.removed", onFilterChange); 335 FilterNotifier.on("filter.removed", onFilterChange);
335 FilterNotifier.on("filter.disabled", arg => onFilterChange(arg, true)); 336 FilterNotifier.on("filter.disabled", arg => onFilterChange(arg, true));
336 FilterNotifier.on("load", onFilterChange); 337 FilterNotifier.on("load", onFilterChange);
LEFTRIGHT
« dependencies ('k') | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld