Skip to content

Commit a022893

Browse files
Add additional example to qhelp + additional resource
1 parent 86abd33 commit a022893

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

csharp/ql/src/Security Features/CWE-639/InsecureDirectObjectReference.qhelp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@ Ensure that the current user is authorized to access the resource of the provide
1818
<p>In the following example, in the case marked BAD, there is no authorization check, so any user is able to edit any comment.
1919
In the case marked GOOD, there is a check that the current usr matches the author of the comment.</p>
2020
<sample src="WebFormsExample.cs" />
21+
<p>The following example shows a similar case for the ASP.NET Core framweork.</p>
22+
<sample src="MVCExample.cs" />
2123

2224

2325
</example>
2426
<references>
2527

2628
<li>OWASP - <a href="https://wiki.owasp.org/index.php/Top_10_2013-A4-Insecure_Direct_Object_References">Insecure Direct Object Refrences</a>.</li>
2729
<li>OWASP - <a href="https://owasp.org/www-project-web-security-testing-guide/latest/4-Web_Application_Security_Testing/05-Authorization_Testing/04-Testing_for_Insecure_Direct_Object_References">Testing for Insecure Direct Object References</a>.</li>
30+
<li>Microsoft Learn = <a href="https://learn.microsoft.com/en-us/aspnet/core/security/authorization/resourcebased?view=aspnetcore-7.0">Resource-based authorization in ASP.NET Core</a>.</li>
2831

2932
</references>
3033
</qhelp>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
public class CommentController : Controller {
2+
private readonly IAuthorizationService _authorizationService;
3+
private readonly IDocumentRepository _commentRepository;
4+
5+
public CommentController(IAuthorizationService authorizationService,
6+
ICommentRepository commentRepository)
7+
{
8+
_authorizationService = authorizationService;
9+
_commentRepository = commentRepository;
10+
}
11+
12+
// BAD: Any user can access this.
13+
public async Task<IActionResult> Edit1(int commentId, string text) {
14+
Comment comment = _commentRepository.Find(commentId);
15+
16+
comment.Text = text;
17+
18+
return View();
19+
}
20+
21+
// GOOD: An authorization check is made.
22+
public async Task<IActionResult> Edit2(int commentId, string text) {
23+
Comment comment = _commentRepository.Find(commentId);
24+
25+
var authResult = await _authorizationService.AuthorizeAsync(User, Comment, "EditPolicy");
26+
27+
if (authResult.Succeeded) {
28+
comment.Text = text;
29+
return View();
30+
}
31+
else {
32+
return ForbidResult();
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)