Skip to the content

Your server

Content security policy

A content security policy (CSP) tells the browser what a page may load and run. The editor works under a strict one. This page lists what it needs, and shows complete policies for common setups.

What the editor needs#

DirectiveSourcesWhy
script-src'wasm-unsafe-eval'The editor's engine is WebAssembly, compiled in the page
script-srchttps://cdn.dorvyn.devOnly if you load the SDK from Dorvyn's CDN instead of your bundle
connect-srchttps://api.dorvyn.devLicenses (only with a publishable key in the page)
connect-srchttps://export.dorvyn.devSaving as .docx (only with a publishable key in the page; EU organizations: https://eu.export.dorvyn.dev)
connect-srchttps://cdn.dorvyn.devDownloading the engine
style-src'unsafe-inline'The editor's own styles, and the document's formatting, which is inline styles
img-srcdata: blob:Pictures in documents, icons and shapes

In strict mode, licenses and saving go through your own server, so connect-src needs only 'self' (your route) and the CDN.

The editor needs no 'unsafe-eval' and no 'unsafe-inline' scripts, and loads no fonts. Printing uses a blank frame of the page's own, so frame-src needs nothing either.

Serving the engine yourself: if you set the engine's address to your own site, 'self' covers it, and you can leave out the CDN. A self-hosted export service needs its own address in connect-src (with a key in the page).

A complete policy (strict mode)#

Text
default-src 'self';
script-src 'self' 'wasm-unsafe-eval' 'nonce-{per-request}' 'strict-dynamic';
style-src 'self' 'unsafe-inline';
img-src 'self' data: blob:;
font-src 'self';
connect-src 'self' https://cdn.dorvyn.dev;
object-src 'none';
base-uri 'self';
form-action 'self';
frame-ancestors 'none';
upgrade-insecure-requests

Add your own sources (analytics, your fonts, your API) to it.

Next.js#

@dorvyn/next/csp adds the editor's sources to a policy of yours:

TypeScript
import { cspHeader, withDorvynCsp } from "@dorvyn/next/csp";

const policy = withDorvynCsp({
  "default-src": ["'self'"],
  "script-src": ["'self'"],
  "object-src": ["'none'"],
  "base-uri": ["'self'"],
  "frame-ancestors": ["'none'"],
});

cspHeader(policy);   // "default-src 'self'; script-src 'self' 'wasm-unsafe-eval' https://cdn.dorvyn.dev; …"

withDorvynCsp adds every source in the table above. A directive you didn't give starts from 'self'. Remove the API and export service from connect-src in strict mode if you like; they're unused there.

Pages rendered per request: a nonce#

Next adds a nonce to its own scripts when your proxy.ts sets one:

TypeScript
// proxy.ts
import { NextResponse, type NextRequest } from "next/server";
import { cspHeader, withDorvynCsp } from "@dorvyn/next/csp";

export function proxy(request: NextRequest) {
  const nonce = Buffer.from(crypto.randomUUID()).toString("base64");
  const dev = process.env.NODE_ENV === "development";
  const csp = cspHeader(
    withDorvynCsp({
      "default-src": ["'self'"],
      "script-src": ["'self'", `'nonce-${nonce}'`, "'strict-dynamic'", ...(dev ? ["'unsafe-eval'"] : [])],
      "object-src": ["'none'"],
      "base-uri": ["'self'"],
      "form-action": ["'self'"],
      "frame-ancestors": ["'none'"],
    }),
  );
  const headers = new Headers(request.headers);
  headers.set("x-nonce", nonce);
  headers.set("content-security-policy", csp);
  const response = NextResponse.next({ request: { headers } });
  response.headers.set("content-security-policy", csp);
  return response;
}

export const config = {
  matcher: [{ source: "/((?!api|_next/static|_next/image|favicon.ico).*)", missing: [{ type: "header", key: "next-router-prefetch" }] }],
};

Pages that use a nonce must be rendered per request (for example by reading headers() or cookies()). Next's development server needs 'unsafe-eval', which the example adds only in development.

Static pages: hashes or 'unsafe-inline'#

A page built ahead of time can't carry a fresh nonce. Next's inline scripts then need 'unsafe-inline' in script-src, or your build can list each inline script's hash. Set the header in next.config.ts:

TypeScript
// next.config.ts
import { cspHeader, withDorvynCsp } from "@dorvyn/next/csp";

const csp = cspHeader(withDorvynCsp({
  "default-src": ["'self'"],
  "script-src": ["'self'", "'unsafe-inline'"],
  "object-src": ["'none'"],
}));

export default {
  async headers() {
    return [{ source: "/(.*)", headers: [{ key: "Content-Security-Policy", value: csp }] }];
  },
};

With output: "export", Next doesn't serve the site, so headers() doesn't apply: set the header on your host (Amplify, Netlify, Vercel, CloudFront, nginx).

Other frameworks#

Set the same header from your server or host. For Vite and Angular builds, your scripts are files, so script-src 'self' 'wasm-unsafe-eval' is enough. Angular's development server injects inline styles and scripts, so use a looser policy there.

Checking it#

Open the browser's console on the page with the editor. A blocked source shows as "Refused to … because it violates the following Content Security Policy directive". Add what it names, if it's in the table above. Try Content-Security-Policy-Report-Only first to see what a policy would block without breaking anything.