Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ (CodeQL) SQL query built from user-controlled sources #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 70 additions & 57 deletions WebGoat.NET/Data/OrderRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,63 +25,76 @@ public Order GetOrderById(int orderId)
}

public int CreateOrder(Order order)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
// These commented lines cause EF Core to do wierd things.
// Instead, make the query manually.

// order = _context.Orders.Add(order).Entity;
// _context.SaveChanges();
// return order.OrderId;

string shippedDate = order.ShippedDate.HasValue ? "'" + string.Format("yyyy-MM-dd", order.ShippedDate.Value) + "'" : "NULL";
var sql = "INSERT INTO Orders (" +
"CustomerId, EmployeeId, OrderDate, RequiredDate, ShippedDate, ShipVia, Freight, ShipName, ShipAddress, " +
"ShipCity, ShipRegion, ShipPostalCode, ShipCountry" +
") VALUES (" +
$"'{order.CustomerId}','{order.EmployeeId}','{order.OrderDate:yyyy-MM-dd}','{order.RequiredDate:yyyy-MM-dd}'," +
$"{shippedDate},'{order.ShipVia}','{order.Freight}','{order.ShipName}','{order.ShipAddress}'," +
$"'{order.ShipCity}','{order.ShipRegion}','{order.ShipPostalCode}','{order.ShipCountry}')";
sql += ";\nSELECT OrderID FROM Orders ORDER BY OrderID DESC LIMIT 1;";

using (var command = _context.Database.GetDbConnection().CreateCommand())
{
command.CommandText = sql;
_context.Database.OpenConnection();

using var dataReader = command.ExecuteReader();
dataReader.Read();
order.OrderId = Convert.ToInt32(dataReader[0]);
}

sql = ";\nINSERT INTO OrderDetails (" +
"OrderId, ProductId, UnitPrice, Quantity, Discount" +
") VALUES ";
foreach (var (orderDetails, i) in order.OrderDetails.WithIndex())
{
orderDetails.OrderId = order.OrderId;
sql += (i > 0 ? "," : "") +
$"('{orderDetails.OrderId}','{orderDetails.ProductId}','{orderDetails.UnitPrice}','{orderDetails.Quantity}'," +
$"'{orderDetails.Discount}')";
}

if (order.Shipment != null)
{
var shipment = order.Shipment;
shipment.OrderId = order.OrderId;
sql += ";\nINSERT INTO Shipments (" +
"OrderId, ShipperId, ShipmentDate, TrackingNumber" +
") VALUES (" +
$"'{shipment.OrderId}','{shipment.ShipperId}','{shipment.ShipmentDate:yyyy-MM-dd}','{shipment.TrackingNumber}')";
}

using (var command = _context.Database.GetDbConnection().CreateCommand())
{
command.CommandText = sql;
_context.Database.OpenConnection();
command.ExecuteNonQuery();
}

{
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
// These commented lines cause EF Core to do wierd things.
// Instead, make the query manually.

// order = _context.Orders.Add(order).Entity;
// _context.SaveChanges();
// return order.OrderId;

string shippedDate = order.ShippedDate.HasValue ? "'" + string.Format("yyyy-MM-dd", order.ShippedDate.Value) + "'" : "NULL";
var sql = "INSERT INTO Orders (" +
"CustomerId, EmployeeId, OrderDate, RequiredDate, ShippedDate, ShipVia, Freight, ShipName, ShipAddress, " +
"ShipCity, ShipRegion, ShipPostalCode, ShipCountry) VALUES (@CustomerId, @EmployeeId, @OrderDate, @RequiredDate, @ShippedDate, @ShipVia, @Freight, @ShipName, @ShipAddress, @ShipCity, @ShipRegion, @ShipPostalCode, @ShipCountry)";
") VALUES (" +
$"'{order.CustomerId}','{order.EmployeeId}','{order.OrderDate:yyyy-MM-dd}','{order.RequiredDate:yyyy-MM-dd}'," +
$"{shippedDate},'{order.ShipVia}','{order.Freight}','{order.ShipName}','{order.ShipAddress}'," +
$"'{order.ShipCity}','{order.ShipRegion}','{order.ShipPostalCode}','{order.ShipCountry}')";
sql += ";\nSELECT OrderID FROM Orders ORDER BY OrderID DESC LIMIT 1;";

using (var command = _context.Database.GetDbConnection().CreateCommand())
{
command.CommandText = sql;
command.Parameters.AddWithValue("@CustomerId", order.CustomerId);
command.Parameters.AddWithValue("@EmployeeId", order.EmployeeId);
command.Parameters.AddWithValue("@OrderDate", order.OrderDate.ToString("yyyy-MM-dd"));
command.Parameters.AddWithValue("@RequiredDate", order.RequiredDate.ToString("yyyy-MM-dd"));
command.Parameters.AddWithValue("@ShippedDate", shippedDate);
command.Parameters.AddWithValue("@ShipVia", order.ShipVia);
command.Parameters.AddWithValue("@Freight", order.Freight);
command.Parameters.AddWithValue("@ShipName", order.ShipName);
command.Parameters.AddWithValue("@ShipAddress", order.ShipAddress);
command.Parameters.AddWithValue("@ShipCity", order.ShipCity);
command.Parameters.AddWithValue("@ShipRegion", order.ShipRegion);
command.Parameters.AddWithValue("@ShipPostalCode", order.ShipPostalCode);
command.Parameters.AddWithValue("@ShipCountry", order.ShipCountry);
_context.Database.OpenConnection();

using var dataReader = command.ExecuteReader();
dataReader.Read();
order.OrderId = Convert.ToInt32(dataReader[0]);
}

sql = ";\nINSERT INTO OrderDetails (" +
"OrderId, ProductId, UnitPrice, Quantity, Discount" +
") VALUES ";
foreach (var (orderDetails, i) in order.OrderDetails.WithIndex())
{
orderDetails.OrderId = order.OrderId;
sql += (i > 0 ? "," : "") +
$"('{orderDetails.OrderId}','{orderDetails.ProductId}','{orderDetails.UnitPrice}','{orderDetails.Quantity}'," +
$"'{orderDetails.Discount}')";
}

if (order.Shipment != null)
{
var shipment = order.Shipment;
shipment.OrderId = order.OrderId;
sql += ";\nINSERT INTO Shipments (" +
"OrderId, ShipperId, ShipmentDate, TrackingNumber" +
") VALUES (" +
$"'{shipment.OrderId}','{shipment.ShipperId}','{shipment.ShipmentDate:yyyy-MM-dd}','{shipment.TrackingNumber}')";
}

using (var command = _context.Database.GetDbConnection().CreateCommand())
{
command.CommandText = sql;
_context.Database.OpenConnection();
command.ExecuteNonQuery();
}

return order.OrderId;
}

Expand Down
Loading