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

Delta Between Two Patch Sets: lib/csp.js

Issue 29680696: [$csp4 adblockpluschrome] Issue 5241 - Add support for Content Security Policy filters (Closed)
Left Patch Set: Rebased and tidied a few things up Created March 6, 2018, 3:43 p.m.
Right Patch Set: Rebased Created March 26, 2018, 6:22 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « dependencies ('k') | lib/devtools.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 "use strict"; 18 "use strict";
19 19
20 const {defaultMatcher} = require("matcher"); 20 const {defaultMatcher} = require("matcher");
21 const {BlockingFilter, RegExpFilter} = require("filterClasses"); 21 const {RegExpFilter, WhitelistFilter} = require("filterClasses");
22 const {getDecodedHostname, stringifyURL} = require("url"); 22 const {extractHostFromFrame, getDecodedHostname,
23 isThirdParty, stringifyURL} = require("url");
23 const {checkWhitelisted} = require("whitelisting"); 24 const {checkWhitelisted} = require("whitelisting");
24 const {FilterNotifier} = require("filterNotifier"); 25 const {FilterNotifier} = require("filterNotifier");
26 const devtools = require("devtools");
25 27
26 const {typeMap} = RegExpFilter; 28 const {typeMap} = RegExpFilter;
27 29
28 browser.webRequest.onHeadersReceived.addListener(details => 30 browser.webRequest.onHeadersReceived.addListener(details =>
29 { 31 {
30 // Chrome seems to sometimes give the response type of "xmlhttprequest" 32 let url = new URL(details.url);
31 // instead of "main_frame", but when it does the tabId is always -1 and 33 let urlString = stringifyURL(url);
32 // the URL matches the initiator's URL (once normalised). 34 let parentFrame = ext.getFrame(details.tabId, details.parentFrameId);
Sebastian Noack 2018/03/06 19:43:24 Might this be the case when XHR (or fetch) is used
kzar 2018/03/07 15:07:01 I'm not sure what's causing this so far, I know th
Sebastian Noack 2018/03/07 17:27:07 Is there an example in which simply ignoring "xmlh
kzar 2018/03/12 16:33:42 Yes, the website I noticed this happening was rege
Sebastian Noack 2018/03/13 03:58:13 Ok, what's going on here is that the request you s
kzar 2018/03/13 18:11:01 Acknowledged.
33 // https://bugs.chromium.org/p/chromium/issues/detail?id=805649 35 let hostname = extractHostFromFrame(parentFrame) || getDecodedHostname(url);
34 if (details.type == "xmlhttprequest" && 36 let thirdParty = isThirdParty(url, hostname);
35 (details.tabId > -1 || details.initiator && 37
36 details.url != new URL(details.initiator).toString())) 38 let cspMatch = defaultMatcher.matchesAny(urlString, typeMap.CSP, hostname,
Sebastian Noack 2018/03/06 19:43:24 Why is it necessary to normalize the URL? Shouldn'
kzar 2018/03/07 15:07:01 Well if you add this debugging code in the listene
Sebastian Noack 2018/03/07 17:27:07 FWIW, this seems like a Chrome bug to me. All othe
kzar 2018/03/12 16:33:42 OK I've filed an issue with that for them https://
39 thirdParty, null, false);
40 if (cspMatch)
37 { 41 {
38 return; 42 let page = new ext.Page({id: details.tabId, url: details.url});
39 } 43 let frame = ext.getFrame(details.tabId, details.frameId);
40 44
41 // To avoid an extra matchesAny for the common case let's assume no 45 if (checkWhitelisted(page, frame))
42 // $genericblock filters apply when searching for a matching $csp filter, 46 return;
43 // we can double check later if necessary.
44 let specificOnly = false;
Sebastian Noack 2018/03/06 19:43:24 Nit: This variable seems superfluous here. You can
kzar 2018/03/07 15:07:01 Strictly you're right, but I added it early in an
kzar 2018/03/13 18:11:01 Now that the logic is simplified this doesn't add
45 47
46 let url = new URL(details.url); 48 // To avoid an extra matchesAny for the common case we assumed no
47 let hostname = getDecodedHostname(url); 49 // $genericblock filters applied when searching for a matching $csp filter.
48 let cspMatch = defaultMatcher.matchesAny(details.url, typeMap.CSP, hostname, 50 // We must now pay the price by first checking for a $genericblock filter
49 false, null, specificOnly); 51 // and if necessary that our $csp filter is specific.
50 if (cspMatch instanceof BlockingFilter) 52 let specificOnly = !!checkWhitelisted(page, frame, typeMap.GENERICBLOCK);
51 { 53 if (specificOnly)
52 if (details.tabId > -1)
53 { 54 {
54 let page = new ext.Page({id: details.tabId, url: details.url}); 55 cspMatch = defaultMatcher.matchesAny(urlString, typeMap.CSP, hostname,
55 let frame = ext.getFrame(details.tabId, details.frameId); 56 thirdParty, null, specificOnly);
57 if (!cspMatch)
58 return;
59 }
56 60
57 if (checkWhitelisted(page, frame, typeMap.DOCUMENT | typeMap.CSP)) 61 devtools.logRequest(page, urlString, "CSP", hostname, thirdParty, null,
Sebastian Noack 2018/03/06 19:43:24 If we check whether the document is whitelisted, a
kzar 2018/03/07 15:07:01 Well your comment made me realise I so far didn't
Sebastian Noack 2018/03/07 17:27:07 Forget about my original comment here. I mistakenl
kzar 2018/03/12 16:33:42 Acknowledged.
58 { 62 specificOnly, cspMatch);
59 return; 63 FilterNotifier.emit("filter.hitCount", cspMatch, 0, 0, page);
60 }
61 64
62 // We pay the price now for skipping the specificOnly check earlier, we 65 if (cspMatch instanceof WhitelistFilter)
63 // must check again for a CSP filter, this time making sure it's specific.
64 specificOnly = !!checkWhitelisted(page, frame, typeMap.GENERICBLOCK);
65 if (specificOnly)
66 {
67 cspMatch = defaultMatcher.matchesAny(details.url, typeMap.CSP, hostname,
68 false, null, specificOnly);
69 if (!(cspMatch instanceof BlockingFilter))
70 return;
71 }
72
73 FilterNotifier.emit("filter.hitCount", cspMatch, 0, 0, page);
74 }
75 // If we don't know the associated tab we'll have to check that if the URL
76 // is whitelisted manually. Things like $sitekey whitelisting and filter hit
77 // logging won't work, but it's the best we can do.
78 else if (defaultMatcher.whitelist.matchesAny(stringifyURL(url),
79 typeMap.DOCUMENT | typeMap.CSP,
80 hostname))
81 {
82 return; 66 return;
83 }
84 67
85 details.responseHeaders.push({ 68 details.responseHeaders.push({
86 name: "Content-Security-Policy", 69 name: "Content-Security-Policy",
87 value: cspMatch.csp 70 value: cspMatch.csp
88 }); 71 });
89 72
90 return {responseHeaders: details.responseHeaders}; 73 return {responseHeaders: details.responseHeaders};
91 } 74 }
92 }, { 75 }, {
93 urls: ["http://*/*", "https://*/*"], 76 urls: ["http://*/*", "https://*/*"],
94 types: ["main_frame", "sub_frame", "xmlhttprequest"] 77 types: ["main_frame", "sub_frame"]
95 }, ["blocking", "responseHeaders"]); 78 }, ["blocking", "responseHeaders"]);
LEFTRIGHT

Powered by Google App Engine
This is Rietveld