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

Side by Side Diff: lib/whitelisting.js

Issue 29338962: Issue 3860 - Move request blocking logic to a seperate module (Closed)
Patch Set: Created March 23, 2016, 1:55 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
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-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 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 whitelisting */ 18 /** @module whitelisting */
19 19
20 "use strict"; 20 "use strict";
21 21
22 let {defaultMatcher} = require("matcher"); 22 let {defaultMatcher} = require("matcher");
23 let {RegExpFilter} = require("filterClasses"); 23 let {RegExpFilter} = require("filterClasses");
24 let {DownloadableSubscription} = require("subscriptionClasses"); 24 let {DownloadableSubscription} = require("subscriptionClasses");
25 let {FilterNotifier} = require("filterNotifier"); 25 let {FilterNotifier} = require("filterNotifier");
26 let {stringifyURL, getDecodedHostname, extractHostFromFrame, isThirdParty} = req uire("url"); 26 let {stringifyURL, getDecodedHostname, extractHostFromFrame, isThirdParty} = req uire("url");
27 let {port} = require("messaging"); 27 let {port} = require("messaging");
28 let devtools = require("devtools"); 28 let devtools = require("devtools");
29 29
30 let pagesWithKey = new ext.PageMap(); 30 let sitekeys = new ext.PageMap();
31 31
32 function match(page, url, typeMask, docDomain, sitekey) 32 function match(page, url, typeMask, docDomain, sitekey)
33 { 33 {
34 let thirdParty = !!docDomain && isThirdParty(url, docDomain); 34 let thirdParty = !!docDomain && isThirdParty(url, docDomain);
35 let urlString = stringifyURL(url); 35 let urlString = stringifyURL(url);
36 36
37 if (!docDomain) 37 if (!docDomain)
38 docDomain = getDecodedHostname(url); 38 docDomain = getDecodedHostname(url);
39 39
40 let filter = defaultMatcher.whitelist.matchesAny( 40 let filter = defaultMatcher.whitelist.matchesAny(
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 filter = match(page, frame.url, typeMask, docDomain, sitekey); 77 filter = match(page, frame.url, typeMask, docDomain, sitekey);
78 frame = parent; 78 frame = parent;
79 } 79 }
80 80
81 return filter; 81 return filter;
82 } 82 }
83 83
84 return match(page, page.url, typeMask); 84 return match(page, page.url, typeMask);
85 }; 85 };
86 86
87 port.on("filters.isPageWhitelisted", (message, sender) =>
Sebastian Noack 2016/03/23 14:02:06 This is unrelated, but I moved this function up he
kzar 2016/03/23 15:36:00 Acknowledged. I can also see an argument for keepi
88 {
89 return !!checkWhitelisted(sender.page);
90 });
91
87 function revalidateWhitelistingState(page) 92 function revalidateWhitelistingState(page)
88 { 93 {
89 FilterNotifier.triggerListeners( 94 FilterNotifier.triggerListeners(
90 "page.WhitelistingStateRevalidate", 95 "page.WhitelistingStateRevalidate",
91 page, checkWhitelisted(page) 96 page, checkWhitelisted(page)
92 ); 97 );
93 } 98 }
94 99
95 let revalidating = false; 100 FilterNotifier.addListener(action =>
96 FilterNotifier.addListener((action, arg) =>
97 { 101 {
98 switch (action) 102 if (action == "filter.behaviorChanged")
99 { 103 ext.pages.query({}, pages =>
100 case "subscription.added": 104 {
101 if (arg instanceof DownloadableSubscription && !arg.lastDownload) 105 for (let page of pages)
102 break; 106 revalidateWhitelistingState(page);
103 case "subscription.removed": 107 });
104 case "subscription.disabled":
105 case "subscription.updated":
106 case "filter.added":
107 case "filter.removed":
108 case "filter.disabled":
109 case "load":
110 if (!revalidating)
111 {
112 revalidating = true;
113 ext.pages.query({}, pages =>
114 {
115 revalidating = false;
116 for (let page of pages)
117 revalidateWhitelistingState(page);
118 });
119 }
120 }
121 }); 108 });
122 109
123 ext.pages.onLoading.addListener(revalidateWhitelistingState); 110 ext.pages.onLoading.addListener(revalidateWhitelistingState);
124 111
125 let getKey = 112 let getKey =
126 /** 113 /**
127 * Gets the public key, previously recorded for the given page 114 * Gets the public key, previously recorded for the given page
128 * and frame, to be considered for the $sitekey filter option. 115 * and frame, to be considered for the $sitekey filter option.
129 * 116 *
130 * @param {Page} page 117 * @param {Page} page
131 * @param {Frame} frame 118 * @param {Frame} frame
132 * @return {string} 119 * @return {string}
133 */ 120 */
134 exports.getKey = function(page, frame) 121 exports.getKey = function(page, frame)
135 { 122 {
136 let urlsWithKey = pagesWithKey.get(page); 123 let keys = sitekeys.get(page);
137 if (!urlsWithKey) 124 if (!keys)
138 return null; 125 return null;
139 126
140 for (; frame != null; frame = frame.parent) 127 for (; frame != null; frame = frame.parent)
141 { 128 {
142 let key = urlsWithKey[stringifyURL(frame.url)]; 129 let key = keys[stringifyURL(frame.url)];
143 if (key) 130 if (key)
144 return key; 131 return key;
145 } 132 }
146 133
147 return null; 134 return null;
148 }; 135 };
149 136
150 function verifyKey(key, signature, url) 137 function recordKey(token, page, url)
151 { 138 {
152 let params = [ 139 let parts = token.split("_");
153 url.pathname + url.search, // REQUEST_URI 140 if (parts.length < 2)
154 url.host, // HTTP_HOST
155 window.navigator.userAgent // HTTP_USER_AGENT
156 ];
157
158 return verifySignature(key, signature, params.join("\0"));
159 }
160
161 function recordKey(page, url, key)
162 {
163 let urlsWithKey = pagesWithKey.get(page);
164
165 if (!urlsWithKey)
166 {
167 urlsWithKey = Object.create(null);
168 pagesWithKey.set(page, urlsWithKey);
169 }
170
171 urlsWithKey[stringifyURL(url)] = key;
172 }
173
174 let processKey =
175 /**
176 * Validates signatures given by the "X-Adblock-Key" response
177 * header or the "data-adblockkey" attribute of the document
178 * element. If the signature is valid, the public key will be
179 * recorded and considered for the $sitekey filter option.
180 *
181 * @param {string} token The base64-encoded public key and
182 * signature separated by an underscrore.
183 * @param {Page} page
184 * @param {Frame} frame
185 */
186 exports.processKey = function(token, page, frame)
187 {
188 if (token.indexOf("_") < 0)
189 return; 141 return;
190 142
191 let [key, signature] = token.split("_", 2); 143 let key = parts[0].replace(/=/g, "");
kzar 2016/03/23 15:36:00 Don't we rather want to trim the "="s from the sta
Sebastian Noack 2016/03/23 16:44:48 We want to trim them from the end. However, they c
kzar 2016/03/23 17:18:22 Acknowledged.
192 key = key.replace(/=/g, ""); 144 let signature = parts[1];
145 let data = url.pathname + url.search + "\0" +
146 url.host + "\0" +
147 window.navigator.userAgent;
148 if (!verifySignature(key, signature, data))
149 return;
193 150
194 if (verifyKey(key, signature, frame.url)) 151 let keys = sitekeys.get(page);
195 recordKey(page, frame.url, key); 152 if (!keys)
196 }; 153 {
154 keys = Object.create(null);
155 sitekeys.set(page, keys);
156 }
157 keys[stringifyURL(url)] = key;
158 }
197 159
198 port.on("filters.addKey", (message, sender) => 160 port.on("filters.addKey", (message, sender) =>
199 { 161 {
200 processKey(message.token, sender.page, sender.frame); 162 recordKey(message.token, sender.page, sender.frame.url);
201 }); 163 });
202 164
203 port.on("filters.isPageWhitelisted", (message, sender) => 165 function onHeadersReceived(details)
204 { 166 {
205 return !!checkWhitelisted(sender.page); 167 let page = new ext.Page({id: details.tabId});
206 }); 168
169 for (let header of details.responseHeaders)
170 if (header.name.toLowerCase() == "x-adblock-key" && header.value)
171 recordKey(header.value, page, new URL(details.url));
172 }
173
174 if (typeof chrome == "object")
175 chrome.webRequest.onHeadersReceived.addListener(
kzar 2016/03/23 15:36:00 I guess this part should be in ext/chrome somewher
Sebastian Noack 2016/03/23 16:44:48 Not really. There is no counterpart for Safari. An
kzar 2016/03/23 17:18:22 Acknowledged.
176 onHeadersReceived,
177 {
178 urls: ["http://*/*", "https://*/*"],
179 types: ["main_frame", "sub_frame"]
180 },
181 ["responseHeaders"]
182 );
OLDNEW
« lib/requestBlocker.js ('K') | « lib/requestBlocker.js ('k') | metadata.common » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld