Skip to content

Commit 55dd99e

Browse files
committed
Properly escape constant regex patterns (#3299)
Fixes #3292 (cherry picked from commit d316b35)
1 parent 3ebe6f5 commit 55dd99e

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/EFCore.PG/Query/Internal/NpgsqlQuerySqlGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,7 @@ protected virtual Expression VisitRegexMatch(PgRegexMatchExpression expression)
962962
}
963963
else
964964
{
965-
Sql.Append(constantPattern);
965+
Sql.Append(constantPattern.Replace("'", "''"));
966966
Sql.Append("'");
967967
}
968968

test/EFCore.PG.FunctionalTests/Query/NorthwindFunctionsQueryNpgsqlTest.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,23 @@ await AssertQuery(
118118
""");
119119
}
120120

121+
[Theory]
122+
[MemberData(nameof(IsAsyncData))]
123+
public async Task Regex_IsMatch_with_constant_pattern_properly_escaped(bool async)
124+
{
125+
await AssertQuery(
126+
async,
127+
cs => cs.Set<Customer>().Where(c => Regex.IsMatch(c.CompanyName, "^A';foo")),
128+
assertEmpty: true);
129+
130+
AssertSql(
131+
"""
132+
SELECT c."CustomerID", c."Address", c."City", c."CompanyName", c."ContactName", c."ContactTitle", c."Country", c."Fax", c."Phone", c."PostalCode", c."Region"
133+
FROM "Customers" AS c
134+
WHERE c."CompanyName" ~ '(?p)^A'';foo'
135+
""");
136+
}
137+
121138
[Theory]
122139
[MemberData(nameof(IsAsyncData))]
123140
public async Task Regex_IsMatch_with_parameter_pattern(bool async)

0 commit comments

Comments
 (0)