Skip to content

Commit ae7d3df

Browse files
rozzajyemin
authored andcommitted
Ensure document keys can't contain NULL character (JAVA-891)
1 parent 363401b commit ae7d3df

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

src/main/com/mongodb/DBCollection.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,10 +1444,12 @@ private void _checkValue(final Object value) {
14441444
* @exception IllegalArgumentException if the key is not valid.
14451445
*/
14461446
private void validateKey(String s ) {
1447+
if ( s.contains( "\0" ) )
1448+
throw new IllegalArgumentException( "Document field names can't have a NULL character. (Bad Key: '" + s + "')" );
14471449
if ( s.contains( "." ) )
1448-
throw new IllegalArgumentException( "fields stored in the db can't have . in them. (Bad Key: '" + s + "')" );
1450+
throw new IllegalArgumentException( "Document field names can't have a . in them. (Bad Key: '" + s + "')" );
14491451
if ( s.startsWith( "$" ) )
1450-
throw new IllegalArgumentException( "fields stored in the db can't start with '$' (Bad Key: '" + s + "')" );
1452+
throw new IllegalArgumentException( "Document field names can't start with '$' (Bad Key: '" + s + "')" );
14511453
}
14521454

14531455
/**

src/main/org/bson/BasicBSONEncoder.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ protected void _putObjectField( String name , Object val ){
201201
return;
202202

203203
if ( DEBUG ) System.out.println( "\t put thing : " + name );
204+
205+
if ( name.contains( "\0" ) )
206+
throw new IllegalArgumentException( "Document field names can't have a NULL character. (Bad Key: '" + name + "')" );
204207

205208
if ( name.equals( "$where") && val instanceof String ){
206209
_put( CODE , name );

src/test/com/mongodb/DBCollectionTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,26 @@ public void testDotKeysFail() {
346346
DBObject obj = BasicDBObjectBuilder.start().add("x",1).add("y",2).add("foo.bar","baz").get();
347347
c.insert(obj);
348348
}
349+
350+
@Test( expectedExceptions = IllegalArgumentException.class )
351+
public void testNullKeysFail() {
352+
DBCollection c = _db.getCollection("testnullkeysFail");
353+
c.drop();
354+
355+
DBObject obj = BasicDBObjectBuilder.start().add("x",1).add("y",2).add("foo\0bar","baz").get();
356+
c.insert(obj);
357+
}
358+
359+
@Test( expectedExceptions = IllegalArgumentException.class )
360+
public void testNullKeysFailWhenNested() {
361+
DBCollection c = _db.getCollection("testnullkeysFailWhenNested");
362+
c.drop();
363+
364+
final BasicDBList list = new BasicDBList();
365+
list.add(new BasicDBObject("foo\0bar","baz"));
366+
DBObject obj = BasicDBObjectBuilder.start().add("x", list).get();
367+
c.insert(obj);
368+
}
349369

350370
@Test
351371
public void testLazyDocKeysPass() {

src/test/org/bson/BSONTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ public void testBasic1()
8282

8383
_test( new BasicBSONObject( "x" , 4 ) , 12 , "d1ed8dbf79b78fa215e2ded74548d89d" );
8484
}
85+
86+
@Test( expectedExceptions = IllegalArgumentException.class )
87+
public void testNullKeysFail() {
88+
BSONEncoder e = new BasicBSONEncoder();
89+
OutputBuffer buf = new BasicOutputBuffer();
90+
e.set( buf );
91+
e.putObject( new BasicBSONObject( "foo\0bar","baz" ) );
92+
}
8593

8694
@Test
8795
public void testArray()

0 commit comments

Comments
 (0)