Skip to main content

Lesson 3 of 8

OWASP Top 10 for ASP.NET Core

The OWASP Top 10 is the authoritative list of critical web application security risks. Several apply directly to ASP.NET Core APIs: injection (SQL, command), broken access control, security misconfiguration, insecure deserialization, and SSRF. Each has specific mitigation patterns in .NET.

30 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

OWASP Mitigations in ASP.NET Core

OWASP Mitigations in ASP.NET Core
// ━━ A01: Broken Access Control - already covered in Module 8, topic 2 ━━
// Resource-based authorization, policy requirements

// ━━ A03: Injection - SQL Injection ━━
// ALWAYS use parameterized queries - EF Core does this by default
// NEVER concatenate user input into SQL

// BAD - SQL injection vulnerability:
// var sql = $"SELECT * FROM Products WHERE Name = '{userInput}'";
// db.Database.ExecuteSqlRaw(sql); // EXPLOIT: userInput = "'; DROP TABLE Products; --"

// GOOD - parameterized:
var safe = await dbContext.Products
    .Where(p => p.Name == userInput)  // EF Core generates parameterized SQL
    .ToListAsync();

// Also safe - explicit parameter:
await dbContext.Database.ExecuteSqlRawAsync(
    "SELECT * FROM Products WHERE Name = {0}", userInput); // {0} = SQL parameter

// ━━ A03: Injection - OS Command Injection ━━
// NEVER pass user input to shell commands
// BAD:
// Process.Start("bash", $"-c ffmpeg -i {userInput} output.mp4"); // shell injection!

// GOOD - use ProcessStartInfo without shell:
var psi = new System.Diagnostics.ProcessStartInfo
{
    FileName = "ffmpeg",
    ArgumentList = { "-i", inputFile, "output.mp4" }, // each arg separate - no shell parsing
    UseShellExecute = false,
    RedirectStandardOutput = true,
};

// ━━ A04: Insecure Design - Mass Assignment ━━
// BAD: binding entire model from request - attacker sends { "IsAdmin": true }
// [HttpPost] public IActionResult Create([FromBody] ApplicationUser user) { }

// GOOD: use DTOs - only map what clients are allowed to set
// [HttpPost] public IActionResult Create([FromBody] CreateUserRequest request) { }

// ━━ A05: Security Misconfiguration ━━
// Helmet-equivalent for ASP.NET Core - security headers middleware
app.Use(async (ctx, next) =>
{
    ctx.Response.Headers["X-Content-Type-Options"] = "nosniff";
    ctx.Response.Headers["X-Frame-Options"] = "DENY";
    ctx.Response.Headers["X-XSS-Protection"] = "1; mode=block";
    ctx.Response.Headers["Referrer-Policy"] = "strict-origin-when-cross-origin";
    ctx.Response.Headers["Permissions-Policy"] = "camera=(), microphone=(), geolocation=()";
    ctx.Response.Headers["Content-Security-Policy"] =
        "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self'";
    await next();
});

// ━━ A07: Identification & Authentication - already M7 ━━
// Password hashing, lockout, 2FA, JWT short expiry, refresh rotation

// ━━ A10: SSRF - Server-Side Request Forgery ━━
// Attacker: POST /api/webhooks { "url": "http://169.254.169.254/latest/meta-data/" }
// Server fetches AWS metadata credentials on attacker's behalf

// Mitigation: validate URLs before fetching
public static bool IsAllowedUrl(string url)
{
    if (!Uri.TryCreate(url, UriKind.Absolute, out var uri)) return false;
    if (uri.Scheme != "https") return false; // HTTPS only

    // Block private IP ranges
    var host = uri.Host;
    if (host == "localhost" || host == "127.0.0.1" || host.StartsWith("10.") ||
        host.StartsWith("192.168.") || host.StartsWith("172.16.") ||
        host == "169.254.169.254") // AWS metadata endpoint
        return false;

    // Allowlist specific domains
    var allowedDomains = new[] { "api.github.com", "hooks.slack.com" };
    return allowedDomains.Any(d => host.EndsWith(d));
}

Microsoft.EntityFrameworkCore.DbContext dbContext = null!;
IApplicationBuilder app = null!;
string userInput = "", inputFile = "";

Tip

Tip

Practice OWASP Top 10 for ASPNET Core in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

.NET provides a unified platform for building various application types

Practice Task

Note

Practice Task - (1) Write a working example of OWASP Top 10 for ASPNET Core from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with OWASP Top 10 for ASPNET Core is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready dotnet code.

Topics in This Module

0/8 done