CSP.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. const crypto = require('crypto')
  2. const path = require('path')
  3. module.exports = function ({
  4. reportUri,
  5. reportPercentage,
  6. reportOnly = false,
  7. exclude = [],
  8. }) {
  9. const header = reportOnly
  10. ? 'Content-Security-Policy-Report-Only'
  11. : 'Content-Security-Policy'
  12. const defaultPolicy = buildDefaultPolicy(reportUri)
  13. return function (req, res, next) {
  14. // set the default policy
  15. res.set(header, defaultPolicy)
  16. const originalRender = res.render
  17. res.render = (...args) => {
  18. const view = relativeViewPath(args[0])
  19. if (exclude.includes(view)) {
  20. // remove the default policy
  21. res.removeHeader(header)
  22. } else {
  23. // set the view policy
  24. res.locals.cspEnabled = true
  25. const scriptNonce = crypto.randomBytes(16).toString('base64')
  26. res.locals.scriptNonce = scriptNonce
  27. const policy = buildViewPolicy(scriptNonce, reportPercentage, reportUri)
  28. // Note: https://csp-evaluator.withgoogle.com/ is useful for checking the policy
  29. res.set(header, policy)
  30. }
  31. originalRender.apply(res, args)
  32. }
  33. next()
  34. }
  35. }
  36. const buildDefaultPolicy = (reportUri, styleSrc) => {
  37. const directives = [
  38. `base-uri 'none'`, // forbid setting a "base" element
  39. `default-src 'none'`, // forbid loading anything from a "src" attribute
  40. `form-action 'none'`, // forbid setting a form action
  41. `frame-ancestors 'none'`, // forbid loading embedded content
  42. `img-src 'self'`, // allow loading images from the same domain (e.g. the favicon).
  43. ]
  44. if (reportUri) {
  45. directives.push(`report-uri ${reportUri}`)
  46. // NOTE: implement report-to once it's more widely supported
  47. }
  48. if (styleSrc) {
  49. directives.push(`style-src ${styleSrc}`)
  50. }
  51. return directives.join('; ')
  52. }
  53. const buildViewPolicy = (scriptNonce, reportPercentage, reportUri) => {
  54. const directives = [
  55. `script-src 'nonce-${scriptNonce}' 'unsafe-inline' 'strict-dynamic' https: 'report-sample'`, // only allow scripts from certain sources
  56. `object-src 'none'`, // forbid loading an "object" element
  57. `base-uri 'none'`, // forbid setting a "base" element
  58. ]
  59. if (reportUri) {
  60. // enable the report URI for a percentage of CSP-enabled requests
  61. const belowReportCutoff = Math.random() * 100 <= reportPercentage
  62. if (belowReportCutoff) {
  63. directives.push(`report-uri ${reportUri}`)
  64. // NOTE: implement report-to once it's more widely supported
  65. }
  66. }
  67. return directives.join('; ')
  68. }
  69. const webRoot = path.resolve(__dirname, '..', '..', '..')
  70. // build the view path relative to the web root
  71. function relativeViewPath(view) {
  72. return path.isAbsolute(view)
  73. ? path.relative(webRoot, view)
  74. : path.join('app', 'views', view)
  75. }
  76. function removeCSPHeaders(res) {
  77. res.removeHeader('Content-Security-Policy')
  78. res.removeHeader('Content-Security-Policy-Report-Only')
  79. }
  80. /**
  81. * WARNING: allowing inline styles can open a security hole;
  82. * this is intended only for use in specific circumstances, such as Safari's built-in PDF viewer.
  83. */
  84. function allowUnsafeInlineStyles(res) {
  85. res.set(
  86. 'Content-Security-Policy',
  87. buildDefaultPolicy(undefined, "'unsafe-inline'")
  88. )
  89. }
  90. module.exports.buildDefaultPolicy = buildDefaultPolicy
  91. module.exports.removeCSPHeaders = removeCSPHeaders
  92. module.exports.allowUnsafeInlineStyles = allowUnsafeInlineStyles