Skip to content

Commit 3f8b97f

Browse files
committed
RUBY-662 prevent ObjectId initialization with bad data
1 parent df95902 commit 3f8b97f

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

lib/bson/types/object_id.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ class ObjectId
4141
# the remaining bytes will consist of the standard machine id, pid, and counter. If
4242
# you need a zeroed timestamp, used ObjectId.from_time.
4343
def initialize(data=nil, time=nil)
44+
if data && (!data.is_a?(Array) || data.size != 12)
45+
raise InvalidObjectId, 'ObjectId requires 12 byte array'
46+
end
4447
@data = data || generate(time)
4548
end
4649

test/bson/object_id_test.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,18 @@ def test_hashcode
2626
end
2727

2828
def test_array_uniq_for_equilavent_ids
29-
a = ObjectId.new('123')
30-
b = ObjectId.new('123')
29+
a = ObjectId.new('123456789101'.unpack('C*'))
30+
b = ObjectId.new('123456789101'.unpack('C*'))
3131
assert_equal a, b
3232
assert_equal 1, [a, b].uniq.size
3333
end
3434

35+
def test_initialization_with_bad_data
36+
assert_raise InvalidObjectId do
37+
ObjectId.new('\xff')
38+
end
39+
end
40+
3541
def test_create_pk_method
3642
doc = {:name => 'Mongo'}
3743
doc = ObjectId.create_pk(doc)

0 commit comments

Comments
 (0)