-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathUnsafeSQL.aspx.cs
35 lines (29 loc) · 1.22 KB
/
UnsafeSQL.aspx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using NHibernate;
using NHibernate.Cfg;
using System;
using System.Collections.Generic;
namespace DotNetUnitTests.TestCases.HQLTestCases
{
public partial class UnsafeSQL : HQLTestCasePage
{
/**
* SELECT: Unsafe when Using String Concatenation on Custom SQL Queries (CreateSQLQuery) Example
* By doing string concatenation in the CreateSQLQuery method, the SQL query is vulnerable to injection.
*/
protected void Page_Load(object sender, EventArgs e)
{
bool expectedSafe = false;
// creating the database session
ISessionFactory sessionFactory = new Configuration().Configure().BuildSessionFactory();
ISession session = sessionFactory.OpenSession();
// creating and receiving the results of the custom SQL query
ISQLQuery query = session.CreateSQLQuery("SELECT * FROM Student WHERE FirstName = '" + hqlText + "';"); // unsafe!
query.AddEntity(typeof(Student));
IList<Student> students = query.List<Student>();
// testing the result
TestResults(students, hqlText, expectedSafe);
session.Close();
sessionFactory.Close();
}
}
}