@youneed/ssr-plugin-csp
v0.1.0
Published
SSR Content-Security-Policy for @youneed: per-request nonce, header builder, and inline-script nonce injection for document responses.
Readme
@youneed/ssr-plugin-csp
Content-Security-Policy tuned for SSR — for @youneed/server,
exposed as an SSR module for @youneed/server-plugin-ssr.
SSR injects several inline scripts (hydration JSON, speculation rules,
JSON-LD, the devtools payload). A strict CSP blocks inline scripts unless they
carry a matching nonce — so this middleware, for document responses only
(Accept: text/html):
- generates a per-request nonce,
- rewrites the document's
<script>tags to carry it, - sets
Content-Security-Policywith'nonce-…'inscript-src.
API/asset traffic is left untouched and unbuffered.
import { ssr } from "@youneed/server-plugin-ssr";
import { csp } from "@youneed/ssr-plugin-csp";
app.plugin(ssr({
pages: [Home],
modules: [csp({ directives: { "img-src": ["'self'", "https://cdn.example.com"] } })],
}));Or as plain server middleware:
import { cspMiddleware, getNonce } from "@youneed/ssr-plugin-csp";
app.use(cspMiddleware({ reportOnly: true, styleNonce: true }));
// inside a handler: getNonce(ctx) → the per-request nonceOptions: directives (merged over the defaults; a key replaces a directive
whole, false drops it), reportOnly, nonce (default true), styleNonce
(default false — shadow-DOM styles make blanket style nonces unreliable),
reportUri.
Build
pnpm --filter @youneed/ssr-plugin-csp run build