Skip to content

Commit fe06345

Browse files
committed
C#: Add more tests for InsecureDirectObjectReference.ql
1 parent 9a00c75 commit fe06345

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

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

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,67 @@
11
using Microsoft.AspNetCore.Mvc;
22
using Microsoft.AspNetCore.Authorization;
3+
using System.Threading.Tasks;
4+
5+
public class CommentController : Controller
6+
{
7+
private readonly IAuthorizationService _authorizationService;
8+
9+
public CommentController(IAuthorizationService authorizationService)
10+
{
11+
_authorizationService = authorizationService;
12+
}
313

4-
public class CommentController : Controller {
514
// BAD: Any user can access this.
6-
public ActionResult Edit1(int commentId, string text) {
15+
public ActionResult Edit1(int commentId, string text)
16+
{
717
editComment(commentId, text);
818
return View();
919
}
1020

1121
// GOOD: The user's authorization is checked.
12-
public ActionResult Edit2(int commentId, string text) {
13-
if (canEditComment(commentId, User.Identity.Name)){
22+
public ActionResult Edit2(int commentId, string text)
23+
{
24+
if (canEditComment(commentId, User.Identity.Name))
25+
{
1426
editComment(commentId, text);
1527
}
1628
return View();
1729
}
1830

1931
// GOOD: The Authorize attribute is used
2032
[Authorize]
21-
public ActionResult Edit3(int commentId, string text) {
33+
public ActionResult Edit3(int commentId, string text)
34+
{
2235
editComment(commentId, text);
2336
return View();
2437
}
2538

2639
// BAD: The AllowAnonymous attribute overrides the Authorize attribute
2740
[Authorize]
2841
[AllowAnonymous]
29-
public ActionResult Edit4(int commentId, string text) {
42+
public ActionResult Edit4(int commentId, string text)
43+
{
44+
editComment(commentId, text);
45+
return View();
46+
}
47+
48+
// GOOD: An authorization check is made.
49+
public async Task<IActionResult> Edit5(int commentId, string text)
50+
{
51+
var authResult = await _authorizationService.AuthorizeAsync(User, "Comment", "EditPolicy");
52+
53+
if (authResult.Succeeded)
54+
{
55+
editComment(commentId, text);
56+
return View();
57+
}
58+
return Forbid();
59+
}
60+
61+
// GOOD: Only users with the `admin` role can access this method.
62+
[Authorize(Roles = "admin")]
63+
public async Task<IActionResult> Edit6(int commentId, string text)
64+
{
3065
editComment(commentId, text);
3166
return View();
3267
}

csharp/ql/test/query-tests/Security Features/CWE-639/MVCTests/InsecureDirectObjectReference.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
| 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. |
1+
| CommentController.cs:15:25:15:29 | Edit1 | This method may be missing authorization checks for which users can access the resource of the provided ID. |
2+
| CommentController.cs:42:25:42:29 | Edit4 | This method may be missing authorization checks for which users can access the resource of the provided ID. |
33
| MiscTestControllers.cs:26:33:26:40 | EditAnon | This method may be missing authorization checks for which users can access the resource of the provided ID. |
44
| MiscTestControllers.cs:34:34:34:41 | EditAnon | This method may be missing authorization checks for which users can access the resource of the provided ID. |
55
| MiscTestControllers.cs:45:25:45:29 | Edit4 | This method may be missing authorization checks for which users can access the resource of the provided ID. |

0 commit comments

Comments
 (0)