Skip to content

Commit 49ea5e3

Browse files
Add injection test cases
1 parent 92097cd commit 49ea5e3

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using NHibernate.Cfg.MappingSchema;
12+
using NHibernate.Mapping.ByCode;
13+
using NUnit.Framework;
14+
15+
namespace NHibernate.Test.NHSpecificTest.GH3516
16+
{
17+
using System.Threading.Tasks;
18+
[TestFixture]
19+
public class FixtureByCodeAsync : TestCaseMappingByCode
20+
{
21+
protected override HbmMapping GetMappings()
22+
{
23+
var mapper = new ModelMapper();
24+
mapper.Class<Entity>(rc =>
25+
{
26+
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
27+
rc.Property(x => x.Name);
28+
});
29+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
30+
}
31+
32+
protected override void OnSetUp()
33+
{
34+
using var session = OpenSession();
35+
using var transaction = session.BeginTransaction();
36+
var e = new Entity { Name = Entity.NameWithSingleQuote };
37+
session.Save(e);
38+
e = new Entity { Name = Entity.NameWithEscapedSingleQuote };
39+
session.Save(e);
40+
41+
transaction.Commit();
42+
}
43+
44+
protected override void OnTearDown()
45+
{
46+
using var session = OpenSession();
47+
using var transaction = session.BeginTransaction();
48+
session.CreateQuery("delete from System.Object").ExecuteUpdate();
49+
50+
transaction.Commit();
51+
}
52+
53+
[Test]
54+
public async Task SqlInjectionInStringsAsync()
55+
{
56+
using var session = OpenSession();
57+
58+
var list = await (session.CreateQuery("from Entity e where e.Name = Entity.NameWithSingleQuote").ListAsync<Entity>());
59+
Assert.That(list, Has.Count.EqualTo(1), $"Unable to find entity with name {nameof(Entity.NameWithSingleQuote)}");
60+
61+
list = await (session.CreateQuery("from Entity e where e.Name = Entity.NameWithEscapedSingleQuote").ListAsync<Entity>());
62+
Assert.That(list, Has.Count.EqualTo(1), $"Unable to find entity with name {nameof(Entity.NameWithEscapedSingleQuote)}");
63+
}
64+
}
65+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
3+
namespace NHibernate.Test.NHSpecificTest.GH3516
4+
{
5+
public class Entity
6+
{
7+
public virtual Guid Id { get; set; }
8+
public virtual string Name { get; set; }
9+
10+
public const string NameWithSingleQuote = "'; drop table Entity; --";
11+
12+
public const string NameWithEscapedSingleQuote = @"\'; drop table Entity; --";
13+
}
14+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using NHibernate.Cfg.MappingSchema;
2+
using NHibernate.Mapping.ByCode;
3+
using NUnit.Framework;
4+
5+
namespace NHibernate.Test.NHSpecificTest.GH3516
6+
{
7+
[TestFixture]
8+
public class FixtureByCode : TestCaseMappingByCode
9+
{
10+
protected override HbmMapping GetMappings()
11+
{
12+
var mapper = new ModelMapper();
13+
mapper.Class<Entity>(rc =>
14+
{
15+
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
16+
rc.Property(x => x.Name);
17+
});
18+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
19+
}
20+
21+
protected override void OnSetUp()
22+
{
23+
using var session = OpenSession();
24+
using var transaction = session.BeginTransaction();
25+
var e = new Entity { Name = Entity.NameWithSingleQuote };
26+
session.Save(e);
27+
e = new Entity { Name = Entity.NameWithEscapedSingleQuote };
28+
session.Save(e);
29+
30+
transaction.Commit();
31+
}
32+
33+
protected override void OnTearDown()
34+
{
35+
using var session = OpenSession();
36+
using var transaction = session.BeginTransaction();
37+
session.CreateQuery("delete from System.Object").ExecuteUpdate();
38+
39+
transaction.Commit();
40+
}
41+
42+
[Test]
43+
public void SqlInjectionInStrings()
44+
{
45+
using var session = OpenSession();
46+
47+
var list = session.CreateQuery("from Entity e where e.Name = Entity.NameWithSingleQuote").List<Entity>();
48+
Assert.That(list, Has.Count.EqualTo(1), $"Unable to find entity with name {nameof(Entity.NameWithSingleQuote)}");
49+
50+
list = session.CreateQuery("from Entity e where e.Name = Entity.NameWithEscapedSingleQuote").List<Entity>();
51+
Assert.That(list, Has.Count.EqualTo(1), $"Unable to find entity with name {nameof(Entity.NameWithEscapedSingleQuote)}");
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)