OLD | NEW |
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-2017 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("matcher"); |
23 const {RegExpFilter} = require("filterClasses"); | 23 const {RegExpFilter} = require("filterClasses"); |
24 const {DownloadableSubscription} = require("subscriptionClasses"); | |
25 const {FilterNotifier} = require("filterNotifier"); | 24 const {FilterNotifier} = require("filterNotifier"); |
26 const {stringifyURL, getDecodedHostname, | 25 const {stringifyURL, getDecodedHostname, |
27 extractHostFromFrame, isThirdParty} = require("url"); | 26 extractHostFromFrame, isThirdParty} = require("url"); |
28 const {port} = require("messaging"); | 27 const {port} = require("messaging"); |
29 const devtools = require("devtools"); | 28 const devtools = require("devtools"); |
30 const {verifySignature} = require("rsa"); | 29 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); | 36 let urlString = stringifyURL(url); |
38 | 37 |
39 if (!docDomain) | 38 if (!docDomain) |
40 docDomain = getDecodedHostname(url); | 39 docDomain = getDecodedHostname(url); |
41 | 40 |
42 let filter = defaultMatcher.whitelist.matchesAny( | 41 let filter = defaultMatcher.whitelist.matchesAny( |
43 urlString, typeMask, docDomain, thirdParty, sitekey | 42 urlString, typeMask, docDomain, thirdParty, sitekey |
44 ); | 43 ); |
45 | 44 |
46 if (filter && devtools) | 45 if (filter && devtools) |
| 46 { |
47 devtools.logWhitelistedDocument( | 47 devtools.logWhitelistedDocument( |
48 page, urlString, typeMask, docDomain, filter | 48 page, urlString, typeMask, docDomain, filter |
49 ); | 49 ); |
| 50 } |
50 | 51 |
51 return filter; | 52 return filter; |
52 } | 53 } |
53 | 54 |
54 let checkWhitelisted = | 55 let checkWhitelisted = |
55 /** | 56 /** |
56 * Gets the active whitelisting filter for the document associated | 57 * Gets the active whitelisting filter for the document associated |
57 * with the given page/frame, or null if it's not whitelisted. | 58 * with the given page/frame, or null if it's not whitelisted. |
58 * | 59 * |
59 * @param {Page} page | 60 * @param {Page} page |
60 * @param {Frame} [frame] | 61 * @param {Frame} [frame] |
61 * @param {number} [typeMask=RegExpFilter.typeMap.DOCUMENT] | 62 * @param {number} [typeMask=RegExpFilter.typeMap.DOCUMENT] |
62 * @return {?WhitelistFilter} | 63 * @return {?WhitelistFilter} |
63 */ | 64 */ |
64 exports.checkWhitelisted = (page, frame, typeMask) => | 65 exports.checkWhitelisted = (page, frame, typeMask) => |
65 { | 66 { |
66 if (typeof typeMask == "undefined") | 67 if (typeof typeMask == "undefined") |
67 typeMask = RegExpFilter.typeMap.DOCUMENT; | 68 typeMask = RegExpFilter.typeMap.DOCUMENT; |
68 | 69 |
69 if (frame) | 70 if (frame) |
70 { | 71 { |
71 let filter = null; | 72 let filter = null; |
72 | 73 |
73 while (frame && !filter) | 74 while (frame && !filter) |
74 { | 75 { |
75 let parent = frame.parent; | 76 let {parent} = frame; |
76 let docDomain = extractHostFromFrame(parent); | 77 let docDomain = extractHostFromFrame(parent); |
77 let sitekey = getKey(page, frame); | 78 let sitekey = getKey(page, frame); |
78 | 79 |
79 filter = match(page, frame.url, typeMask, docDomain, sitekey); | 80 filter = match(page, frame.url, typeMask, docDomain, sitekey); |
80 frame = parent; | 81 frame = parent; |
81 } | 82 } |
82 | 83 |
83 return filter; | 84 return filter; |
84 } | 85 } |
85 | 86 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 * @return {string} | 121 * @return {string} |
121 */ | 122 */ |
122 exports.getKey = (page, frame) => | 123 exports.getKey = (page, frame) => |
123 { | 124 { |
124 let keys = sitekeys.get(page); | 125 let keys = sitekeys.get(page); |
125 if (!keys) | 126 if (!keys) |
126 return null; | 127 return null; |
127 | 128 |
128 for (; frame != null; frame = frame.parent) | 129 for (; frame != null; frame = frame.parent) |
129 { | 130 { |
130 let key = keys[stringifyURL(frame.url)]; | 131 let key = keys.get(stringifyURL(frame.url)); |
131 if (key) | 132 if (key) |
132 return key; | 133 return key; |
133 } | 134 } |
134 | 135 |
135 return null; | 136 return null; |
136 }; | 137 }; |
137 | 138 |
138 function checkKey(token, url) | 139 function checkKey(token, url) |
139 { | 140 { |
140 let parts = token.split("_"); | 141 let parts = token.split("_"); |
141 if (parts.length < 2) | 142 if (parts.length < 2) |
142 return false; | 143 return false; |
143 | 144 |
144 let key = parts[0].replace(/=/g, ""); | 145 let key = parts[0].replace(/=/g, ""); |
145 let signature = parts[1]; | 146 let signature = parts[1]; |
146 let data = url.pathname + url.search + "\0" + | 147 let data = url.pathname + url.search + "\0" + |
147 url.host + "\0" + | 148 url.host + "\0" + |
148 window.navigator.userAgent; | 149 window.navigator.userAgent; |
149 if (!verifySignature(key, signature, data)) | 150 if (!verifySignature(key, signature, data)) |
150 return false; | 151 return false; |
151 | 152 |
152 return key; | 153 return key; |
153 } | 154 } |
154 | 155 |
155 function recordKey(key, page, url) | 156 function recordKey(key, page, url) |
156 { | 157 { |
157 let keys = sitekeys.get(page); | 158 let keys = sitekeys.get(page); |
158 if (!keys) | 159 if (!keys) |
159 { | 160 { |
160 keys = Object.create(null); | 161 keys = new Map(); |
161 sitekeys.set(page, keys); | 162 sitekeys.set(page, keys); |
162 } | 163 } |
163 keys[stringifyURL(url)] = key; | 164 keys.set(stringifyURL(url), key); |
164 } | 165 } |
165 | 166 |
166 port.on("filters.addKey", (message, sender) => | 167 port.on("filters.addKey", (message, sender) => |
167 { | 168 { |
168 let key = checkKey(message.token, sender.frame.url); | 169 let key = checkKey(message.token, sender.frame.url); |
169 if (key) | 170 if (key) |
170 recordKey(key, sender.page, sender.frame.url); | 171 recordKey(key, sender.page, sender.frame.url); |
171 }); | 172 }); |
172 | 173 |
173 function onHeadersReceived(details) | 174 function onHeadersReceived(details) |
174 { | 175 { |
175 let page = new ext.Page({id: details.tabId}); | 176 let page = new ext.Page({id: details.tabId}); |
176 | 177 |
177 for (let header of details.responseHeaders) | 178 for (let header of details.responseHeaders) |
178 { | 179 { |
179 if (header.name.toLowerCase() == "x-adblock-key" && header.value) | 180 if (header.name.toLowerCase() == "x-adblock-key" && header.value) |
180 { | 181 { |
181 let url = new URL(details.url); | 182 let url = new URL(details.url); |
182 let key = checkKey(header.value, url); | 183 let key = checkKey(header.value, url); |
183 if (key) | 184 if (key) |
184 { | 185 { |
185 // For pre-rendered tabs we don't know for sure the navigation is going | |
186 // to happen until the onCommitted event fires. Unfortunately if we want | |
187 // sitekey whitelisting to work for requests made before onCommitted has | |
188 // been fired we must update the page structure now anyway. | |
189 ext._updatePageFrameStructure(details.frameId, details.tabId, details.ur
l, true); | |
190 recordKey(key, page, url); | 186 recordKey(key, page, url); |
191 break; | 187 break; |
192 } | 188 } |
193 } | 189 } |
194 } | 190 } |
195 } | 191 } |
196 | 192 |
197 if (typeof chrome == "object") | 193 if (typeof chrome == "object") |
| 194 { |
198 chrome.webRequest.onHeadersReceived.addListener( | 195 chrome.webRequest.onHeadersReceived.addListener( |
199 onHeadersReceived, | 196 onHeadersReceived, |
200 { | 197 { |
201 urls: ["http://*/*", "https://*/*"], | 198 urls: ["http://*/*", "https://*/*"], |
202 types: ["main_frame", "sub_frame"] | 199 types: ["main_frame", "sub_frame"] |
203 }, | 200 }, |
204 ["responseHeaders"] | 201 ["responseHeaders"] |
205 ); | 202 ); |
| 203 } |
OLD | NEW |