| 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 /** @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 {isThirdParty} = require("../adblockpluscore/lib/domain"); | |
| 28 const {Prefs} = require("./prefs"); | 27 const {Prefs} = require("./prefs"); |
| 29 const {checkWhitelisted, getKey} = require("./whitelisting"); | 28 const {checkWhitelisted, getKey} = require("./whitelisting"); |
| 30 const {extractHostFromFrame} = require("./url"); | 29 const {extractHostFromFrame} = require("./url"); |
| 31 const {port} = require("./messaging"); | 30 const {port} = require("./messaging"); |
| 32 const {logRequest: hitLoggerLogRequest} = require("./hitLogger"); | 31 const {logRequest: hitLoggerLogRequest} = require("./hitLogger"); |
| 33 | 32 |
| 34 const extensionProtocol = new URL(browser.extension.getURL("")).protocol; | 33 const extensionProtocol = new URL(browser.extension.getURL("")).protocol; |
| 35 | 34 |
| 36 // Chrome can't distinguish between OBJECT_SUBREQUEST and OBJECT requests. | 35 // Chrome can't distinguish between OBJECT_SUBREQUEST and OBJECT requests. |
| 37 if (!browser.webRequest.ResourceType || | 36 if (!browser.webRequest.ResourceType || |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 function getDocumentInfo(page, frame, originUrl) | 80 function getDocumentInfo(page, frame, originUrl) |
| 82 { | 81 { |
| 83 return [ | 82 return [ |
| 84 extractHostFromFrame(frame, originUrl), | 83 extractHostFromFrame(frame, originUrl), |
| 85 getKey(page, frame, originUrl), | 84 getKey(page, frame, originUrl), |
| 86 !!checkWhitelisted(page, frame, originUrl, | 85 !!checkWhitelisted(page, frame, originUrl, |
| 87 RegExpFilter.typeMap.GENERICBLOCK) | 86 RegExpFilter.typeMap.GENERICBLOCK) |
| 88 ]; | 87 ]; |
| 89 } | 88 } |
| 90 | 89 |
| 91 function matchRequest(url, type, docDomain, sitekey, specificOnly) | |
| 92 { | |
| 93 let thirdParty = isThirdParty(url, docDomain); | |
| 94 let filter = defaultMatcher.matchesAny(url.href, RegExpFilter.typeMap[type], | |
| 95 docDomain, thirdParty, | |
| 96 sitekey, specificOnly); | |
| 97 return [filter, thirdParty]; | |
| 98 } | |
| 99 | |
| 100 function getRelatedTabIds(details) | 90 function getRelatedTabIds(details) |
| 101 { | 91 { |
| 102 // This is the common case, the request is associated with a single tab. | 92 // This is the common case, the request is associated with a single tab. |
| 103 // If tabId is -1, its not (e.g. the request was sent by | 93 // If tabId is -1, its not (e.g. the request was sent by |
| 104 // a Service/Shared Worker) and we have to identify the related tabs. | 94 // a Service/Shared Worker) and we have to identify the related tabs. |
| 105 if (details.tabId != -1) | 95 if (details.tabId != -1) |
| 106 return Promise.resolve([details.tabId]); | 96 return Promise.resolve([details.tabId]); |
| 107 | 97 |
| 108 let url; // Firefox provides "originUrl" indicating the | 98 let url; // Firefox provides "originUrl" indicating the |
| 109 if (details.originUrl) // URL of the tab that caused this request. | 99 if (details.originUrl) // URL of the tab that caused this request. |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 // this can also indicate a request sent by a Shared/Service Worker). | 171 // this can also indicate a request sent by a Shared/Service Worker). |
| 182 if (!frame && !originUrl) | 172 if (!frame && !originUrl) |
| 183 return; | 173 return; |
| 184 | 174 |
| 185 if (checkWhitelisted(page, frame, originUrl)) | 175 if (checkWhitelisted(page, frame, originUrl)) |
| 186 return; | 176 return; |
| 187 | 177 |
| 188 let type = resourceTypes.get(details.type) || "OTHER"; | 178 let type = resourceTypes.get(details.type) || "OTHER"; |
| 189 let [docDomain, sitekey, specificOnly] = getDocumentInfo(page, frame, | 179 let [docDomain, sitekey, specificOnly] = getDocumentInfo(page, frame, |
| 190 originUrl); | 180 originUrl); |
| 191 let [filter, thirdParty] = matchRequest(url, type, docDomain, | 181 let filter = defaultMatcher.matchesAny(url, RegExpFilter.typeMap[type], |
| 192 sitekey, specificOnly); | 182 docDomain, sitekey, specificOnly); |
| 193 | 183 |
| 194 let result; | 184 let result; |
| 195 let rewrittenUrl; | 185 let rewrittenUrl; |
| 196 | 186 |
| 197 if (filter instanceof BlockingFilter) | 187 if (filter instanceof BlockingFilter) |
| 198 { | 188 { |
| 199 if (typeof filter.rewrite == "string") | 189 if (typeof filter.rewrite == "string") |
| 200 { | 190 { |
| 201 rewrittenUrl = filter.rewriteUrl(details.url); | 191 rewrittenUrl = filter.rewriteUrl(details.url); |
| 202 // If no rewrite happened (error, different origin), we'll | 192 // If no rewrite happened (error, different origin), we'll |
| 203 // return undefined in order to avoid an "infinite" loop. | 193 // return undefined in order to avoid an "infinite" loop. |
| 204 if (rewrittenUrl != details.url) | 194 if (rewrittenUrl != details.url) |
| 205 result = {redirectUrl: rewrittenUrl}; | 195 result = {redirectUrl: rewrittenUrl}; |
| 206 } | 196 } |
| 207 else | 197 else |
| 208 result = {cancel: true}; | 198 result = {cancel: true}; |
| 209 } | 199 } |
| 210 | 200 |
| 211 getRelatedTabIds(details).then(tabIds => | 201 getRelatedTabIds(details).then(tabIds => |
| 212 { | 202 { |
| 213 logRequest( | 203 logRequest( |
| 214 tabIds, | 204 tabIds, |
| 215 { | 205 { |
| 216 url: details.url, type, docDomain, thirdParty, | 206 url: details.url, type, docDomain, |
| 217 sitekey, specificOnly, rewrittenUrl | 207 sitekey, specificOnly, rewrittenUrl |
| 218 }, | 208 }, |
| 219 filter | 209 filter |
| 220 ); | 210 ); |
| 221 }); | 211 }); |
| 222 | 212 |
| 223 return result; | 213 return result; |
| 224 }, {urls: ["<all_urls>"]}, ["blocking"]); | 214 }, {urls: ["<all_urls>"]}, ["blocking"]); |
| 225 | 215 |
| 226 port.on("filters.collapse", (message, sender) => | 216 port.on("filters.collapse", (message, sender) => |
| 227 { | 217 { |
| 228 let {page, frame} = sender; | 218 let {page, frame} = sender; |
| 229 | 219 |
| 230 if (checkWhitelisted(page, frame)) | 220 if (checkWhitelisted(page, frame)) |
| 231 return false; | 221 return false; |
| 232 | 222 |
| 233 let blocked = false; | 223 let blocked = false; |
| 234 let [docDomain, sitekey, specificOnly] = getDocumentInfo(page, frame); | 224 let [docDomain, sitekey, specificOnly] = getDocumentInfo(page, frame); |
| 235 | 225 |
| 236 for (let url of message.urls) | 226 for (let url of message.urls) |
| 237 { | 227 { |
| 238 let [filter] = matchRequest(new URL(url, message.baseURL), | 228 let filter = defaultMatcher.matchesAny(new URL(url, message.baseURL), |
| 239 message.mediatype, docDomain, | 229 RegExpFilter.typeMap[message.mediatyp
e], |
| 240 sitekey, specificOnly); | 230 docDomain, sitekey, specificOnly); |
| 241 | 231 |
| 242 if (filter instanceof BlockingFilter) | 232 if (filter instanceof BlockingFilter) |
| 243 { | 233 { |
| 244 if (filter.collapse != null) | 234 if (filter.collapse != null) |
| 245 return filter.collapse; | 235 return filter.collapse; |
| 246 blocked = true; | 236 blocked = true; |
| 247 } | 237 } |
| 248 } | 238 } |
| 249 | 239 |
| 250 return blocked && Prefs.hidePlaceholders; | 240 return blocked && Prefs.hidePlaceholders; |
| 251 }); | 241 }); |
| 252 | 242 |
| 253 port.on("request.blockedByRTCWrapper", (msg, sender) => | 243 port.on("request.blockedByRTCWrapper", (msg, sender) => |
| 254 { | 244 { |
| 255 let {page, frame} = sender; | 245 let {page, frame} = sender; |
| 256 | 246 |
| 257 if (checkWhitelisted(page, frame)) | 247 if (checkWhitelisted(page, frame)) |
| 258 return false; | 248 return false; |
| 259 | 249 |
| 260 let {url} = msg; | 250 let {url} = msg; |
| 261 let [docDomain, sitekey, specificOnly] = getDocumentInfo(page, frame); | 251 let [docDomain, sitekey, specificOnly] = getDocumentInfo(page, frame); |
| 262 let [filter, thirdParty] = matchRequest(new URL(url), "WEBRTC", docDomain, | 252 let filter = defaultMatcher.matchesAny(new URL(url), |
| 263 sitekey, specificOnly); | 253 RegExpFilter.typeMap.WEBRTC, |
| 254 docDomain, sitekey, specificOnly); |
| 264 logRequest( | 255 logRequest( |
| 265 [sender.page.id], | 256 [sender.page.id], |
| 266 {url, type: "WEBRTC", docDomain, thirdParty, sitekey, specificOnly}, | 257 {url, type: "WEBRTC", docDomain, sitekey, specificOnly}, |
| 267 filter | 258 filter |
| 268 ); | 259 ); |
| 269 | 260 |
| 270 return filter instanceof BlockingFilter; | 261 return filter instanceof BlockingFilter; |
| 271 }); | 262 }); |
| 272 | 263 |
| 273 let ignoreFilterNotifications = false; | 264 let ignoreFilterNotifications = false; |
| 274 let handlerBehaviorChangedQuota = | 265 let handlerBehaviorChangedQuota = |
| 275 browser.webRequest.MAX_HANDLER_BEHAVIOR_CHANGED_CALLS_PER_10_MINUTES; | 266 browser.webRequest.MAX_HANDLER_BEHAVIOR_CHANGED_CALLS_PER_10_MINUTES; |
| 276 | 267 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 330 } | 321 } |
| 331 | 322 |
| 332 filterNotifier.on("subscription.added", onFilterChange); | 323 filterNotifier.on("subscription.added", onFilterChange); |
| 333 filterNotifier.on("subscription.removed", arg => onFilterChange(arg, false)); | 324 filterNotifier.on("subscription.removed", arg => onFilterChange(arg, false)); |
| 334 filterNotifier.on("subscription.updated", onFilterChange); | 325 filterNotifier.on("subscription.updated", onFilterChange); |
| 335 filterNotifier.on("subscription.disabled", arg => onFilterChange(arg, true)); | 326 filterNotifier.on("subscription.disabled", arg => onFilterChange(arg, true)); |
| 336 filterNotifier.on("filter.added", onFilterChange); | 327 filterNotifier.on("filter.added", onFilterChange); |
| 337 filterNotifier.on("filter.removed", onFilterChange); | 328 filterNotifier.on("filter.removed", onFilterChange); |
| 338 filterNotifier.on("filter.disabled", arg => onFilterChange(arg, true)); | 329 filterNotifier.on("filter.disabled", arg => onFilterChange(arg, true)); |
| 339 filterNotifier.on("load", onFilterChange); | 330 filterNotifier.on("load", onFilterChange); |
| OLD | NEW |