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, ignored requests cached by ServiceWorkers and addressed Sebastian's feedback Created March 13, 2018, 6:08 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') | lib/devtools.js » ('j') | metadata.chrome » ('J')
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,
22 // headers below as a workaround. See https://crbug.com/129353 and 22 RegExpFilter,
23 // https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/10297376 / 23 WhitelistFilter} = require("filterClasses");
24 if (!browser.webRequest.ResourceType || 24 const {getDecodedHostname, stringifyURL} = require("url");
25 !("WEBSOCKET" in browser.webRequest.ResourceType)) 25 const {checkWhitelisted} = require("whitelisting");
26 const {FilterNotifier} = require("filterNotifier");
27 const devtools = require("devtools");
28
29 const {typeMap} = RegExpFilter;
30
31 browser.webRequest.onHeadersReceived.addListener(details =>
26 { 32 {
27 const {defaultMatcher} = require("matcher"); 33 let url = new URL(details.url);
28 const {BlockingFilter, RegExpFilter} = require("filterClasses"); 34 let hostname = getDecodedHostname(url);
29 const {getDecodedHostname} = require("url"); 35 let cspMatch = defaultMatcher.matchesAny(details.url, typeMap.CSP, hostname,
Sebastian Noack 2018/03/13 22:28:30 It seems, we use stringifyURL(new URL(...)) everyw
kzar 2018/03/14 10:46:43 Done.
Sebastian Noack 2018/03/14 15:57:27 ESLint doesn't catch it because it considers the s
kzar 2018/03/14 17:16:07 OK
30 const {checkWhitelisted} = require("whitelisting"); 36 false, null, false);
37 if (cspMatch)
38 {
39 let page = new ext.Page({id: details.tabId, url: details.url});
40 let frame = ext.getFrame(details.tabId, details.frameId);
41 if (checkWhitelisted(page, frame, typeMap.DOCUMENT | typeMap.CSP))
Sebastian Noack 2018/03/13 22:28:30 Are filters like @@||example.com$csp even supposed
kzar 2018/03/14 10:46:43 Yea, the devtools interface adds a filter like tha
Sebastian Noack 2018/03/14 15:57:27 This should work regardless of checking for CSP, h
kzar 2018/03/14 17:16:07 I think we need to check for CSP here as well sinc
Sebastian Noack 2018/03/14 20:19:42 I don't understand how this is related. Let's say
kzar 2018/03/15 13:10:13 Sebastian Noack wrote:
Sebastian Noack 2018/03/15 17:14:24 Good point, as far as Acceptable Ads is concerned,
kzar 2018/03/15 18:18:43 OK, I've sent him a message.
kzar 2018/03/16 12:54:59 He voted for non-recursively.
Sebastian Noack 2018/03/16 23:52:38 This behavior seems expected/correct to me. $cs
kzar 2018/03/19 14:41:30 Thanks for the explanation, Done.
42 return;
31 43
32 browser.webRequest.onHeadersReceived.addListener(details => 44 // To avoid an extra matchesAny for the common case we assumed no
33 { 45 // $genericblock filters applied when searching for a matching $csp filter.
34 let hostname = getDecodedHostname(new URL(details.url)); 46 // We must now pay the price by first checking for a $genericblock filter
35 let match = defaultMatcher.matchesAny("", RegExpFilter.typeMap.WEBSOCKET, 47 // and if necessary that our $csp filter is specific.
36 hostname, false, null, true); 48 let specificOnly = checkWhitelisted(page, frame, typeMap.GENERICBLOCK);
37 if (match instanceof BlockingFilter && 49 if (specificOnly)
38 !checkWhitelisted(new ext.Page({id: details.tabId}),
39 ext.getFrame(details.tabId, details.frameId)))
40 { 50 {
41 details.responseHeaders.push({ 51 cspMatch = defaultMatcher.matchesAny(details.url, typeMap.CSP, hostname,
42 name: "Content-Security-Policy", 52 false, null, specificOnly);
43 // We're blocking WebSockets here by adding a connect-src restriction 53 if (!(cspMatch instanceof BlockingFilter))
44 // since the Chrome extension API does not allow us to intercept them. 54 return;
45 // https://bugs.chromium.org/p/chromium/issues/detail?id=129353
46 //
47 // We also need the frame-src and object-src restrictions since CSPs
48 // are not inherited from the parent for documents with data: and blob:
49 // URLs, see https://crbug.com/513860.
50 //
51 // We must use the deprecated child-src directive instead of worker-src
52 // since that's not supported yet (as of Chrome 56.)
53 //
54 // "http:" also includes "https:" implictly.
55 // https://www.chromestatus.com/feature/6653486812889088
56 value: "connect-src http:; child-src http:; " +
57 "frame-src http:; object-src http:"
58 });
59 return {responseHeaders: details.responseHeaders};
60 } 55 }
61 }, { 56
62 urls: ["http://*/*", "https://*/*"], 57 FilterNotifier.emit("filter.hitCount", cspMatch, 0, 0, page);
63 // We must also intercept script requests since otherwise Web Workers can 58 devtools.logRequest(page, details.url, "CSP", hostname, false, null,
64 // be abused to execute scripts for which our Content Security Policy 59 specificOnly, cspMatch);
65 // won't be injected. 60
66 // https://github.com/gorhill/uBO-Extra/issues/19 61 details.responseHeaders.push({
67 types: ["main_frame", "sub_frame", "script"] 62 name: "Content-Security-Policy",
68 }, ["blocking", "responseHeaders"]); 63 value: cspMatch.csp
69 } 64 });
65
66 return {responseHeaders: details.responseHeaders};
67 }
68 }, {
69 urls: ["http://*/*", "https://*/*"],
70 types: ["main_frame", "sub_frame"]
71 }, ["blocking", "responseHeaders"]);
OLDNEW
« no previous file with comments | « dependencies ('k') | lib/devtools.js » ('j') | metadata.chrome » ('J')

Powered by Google App Engine
This is Rietveld