Modern Web Guidance is now stable! Upgrade your coding agents with expert-vetted web development best practices.
Read the announcement.
Stay organized with collections
Save and categorize content based on your preferences.
Add a nonce attribute to <script> elements
With a nonce-based CSP, every <script> element must have a nonce attribute
which matches the random nonce value specified in the CSP header (all scripts
can have the same nonce). The first step is to add these attributes to all
scripts:
Blocked by CSP
<script src="/path/to/script.js"></script>
<script>foo()</script>
CSP will block these scripts, because they don't have
`nonce` attributes.
Allowed by CSP
<script nonce="${NONCE}" src="/path/to/script.js"></script>
<script nonce="${NONCE}">foo()</script>
CSP will allow the execution of these scripts if `${NONCE}`
is replaced with a value matching the nonce in the CSP response header. Note
that some browsers will hide the `nonce` attribute when inspecting the page
source.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],[],[],[]]