Security middleware for XSS and SQL injection protection in ASP.NET Core
$ dotnet add package ShieldXSSShieldXSS is a lightweight, fast, and simple security middleware for ASP.NET Core (.NET 8/10) that provides essential protection against malicious input.
It is designed to be small, dependency-free, and extremely easy to integrate.
dotnet add package ShieldXSS
Install-Package ShieldXSS
builder.Services.AddShieldXSS(options =>
{
options.EnableXSSProtection = true;
options.EnableSQLInjectionProtection = true;
options.EnableRateLimiting = true;
options.MaxAttempts = 5;
options.TimeWindow = TimeSpan.FromMinutes(1);
options.BlockedResponseMessage = "Request blocked for security reasons";
});
Use UseShieldXSS in the ASP.NET pipeline:
app.UseShieldXSS();
web.ConfigureServices(services =>
{
services.AddShieldXSS(options =>
{
options.EnableXSSProtection = true;
options.EnableSQLInjectionProtection = true;
options.EnableRateLimiting = true;
options.MaxAttempts = 20;
options.TimeWindow = TimeSpan.FromMinutes(1);
options.BlockedResponseMessage = "Access denied by ShieldXSS middleware.";
});
});
web.Configure(app =>
{
app.UseMiddleware<ShieldXSSMiddleware>();
});
EnableXSSProtection — enables XSS pattern detection
EnableSQLInjectionProtection — enables SQL injection detection
EnableRateLimiting — enables IP-based throttling
MaxAttempts — requests allowed per window
TimeWindow — rate-limit window duration
BlockedResponseMessage — message on blocked request
CustomXSSPatterns — additional XSS regex rules
CustomSQLPatterns — additional SQL regex rules
XSS examples:
?q=<script>alert(1)</script>
?q=<img src=x onerror=alert(1)>
?q=javascript:alert(1)
?q=document.cookie
SQL injection examples: ?id=1 OR 1=1 ?id=10';DROP TABLE Users-- ?q=UNION SELECT username FROM users ?q=CHAR(65)+CHAR(66)
Rate limiting test (CMD):
for /l %i in (1,1,20) do curl "http://localhost:51179/"
Rate limiting test (PowerShell):
1..20 | % { curl "http://localhost:51179/" }
Expected:
First 5 → 200 OK
After limit → 403 Forbidden
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
Referrer-Policy: strict-origin-when-cross-origin
Content-Security-Policy: default-src 'self'
MIT License — see LICENSE.
Created and maintained by Amelia Keki
https://github.com/ameliagherdan/ShieldXSS/issues