Skip to content

Commit d7ee3fa

Browse files
Obsolete literal AddColumn
SqlInsert/UpdateBuilder AddColumn overloads taking a value have a SQL injection vulnerability, and have no usage.
1 parent 1605507 commit d7ee3fa

File tree

4 files changed

+15
-18
lines changed

4 files changed

+15
-18
lines changed

src/NHibernate.Test/SqlCommandTest/SqlInsertBuilderFixture.cs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using NHibernate.Engine;
33
using NHibernate.SqlCommand;
44
using NHibernate.SqlTypes;
5-
using NHibernate.Type;
65
using NUnit.Framework;
76

87
namespace NHibernate.Test.SqlCommandTest
@@ -26,15 +25,13 @@ public void InsertSqlStringTest()
2625

2726
insert.AddColumn("intColumn", NHibernateUtil.Int32);
2827
insert.AddColumn("longColumn", NHibernateUtil.Int64);
29-
insert.AddColumn("literalColumn", false, (ILiteralType) NHibernateUtil.Boolean);
3028
insert.AddColumn("stringColumn", 5.ToString());
3129

3230
SqlCommandInfo sqlCommand = insert.ToSqlCommandInfo();
3331
SqlType[] actualParameterTypes = sqlCommand.ParameterTypes;
3432

35-
string falseString = factoryImpl.Dialect.ToBooleanValueString(false);
3633
string expectedSql =
37-
"INSERT INTO test_insert_builder (intColumn, longColumn, literalColumn, stringColumn) VALUES (?, ?, " + falseString + ", 5)";
34+
"INSERT INTO test_insert_builder (intColumn, longColumn, stringColumn) VALUES (?, ?, 5)";
3835
Assert.AreEqual(expectedSql, sqlCommand.Text.ToString(), "SQL String");
3936

4037
Assert.AreEqual(2, actualParameterTypes.Length);
@@ -48,15 +45,15 @@ public void Commented()
4845
Configuration cfg = new Configuration();
4946
ISessionFactory factory = cfg.BuildSessionFactory();
5047

51-
ISessionFactoryImplementor factoryImpl = (ISessionFactoryImplementor)factory;
48+
ISessionFactoryImplementor factoryImpl = (ISessionFactoryImplementor) factory;
5249
SqlInsertBuilder insert = new SqlInsertBuilder(factoryImpl);
5350

5451
insert.SetTableName("test_insert_builder");
5552

56-
insert.AddColumn("stringColumn", "aSQLValue", (ILiteralType)NHibernateUtil.String);
53+
insert.AddColumn("intColumn", NHibernateUtil.Int32);
5754
insert.SetComment("Test insert");
5855
string expectedSql =
59-
"/* Test insert */ INSERT INTO test_insert_builder (stringColumn) VALUES ('aSQLValue')";
56+
"/* Test insert */ INSERT INTO test_insert_builder (intColumn) VALUES (?)";
6057
Assert.AreEqual(expectedSql, insert.ToSqlString().ToString(), "SQL String");
6158
}
6259

@@ -66,27 +63,25 @@ public void MixingParametersAndValues()
6663
Configuration cfg = new Configuration();
6764
ISessionFactory factory = cfg.BuildSessionFactory();
6865

69-
ISessionFactoryImplementor factoryImpl = (ISessionFactoryImplementor)factory;
66+
ISessionFactoryImplementor factoryImpl = (ISessionFactoryImplementor) factory;
7067
SqlInsertBuilder insert = new SqlInsertBuilder(factoryImpl);
7168

7269
insert.SetTableName("test_insert_builder");
7370

74-
insert.AddColumn("literalColumn", false, (ILiteralType)NHibernateUtil.Boolean);
7571
insert.AddColumn("intColumn", NHibernateUtil.Int32);
7672
insert.AddColumn("stringColumn", 5.ToString());
7773
insert.AddColumn("longColumn", NHibernateUtil.Int64);
7874

7975
SqlCommandInfo sqlCommand = insert.ToSqlCommandInfo();
8076
SqlType[] actualParameterTypes = sqlCommand.ParameterTypes;
8177

82-
string falseString = factoryImpl.Dialect.ToBooleanValueString(false);
83-
string expectedSql =
84-
"INSERT INTO test_insert_builder (literalColumn, intColumn, stringColumn, longColumn) VALUES (" + falseString + ", ?, 5, ?)";
78+
string expectedSql =
79+
"INSERT INTO test_insert_builder (intColumn, stringColumn, longColumn) VALUES (?, 5, ?)";
8580
Assert.AreEqual(expectedSql, sqlCommand.Text.ToString(), "SQL String");
8681

8782
Assert.AreEqual(2, actualParameterTypes.Length);
8883
Assert.AreEqual(SqlTypeFactory.Int32, actualParameterTypes[0], "First Parameter Type");
89-
Assert.AreEqual(SqlTypeFactory.Int64, actualParameterTypes[1], "Second Parameter Type");
84+
Assert.AreEqual(SqlTypeFactory.Int64, actualParameterTypes[1], "Second Parameter Type");
9085
}
9186
}
92-
}
87+
}

src/NHibernate.Test/SqlCommandTest/SqlUpdateBuilderFixture.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public void UpdateStringSqlTest()
2828

2929
update.AddColumns(new string[] {"intColumn"}, NHibernateUtil.Int32);
3030
update.AddColumns(new string[] {"longColumn"}, NHibernateUtil.Int64);
31-
update.AddColumn("literalColumn", false, (ILiteralType) NHibernateUtil.Boolean);
3231
update.AddColumn("stringColumn", 5.ToString());
3332

3433
update.SetIdentityColumn(new string[] {"decimalColumn"}, NHibernateUtil.Decimal);
@@ -38,9 +37,8 @@ public void UpdateStringSqlTest()
3837
SqlCommandInfo sqlCommand = update.ToSqlCommandInfo();
3938

4039
Assert.AreEqual(CommandType.Text, sqlCommand.CommandType);
41-
string falseString = factoryImpl.Dialect.ToBooleanValueString(false);
4240
string expectedSql =
43-
"UPDATE test_update_builder SET intColumn = ?, longColumn = ?, literalColumn = " + falseString + ", stringColumn = 5 WHERE decimalColumn = ? AND versionColumn = ? AND a=b";
41+
"UPDATE test_update_builder SET intColumn = ?, longColumn = ?, stringColumn = 5 WHERE decimalColumn = ? AND versionColumn = ? AND a=b";
4442
Assert.AreEqual(expectedSql, sqlCommand.Text.ToString(), "SQL String");
4543

4644
SqlType[] actualParameterTypes = sqlCommand.ParameterTypes;
@@ -60,4 +58,4 @@ public void UpdateStringSqlTest()
6058
Assert.AreEqual(expectedParameterTypes[3], actualParameterTypes[3], "fourthParam Type");
6159
}
6260
}
63-
}
61+
}

src/NHibernate/SqlCommand/SqlInsertBuilder.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ public virtual SqlInsertBuilder AddColumn(string columnName, IType propertyType)
6666
/// <param name="val">The value to set for the column.</param>
6767
/// <param name="literalType">The NHibernateType to use to convert the value to a sql string.</param>
6868
/// <returns>The SqlInsertBuilder.</returns>
69+
// Since v5.6
70+
[Obsolete("This method is unsafe and has no more usages. Use the overload with a property type and use a parameterized query.")]
6971
public SqlInsertBuilder AddColumn(string columnName, object val, ILiteralType literalType)
7072
{
7173
return AddColumn(columnName, literalType.ObjectToSQLString(val, Dialect));

src/NHibernate/SqlCommand/SqlUpdateBuilder.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public SqlUpdateBuilder SetComment(string comment)
4747
/// <param name="val">The value to set for the column.</param>
4848
/// <param name="literalType">The NHibernateType to use to convert the value to a sql string.</param>
4949
/// <returns>The SqlUpdateBuilder.</returns>
50+
// Since v5.6
51+
[Obsolete("This method is unsafe and has no more usages. Use the overload with a property type and use a parameterized query.")]
5052
public SqlUpdateBuilder AddColumn(string columnName, object val, ILiteralType literalType)
5153
{
5254
return AddColumn(columnName, literalType.ObjectToSQLString(val, Dialect));

0 commit comments

Comments
 (0)