Skip to content

Commit 5d14307

Browse files
committed
C#: Add a SQL injection test case for ASP.NET.
1 parent a1a6fe4 commit 5d14307

File tree

3 files changed

+85
-55
lines changed

3 files changed

+85
-55
lines changed

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

Lines changed: 29 additions & 0 deletions
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
}

0 commit comments

Comments
 (0)