If you’re thrilled about Hot Reload like me AND you likewise desire an “A” grade from SecurityHeaders.com (truly, go attempt this now) then you will discover extremely rapidly about Content-Security-Policy headers. You require to invest a long time reading and you might wind up with a rather advanced list of enabled things, scripts, stylesheets, and so on
In DasBlog Core (the cross platform blog site engine that runs this blog site) Mark Downie makes these configurable and utilizes the NWebSpec ASP.NET Middleware library to include the required headers.
if (SecurityStyleSources!= null && & & SecurityScriptSources! =null && & & DefaultSources! =null)
app.UseCsp( choices => > choices
DefaultSources( s => > s.Self()
StyleSources( s => > s.Self()
ScriptSources( s => > s.Self()
Each of those variables comes out of a config file. Yes, it would be more security if they came out of a vault or were even difficult coded.
DasBlog is a quite big and cool app and we observed right away upon Mark updating it to.NET 6 that we were not able to utilize Hot Reload (through dotnet watch or from VS 2022). We can grumble about it, or we can discover how it works and why it’s not working for us!
<< script src=" http://feeds.hanselman.com/_framework/aspnetcore-browser-refresh.js"><>
Ok, this makes good sense as we understand not just does HotReload assistance C# (code behinds) however likewise Markup through Razor Pages and altering CSS! It would absolutely require to interact “back house” to the runner which is either “dotnet watch” or VS2022.
If I alter the ASPNETCORE_ENVIRONMENT to “Production” (either through launch.json, launchsettings, or an environment variable like this, I can see that additional HotReload assistant script isn’t there:
C: githubwshotreloadtest>> dotnet run-- environment=" Production"
Now listening on: https://localhost:7216
Now listening on: http://localhost:5216
Keep In Mind: You never ever wish to utilize dotnet run in production! It’s an SDK structure command! You’ll wish to utilize dotnet officer your.dll, dotnet your.dll, or most importantly, in.NET 6 simply call the EXE straight!. binDebugnet6.0wshotreloadtest.exe in my example. Why? dotnet run will constantly presume it remains in Advancement (you actually inform it to bring back, develop, and officer in one run command) if you run it. You’ll keep in mind that running the real EXE is constantly method much faster also! Do not deliver your.NET SDK to your webserver and do not recompile the entire thing on start-up in production!
We can see that that aspnnetcore-browser-refresh. js is the customer side of Development-time HotReload. Taking a look at our internet browser console we see:
Declined to link to 'wss:// localhost:62486/'
due to the fact that it breaks the following Material Security Policy
regulation: "default-src 'self'".
Keep in mind that 'connect-src' was not clearly set,
so 'default-src' is utilized as an alternative.
That’s a lot to consider. I began my ASP.NET Web App’s middle ware stating it was okay to talk “back to myself” however no place else.
app.UseCsp( choices => > options.DefaultSources( s => > s.Self()));
Hm, self appears affordable, why can’t the internet browser link BACK to the dotnet run’ ed Kestrel Web Server? It’s all localhost, right? Well, particularly it’s http://localhost not ws:// localhost, or perhaps wss:// localhost (that additional s is for protected) so I require to clearly permit ws: or wss: or both, however just in Advancement.
Perhaps like this (once again, I’m utilizing NWebSpec, however these are simply HTTP Headers so you can actually simply include them if you desire, hardcoded.)
app.UseCsp( choices => > options.DefaultSources( s => > s.Self())
ConnectSources( s => > s.CustomSources(" wss:// localhost:62895")));
However port numbers modification, right? Let’s do simply wss:, just in Advancement. Now, if I’m utilizing both CSPs and WebSockets (ws:, wss:-RRB- in Production, I’ll require to be deliberate about this.
What’s the ethical?
If you begin utilizing CSP Headers to tighten up things up, be mindful and knowledgeable about the headers you require for benefits like Hot Reload in Advancement versus whatever things you might require in Production.
Hope this assists in saving you a long time!
Sponsor: At Rocket Home Loan Â® the work you do around here will be 100% impactful however will not take all your downtime, providing you the ideal work-life balance. Or as we call it, tech/life balance! Discover More.
. . .