Skip to main content

Overview

As described on MDN: Content Security Policy (CSP) is a feature that helps to prevent or minimize the risk of certain types of security threats. It consists of a series of instructions from a website to a browser, which instruct the browser to place restrictions on the things that the code comprising the site is allowed to do. The primary use case for CSP is to control which resources, in particular JavaScript resources, a document is allowed to load. This is mainly used as a defense against cross-site scripting (XSS) attacks, in which an attacker is able to inject malicious code into the victim’s site.

Why CSP and SRI belong together

A CSP host allowlist says where scripts may come from. It does not say what those scripts contain. If you allowlist a third-party domain and that vendor (or its CDN) is compromised, the browser will happily run whatever the domain serves. This is how compromised analytics and A/B testing scripts have shipped malware to large sites in the past. Formo closes that gap in two ways:
  1. Pin the exact script with SRI. The integrity hash locks cdn.formo.so/analytics@<version> to known bytes. If the file changes, the browser refuses to run it, even though the domain is allowlisted. Pinned versions on cdn.formo.so are immutable.
  2. Restrict where scripts can send data with connect-src. Even if an attacker injects code into your page, the browser blocks fetch, XHR, WebSocket, and beacon requests to hosts you did not list. connect-src does not cover image loads or form submissions: use img-src and form-action to limit those.
The Formo SDK does not need 'unsafe-eval' or 'unsafe-inline'. It uses no eval(), new Function(), innerHTML, document.write(), or dynamic script injection. Do not add either keyword for Formo.

Domains used by Formo

Allowlist these exact hosts rather than https://*.formo.so. A wildcard trusts every current and future subdomain. If you send events through a proxy with the apiHost option, list your proxy host in connect-src instead of events.formo.so. A same-origin path such as /api/ingest is already covered by 'self'.
Do not initialize the SDK with an inline onload="..." attribute when you enable CSP. Inline event handlers are blocked unless the policy allows 'unsafe-inline', so the SDK script loads but formofy() never runs and no events are sent.
Put your write key in data-write-key on the SDK tag. The SDK starts itself and identifies the connected wallet, with no inline code.
Get the latest version and integrity hash from your project settings or the GitHub releases. Keep the version in src and the hash in integrity in sync: a mismatch blocks the script.

Options and callbacks

data-write-key uses the default options. To pass options or a ready callback, leave out data-write-key and move the formofy() call into a small script served from your own domain. Load it with defer right after the SDK. Deferred scripts run in document order, so window.formofy exists when your init script runs.
formo-init.js
This configuration ensures:
  • Scripts can only load from your domain and cdn.formo.so, and SRI pins the SDK script to known bytes
  • The SDK bytes are cryptographically verified before execution
  • fetch, XHR, and beacon requests can only go to your domain and Formo’s ingestion endpoint
  • No 'unsafe-inline' or 'unsafe-eval' is needed
Add the directives above to your existing policy rather than replacing it. Prefer sending CSP as an HTTP response header over a <meta> tag: the header also supports frame-ancestors and reporting, which <meta> ignores.

Nonce-based policies ('strict-dynamic')

If your site already uses a nonce-based CSP (for example the Next.js nonce middleware), add your per-request nonce to the Formo script tag. With 'strict-dynamic', host allowlists in script-src are ignored, so the nonce is what authorizes the script:
You still need https://events.formo.so (or your proxy host) in connect-src.

NPM package installs

When you bundle @formo/analytics from npm, the SDK is served from your own domain, so no script-src change is needed. Add only the ingestion host:

Web Vitals

If you load web-vitals from unpkg for Web Vitals tracking, add https://unpkg.com to script-src and keep the pinned version and integrity hash on that tag too. Bundling web-vitals from npm avoids the extra host.

Rolling out safely

To test a policy without breaking your site, send it first as a Content-Security-Policy-Report-Only HTTP header with a report-uri or report-to endpoint. Check the reports (or the browser console) for violations, then switch to the enforcing Content-Security-Policy header. To confirm Formo works under your policy, open the browser console and run typeof window.formofy (should print "function"), then check the Network tab for requests to events.formo.so and the console for any Content Security Policy errors.