// Copyright (c) André N. Klingsheim. See License.txt in the project root for license information. using System; using NWebsec.Core.HttpHeaders.Configuration.Validation; using NWebsec.Middleware; using NWebsec.Middleware.Middleware; // ReSharper disable once CheckNamespace namespace Microsoft.AspNetCore.Builder { public static class ApplicationBuilderExtensions { /// /// Adds a middleware to the pipeline that validates redirects. /// /// The to which the middleware is added. /// The supplied in the app parameter. public static IApplicationBuilder UseRedirectValidation(this IApplicationBuilder app) { if (app == null) throw new ArgumentNullException(nameof(app)); var options = new RedirectValidationOptions(); return app.UseMiddleware(options); } /// /// Adds a middleware to the ASP.NET pipeline that validates redirects. /// /// The to which the middleware is added. /// An that configures the options for the middleware. /// The supplied in the app parameter. public static IApplicationBuilder UseRedirectValidation(this IApplicationBuilder app, Action configurer) { if (app == null) throw new ArgumentNullException(nameof(app)); if (configurer == null) throw new ArgumentNullException(nameof(configurer)); var options = new RedirectValidationOptions(); configurer(options); return app.UseMiddleware(options); } /// /// Adds a middleware to the ASP.NET pipeline that sets the Strict-Transport-Security header. /// /// The to which the middleware is added. /// An that configures the options for the middleware. /// The supplied in the app parameter. public static IApplicationBuilder UseHsts(this IApplicationBuilder app, Action configurer) { if (app == null) throw new ArgumentNullException(nameof(app)); if (configurer == null) throw new ArgumentNullException(nameof(configurer)); var options = new HstsOptions(); configurer(options); new HstsConfigurationValidator().Validate(options); return app.UseMiddleware(options); } /// /// Adds a middleware to the ASP.NET pipeline that sets the Public-Key-Pins header. /// /// The to which the middleware is added. /// An that configures the options for the middleware. /// The supplied in the app parameter. public static IApplicationBuilder UseHpkp(this IApplicationBuilder app, Action configurer) { if (app == null) throw new ArgumentNullException(nameof(app)); if (configurer == null) throw new ArgumentNullException(nameof(configurer)); var options = new HpkpOptions(); configurer(options); new HpkpConfigurationValidator().ValidateNumberOfPins(options.Config); return app.UseMiddleware(options, false); } /// /// Adds a middleware to the ASP.NET pipeline that sets the Public-Key-Pins-Report-Only header. /// /// The to which the middleware is added. /// An that configures the options for the middleware. /// The supplied in the app parameter. public static IApplicationBuilder UseHpkpReportOnly(this IApplicationBuilder app, Action configurer) { if (app == null) throw new ArgumentNullException(nameof(app)); if (configurer == null) throw new ArgumentNullException(nameof(configurer)); var options = new HpkpOptions(); configurer(options); new HpkpConfigurationValidator().ValidateNumberOfPins(options.Config); return app.UseMiddleware(options, true); } /// /// Adds a middleware to the ASP.NET pipeline that sets the X-Content-Type-Options header. /// /// The to which the middleware is added. /// The supplied in the app parameter. public static IApplicationBuilder UseXContentTypeOptions(this IApplicationBuilder app) { if (app == null) throw new ArgumentNullException(nameof(app)); return app.UseMiddleware(); } /// /// Adds a middleware to the ASP.NET pipeline that sets the X-Download-Options header. /// /// The to which the middleware is added. /// The supplied in the app parameter. public static IApplicationBuilder UseXDownloadOptions(this IApplicationBuilder app) { if (app == null) throw new ArgumentNullException(nameof(app)); return app.UseMiddleware(); } /// /// Adds a middleware to the ASP.NET pipeline that sets the X-Frame-Options header. /// /// The to which the middleware is added. /// An that configures the options for the middleware. /// The supplied in the app parameter. public static IApplicationBuilder UseXfo(this IApplicationBuilder app, Action configurer) { if (app == null) throw new ArgumentNullException(nameof(app)); if (configurer == null) throw new ArgumentNullException(nameof(configurer)); var options = new XFrameOptions(); configurer(options); return app.UseMiddleware(options); } /// /// Adds a middleware to the ASP.NET pipeline that sets the X-Robots-Tag header. /// /// The to which the middleware is added. /// An that configures the options for the middleware. /// The supplied in the app parameter. public static IApplicationBuilder UseXRobotsTag(this IApplicationBuilder app, Action configurer) { if (app == null) throw new ArgumentNullException(nameof(app)); if (configurer == null) throw new ArgumentNullException(nameof(configurer)); var options = new XRobotsTagOptions(); configurer(options); return app.UseMiddleware(options); } /// /// Adds a middleware to the ASP.NET pipeline that sets the X-Xss-Protection header. /// /// The to which the middleware is added. /// An that configures the options for the middleware. /// The supplied in the app parameter. public static IApplicationBuilder UseXXssProtection(this IApplicationBuilder app, Action configurer) { if (app == null) throw new ArgumentNullException(nameof(app)); if (configurer == null) throw new ArgumentNullException(nameof(configurer)); var options = new XXssProtectionOptions(); configurer(options); return app.UseMiddleware(options); } /// /// Adds a middleware to the ASP.NET pipeline that sets the Content-Security-Policy header. /// /// The to which the middleware is added. /// An that configures the options for the middleware. /// The supplied in the app parameter. public static IApplicationBuilder UseCsp(this IApplicationBuilder app, Action configurer) { if (app == null) throw new ArgumentNullException(nameof(app)); if (configurer == null) throw new ArgumentNullException(nameof(configurer)); var options = new CspOptions(); configurer(options); return app.UseMiddleware(options, false); //Last param indicates it's not reportOnly. } /// /// Adds a middleware to the ASP.NET pipeline that sets the Content-Security-Policy-Report-Only header. /// /// The to which the middleware is added. /// An that configures the options for the middleware. /// The supplied in the app parameter. public static IApplicationBuilder UseCspReportOnly(this IApplicationBuilder app, Action configurer) { if (app == null) throw new ArgumentNullException(nameof(app)); if (configurer == null) throw new ArgumentNullException(nameof(configurer)); var options = new CspOptions(); configurer(options); return app.UseMiddleware(options, true); //Last param indicates it's reportOnly. } } }