Async Clipboard API and User Activation vs Content Security Policy Nonces and strict-dynamic
Clipboard sanitization strips executable markup from pasted HTML rather than blocking scripts via CSP.
Structured Comparison Matrix
| Dimension | Async Clipboard API and User Activation | Content Security Policy Nonces and strict-dynamic |
|---|---|---|
| Mental Model | Promise-based OS clipboard access gated by HTTPS, document focus, and transient user activation tokens, supporting deferred Blob resolution via ClipboardItem. | HTTP-header script execution allowlist using per-response cryptographic nonces and 'strict-dynamic' trust propagation to block injected scripts without brittle domain allowlists. |
| Maturity & Domain | Baseline · Widely · Web APIs Chrome 66+ (writeText), 76+ (ClipboardItem) · Firefox 63+ (writeText), 127+ (ClipboardItem) · Safari 13.1+ | Baseline · Widely · Security & Accessibility Chrome 52+ · Firefox 52+ · Safari 15.4+ |
| Prerequisites | ||
| When Not to Use | When not to use navigator.clipboard.readText(): Never poll clipboard contents on focus; it triggers intrusive browser permission prompts and erodes user trust. | When not to use CSP nonces: Static SPAs served from an immutable CDN without edge compute should use SHA-256 script hashes ('sha256-...') instead of nonces. |
Engine Mechanics & Trade-Offs
The browser gates OS pasteboard IPC behind HTTPS, document focus, and a decaying transient activation token, re-encoding images to strip exploit payloads.
Key Pitfalls: Awaiting before calling write(): Even a 300ms fetch() before navigator.clipboard.writeText() triggers NotAllowedError in Safari; wrap the fetch() inside ClipboardItem. Unfocused document or iframe: Calling clipboard methods inside an iframe without allow="clipboard-write" or while DevTools holds focus throws NotAllowed
The HTML parser and script loader compare each <script nonce> attribute against the response header's one-time random token, propagating trust to dynamically loaded chunks under 'strict-dynamic'.
Key Pitfalls: Caching HTML with a baked-in nonce: If a CDN serves the same nonce to multiple users, attackers can read it and craft matching <script nonce="..."> payloads; mint nonces dynamically per response. Omitting base-uri 'none' and object-src 'none': Without base-uri 'none', an injected <base href="https://evil.test"> hijacks