From 7379551e0c19374b48bda3cba81d8ef6ca537db4 Mon Sep 17 00:00:00 2001 From: Shahzeb khan <08bitshahzebk@seecs.edu.pk> Date: Fri, 3 Dec 2021 03:10:56 +0100 Subject: [PATCH 1/4] added csp report json file generation --- lib/middleware/csp.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/middleware/csp.js b/lib/middleware/csp.js index 76f6cca8..54e3648b 100644 --- a/lib/middleware/csp.js +++ b/lib/middleware/csp.js @@ -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"; @@ -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"] @@ -64,6 +65,7 @@ function createMiddleware(sCspUrlParameterName, oConfig) { defaultPolicy2 = null, defaultPolicy2IsReportOnly = false, definedPolicies = {}, + generateCSPReports = false, serveCSPReports = false, ignorePaths = [] } = oConfig; @@ -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) + { + log.error(new Error(`Cannot write file to path specified`)); + } + }); + } res.end(); } else { next(); From b50c8c1aaaf8d3e5185c526812c23bee6a2ab39e Mon Sep 17 00:00:00 2001 From: Shahzeb khan <08bitshahzebk@seecs.edu.pk> Date: Fri, 3 Dec 2021 03:15:42 +0100 Subject: [PATCH 2/4] Update csp.js --- lib/middleware/csp.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/middleware/csp.js b/lib/middleware/csp.js index 54e3648b..7359111e 100644 --- a/lib/middleware/csp.js +++ b/lib/middleware/csp.js @@ -101,10 +101,9 @@ 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) - { + if (generateCSPReports) { + fs.writeFile("./cspReport.json", JSON.stringify(cspReportEntries), function(err) { + if (err) { log.error(new Error(`Cannot write file to path specified`)); } }); From c691709f7c4226ac96b46914628190f0077fec3a Mon Sep 17 00:00:00 2001 From: Shahzeb khan <08bitshahzebk@seecs.edu.pk> Date: Fri, 3 Dec 2021 03:18:04 +0100 Subject: [PATCH 3/4] Update csp.js --- lib/middleware/csp.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/middleware/csp.js b/lib/middleware/csp.js index 7359111e..cc4a9696 100644 --- a/lib/middleware/csp.js +++ b/lib/middleware/csp.js @@ -104,7 +104,7 @@ function createMiddleware(sCspUrlParameterName, oConfig) { if (generateCSPReports) { fs.writeFile("./cspReport.json", JSON.stringify(cspReportEntries), function(err) { if (err) { - log.error(new Error(`Cannot write file to path specified`)); + log.error(new Error(`Cannot write file to path specified`)); } }); } From a79a863b631270fbab35261b320071301b804c3f Mon Sep 17 00:00:00 2001 From: Shahzeb khan <08bitshahzebk@seecs.edu.pk> Date: Fri, 3 Dec 2021 03:21:56 +0100 Subject: [PATCH 4/4] Update csp.js --- lib/middleware/csp.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/middleware/csp.js b/lib/middleware/csp.js index cc4a9696..34d7b281 100644 --- a/lib/middleware/csp.js +++ b/lib/middleware/csp.js @@ -104,7 +104,8 @@ function createMiddleware(sCspUrlParameterName, oConfig) { if (generateCSPReports) { fs.writeFile("./cspReport.json", JSON.stringify(cspReportEntries), function(err) { if (err) { - log.error(new Error(`Cannot write file to path specified`)); + const error = new Error(`Cannot write file to path specified`); + log.error(error); } }); }