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

Side by Side Diff: lib/filterComposer.js

Issue 5225119261655040: Issue 1282 - Don't generate filters conflicting with existing exception rules (Closed)
Patch Set: Rebased Created Feb. 28, 2015, 6:03 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« include.postload.js ('K') | « include.preload.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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-2015 Eyeo GmbH 3 * Copyright (C) 2006-2015 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 let {getDecodedHostname, stringifyURL} = require("url"); 18 let {extractHostFromFrame, stringifyURL, isThirdParty} = require("url");
19 let {getKey, isFrameWhitelisted} = require("whitelisting");
20 let {defaultMatcher} = require("matcher");
21 let {WhitelistFilter} = require("filterClasses");
19 22
20 function escapeChar(chr) 23 function escapeChar(chr)
21 { 24 {
22 let code = chr.charCodeAt(0); 25 let code = chr.charCodeAt(0);
23 26
24 // Control characters and leading digits must be escaped based on 27 // Control characters and leading digits must be escaped based on
25 // their char code in CSS. Moreover, curly brackets aren't allowed 28 // their char code in CSS. Moreover, curly brackets aren't allowed
26 // in elemhide filters, and therefore must be escaped based on their 29 // in elemhide filters, and therefore must be escaped based on their
27 // char code as well. 30 // char code as well.
28 if (code <= 0x1F || code == 0x7F || /[\d\{\}]/.test(chr)) 31 if (code <= 0x1F || code == 0x7F || /[\d\{\}]/.test(chr))
(...skipping 19 matching lines...) Expand all
48 * 51 *
49 * @param {string} value 52 * @param {string} value
50 * @return {string} 53 * @return {string}
51 */ 54 */
52 function quoteCSS(value) 55 function quoteCSS(value)
53 { 56 {
54 return '"' + value.replace(/["\\\{\}\x00-\x1F\x7F]/g, escapeChar) + '"'; 57 return '"' + value.replace(/["\\\{\}\x00-\x1F\x7F]/g, escapeChar) + '"';
55 } 58 }
56 exports.quoteCSS = quoteCSS; 59 exports.quoteCSS = quoteCSS;
57 60
58 function canBlockURL(url) 61 function canBlockURL(url, type, page, frame)
59 { 62 {
60 return url.protocol == "http:" || url.protocol == "https:"; 63 if (url.protocol != "http:" && url.protocol != "https:")
64 return false;
65
66 let docDomain = extractHostFromFrame(frame);
67 let filter = defaultMatcher.matchesAny(
68 stringifyURL(url), type, docDomain,
69 isThirdParty(url, docDomain), getKey(page, frame)
70 );
71
72 return !(filter instanceof WhitelistFilter);
Wladimir Palant 2015/03/02 20:14:19 For sake of consistency, it might make sense to mo
Sebastian Noack 2015/03/03 14:29:00 This function no longer exist. The code has been i
61 } 73 }
62 74
63 /** 75 /**
64 * Generates filters to block an element. 76 * Generates filters to block an element.
65 * 77 *
66 * @param {string} tagName The element's tag name 78 * @param {string} tagName The element's tag name
67 * @param {string} [src] The element's "src" attribute 79 * @param {string} [src] The element's "src" attribute
68 * @param {string} [id] The element's "id" attribute 80 * @param {string} [id] The element's "id" attribute
69 * @param {string} [style] The element's "style" attribute 81 * @param {string} [style] The element's "style" attribute
70 * @param {string[]} classes The classes given by the element's "class" attribu te 82 * @param {string[]} classes The classes given by the element's "class" attribu te
71 * @param {string[]} urls The URLs considered when loading the element 83 * @param {string[]} urls The URLs considered when loading the element
72 * @param {URL} baseURL The URL of the document containing the element 84 * @param {string} [type] The request type (will be ignored if there are no URLs)
85 * @param {string} baseURL The URL of the document containing the element
86 * @param {Page} page The page containing the element
87 * @param {Frame} frame The frame containing the element
73 * 88 *
74 * @return {object} An object holding the list of generated filters and 89 * @return {object} An object holding the list of generated filters and
75 * the list of CSS selectors for the included element 90 * the list of CSS selectors for the included element
76 * hiding filters: {filters: [...], selectors: [...]} 91 * hiding filters: {filters: [...], selectors: [...]}
77 */ 92 */
78 function composeFilters(tagName, id, src, style, classes, urls, baseURL) 93 function composeFilters(tagName, id, src, style, classes, urls, type, baseURL, p age, frame)
79 { 94 {
Wladimir Palant 2015/03/02 20:14:19 A call to isFrameWhitelisted(..., "DOCUMENT") is m
Sebastian Noack 2015/03/03 14:29:00 Done.
80 // Add a blocking filter for each HTTP(S) URL associated with the element 95 // Add a blocking filter for each URL of the element that can be blocked
81 let filters = []; 96 let filters = [];
82 for (let url of urls) 97 for (let url of urls)
83 { 98 {
84 let urlObj = new URL(url, baseURL); 99 let urlObj = new URL(url, baseURL);
85 if (canBlockURL(urlObj)) 100 if (canBlockURL(urlObj, type, page, frame))
86 { 101 {
87 let filter = stringifyURL(urlObj).replace(/^[\w\-]+:\/+(?:www\.)?/, "||"); 102 let filter = stringifyURL(urlObj).replace(/^[\w\-]+:\/+(?:www\.)?/, "||");
88 103
89 if (filters.indexOf(filter) == -1) 104 if (filters.indexOf(filter) == -1)
90 filters.push(filter); 105 filters.push(filter);
91 } 106 }
92 } 107 }
93 108
94 // Generate CSS selectors based on the element's "id" and "class" attribute
95 let selectors = []; 109 let selectors = [];
96 if (id) 110 if (!isFrameWhitelisted(page, frame, "ELEMHIDE"))
97 selectors.push("#" + escapeCSS(id)); 111 {
98 if (classes.length > 0) 112 // Generate CSS selectors based on the element's "id" and "class" attribute
99 selectors.push(classes.map(c => "." + escapeCSS(c)).join("")); 113 if (id)
114 selectors.push("#" + escapeCSS(id));
115 if (classes.length > 0)
116 selectors.push(classes.map(c => "." + escapeCSS(c)).join(""));
100 117
101 // If there is a "src" attribute, specifiying a URL that we can't block, 118 // If there is a "src" attribute, specifiying a URL that we can't block,
102 // generate a CSS selector matching the "src" attribute 119 // generate a CSS selector matching the "src" attribute
103 if (src && !canBlockURL(new URL(src, baseURL))) 120 if (src && !canBlockURL(new URL(src, baseURL), type, page, frame))
104 selectors.push(escapeCSS(tagName) + "[src=" + quoteCSS(src) + "]"); 121 selectors.push(escapeCSS(tagName) + "[src=" + quoteCSS(src) + "]");
105 122
106 // As last resort, if there is a "style" attribute, and we couldn't generate 123 // As last resort, if there is a "style" attribute, and we couldn't generate
107 // any filters so far, generate a CSS selector matching the "style" attribute 124 // any filters so far, generate a CSS selector matching the "style" attribut e
108 if (style && selectors.length == 0 && filters.length == 0) 125 if (style && selectors.length == 0 && filters.length == 0)
109 selectors.push(escapeCSS(tagName) + "[style=" + quoteCSS(style) + "]"); 126 selectors.push(escapeCSS(tagName) + "[style=" + quoteCSS(style) + "]");
110 127
111 // Add an element hiding filter for each generated CSS selector 128 // Add an element hiding filter for each generated CSS selector
112 if (selectors.length > 0) 129 if (selectors.length > 0)
113 { 130 {
114 let domain = getDecodedHostname(baseURL).replace(/^www\./, ""); 131 let domain = extractHostFromFrame(frame).replace(/^www\./, "");
115 132
116 for (let selector of selectors) 133 for (let selector of selectors)
117 filters.push(domain + "##" + selector); 134 filters.push(domain + "##" + selector);
135 }
118 } 136 }
119 137
120 return {filters: filters, selectors: selectors}; 138 return {filters: filters, selectors: selectors};
121 } 139 }
122 exports.composeFilters = composeFilters; 140 exports.composeFilters = composeFilters;
OLDNEW
« include.postload.js ('K') | « include.preload.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld