csp.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import * as helmet 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:'],
  10. scriptSrc: ["'self' 'unsafe-inline' 'unsafe-eval'"],
  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: true } : {}
  22. )
  23. const baseCSP = helmet.contentSecurityPolicy({
  24. directives: baseDirectives,
  25. browserSniff: false,
  26. reportOnly: CONFIG.CSP.REPORT_ONLY
  27. })
  28. const embedCSP = helmet.contentSecurityPolicy({
  29. directives: Object.assign({}, baseDirectives, { frameAncestors: ['*'] }),
  30. browserSniff: false, // assumes a modern browser, but allows CDN in front
  31. reportOnly: CONFIG.CSP.REPORT_ONLY
  32. })
  33. // ---------------------------------------------------------------------------
  34. export {
  35. baseCSP,
  36. embedCSP
  37. }