SonarSource / sonar-dotnet

Code analyzer for C# and VB.NET projects
https://redirect.sonarsource.com/plugins/csharp.html
GNU Lesser General Public License v3.0
786 stars 226 forks source link

Fix S2077 FN: SQL Injection Hotspot not triggered using EntityFramework #5636

Open mattwatsonad opened 2 years ago

mattwatsonad commented 2 years ago

Description

When using DataContext.TableName.FromSql("") sql injection hotspots are not being picked up.

Repro steps

[HttpGet("search/{searchTerm}")]
public async Task<IActionResult> DoSearch(string searchTerm)
{
    var result = await DataContext.MyTable
        .FromSql($"Search { searchTerm }")
        .AsNoTracking()
        .ToListAsync();
    return Ok(result);
}

Expected behavior

It should identify that that user-input is being put straight into a string executed as sql, and flag as a hotspot.

Actual behavior

No issues of any kind detected on above snippet

Known workarounds

None

Related information

SonarQube Developer Edition Version 9.3 (build 51899) ASP.NET Core 3.1 C# Project

costin-zaharia-sonarsource commented 2 years ago

Hi @mattwatsonad, thanks for reporting the issue. I can confirm the false negative.

sebastien-marichal commented 4 months ago

Base on the documentation-system-formattablestring)), interpolated string are safe.

Any interpolated parameter values you supply will automatically be converted to a DbParameter

Before closing this issue we need to update the reproduced to remove the // Compliant - FN comments.