Improve bit Boilerplate response security headers (#12014)#12015
Improve bit Boilerplate response security headers (#12014)#12015yasmoradi merged 2 commits intobitfoundation:developfrom
Conversation
|
Important Review skippedAuto incremental reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the
WalkthroughThe PR consolidates security header middleware configuration by centralizing NWebsec.AspNetCore.Middleware to the shared library, introducing a new Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Fix all issues with AI agents
In
`@src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Shared/Boilerplate.Server.Shared.csproj`:
- Line 13: Remove the unmaintained PackageReference to
NWebsec.AspNetCore.Middleware from Boilerplate.Server.Shared.csproj and replace
its usage with a supported approach: either add a maintained security-header
package (e.g., a modern ASP.NET Core header middleware) or implement a small
custom middleware in Startup/Program (AddSecurityHeadersMiddleware or similar)
that sets the required headers; update any Startup/Program.cs references
currently relying on NWebsec.AspNetCore.Middleware to use the new package or the
custom AddSecurityHeadersMiddleware and remove the obsolete PackageReference
entry from the .csproj.
In
`@src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Shared/Infrastructure/Extensions/WebApplicationExtensions.cs`:
- Around line 86-135: The CSP set in UseSecurityHeaders is missing a script-src
directive which breaks Blazor; update the Content-Security-Policy header (set on
context.Response.Headers.Append("Content-Security-Policy", ...)) to include a
script-src directive—for Blazor Server use "script-src 'self'" and for Blazor
WASM include "script-src 'self' 'wasm-unsafe-eval'"; also consider adding
style-src and img-src directives for styles/images; replace Headers.Append with
a non-duplicating approach (e.g., check/replace or TryAdd-like logic) so calling
UseSecurityHeaders multiple times won’t duplicate headers.
...rplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Shared/Boilerplate.Server.Shared.csproj
Show resolved
Hide resolved
...e/src/Server/Boilerplate.Server.Shared/Infrastructure/Extensions/WebApplicationExtensions.cs
Show resolved
Hide resolved
There was a problem hiding this comment.
Pull request overview
Improves the Boilerplate template’s HTTP response security headers by centralizing header configuration and reducing duplicated middleware setup, while also adjusting caching-related behavior for antiforgery.
Changes:
- Introduces a shared
UseSecurityHeaders()extension and replaces per-project NWebsec header middleware calls with it. - Moves
NWebsec.AspNetCore.Middlewarepackage reference intoBoilerplate.Server.Sharedfor shared consumption. - Updates response-caching signaling (
HttpContext.Items) and stream prerendering suppression logic, and makes antiforgery “no-op” conditional based on caching settings.
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Web/Program.Services.cs | Conditionally registers NoOpAntiforgery based on response caching settings. |
| src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Web/Program.Middlewares.cs | Switches to app.UseSecurityHeaders() in non-development. |
| src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Web/Infrastructure/Services/WebServerExceptionHandler.cs | Updates internal comment to reflect renamed caching flag key. |
| src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Web/Infrastructure/Services/NoOpAntiforgery.cs | Expands documentation explaining antiforgery disabling for CDN caching scenarios. |
| src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Web/Infrastructure/Extensions/HttpRequestExtensions.cs | Uses shared-cache flag to suppress streaming prerendering when caching is enabled. |
| src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Web/Infrastructure/Extensions/HttpContextExtensions.cs | Adds IsSharedCacheEnabled() helper for the new cache-flag key. |
| src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Web/Boilerplate.Server.Web.csproj | Removes direct NWebsec package reference (now shared). |
| src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Shared/Infrastructure/Services/AppResponseCachePolicy.cs | Renames/rewrites the HttpContext cache flag to represent “shared cache enabled”. |
| src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Shared/Infrastructure/Extensions/WebApplicationExtensions.cs | Adds UseSecurityHeaders() and configures additional headers (Permissions-Policy, CORP, CSP). |
| src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Shared/Boilerplate.Server.Shared.csproj | Adds NWebsec package reference to support shared security header middleware. |
| src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Program.Middlewares.cs | Switches to app.UseSecurityHeaders() in non-development. |
| src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Boilerplate.Server.Api.csproj | Removes direct NWebsec package reference (now shared). |
src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Web/Program.Services.cs
Outdated
Show resolved
Hide resolved
...e/src/Server/Boilerplate.Server.Shared/Infrastructure/Extensions/WebApplicationExtensions.cs
Show resolved
Hide resolved
...plate/src/Server/Boilerplate.Server.Shared/Infrastructure/Services/AppResponseCachePolicy.cs
Show resolved
Hide resolved
...Bit.Boilerplate/src/Server/Boilerplate.Server.Web/Infrastructure/Services/NoOpAntiforgery.cs
Outdated
Show resolved
Hide resolved
...erplate/src/Server/Boilerplate.Server.Web/Infrastructure/Extensions/HttpContextExtensions.cs
Outdated
Show resolved
Hide resolved
|



closes #12014
Summary by CodeRabbit
Refactor
Documentation
✏️ Tip: You can customize this high-level summary in your review settings.