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

Powered by Google App Engine
This is Rietveld