diff --git a/lib/bson/types/object_id.rb b/lib/bson/types/object_id.rb index 5de7f66241..6e44efa515 100644 --- a/lib/bson/types/object_id.rb +++ b/lib/bson/types/object_id.rb @@ -51,7 +51,7 @@ def initialize(data=nil, time=nil) # # @return [Boolean] def self.legal?(str) - str =~ /^[0-9a-f]{24}$/i ? true : false + str =~ /\A[0-9a-f]{24}\z/i ? true : false end # Create an object id from the given time. This is useful for doing range diff --git a/test/bson/object_id_test.rb b/test/bson/object_id_test.rb index 5626c61b84..06de831cc9 100644 --- a/test/bson/object_id_test.rb +++ b/test/bson/object_id_test.rb @@ -93,6 +93,9 @@ def test_illegal_from_string assert_raise InvalidObjectId do ObjectId.from_string("") end + assert_raise InvalidObjectId do + ObjectId.from_string("a"*24+"\na") + end end def test_from_string_with_object_id