You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
3.7 KiB

4 years ago
  1. +++
  2. title = "Content Security Policy"
  3. description = "Control resources the user agent is allowed to load."
  4. categories = ["security"]
  5. tags = ["privacy", "xss", "csp"]
  6. features = ["code highlighter", "related content", "snippets"]
  7. [blackfriday]
  8. smartypants = false
  9. [[copyright]]
  10. owner = "Josh Habdas"
  11. date = "2019"
  12. license = "agpl-3.0-or-later"
  13. +++
  14. After Dark helps you secure your site using Content Security Policy (CSP). Basic CSP is enabled by default and [Advanced Configurations]({{< relref "#advanced-configuration" >}}) are also possible.
  15. Basic CSP offers minimal protection but is still helpful as it keeps resources from accidentally being loaded over `http`. In addition, Basic CSP automatically blocks all Flash and Silverlight objects before they have a chance to load.
  16. ## Basic Configuration
  17. Disable CSP site-wide add the following to your site config:
  18. ```toml
  19. [params]
  20. disable_csp = true # disable site-wide
  21. ```
  22. Disable CSP for a specific page using front matter:
  23. ```toml
  24. disable_csp = true # disable for page
  25. ```
  26. ## Advanced Configuration
  27. Advanced configuration allows you to fine-tune policy directives based on your specific needs. To get started specify at least 1 CSP directive in site config.
  28. {{% hackcss-alert type="info" %}}**Note:** You may wish to {{< external "https://gohugo.io/getting-started/usage/#disable-livereload" "Disable LiveReload" />}} while defining advanced config.{{% /hackcss-alert %}}
  29. Define the {{< external "https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src" "script-src" />}} directive site-wide:
  30. ```toml
  31. [params.security.csp.directives]
  32. scriptSrc = [
  33. "'self'",
  34. "'unsafe-inline'",
  35. "https:",
  36. "'sha512-Jx/MqTxYWqHdoOkHItRJJZCvFDhERPr5gG4I5ESu3V+BgQyAQ6wXfdsGzhzmT0yyvkAWz2jbrn81q90RRJTSTg=='",
  37. "'sha512-hno7WeTIciCJSjg/myjyK30HYkrcGCVwo4g4SpUalvrs3r2lS7bPNIQwbCNypKbg7BZ1sA4AsGnk6Gq4NOKpGA=='",
  38. "'sha512-ISTAV0GadOIz/NXXHOS&+eCM0ysXVVHhQTlvA6LJxz/DeA5yIxm0Vqf5IE&+WH0yuuXkayAKtoZkQ326nch5f/fg=='",
  39. "'strict-dynamic'"
  40. ]
  41. ```
  42. This will override the `script-src` directive's advanced default:
  43. Directive | Mapping | Advanced Default
  44. --- | --- | ---
  45. default-src | defaultSrc | 'none'
  46. connect-src | connectSrc | 'self'
  47. worker-src | workerSrc | 'self'
  48. font-src | fontSrc | 'self'
  49. media-src | mediaSrc | 'self'
  50. img-src | imgSrc | 'self' data:
  51. script-src | scriptSrc | 'none'
  52. style-src | styleSrc | 'self' 'unsafe-inline'
  53. frame-src | frameSrc | 'self'
  54. object-src | objectSrc | 'none'
  55. Once set, view the `Content-Security-Policy` meta tag in the `head` of your page and inspect the console for errors as you navigate to different pages. Each time you encounter an error caused by CSP is an opportunity to improve your policy.
  56. Add page-specific directives from page front matter using the same structure and naming conventions used in site config to append items for that page:
  57. ```toml
  58. [security.csp.directives]
  59. scriptSrc = [
  60. "'sha512-TKVuLlCT8+a0Chpa6Pw3clhu9fhZ9JOzgblgxQaUQVP/z4lfPnrdyWDOgucORnS2qapWu/iPVG2d0ywyGH2NjA=='"
  61. ]
  62. ```
  63. {{% hackcss-alert type="info" %}}**Note:** Page-specific directives _will not_ override any site-wide setting and will not apply without first overriding its advanced default via site config.{{% /hackcss-alert %}}
  64. Continue overriding advanced defaults as necessary until all CSP errors are resolved or you're satisfied with the changes.
  65. ## Additional Resources
  66. - About {{< external "https://infosec.mozilla.org/guidelines/web_security#content-security-policy" "Content Security Policy" />}}
  67. - Test policy with {{< external "https://observatory.mozilla.org" "Observatory by Mozilla" />}}
  68. - See {{< external "https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy#Examples" "CSP examples" />}} to learn more