Skip to content

Commit 528536b

Browse files
Add a test for special characters
1 parent 589a7b3 commit 528536b

File tree

3 files changed

+129
-1
lines changed

3 files changed

+129
-1
lines changed

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

+63
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
//------------------------------------------------------------------------------
99

1010

11+
using System;
1112
using NHibernate.Cfg.MappingSchema;
1213
using NHibernate.Mapping.ByCode;
1314
using NUnit.Framework;
@@ -61,5 +62,67 @@ public async Task SqlInjectionInStringsAsync()
6162
list = await (session.CreateQuery("from Entity e where e.Name = Entity.NameWithEscapedSingleQuote").ListAsync<Entity>());
6263
Assert.That(list, Has.Count.EqualTo(1), $"Unable to find entity with name {nameof(Entity.NameWithEscapedSingleQuote)}");
6364
}
65+
66+
private static readonly string[] _specialNames =
67+
new[]
68+
{
69+
"\0; drop table Entity; --",
70+
"\b; drop table Entity; --",
71+
"\n; drop table Entity; --",
72+
"\r; drop table Entity; --",
73+
"\t; drop table Entity; --",
74+
"\x1A; drop table Entity; --",
75+
"\"; drop table Entity; --",
76+
"\\; drop table Entity; --"
77+
};
78+
79+
[TestCaseSource(nameof(_specialNames))]
80+
public async Task StringsWithSpecialCharactersAsync(string name)
81+
{
82+
// We may not even be able to insert the entity.
83+
var wasInserted = false;
84+
try
85+
{
86+
using var s = OpenSession();
87+
using var t = s.BeginTransaction();
88+
var e = new Entity { Name = name };
89+
await (s.SaveAsync(e));
90+
await (t.CommitAsync());
91+
92+
wasInserted = true;
93+
}
94+
catch (Exception e)
95+
{
96+
Assert.Warn($"The entity insertion failed with message {e}");
97+
}
98+
99+
using var session = OpenSession();
100+
Entity.NameWithPotentiallyTroublesomeCharacters = name;
101+
try
102+
{
103+
var list = await (session.CreateQuery("from Entity e where e.Name = Entity.NameWithPotentiallyTroublesomeCharacters").ListAsync<Entity>());
104+
if (wasInserted && list.Count != 1)
105+
Assert.Warn($"Unable to find entity with name {nameof(Entity.NameWithPotentiallyTroublesomeCharacters)}");
106+
}
107+
catch (Exception e)
108+
{
109+
Assert.Warn($"The query has failed with message {e}");
110+
}
111+
112+
// Check the db is not wrecked.
113+
if (wasInserted)
114+
{
115+
var list = await (session
116+
.CreateQuery("from Entity e where e.Name = :name")
117+
.SetString("name", name)
118+
.ListAsync<Entity>());
119+
Assert.That(list, Has.Count.EqualTo(1));
120+
}
121+
else
122+
{
123+
var all = await (session.CreateQuery("from Entity e").ListAsync<Entity>());
124+
Assert.That(all, Has.Count.GreaterThan(0));
125+
}
126+
}
64127
}
65128
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ public class Entity
1010
public const string NameWithSingleQuote = "'; drop table Entity; --";
1111

1212
public const string NameWithEscapedSingleQuote = @"\'; drop table Entity; --";
13+
14+
public static string NameWithPotentiallyTroublesomeCharacters;
1315
}
1416
}

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

+64-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using NHibernate.Cfg.MappingSchema;
1+
using System;
2+
using NHibernate.Cfg.MappingSchema;
23
using NHibernate.Mapping.ByCode;
34
using NUnit.Framework;
45

@@ -50,5 +51,67 @@ public void SqlInjectionInStrings()
5051
list = session.CreateQuery("from Entity e where e.Name = Entity.NameWithEscapedSingleQuote").List<Entity>();
5152
Assert.That(list, Has.Count.EqualTo(1), $"Unable to find entity with name {nameof(Entity.NameWithEscapedSingleQuote)}");
5253
}
54+
55+
private static readonly string[] _specialNames =
56+
new[]
57+
{
58+
"\0; drop table Entity; --",
59+
"\b; drop table Entity; --",
60+
"\n; drop table Entity; --",
61+
"\r; drop table Entity; --",
62+
"\t; drop table Entity; --",
63+
"\x1A; drop table Entity; --",
64+
"\"; drop table Entity; --",
65+
"\\; drop table Entity; --"
66+
};
67+
68+
[TestCaseSource(nameof(_specialNames))]
69+
public void StringsWithSpecialCharacters(string name)
70+
{
71+
// We may not even be able to insert the entity.
72+
var wasInserted = false;
73+
try
74+
{
75+
using var s = OpenSession();
76+
using var t = s.BeginTransaction();
77+
var e = new Entity { Name = name };
78+
s.Save(e);
79+
t.Commit();
80+
81+
wasInserted = true;
82+
}
83+
catch (Exception e)
84+
{
85+
Assert.Warn($"The entity insertion failed with message {e}");
86+
}
87+
88+
using var session = OpenSession();
89+
Entity.NameWithPotentiallyTroublesomeCharacters = name;
90+
try
91+
{
92+
var list = session.CreateQuery("from Entity e where e.Name = Entity.NameWithPotentiallyTroublesomeCharacters").List<Entity>();
93+
if (wasInserted && list.Count != 1)
94+
Assert.Warn($"Unable to find entity with name {nameof(Entity.NameWithPotentiallyTroublesomeCharacters)}");
95+
}
96+
catch (Exception e)
97+
{
98+
Assert.Warn($"The query has failed with message {e}");
99+
}
100+
101+
// Check the db is not wrecked.
102+
if (wasInserted)
103+
{
104+
var list = session
105+
.CreateQuery("from Entity e where e.Name = :name")
106+
.SetString("name", name)
107+
.List<Entity>();
108+
Assert.That(list, Has.Count.EqualTo(1));
109+
}
110+
else
111+
{
112+
var all = session.CreateQuery("from Entity e").List<Entity>();
113+
Assert.That(all, Has.Count.GreaterThan(0));
114+
}
115+
}
53116
}
54117
}

0 commit comments

Comments
 (0)