EF1003: Detect explicit string.Format/string.Concat in raw SQL APIs#38208
Merged
AndriySvyryd merged 2 commits intoJun 8, 2026
Merged
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 |
There was a problem hiding this comment.
Pull request overview
This PR extends the EF Core analyzers’ EF1003 rule (StringConcatenationUsageInRawQueries) to also flag raw SQL strings constructed via explicit string.Format(...) and string.Concat(...) calls (in addition to + concatenation and interpolated strings), closing a gap that could allow SQL-injection-prone dynamic SQL to go undetected.
Changes:
- Extend
StringsUsageInRawQueriesDiagnosticAnalyzer.AnalyzeInvocationto recognizestring.Format/string.Concatinvocations and warn when any relevant argument is non-constant. - Add new analyzer tests covering constant vs. non-constant
string.Format/string.Concatused inFromSqlRaw,ExecuteSqlRaw,ExecuteSqlRawAsync, andSqlQueryRaw<T>.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/EFCore.Analyzers/StringsUsageInRawQueriesDiagnosticAnalyzer.cs | Adds detection for raw SQL built via string.Format / string.Concat based on const-ness of arguments. |
| test/EFCore.Analyzers.Tests/StringConcatenationInRawQueriesAnalyzerTests.cs | Adds new theory cases validating EF1003 behavior for Format/Concat construction patterns. |
The IFormatProvider argument of the string.Format(IFormatProvider, ...) overloads (e.g. CultureInfo.InvariantCulture) is never a compile-time constant, so HasNonConstantArgument reported EF1003 even when the format string and every formatted value were constant. Skip that argument by its parameter type — it never contributes to the SQL text — and evaluate only the format string and formatted values. string.Concat is unaffected (it has no IFormatProvider overload). Add a no-diagnostic test for the provider overload and a companion should-report test to confirm the skip doesn't suppress genuine warnings.
AndriySvyryd
approved these changes
Jun 8, 2026
Member
|
Thanks for your contribution! |
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>.