csp.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { contentSecurityPolicy } from 'helmet'
  2. import { CONFIG } from '../initializers/config'
  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. styleSrc: [ '\'self\' \'unsafe-inline\'' ],
  12. objectSrc: [ '\'none\'' ], // only define to allow plugins, else let defaultSrc 'none' block it
  13. formAction: [ '\'self\'' ],
  14. frameAncestors: [ '\'none\'' ],
  15. baseUri: [ '\'self\'' ],
  16. manifestSrc: [ '\'self\'' ],
  17. frameSrc: [ '\'self\'' ], // instead of deprecated child-src / self because of test-embed
  18. workerSrc: [ '\'self\'', 'blob:' ] // instead of deprecated child-src
  19. },
  20. CONFIG.CSP.REPORT_URI ? { reportUri: CONFIG.CSP.REPORT_URI } : {},
  21. CONFIG.WEBSERVER.SCHEME === 'https' ? { upgradeInsecureRequests: [] } : {}
  22. )
  23. const baseCSP = contentSecurityPolicy({
  24. directives: baseDirectives,
  25. reportOnly: CONFIG.CSP.REPORT_ONLY
  26. })
  27. const embedCSP = contentSecurityPolicy({
  28. directives: Object.assign({}, baseDirectives, { frameAncestors: [ '*' ] }),
  29. reportOnly: CONFIG.CSP.REPORT_ONLY
  30. })
  31. // ---------------------------------------------------------------------------
  32. export {
  33. baseCSP,
  34. embedCSP
  35. }