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

Obsolete vulnerable literal AddColumn #3517

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ public void InsertSqlStringTest()

insert.AddColumn("intColumn", NHibernateUtil.Int32);
insert.AddColumn("longColumn", NHibernateUtil.Int64);
#pragma warning disable CS0618 // Type or member is obsolete
insert.AddColumn("literalColumn", false, (ILiteralType) NHibernateUtil.Boolean);
#pragma warning restore CS0618 // Type or member is obsolete
insert.AddColumn("stringColumn", 5.ToString());

SqlCommandInfo sqlCommand = insert.ToSqlCommandInfo();
Expand All @@ -53,7 +55,9 @@ public void Commented()

insert.SetTableName("test_insert_builder");

#pragma warning disable CS0618 // Type or member is obsolete
insert.AddColumn("stringColumn", "aSQLValue", (ILiteralType)NHibernateUtil.String);
#pragma warning restore CS0618 // Type or member is obsolete
insert.SetComment("Test insert");
string expectedSql =
"/* Test insert */ INSERT INTO test_insert_builder (stringColumn) VALUES ('aSQLValue')";
Expand All @@ -71,7 +75,9 @@ public void MixingParametersAndValues()

insert.SetTableName("test_insert_builder");

#pragma warning disable CS0618 // Type or member is obsolete
insert.AddColumn("literalColumn", false, (ILiteralType)NHibernateUtil.Boolean);
#pragma warning restore CS0618 // Type or member is obsolete
insert.AddColumn("intColumn", NHibernateUtil.Int32);
insert.AddColumn("stringColumn", 5.ToString());
insert.AddColumn("longColumn", NHibernateUtil.Int64);
Expand All @@ -89,4 +95,4 @@ public void MixingParametersAndValues()
Assert.AreEqual(SqlTypeFactory.Int64, actualParameterTypes[1], "Second Parameter Type");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public void UpdateStringSqlTest()

update.AddColumns(new string[] {"intColumn"}, NHibernateUtil.Int32);
update.AddColumns(new string[] {"longColumn"}, NHibernateUtil.Int64);
#pragma warning disable CS0618 // Type or member is obsolete
update.AddColumn("literalColumn", false, (ILiteralType) NHibernateUtil.Boolean);
#pragma warning restore CS0618 // Type or member is obsolete
update.AddColumn("stringColumn", 5.ToString());

update.SetIdentityColumn(new string[] {"decimalColumn"}, NHibernateUtil.Decimal);
Expand Down Expand Up @@ -60,4 +62,4 @@ public void UpdateStringSqlTest()
Assert.AreEqual(expectedParameterTypes[3], actualParameterTypes[3], "fourthParam Type");
}
}
}
}
2 changes: 2 additions & 0 deletions src/NHibernate/SqlCommand/SqlInsertBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public virtual SqlInsertBuilder AddColumn(string columnName, IType propertyType)
/// <param name="val">The value to set for the column.</param>
/// <param name="literalType">The NHibernateType to use to convert the value to a sql string.</param>
/// <returns>The SqlInsertBuilder.</returns>
// Since v5.6
[Obsolete("This method is unsafe and has no more usages. Use the overload with a property type and use a parameterized query.")]
public SqlInsertBuilder AddColumn(string columnName, object val, ILiteralType literalType)
{
return AddColumn(columnName, literalType.ObjectToSQLString(val, Dialect));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As noted by @gliljas in #3516, these methods implementations are unsafe and may lead to SQL injections or invalid SQL. Since these AddColumn overloads have no usage, better just obsolete them. Adding a literal value in a SQL query as a string fragment is anyway a bad practice. Parameterization should be used instead.

Expand Down
2 changes: 2 additions & 0 deletions src/NHibernate/SqlCommand/SqlUpdateBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public SqlUpdateBuilder SetComment(string comment)
/// <param name="val">The value to set for the column.</param>
/// <param name="literalType">The NHibernateType to use to convert the value to a sql string.</param>
/// <returns>The SqlUpdateBuilder.</returns>
// Since v5.6
[Obsolete("This method is unsafe and has no more usages. Use the overload with a property type and use a parameterized query.")]
public SqlUpdateBuilder AddColumn(string columnName, object val, ILiteralType literalType)
{
return AddColumn(columnName, literalType.ObjectToSQLString(val, Dialect));
Expand Down