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 and addressed comments Created March 3, 2015, 2:28 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
« no previous file with comments | « 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 22 matching lines...) Expand all
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 /** 61 /**
59 * Generates filters to block an element. 62 * Generates filters to block an element.
60 * 63 *
61 * @param {string} tagName The element's tag name 64 * @param {string} tagName The element's tag name
62 * @param {string} [src] The element's "src" attribute 65 * @param {Object} attributes The element's "id", "src" and "style attreibute
63 * @param {string} [id] The element's "id" attribute 66 * @param {string[]} classes The classes given by the element's "class" attr ibute
64 * @param {string} [style] The element's "style" attribute 67 * @param {string[]} urls The URLs considered when loading the element
65 * @param {string[]} classes The classes given by the element's "class" attribu te 68 * @param {string} [type] The request type (will be ignored if there are no URLs)
66 * @param {string[]} urls The URLs considered when loading the element 69 * @param {string} baseURL The URL of the document containing the element
67 * @param {URL} baseURL The URL of the document containing the element 70 * @param {Page} page The page containing the element
71 * @param {Frame} frame The frame containing the element
68 * 72 *
69 * @return {object} An object holding the list of generated filters and 73 * @return {object} An object holding the list of generated filters and
70 * the list of CSS selectors for the included element 74 * the list of CSS selectors for the included element
71 * hiding filters: {filters: [...], selectors: [...]} 75 * hiding filters: {filters: [...], selectors: [...]}
72 */ 76 */
73 function composeFilters(tagName, id, src, style, classes, urls, baseURL) 77 function composeFilters(tagName, attributes, classes, urls, type, baseURL, page, frame)
74 { 78 {
75 // Add a blocking filter for each HTTP(S) URL associated with the element
76 let filters = []; 79 let filters = [];
77 for (let url of urls) 80 let selectors = [];
81
82 if (!isFrameWhitelisted(page, frame, "DOCUMENT"))
78 { 83 {
79 let urlObj = new URL(url, baseURL); 84 let docDomain = extractHostFromFrame(frame);
80 if (urlObj.protocol == "http:" || urlObj.protocol == "https:") 85
86 // Add a blocking filter for each URL of the element that can be blocked
87 for (let url of urls)
81 { 88 {
82 let filter = stringifyURL(urlObj).replace(/^[\w\-]+:\/+(?:www\.)?/, "||"); 89 let urlObj = new URL(url, baseURL);
83 90
84 if (filters.indexOf(filter) == -1) 91 if (url.protocol == "http:" || url.protocol == "https:")
85 filters.push(filter); 92 {
93 url = stringifyURL(urlObj);
94
95 let filter = defaultMatcher.matchesAny(
96 url, type, docDomain,
97 isThirdParty(urlObj, docDomain),
98 getKey(page, frame)
99 );
100
101 if (!(filter instanceof WhitelistFilter))
102 {
103 let filterText = url.replace(/^[\w\-]+:\/+(?:www\.)?/, "||");
104
105 if (filters.indexOf(filterText) == -1)
106 filters.push(filterText);
107 }
108 }
109 }
110
111 // If we couldn't generate any blocking filters, fallback to element hiding
112 let selectors = [];
113 if (filters.length == 0 && !isFrameWhitelisted(page, frame, "ELEMHIDE"))
114 {
115 // Generate CSS selectors based on the element's "id" and "class" attribut e
116 if (attributes.id)
117 selectors.push("#" + escapeCSS(attributes.id));
118 if (classes.length > 0)
119 selectors.push(classes.map(c => "." + escapeCSS(c)).join(""));
120
121 // If there is a "src" attribute, specifiying a URL that we can't block,
122 // generate a CSS selector matching the "src" attribute
123 if (attributes.src)
124 selectors.push(escapeCSS(tagName) + "[src=" + quoteCSS(attributes.src) + "]");
125
126 // As last resort, if there is a "style" attribute, and we couldn't genera te
127 // any filters so far, generate a CSS selector matching the "style" attrib ute
128 if (attributes.style && selectors.length == 0 && filters.length == 0)
129 selectors.push(escapeCSS(tagName) + "[style=" + quoteCSS(attributes.styl e) + "]");
130
131 // Add an element hiding filter for each generated CSS selector
132 for (let selector of selectors)
133 filters.push(docDomain.replace(/^www\./, "") + "##" + selector);
86 } 134 }
87 } 135 }
88 136
89 // If we couldn't generate any blocking filters, fallback to element hiding
90 let selectors = [];
91 if (filters.length == 0)
92 {
93 // Generate CSS selectors based on the element's "id" and "class" attribute
94 if (id)
95 selectors.push("#" + escapeCSS(id));
96 if (classes.length > 0)
97 selectors.push(classes.map(c => "." + escapeCSS(c)).join(""));
98
99 // If there is a "src" attribute, specifiying a URL that we can't block,
100 // generate a CSS selector matching the "src" attribute
101 if (src)
102 selectors.push(escapeCSS(tagName) + "[src=" + quoteCSS(src) + "]");
103
104 // As last resort, if there is a "style" attribute, and we couldn't generate
105 // any filters so far, generate a CSS selector matching the "style" attribut e
106 if (style && selectors.length == 0 && filters.length == 0)
107 selectors.push(escapeCSS(tagName) + "[style=" + quoteCSS(style) + "]");
108
109 // Add an element hiding filter for each generated CSS selector
110 if (selectors.length > 0)
111 {
112 let domain = getDecodedHostname(baseURL).replace(/^www\./, "");
113
114 for (let selector of selectors)
115 filters.push(domain + "##" + selector);
116 }
117 }
118
119 return {filters: filters, selectors: selectors}; 137 return {filters: filters, selectors: selectors};
120 } 138 }
121 exports.composeFilters = composeFilters; 139 exports.composeFilters = composeFilters;
OLDNEW
« no previous file with comments | « include.preload.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld