Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/middleware/csp.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const parseurl = require("parseurl");
const Router = require("router");
const querystring = require("querystring");

const fs = require("fs");
const log = require("@ui5/logger").getLogger("server:middleware:csp");

const HEADER_CONTENT_SECURITY_POLICY = "Content-Security-Policy";
Expand Down Expand Up @@ -42,6 +42,7 @@ function containsPath(uriPath, req, pathName) {
* @property {string} defaultPolicy2
* @property {boolean} defaultPolicy2IsReportOnly
* @property {object} definedPolicies
* @property {boolean} generateCSPReports whether to generate the csp resources
* @property {boolean} serveCSPReports whether to serve the csp resources
* @property {string[]} ignorePaths URI paths which are ignored by the CSP reports,
* e.g. ["test-resources/sap/ui/qunit/testrunner.html"]
Expand All @@ -64,6 +65,7 @@ function createMiddleware(sCspUrlParameterName, oConfig) {
defaultPolicy2 = null,
defaultPolicy2IsReportOnly = false,
definedPolicies = {},
generateCSPReports = false,
serveCSPReports = false,
ignorePaths = []
} = oConfig;
Expand Down Expand Up @@ -99,6 +101,14 @@ function createMiddleware(sCspUrlParameterName, oConfig) {
// extract the csp-report and add it to the cspReportEntries list
cspReportEntries.push(cspReportObject);
}
if (generateCSPReports) {
fs.writeFile("./cspReport.json", JSON.stringify(cspReportEntries), function(err) {
if (err) {
const error = new Error(`Cannot write file to path specified`);
log.error(error);
}
});
}
res.end();
} else {
next();
Expand Down