From d60aa255e3e54ea385dffb6a0c8d4889144273f3 Mon Sep 17 00:00:00 2001 From: Eliot Horowitz Date: Thu, 30 Dec 2010 23:59:01 -0500 Subject: [PATCH] don't allow empty collection names SERVER-2314 --- db/pdfile.cpp | 17 ++++++++++++++--- db/pdfile.h | 2 ++ dbtests/basictests.cpp | 10 ++++++++++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/db/pdfile.cpp b/db/pdfile.cpp index 1129aad3fb7..ce7b3e55568 100644 --- a/db/pdfile.cpp +++ b/db/pdfile.cpp @@ -186,7 +186,7 @@ namespace mongo { return false; } - log(1) << "create collection " << ns << ' ' << options << '\n'; + log(1) << "create collection " << ns << ' ' << options << endl; /* todo: do this only when we have allocated space successfully? or we could insert with a { ok: 0 } field and then go back and set to ok : 1 after we are done. @@ -1444,7 +1444,7 @@ namespace mongo { DiskLoc DataFileMgr::insert(const char *ns, const void *obuf, int len, bool god, const BSONElement &writeId, bool mayAddIndex) { bool wouldAddIndex = false; massert( 10093 , "cannot insert into reserved $ collection", god || isANormalNSName( ns ) ); - uassert( 10094 , "invalid ns", strchr( ns , '.' ) > 0 ); + uassert( 10094 , str::stream() << "invalid ns: " << ns , isValidNS( ns ) ); const char *sys = strstr(ns, "system."); if ( sys ) { uassert( 10095 , "attempt to insert in reserved database name 'system'", sys != ns); @@ -1961,7 +1961,7 @@ namespace mongo { BOOST_CHECK_EXCEPTION( ok = fo.apply(q) ); if ( ok ) { if ( extra != 10 ){ - log(1) << fo.op() << " file " << q.string() << '\n'; + log(1) << fo.op() << " file " << q.string() << endl; log() << " _applyOpToDataFiles() warning: extra == " << extra << endl; } } @@ -2013,6 +2013,17 @@ namespace mongo { return true; } + + bool isValidNS( const StringData& ns ){ + // TODO: should check for invalid characters + + const char * x = strchr( ns.data() , '.' ); + if ( ! x ) + return false; + + x++; + return *x > 0; + } } // namespace mongo diff --git a/db/pdfile.h b/db/pdfile.h index b218f645d5e..ed8406c0ee7 100644 --- a/db/pdfile.h +++ b/db/pdfile.h @@ -55,6 +55,8 @@ namespace mongo { // -1 if library unavailable. boost::intmax_t freeSpace( const string &path = dbpath ); + bool isValidNS( const StringData& ns ); + /*---------------------------------------------------------------------*/ class MongoDataFile { diff --git a/dbtests/basictests.cpp b/dbtests/basictests.cpp index 82db47adcad..a417f05b28c 100644 --- a/dbtests/basictests.cpp +++ b/dbtests/basictests.cpp @@ -425,6 +425,14 @@ namespace BasicTests { } }; + class NSValidNames { + public: + void run(){ + ASSERT( isValidNS( "test.foo" ) ); + ASSERT( ! isValidNS( "test." ) ); + ASSERT( ! isValidNS( "test" ) ); + } + }; class PtrTests { public: @@ -592,6 +600,8 @@ namespace BasicTests { add< DatabaseValidNames >(); add< DatabaseOwnsNS >(); + add< NSValidNames >(); + add< PtrTests >(); add< StringSplitterTest >();