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

Side by Side Diff: lib/csp.js

Issue 29680696: [$csp4 adblockpluschrome] Issue 5241 - Add support for Content Security Policy filters (Closed)
Patch Set: Rebased and tidied a few things up Created March 6, 2018, 3:43 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
« no previous file with comments | « dependencies ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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-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 // The webRequest API doesn't support WebSocket connection blocking in Microsoft 20 const {defaultMatcher} = require("matcher");
21 // Edge and versions of Chrome before 58. Therefore for those we inject CSP 21 const {BlockingFilter, RegExpFilter} = require("filterClasses");
22 // headers below as a workaround. See https://crbug.com/129353 and 22 const {getDecodedHostname, stringifyURL} = require("url");
23 // https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/10297376 / 23 const {checkWhitelisted} = require("whitelisting");
24 if (!browser.webRequest.ResourceType || 24 const {FilterNotifier} = require("filterNotifier");
25 !("WEBSOCKET" in browser.webRequest.ResourceType)) 25
26 const {typeMap} = RegExpFilter;
27
28 browser.webRequest.onHeadersReceived.addListener(details =>
26 { 29 {
27 const {defaultMatcher} = require("matcher"); 30 // Chrome seems to sometimes give the response type of "xmlhttprequest"
28 const {BlockingFilter, RegExpFilter} = require("filterClasses"); 31 // instead of "main_frame", but when it does the tabId is always -1 and
29 const {getDecodedHostname} = require("url"); 32 // the URL matches the initiator's URL (once normalised).
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.
30 const {checkWhitelisted} = require("whitelisting"); 33 // https://bugs.chromium.org/p/chromium/issues/detail?id=805649
34 if (details.type == "xmlhttprequest" &&
35 (details.tabId > -1 || details.initiator &&
36 details.url != new URL(details.initiator).toString()))
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://
37 {
38 return;
39 }
31 40
32 browser.webRequest.onHeadersReceived.addListener(details => 41 // To avoid an extra matchesAny for the common case let's assume no
42 // $genericblock filters apply when searching for a matching $csp filter,
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
46 let url = new URL(details.url);
47 let hostname = getDecodedHostname(url);
48 let cspMatch = defaultMatcher.matchesAny(details.url, typeMap.CSP, hostname,
49 false, null, specificOnly);
50 if (cspMatch instanceof BlockingFilter)
33 { 51 {
34 let hostname = getDecodedHostname(new URL(details.url)); 52 if (details.tabId > -1)
35 let match = defaultMatcher.matchesAny("", RegExpFilter.typeMap.WEBSOCKET,
36 hostname, false, null, true);
37 if (match instanceof BlockingFilter &&
38 !checkWhitelisted(new ext.Page({id: details.tabId}),
39 ext.getFrame(details.tabId, details.frameId)))
40 { 53 {
41 details.responseHeaders.push({ 54 let page = new ext.Page({id: details.tabId, url: details.url});
42 name: "Content-Security-Policy", 55 let frame = ext.getFrame(details.tabId, details.frameId);
43 // We're blocking WebSockets here by adding a connect-src restriction 56
44 // since the Chrome extension API does not allow us to intercept them. 57 if (checkWhitelisted(page, frame, typeMap.DOCUMENT | typeMap.CSP))
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.
45 // https://bugs.chromium.org/p/chromium/issues/detail?id=129353 58 {
46 // 59 return;
47 // We also need the frame-src and object-src restrictions since CSPs 60 }
48 // are not inherited from the parent for documents with data: and blob: 61
49 // URLs, see https://crbug.com/513860. 62 // We pay the price now for skipping the specificOnly check earlier, we
50 // 63 // must check again for a CSP filter, this time making sure it's specific.
51 // We must use the deprecated child-src directive instead of worker-src 64 specificOnly = !!checkWhitelisted(page, frame, typeMap.GENERICBLOCK);
52 // since that's not supported yet (as of Chrome 56.) 65 if (specificOnly)
53 // 66 {
54 // "http:" also includes "https:" implictly. 67 cspMatch = defaultMatcher.matchesAny(details.url, typeMap.CSP, hostname,
55 // https://www.chromestatus.com/feature/6653486812889088 68 false, null, specificOnly);
56 value: "connect-src http:; child-src http:; " + 69 if (!(cspMatch instanceof BlockingFilter))
57 "frame-src http:; object-src http:" 70 return;
58 }); 71 }
59 return {responseHeaders: details.responseHeaders}; 72
73 FilterNotifier.emit("filter.hitCount", cspMatch, 0, 0, page);
60 } 74 }
Sebastian Noack 2018/03/06 19:43:24 Nit: Don't we omit braces anymore if there is only
kzar 2018/03/07 15:07:01 The rule is to not omit the braces if the block (o
61 }, { 75 // If we don't know the associated tab we'll have to check that if the URL
62 urls: ["http://*/*", "https://*/*"], 76 // is whitelisted manually. Things like $sitekey whitelisting and filter hit
63 // We must also intercept script requests since otherwise Web Workers can 77 // logging won't work, but it's the best we can do.
64 // be abused to execute scripts for which our Content Security Policy 78 else if (defaultMatcher.whitelist.matchesAny(stringifyURL(url),
65 // won't be injected. 79 typeMap.DOCUMENT | typeMap.CSP,
66 // https://github.com/gorhill/uBO-Extra/issues/19 80 hostname))
67 types: ["main_frame", "sub_frame", "script"] 81 {
68 }, ["blocking", "responseHeaders"]); 82 return;
69 } 83 }
84
85 details.responseHeaders.push({
86 name: "Content-Security-Policy",
87 value: cspMatch.csp
88 });
89
90 return {responseHeaders: details.responseHeaders};
91 }
92 }, {
93 urls: ["http://*/*", "https://*/*"],
94 types: ["main_frame", "sub_frame", "xmlhttprequest"]
95 }, ["blocking", "responseHeaders"]);
OLDNEW
« no previous file with comments | « dependencies ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld