csp.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { contentSecurityPolicy } from 'helmet'
  2. import { CONFIG } from '../initializers/config.js'
  3. const baseDirectives = Object.assign({},
  4. {
  5. defaultSrc: [ '\'none\'' ], // by default, not specifying default-src = '*'
  6. connectSrc: [ '*', 'data:' ],
  7. mediaSrc: [ '\'self\'', 'https:', 'blob:' ],
  8. fontSrc: [ '\'self\'', 'data:' ],
  9. imgSrc: [ '\'self\'', 'data:', 'blob:' ],
  10. scriptSrc: [ '\'self\' \'unsafe-inline\' \'unsafe-eval\'', 'blob:' ],
  11. scriptSrcAttr: [ '\'unsafe-inline\'' ],
  12. styleSrc: [ '\'self\' \'unsafe-inline\'' ],
  13. objectSrc: [ '\'none\'' ], // only define to allow plugins, else let defaultSrc 'none' block it
  14. formAction: [ '\'self\'' ],
  15. frameAncestors: [ '\'none\'' ],
  16. baseUri: [ '\'self\'' ],
  17. manifestSrc: [ '\'self\'' ],
  18. frameSrc: [ '\'self\'' ], // instead of deprecated child-src / self because of test-embed
  19. workerSrc: [ '\'self\'', 'blob:' ] // instead of deprecated child-src
  20. },
  21. CONFIG.CSP.REPORT_URI
  22. ? { reportUri: CONFIG.CSP.REPORT_URI }
  23. : {},
  24. CONFIG.WEBSERVER.SCHEME === 'https'
  25. ? { upgradeInsecureRequests: [] }
  26. : {}
  27. )
  28. const baseCSP = contentSecurityPolicy({
  29. directives: baseDirectives,
  30. reportOnly: CONFIG.CSP.REPORT_ONLY
  31. })
  32. const embedCSP = contentSecurityPolicy({
  33. directives: Object.assign({}, baseDirectives, { frameAncestors: [ '*' ] }),
  34. reportOnly: CONFIG.CSP.REPORT_ONLY
  35. })
  36. // ---------------------------------------------------------------------------
  37. export {
  38. baseCSP,
  39. embedCSP
  40. }