You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
FormattableString overload on ExecuteSqlCommand invites subtle SQL injection vulnerability #10080
db.ExecuteSqlCommand($"delete from Log where Time<{time}");
But this is not:
db.ExecuteSqlCommand(useLogA?$"delete from LogA where Time<{time}":$"delete from LogB where Time<{time}");
Likewise, this is not:
db.ExecuteSqlCommand($"delete from Log "+$"where Time<{time}");
In the first unsafe case, when two FormattableString operands are used with the conditional operator, the expression evaluates to a string, not another FormattableString. Unless you know this subtle (and I find rather unintuitive) rule, you might easily write code expecting the argument to be passed as a SQL parameter, only to find you've really dumped unsanitized user content into a SQL command.
In the second unsafe case, you're using multiple lines for readability. If you're a big C# fan, you might think C# is awesome enough to use string literal concatenation with interpolated strings. You'll be extra disappointed when your evil hacker trim, who hates C#, thinks otherwise, and proves he's right by bringing down your site.
In both unsafe cases the implicit conversion from string to RawSqlString causes the unintended overload to be invoked.
Proposed resolution:
Long term: Improve C# string interpolation.
Short term: Mark the FormattableString overload obsolete so that a warning alerts devs to the potential danger. Create a replacement that simply has a different name from the RawSqlString overload, perhaps ExecuteFormattedSqlCommand.
I don't question that there are reasonable ways to get EF Core to do what you want. Indeed, when I encountered this issue, once I discovered the parameters in my query weren't getting sent as SQL parameters, it was easy to fix.
My concern is discovery. In my experience that led to uncovering this issue, I was alerted to the problem when I was passing a string value that resulted in invalid SQL query since the string wasn't quoted. If a surprise absence of FormattableString always causes tests to fail, there's no problem. However, if there are scenarios where the SQL command would still work even without SQL parameters, that's when security holes can sneak in.
I think the key question - which I don't know the answer to - is whether there are types that are (1) open to SQL injection and (2) work with and without SQL parameterization. For example, strings are open to SQL injection, but would a string intended to be a parameter always fail if it ended up inline, due to lack of quoting?
In general, EF Core's design should account for rules of the major languages used to program with it. A good design avoids making it easy to write code that is insecure in a way likely to go unnoticed by the initial dev or a security code review.
@breyed@Mrswain The issue was previously brought to our attention e.g. at #9734 (comment), but we appreciate the level headed explanation and the additional examples provided here.
We are currently evaluating/discussing options within reach of the EF team, mainly trying to understand if changing things would actually result in a net improvement.
The problem with deprecating an overload (either the one that takes FormattableString or the one that takes RawSqlString) is that it might affect the usability of the API differently across different tools and IDEs, but this is indeed one of the options on the table. We are also going to brainstorm with the language team to see if they can come up with anything better.
@Mrswain Such a pragma would have the same effect as marking the overload obsolete: It would mark existing use and discourage future use of the dangerous construct. The obsolete marker is much easier to implement. In either case, there still should be a safe, friction-free option for new code and for migration of existing code, such as a non-overloaded method that takes the FormattableString.
changed the title [-]FormattableString overload on ExecuteSqlCommand invites subtle SQL injection vulverability[/-][+]FormattableString overload on ExecuteSqlCommand invites subtle SQL injection vulnerability[/+]on Feb 17, 2018
Activity
breyed commentedon Oct 16, 2017
I don't question that there are reasonable ways to get EF Core to do what you want. Indeed, when I encountered this issue, once I discovered the parameters in my query weren't getting sent as SQL parameters, it was easy to fix.
My concern is discovery. In my experience that led to uncovering this issue, I was alerted to the problem when I was passing a string value that resulted in invalid SQL query since the string wasn't quoted. If a surprise absence of FormattableString always causes tests to fail, there's no problem. However, if there are scenarios where the SQL command would still work even without SQL parameters, that's when security holes can sneak in.
I think the key question - which I don't know the answer to - is whether there are types that are (1) open to SQL injection and (2) work with and without SQL parameterization. For example, strings are open to SQL injection, but would a string intended to be a parameter always fail if it ended up inline, due to lack of quoting?
In general, EF Core's design should account for rules of the major languages used to program with it. A good design avoids making it easy to write code that is insecure in a way likely to go unnoticed by the initial dev or a security code review.
divega commentedon Oct 17, 2017
@breyed @Mrswain The issue was previously brought to our attention e.g. at #9734 (comment), but we appreciate the level headed explanation and the additional examples provided here.
We are currently evaluating/discussing options within reach of the EF team, mainly trying to understand if changing things would actually result in a net improvement.
The problem with deprecating an overload (either the one that takes FormattableString or the one that takes RawSqlString) is that it might affect the usability of the API differently across different tools and IDEs, but this is indeed one of the options on the table. We are also going to brainstorm with the language team to see if they can come up with anything better.
breyed commentedon Oct 18, 2017
@Mrswain Such a pragma would have the same effect as marking the overload obsolete: It would mark existing use and discourage future use of the dangerous construct. The obsolete marker is much easier to implement. In either case, there still should be a safe, friction-free option for new code and for migration of existing code, such as a non-overloaded method that takes the FormattableString.
divega commentedon Feb 16, 2018
We will try to address this with an analyzer. Assigning to @anpete.
[-]FormattableString overload on ExecuteSqlCommand invites subtle SQL injection vulverability[/-][+]FormattableString overload on ExecuteSqlCommand invites subtle SQL injection vulnerability[/+]Analyzers: Part of #10080 - Adds potential SQL injection analysis.
Analyzers: Part of #10080 - Adds potential SQL injection analysis.
Analyzers: Part of #10080 - Adds potential SQL injection analysis.
anpete commentedon Mar 27, 2018
Analyzer will be in 2.1