Skip to content

Integration

The browser needs the server’s public key and five files. You choose the path and the way.

pqfiber.Mount(app, "/any/path", sealer)

This also works on a group. pqfiber replaces headers a middleware has already set. Do not use adaptor.HTTPHandler(pqhttp.Handler(sealer)) for this: the adaptor appends headers. The worker would get two CSPs and the module would be blocked.

mux.Handle("/any/path/", http.StripPrefix("/any/path", pqhttp.Handler(sealer)))

sealer.PublicKey() returns the key as a string, web.Assets() the files with their headers. Serve each file at its Path, with ContentType, CacheControl, X-Content-Type-Options: nosniff and, if set, CSP. Body honours Accept-Encoding: if it reports gzip, send Content-Encoding: gzip, and with Gzip always Vary: Accept-Encoding.

The client gets the key through serverKey, on any route you like:

const seal = await SealClient.connect({
base: '/files/pq-seal',
serverKey: () => fetch('/api/my/key').then((response) => response.text()),
});

serverKey must deliver the current key on every call. A value written into the page stops working after a rotation.

The files must come from the same origin as the page, so a CDN does not work: workers and their policy require that.

The client is an ES module without dependencies and works in Svelte, Lit and without a framework. It is loaded at runtime and not bundled, so that it always matches the server.

const address = '/pq-seal/client.js';
const { SealClient } = await import(/* @vite-ignore */ address);
const seal = await SealClient.connect();

connect finds the other files next to client.js. Connect once per page and keep the client: loading takes 100 to 700 ms, encrypting only a few milliseconds.

If the application is not at the root path, the address needs that path too: `${import.meta.env.BASE_URL}pq-seal/client.js`. The same goes for the Trusted Types rule below.

The string in a variable is deliberate: the Vite dev server tries to resolve a literal, even with @vite-ignore.

If the worker dies, all calls fail with worker_failed and then closed. Its keys are gone, call connect again.

Forward the path to the backend:

export default {
server: { proxy: { '/pq-seal': 'http://localhost:8080' } },
};

Copy the types into the project:

Terminal window
cp "$(go list -m -f '{{.Dir}}' gitlab.techeve.de/techeve/pq-seal)/web/client.d.ts" src/pq-seal.d.ts
const address = '/pq-seal/client.js';
const { SealClient } = (await import(/* @vite-ignore */ address)) as typeof import('./pq-seal');

The page does not need 'wasm-unsafe-eval'. It needs:

  • script-src 'self', for client.js
  • worker-src 'self'
  • connect-src 'self'

The worker brings its own policy. If a proxy adds CSP headers, it must not replace or extend the worker’s.

With require-trusted-types-for 'script', pass scriptUrl:

const policy = trustedTypes.createPolicy('pq-seal', {
createScriptURL(url) {
if (new URL(url).pathname.startsWith('/pq-seal/worker-')) return url;
throw new TypeError(`not allowed: ${url}`);
},
});
const seal = await SealClient.connect({ scriptUrl: (url) => policy.createScriptURL(url) });

The module is about 1.2 MiB compressed. Thanks to the content hash in its address it stays in the HTTP cache and is not downloaded again on a second visit, measured in Chrome, Firefox and Safari.

For messages above 1 MiB, set Options.MaxBytes on the server and maxBytes in connect.