From 11ccb4523ac4fa6d33dbabaefb79ad9f8097f6e1 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Wed, 14 Mar 2018 12:06:49 -0400 Subject: [PATCH] update fix for https://github.com/gorhill/uMatrix/issues/967 --- src/js/traffic.js | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/src/js/traffic.js b/src/js/traffic.js index 1856b7dea..452d50997 100644 --- a/src/js/traffic.js +++ b/src/js/traffic.js @@ -1077,15 +1077,45 @@ var injectCSP = function(pageStore, details) { // Ref.: https://www.w3.org/TR/CSP2/#implementation-considerations // // https://github.com/gorhill/uMatrix/issues/967 - // Inject a new CSP header rather than modify an existing one. - details.responseHeaders.push({ + // Inject a new CSP header rather than modify an existing one, except + // if the current environment does not support merging headers: + // Firefox 58/webext and less can't merge CSP headers, so we will merge + // them here. + var headers = details.responseHeaders; + + if ( cantMergeCSPHeaders ) { + var i = headerIndexFromName('content-security-policy', headers); + if ( i !== -1 ) { + cspSubsets.unshift(headers[i].value.trim()); + headers.splice(i, 1); + } + } + + headers.push({ name: 'Content-Security-Policy', value: cspSubsets.join(', ') }); - return { 'responseHeaders': details.responseHeaders }; + return { 'responseHeaders': headers }; }; +// https://github.com/gorhill/uMatrix/issues/967#issuecomment-373002011 +// This can be removed once Firefox 60 ESR is released. +var cantMergeCSPHeaders = (function() { + if ( + self.browser instanceof Object && + typeof self.browser.runtime.getBrowserInfo === 'function' + ) { + self.browser.runtime.getBrowserInfo().then(function(info) { + cantMergeCSPHeaders = + info.vendor === 'Mozilla' && + info.name === 'Firefox' && + parseInt(info.version, 10) < 59; + }); + } + return false; +})(); + /******************************************************************************/ // https://github.com/gorhill/uBlock/issues/1163