Force CSP header for all requests
Currently styles / plugins were not actually under the CSP header protection. There's no real reason to not have them for all requests, so add them as a root middleware.
This commit is contained in:
parent
5d7e62ed67
commit
544146d9aa
@ -50,6 +50,7 @@ module.exports = function (options = {}) {
|
|||||||
app.set("env", "production")
|
app.set("env", "production")
|
||||||
.disable("x-powered-by")
|
.disable("x-powered-by")
|
||||||
.use(allRequests)
|
.use(allRequests)
|
||||||
|
.use(addSecurityHeaders)
|
||||||
.get("/", indexRequest)
|
.get("/", indexRequest)
|
||||||
.get("/service-worker.js", forceNoCacheRequest)
|
.get("/service-worker.js", forceNoCacheRequest)
|
||||||
.get("/js/bundle.js.map", forceNoCacheRequest)
|
.get("/js/bundle.js.map", forceNoCacheRequest)
|
||||||
@ -286,14 +287,7 @@ function allRequests(req, res, next) {
|
|||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
|
|
||||||
function forceNoCacheRequest(req, res, next) {
|
function addSecurityHeaders(req, res, next) {
|
||||||
// Intermittent proxies must not cache the following requests,
|
|
||||||
// browsers must fetch the latest version of these files (service worker, source maps)
|
|
||||||
res.setHeader("Cache-Control", "no-cache, no-transform");
|
|
||||||
return next();
|
|
||||||
}
|
|
||||||
|
|
||||||
function indexRequest(req, res) {
|
|
||||||
const policies = [
|
const policies = [
|
||||||
"default-src 'none'", // default to nothing
|
"default-src 'none'", // default to nothing
|
||||||
"base-uri 'none'", // disallow <base>, has no fallback to default-src
|
"base-uri 'none'", // disallow <base>, has no fallback to default-src
|
||||||
@ -317,10 +311,22 @@ function indexRequest(req, res) {
|
|||||||
policies.push("img-src http: https: data:");
|
policies.push("img-src http: https: data:");
|
||||||
}
|
}
|
||||||
|
|
||||||
res.setHeader("Content-Type", "text/html");
|
|
||||||
res.setHeader("Content-Security-Policy", policies.join("; "));
|
res.setHeader("Content-Security-Policy", policies.join("; "));
|
||||||
res.setHeader("Referrer-Policy", "no-referrer");
|
res.setHeader("Referrer-Policy", "no-referrer");
|
||||||
|
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
|
||||||
|
function forceNoCacheRequest(req, res, next) {
|
||||||
|
// Intermittent proxies must not cache the following requests,
|
||||||
|
// browsers must fetch the latest version of these files (service worker, source maps)
|
||||||
|
res.setHeader("Cache-Control", "no-cache, no-transform");
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
|
||||||
|
function indexRequest(req, res) {
|
||||||
|
res.setHeader("Content-Type", "text/html");
|
||||||
|
|
||||||
return fs.readFile(
|
return fs.readFile(
|
||||||
path.join(__dirname, "..", "client", "index.html.tpl"),
|
path.join(__dirname, "..", "client", "index.html.tpl"),
|
||||||
"utf-8",
|
"utf-8",
|
||||||
|
Loading…
Reference in New Issue
Block a user