File tree Expand file tree Collapse file tree 1 file changed +11
-5
lines changed Expand file tree Collapse file tree 1 file changed +11
-5
lines changed Original file line number Diff line number Diff line change 1- using System ;
2- using System . Collections . Generic ;
3- using System . Text ;
1+ using System ;
2+ using System . Security . Cryptography ;
43using Newtonsoft . Json ;
54using Newtonsoft . Json . Linq ;
65using StackifyLib . Utils ;
@@ -9,6 +8,8 @@ namespace StackifyLib.Web
98{
109 public static class RealUserMonitoring
1110 {
11+ private static readonly RandomNumberGenerator Rng = new RNGCryptoServiceProvider ( ) ;
12+
1213 public static string GetHeaderScript ( )
1314 {
1415 var rumScriptUrl = Config . RumScriptUrl ;
@@ -51,8 +52,13 @@ public static string GetHeaderScript()
5152 settings [ "Trans" ] = Convert . ToBase64String ( System . Text . Encoding . UTF8 . GetBytes ( reportingUrl ) ) ;
5253 }
5354
54- return string . Format ( @"<script type=""text/javascript"">(window.StackifySettings || (window.StackifySettings = {0}))</script><script src=""{1}"" data-key=""{2}"" async></script>" ,
55- settings . ToString ( Formatting . None ) , rumScriptUrl , rumKey ) ;
55+ // generate nonce for strict CSP rules
56+ var nonceBytes = new byte [ 20 ] ;
57+ Rng . GetNonZeroBytes ( nonceBytes ) ;
58+ var nonce = Convert . ToBase64String ( nonceBytes ) ;
59+
60+ return string . Format ( "<script type=\" text/javascript\" nonce=\" {3}\" >(window.StackifySettings || (window.StackifySettings = {0}))</script><script src=\" {1}\" data-key=\" {2}\" async></script>" ,
61+ settings . ToString ( Formatting . None ) , rumScriptUrl , rumKey , nonce ) ;
5662 }
5763 }
5864}
You can’t perform that action at this time.
0 commit comments