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

Fix code scanning alert no. 327: SQL query built from user-controlled sources #460

Closed
wants to merge 1 commit into from
Closed
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
68 changes: 40 additions & 28 deletions DotnetPlayground.Web/Repositories/HashesRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -621,34 +621,34 @@ LIMIT @limit OFFSET @offset
{
// string col_names = string.Join("\",\"", PostgresAllColumnNames);
string sql =
(string.IsNullOrEmpty(searchText) ?
$@"
SELECT A.*, (SELECT count(*) FROM ""Hashes"") cnt
FROM
(SELECT *
FROM ""Hashes""
--The 'deferred join.' approach taken from https://aaronfrancis.com/2022/efficient-pagination-using-deferred-joins
inner join ( -- The 'deferred join.'
select ""Key"" from ""Hashes"" -- The pagination using a fast index.
order by ""Key""
LIMIT @limit OFFSET @offset
) as tmp using(""Key"")
{(string.IsNullOrEmpty(sortColumn) ? "" : $"ORDER BY \"{PostgresAllColumnNames.FirstOrDefault(x => string.Compare(x, sortColumn, StringComparison.CurrentCultureIgnoreCase) == 0)}\" {sortOrderDirection}")}
) A
"
:
$@"
WITH RowAndWhere AS
(
SELECT *
FROM ""Hashes""
WHERE {WhereColumnCondition('"', '"', PostgresAllColumnNames)}
)
SELECT B.*, (SELECT COUNT(*) FROM RowAndWhere) cnt
FROM RowAndWhere B
{(string.IsNullOrEmpty(sortColumn) ? "" : $"ORDER BY B.\"{PostgresAllColumnNames.FirstOrDefault(x => string.Compare(x, sortColumn, StringComparison.CurrentCultureIgnoreCase) == 0)}\" {sortOrderDirection}")}
LIMIT @limit OFFSET @offset
");
(string.IsNullOrEmpty(searchText) ?
$@"
SELECT A.*, (SELECT count(*) FROM ""Hashes"") cnt
FROM
(SELECT *
FROM ""Hashes""
--The 'deferred join.' approach taken from https://aaronfrancis.com/2022/efficient-pagination-using-deferred-joins
inner join ( -- The 'deferred join.'
select ""Key"" from ""Hashes"" -- The pagination using a fast index.
order by ""Key""
LIMIT @limit OFFSET @offset
) as tmp using(""Key"")
{(string.IsNullOrEmpty(sortColumn) ? "" : $"ORDER BY @sortColumn @sortOrderDirection")}
) A
"
:
$@"
WITH RowAndWhere AS
(
SELECT *
FROM ""Hashes""
WHERE {WhereColumnCondition('"', '"', PostgresAllColumnNames)}
)
SELECT B.*, (SELECT COUNT(*) FROM RowAndWhere) cnt
FROM RowAndWhere B
{(string.IsNullOrEmpty(sortColumn) ? "" : $"ORDER BY B.@sortColumn @sortOrderDirection")}
LIMIT @limit OFFSET @offset
");

using (var conn = new NpgsqlConnection(_configuration.GetConnectionString("PostgreSql")))
{
Expand Down Expand Up @@ -677,6 +677,18 @@ LIMIT @limit OFFSET @offset
parameter.Value = limit;
cmd.Parameters.Add(parameter);

parameter = cmd.CreateParameter();
parameter.ParameterName = "@sortColumn";
parameter.DbType = DbType.String;
parameter.Value = sortColumn;
cmd.Parameters.Add(parameter);

parameter = cmd.CreateParameter();
parameter.ParameterName = "@sortOrderDirection";
parameter.DbType = DbType.String;
parameter.Value = sortOrderDirection;
cmd.Parameters.Add(parameter);

if (!string.IsNullOrEmpty(searchText))
{
parameter = cmd.CreateParameter();
Expand Down
Loading