Skip to content

added csp report json file generation #429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: v2
Choose a base branch
from
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