Browse files

Merge pull request #176 from mongodb/raise-error-on-system-insert

Raise error on insert into system collection.
  • Loading branch information...
2 parents fedbb1e + c39c2ab commit e7b70cdf15604408f5b002f30943b8d77211b01f @durran durran committed Mar 15, 2013
Showing with 20 additions and 4 deletions.
  1. +7 −4 lib/mongo/collection.rb
  2. +13 −0 test/functional/collection_test.rb
View
11 lib/mongo/collection.rb
@@ -313,10 +313,10 @@ def find_one(spec_or_object_id=nil, opts={})
# @return [ObjectId] the _id of the saved document.
#
# @option opts [Hash] :w, :j, :wtimeout, :fsync Set the write concern for this operation.
- # :w > 0 will run a +getlasterror+ command on the database to report any assertion.
+ # :w > 0 will run a +getlasterror+ command on the database to report any assertion.
# :j will confirm a write has been committed to the journal,
# :wtimeout specifies how long to wait for write confirmation,
- # :fsync will confirm that a write has been fsynced.
+ # :fsync will confirm that a write has been fsynced.
# Options provided here will override any write concern options set on this collection,
# its database object, or the current connection. See the options
# for DB#get_last_error.
@@ -349,10 +349,10 @@ def save(doc, opts={})
# @option opts [Boolean] :j (false) Set journal acknowledgement
# @option opts [Integer] :wtimeout (nil) Set replica set acknowledgement timeout
# @option opts [Boolean] :fsync (false) Set fsync acknowledgement.
- #
+ #
# Notes on write concern:
# Options provided here will override any write concern options set on this collection,
- # its database object, or the current connection. See the options for +DB#get_last_error+.
+ # its database object, or the current connection. See the options for +DB#get_last_error+.
#
# @option opts [Boolean] :continue_on_error (+false+) If true, then
# continue a bulk insert even if one of the documents inserted
@@ -369,6 +369,9 @@ def save(doc, opts={})
#
# @core insert insert-instance_method
def insert(doc_or_docs, opts={})
+ if name.start_with?("system.") && name !~ /(\Asystem\.users)|(\Asystem\.indexes)/
+ raise Mongo::InvalidNSName, "cannot insert into system collections."
+ end
doc_or_docs = [doc_or_docs] unless doc_or_docs.is_a?(Array)
doc_or_docs.collect! { |doc| @pk_factory.create_pk(doc) }
write_concern = get_write_concern(opts, self)
View
13 test/functional/collection_test.rb
@@ -1075,6 +1075,19 @@ def test_ensure_index_timeout
coll.ensure_index([['a', 1]])
end
+ def test_insert_raises_exception_on_system_collection
+ collection = @@db["system.foo"]
+ assert_raise Mongo::InvalidNSName do
+ collection.insert({ :a => 1 })
+ end
+ end
+
+ def test_save_raises_exception_on_system_collection
+ collection = @@db["system.foo"]
+ assert_raise Mongo::InvalidNSName do
+ collection.save({ :a => 1 })
+ end
+ end
if @@version > '2.0.0'
def test_show_disk_loc

0 comments on commit e7b70cd

Please sign in to comment.