2018-07-16 00:14:44 +02:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
(function(){
|
|
|
|
"use strict";
|
|
|
|
|
2019-11-28 01:26:35 +01:00
|
|
|
let scope;
|
2018-07-16 00:14:44 +02:00
|
|
|
if ((typeof exports) !== "undefined"){
|
|
|
|
scope = exports;
|
|
|
|
}
|
|
|
|
else {
|
2019-03-12 22:24:23 +01:00
|
|
|
scope = require.register("./dataUrls", {});
|
2018-07-16 00:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const logging = require("./logging");
|
|
|
|
const settings = require("./settings");
|
2019-04-19 13:58:04 +02:00
|
|
|
const settingContainer = require("./settingContainers");
|
2018-07-24 21:30:57 +02:00
|
|
|
let canMergeHeader = false;
|
2018-07-24 23:30:00 +02:00
|
|
|
let blockBlob = true;
|
2018-07-24 21:30:57 +02:00
|
|
|
browser.runtime.getBrowserInfo().then(function(info){
|
2018-07-24 23:30:00 +02:00
|
|
|
const mainVersion = parseInt(info.version.replace(/\..+/, ""), 10);
|
|
|
|
canMergeHeader = mainVersion > 59;
|
|
|
|
blockBlob = mainVersion < 60;
|
2019-11-28 01:26:35 +01:00
|
|
|
return canMergeHeader;
|
|
|
|
}).catch(function(){
|
|
|
|
canMergeHeader = false;
|
|
|
|
blockBlob = true;
|
2018-07-24 21:30:57 +02:00
|
|
|
});
|
|
|
|
function setHeader(headers, header){
|
|
|
|
if (canMergeHeader){
|
|
|
|
headers.push(header);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const headerName = header.name.toLowerCase();
|
|
|
|
const presentHeader = headers.filter(function(h){
|
|
|
|
return h.name.toLowerCase() === headerName;
|
|
|
|
});
|
|
|
|
if (presentHeader.length){
|
|
|
|
presentHeader[0].value += ", " + header.value;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
headers.push(header);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-16 00:14:44 +02:00
|
|
|
|
|
|
|
scope.init = function(){
|
2018-09-06 08:38:22 +02:00
|
|
|
function listener(details){
|
2018-08-28 21:09:03 +02:00
|
|
|
const headers = details.responseHeaders;
|
2019-04-19 13:58:04 +02:00
|
|
|
if (settings.get("blockDataURLs", new URL(details.url))){
|
2018-07-24 23:30:00 +02:00
|
|
|
const cspMatch = (blockBlob? "": "blob: ") + "filesystem: *";
|
2018-08-28 21:09:03 +02:00
|
|
|
logging.verbose("Adding CSP header to", details);
|
|
|
|
setHeader(headers, {
|
|
|
|
name: "Content-Security-Policy",
|
|
|
|
value: `object-src ${cspMatch}; frame-src ${cspMatch}`
|
|
|
|
// + "; report-to https://canvasblocker.invalid/; report-uri https://canvasblocker.invalid/"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
responseHeaders: headers
|
|
|
|
};
|
|
|
|
}
|
|
|
|
function addListener(){
|
|
|
|
if (!browser.webRequest.onHeadersReceived.hasListener(listener)){
|
2019-04-19 13:58:04 +02:00
|
|
|
logging.message("add listener for CSP headers (data URL protection)");
|
2018-08-28 21:09:03 +02:00
|
|
|
browser.webRequest.onHeadersReceived.addListener(
|
|
|
|
listener,
|
|
|
|
{
|
|
|
|
urls: ["<all_urls>"],
|
|
|
|
types: ["main_frame", "sub_frame", "object"]
|
|
|
|
},
|
|
|
|
["blocking", "responseHeaders"]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function removeListener(){
|
2019-04-19 13:58:04 +02:00
|
|
|
if (browser.webRequest.onHeadersReceived.hasListener(listener)){
|
|
|
|
logging.message("remove listener for CSP headers (data URL protection)");
|
|
|
|
browser.webRequest.onHeadersReceived.removeListener(listener);
|
|
|
|
}
|
2018-08-28 21:09:03 +02:00
|
|
|
}
|
|
|
|
function adjustListener(){
|
2019-04-19 13:58:04 +02:00
|
|
|
if (
|
|
|
|
settings.blockDataURLs ||
|
|
|
|
settingContainer.urlContainer.get().some(function(entry){
|
|
|
|
return entry.blockDataURLs;
|
|
|
|
})
|
|
|
|
){
|
2018-08-28 21:09:03 +02:00
|
|
|
addListener();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
removeListener();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
settings.onloaded(adjustListener);
|
|
|
|
settings.on("blockDataURLs", adjustListener);
|
2019-04-19 13:58:04 +02:00
|
|
|
settingContainer.urlContainer.on(adjustListener);
|
2018-07-16 00:14:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}());
|