Commit 3445e40

Daniel Axtens <dja@axtens.net>
2026-10-04 23:17:31
more client-side security headers
1 parent 0b7e9a4
Changed files (3)
cgi-bin/fastfetch.sh
@@ -2,6 +2,7 @@
 
 echo "Content-Type: text/html"
 echo "Cache-Control: private, no-store"
+echo "Content-Security-Policy: style-src 'self' 'unsafe-inline'; default-src 'none'; require-trusted-types-for 'script';"
 echo
 echo
 /usr/local/bin/fastfetch --pipe false | /usr/local/bin/aha -b -t fastfetch
nginx/client-side-security.inc
@@ -0,0 +1,6 @@
+# These headers for client-side security are based on Mozilla's
+# recommendations
+add_header Referrer-Policy "no-referrer";
+add_header X-Content-Type-Options "nosniff";
+# todo: when we get CSP everywhere, fold this into the CSP
+add_header X-Frame-Options "DENY";
nginx/nginx.conf
@@ -50,10 +50,12 @@ http {
             }
 
             # should always be overridden below but just in case
-            # keep our CSP and give us a surrogate key to purge
+            # keep our security bits and bobs and give us a surrogate key to purge
             add_header Surrogate-Key "blog-etag-missing";
             add_header Last-Modified "";
-            add_header Content-Security-Policy "default-src 'none'; style-src 'self'; base-uri 'none'; require-trusted-types-for;";
+            add_header Content-Security-Policy "default-src 'none'; style-src 'self'; img-src 'self'; base-uri 'none'; require-trusted-types-for 'script';";
+            include client-side-security.inc;
+
 
             etag off;
             if ($hash_etag != "") {
@@ -67,7 +69,8 @@ http {
 
                 # Content security policy
                 # Assumes no JS, which works fine atm but may one day require adjusting
-                add_header Content-Security-Policy "default-src 'none'; style-src 'self'; base-uri 'none'; require-trusted-types-for;";
+                add_header Content-Security-Policy "default-src 'none'; style-src 'self'; img-src 'self'; base-uri 'none'; require-trusted-types-for 'script';";
+                include client-side-security.inc;
             }
         }
 
@@ -96,18 +99,28 @@ http {
                 return 304;
             }
 
+            # a weaker CSP because of inline styles
+            add_header Content-Security-Policy "default-src 'none'; style-src 'self' 'unsafe-inline'; img-src 'self'; base-uri 'none'; require-trusted-types-for 'script';";
+            include client-side-security.inc;
+
             etag off;
             if ($hash_etag != "") {
                 add_header ETag $hash_etag always;
                 add_header Cache-Control "public, s-maxage=31536000, stale-if-error=3600";
                 add_header Surrogate-Key "git";
                 add_header Last-Modified "";
+                add_header Content-Security-Policy "default-src 'none'; style-src 'self' 'unsafe-inline'; img-src 'self'; base-uri 'none'; require-trusted-types-for 'script';";
+                include client-side-security.inc;
             }
         }
 
         location ^~ /cgi-bin/ {
             root /usr/local/www/;
 
+            # don't set a default CSP, leave that to the scripts on the basis of what they use,
+            # but we can have the others.
+            include client-side-security.inc;
+
             # Point to the FreeBSD fcgiwrap Unix socket
             fastcgi_pass unix:/var/run/fcgiwrap/fcgiwrap.sock;