Skip to content

Commit eef6daa

Browse files
committed
C#: Add cs/sql-injection tests for APIs in Microsoft.Data.SqlClient.
1 parent 96f6a8e commit eef6daa

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
3+
namespace System.Web.UI.WebControls
4+
{
5+
public class TextBox { public string Text { get; set; } }
6+
}
7+
8+
namespace Test
9+
{
10+
using Microsoft.Data;
11+
using Microsoft.Data.SqlClient;
12+
using System.Web.UI.WebControls;
13+
14+
class SqlInjection
15+
{
16+
TextBox categoryTextBox;
17+
string connectionString;
18+
19+
public void MakeSqlCommand()
20+
{
21+
// BAD: Text from a local textbox
22+
using (var connection = new SqlConnection(connectionString))
23+
{
24+
var queryString = "SELECT ITEM,PRICE FROM PRODUCT WHERE ITEM_CATEGORY='"
25+
+ box1.Text + "' ORDER BY PRICE"; // $ Source[cs/sql-injection]
26+
var cmd = new SqlCommand(queryString); // $ Alert[cs/sql-injection]
27+
var adapter = new SqlDataAdapter(cmd); // $ Alert[cs/sql-injection]
28+
}
29+
30+
// BAD: Input from the command line.
31+
using (var connection = new SqlConnection(connectionString))
32+
{
33+
var queryString = "SELECT ITEM,PRICE FROM PRODUCT WHERE ITEM_CATEGORY='"
34+
+ Console.ReadLine() + "' ORDER BY PRICE"; // $ Source[cs/sql-injection]
35+
var cmd = new SqlCommand(queryString); // $ Alert[cs/sql-injection]
36+
var adapter = new SqlDataAdapter(cmd); // $ Alert[cs/sql-injection]
37+
}
38+
}
39+
40+
System.Windows.Forms.TextBox box1;
41+
}
42+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
extensions:
2+
3+
- addsTo:
4+
pack: codeql/threat-models
5+
extensible: threatModelConfiguration
6+
data:
7+
- ["local", true, 0]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
query: Security Features/CWE-089/SqlInjection.ql
2+
postprocess:
3+
- utils/test/PrettyPrintModels.ql
4+
- utils/test/InlineExpectationsTestQuery.ql
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
semmle-extractor-options: /nostdlib /noconfig
2+
semmle-extractor-options: --load-sources-from-project:${testdir}/../../../resources/stubs/Microsoft.Data.SqlClient/6.0.2/Microsoft.Data.SqlClient.csproj
3+
semmle-extractor-options: ${testdir}/../../../resources/stubs/System.Windows.cs
4+
semmle-extractor-options: --load-sources-from-project:${testdir}/../../../resources/stubs/_frameworks/Microsoft.AspNetCore.App/Microsoft.AspNetCore.App.csproj

0 commit comments

Comments
 (0)