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-2017 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 abp2blocklist */ | 18 /** @module abp2blocklist */ |
19 | 19 |
20 "use strict"; | 20 "use strict"; |
21 | 21 |
22 let filterClasses = require("filterClasses"); | 22 let filterClasses = require("filterClasses"); |
23 let punycode = require("punycode"); | 23 let punycode = require("punycode"); |
24 | 24 |
25 const selectorLimit = 5000; | 25 const selectorLimit = 5000; |
26 const typeMap = filterClasses.RegExpFilter.typeMap; | 26 const typeMap = filterClasses.RegExpFilter.typeMap; |
27 const whitelistableRequestTypes = (typeMap.IMAGE | 27 |
28 | typeMap.STYLESHEET | 28 const httpRequestTypes = typeMap.IMAGE | |
29 | typeMap.SCRIPT | 29 typeMap.STYLESHEET | |
30 | typeMap.FONT | 30 typeMap.SCRIPT | |
31 | typeMap.MEDIA | 31 typeMap.FONT | |
32 | typeMap.POPUP | 32 typeMap.MEDIA | |
33 | typeMap.OBJECT | 33 typeMap.POPUP | |
34 | typeMap.OBJECT_SUBREQUEST | 34 typeMap.OBJECT | |
35 | typeMap.XMLHTTPREQUEST | 35 typeMap.OBJECT_SUBREQUEST | |
36 | typeMap.WEBSOCKET | 36 typeMap.XMLHTTPREQUEST | |
37 | typeMap.WEBRTC | 37 typeMap.PING | |
38 | typeMap.PING | 38 typeMap.SUBDOCUMENT | |
39 | typeMap.SUBDOCUMENT | 39 typeMap.OTHER; |
40 | typeMap.OTHER); | 40 const rawRequestTypes = typeMap.XMLHTTPREQUEST | |
| 41 typeMap.WEBSOCKET | |
| 42 typeMap.WEBRTC | |
| 43 typeMap.OBJECT_SUBREQUEST | |
| 44 typeMap.PING | |
| 45 typeMap.OTHER; |
| 46 const whitelistableRequestTypes = httpRequestTypes | |
| 47 typeMap.WEBSOCKET | |
| 48 typeMap.WEBRTC; |
41 | 49 |
42 function parseDomains(domains, included, excluded) | 50 function parseDomains(domains, included, excluded) |
43 { | 51 { |
44 for (let domain in domains) | 52 for (let domain in domains) |
45 { | 53 { |
46 if (domain != "") | 54 if (domain != "") |
47 { | 55 { |
48 let enabled = domains[domain]; | 56 let enabled = domains[domain]; |
49 domain = punycode.toASCII(domain.toLowerCase()); | 57 domain = punycode.toASCII(domain.toLowerCase()); |
50 | 58 |
(...skipping 16 matching lines...) Expand all Loading... |
67 } | 75 } |
68 | 76 |
69 function getURLSchemes(contentType) | 77 function getURLSchemes(contentType) |
70 { | 78 { |
71 // If the given content type includes all supported URL schemes, simply | 79 // If the given content type includes all supported URL schemes, simply |
72 // return a single generic URL scheme pattern. This minimizes the size of the | 80 // return a single generic URL scheme pattern. This minimizes the size of the |
73 // generated rule set. The downside to this is that it will also match | 81 // generated rule set. The downside to this is that it will also match |
74 // schemes that we do not want to match (e.g. "ftp://"), but this can be | 82 // schemes that we do not want to match (e.g. "ftp://"), but this can be |
75 // mitigated by adding exceptions for those schemes. | 83 // mitigated by adding exceptions for those schemes. |
76 if (contentType & typeMap.WEBSOCKET && contentType & typeMap.WEBRTC && | 84 if (contentType & typeMap.WEBSOCKET && contentType & typeMap.WEBRTC && |
77 contentType & ~(typeMap.WEBSOCKET | typeMap.WEBRTC)) | 85 contentType & httpRequestTypes) |
78 return ["[^:]+:(//)?"]; | 86 return ["[^:]+:(//)?"]; |
79 | 87 |
80 let urlSchemes = []; | 88 let urlSchemes = []; |
81 | 89 |
82 if (contentType & typeMap.WEBSOCKET) | 90 if (contentType & typeMap.WEBSOCKET) |
83 urlSchemes.push("wss?://"); | 91 urlSchemes.push("wss?://"); |
84 | 92 |
85 if (contentType & typeMap.WEBRTC) | 93 if (contentType & typeMap.WEBRTC) |
86 urlSchemes.push("stuns?:", "turns?:"); | 94 urlSchemes.push("stuns?:", "turns?:"); |
87 | 95 |
88 if (contentType & ~(typeMap.WEBSOCKET | typeMap.WEBRTC)) | 96 if (contentType & httpRequestTypes) |
89 urlSchemes.push("https?://"); | 97 urlSchemes.push("https?://"); |
90 | 98 |
91 return urlSchemes; | 99 return urlSchemes; |
92 } | 100 } |
93 | 101 |
94 function findSubdomainsInList(domain, list) | 102 function findSubdomainsInList(domain, list) |
95 { | 103 { |
96 let subdomains = []; | 104 let subdomains = []; |
97 let suffixLength = domain.length + 1; | 105 let suffixLength = domain.length + 1; |
98 | 106 |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 if (contentType & typeMap.STYLESHEET) | 289 if (contentType & typeMap.STYLESHEET) |
282 types.push("style-sheet"); | 290 types.push("style-sheet"); |
283 if (contentType & typeMap.SCRIPT) | 291 if (contentType & typeMap.SCRIPT) |
284 types.push("script"); | 292 types.push("script"); |
285 if (contentType & typeMap.FONT) | 293 if (contentType & typeMap.FONT) |
286 types.push("font"); | 294 types.push("font"); |
287 if (contentType & (typeMap.MEDIA | typeMap.OBJECT)) | 295 if (contentType & (typeMap.MEDIA | typeMap.OBJECT)) |
288 types.push("media"); | 296 types.push("media"); |
289 if (contentType & typeMap.POPUP) | 297 if (contentType & typeMap.POPUP) |
290 types.push("popup"); | 298 types.push("popup"); |
291 if (contentType & (typeMap.XMLHTTPREQUEST | | 299 if (contentType & rawRequestTypes) |
292 typeMap.WEBSOCKET | | |
293 typeMap.WEBRTC | | |
294 typeMap.OBJECT_SUBREQUEST | | |
295 typeMap.PING | | |
296 typeMap.OTHER)) | |
297 { | |
298 types.push("raw"); | 300 types.push("raw"); |
299 } | |
300 if (contentType & typeMap.SUBDOCUMENT) | 301 if (contentType & typeMap.SUBDOCUMENT) |
301 types.push("document"); | 302 types.push("document"); |
302 | 303 |
303 return types; | 304 return types; |
304 } | 305 } |
305 | 306 |
306 function makeRuleCopies(trigger, action, urlSchemes) | 307 function makeRuleCopies(trigger, action, urlSchemes) |
307 { | 308 { |
308 let copies = []; | 309 let copies = []; |
309 | 310 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
344 trigger["top-url-filter-is-case-sensitive"] = true; | 345 trigger["top-url-filter-is-case-sensitive"] = true; |
345 } | 346 } |
346 | 347 |
347 function convertFilterAddRules(rules, filter, action, withResourceTypes, | 348 function convertFilterAddRules(rules, filter, action, withResourceTypes, |
348 exceptionDomains, contentType) | 349 exceptionDomains, contentType) |
349 { | 350 { |
350 if (!contentType) | 351 if (!contentType) |
351 contentType = filter.contentType; | 352 contentType = filter.contentType; |
352 | 353 |
353 // If WebSocket or WebRTC are given along with other options but not | 354 // If WebSocket or WebRTC are given along with other options but not |
354 // including all three of WebSocket, WebRTC, and XMLHttpRequest, we must | 355 // including all three of WebSocket, WebRTC, and at least one HTTP raw type, |
355 // generate multiple rules. For example, for the filter | 356 // we must generate multiple rules. For example, for the filter |
356 // "foo$websocket,image", we must generate one rule with "^wss?://" and "raw" | 357 // "foo$websocket,image", we must generate one rule with "^wss?://" and "raw" |
357 // and another rule with "^https?://" and "image". If we merge the two, we | 358 // and another rule with "^https?://" and "image". If we merge the two, we |
358 // end up blocking requests of type XMLHttpRequest inadvertently. | 359 // end up blocking requests of all HTTP raw types (e.g. XMLHttpRequest) |
| 360 // inadvertently. |
359 if ((contentType & typeMap.WEBSOCKET && contentType != typeMap.WEBSOCKET && | 361 if ((contentType & typeMap.WEBSOCKET && contentType != typeMap.WEBSOCKET && |
360 !(contentType & typeMap.WEBRTC && | 362 !(contentType & typeMap.WEBRTC && |
361 contentType & typeMap.XMLHTTPREQUEST)) || | 363 contentType & rawRequestTypes & httpRequestTypes)) || |
362 (contentType & typeMap.WEBRTC && contentType != typeMap.WEBRTC && | 364 (contentType & typeMap.WEBRTC && contentType != typeMap.WEBRTC && |
363 !(contentType & typeMap.WEBSOCKET && | 365 !(contentType & typeMap.WEBSOCKET && |
364 contentType & typeMap.XMLHTTPREQUEST))) | 366 contentType & rawRequestTypes & httpRequestTypes))) |
365 { | 367 { |
366 if (contentType & typeMap.WEBSOCKET) | 368 if (contentType & typeMap.WEBSOCKET) |
367 { | 369 { |
368 convertFilterAddRules(rules, filter, action, withResourceTypes, | 370 convertFilterAddRules(rules, filter, action, withResourceTypes, |
369 exceptionDomains, typeMap.WEBSOCKET); | 371 exceptionDomains, typeMap.WEBSOCKET); |
370 } | 372 } |
371 | 373 |
372 if (contentType & typeMap.WEBRTC) | 374 if (contentType & typeMap.WEBRTC) |
373 { | 375 { |
374 convertFilterAddRules(rules, filter, action, withResourceTypes, | 376 convertFilterAddRules(rules, filter, action, withResourceTypes, |
(...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
746 { | 748 { |
747 convertFilterAddRules(rules, filter, "block", true, | 749 convertFilterAddRules(rules, filter, "block", true, |
748 requestFilterExceptionDomains); | 750 requestFilterExceptionDomains); |
749 } | 751 } |
750 | 752 |
751 for (let filter of this.requestExceptions) | 753 for (let filter of this.requestExceptions) |
752 convertFilterAddRules(rules, filter, "ignore-previous-rules", true); | 754 convertFilterAddRules(rules, filter, "ignore-previous-rules", true); |
753 | 755 |
754 return rules; | 756 return rules; |
755 }; | 757 }; |
OLD | NEW |