AbsoluteJS

Security Headers

secureHeaders() sends a strict Content-Security-Policy with a new nonce on every request, plus the usual security headers. AbsoluteJS puts that nonce on every inline script and style it writes, so hydration, islands and streaming keep working under the policy.

#Setup

The plugin is opt-in. Register it before absolutejs.

TS
// src/backend/server.ts
import { asset, prepare, secureHeaders } from '@absolutejs/absolute';
import { handleReactPageRequest } from '@absolutejs/absolute/react';
import { Elysia } from 'elysia';
import { Home } from '../frontend/react/pages/Home';

const { absolutejs, manifest } = await prepare();

export const server = new Elysia()
  .use(secureHeaders())
  .use(absolutejs)
  .get('/', () =>
    handleReactPageRequest({
      Page: Home,
      index: asset(manifest, 'HomeIndex')
    })
  );

Every response then carries these headers:

HeaderDefault
Content-Security-PolicyThe policy below, with a nonce
X-Content-Type-Optionsnosniff
Referrer-Policystrict-origin-when-cross-origin
Permissions-Policycamera=(), geolocation=(), microphone=(), payment=(), usb=()

#Default Policy

TXT
base-uri 'self';
connect-src 'self';
default-src 'self';
font-src 'self' data:;
form-action 'self';
img-src 'self' data: blob:;
manifest-src 'self';
object-src 'none';
script-src 'self' 'strict-dynamic' 'nonce-…';
style-src 'self' 'nonce-…';
style-src-attr 'unsafe-inline';
worker-src 'self' blob:;
frame-ancestors 'self'

Scripts run only with the request's nonce. There is no 'unsafe-inline' for scripts. 'strict-dynamic' lets a script that has the nonce load others, so hydration chunks, islands and lazy routes work without being listed.

Inline <style> elements need the nonce too. style="" attributes are allowed, because frameworks write them (Vue :style, Svelte style:) and they can't run script.

Under absolute dev the policy also allows the HMR WebSocket on the current host and 'unsafe-eval', which Angular's dev compiler needs. Production builds don't get either.

TXT
connect-src 'self' ws://localhost:3000 wss://localhost:3000;
script-src 'self' 'strict-dynamic' 'nonce-…' 'unsafe-eval';

#What Carries the Nonce

Each request gets a new 128-bit random nonce. AbsoluteJS adds it to every inline <script> and <style> it writes, in development and in production:

Hydration dataPage bootstraps and initial props for React, Svelte, Vue, Angular and Ember, plus the module preload links for page bundles.
Streamed slotsThe streaming-slot runtime and every patch script it sends as slots resolve.
IslandsIsland state, the island bootstrap, and each island stylesheet. Svelte and Vue island styles ship as a linked stylesheet per island, added with the nonce on the pages that render that island. Angular pages and islands pass the nonce to Angular in the browser, so styles it adds after hydration are allowed.
StylesInline page CSS and the styles Svelte and Angular render.
HTML & HTMXEvery inline script and style in your HTML and HTMX page files gets the nonce. htmx is configured to put it on its indicator styles.
Dev serverThe HMR client and the styles Angular adds on a hot update.
Other outputSpeculation rules and the error pages.

On React, Svelte, Vue and Angular pages the nonce is added where each script is written, not by rewriting the finished HTML. A script injected through dangerouslySetInnerHTML, v-html or {@html} stays blocked. That is what the policy is for.

#Your Own Scripts

An inline script you write on a framework page needs the nonce. In a React page, call getCspNonce() from @absolutejs/absolute/react. During server rendering it returns the request's nonce. In the browser it returns the same value, read from the page's own scripts, so the page hydrates with the nonce it was rendered with. It returns undefined when secureHeaders() isn't in use.

TSX
// src/frontend/react/pages/Home.tsx
import { getCspNonce } from '@absolutejs/absolute/react';

const analytics = 'window.analyticsReady = true;';

export const Home = () => (
  <html>
    <head>
      <title>Home</title>
    </head>
    <body>
      <h1>Home</h1>
      <script
        dangerouslySetInnerHTML={{ __html: analytics }}
        nonce={getCspNonce()}
        suppressHydrationWarning
      />
    </body>
  </html>
);

Browsers hide the nonce attribute once the page loads. React 19.2 reports that as a hydration mismatch in development, and suppressHydrationWarning keeps it quiet. React 19.3 reads the nonce the browser kept and doesn't warn.

External <script src> tags you add to a framework page need the nonce as well: 'strict-dynamic' ignores host allowlists.

In server code, read the nonce for the request being handled. Route handlers get it in the context as cspNonce. getCspNonce() from @absolutejs/absolute returns the same value in any server code that runs for the request, and undefined when secureHeaders() isn't in use. The root package is server code, so a page component imports getCspNonce from @absolutejs/absolute/react instead.

TS
// src/backend/server.ts
import { getCspNonce } from '@absolutejs/absolute';

.get('/status', () =>
  new Response(
    `<!doctype html>
<p id="status"></p>
<script nonce="${getCspNonce() ?? ''}">
  document.getElementById('status').textContent = 'ok';
</script>`,
    { headers: { 'content-type': 'text/html; charset=utf-8' } }
  )
)

#Customizing

Every directive and header can be changed or turned off.

TS
secureHeaders({
  contentSecurityPolicy: {
    directives: {
      'img-src': ["'self'", 'data:', 'https://images.example.com'],
      'object-src': false // remove a directive
    },
    reportOnly: true // send Content-Security-Policy-Report-Only
  },
  contentTypeOptions: false,
  frameAncestors: ["'none'"],
  permissionsPolicy: { camera: ['self'], geolocation: [] },
  referrerPolicy: 'no-referrer'
});
OptionEffect
contentSecurityPolicy.directivesDirective name to a list of sources. Each one replaces the default for that directive. false removes it.
contentSecurityPolicy.reportOnlySend Content-Security-Policy-Report-Only instead of Content-Security-Policy.
contentSecurityPolicy: falseSend no policy and turn nonces off.
frameAncestorsThe frame-ancestors sources. Default ['self']. false omits it.
permissionsPolicyFeature to allowlist. Replaces the whole default header. false omits it.
referrerPolicyHeader value. false omits it.
contentTypeOptionsfalse omits X-Content-Type-Options.

A directive you set replaces the default one, so list every source you still want. The nonce is still added to script-src and style-src. If you set script-src, keep 'strict-dynamic' in it so the scripts AbsoluteJS loads keep running.

reportOnly: true sends the same policy as Content-Security-Policy-Report-Only. The browser reports violations in the console and blocks nothing, which is a safe way to try a policy on a live site.

#Fonts

The default policy loads fonts and stylesheets from your own origin only. A stylesheet from fonts.googleapis.com is blocked, and so is the font prop of Head, which links one. Self-host your fonts with the fonts config (see Fonts). Its tags are links, not inline styles, so they need no nonce.

To keep loading fonts from Google, extend the policy:

TS
secureHeaders({
  contentSecurityPolicy: {
    directives: {
      'font-src': ["'self'", 'https://fonts.gstatic.com'],
      'style-src': ["'self'", 'https://fonts.googleapis.com']
    }
  }
});

#Pre-rendered Pages

When a page is pre-rendered or re-rendered by ISR under secureHeaders(), the nonce that render used is stored with it. Each time the page is served, that value is replaced with the request's nonce, and the response gets that request's policy. Markup injected into the page couldn't have known the render-time nonce, so it stays blocked.

A page pre-rendered without secureHeaders() has no nonce on its scripts. It is served without a Content-Security-Policy header, and the server logs a warning naming it. Rebuild with secureHeaders() in use to cover it.