Skip to content

Commit e7b70cd

Browse files
committed
Merge pull request #176 from mongodb/raise-error-on-system-insert
Raise error on insert into system collection.
2 parents fedbb1e + c39c2ab commit e7b70cd

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

lib/mongo/collection.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,10 +313,10 @@ def find_one(spec_or_object_id=nil, opts={})
313313
# @return [ObjectId] the _id of the saved document.
314314
#
315315
# @option opts [Hash] :w, :j, :wtimeout, :fsync Set the write concern for this operation.
316-
# :w > 0 will run a +getlasterror+ command on the database to report any assertion.
316+
# :w > 0 will run a +getlasterror+ command on the database to report any assertion.
317317
# :j will confirm a write has been committed to the journal,
318318
# :wtimeout specifies how long to wait for write confirmation,
319-
# :fsync will confirm that a write has been fsynced.
319+
# :fsync will confirm that a write has been fsynced.
320320
# Options provided here will override any write concern options set on this collection,
321321
# its database object, or the current connection. See the options
322322
# for DB#get_last_error.
@@ -349,10 +349,10 @@ def save(doc, opts={})
349349
# @option opts [Boolean] :j (false) Set journal acknowledgement
350350
# @option opts [Integer] :wtimeout (nil) Set replica set acknowledgement timeout
351351
# @option opts [Boolean] :fsync (false) Set fsync acknowledgement.
352-
#
352+
#
353353
# Notes on write concern:
354354
# Options provided here will override any write concern options set on this collection,
355-
# its database object, or the current connection. See the options for +DB#get_last_error+.
355+
# its database object, or the current connection. See the options for +DB#get_last_error+.
356356
#
357357
# @option opts [Boolean] :continue_on_error (+false+) If true, then
358358
# continue a bulk insert even if one of the documents inserted
@@ -369,6 +369,9 @@ def save(doc, opts={})
369369
#
370370
# @core insert insert-instance_method
371371
def insert(doc_or_docs, opts={})
372+
if name.start_with?("system.") && name !~ /(\Asystem\.users)|(\Asystem\.indexes)/
373+
raise Mongo::InvalidNSName, "cannot insert into system collections."
374+
end
372375
doc_or_docs = [doc_or_docs] unless doc_or_docs.is_a?(Array)
373376
doc_or_docs.collect! { |doc| @pk_factory.create_pk(doc) }
374377
write_concern = get_write_concern(opts, self)

test/functional/collection_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,19 @@ def test_ensure_index_timeout
10751075
coll.ensure_index([['a', 1]])
10761076
end
10771077

1078+
def test_insert_raises_exception_on_system_collection
1079+
collection = @@db["system.foo"]
1080+
assert_raise Mongo::InvalidNSName do
1081+
collection.insert({ :a => 1 })
1082+
end
1083+
end
1084+
1085+
def test_save_raises_exception_on_system_collection
1086+
collection = @@db["system.foo"]
1087+
assert_raise Mongo::InvalidNSName do
1088+
collection.save({ :a => 1 })
1089+
end
1090+
end
10781091

10791092
if @@version > '2.0.0'
10801093
def test_show_disk_loc

0 commit comments

Comments
 (0)