|
8 | 8 | //------------------------------------------------------------------------------
|
9 | 9 |
|
10 | 10 |
|
| 11 | +using System; |
11 | 12 | using NHibernate.Cfg.MappingSchema;
|
12 | 13 | using NHibernate.Mapping.ByCode;
|
13 | 14 | using NUnit.Framework;
|
@@ -61,5 +62,69 @@ public async Task SqlInjectionInStringsAsync()
|
61 | 62 | list = await (session.CreateQuery("from Entity e where e.Name = Entity.NameWithEscapedSingleQuote").ListAsync<Entity>());
|
62 | 63 | Assert.That(list, Has.Count.EqualTo(1), $"Unable to find entity with name {nameof(Entity.NameWithEscapedSingleQuote)}");
|
63 | 64 | }
|
| 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 | + } |
64 | 129 | }
|
65 | 130 | }
|
0 commit comments