HTML Basic
HTML Reference
- HTML Tags
- HTML Deprecated Tags
- HTML Events
- HTML Attributes
- accept
- accept-charset
- accesskey
- action
- align
- alt
- as
- async
- autocomplete
- autofocus
- autoplay
- bgcolor
- border
- charset
- checked
- cite
- class
- color
- cols
- colspan
- content
- contenteditable
- controls
- coords
- data
- data-*
- datetime
- default
- defer
- dir
- dirname
- disabled
- download
- draggable
- enctype
- enterkeyhint
- for
- form
- formaction
- headers
- height
- hidden
- high
- href
- hreflang
- http-equiv
- id
- inert
- inputmode
- ismap
- kind
- label
- lang
- list
- loop
- low
- max
- maxlength
- media
- method
- min
- multiple
- muted
- name
- novalidate
- onabort
- onafterprint
- onbeforeprint
- onbeforeunload
- onblur
- oncanplay
- oncanplaythrough
- onchange
- onclick
- oncontextmenu
- oncopy
- oncuechange
- oncut
- ondblclick
- ondrag
- ondragend
- ondragenter
- ondragleave
- ondragover
- ondragstart
- ondrop
- ondurationchange
- onemptied
- onended
- onerror
- onfocus
- onhashchange
- oninput
- oninvalid
- onkeydown
- onkeypress
- onkeyup
- onload
- onloadeddata
- onloadedmetadata
- onloadstart
- onmousedown
- onmousemove
- onmouseout
- onmouseover
- onmouseup
- onmousewheel
- onoffline
- ononline
- onpagehide
- onpageshow
- onpaste
- onpause
- onplay
- onplaying
- onpopstate
- onprogress
- onratechange
- onreset
- onresize
- onscroll
- onsearch
- onseeked
- onseeking
- onselect
- onstalled
- onstorage
- onsubmit
- onsuspend
- ontimeupdate
- ontoggle
- onunload
- onvolumechange
- onwaiting
- onwheel
- open
- optimum
- pattern
- placeholder
- popover
- popovertarget
- popovertargetaction
- poster
- preload
- readonly
- rel
- required
- reversed
- rows
- rowspan
- sandbox
- scope
- selected
- shape
- size
- sizes
- span
- spellcheck
- src
- srcdoc
- srclang
- srcset
- start
- step
- style
- tabindex
- target
- title
- translate
- type
- usemap
- value
- width
- wrap
- HTML Global Attributes
- HTML Status Code
- HTML Language Code
- HTML Country Code
- HTML Charset
- MIME Types
HTML nonce Attribute
Photo Credit to CodeToFun
🙋 Introduction
The nonce
attribute is an essential security feature in HTML that helps prevent certain types of attacks, such as cross-site scripting (XSS).
It is primarily used with inline scripts and style elements to ensure that only scripts/styles from trusted sources are executed/rendered by the browser.
🎯 Purpose of nonce
The main purpose of the nonce
attribute is to provide a cryptographic nonce (number used once) that is unique for each page load. This nonce is then included in the inline script or style element, and the browser only executes/render styles/scripts with matching nonces. This helps mitigate the risk of executing malicious scripts injected into a webpage.
💎 Values
The nonce
attribute accepts a cryptographic nonce value generated by the server. This value should be unpredictable and unique for each page load to ensure security. Here's an example of how the nonce
attribute is used:
<script nonce="ABC123">
// Inline script code here
</script>
🧠 How it Works
In this example, "ABC123" is the nonce value generated by the server. The browser will only execute this script if the page contains a matching nonce.
📄 Implementation Example:
Let's look at a simple example of how to use the nonce
attribute in an HTML document:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML nonce Attribute Example</title>
<script nonce="ABC123">
// Inline script code here
</script>
</head>
<body>
<h1>HTML `nonce` Attribute Example</h1>
<p>This is a simple example demonstrating the usage of the `nonce` attribute.</p>
</body>
</html>
🧠 How it Works
In this example, the nonce
attribute is set to "ABC123" for the inline script. The browser will only execute this script if it matches the nonce provided in the server response.
🔄 Dynamic Values with JavaScript
You can dynamically set the nonce
attribute using JavaScript, although this is less common than setting it server-side. Here's a brief example:
<script>
// Dynamically set nonce for an inline script
document.querySelector("script").nonce = "DEF456";
</script>
🧠 How it Works
In this script, the nonce
attribute is set to "DEF456" for the first <script> element found in the document. This can be useful in certain scenarios where nonce values need to be updated dynamically.
🏆 Best Practices
- Always use a secure and unpredictable nonce value generated by the server to ensure the effectiveness of the
nonce
attribute. - Ensure that the
nonce
attribute is included in all inline scripts and style elements to prevent unauthorized execution/rendering. - Regularly review and update nonce values to maintain security, especially in dynamic web applications.
🎉 Conclusion
The nonce
attribute is a crucial tool for enhancing the security of web applications by mitigating the risk of XSS attacks.
By understanding its purpose and proper implementation, developers can create safer and more secure web experiences.
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (HTML nonce Attribute), please comment here. I will help you immediately.