/* goodG2B() - use goodsource and badsink */ private static void GoodG2B(HttpRequest req, HttpResponse resp) { string data = CWE89_SQL_Injection__Web_File_ExecuteNonQuery_61b.GoodG2BSource(req, resp); int? result = null; try { using (SqlConnection dbConnection = IO.GetDBConnection()) { dbConnection.Open(); using (SqlCommand badSqlCommand = new SqlCommand(null, dbConnection)) { /* POTENTIAL FLAW: data concatenated into SQL statement used in ExecuteNonQuery(), which could result in SQL Injection */ badSqlCommand.CommandText = "insert into users (status) values ('updated') where name='" + data + "'"; result = badSqlCommand.ExecuteNonQuery(); if (result != null) { IO.WriteLine("Name, " + data + ", updated successfully"); } else { IO.WriteLine("Unable to update records for user: "******"Error getting database connection", exceptSql); } }
/* goodB2G() - use badsource and goodsink */ private static void GoodB2G(HttpRequest req, HttpResponse resp) { string data = CWE89_SQL_Injection__Web_File_ExecuteNonQuery_61b.GoodB2GSource(req, resp); int? result = null; try { using (SqlConnection dbConnection = IO.GetDBConnection()) { dbConnection.Open(); using (SqlCommand goodSqlCommand = new SqlCommand(null, dbConnection)) { goodSqlCommand.CommandText = "insert into users (status) values ('updated') where name=@name"; /* FIX: Use prepared statement and ExecuteNonQuery (properly) */ SqlParameter nameParam = new SqlParameter("@name", SqlDbType.VarChar, 0); nameParam.Value = data; goodSqlCommand.Parameters.Add(nameParam); goodSqlCommand.Prepare(); result = goodSqlCommand.ExecuteNonQuery(); if (result != null) { IO.WriteLine("Name, " + data + ", updated successfully"); } else { IO.WriteLine("Unable to update records for user: "******"Error getting database connection", exceptSql); } }