Skip to content

Commit bb85e24

Browse files
committed
C#: Convert SQL injection test to use inline expectations.
1 parent af2ebed commit bb85e24

File tree

5 files changed

+44
-42
lines changed

5 files changed

+44
-42
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,20 @@ public void ProcessRequest()
1717
{
1818
connection.Open();
1919
SqlCommand customerCommand = new SqlCommand("SELECT * FROM customers", connection);
20-
SqlDataReader customerReader = customerCommand.ExecuteReader();
20+
SqlDataReader customerReader = customerCommand.ExecuteReader(); // $ Source[cs/sql-injection]
2121

2222
while (customerReader.Read())
2323
{
2424
// BAD: Read from database, write it straight to another query
25-
SqlCommand secondCustomerCommand = new SqlCommand("SELECT * FROM customers WHERE customerName=" + customerReader.GetString(1), connection);
25+
SqlCommand secondCustomerCommand = new SqlCommand("SELECT * FROM customers WHERE customerName=" + customerReader.GetString(1), connection); // $ Alert[cs/sql-injection]
2626
}
2727
customerReader.Close();
2828
}
2929
}
3030

3131
public void RunSQLFromFile()
3232
{
33-
using (FileStream fs = new FileStream("myfile.txt", FileMode.Open))
33+
using (FileStream fs = new FileStream("myfile.txt", FileMode.Open)) // $ Source[cs/sql-injection]
3434
{
3535
using (StreamReader sr = new StreamReader(fs, Encoding.UTF8))
3636
{
@@ -42,7 +42,7 @@ public void RunSQLFromFile()
4242
continue;
4343
using (var connection = new SQLiteConnection(""))
4444
{
45-
var cmd = new SQLiteCommand(sql, connection);
45+
var cmd = new SQLiteCommand(sql, connection); // $ Alert[cs/sql-injection]
4646
cmd.ExecuteScalar();
4747
}
4848
}

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ public void GetDataSetByCategory()
3535
using (var connection = new SqlConnection(connectionString))
3636
{
3737
var query1 = "SELECT ITEM,PRICE FROM PRODUCT WHERE ITEM_CATEGORY='"
38-
+ categoryTextBox.Text + "' ORDER BY PRICE";
39-
var adapter = new SqlDataAdapter(query1, connection);
38+
+ categoryTextBox.Text + "' ORDER BY PRICE"; // $ Source[cs/sql-injection]
39+
var adapter = new SqlDataAdapter(query1, connection); // $ Alert[cs/sql-injection]
4040
var result = new DataSet();
4141
adapter.Fill(result);
4242
}
@@ -70,9 +70,9 @@ public void GetDataSetByCategory()
7070
{
7171
// BAD: Use EntityFramework direct Sql execution methods
7272
var query1 = "SELECT ITEM,PRICE FROM PRODUCT WHERE ITEM_CATEGORY='"
73-
+ categoryTextBox.Text + "' ORDER BY PRICE";
74-
context.Database.ExecuteSqlCommand(query1);
75-
context.Database.SqlQuery<string>(query1);
73+
+ categoryTextBox.Text + "' ORDER BY PRICE"; // $ Source[cs/sql-injection]
74+
context.Database.ExecuteSqlCommand(query1); // $ Alert[cs/sql-injection]
75+
context.Database.SqlQuery<string>(query1); // $ Alert[cs/sql-injection]
7676
// GOOD: Use EntityFramework direct Sql execution methods with parameter
7777
var query2 = "SELECT ITEM,PRICE FROM PRODUCT WHERE ITEM_CATEGORY="
7878
+ "@p0 ORDER BY PRICE";
@@ -84,8 +84,8 @@ public void GetDataSetByCategory()
8484
using (var connection = new SqlConnection(connectionString))
8585
{
8686
var query1 = "SELECT ITEM,PRICE FROM PRODUCT WHERE ITEM_CATEGORY='"
87-
+ box1.Text + "' ORDER BY PRICE";
88-
var adapter = new SqlDataAdapter(query1, connection);
87+
+ box1.Text + "' ORDER BY PRICE"; // $ Source[cs/sql-injection]
88+
var adapter = new SqlDataAdapter(query1, connection); // $ Alert[cs/sql-injection]
8989
var result = new DataSet();
9090
adapter.Fill(result);
9191
}
@@ -94,9 +94,9 @@ public void GetDataSetByCategory()
9494
using (var connection = new SqlConnection(connectionString))
9595
{
9696
var queryString = "SELECT ITEM,PRICE FROM PRODUCT WHERE ITEM_CATEGORY='"
97-
+ box1.Text + "' ORDER BY PRICE";
98-
var cmd = new SqlCommand(queryString);
99-
var adapter = new SqlDataAdapter(cmd);
97+
+ box1.Text + "' ORDER BY PRICE"; // $ Source[cs/sql-injection]
98+
var cmd = new SqlCommand(queryString); // $ Alert[cs/sql-injection]
99+
var adapter = new SqlDataAdapter(cmd); // $ Alert[cs/sql-injection]
100100
var result = new DataSet();
101101
adapter.Fill(result);
102102
}
@@ -105,9 +105,9 @@ public void GetDataSetByCategory()
105105
using (var connection = new SqlConnection(connectionString))
106106
{
107107
var queryString = "SELECT ITEM,PRICE FROM PRODUCT WHERE ITEM_CATEGORY='"
108-
+ Console.ReadLine()! + "' ORDER BY PRICE";
109-
var cmd = new SqlCommand(queryString);
110-
var adapter = new SqlDataAdapter(cmd);
108+
+ Console.ReadLine()! + "' ORDER BY PRICE"; // $ Source[cs/sql-injection]
109+
var cmd = new SqlCommand(queryString); // $ Alert[cs/sql-injection]
110+
var adapter = new SqlDataAdapter(cmd); // $ Alert[cs/sql-injection]
111111
var result = new DataSet();
112112
adapter.Fill(result);
113113
}
@@ -119,14 +119,14 @@ public void GetDataSetByCategory()
119119
public abstract class MyController : Controller
120120
{
121121
[HttpPost("{userId:string}")]
122-
public async Task<IActionResult> GetUserById([FromRoute] string userId, CancellationToken cancellationToken)
122+
public async Task<IActionResult> GetUserById([FromRoute] string userId, CancellationToken cancellationToken) // $ Source[cs/sql-injection]
123123
{
124124
// This is a vulnerable method due to SQL injection
125125
string query = "SELECT * FROM Users WHERE UserId = '" + userId + "'";
126126

127127
using (SqlConnection connection = new SqlConnection("YourConnectionString"))
128128
{
129-
SqlCommand command = new SqlCommand(query, connection);
129+
SqlCommand command = new SqlCommand(query, connection); // $ Alert[cs/sql-injection]
130130
connection.Open();
131131

132132
SqlDataReader reader = command.ExecuteReader();
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
query: Security Features/CWE-089/SqlInjection.ql
2-
postprocess: utils/test/PrettyPrintModels.ql
2+
postprocess:
3+
- utils/test/PrettyPrintModels.ql
4+
- utils/test/InlineExpectationsTestQuery.ql

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,64 +17,64 @@ public void Bad01()
1717
{
1818
using (var connection = new SqlConnection(connectionString))
1919
{
20-
var query = "SELECT ITEM,PRICE FROM PRODUCT WHERE ITEM_CATEGORY='" + box1.Text + "' ORDER BY PRICE";
21-
var result = connection.Query<object>(query);
20+
var query = "SELECT ITEM,PRICE FROM PRODUCT WHERE ITEM_CATEGORY='" + box1.Text + "' ORDER BY PRICE"; // $ Source[cs/sql-injection]
21+
var result = connection.Query<object>(query); // $ Alert[cs/sql-injection]
2222
}
2323
}
2424

2525
public async Task Bad02()
2626
{
2727
using (var connection = new SqlConnection(connectionString))
2828
{
29-
var query = "SELECT ITEM,PRICE FROM PRODUCT WHERE ITEM_CATEGORY='" + box1.Text + "' ORDER BY PRICE";
30-
var result = await connection.QueryAsync<object>(query);
29+
var query = "SELECT ITEM,PRICE FROM PRODUCT WHERE ITEM_CATEGORY='" + box1.Text + "' ORDER BY PRICE"; // $ Source[cs/sql-injection]
30+
var result = await connection.QueryAsync<object>(query); // $ Alert[cs/sql-injection]
3131
}
3232
}
3333

3434
public async Task Bad03()
3535
{
3636
using (var connection = new SqlConnection(connectionString))
3737
{
38-
var query = "SELECT ITEM,PRICE FROM PRODUCT WHERE ITEM_CATEGORY='" + box1.Text + "' ORDER BY PRICE";
39-
var result = await connection.QueryFirstAsync(query);
38+
var query = "SELECT ITEM,PRICE FROM PRODUCT WHERE ITEM_CATEGORY='" + box1.Text + "' ORDER BY PRICE"; // $ Source[cs/sql-injection]
39+
var result = await connection.QueryFirstAsync(query); // $ Alert[cs/sql-injection]
4040
}
4141
}
4242

4343
public async Task Bad04()
4444
{
4545
using (var connection = new SqlConnection(connectionString))
4646
{
47-
var query = "SELECT ITEM,PRICE FROM PRODUCT WHERE ITEM_CATEGORY='" + box1.Text + "' ORDER BY PRICE";
47+
var query = "SELECT ITEM,PRICE FROM PRODUCT WHERE ITEM_CATEGORY='" + box1.Text + "' ORDER BY PRICE"; // $ Source[cs/sql-injection]
4848

49-
await connection.ExecuteAsync(query);
49+
await connection.ExecuteAsync(query); // $ Alert[cs/sql-injection]
5050
}
5151
}
5252

5353
public void Bad05()
5454
{
5555
using (var connection = new SqlConnection(connectionString))
5656
{
57-
var query = "SELECT ITEM,PRICE FROM PRODUCT WHERE ITEM_CATEGORY='" + box1.Text + "' ORDER BY PRICE";
58-
connection.ExecuteScalar(query);
57+
var query = "SELECT ITEM,PRICE FROM PRODUCT WHERE ITEM_CATEGORY='" + box1.Text + "' ORDER BY PRICE"; // $ Source[cs/sql-injection]
58+
connection.ExecuteScalar(query); // $ Alert[cs/sql-injection]
5959
}
6060
}
6161

6262
public void Bad06()
6363
{
6464
using (var connection = new SqlConnection(connectionString))
6565
{
66-
var query = "SELECT ITEM,PRICE FROM PRODUCT WHERE ITEM_CATEGORY='" + box1.Text + "' ORDER BY PRICE";
67-
connection.ExecuteReader(query);
66+
var query = "SELECT ITEM,PRICE FROM PRODUCT WHERE ITEM_CATEGORY='" + box1.Text + "' ORDER BY PRICE"; // $ Source[cs/sql-injection]
67+
connection.ExecuteReader(query); // $ Alert[cs/sql-injection]
6868
}
6969
}
7070

7171
public async Task Bad07()
7272
{
7373
using (var connection = new SqlConnection(connectionString))
7474
{
75-
var query = "SELECT ITEM,PRICE FROM PRODUCT WHERE ITEM_CATEGORY='" + box1.Text + "' ORDER BY PRICE";
75+
var query = "SELECT ITEM,PRICE FROM PRODUCT WHERE ITEM_CATEGORY='" + box1.Text + "' ORDER BY PRICE"; // $ Source[cs/sql-injection]
7676

77-
var comDef = new CommandDefinition(query);
77+
var comDef = new CommandDefinition(query); // $ Alert[cs/sql-injection]
7878
var result = await connection.QueryFirstAsync(comDef);
7979
}
8080
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ class SqlInjection
1616
public void InjectUntrustedData()
1717
{
1818
// BAD: untrusted data is not sanitized.
19-
SQLiteCommand cmd = new SQLiteCommand(untrustedData.Text);
19+
SQLiteCommand cmd = new SQLiteCommand(untrustedData.Text); // $ Alert[cs/sql-injection]
2020

2121
// BAD: untrusted data is not sanitized.
2222
using (var connection = new SQLiteConnection(connectionString))
2323
{
24-
cmd = new SQLiteCommand(untrustedData.Text, connection);
24+
cmd = new SQLiteCommand(untrustedData.Text, connection); // $ Source[cs/sql-injection] Alert[cs/sql-injection]
2525
}
2626

2727
SQLiteDataAdapter adapter;
@@ -30,23 +30,23 @@ public void InjectUntrustedData()
3030
// BAD: untrusted data is not sanitized.
3131
using (var connection = new SQLiteConnection(connectionString))
3232
{
33-
adapter = new SQLiteDataAdapter(untrustedData.Text, connection);
33+
adapter = new SQLiteDataAdapter(untrustedData.Text, connection); // $ Alert[cs/sql-injection]
3434
result = new DataSet();
3535
adapter.Fill(result);
3636
}
3737

3838
// BAD: untrusted data is not sanitized.
39-
adapter = new SQLiteDataAdapter(untrustedData.Text, connectionString);
39+
adapter = new SQLiteDataAdapter(untrustedData.Text, connectionString); // $ Alert[cs/sql-injection]
4040
result = new DataSet();
4141
adapter.Fill(result);
4242

4343
// BAD: untrusted data is not sanitized.
44-
adapter = new SQLiteDataAdapter(cmd);
44+
adapter = new SQLiteDataAdapter(cmd); // $ Alert[cs/sql-injection]
4545
result = new DataSet();
4646
adapter.Fill(result);
4747

4848
// BAD: untrusted data as filename is not sanitized.
49-
using (FileStream fs = new FileStream(untrustedData.Text, FileMode.Open))
49+
using (FileStream fs = new FileStream(untrustedData.Text, FileMode.Open)) // $ Source[cs/sql-injection]
5050
{
5151
using (StreamReader sr = new StreamReader(fs, Encoding.UTF8))
5252
{
@@ -58,12 +58,12 @@ public void InjectUntrustedData()
5858
continue;
5959
using (var connection = new SQLiteConnection(""))
6060
{
61-
cmd = new SQLiteCommand(sql, connection);
61+
cmd = new SQLiteCommand(sql, connection); // $ Alert[cs/sql-injection]
6262
cmd.ExecuteScalar();
6363
}
6464
}
6565
}
6666
}
6767
}
6868
}
69-
}
69+
}

0 commit comments

Comments
 (0)