Security, SRI & CSP Nonces
Vite ships with built-in support for strict Content Security Policies (CSP) and Subresource Integrity (SRI), plus strict HTML attribute escaping on every emitted tag.
Content Security Policy (CSP) Nonces
Section titled “Content Security Policy (CSP) Nonces”Generate a cryptographically random per-request nonce in site/init.php or _init.php, send it in your Content-Security-Policy header, and pass it to Vite via useNonce():
<?php namespace ProcessWire;
$cspNonce = base64_encode(random_bytes(16));
header(sprintf( "Content-Security-Policy: default-src 'self'; script-src 'self' 'nonce-%s'; style-src 'self' 'nonce-%s';", $cspNonce, $cspNonce));
$vite = $this->wire()->vite->copy()->useNonce($cspNonce);
echo $vite->withEntries([ 'resources/css/app.css', 'resources/ts/app.ts',]);When a nonce is set:
- Every
<script>tag (including@vite/client, React Fast Refresh inline bootstrap, and production module scripts) receivesnonce="...". - Every
<link rel="stylesheet">and<link rel="modulepreload">tag receivesnonce="...". - Call
$vite->nonce()to read the active nonce for any inline scripts your template renders separately.
Subresource Integrity (SRI)
Section titled “Subresource Integrity (SRI)”If your Vite build uses an SRI plugin (such as vite-plugin-manifest-sri) that adds an integrity key to each chunk in manifest.json, Vite automatically emits:
integrity="sha384-..."crossorigin="anonymous"
on production <script>, <link rel="stylesheet">, and <link rel="modulepreload"> tags.
Customizing or Disabling SRI
Section titled “Customizing or Disabling SRI”// Read SRI hashes from a custom manifest property$vite->useIntegrity('sriHash');
// Disable SRI attribute emission completely$vite->useIntegrity(false);Production Hardening Checklist
Section titled “Production Hardening Checklist”| Control | Recommendation |
|---|---|
.gitignore the hot file |
Ensure site/templates/hot (and any custom hotFile) is never committed or deployed to staging/production. |
Lock down rootPath |
Point rootPath strictly to a directory containing only your build artifacts and manifest. |
| Escape dynamic attributes | Vite escapes all attribute values with htmlspecialchars(..., ENT_QUOTES, 'UTF-8'), but avoid passing untrusted visitor input into useScriptTagAttributes() or entry names. |
| Combine SRI + immutable cache headers | Serve /site/templates/build/assets/* with Cache-Control: public, max-age=31536000, immutable alongside SRI hashes. |