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:- Pin the exact script with SRI. The
integrityhash lockscdn.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 oncdn.formo.soare immutable. - Restrict where scripts can send data with
connect-src. Even if an attacker injects code into your page, the browser blocksfetch, XHR, WebSocket, and beacon requests to hosts you did not list.connect-srcdoes not cover image loads or form submissions: useimg-srcandform-actionto limit those.
'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'.
Recommended policy (HTML snippet)
Put your write key indata-write-key on the SDK tag. The SDK starts itself and identifies the connected wallet, with no inline code.
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
- 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
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:
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 loadweb-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 aContent-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.