header.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // https://scotthelme.co.uk/security-headers-cloudflare-worker/
  2. let securityHeaders =
  3. {
  4. "Content-Security-Policy" : "default-src 'self'; script-src https://aui-cdn.atlassian.com https://connect-cdn.atl-paas.net https://ajax.googleapis.com 'self' https://storage.googleapis.com https://apis.google.com https://*.pusher.com https://code.jquery.com https://www.dropbox.com https://api.trello.com 'sha256-JqdgAC+ydIDMtmQclZEqgbw94J4IeABIfXAxwEJGDJs=' 'sha256-4Dg3/NrB8tLC7TUSCbrtUDWD/J6bSLka01GHn+qtNZ0='; connect-src 'self' https://*.draw.io https://*.diagrams.net https://*.googleapis.com wss://*.pusher.com https://*.pusher.com https://api.github.com https://raw.githubusercontent.com https://gitlab.com https://graph.microsoft.com https://*.sharepoint.com https://*.1drv.com https://*.dropboxapi.com https://api.trello.com https://*.google.com https://fonts.gstatic.com https://fonts.googleapis.com; img-src * data:; media-src * data:; font-src * about:; frame-src https://www.lucidchart.com https://app.lucidchart.com 'self' https://www.draw.io https://*.google.com; style-src https://aui-cdn.atlassian.com https://*.atlassian.net 'self' 'unsafe-inline' https://fonts.googleapis.com;",
  5. "X-XSS-Protection" : "1; mode=block",
  6. "Feature-Policy" : "accelerometer 'none'; ambient-light-sensor 'none'; battery 'none'; camera 'none'; display-capture 'none'; document-domain 'none; encrypted-media 'none'; fullscreen 'none'; geolocation 'none'; gyroscope 'none'; magnetometer 'none'; midi 'none'; navigation-override 'none'; payment 'none'; picture-in-picture 'none'; usb 'none'; wake-lock 'none'; xr-spatial-tracking 'none';"
  7. }
  8. addEventListener('fetch', event =>
  9. {
  10. event.respondWith(addHeaders(event.request))
  11. })
  12. async function addHeaders(req)
  13. {
  14. let response = await fetch(req)
  15. let newHdrs = new Headers(response.headers)
  16. if (newHdrs.has("Content-Type") && !newHdrs.get("Content-Type").includes("text/html"))
  17. {
  18. return new Response(response.body ,
  19. {
  20. status: response.status,
  21. statusText: response.statusText,
  22. headers: newHdrs
  23. })
  24. }
  25. Object.keys(securityHeaders).map(function(name, index)
  26. {
  27. newHdrs.set(name, securityHeaders[name]);
  28. })
  29. return new Response(response.body ,
  30. {
  31. status: response.status,
  32. statusText: response.statusText,
  33. headers: newHdrs
  34. })
  35. }