Skip to content

Commit f079965

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

File tree

3 files changed

+133
-1
lines changed

3 files changed

+133
-1
lines changed

Diff for: src/NHibernate.Test/Async/NHSpecificTest/GH3516/FixtureByCode.cs

+65
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,69 @@ 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+
try
100+
{
101+
using var session = OpenSession();
102+
Entity.NameWithPotentiallyTroublesomeCharacters = name;
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+
using var session = OpenSession();
116+
var list = await (session
117+
.CreateQuery("from Entity e where e.Name = :name")
118+
.SetString("name", name)
119+
.ListAsync<Entity>());
120+
Assert.That(list, Has.Count.EqualTo(1));
121+
}
122+
else
123+
{
124+
using var session = OpenSession();
125+
var all = await (session.CreateQuery("from Entity e").ListAsync<Entity>());
126+
Assert.That(all, Has.Count.GreaterThan(0));
127+
}
128+
}
64129
}
65130
}

Diff for: 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
}

Diff for: src/NHibernate.Test/NHSpecificTest/GH3516/FixtureByCode.cs

+66-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,69 @@ 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+
try
89+
{
90+
using var session = OpenSession();
91+
Entity.NameWithPotentiallyTroublesomeCharacters = name;
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+
using var session = OpenSession();
105+
var list = session
106+
.CreateQuery("from Entity e where e.Name = :name")
107+
.SetString("name", name)
108+
.List<Entity>();
109+
Assert.That(list, Has.Count.EqualTo(1));
110+
}
111+
else
112+
{
113+
using var session = OpenSession();
114+
var all = session.CreateQuery("from Entity e").List<Entity>();
115+
Assert.That(all, Has.Count.GreaterThan(0));
116+
}
117+
}
53118
}
54119
}

0 commit comments

Comments
 (0)