-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a Content Security Policy (CSP) middleware which can be enabled via URL parameter (sap-ui-xx-csp-policy). One can set the value to a policy string directly, or use one of the already defined policy IDs (sap-target-level-1, sap-target-level-2). The OpenUI5 QUnit test "[sap/ui/core/qunit/csp/ContentSecurityPolicy.qunit.html](https://github.com/SAP/openui5/blob/master/src/sap.ui.core/test/sap/ui/core/qunit/csp/ContentSecurityPolicy.qunit.html)" can be used for a starting point validating for CSP compliance.
- Loading branch information
Showing
3 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const url = require("url"); | ||
const querystring = require("querystring"); | ||
|
||
const HEADER_CONTENT_SECURITY_POLICY = "Content-Security-Policy"; | ||
const HEADER_CONTENT_SECURITY_POLICY_REPORT_ONLY = "Content-Security-Policy-Report-Only"; | ||
const rPolicy = /([-_a-zA-Z0-9]+)(:report-only)?/i; | ||
|
||
function createMiddleware(sCspUrlParameterName, oConfig) { | ||
const { | ||
allowDynamicPolicySelection=false, | ||
allowDynamicPolicyDefinition=false, | ||
defaultPolicyIsReportOnly=false | ||
} = oConfig; | ||
|
||
return function csp(req, res, next) { | ||
let oPolicy; | ||
let bReportOnly = defaultPolicyIsReportOnly; | ||
|
||
// If a policy with name 'default' is defined, it will even be send without a present URL parameter. | ||
if (oConfig.definedPolicies["default"]) { | ||
oPolicy = { | ||
name: "default", | ||
policy: oConfig.definedPolicies["default"] | ||
}; | ||
} | ||
|
||
// Use random protocol, host and port to establish a valid URL for parsing query parameters | ||
let oParsedUrl = url.parse(req.url); | ||
let oQuery = querystring.parse(oParsedUrl.query); | ||
let sCspUrlParameterValue = oQuery[sCspUrlParameterName]; | ||
|
||
if (sCspUrlParameterValue) { | ||
let mPolicyMatch = rPolicy.exec(sCspUrlParameterValue); | ||
|
||
if (mPolicyMatch && mPolicyMatch[1] | ||
&& oConfig.definedPolicies[mPolicyMatch[1]] && allowDynamicPolicySelection) { | ||
oPolicy = { | ||
name: mPolicyMatch[1], | ||
policy: oConfig.definedPolicies[mPolicyMatch[1]] | ||
}; | ||
bReportOnly = mPolicyMatch[2] !== undefined; | ||
} else if (allowDynamicPolicyDefinition) { | ||
// Custom CSP policy directives get passed as part of the CSP URL-Parameter value | ||
bReportOnly = sCspUrlParameterValue.endsWith(":report-only"); | ||
if (bReportOnly) { | ||
sCspUrlParameterValue = sCspUrlParameterValue.slice(0, - ":report-only".length); | ||
} | ||
oPolicy = { | ||
name: "dynamic-custom-policy", | ||
policy: sCspUrlParameterValue | ||
}; | ||
} | ||
} | ||
|
||
if (oPolicy) { | ||
let sHeader = bReportOnly ? HEADER_CONTENT_SECURITY_POLICY_REPORT_ONLY : HEADER_CONTENT_SECURITY_POLICY; | ||
let sHeaderValue = oPolicy.policy; | ||
|
||
// Send response with CSP header | ||
res.removeHeader(HEADER_CONTENT_SECURITY_POLICY); | ||
res.removeHeader(HEADER_CONTENT_SECURITY_POLICY_REPORT_ONLY); | ||
res.setHeader(sHeader, sHeaderValue); | ||
} | ||
|
||
next(); | ||
}; | ||
} | ||
|
||
module.exports = createMiddleware; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters