Skip to content

Commit 8fdec4f

Browse files
Add documentation
1 parent 12bb418 commit 8fdec4f

File tree

5 files changed

+95
-1
lines changed

5 files changed

+95
-1
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class ProfileController : Controller {
2+
3+
// BAD: No authorization is used.
4+
public ActionResult Edit(int id) {
5+
...
6+
}
7+
8+
// GOOD: The `Authorize` tag is used.
9+
[Authorize]
10+
public ActionResult Delete(int id) {
11+
...
12+
}
13+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
6+
<overview>
7+
<p>
8+
Sensitive actions, such as editing or deleting content, or accessing admin pages, should have authentication checks
9+
to ensure that they cannot be used by arbitrary users.
10+
</p>
11+
12+
</overview>
13+
<recommendation>
14+
15+
<p>
16+
Ensure that proper authorization checks are made for sensitive actions.
17+
For WebForms applications, the <code>authorazation</code> tag in <code>Web.config</code> XML files
18+
can be used to implement access control. The <code>System.Web.UI.Page.User</code> property can also be
19+
used to verify a user's roles.
20+
For MVC applications, the <code>Authorize</code> attribute can be used to require authorization on specific
21+
action methods.
22+
</p>
23+
24+
</recommendation>
25+
<example>
26+
27+
<p>
28+
In the following WebForms example, the case marked BAD has no authorization checks; whereas the
29+
case marked GOOD uses <code>User.IsInRole</code> to check for the user's role.
30+
</p>
31+
32+
<sample src="WebForms.cs" />
33+
34+
<p>
35+
The following <code>Web.config</code> file uses the <code>authorization</code> tag to deny access to anonymous users,
36+
in a <location> tag to have it apply to a specific path.
37+
</p>
38+
39+
<sample src="Web.config" />
40+
41+
<p>
42+
In the following MVC example, the case marked BAD has no authorization
43+
checks; whereas the case marked GOOD uses the <code>Authorize</code> attribute.
44+
</p>
45+
46+
<sample src="MVC.cs" />
47+
48+
</example>
49+
<references>
50+
<li><code>Page.User</code> Property - <a href="https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.page.user?view=netframework-4.8.1#system-web-ui-page-user">Microsoft Learn</a></li>
51+
<li>Control authorization permissions in an ASP.NET application - <a href="https://learn.microsoft.com/en-us/troubleshoot/developer/webapps/aspnet/www-authentication-authorization/authorization-permissions">Microsoft Learn</a></li>
52+
<li>Simple authorization in ASP.NET Core - <a href="https://learn.microsoft.com/en-us/aspnet/core/security/authorization/simple?view=aspnetcore-7.0">Microsoft Learn</a></li>
53+
</references>
54+
</qhelp>

csharp/ql/src/Security Features/CWE-285/MissingAccessControl.ql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
/**
22
* @name Missing function level access control
3-
* @description ... TODO
3+
* @description Sensitive actions should have authorization checks to prevent them from being used by arbitrary users.
44
* @kind problem
55
* @problem.severity warning
66
* @security-severity 7.5
77
* @precision medium
88
* @id cs/web/missing-function-level-access-control
99
* @tags security
1010
* external/cwe/cwe-285
11+
* external/cwe/cwe-284
12+
* external/cwe/cwe-862
1113
*/
1214

1315
import csharp
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0"?>
2+
3+
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
4+
<location path="User/Profile">
5+
<system.web>
6+
<authorization>
7+
<deny users="?" />
8+
</authorization>
9+
</system.web>
10+
</location>
11+
</configuration>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class ProfilePage : System.Web.UI.Page {
2+
// BAD: No authorization is used
3+
protected void btn1_Edit_Click(object sender, EventArgs e) {
4+
...
5+
}
6+
7+
// GOOD: `User.IsInRole` checks the current user's role.
8+
protected void btn2_Delete_Click(object sender, EventArgs e) {
9+
if (!User.IsInRole("admin")) {
10+
return;
11+
}
12+
...
13+
}
14+
}

0 commit comments

Comments
 (0)