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("../adblockpluscore/lib/matcher"); | 22 const {defaultMatcher} = require("../adblockpluscore/lib/matcher"); |
23 const {Filter, RegExpFilter} = require("../adblockpluscore/lib/filterClasses"); | 23 const {Filter, RegExpFilter} = require("../adblockpluscore/lib/filterClasses"); |
24 const {FilterNotifier} = require("../adblockpluscore/lib/filterNotifier"); | 24 const {FilterNotifier} = require("../adblockpluscore/lib/filterNotifier"); |
25 const {FilterStorage} = require("../adblockpluscore/lib/filterStorage"); | 25 const {FilterStorage} = require("../adblockpluscore/lib/filterStorage"); |
26 const {stringifyURL, getDecodedHostname, | 26 const {stringifyURL, getDecodedHostname, |
27 extractHostFromFrame, isThirdParty} = require("./url"); | 27 extractHostFromFrame, isThirdParty} = require("./url"); |
28 const {port} = require("./messaging"); | 28 const {port} = require("./messaging"); |
29 const devtools = require("./devtools"); | 29 const {logWhitelistedDocument} = require("./devtools"); |
30 const {verifySignature} = require("../adblockpluscore/lib/rsa"); | 30 const {verifySignature} = require("../adblockpluscore/lib/rsa"); |
31 | 31 |
32 let sitekeys = new ext.PageMap(); | 32 let sitekeys = new ext.PageMap(); |
33 | 33 |
34 function match(page, url, typeMask, docDomain, sitekey) | 34 function match(page, url, typeMask, docDomain, sitekey) |
35 { | 35 { |
36 let thirdParty = !!docDomain && isThirdParty(url, docDomain); | 36 let thirdParty = !!docDomain && isThirdParty(url, docDomain); |
37 let urlString = stringifyURL(url); | 37 let urlString = stringifyURL(url); |
38 | 38 |
39 if (!docDomain) | 39 if (!docDomain) |
40 docDomain = getDecodedHostname(url); | 40 docDomain = getDecodedHostname(url); |
41 | 41 |
42 let filter = defaultMatcher.whitelist.matchesAny( | 42 let filter = defaultMatcher.whitelist.matchesAny( |
43 urlString, typeMask, docDomain, thirdParty, sitekey | 43 urlString, typeMask, docDomain, thirdParty, sitekey |
44 ); | 44 ); |
45 | 45 |
46 if (filter && devtools) | 46 if (filter && page) |
47 { | 47 logWhitelistedDocument(page.id, urlString, typeMask, docDomain, filter); |
48 devtools.logWhitelistedDocument( | |
49 page, urlString, typeMask, docDomain, filter | |
50 ); | |
51 } | |
52 | 48 |
53 return filter; | 49 return filter; |
54 } | 50 } |
55 | 51 |
56 let checkWhitelisted = | 52 let checkWhitelisted = |
57 /** | 53 /** |
58 * Gets the active whitelisting filter for the document associated | 54 * Gets the active whitelisting filter for the document associated |
59 * with the given page/frame, or null if it's not whitelisted. | 55 * with the given page/frame, or null if it's not whitelisted. |
60 * | 56 * |
61 * @param {Page} page | 57 * @param {?Page} page |
62 * @param {Frame} [frame] | 58 * @param {?Frame} [frame] |
| 59 * @param {?URL} [originUrl] |
63 * @param {number} [typeMask=RegExpFilter.typeMap.DOCUMENT] | 60 * @param {number} [typeMask=RegExpFilter.typeMap.DOCUMENT] |
64 * @return {?WhitelistFilter} | 61 * @return {?WhitelistFilter} |
65 */ | 62 */ |
66 exports.checkWhitelisted = (page, frame, typeMask) => | 63 exports.checkWhitelisted = (page, frame, originUrl, |
67 { | 64 typeMask = RegExpFilter.typeMap.DOCUMENT) => |
68 if (typeof typeMask == "undefined") | 65 { |
69 typeMask = RegExpFilter.typeMap.DOCUMENT; | 66 if (frame || originUrl) |
70 | 67 { |
71 if (frame) | 68 while (frame) |
72 { | 69 { |
73 let filter = null; | 70 let parentFrame = frame.parent; |
74 | 71 let filter = match(page, frame.url, typeMask, |
75 while (frame && !filter) | 72 extractHostFromFrame(parentFrame, originUrl), |
76 { | 73 getKey(page, frame, originUrl)); |
77 let {parent} = frame; | 74 |
78 let docDomain = extractHostFromFrame(parent); | 75 if (filter) |
79 let sitekey = getKey(page, frame); | 76 return filter; |
80 | 77 |
81 filter = match(page, frame.url, typeMask, docDomain, sitekey); | 78 frame = parentFrame; |
82 frame = parent; | 79 } |
83 } | 80 |
84 | 81 return originUrl && match(page, originUrl, typeMask, null, |
85 return filter; | 82 getKey(null, null, originUrl)); |
86 } | 83 } |
87 | 84 |
88 return match(page, page.url, typeMask); | 85 return page && match(page, page.url, typeMask); |
89 }; | 86 }; |
90 | 87 |
91 port.on("filters.isWhitelisted", message => | 88 port.on("filters.isWhitelisted", message => |
92 { | 89 { |
93 return !!checkWhitelisted(new ext.Page(message.tab)); | 90 return !!checkWhitelisted(new ext.Page(message.tab)); |
94 }); | 91 }); |
95 | 92 |
96 port.on("filters.whitelist", message => | 93 port.on("filters.whitelist", message => |
97 { | 94 { |
98 let page = new ext.Page(message.tab); | 95 let page = new ext.Page(message.tab); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 }); | 137 }); |
141 }); | 138 }); |
142 | 139 |
143 ext.pages.onLoading.addListener(revalidateWhitelistingState); | 140 ext.pages.onLoading.addListener(revalidateWhitelistingState); |
144 | 141 |
145 let getKey = | 142 let getKey = |
146 /** | 143 /** |
147 * Gets the public key, previously recorded for the given page | 144 * Gets the public key, previously recorded for the given page |
148 * and frame, to be considered for the $sitekey filter option. | 145 * and frame, to be considered for the $sitekey filter option. |
149 * | 146 * |
150 * @param {Page} page | 147 * @param {?Page} page |
151 * @param {Frame} frame | 148 * @param {?Frame} frame |
| 149 * @param {URL} [originUrl] |
152 * @return {string} | 150 * @return {string} |
153 */ | 151 */ |
154 exports.getKey = (page, frame) => | 152 exports.getKey = (page, frame, originUrl) => |
155 { | 153 { |
156 let keys = sitekeys.get(page); | 154 if (page) |
157 if (!keys) | 155 { |
158 return null; | 156 let keys = sitekeys.get(page); |
159 | 157 if (keys) |
160 for (; frame != null; frame = frame.parent) | 158 { |
161 { | 159 for (; frame; frame = frame.parent) |
162 let key = keys.get(stringifyURL(frame.url)); | 160 { |
163 if (key) | 161 let key = keys.get(stringifyURL(frame.url)); |
164 return key; | 162 if (key) |
| 163 return key; |
| 164 } |
| 165 } |
| 166 } |
| 167 |
| 168 if (originUrl) |
| 169 { |
| 170 for (let keys of sitekeys._map.values()) |
| 171 { |
| 172 let key = keys.get(stringifyURL(originUrl)); |
| 173 if (key) |
| 174 return key; |
| 175 } |
165 } | 176 } |
166 | 177 |
167 return null; | 178 return null; |
168 }; | 179 }; |
169 | 180 |
170 function checkKey(token, url) | 181 function checkKey(token, url) |
171 { | 182 { |
172 let parts = token.split("_"); | 183 let parts = token.split("_"); |
173 if (parts.length < 2) | 184 if (parts.length < 2) |
174 return false; | 185 return false; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 { | 236 { |
226 browser.webRequest.onHeadersReceived.addListener( | 237 browser.webRequest.onHeadersReceived.addListener( |
227 onHeadersReceived, | 238 onHeadersReceived, |
228 { | 239 { |
229 urls: ["http://*/*", "https://*/*"], | 240 urls: ["http://*/*", "https://*/*"], |
230 types: ["main_frame", "sub_frame"] | 241 types: ["main_frame", "sub_frame"] |
231 }, | 242 }, |
232 ["responseHeaders"] | 243 ["responseHeaders"] |
233 ); | 244 ); |
234 } | 245 } |
LEFT | RIGHT |