Home / Notes /

A strict Content-Security-Policy on a static site

The portfolio is plain HTML, CSS and JavaScript. Making it run with no inline script meant changing how every button works, and it removed a whole class of problems.

A Content-Security-Policy is a response header that tells the browser what a page is allowed to load and run. If something is not on the list, the browser refuses it and logs the refusal. This site is static, so there is no server code to attack, but an injected script would still run with the page's full rights. The policy is what stops that.

The policy

default-src 'self';
script-src 'self' 'sha256-…' https://cdn.jsdelivr.net https://cdnjs.cloudflare.com;
style-src 'self' 'unsafe-inline' https://fonts.googleapis.com;
font-src 'self' https://fonts.gstatic.com;
img-src 'self' data: https:;
connect-src 'self' https://scorpia-monitor.vercel.app;
object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'

The line that matters most is script-src. There is no 'unsafe-inline' in it, so the browser will not run a <script> block written into the page, and it will not run an onclick="…" attribute either.

What had to change

Inline handlers became data attributes. The page had buttons like onclick="openProjectModal('minutes-ai')". Each one is now a plain attribute, and a single listener in a separate file reads it:

document.addEventListener('click', function (e) {
  var el = e.target.closest('[data-project-modal], [data-copy], [data-print]');
  if (!el) return;
  if (el.hasAttribute('data-project-modal')) {
    window.openProjectModal(el.getAttribute('data-project-modal'));
  }
  …
});

One listener for the whole page is also less code than a handler on every button.

One inline script stayed, by hash. The theme has to be set before the first paint, or a visitor who chose light mode sees a dark flash. That tiny script sits in the <head>, and the policy allows it by the SHA-256 of its exact text. Change one character and the browser refuses it, which is the point.

Outside scripts are pinned. three.js and GSAP load from two CDNs. Each tag names an exact version and carries an integrity hash, so a file that changed on the CDN would not run.

The page can call exactly one outside address. connect-src lists the monitoring app, which receives the contact form and answers the status line in the footer. A script that tried to send data anywhere else would be blocked.

No framing, no plugins. frame-ancestors 'none' means no other site can put these pages in a frame, and object-src 'none' rules out plugin content.

What is still loose

  • style-src keeps 'unsafe-inline'. The markup uses a few style="…" attributes, and removing them all was not worth it yet. Injected styles are a much smaller risk than injected scripts.
  • img-src allows any HTTPS image.
  • /stream is a separate Next.js app and carries a lighter policy of its own.

How I check it

I open the page with the browser console open and click everything. A broken rule shows up as a red line that names the directive and the thing it blocked. After every change to the header I do it again, in both themes, because the failure is silent to a visitor: a button that simply does nothing.