Skip to content

Commit

Permalink
Obsolete literal AddColumn
Browse files Browse the repository at this point in the history
SqlInsert/UpdateBuilder AddColumn overloads taking a value have a SQL injection vulnerability, and have no usage.
  • Loading branch information
fredericDelaporte committed May 9, 2024
1 parent b3256e7 commit 6bd6bd4
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
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));
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

0 comments on commit 6bd6bd4

Please sign in to comment.