Skip to content

Commit ed7f682

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

File tree

5 files changed

+69
-0
lines changed

5 files changed

+69
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using Microsoft.Data;
3+
using Microsoft.Data.SqlClient;
4+
5+
namespace Test
6+
{
7+
class SqlInjection
8+
{
9+
string connectionString;
10+
System.Windows.Forms.TextBox box1;
11+
12+
public void MakeSqlCommand()
13+
{
14+
// BAD: Text from a local textbox
15+
using (var connection = new SqlConnection(connectionString))
16+
{
17+
var queryString = "SELECT ITEM,PRICE FROM PRODUCT WHERE ITEM_CATEGORY='"
18+
+ box1.Text + "' ORDER BY PRICE"; // $ Source[cs/sql-injection]
19+
var cmd = new SqlCommand(queryString); // $ Alert[cs/sql-injection]
20+
var adapter = new SqlDataAdapter(cmd); // $ Alert[cs/sql-injection]
21+
}
22+
23+
// BAD: Input from the command line.
24+
using (var connection = new SqlConnection(connectionString))
25+
{
26+
var queryString = "SELECT ITEM,PRICE FROM PRODUCT WHERE ITEM_CATEGORY='"
27+
+ Console.ReadLine() + "' ORDER BY PRICE"; // $ Source[cs/sql-injection]
28+
var cmd = new SqlCommand(queryString); // $ Alert[cs/sql-injection]
29+
var adapter = new SqlDataAdapter(cmd); // $ Alert[cs/sql-injection]
30+
}
31+
}
32+
}
33+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#select
2+
| SqlInjection.cs:19:42:19:52 | access to local variable queryString | SqlInjection.cs:18:21:18:29 | access to property Text : String | SqlInjection.cs:19:42:19:52 | access to local variable queryString | This query depends on $@. | SqlInjection.cs:18:21:18:29 | access to property Text : String | this TextBox text |
3+
| SqlInjection.cs:28:42:28:52 | access to local variable queryString | SqlInjection.cs:27:21:27:38 | call to method ReadLine : String | SqlInjection.cs:28:42:28:52 | access to local variable queryString | This query depends on $@. | SqlInjection.cs:27:21:27:38 | call to method ReadLine : String | this read from stdin |
4+
edges
5+
| SqlInjection.cs:17:21:17:31 | access to local variable queryString : String | SqlInjection.cs:19:42:19:52 | access to local variable queryString | provenance | |
6+
| SqlInjection.cs:18:21:18:29 | access to property Text : String | SqlInjection.cs:17:21:17:31 | access to local variable queryString : String | provenance | |
7+
| SqlInjection.cs:26:21:26:31 | access to local variable queryString : String | SqlInjection.cs:28:42:28:52 | access to local variable queryString | provenance | |
8+
| SqlInjection.cs:27:21:27:38 | call to method ReadLine : String | SqlInjection.cs:26:21:26:31 | access to local variable queryString : String | provenance | Src:MaD:1 |
9+
models
10+
| 1 | Source: System; Console; false; ReadLine; ; ; ReturnValue; stdin; manual |
11+
nodes
12+
| SqlInjection.cs:17:21:17:31 | access to local variable queryString : String | semmle.label | access to local variable queryString : String |
13+
| SqlInjection.cs:18:21:18:29 | access to property Text : String | semmle.label | access to property Text : String |
14+
| SqlInjection.cs:19:42:19:52 | access to local variable queryString | semmle.label | access to local variable queryString |
15+
| SqlInjection.cs:26:21:26:31 | access to local variable queryString : String | semmle.label | access to local variable queryString : String |
16+
| SqlInjection.cs:27:21:27:38 | call to method ReadLine : String | semmle.label | call to method ReadLine : String |
17+
| SqlInjection.cs:28:42:28:52 | access to local variable queryString | semmle.label | access to local variable queryString |
18+
subpaths
19+
testFailures
20+
| SqlInjection.cs:20:56:20:83 | // ... | Missing result: Alert[cs/sql-injection] |
21+
| SqlInjection.cs:29:56:29:83 | // ... | Missing result: Alert[cs/sql-injection] |
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)