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

Side by Side Diff: lib/csp.js

Issue 29705719: Issue 6402 - Split filter hit / request logging out into own API (Closed)
Patch Set: Addressed Manish's feedback Created May 1, 2018, 10:49 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
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 const {defaultMatcher} = require("../adblockpluscore/lib/matcher"); 20 const {defaultMatcher} = require("../adblockpluscore/lib/matcher");
21 const {RegExpFilter, WhitelistFilter} = 21 const {RegExpFilter, WhitelistFilter} =
22 require("../adblockpluscore/lib/filterClasses"); 22 require("../adblockpluscore/lib/filterClasses");
23 const {extractHostFromFrame, getDecodedHostname, 23 const {extractHostFromFrame, getDecodedHostname,
24 isThirdParty, stringifyURL} = require("./url"); 24 isThirdParty, stringifyURL} = require("./url");
25 const {checkWhitelisted} = require("./whitelisting"); 25 const {checkWhitelisted} = require("./whitelisting");
26 const {FilterNotifier} = require("filterNotifier"); 26 const {FilterNotifier} = require("filterNotifier");
27 const devtools = require("./devtools"); 27 const {logRequest} = require("./hitLogger");
28 28
29 const {typeMap} = RegExpFilter; 29 const {typeMap} = RegExpFilter;
30 30
31 browser.webRequest.onHeadersReceived.addListener(details => 31 browser.webRequest.onHeadersReceived.addListener(details =>
32 { 32 {
33 let url = new URL(details.url); 33 let url = new URL(details.url);
34 let urlString = stringifyURL(url); 34 let urlString = stringifyURL(url);
35 let parentFrame = ext.getFrame(details.tabId, details.parentFrameId); 35 let parentFrame = ext.getFrame(details.tabId, details.parentFrameId);
36 let hostname = extractHostFromFrame(parentFrame) || getDecodedHostname(url); 36 let docDomain = extractHostFromFrame(parentFrame) || getDecodedHostname(url);
kzar 2018/05/01 10:53:09 I renamed this in order to use the Object shorthan
Manish Jethani 2018/05/02 13:25:14 Acknowledged. Maybe rename urlString to url and a
kzar 2018/05/02 15:41:50 I think I prefer it like it is, I find the name ur
Sebastian Noack 2018/05/02 16:11:32 Agreed with kzar (also in favor to avoid wrapping
37 let thirdParty = isThirdParty(url, hostname); 37 let thirdParty = isThirdParty(url, docDomain);
38 38
39 let cspMatch = defaultMatcher.matchesAny(urlString, typeMap.CSP, hostname, 39 let cspMatch = defaultMatcher.matchesAny(urlString, typeMap.CSP, docDomain,
40 thirdParty, null, false); 40 thirdParty, null, false);
41 if (cspMatch) 41 if (cspMatch)
42 { 42 {
43 let page = new ext.Page({id: details.tabId, url: details.url}); 43 let page = new ext.Page({id: details.tabId, url: details.url});
44 let frame = ext.getFrame(details.tabId, details.frameId); 44 let frame = ext.getFrame(details.tabId, details.frameId);
45 45
46 if (checkWhitelisted(page, frame)) 46 if (checkWhitelisted(page, frame))
47 return; 47 return;
48 48
49 // To avoid an extra matchesAny for the common case we assumed no 49 // To avoid an extra matchesAny for the common case we assumed no
50 // $genericblock filters applied when searching for a matching $csp filter. 50 // $genericblock filters applied when searching for a matching $csp filter.
51 // We must now pay the price by first checking for a $genericblock filter 51 // We must now pay the price by first checking for a $genericblock filter
52 // and if necessary that our $csp filter is specific. 52 // and if necessary that our $csp filter is specific.
53 let specificOnly = !!checkWhitelisted(page, frame, null, 53 let specificOnly = !!checkWhitelisted(page, frame, null,
54 typeMap.GENERICBLOCK); 54 typeMap.GENERICBLOCK);
55 if (specificOnly) 55 if (specificOnly)
56 { 56 {
57 cspMatch = defaultMatcher.matchesAny(urlString, typeMap.CSP, hostname, 57 cspMatch = defaultMatcher.matchesAny(urlString, typeMap.CSP, docDomain,
58 thirdParty, null, specificOnly); 58 thirdParty, null, specificOnly);
59 if (!cspMatch) 59 if (!cspMatch)
60 return; 60 return;
61 } 61 }
62 62
63 devtools.logRequest([details.tabId], urlString, "CSP", hostname, 63 logRequest(
64 thirdParty, null, specificOnly, cspMatch); 64 [details.tabId],
65 {url: urlString, type: "CSP", docDomain, thirdParty, specificOnly},
66 cspMatch
67 );
65 FilterNotifier.emit("filter.hitCount", cspMatch, 0, 0, [details.tabId]); 68 FilterNotifier.emit("filter.hitCount", cspMatch, 0, 0, [details.tabId]);
66 69
67 if (cspMatch instanceof WhitelistFilter) 70 if (cspMatch instanceof WhitelistFilter)
68 return; 71 return;
69 72
70 details.responseHeaders.push({ 73 details.responseHeaders.push({
71 name: "Content-Security-Policy", 74 name: "Content-Security-Policy",
72 value: cspMatch.csp 75 value: cspMatch.csp
73 }); 76 });
74 77
75 return {responseHeaders: details.responseHeaders}; 78 return {responseHeaders: details.responseHeaders};
76 } 79 }
77 }, { 80 }, {
78 urls: ["http://*/*", "https://*/*"], 81 urls: ["http://*/*", "https://*/*"],
79 types: ["main_frame", "sub_frame"] 82 types: ["main_frame", "sub_frame"]
80 }, ["blocking", "responseHeaders"]); 83 }, ["blocking", "responseHeaders"]);
OLDNEW
« no previous file with comments | « include.preload.js ('k') | lib/cssInjection.js » ('j') | lib/devtools.js » ('J')

Powered by Google App Engine
This is Rietveld