A weak Content-Security-Policy filled with 'unsafe-inline' and wildcard CDNs defeats most of the XSS mitigation CSP was designed to provide. For static sites and documentation portals, you can often tighten the policy dramatically.
Eliminating Inline Dependencies
When building static pages with Hugo:
- Keep all CSS in a versioned static stylesheet (
/css/style.css). - Set
noClasses = falseunder[markup.highlight]so Chroma syntax highlighting emits CSS classes instead of inlinestyle="..."attributes on every token span. - Avoid third-party font CDNs or tracking scripts.
With zero inline styles or scripts in the rendered HTML, Nginx can enforce a lock-tight policy:
add_header Content-Security-Policy "default-src 'self'; style-src 'self'; script-src 'none'; img-src 'self' data:; font-src 'self'; connect-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'none'; form-action 'self';" always;
Watch Out for Nginx add_header Inheritance
One classic Nginx pitfall: if you declare add_header inside a location block (for example, Cache-Control on static assets), Nginx drops all add_header directives inherited from the server block.
Extracting security headers into an included snippet (security-headers.conf) and including it in every location block guarantees headers like X-Content-Type-Options: nosniff, X-Frame-Options: DENY, and Strict-Transport-Security are present on every response—including /healthz, CSS assets, and 404 pages.