csp.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import * as helmet from 'helmet'
  2. import { CONFIG } from '../initializers/constants'
  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:'],
  10. scriptSrc: ["'self' 'unsafe-inline'"],
  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. pluginTypes: ["'none'"],
  17. manifestSrc: ["'self'"],
  18. frameSrc: ["'self'"], // instead of deprecated child-src / self because of test-embed
  19. workerSrc: ["'self'"], // instead of deprecated child-src
  20. upgradeInsecureRequests: true
  21. },
  22. (CONFIG.SERVICES['CSP-LOGGER'] != null) ? { reportUri: CONFIG.SERVICES['CSP-LOGGER'] } : {}
  23. )
  24. const baseCSP = helmet.contentSecurityPolicy({
  25. directives: baseDirectives,
  26. browserSniff: false,
  27. reportOnly: true
  28. })
  29. const embedCSP = helmet.contentSecurityPolicy({
  30. directives: Object.assign(baseDirectives, {
  31. frameAncestors: ['*']
  32. }),
  33. browserSniff: false, // assumes a modern browser, but allows CDN in front
  34. reportOnly: true
  35. })
  36. // ---------------------------------------------------------------------------
  37. export {
  38. baseCSP,
  39. embedCSP
  40. }