EF1003: Detect explicit string.Format/string.Concat in raw SQL APIs#38208
Open
m-x-shokhzod wants to merge 1 commit intodotnet:mainfrom
Open
EF1003: Detect explicit string.Format/string.Concat in raw SQL APIs#38208m-x-shokhzod wants to merge 1 commit intodotnet:mainfrom
m-x-shokhzod wants to merge 1 commit intodotnet:mainfrom
Conversation
The EF1003 analyzer warns when a raw SQL API receives a dynamically
built string. It already covered interpolated strings (EF1002) and the
binary `+` concatenation operator (EF1003), but explicit calls such as
db.Database.ExecuteSqlRaw(string.Format("UPDATE T SET X={0}", id));
db.Users.FromSqlRaw(string.Concat("SELECT * FROM T WHERE Id=", id));
were silently allowed. Extend the analyzer to also report these when at
least one argument is non-constant. Implicit conversions (object
boxing in the `string.Format(string, object)` overloads) are unwrapped
so that fully constant calls do not trigger the warning.
Fixes dotnet#37915
Contributor
Author
|
@dotnet-policy-service agree |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #37915.
Problem
EF1003 (
StringConcatenationUsageInRawQueries) currently warns when a raw SQL API receives a string built with the+operator, and EF1002 covers interpolated strings. Equivalent dynamic-string patterns built with explicit method calls were silently allowed:These have the same SQL-injection profile as the
+form but produced no diagnostic.Fix
Extend
StringsUsageInRawQueriesDiagnosticAnalyzer.AnalyzeInvocationwith a third pattern: when the SQL argument is itself anIInvocationOperationofstring.Formatorstring.Concat, walk its arguments and report EF1003 if at least one of them is non-constant.Per the issue title (
EF1003: Also detect ...), bothstring.Formatandstring.Concatmap to EF1003. Happy to splitstring.Formatonto EF1002 if reviewers prefer the semantic split.Implementation notes:
string→objectboxing instring.Format(string, object)overloads) are unwrapped via a small helper so a fully constant call likestring.Format("X = {0}", "1")is correctly recognized as constant and does not warn.params object?[]arrays are inspected element-by-element rather than tested as a whole.Verification
EFCore.Analyzersbuilds clean (netstandard2.0).test/EFCore.Analyzers.Tests— full suite passes locally: 114/114 (was 90; +24 new theory cases across the four target methods × six new scenarios).New test scenarios cover:
string.Format(constFormat, constArg)— does not reportstring.Format(constFormat, parameter)— reports EF1003string.Format(constFormat, GetId())— reports EF1003string.Concat(const, const, const)— does not reportstring.Concat(const, parameter)— reports EF1003string.Concat(const, GetId())— reports EF1003Each scenario is parameterized over
FromSqlRaw,ExecuteSqlRaw,ExecuteSqlRawAsync, andSqlQueryRaw<T>.