How-to
Website
Web app
Allow other origins in the security policy
With "build": { "csp": true } (on for a project wf init creates), every page carries a Content-Security-Policy that is 'self' throughout. A page that talks to an API on another domain, loads images from a CDN or embeds a video names those origins in webfluent.app.json, and the build widens the policy by exactly them. This config loads Google's tag manager after the page and allows what it sends and draws:
{
"meta": {
"scripts": [{ "src": "https://www.googletagmanager.com/gtag/js?id=G-XXXXXXX", "load": "after" }],
"connect": ["https://*.google-analytics.com", "https://*.analytics.google.com",
"https://www.googletagmanager.com"],
"img": ["https://*.google-analytics.com", "https://www.googletagmanager.com"]
}
}
How it works
- Requests. A page may send requests only to its own origin. Each entry of
meta.connect— an origin such ashttps://api.example.com, orhttps://*.example.comfor its subdomains — joinsconnect-src. - Images. A page may load images from its own origin and
data:, unlessmeta.imgnames others, which joinimg-src. Name a CDN there, or the pixels a tag requests (https://www.googletagmanager.com/a,/td), which a tag assistant otherwise reports as blocked. - Frames.
meta.framenames the origins a page may embed — a video player (https://www.youtube-nocookie.com), a map — and they joinframe-src. - Fonts, stylesheets and scripts need nothing here: the origins
meta.fonts,meta.stylesheetsandmeta.scriptsdeclare are added to the policy already, and your own scripts undersrc/are same-origin. - An entry with a path, or a keyword such as
'self', isE111.
A tag manager's own set-up — the dataLayer and the gtag('config', …) call a vendor gives you as an inline <script> — goes in a plain script under src/, which the policy already allows: the build refuses an inline script (Call your own JavaScript).
Variations
- An app's own scheme. An app that serves its pages itself — a desktop shell — has schemes of its own.
meta.connectandmeta.frametake one as a scheme alone:"ipc:"for the channel a shell's pages call it on,"wfpreview:"for a frame the app fills from its own protocol. A scheme named inmeta.frameis also one a frame'ssrcmay use: the address is kept, where any other unknown scheme is emptied. The web's schemes are refused:https:alone would allow every origin, anddata:,blob:andjavascript:run whatever a page writes (Make a desktop or phone app with Tauri). - WebAssembly. A page that compiles WebAssembly — a parser, an image codec — sets
meta.wasm: true, andscript-srcgains'wasm-unsafe-eval'. WebAssembly compiles;evalis still refused. - Style elements. A page whose scripts write
<style>elements — an editor such as CodeMirror, a chart library — setsmeta.style_elements: true, andstyle-srcgains'unsafe-inline', for styles only. - Pin another origin's files. A stylesheet or a font from another origin is code that origin can change after you read it. Declare its hash in
meta.integrity, keyed by its URL:
The build emits `integrity` and `crossorigin="anonymous"` on the tag, and warns (`D05`) about a declared external asset that has no hash. It never fetches the file to work the hash out, so a build does not depend on the network. A Google Fonts stylesheet is exempt: Google writes it per browser, so its bytes differ between readers and no hash can match. A library is pinned the same way ([Load a library from a CDN](/recipes/library-from-a-cdn)).
[Security](/about/security#the-content-security-policy) explains the policy itself, and how the build holds every page to it.