I have an extension for Entity Framework Core query to support between and I see that the SQL being parsed includes literal values. Is it vulnerable for SQL injection attacks?
This is the extension method:
public static IQueryable<T> WhereBetween<T, TValue>(
this IQueryable<T> query,
TValue? min,
TValue? max,
Expression<Func<T, TValue>> selector)
where TValue : struct, IComparable<TValue>
{
if (min.HasValue)
{
query = query.Where(Expression.Lambda<Func<T, bool>>(
Expression.GreaterThanOrEqual(
selector.Body,
Expression.Constant(min.Value)
),
selector.Parameters
));
}
if (max.HasValue)
{
query = query.Where(Expression.Lambda<Func<T, bool>>(
Expression.LessThanOrEqual(
selector.Body,
Expression.Constant(max.Value)
),
selector.Parameters
));
}
return query;
}
I use it as a filter with minimum value and maximum value, and this returns as >= and <=. I use it for multiple data types like dates, ints etc.
Example method:
public List<Order> GetOrders(int minValue, int maxValue, DateOnly startDate, DateOnly endDate)
{
return dbContext.Orders
.WhereBetween(minValue, maxValue, o => o.OrderTotal)
.WhereBetween(startDate, endDate, o => o.OrderDate)
.ToList();
}
This uses the same extension for DateOnly and int. The SQL that it's being parsed to is
select ...
from Orders o
where o.OrderTotal >= 10
and o.OrderTotal <= 20
and o.OrderDate >= '2025-01-01'
and o.OrderDate <= '2025-02-02'
It's being parsed as literal values not parameterized. Do I need to worry for SQL injection attacks? Or is it just displayed in the logs as literal values but it is being sent as escaped literal values?
Where(o.OrderTotal>=min && ..)manually it's different?where o.OrderTotal >= @order_minor something like that.where o.OrderTotal >= @order_min. Keep in mind that prepared statements do not add the values "back" into the query.