Skip to content

Commit 6a95ed6

Browse files
Add test cases for authorization from attributes
1 parent ac45050 commit 6a95ed6

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

csharp/ql/test/query-tests/Security Features/CWE-639/MVCTests/CommentController.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@ public ActionResult Edit2(int commentId, string text) {
1616
return View();
1717
}
1818

19+
// GOOD: The Authorize attribute is used
20+
[Authorize]
21+
public ActionResult Edit3(int commentId, string text) {
22+
editComment(commentId, text);
23+
return View();
24+
}
25+
26+
// BAD: The AllowAnonymous attribute overrides the Authorize attribute
27+
[Authorize]
28+
[AllowAnonymous]
29+
public ActionResult Edit4(int commentId, string text) {
30+
editComment(commentId, text);
31+
return View();
32+
}
33+
1934
void editComment(int commentId, string text) { }
2035

2136
bool canEditComment(int commentId, string userName) { return false; }
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
| CommentController.cs:6:25:6:29 | Edit1 | This method may be missing authorization checks for which users can access the resource of the provided ID. |
2+
| CommentController.cs:29:25:29:29 | Edit4 | This method may be missing authorization checks for which users can access the resource of the provided ID. |
3+
| ProfileController.cs:14:25:14:29 | Edit2 | This method may be missing authorization checks for which users can access the resource of the provided ID. |
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using Microsoft.AspNetCore.Authorization;
3+
4+
[Authorize]
5+
public class ProfileController : Controller {
6+
// GOOD: The Authorize attribute of the class restricts access to this method.
7+
public ActionResult Edit1(int profileId, string text) {
8+
editProfileName(profileId, text);
9+
return View();
10+
}
11+
12+
// BAD: The AllowAnonymous attribute therides the Authorize attribute on the class.
13+
[AllowAnonymous]
14+
public ActionResult Edit2(int profileId, string text) {
15+
editProfileName(profileId, text);
16+
return View();
17+
}
18+
19+
void editProfileName(int profileId, string text) { }
20+
}

0 commit comments

Comments
 (0)