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

Unified 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.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « dependencies ('k') | lib/devtools.js » ('j') | metadata.chrome » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/csp.js
diff --git a/lib/csp.js b/lib/csp.js
index 6e7656d1b552a99a74279fefe5fe037a13a89840..b182e4414f42cb669e6c6a3ce7b3f3ece82f4756 100644
--- a/lib/csp.js
+++ b/lib/csp.js
@@ -17,53 +17,55 @@
"use strict";
-// The webRequest API doesn't support WebSocket connection blocking in Microsoft
-// Edge and versions of Chrome before 58. Therefore for those we inject CSP
-// headers below as a workaround. See https://crbug.com/129353 and
-// https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/10297376/
-if (!browser.webRequest.ResourceType ||
- !("WEBSOCKET" in browser.webRequest.ResourceType))
-{
- const {defaultMatcher} = require("matcher");
- const {BlockingFilter, RegExpFilter} = require("filterClasses");
- const {getDecodedHostname} = require("url");
- const {checkWhitelisted} = require("whitelisting");
+const {defaultMatcher} = require("matcher");
+const {BlockingFilter,
+ RegExpFilter,
+ WhitelistFilter} = require("filterClasses");
+const {getDecodedHostname, stringifyURL} = require("url");
+const {checkWhitelisted} = require("whitelisting");
+const {FilterNotifier} = require("filterNotifier");
+const devtools = require("devtools");
+
+const {typeMap} = RegExpFilter;
- browser.webRequest.onHeadersReceived.addListener(details =>
+browser.webRequest.onHeadersReceived.addListener(details =>
+{
+ let url = new URL(details.url);
+ let hostname = getDecodedHostname(url);
+ 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
+ false, null, false);
+ if (cspMatch)
{
- let hostname = getDecodedHostname(new URL(details.url));
- let match = defaultMatcher.matchesAny("", RegExpFilter.typeMap.WEBSOCKET,
- hostname, false, null, true);
- if (match instanceof BlockingFilter &&
- !checkWhitelisted(new ext.Page({id: details.tabId}),
- ext.getFrame(details.tabId, details.frameId)))
+ let page = new ext.Page({id: details.tabId, url: details.url});
+ let frame = ext.getFrame(details.tabId, details.frameId);
+ 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.
+ return;
+
+ // To avoid an extra matchesAny for the common case we assumed no
+ // $genericblock filters applied when searching for a matching $csp filter.
+ // We must now pay the price by first checking for a $genericblock filter
+ // and if necessary that our $csp filter is specific.
+ let specificOnly = checkWhitelisted(page, frame, typeMap.GENERICBLOCK);
+ if (specificOnly)
{
- details.responseHeaders.push({
- name: "Content-Security-Policy",
- // We're blocking WebSockets here by adding a connect-src restriction
- // since the Chrome extension API does not allow us to intercept them.
- // https://bugs.chromium.org/p/chromium/issues/detail?id=129353
- //
- // We also need the frame-src and object-src restrictions since CSPs
- // are not inherited from the parent for documents with data: and blob:
- // URLs, see https://crbug.com/513860.
- //
- // We must use the deprecated child-src directive instead of worker-src
- // since that's not supported yet (as of Chrome 56.)
- //
- // "http:" also includes "https:" implictly.
- // https://www.chromestatus.com/feature/6653486812889088
- value: "connect-src http:; child-src http:; " +
- "frame-src http:; object-src http:"
- });
- return {responseHeaders: details.responseHeaders};
+ cspMatch = defaultMatcher.matchesAny(details.url, typeMap.CSP, hostname,
+ false, null, specificOnly);
+ if (!(cspMatch instanceof BlockingFilter))
+ return;
}
- }, {
- urls: ["http://*/*", "https://*/*"],
- // We must also intercept script requests since otherwise Web Workers can
- // be abused to execute scripts for which our Content Security Policy
- // won't be injected.
- // https://github.com/gorhill/uBO-Extra/issues/19
- types: ["main_frame", "sub_frame", "script"]
- }, ["blocking", "responseHeaders"]);
-}
+
+ FilterNotifier.emit("filter.hitCount", cspMatch, 0, 0, page);
+ devtools.logRequest(page, details.url, "CSP", hostname, false, null,
+ specificOnly, cspMatch);
+
+ details.responseHeaders.push({
+ name: "Content-Security-Policy",
+ value: cspMatch.csp
+ });
+
+ return {responseHeaders: details.responseHeaders};
+ }
+}, {
+ urls: ["http://*/*", "https://*/*"],
+ types: ["main_frame", "sub_frame"]
+}, ["blocking", "responseHeaders"]);
« no previous file with comments | « dependencies ('k') | lib/devtools.js » ('j') | metadata.chrome » ('J')

Powered by Google App Engine
This is Rietveld