Skip to content

Commit a7fbdb3

Browse files
author
Robert Stam
committed
CSHARP-788: Strengthen checks for nulls in CStrings.
1 parent 1f2f648 commit a7fbdb3

File tree

9 files changed

+147
-0
lines changed

9 files changed

+147
-0
lines changed

MongoDB.Bson/IO/BsonBuffer.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,14 @@ public void WriteBytes(byte[] value)
554554
/// <param name="value">A string.</param>
555555
public void WriteCString(UTF8Encoding encoding, string value)
556556
{
557+
if (value == null)
558+
{
559+
throw new ArgumentNullException("value");
560+
}
561+
if (value.IndexOf('\0') != -1)
562+
{
563+
throw new ArgumentException("CStrings cannot contain nulls.", "value");
564+
}
557565
ThrowIfDisposed();
558566

559567
var maxLength = encoding.GetMaxByteCount(value.Length) + 1;

MongoDB.Bson/IO/BsonWriter.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,14 @@ public void WriteMinKey(string name)
495495
/// <param name="name">The name of the element.</param>
496496
public virtual void WriteName(string name)
497497
{
498+
if (name == null)
499+
{
500+
throw new ArgumentNullException("name");
501+
}
502+
if (name.IndexOf('\0') != -1)
503+
{
504+
throw new ArgumentException("Element names cannot contain nulls.", "name");
505+
}
498506
if (_disposed) { throw new ObjectDisposedException(this.GetType().Name); }
499507
if (_state != BsonWriterState.Name)
500508
{

MongoDB.Bson/Serialization/BsonMemberMap.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,10 @@ public BsonMemberMap SetElementName(string elementName)
374374
{
375375
throw new ArgumentNullException("elementName");
376376
}
377+
if (elementName.IndexOf('\0') != -1)
378+
{
379+
throw new ArgumentException("Element names cannot contain nulls.", "elementName");
380+
}
377381
if (_frozen) { ThrowFrozenException(); }
378382

379383
_elementName = elementName;

MongoDB.Bson/Serialization/Conventions/StandardDiscriminatorConvention.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ public abstract class StandardDiscriminatorConvention : IDiscriminatorConvention
3939
/// <param name="elementName">The element name.</param>
4040
protected StandardDiscriminatorConvention(string elementName)
4141
{
42+
if (elementName == null)
43+
{
44+
throw new ArgumentNullException("elementName");
45+
}
46+
if (elementName.IndexOf('\0') != -1)
47+
{
48+
throw new ArgumentException("Element names cannot contain nulls.", "elementName");
49+
}
4250
_elementName = elementName;
4351
}
4452

MongoDB.BsonUnitTests/IO/BsonBufferTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
* limitations under the License.
1414
*/
1515

16+
using System;
1617
using System.IO;
1718
using System.Text;
1819
using MongoDB.Bson;
20+
using MongoDB.Bson.IO;
1921
using MongoDB.Bson.Serialization;
2022
using NUnit.Framework;
2123

@@ -127,5 +129,23 @@ public void TestReadStringTwoCharactersDecoderException()
127129
Assert.AreEqual(15, bytes.Length);
128130
var ex = Assert.Throws<DecoderFallbackException>(() => { BsonSerializer.Deserialize<BsonDocument>(bytes); });
129131
}
132+
133+
[Test]
134+
public void TestWriteCStringThrowsWhenValueContainsNulls()
135+
{
136+
using (var bsonBuffer = new BsonBuffer())
137+
{
138+
Assert.Throws<ArgumentException>(() => { bsonBuffer.WriteCString((UTF8Encoding)Encoding.UTF8, "a\0b"); });
139+
}
140+
}
141+
142+
[Test]
143+
public void TestWriteCStringThrowsWhenValueIsNull()
144+
{
145+
using (var bsonBuffer = new BsonBuffer())
146+
{
147+
Assert.Throws<ArgumentNullException>(() => { bsonBuffer.WriteCString((UTF8Encoding)Encoding.UTF8, null); });
148+
}
149+
}
130150
}
131151
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* Copyright 2010-2013 10gen Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
using MongoDB.Bson.IO;
18+
using NUnit.Framework;
19+
20+
namespace MongoDB.BsonUnitTests.IO
21+
{
22+
[TestFixture]
23+
public class BsonWriterTests
24+
{
25+
[Test]
26+
public void TestWriteNameThrowsWhenValueContainsNulls()
27+
{
28+
using (var bsonWriter = BsonWriter.Create(BsonBinaryWriterSettings.Defaults))
29+
{
30+
Assert.Throws<ArgumentException>(() => { bsonWriter.WriteName("a\0b"); });
31+
}
32+
}
33+
34+
[Test]
35+
public void TestWriteNameThrowsWhenValueIsNull()
36+
{
37+
using (var bsonWriter = BsonWriter.Create(BsonBinaryWriterSettings.Defaults))
38+
{
39+
Assert.Throws<ArgumentNullException>(() => { bsonWriter.WriteName(null); });
40+
}
41+
}
42+
}
43+
}

MongoDB.BsonUnitTests/MongoDB.BsonUnitTests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
</Compile>
8282
<Compile Include="BsonExtensionMethodsTests.cs" />
8383
<Compile Include="BsonUtilsTests.cs" />
84+
<Compile Include="IO\BsonWriterTests.cs" />
8485
<Compile Include="IO\ByteArrayBufferTests.cs" />
8586
<Compile Include="IO\MultiChunkBufferTests.cs" />
8687
<Compile Include="Jira\CSharp728Tests.cs" />
@@ -118,6 +119,7 @@
118119
<Compile Include="Serialization\Conventions\NamedIdConventionsTests.cs" />
119120
<Compile Include="Serialization\Conventions\PropertyFinderConventionsTests.cs" />
120121
<Compile Include="Serialization\Conventions\ReadWriteMemberFinderConventionsTests.cs" />
122+
<Compile Include="Serialization\Conventions\StandardDiscriminatorConventionTests.cs" />
121123
<Compile Include="Serialization\Conventions\StringObjectIdGeneratorConventionsTests.cs" />
122124
<Compile Include="Serialization\IdGenerators\AscendingGuidGeneratorTests.cs" />
123125
<Compile Include="Serialization\IdGenerators\CombGuidGeneratorTests.cs" />

MongoDB.BsonUnitTests/Serialization/BsonMemberMapTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16+
using System;
1617
using MongoDB.Bson;
1718
using MongoDB.Bson.Serialization;
1819
using MongoDB.Bson.Serialization.IdGenerators;
@@ -68,6 +69,22 @@ public void TestIsReadOnlyPropertyOfAField()
6869
Assert.IsFalse(memberMap.IsReadOnly);
6970
}
7071

72+
[Test]
73+
public void TestSetElementNameThrowsWhenElementNameContainsNulls()
74+
{
75+
var classMap = new BsonClassMap<TestClass>(cm => cm.AutoMap());
76+
var memberMap = classMap.GetMemberMap("Property");
77+
Assert.Throws<ArgumentException>(() => { memberMap.SetElementName("a\0b"); });
78+
}
79+
80+
[Test]
81+
public void TestSetElementNameThrowsWhenElementNameIsNull()
82+
{
83+
var classMap = new BsonClassMap<TestClass>(cm => cm.AutoMap());
84+
var memberMap = classMap.GetMemberMap("Property");
85+
Assert.Throws<ArgumentNullException>(() => { memberMap.SetElementName(null); });
86+
}
87+
7188
[Test]
7289
public void TestSettingAField()
7390
{
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* Copyright 2010-2013 10gen Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
using MongoDB.Bson.Serialization.Conventions;
18+
using NUnit.Framework;
19+
20+
namespace MongoDB.BsonUnitTests.Serialization.Conventions
21+
{
22+
[TestFixture]
23+
public class StandardDiscriminatorConventionTests
24+
{
25+
[Test]
26+
public void TestConstructorThrowsWhenElementNameContainsNulls()
27+
{
28+
Assert.Throws<ArgumentException>(() => { var discriminatorConvention = new ScalarDiscriminatorConvention("a\0b"); });
29+
}
30+
31+
[Test]
32+
public void TestConstructorThrowsWhenElementNameIsNull()
33+
{
34+
Assert.Throws<ArgumentNullException>(() => { var discriminatorConvention = new ScalarDiscriminatorConvention(null); });
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)