Skip to content

Commit bd69b96

Browse files
authored
Merge pull request #17273 from michaelnebel/csharp/sqlinject
C#: ASP.NET Controller is allowed to be abstract.
2 parents 43f54db + 7049499 commit bd69b96

File tree

9 files changed

+111
-65
lines changed

9 files changed

+111
-65
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* Parameters of public methods in abstract controller-like classes are now considered remote flow sources.

csharp/ql/lib/semmle/code/csharp/frameworks/microsoft/AspNetCore.qll

-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,6 @@ class MicrosoftAspNetCoreMvcController extends Class {
205205
)
206206
) and
207207
this.isPublic() and
208-
(not this.isAbstract() or this instanceof MicrosoftAspNetCoreMvcControllerBaseClass) and
209208
not this instanceof Generic and
210209
(
211210
this.getABaseType*() instanceof MicrosoftAspNetCoreMvcControllerBaseClass

csharp/ql/test/library-tests/dataflow/flowsources/aspremote/AspRemoteFlowSource.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,9 @@ public void M1(string[] args)
5858
app.Run();
5959
}
6060
}
61-
}
61+
62+
public abstract class AbstractTestController : Controller
63+
{
64+
public void MyActionMethod(string param) { }
65+
}
66+
}

csharp/ql/test/library-tests/dataflow/flowsources/aspremote/aspRemoteFlowSource.expected

+1
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ remoteFlowSources
1212
| AspRemoteFlowSource.cs:53:63:53:73 | mapPutParam |
1313
| AspRemoteFlowSource.cs:54:69:54:82 | mapDeleteParam |
1414
| AspRemoteFlowSource.cs:56:41:56:44 | item |
15+
| AspRemoteFlowSource.cs:64:43:64:47 | param |

csharp/ql/test/library-tests/frameworks/microsoft/AspNetCore.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,17 @@ public string Index()
5656
}
5757
}
5858

59-
// is not public
60-
internal class NotHomeController : Controller
59+
// is abstract
60+
public abstract class HomeController6 : Controller
6161
{
6262
public string Index()
6363
{
6464
return "This is Home Controller";
6565
}
6666
}
6767

68-
// is abstract
69-
public abstract class NotHomeController2 : Controller
68+
// is not public
69+
internal class NotHomeController : Controller
7070
{
7171
public string Index()
7272
{
@@ -75,7 +75,7 @@ public string Index()
7575
}
7676

7777
// contains generic parameters
78-
public class NotHomeController3<T> : Controller
78+
public class NotHomeController2<T> : Controller
7979
{
8080
public string Index()
8181
{
@@ -85,7 +85,7 @@ public string Index()
8585

8686
// has [NonController] attribute
8787
[NonController]
88-
public class NotHomeController4 : Controller
88+
public class NotHomeController3 : Controller
8989
{
9090
public string Index()
9191
{
@@ -94,10 +94,10 @@ public string Index()
9494
}
9595

9696
// derived from a class that has [NonController] attribute
97-
public class NotController : NotHomeController4
97+
public class NotController : NotHomeController3
9898
{
9999
public string Index()
100100
{
101101
return "This is Home Controller";
102102
}
103-
}
103+
}

csharp/ql/test/library-tests/frameworks/microsoft/AspNetCore.expected

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
| AspNetCore.cs:32:14:32:28 | HomeController3 |
55
| AspNetCore.cs:42:14:42:28 | HomeController4 |
66
| AspNetCore.cs:51:14:51:28 | HomeController5 |
7+
| AspNetCore.cs:60:23:60:37 | HomeController6 |

csharp/ql/test/query-tests/Security Features/CWE-089/SqlInjection.cs

+29
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ namespace Test
1010
using System.Data;
1111
using System.Data.Entity;
1212
using System.Data.SqlClient;
13+
using System.Diagnostics.CodeAnalysis;
14+
using System.Threading;
15+
using System.Threading.Tasks;
1316
using System.Web.UI.WebControls;
17+
using Microsoft.AspNetCore.Http;
18+
using Microsoft.AspNetCore.Mvc;
1419

1520
public class EntityFrameworkContext : DbContext
1621
{
@@ -110,4 +115,28 @@ public void GetDataSetByCategory()
110115

111116
System.Windows.Forms.TextBox box1;
112117
}
118+
119+
public abstract class MyController : Controller
120+
{
121+
[HttpPost("{userId:string}")]
122+
public async Task<IActionResult> GetUserById([FromRoute] string userId, CancellationToken cancellationToken)
123+
{
124+
// This is a vulnerable method due to SQL injection
125+
string query = "SELECT * FROM Users WHERE UserId = '" + userId + "'";
126+
127+
using (SqlConnection connection = new SqlConnection("YourConnectionString"))
128+
{
129+
SqlCommand command = new SqlCommand(query, connection);
130+
connection.Open();
131+
132+
SqlDataReader reader = command.ExecuteReader();
133+
while (reader.Read())
134+
{
135+
Console.WriteLine(String.Format("{0}, {1}", reader["UserId"], reader["Username"]));
136+
}
137+
}
138+
139+
return Ok();
140+
}
141+
}
113142
}

csharp/ql/test/query-tests/Security Features/CWE-089/SqlInjection.expected

+61-55
Large diffs are not rendered by default.

csharp/ql/test/query-tests/Security Features/CWE-089/options

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ semmle-extractor-options: --load-sources-from-project:${testdir}/../../../resour
33
semmle-extractor-options: --load-sources-from-project:${testdir}/../../../resources/stubs/System.Data.SqlClient/4.8.5/System.Data.SqlClient.csproj
44
semmle-extractor-options: --load-sources-from-project:${testdir}/../../../resources/stubs/System.Data.SQLite/1.0.118/System.Data.SQLite.csproj
55
semmle-extractor-options: ${testdir}/../../../resources/stubs/System.Windows.cs
6+
semmle-extractor-options: --load-sources-from-project:${testdir}/../../../resources/stubs/_frameworks/Microsoft.AspNetCore.App/Microsoft.AspNetCore.App.csproj

0 commit comments

Comments
 (0)