Skip to content

Commit

Permalink
Change HTML middleware back to "use" / fix tests (async)
Browse files Browse the repository at this point in the history
  • Loading branch information
matz3 committed Jun 8, 2020
1 parent 7e5274c commit ebd252d
Show file tree
Hide file tree
Showing 2 changed files with 209 additions and 168 deletions.
56 changes: 25 additions & 31 deletions lib/middleware/csp.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,41 +61,35 @@ function createMiddleware(sCspUrlParameterName, oConfig) {
const router = new Router();
// .csplog
// body parser is required to parse csp-report in body (json)
if (serveCSPReports) {
router.post("/.ui5/csp/report.csplog", bodyParser.json({type: "application/csp-report"}));
router.post("/.ui5/csp/report.csplog", function(req, res, next) {
if (req.headers["content-type"] === "application/csp-report") {
// Write the violation into an array
// They can be retrieved via a request to '/.ui5/csp/csp-reports.json'
if (typeof req.body !== "object") {
const error = new Error(`No body content available: ${req.url}`);
log.error(error);
next(error);
return;
}
const cspReportObject = req.body["csp-report"];
if (cspReportObject) {
// extract the csp-report and add it to the cspReportEntries list
cspReportEntries.push(cspReportObject);
}
router.post("/.ui5/csp/report.csplog", bodyParser.json({type: "application/csp-report"}));
router.post("/.ui5/csp/report.csplog", function(req, res, next) {
if (req.headers["content-type"] === "application/csp-report") {
if (!serveCSPReports) {
res.end();
} else {
next();
return;
}
});
} else {
router.post("/.ui5/csp/report.csplog", function(req, res, next) {
if (req.headers["content-type"] === "application/csp-report") {
res.end();
} else {
next();
// Write the violation into an array
// They can be retrieved via a request to '/.ui5/csp/csp-reports.json'
if (typeof req.body !== "object") {
const error = new Error(`No body content available: ${req.url}`);
log.error(error);
next(error);
return;
}
});
}
const cspReportObject = req.body["csp-report"];
if (cspReportObject) {
// extract the csp-report and add it to the cspReportEntries list
cspReportEntries.push(cspReportObject);
}
res.end();
} else {
next();
}
});

// csp-reports.json
if (serveCSPReports) {
router.get("/.ui5/csp/csp-reports.json", function(req, res, next) {
router.get("/.ui5/csp/csp-reports.json", (req, res, next) => {
// serve csp reports
const body = JSON.stringify({
"csp-reports": cspReportEntries
Expand All @@ -109,11 +103,11 @@ function createMiddleware(sCspUrlParameterName, oConfig) {

// html get requests
// add csp headers
router.get("*", function(req, res, next) {
router.use((req, res, next) => {
const oParsedURL = parseurl(req);

// add CSP headers only to get requests for *.html pages
if (!oParsedURL.pathname.endsWith(".html")) {
if (req.method !== "GET" || !oParsedURL.pathname.endsWith(".html")) {
next();
return;
}
Expand Down
Loading

0 comments on commit ebd252d

Please sign in to comment.