Skip to content

Commit 1df0b0b

Browse files
Implement security hardening with CSP and localized assets
Implements a Content Security Policy (CSP) and localizes the vis-network dependency to ensure compliance. Additionally, this adds anti-forgery token validation to page models and forces the HTTPS scheme in middleware.
1 parent eb6e7ef commit 1df0b0b

5 files changed

Lines changed: 54 additions & 2 deletions

File tree

src/Visualiser/Pages/Index.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,6 @@
132132
@Html.AntiForgeryToken()
133133

134134
@section Scripts {
135-
<script type="text/javascript" src="https://unpkg.com/vis-network/standalone/umd/vis-network.min.js"></script>
135+
<script type="text/javascript" src="~/js/vis-network.min.js"></script>
136136
<script type="text/javascript" src="~/js/network.js" asp-append-version="true"></script>
137137
}

src/Visualiser/Pages/Index.cshtml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
using System.Text;
88
using System.Text.Json;
99
namespace Visualiser.Pages;
10-
10+
[ValidateAntiForgeryToken]
1111
public class IndexModel(IDownstreamApi api) : PageModel
1212
{
1313
public void OnGet() { }

src/Visualiser/Pages/ProcessedFiles.cshtml.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace Visualiser.Pages
1212
{
13+
[ValidateAntiForgeryToken]
1314
public class ProcessedFilesModel(IDownstreamApi api) : PageModel
1415
{
1516

src/Visualiser/Program.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,23 @@
5656
app.UseAuthentication();
5757
app.UseAuthorization();
5858

59+
app.Use((context, next) =>
60+
{
61+
context.Request.Scheme = "https";
62+
return next();
63+
});
64+
65+
app.Use(async (context, next) =>
66+
{
67+
if(context.Response.Headers.IsReadOnly == false)
68+
{
69+
string csp = app.Configuration["Content-Security-Policy"]
70+
?? "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self'; object-src 'self' data:; frame-src 'self' data:;";
71+
context.Response.Headers.Append("Content-Security-Policy", csp);
72+
}
73+
await next();
74+
});
75+
5976
app.MapStaticAssets();
6077
app.MapRazorPages()
6178
.WithStaticAssets();

src/Visualiser/wwwroot/js/vis-network.min.js

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)