Add style-src directive to CSP for binary template PDF output (#14573)

GitOrigin-RevId: c4a93af0a3dad8accbe2655c2323f2d14cc8e551
This commit is contained in:
Alf Eaton 2023-09-01 12:04:21 +01:00 committed by Copybot
parent 3bbce358c6
commit 5aeb461551

View file

@ -47,7 +47,7 @@ module.exports = function ({
}
}
const buildDefaultPolicy = reportUri => {
const buildDefaultPolicy = (reportUri, styleSrc) => {
const directives = [
`base-uri 'none'`, // forbid setting a "base" element
`default-src 'none'`, // forbid loading anything from a "src" attribute
@ -61,6 +61,10 @@ const buildDefaultPolicy = reportUri => {
// NOTE: implement report-to once it's more widely supported
}
if (styleSrc) {
directives.push(`style-src ${styleSrc}`)
}
return directives.join('; ')
}
@ -98,5 +102,17 @@ function removeCSPHeaders(res) {
res.removeHeader('Content-Security-Policy-Report-Only')
}
/**
* WARNING: allowing inline styles can open a security hole;
* this is intended only for use in specific circumstances, such as Safari's built-in PDF viewer.
*/
function allowUnsafeInlineStyles(res) {
res.set(
'Content-Security-Policy',
buildDefaultPolicy(undefined, "'unsafe-inline'")
)
}
module.exports.buildDefaultPolicy = buildDefaultPolicy
module.exports.removeCSPHeaders = removeCSPHeaders
module.exports.allowUnsafeInlineStyles = allowUnsafeInlineStyles