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: Created Jan. 26, 2018, 6:05 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");
27
28 const {typeMap} = RegExpFilter;
25 29
26 browser.webRequest.onHeadersReceived.addListener(details => 30 browser.webRequest.onHeadersReceived.addListener(details =>
27 { 31 {
28 // Chrome seems to sometimes give the response type of "xmlhttprequest" 32 let url = new URL(details.url);
29 // instead of "main_frame", but when it does the tabId is always -1 and 33 let urlString = stringifyURL(url);
30 // the URL matches the initiator's URL (once normalised). 34 let parentFrame = ext.getFrame(details.tabId, details.parentFrameId);
31 // https://bugs.chromium.org/p/chromium/issues/detail?id=805649 35 let hostname = extractHostFromFrame(parentFrame) || getDecodedHostname(url);
kzar 2018/01/26 18:12:37 This bug is also potentially relevant for our ext/
32 if (details.type == "xmlhttprequest" && 36 let thirdParty = isThirdParty(url, hostname);
33 (details.tabId > -1 || details.initiator && 37
34 details.url != new URL(details.initiator).toString())) 38 let cspMatch = defaultMatcher.matchesAny(urlString, typeMap.CSP, hostname,
39 thirdParty, null, false);
40 if (cspMatch)
35 { 41 {
36 return; 42 let page = new ext.Page({id: details.tabId, url: details.url});
37 } 43 let frame = ext.getFrame(details.tabId, details.frameId);
38 44
39 // To avoid an extra matchesAny for the common case let's assume no 45 if (checkWhitelisted(page, frame))
40 // $genericblock filters apply when searching for a matching $csp filter, 46 return;
41 // we double check later when necessary.
42 let specificOnly = false;
43 47
44 let url = new URL(details.url); 48 // To avoid an extra matchesAny for the common case we assumed no
45 let hostname = getDecodedHostname(url); 49 // $genericblock filters applied when searching for a matching $csp filter.
46 let cspMatch = defaultMatcher.matchesAny( 50 // We must now pay the price by first checking for a $genericblock filter
47 details.url, 51 // and if necessary that our $csp filter is specific.
48 RegExpFilter.typeMap.CSP, 52 let specificOnly = !!checkWhitelisted(page, frame, typeMap.GENERICBLOCK);
49 hostname, false, null, specificOnly 53 if (specificOnly)
50 ); 54 {
55 cspMatch = defaultMatcher.matchesAny(urlString, typeMap.CSP, hostname,
56 thirdParty, null, specificOnly);
57 if (!cspMatch)
58 return;
59 }
51 60
52 if (cspMatch instanceof BlockingFilter) 61 devtools.logRequest(page, urlString, "CSP", hostname, thirdParty, null,
53 { 62 specificOnly, cspMatch);
54 if (details.tabId > -1) 63 FilterNotifier.emit("filter.hitCount", cspMatch, 0, 0, page);
55 {
56 let page = new ext.Page({id: details.tabId, url: details.url});
57 let frame = ext.getFrame(details.tabId, details.frameId);
58 64
59 if (checkWhitelisted(page, frame, RegExpFilter.typeMap.DOCUMENT | 65 if (cspMatch instanceof WhitelistFilter)
60 RegExpFilter.typeMap.CSP))
61 {
62 return;
63 }
64
65 // We pay the price now for skipping the specificOnly check earlier, we
66 // must check again for a CSP filter, this time making sure it's specific.
67 specificOnly = !!checkWhitelisted(page, frame,
68 RegExpFilter.typeMap.GENERICBLOCK);
69 if (specificOnly)
70 {
71 cspMatch = defaultMatcher.matchesAny(
72 details.url,
73 RegExpFilter.typeMap.CSP,
74 hostname, false, null, specificOnly
75 );
76 if (!(cspMatch instanceof BlockingFilter))
77 return;
78 }
79
80 FilterNotifier.emit("filter.hitCount", cspMatch, 0, 0, page);
81 }
82 // If we don't know the associated tab we'll have to check that if the URL
83 // is whitelisted manually. Things like $sitekey whitelisting and filter hit
84 // logging won't work, but it's the best we can do.
85 else if (defaultMatcher.whitelist.matchesAny(stringifyURL(url),
86 RegExpFilter.typeMap.DOCUMENT |
87 RegExpFilter.typeMap.CSP,
88 hostname))
89 {
90 return; 66 return;
91 }
92 67
93 details.responseHeaders.push({ 68 details.responseHeaders.push({
94 name: "Content-Security-Policy", 69 name: "Content-Security-Policy",
95 value: cspMatch.csp 70 value: cspMatch.csp
96 }); 71 });
97 72
98 return {responseHeaders: details.responseHeaders}; 73 return {responseHeaders: details.responseHeaders};
99 } 74 }
100 }, { 75 }, {
101 urls: ["http://*/*", "https://*/*"], 76 urls: ["http://*/*", "https://*/*"],
102 types: ["main_frame", "sub_frame", "xmlhttprequest"] 77 types: ["main_frame", "sub_frame"]
103 }, ["blocking", "responseHeaders"]); 78 }, ["blocking", "responseHeaders"]);
LEFTRIGHT

Powered by Google App Engine
This is Rietveld