How Do I set a CSP Nonce Using webpack?
Asked Answered
T

1

9

I need to add a nonce to the inline scripts created by webpack and I can't find any documentation on how to configure this. I've found this pr: https://github.com/webpack/webpack/pull/3210/files but I can't figure out how to set __webpack_nonce__ in my dev environment.

Tress answered 5/12, 2016 at 21:22 Comment(1)
It's over 4 years later and despite the docs improving and my spending many hours on the issue, I've never gotten this working.Littles
S
0

You can set the nonce value using the __webpack_nonce__ property. In a project with React, Umbraco, and Razor pages, I handled it in the master.cshtml page like this:

var xNonce = HttpContext.Current.Response.Headers.GetValues("X-Nonce")?.First();

<script type="application/javascript" nonce="@xNonce">
   __webpack_nonce__ = "@xNonce";
</script>
Shrine answered 8/8 at 13:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.