Guidance for AI Agents when working in this repository.
NetEscapades.AspNetCore.SecurityHeaders is a NuGet package providing ASP.NET Core middleware for adding security-related HTTP response headers (CSP, HSTS, Permissions-Policy, X-Frame-Options, Cross-Origin-*, etc.). Published to NuGet as two packages:
NetEscapades.AspNetCore.SecurityHeaders— the main middlewareNetEscapades.AspNetCore.SecurityHeaders.TagHelpers— Razor tag helpers for CSP nonces and inline script/style hashes
src/
NetEscapades.AspNetCore.SecurityHeaders/ Main library (middleware, policies, header builders)
Headers/ Header types (CSP, HSTS, Permissions-Policy, ...)
Infrastructure/ Middleware plumbing, CustomHeaderService
NetEscapades.AspNetCore.SecurityHeaders.TagHelpers/ Razor tag helpers (Nonce, Hash, AttributeHash)
NetEscapades.AspNetCore.SecurityHeaders.Analyzers/ Roslyn analyzers shipped with the main package
SourceGenerator/ Incremental source generator for CSP builders
test/
*.Test/ xUnit test projects
RazorWebSite/, SecurityHeadersMiddlewareWebSite/ Test web apps used via TestServer
build/ Nuke build definition (_build.csproj, Build.cs)
The Analyzers and SourceGenerator projects are referenced as OutputItemType="Analyzer" / PrivateAssets="All" — they ship inside the main package, not as separate NuGet dependencies.
Generated source files are emitted to src/NetEscapades.AspNetCore.SecurityHeaders/Generated/<tfm>/ (EmitCompilerGeneratedFiles=true). They are excluded from compilation but visible in the IDE — do not edit them; change the generator instead.
- SDK pinned in
global.json: .NET 10.0.101 (no prerelease). Install matching SDK before building. - Use the Nuke wrappers, not raw
dotnet:./build.sh(Linux/macOS) orbuild.cmd(Windows) — default target runsTest+Pack- Targets:
Clean,Restore,Compile,Test,Pack,GenerateSbom,PushToNuGet - Example:
./build.sh Testor./build.sh Compile --configuration Release
- Ad-hoc
dotnet test NetEscapades.AspNetCore.SecurityHeaders.slnalso works for quick iteration.
- Main library:
netcoreapp3.1only (single-target — do not add more TFMs without discussion; it affects analyzer/source-generator packaging). - Test projects:
net6.0;net8.0;net9.0;net10.0(plusnetcoreapp3.1on Windows only — Linux CI skips it due to missing libssl).
TreatWarningsAsErrors=trueon the main library — any new warning fails the build.NullableandImplicitUsingsare enabled on the main library.GenerateDocumentationFile=true— public APIs must have XML doc comments or StyleCop/CS warnings will fail the build.- StyleCop.Analyzers is active with a custom ruleset (
NetEscapades.AspNetCore.SecurityHeaders.ruleset) andstylecop.json. LangVersion=latest.
- New header type →
src/NetEscapades.AspNetCore.SecurityHeaders/Headers/with a*Header.cs+*HeaderExtensions.cspair; register via extensions onHeaderPolicyCollection. Include an MDN reference on the header class, builder, and extension class — use/// <seealso href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/<header-name>"/>(matches the existing convention inCross-Origin-Opener-PolicyandClear-Site-Databuilders). - New CSP directive → usually needs changes in
src/SourceGenerator/(the directive builders are generated). CheckContentSecurityPolicyGenerator.csandSourceGenerationHelper.cs. - Deprecation / insecure API warnings →
src/NetEscapades.AspNetCore.SecurityHeaders.Analyzers/and the[Deprecated]/[InsecureApi]attributes inHelpers/. - Tests → prefer adding to
NetEscapades.AspNetCore.SecurityHeaders.TestusingTestServeragainst the sample web apps rather than unit-testing middleware internals directly.
- Version is currently hardcoded in
build/Build.cs(readonly string Version = "1.3.1") — bump there, not in csproj files. ReleaseNotes.mdis embedded intoPackageReleaseNotesviaDirectory.Build.props. UpdateCHANGELOG.mdandReleaseNotes.mdon version bumps.- Publishing is gated on a git tag + CI + Windows (
PushToNuGettarget). Releases include provenance attestations and CycloneDX SBOMs — don't bypass the CI pipeline for a release.