All notable changes to SafeWebCore will be documented in this file.
The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.
- Razor nonce TagHelpers — New
CspScriptNonceTagHelperandCspStyleNonceTagHelperautomatically inject request nonces into<script>and<style>tags whennonceis missing. - Path-based policy overrides — New
PathPolicyOptionssupport inNetSecureHeadersOptionsallows route-prefix policy selection with longest-prefix match behavior. - Startup option validation — New
NetSecureHeadersOptionsValidatorvalidates global and path policy settings usingValidateOnStart().
- CSP report-only support — Middleware now emits
Content-Security-Policy-Report-OnlywhenUseCspReportOnlyis enabled.
1.1.0 — 2025-06-28
HttpContext.GetCspNonce()extension method — Discoverable way to retrieve the per-request CSP nonce without magic strings. Available viausing SafeWebCore.Extensions;.var nonce = HttpContext.GetCspNonce();
NonceService.TryWriteNonce(Span<char>, out int)— Zero-allocation overload that writes the nonce directly into a caller-provided buffer. Ideal for high-throughput scenarios or writing directly into response buffers.Span<char> buffer = stackalloc char[NonceService.NonceLength]; if (nonceService.TryWriteNonce(buffer, out int written)) { // Use buffer[..written] — no heap allocation }
NonceService.NonceLengthconstant — Public constant (44) for the length of a generated nonce string. Eliminates magic numbers when pre-allocating buffers.
- CSP template is now pre-built once in the middleware constructor instead of being rebuilt on every request. Only the lightweight
string.Replace("{nonce}", nonce)runs per-request. This significantly reduces per-request allocations. CspOptions.Build()usesStringBuilder— ReplacedList<string>+ interpolated string allocations +string.Joinwith a pre-sizedStringBuilder(512). Eliminates ~20 intermediate string allocations per call.CspReportMiddlewarenow passesCancellationToken—ReadToEndAsyncusescontext.RequestAbortedfor proper cancellation when clients disconnect.CspNonceAttributeuses C# pattern matching — Collapsed nested conditionals into a singleis string { Length: > 0 } noncepattern expression.- Preset application extracted to
ApplyPresethelper — InternalNetSecureHeadersOptions.ApplyPreset()method consolidates the 20+ line property copy into a single reusable call. Adding new options in the future requires updating only one place.
- ✅ 100% backwards compatible with v1.0.0
- All existing public APIs (
AddNetSecureHeadersStrictAPlus,UseNetSecureHeaders,CspBuilder,[CspNonce], CSP reporting) remain unchanged - No breaking changes to method signatures, behavior, or configuration
- All 40 existing tests pass without modification
1.0.0 — 2025-06-15
- Strict A+ preset —
AddNetSecureHeadersStrictAPlus()for one-line A+ configuration on securityheaders.com - Fluent
CspBuilderwith full CSP Level 3 (W3C Recommendation) directive coverage - CSP Level 4 support — Trusted Types (
require-trusted-types-for,trusted-types),fenced-frame-src - Per-request cryptographic nonce generation with
stackalloc+RandomNumberGenerator(zero heap allocations) [CspNonce]action filter attribute for Razor view nonce injection- Built-in CSP violation reporting middleware (
/csp-reportendpoint) - Extensible
IHeaderPolicyinterface for custom header policies - Full security header suite: HSTS, X-Frame-Options, X-Content-Type-Options, Referrer-Policy, Permissions-Policy, COEP, COOP, CORP, X-DNS-Prefetch-Control, X-Permitted-Cross-Domain-Policies
- Server header removal
- Comprehensive documentation and test suite