LEFT | RIGHT |
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 whitelisting */ | 18 /** @module whitelisting */ |
19 | 19 |
20 "use strict"; | 20 "use strict"; |
21 | 21 |
22 const {defaultMatcher} = require("matcher"); | 22 const {defaultMatcher} = require("../adblockpluscore/lib/matcher"); |
23 const {Filter, RegExpFilter} = require("filterClasses"); | 23 const {Filter, RegExpFilter} = require("../adblockpluscore/lib/filterClasses"); |
24 const {FilterNotifier} = require("filterNotifier"); | 24 const {FilterNotifier} = require("../adblockpluscore/lib/filterNotifier"); |
25 const {FilterStorage} = require("filterStorage"); | 25 const {FilterStorage} = require("../adblockpluscore/lib/filterStorage"); |
26 const {stringifyURL, getDecodedHostname, | 26 const {extractHostFromFrame, isThirdParty} = require("./url"); |
27 extractHostFromFrame, isThirdParty} = require("url"); | 27 const {port} = require("./messaging"); |
28 const {port} = require("messaging"); | 28 const {logWhitelistedDocument} = require("./hitLogger"); |
29 const {logWhitelistedDocument} = require("hitLogger"); | 29 const {verifySignature} = require("../adblockpluscore/lib/rsa"); |
30 const {verifySignature} = require("rsa"); | |
31 | 30 |
32 let sitekeys = new ext.PageMap(); | 31 let sitekeys = new ext.PageMap(); |
33 | 32 |
34 function match(page, url, typeMask, docDomain, sitekey) | 33 function match(page, url, typeMask, docDomain, sitekey) |
35 { | 34 { |
36 let thirdParty = !!docDomain && isThirdParty(url, docDomain); | 35 let thirdParty = !!docDomain && isThirdParty(url, docDomain); |
37 let urlString = stringifyURL(url); | |
38 | 36 |
39 if (!docDomain) | 37 if (!docDomain) |
40 docDomain = getDecodedHostname(url); | 38 docDomain = url.hostname; |
41 | 39 |
42 let filter = defaultMatcher.whitelist.matchesAny( | 40 let filter = defaultMatcher.whitelist.matchesAny( |
43 urlString, typeMask, docDomain, thirdParty, sitekey | 41 url.href, typeMask, docDomain, thirdParty, sitekey |
44 ); | 42 ); |
45 | 43 |
46 if (filter) | 44 if (filter && page) |
47 { | 45 logWhitelistedDocument(page.id, url.href, typeMask, docDomain, filter); |
48 logWhitelistedDocument(page ? page.id : -1, urlString, typeMask, docDomain, | 46 |
49 filter); | |
50 } | |
51 return filter; | 47 return filter; |
52 } | 48 } |
53 | 49 |
54 let checkWhitelisted = | 50 let checkWhitelisted = |
55 /** | 51 /** |
56 * Gets the active whitelisting filter for the document associated | 52 * Gets the active whitelisting filter for the document associated |
57 * with the given page/frame, or null if it's not whitelisted. | 53 * with the given page/frame, or null if it's not whitelisted. |
58 * | 54 * |
59 * @param {Page} page | 55 * @param {?Page} page |
60 * @param {Frame} [frame] | 56 * @param {?Frame} [frame] |
| 57 * @param {?URL} [originUrl] |
61 * @param {number} [typeMask=RegExpFilter.typeMap.DOCUMENT] | 58 * @param {number} [typeMask=RegExpFilter.typeMap.DOCUMENT] |
62 * @return {?WhitelistFilter} | 59 * @return {?WhitelistFilter} |
63 */ | 60 */ |
64 exports.checkWhitelisted = (page, frame, typeMask) => | 61 exports.checkWhitelisted = (page, frame, originUrl, |
65 { | 62 typeMask = RegExpFilter.typeMap.DOCUMENT) => |
66 if (typeof typeMask == "undefined") | 63 { |
67 typeMask = RegExpFilter.typeMap.DOCUMENT; | 64 if (frame || originUrl) |
68 | 65 { |
69 if (frame) | 66 while (frame) |
70 { | 67 { |
71 let filter = null; | 68 let parentFrame = frame.parent; |
72 | 69 let filter = match(page, frame.url, typeMask, |
73 while (frame && !filter) | 70 extractHostFromFrame(parentFrame, originUrl), |
74 { | 71 getKey(page, frame, originUrl)); |
75 let {parent} = frame; | 72 |
76 let docDomain = extractHostFromFrame(parent); | 73 if (filter) |
77 let sitekey = getKey(page, frame); | 74 return filter; |
78 | 75 |
79 filter = match(page, frame.url, typeMask, docDomain, sitekey); | 76 frame = parentFrame; |
80 frame = parent; | 77 } |
81 } | 78 |
82 | 79 return originUrl && match(page, originUrl, typeMask, null, |
83 return filter; | 80 getKey(null, null, originUrl)); |
84 } | 81 } |
85 | 82 |
86 return match(page, page.url, typeMask); | 83 return page && match(page, page.url, typeMask); |
87 }; | 84 }; |
88 | 85 |
89 port.on("filters.isWhitelisted", message => | 86 port.on("filters.isWhitelisted", message => |
90 { | 87 { |
91 return !!checkWhitelisted(new ext.Page(message.tab)); | 88 return !!checkWhitelisted(new ext.Page(message.tab)); |
92 }); | 89 }); |
93 | 90 |
94 port.on("filters.whitelist", message => | 91 port.on("filters.whitelist", message => |
95 { | 92 { |
96 let page = new ext.Page(message.tab); | 93 let page = new ext.Page(message.tab); |
97 let host = getDecodedHostname(page.url).replace(/^www\./, ""); | 94 let host = page.url.hostname.replace(/^www\./, ""); |
98 let filter = Filter.fromText("@@||" + host + "^$document"); | 95 let filter = Filter.fromText("@@||" + host + "^$document"); |
99 if (filter.subscriptions.length && filter.disabled) | 96 if (filter.subscriptions.length && filter.disabled) |
100 { | 97 { |
101 filter.disabled = false; | 98 filter.disabled = false; |
102 } | 99 } |
103 else | 100 else |
104 { | 101 { |
105 filter.disabled = false; | 102 filter.disabled = false; |
106 FilterStorage.addFilter(filter); | 103 FilterStorage.addFilter(filter); |
107 } | 104 } |
(...skipping 30 matching lines...) Expand all Loading... |
138 }); | 135 }); |
139 }); | 136 }); |
140 | 137 |
141 ext.pages.onLoading.addListener(revalidateWhitelistingState); | 138 ext.pages.onLoading.addListener(revalidateWhitelistingState); |
142 | 139 |
143 let getKey = | 140 let getKey = |
144 /** | 141 /** |
145 * Gets the public key, previously recorded for the given page | 142 * Gets the public key, previously recorded for the given page |
146 * and frame, to be considered for the $sitekey filter option. | 143 * and frame, to be considered for the $sitekey filter option. |
147 * | 144 * |
148 * @param {Page} page | 145 * @param {?Page} page |
149 * @param {Frame} frame | 146 * @param {?Frame} frame |
| 147 * @param {URL} [originUrl] |
150 * @return {string} | 148 * @return {string} |
151 */ | 149 */ |
152 exports.getKey = (page, frame) => | 150 exports.getKey = (page, frame, originUrl) => |
153 { | 151 { |
154 let keys = sitekeys.get(page); | 152 if (page) |
155 if (!keys) | 153 { |
156 return null; | 154 let keys = sitekeys.get(page); |
157 | 155 if (keys) |
158 for (; frame != null; frame = frame.parent) | 156 { |
159 { | 157 for (; frame; frame = frame.parent) |
160 let key = keys.get(stringifyURL(frame.url)); | 158 { |
161 if (key) | 159 let key = keys.get(frame.url.href); |
162 return key; | 160 if (key) |
| 161 return key; |
| 162 } |
| 163 } |
| 164 } |
| 165 |
| 166 if (originUrl) |
| 167 { |
| 168 for (let keys of sitekeys._map.values()) |
| 169 { |
| 170 let key = keys.get(originUrl.href); |
| 171 if (key) |
| 172 return key; |
| 173 } |
163 } | 174 } |
164 | 175 |
165 return null; | 176 return null; |
166 }; | 177 }; |
167 | 178 |
168 function checkKey(token, url) | 179 function checkKey(token, url) |
169 { | 180 { |
170 let parts = token.split("_"); | 181 let parts = token.split("_"); |
171 if (parts.length < 2) | 182 if (parts.length < 2) |
172 return false; | 183 return false; |
(...skipping 10 matching lines...) Expand all Loading... |
183 } | 194 } |
184 | 195 |
185 function recordKey(key, page, url) | 196 function recordKey(key, page, url) |
186 { | 197 { |
187 let keys = sitekeys.get(page); | 198 let keys = sitekeys.get(page); |
188 if (!keys) | 199 if (!keys) |
189 { | 200 { |
190 keys = new Map(); | 201 keys = new Map(); |
191 sitekeys.set(page, keys); | 202 sitekeys.set(page, keys); |
192 } | 203 } |
193 keys.set(stringifyURL(url), key); | 204 keys.set(url.href, key); |
194 } | 205 } |
195 | 206 |
196 port.on("filters.addKey", (message, sender) => | 207 port.on("filters.addKey", (message, sender) => |
197 { | 208 { |
198 let key = checkKey(message.token, sender.frame.url); | 209 let key = checkKey(message.token, sender.frame.url); |
199 if (key) | 210 if (key) |
200 recordKey(key, sender.page, sender.frame.url); | 211 recordKey(key, sender.page, sender.frame.url); |
201 }); | 212 }); |
202 | 213 |
203 function onHeadersReceived(details) | 214 function onHeadersReceived(details) |
(...skipping 19 matching lines...) Expand all Loading... |
223 { | 234 { |
224 browser.webRequest.onHeadersReceived.addListener( | 235 browser.webRequest.onHeadersReceived.addListener( |
225 onHeadersReceived, | 236 onHeadersReceived, |
226 { | 237 { |
227 urls: ["http://*/*", "https://*/*"], | 238 urls: ["http://*/*", "https://*/*"], |
228 types: ["main_frame", "sub_frame"] | 239 types: ["main_frame", "sub_frame"] |
229 }, | 240 }, |
230 ["responseHeaders"] | 241 ["responseHeaders"] |
231 ); | 242 ); |
232 } | 243 } |
LEFT | RIGHT |