-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathSqlInjection.cs
More file actions
33 lines (30 loc) · 1.21 KB
/
SqlInjection.cs
File metadata and controls
33 lines (30 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System;
using Microsoft.Data;
using Microsoft.Data.SqlClient;
namespace Test
{
class SqlInjection
{
string connectionString;
System.Windows.Forms.TextBox box1;
public void MakeSqlCommand()
{
// BAD: Text from a local textbox
using (var connection = new SqlConnection(connectionString))
{
var queryString = "SELECT ITEM,PRICE FROM PRODUCT WHERE ITEM_CATEGORY='"
+ box1.Text + "' ORDER BY PRICE"; // $ Source[cs/sql-injection]
var cmd = new SqlCommand(queryString); // $ Alert[cs/sql-injection]
var adapter = new SqlDataAdapter(cmd); // $ Alert[cs/sql-injection]
}
// BAD: Input from the command line.
using (var connection = new SqlConnection(connectionString))
{
var queryString = "SELECT ITEM,PRICE FROM PRODUCT WHERE ITEM_CATEGORY='"
+ Console.ReadLine() + "' ORDER BY PRICE"; // $ Source[cs/sql-injection]
var cmd = new SqlCommand(queryString); // $ Alert[cs/sql-injection]
var adapter = new SqlDataAdapter(cmd); // $ Alert[cs/sql-injection]
}
}
}
}