SafeWebCore.Analyzers 1.0.0-preview.1

This is a prerelease version of SafeWebCore.Analyzers.
dotnet add package SafeWebCore.Analyzers --version 1.0.0-preview.1
                    
NuGet\Install-Package SafeWebCore.Analyzers -Version 1.0.0-preview.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SafeWebCore.Analyzers" Version="1.0.0-preview.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SafeWebCore.Analyzers" Version="1.0.0-preview.1" />
                    
Directory.Packages.props
<PackageReference Include="SafeWebCore.Analyzers" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SafeWebCore.Analyzers --version 1.0.0-preview.1
                    
#r "nuget: SafeWebCore.Analyzers, 1.0.0-preview.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package SafeWebCore.Analyzers@1.0.0-preview.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SafeWebCore.Analyzers&version=1.0.0-preview.1&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=SafeWebCore.Analyzers&version=1.0.0-preview.1&prerelease
                    
Install as a Cake Tool

SafeWebCore.Analyzers

SafeWebCore.Analyzers is the v1.5 and newer tooling package for SafeWebCore.

Purpose

This package provides Roslyn analyzers that catch common SafeWebCore integration mistakes at build time, before they become runtime or production issues.

Rules

SWC001 — SafeWebCore middleware is registered but not used

Severity: Warning
Category: SafeWebCore

When reported:
You call one of the registration methods:

  • AddNetSecureHeaders(...)
  • AddNetSecureHeadersStrictAPlus(...)
  • AddNetSecureHeadersFromConfiguration(...)
  • AddNetSecureHeaders*Preset(...)
  • AddNetSecureHeadersForEnvironment(...)
  • AddNetSecureHeadersStrictAPlusForEnvironment(...)

...but UseNetSecureHeaders() is never called on the application pipeline.

Why it matters:
Registration only adds services. Without UseNetSecureHeaders(), the middleware never runs and no security headers are emitted.

Example of the problem:

// In Program.cs or Startup
builder.Services.AddNetSecureHeadersStrictAPlus();

// ... later in the pipeline
app.UseRouting();
// Missing: app.UseNetSecureHeaders();
app.MapControllers();

Fix:

app.UseNetSecureHeaders();   // Add this

The analyzer uses a compilation-wide heuristic. It reports on every registration call when no UseNetSecureHeaders call is found anywhere in the compilation.

SWC002 — CSP is configured in report-only mode

Severity: Warning
Category: SafeWebCore

When reported:
UseCspReportOnly = true is set (either directly or in object initializers / configuration).

Why it matters:
Report-only mode is very useful during development and rollout, but it is frequently left permanently enabled. This means CSP violations are only logged — the policy never actually blocks anything.

Example of the problem:

builder.Services.AddNetSecureHeadersStrictAPlus(opts =>
{
    opts.UseCspReportOnly = true;   // ← Warning SWC002
});

Recommended practice:

  • Use report-only during development/staging (via AddNetSecureHeaders*ForEnvironment helpers)
  • Explicitly set UseCspReportOnly = false when you are ready to enforce
  • Or remove the flag entirely when you want strict enforcement

Current status

  • SWC001 — Detects registration without UseNetSecureHeaders()
  • SWC002 — Detects permanent UseCspReportOnly = true
  • SWC003 — Detects 'unsafe-inline' without a nonce
  • SWC004 — Detects overly broad CSP sources (*, bare https:, unsafe-eval)

Package is additive and opt-in. More rules may be added in future v1.5.x releases.

Installation (when published)

dotnet add package SafeWebCore.Analyzers

The analyzer will be automatically discovered by the .NET SDK.

Compatibility

This package does not change runtime behavior of SafeWebCore. It only provides build-time diagnostics. Existing consumers are unaffected unless they reference this package.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0-preview.1 27 7/25/2026