EF Core and SQL injection

Content:

(inspired by LinkedIn post)

SQL injection is normally not a problem in EF and EF Core. But the FromSqlRaw method opens up for it.

db.T1.Where(row => row.F2 == f2) is good (parameter)
db.T1.FromSql($"SELECT * FROM t1 WHERE f2 = {f2}").ToList() is good (parameter)
db.T1.FromSqlRaw($"SELECT * FROM t1 WHERE f2 = '{f2}'").ToList() is bad (literal)
db.T1.FromSqlRaw("SELECT * FROM t1 WHERE f2 = {0}", f2).ToList() is good (parameter)

Full example:

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Common;
using System.Linq;

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;

namespace EFCI
{
    [Table("t1")]
    public class T1
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        [Column("f1")]
        public int F1 { get; set; }
        [Column("f2")]
        public string F2 { get; set; }
        public T1() : this(0, "")
        {
        }
        public T1(int f1, string f2)
        {
            F1 = f1;
            F2 = f2;
        }
        public override string ToString()
        {
            return string.Format("[{0},{1}]", F1, F2);
        }
    }
    public class MyInterceptor : DbCommandInterceptor
    {
        public override DbDataReader ReaderExecuted(DbCommand command, CommandExecutedEventData eventData, DbDataReader result)
        {
            Console.WriteLine(command.CommandText);
            return base.ReaderExecuted(command, eventData, result);
        }
    }
    public class MyDbContext : DbContext
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(@"Server=ARNEPC5\SQLEXPRESS;Integrated Security=true;Database=Test;Encrypt=True;TrustServerCertificate=True")
                          .AddInterceptors(new MyInterceptor());
        }
        public DbSet<T1> T1 { get; set; }
    }
    public class Program
    {
        public static void Main(string[] args)
        {
            using (MyDbContext db = new MyDbContext())
            {
                foreach (T1 o in db.T1)
                {
                    Console.WriteLine(o);
                }
                string f2 = "BB";
                foreach (T1 o in db.T1.Where(row => row.F2 == f2))
                {
                    Console.WriteLine(o);
                }
                foreach (T1 o in db.T1.FromSql($"SELECT * FROM t1 WHERE f2 = {f2}").ToList())
                {
                    Console.WriteLine(o);
                }
                foreach (T1 o in db.T1.FromSqlRaw($"SELECT * FROM t1 WHERE f2 = '{f2}'").ToList())
                {
                    Console.WriteLine(o);
                }
                foreach (T1 o in db.T1.FromSqlRaw("SELECT * FROM t1 WHERE f2 = {0}", f2).ToList())
                {
                    Console.WriteLine(o);
                }
            }
        }
    }
}

Output:

SELECT [t].[f1], [t].[f2]
FROM [t1] AS [t]
[1,A]
[2,BB]
[3,CCC]
SELECT [t].[f1], [t].[f2]
FROM [t1] AS [t]
WHERE [t].[f2] = @__f2_0
[2,BB]
SELECT * FROM t1 WHERE f2 = @p0

[2,BB]
SELECT * FROM t1 WHERE f2 = 'BB'

[2,BB]
SELECT * FROM t1 WHERE f2 = @p0

[2,BB]

Comments: