Skip to content

Commit 8856e97

Browse files
authored
Merge pull request #154 from gfulep/csp-allow-inline-with-nonce
Add nonce attribute to inline script element
2 parents eba5278 + 531fc07 commit 8856e97

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

src/Components/NToastNotifyViewComponent.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public NToastNotifyViewComponent(IToastNotification toastNotification, ILibrary
1818
_nToastNotifyOption = nToastNotifyOption;
1919
}
2020

21-
public IViewComponentResult Invoke()
21+
public IViewComponentResult Invoke(string? nonce = null)
2222
{
2323
var assemblyName = GetType().Assembly.GetName();
2424
var model = new ToastNotificationViewModel(
@@ -27,7 +27,8 @@ public IViewComponentResult Invoke()
2727
responseHeaderKey: Constants.ResponseHeaderKey,
2828
libraryDetails: _library,
2929
disableAjaxToasts: _nToastNotifyOption.DisableAjaxToasts,
30-
libraryJsPath: $"~/_content/{assemblyName.Name}/{_library.VarName}.js?{assemblyName.Version}");
30+
libraryJsPath: $"~/_content/{assemblyName.Name}/{_library.VarName}.js?{assemblyName.Version}",
31+
nonce: nonce);
3132

3233
return View("Default", model);
3334
}

src/Components/ToastNotificationViewModel.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,32 @@
22
{
33
public class ToastNotificationViewModel
44
{
5-
public ToastNotificationViewModel(string toastMessagesJson, string requestHeaderKey, string responseHeaderKey, ILibrary libraryDetails, bool disableAjaxToasts, string libraryJsPath)
5+
public ToastNotificationViewModel(string toastMessagesJson, string requestHeaderKey, string responseHeaderKey, ILibrary libraryDetails, bool disableAjaxToasts, string libraryJsPath, string? nonce)
66
{
77
ToastMessagesJson = toastMessagesJson;
88
RequestHeaderKey = requestHeaderKey;
99
ResponseHeaderKey = responseHeaderKey;
1010
LibraryDetails = libraryDetails;
1111
DisableAjaxToasts = disableAjaxToasts;
1212
LibraryJsPath = libraryJsPath;
13+
Nonce = nonce;
1314
}
15+
1416
/// <summary>
1517
/// JSON string of arrays of message
1618
/// </summary>
1719
public string ToastMessagesJson { get; }
20+
1821
/// <summary>
1922
/// Request header key used to show toast notification in AJAX calls
2023
/// </summary>
2124
public string RequestHeaderKey { get; }
25+
2226
/// <summary>
2327
/// Response header key used to show toast notification in AJAX calls
2428
/// </summary>
2529
public string ResponseHeaderKey { get; }
30+
2631
/// <summary>
2732
/// Library details
2833
/// </summary>
@@ -37,5 +42,10 @@ public ToastNotificationViewModel(string toastMessagesJson, string requestHeader
3742
/// The path of the js
3843
/// </summary>
3944
public string LibraryJsPath { get; set; }
45+
46+
/// <summary>
47+
/// Nonce value for allow the inline script to run if CSP is set
48+
/// </summary>
49+
public string? Nonce { get; set; }
4050
}
4151
}

src/Views/Shared/Components/NToastNotify/Default.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
throw new Exception();
66
}
77
<script src=@Url.Content(@Model.LibraryJsPath) type="text/javascript"></script>
8-
<script>
8+
<script nonce=@Model.Nonce>
99
if (nToastNotify) {
1010
nToastNotify.init({
1111
firstLoadEvent: 'DOMContentLoaded',

0 commit comments

Comments
 (0)