Blog · Security

CSP without 'unsafe-inline' in Astro: 3 real pitfalls

· 4 min read · by

Originally written in Portuguese. Português

Dark cover with the title CSP without unsafe-inline in Astro and the Content-Security-Policy used on the site

4 min read

The N3XUS website is static, built with Astro, and is served with a strict Content Security Policy: no scripts or styles from outside our own domain and no styles embedded in the HTML. It seemed settled until the Lighthouse audit flagged console errors on a single page: a blog post.

The cause was not our code. It was three features of the ecosystem itself that generate inline styles by default.

In short

  • Astro’s default Markdown syntax highlighter (Shiki) writes style="" into code blocks. We switched to Prism, which uses classes.
  • The <Font /> component from the fonts API injects an inline <style>. We use the Fontsource CSS and preload manually.
  • Astro inlines small CSS into the HTML by default. We set inlineStylesheets: 'never'.

The policy we wanted to keep

The header is sent by nginx, in front of the site:

default-src 'self'; script-src 'self'; style-src 'self'; font-src 'self';
img-src 'self' data:; object-src 'none'; base-uri 'self'; form-action 'self';
frame-ancestors 'self'; upgrade-insecure-requests

We prefer the header over the <meta> tag because some directives do not work through meta. The MDN documentation is explicit about frame-ancestors, for example. With style-src 'self', any style="" attribute and any <style> in the HTML is blocked.

Pitfall 1: code highlighting with inline styles

By default, Astro colors Markdown code blocks with Shiki, and the colors go straight into the HTML:

<pre class="astro-code" style="background-color:#24292e;color:#e1e4e8">

The browser blocks these styles and logs a CSP error in the console. That is what dropped the post’s best practices score from 100 to 92.

Shiki’s defaultColor: false option does not solve it: it replaces the colors with CSS variables, but still writes the variables into a style attribute. The way out was Prism, which marks up the code with classes:

// astro.config.mjs
export default defineConfig({
  markdown: { syntaxHighlight: 'prism' },
});

Prism does not ship a theme, so we wrote our own in the site CSS, with half a dozen rules for .token.keyword, .token.string and .token.comment.

Pitfall 2: the fonts component

Astro’s fonts API is great: it downloads the fonts, hosts them on the site itself and generates fallback metrics. But the <Font /> component injects the @font-face declarations into a <style> inside the HTML, which our CSP blocks.

We kept the fonts through the Fontsource package, imported in the global CSS, which becomes an external file:

@import '@fontsource-variable/geist';
@import '@fontsource-variable/geist-mono';

That brought another problem: swapping the fallback font for the final font shifted the layout of the large headings. The solutions page reached a CLS of 0.33 on desktop. The fix was to preload the main files, importing the hashed URL directly from the package:

---
import geist from '@fontsource-variable/geist/files/geist-latin-wght-normal.woff2?url';
---
<link rel="preload" href={geist} as="font" type="font/woff2" crossorigin />

Vite returns the same URL the CSS uses, so the browser downloads the file only once. CLS dropped to 0 on every page.

Pitfall 3: small CSS inlined into the HTML

With the default setting (auto), Astro puts stylesheets smaller than 4 KB straight into the HTML, inside <style>. It is a performance optimization that conflicts with the CSP. The option below forces everything into files:

export default defineConfig({
  build: { inlineStylesheets: 'never' },
});

How to detect it before publishing

  • Browser console. Open each type of page (home, post with code, page with images) and look for “Content Security Policy”.
  • Lighthouse. The “Browser errors were logged to the console” audit reports the block and lowers the best practices score.
  • CI. In our pipeline, the build fails if any generated HTML contains style=":
- name: Nada de style inline
  run: "! grep -rl ' style=\"' dist --include=*.html"

We also avoid inline styles in our own code: what would once have been style="order:2" became a class. The only exception on the site is the admin panel, which runs a third-party CMS and gets its own, more permissive CSP, only on the /admin/ path, protected by login.

References

Want to review your site’s CSP or security headers? Talk to us.