Skip to content

Commit 00ccd64

Browse files
committed
C#: Add global ASP.NET Core - enable validation for all action methods.
1 parent f70e0b5 commit 00ccd64

4 files changed

Lines changed: 43 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.AspNetCore.Mvc.ViewFeatures;
4+
using Microsoft.AspNetCore.Routing;
5+
using Microsoft.Extensions.DependencyInjection;
6+
7+
public class HomeController : Controller
8+
{
9+
// GOOD: This is validated by the global filter.
10+
[HttpPost]
11+
public ActionResult Login()
12+
{
13+
return View();
14+
}
15+
16+
// GOOD: Antiforgery token is validated explicitly.
17+
[HttpPost]
18+
[ValidateAntiForgeryToken]
19+
public ActionResult UpdateDetails()
20+
{
21+
return View();
22+
}
23+
}
24+
25+
public class Program
26+
{
27+
public static void Main(string[] args)
28+
{
29+
var builder = WebApplication.CreateBuilder(args);
30+
31+
// Register MVC controllers and Razor views.
32+
// The global filter automatically validates antiforgery tokens
33+
// for unsafe HTTP methods such as POST, PUT, PATCH, and DELETE.
34+
builder.Services.AddControllersWithViews(options =>
35+
{
36+
options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute());
37+
});
38+
}
39+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| MissingAntiForgeryTokenValidation.cs:11:25:11:29 | Login | Method 'Login' handles a POST request without performing CSRF token validation. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
query: Security Features/CWE-352/MissingAntiForgeryTokenValidation.ql
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
semmle-extractor-options: /nostdlib /noconfig
2+
semmle-extractor-options: --load-sources-from-project:${testdir}/../../../../resources/stubs/_frameworks/Microsoft.AspNetCore.App/Microsoft.AspNetCore.App.csproj

0 commit comments

Comments
 (0)