🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

InputSanitizer

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

InputSanitizer

Package Description

0.0.4
NuGet
Version published
Maintainers
1
Created
Source

InputSanitizer

NuGet version

InputSanitizer is a filter used to clear all illegal strings from the data received by the API or MVC Controller.

It is based on HtmlSanitizer as an HTML-related cleaner, and also adds Regex pattern matching cleanup.

It supports data model binding in DTO mode and also supports dynamic types based on Json.

How To Use

Add InputSanitize to ServiceCollection with policy in Program.cs

var policy = new InputSanitizerPolicy()
            {
                Name = "Default",
                AllowedTags = new HashSet<string>(HtmlSanitizerDefaults.AllowedTags, StringComparer.OrdinalIgnoreCase),
                AllowedSchemes = new HashSet<string>(HtmlSanitizerDefaults.AllowedSchemes, StringComparer.OrdinalIgnoreCase),
                AllowedAttributes = new HashSet<string>(HtmlSanitizerDefaults.AllowedAttributes, StringComparer.OrdinalIgnoreCase),
                UriAttributes = new HashSet<string>(HtmlSanitizerDefaults.UriAttributes, StringComparer.OrdinalIgnoreCase),
                AllowedCssProperties = new HashSet<string>(HtmlSanitizerDefaults.AllowedCssProperties, StringComparer.OrdinalIgnoreCase),
                AllowedAtRules = new HashSet<CssRuleType>(HtmlSanitizerDefaults.AllowedAtRules),
                AllowedCssClasses = new HashSet<string>(HtmlSanitizerDefaults.AllowedClasses),
                ExceptionMessage = "What are you doing?",
                InvalidInputBehaviour = InvalidInputBehaviour.SetModelState
            }
            
builder.Services.AddInputSanitizer(InputSanitizerPolicies.Policies);

Add To the Controller

    [ApiController]
    [Route("[controller]")]
    [SanitizeAllInputFilter(PolicyName = "Default")]  // this line HERE
    public class WeatherForecastController : ControllerBase
    {
        private readonly ILogger<WeatherForecastController> _logger;

        public WeatherForecastController(ILogger<WeatherForecastController> logger)
        {
            _logger = logger;
        }

        [HttpPost(Name = "PostWeatherForecast")]
        public IActionResult Post(DTOModel data)    // OR public IActionResult Post(dynamic data)
        {
            if (!ModelState.IsValid)
            {
                return UnprocessableEntity(ModelState);
            }
            return Ok(data);
        }
    }

Keywords

DTO

FAQs

Package last updated on 16 Nov 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts