Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly escape constant regex patterns #3299

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/EFCore.PG/Query/Internal/NpgsqlQuerySqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,7 @@ protected virtual Expression VisitRegexMatch(PgRegexMatchExpression expression,
}
else
{
Sql.Append(constantPattern);
Sql.Append(constantPattern.Replace("'", "''"));
Sql.Append("'");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,23 @@ await AssertQuery(
""");
}

[Theory]
[MemberData(nameof(IsAsyncData))]
public async Task Regex_IsMatch_with_constant_pattern_properly_escaped(bool async)
{
await AssertQuery(
async,
cs => cs.Set<Customer>().Where(c => Regex.IsMatch(c.CompanyName, "^A';foo")),
assertEmpty: true);

AssertSql(
"""
SELECT c."CustomerID", c."Address", c."City", c."CompanyName", c."ContactName", c."ContactTitle", c."Country", c."Fax", c."Phone", c."PostalCode", c."Region"
FROM "Customers" AS c
WHERE c."CompanyName" ~ '(?p)^A'';foo'
""");
}

[Theory]
[MemberData(nameof(IsAsyncData))]
public async Task Regex_IsMatch_with_parameter_pattern(bool async)
Expand Down