Skip to content

Commit bf88006

Browse files
Add test for char
1 parent ff80d45 commit bf88006

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

src/NHibernate.Test/Async/NHSpecificTest/GH3516/FixtureByCode.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010

1111
using System;
12+
using System.Collections.Generic;
1213
using NHibernate.Cfg.MappingSchema;
1314
using NHibernate.Mapping.ByCode;
1415
using NUnit.Framework;
@@ -26,6 +27,7 @@ protected override HbmMapping GetMappings()
2627
{
2728
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
2829
rc.Property(x => x.Name);
30+
rc.Property(x => x.Initial);
2931
});
3032
return mapper.CompileMappingForAllExplicitlyAddedEntities();
3133
}
@@ -34,9 +36,9 @@ protected override void OnSetUp()
3436
{
3537
using var session = OpenSession();
3638
using var transaction = session.BeginTransaction();
37-
var e = new Entity { Name = Entity.NameWithSingleQuote };
39+
var e = new Entity { Name = Entity.NameWithSingleQuote, Initial = Entity.QuoteInitial };
3840
session.Save(e);
39-
e = new Entity { Name = Entity.NameWithEscapedSingleQuote };
41+
e = new Entity { Name = Entity.NameWithEscapedSingleQuote, Initial = Entity.BackslashInitial };
4042
session.Save(e);
4143

4244
transaction.Commit();
@@ -132,5 +134,22 @@ public async Task StringsWithSpecialCharactersAsync(string name)
132134
Assert.That(all, Has.Count.GreaterThan(0));
133135
}
134136
}
137+
138+
private static readonly string[] _charInjectionsProperties =
139+
new[]
140+
{
141+
nameof(Entity.QuoteInitial),
142+
nameof(Entity.BackslashInitial)
143+
};
144+
145+
[TestCaseSource(nameof(_charInjectionsProperties))]
146+
public void SqlInjectionInCharAsync(string propertyName)
147+
{
148+
using var session = OpenSession();
149+
var query = session.CreateQuery($"from Entity e where e.Initial = Entity.{propertyName}");
150+
IList<Entity> list = null;
151+
Assert.That(async () => list = await (query.ListAsync<Entity>()), Throws.Nothing);
152+
Assert.That(list, Is.Not.Null.And.Count.EqualTo(1), $"Unable to find entity with initial {propertyName}");
153+
}
135154
}
136155
}

src/NHibernate.Test/NHSpecificTest/GH3516/Entity.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ public class Entity
66
{
77
public virtual Guid Id { get; set; }
88
public virtual string Name { get; set; }
9+
public virtual char Initial { get; set; }
10+
11+
public const char QuoteInitial = '\'';
12+
13+
public const char BackslashInitial = '\\';
914

1015
public const string NameWithSingleQuote = "'; drop table Entity; --";
1116

src/NHibernate.Test/NHSpecificTest/GH3516/FixtureByCode.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using NHibernate.Cfg.MappingSchema;
34
using NHibernate.Mapping.ByCode;
45
using NUnit.Framework;
@@ -15,6 +16,7 @@ protected override HbmMapping GetMappings()
1516
{
1617
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
1718
rc.Property(x => x.Name);
19+
rc.Property(x => x.Initial);
1820
});
1921
return mapper.CompileMappingForAllExplicitlyAddedEntities();
2022
}
@@ -23,9 +25,9 @@ protected override void OnSetUp()
2325
{
2426
using var session = OpenSession();
2527
using var transaction = session.BeginTransaction();
26-
var e = new Entity { Name = Entity.NameWithSingleQuote };
28+
var e = new Entity { Name = Entity.NameWithSingleQuote, Initial = Entity.QuoteInitial };
2729
session.Save(e);
28-
e = new Entity { Name = Entity.NameWithEscapedSingleQuote };
30+
e = new Entity { Name = Entity.NameWithEscapedSingleQuote, Initial = Entity.BackslashInitial };
2931
session.Save(e);
3032

3133
transaction.Commit();
@@ -121,5 +123,22 @@ public void StringsWithSpecialCharacters(string name)
121123
Assert.That(all, Has.Count.GreaterThan(0));
122124
}
123125
}
126+
127+
private static readonly string[] _charInjectionsProperties =
128+
new[]
129+
{
130+
nameof(Entity.QuoteInitial),
131+
nameof(Entity.BackslashInitial)
132+
};
133+
134+
[TestCaseSource(nameof(_charInjectionsProperties))]
135+
public void SqlInjectionInChar(string propertyName)
136+
{
137+
using var session = OpenSession();
138+
var query = session.CreateQuery($"from Entity e where e.Initial = Entity.{propertyName}");
139+
IList<Entity> list = null;
140+
Assert.That(() => list = query.List<Entity>(), Throws.Nothing);
141+
Assert.That(list, Is.Not.Null.And.Count.EqualTo(1), $"Unable to find entity with initial {propertyName}");
142+
}
124143
}
125144
}

0 commit comments

Comments
 (0)