bson 5.2.0 → 5.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f383730000f9b08a131535250a7855d8ec8dcb0e3bb334f4779dcdcc95b56023
4
- data.tar.gz: 57cb5e6db027b6982263d4f4eb0228968b6fc71e6b6499ef5563c0a160a3b2b0
3
+ metadata.gz: d550c8e03e3a8aa40b00f83dbf20a05aa98d8c52adcc5095242342ea249b93ce
4
+ data.tar.gz: 1e2205d28adbb50c7175e014c8c2f3b31cfabd14b6e4d45cecf12dee4a1afccd
5
5
  SHA512:
6
- metadata.gz: 433d3758ccd6ad9222b98bf43a4746b71b5966d6eb01321dff869eca61acef89e58b42baae0bd9c979d41cc922b4baf227ae0b71fdc4f21d37b0d3ec7137a85b
7
- data.tar.gz: 7f61000684c0acb7af4d2d50c3e2cbc1e47ab342c415fc91addf7e1c5533cb3e23cfe4d16ba2ccd5dea72d70d8a89698eec3f4520fee3320c3c64c7aec95f10d
6
+ metadata.gz: 945488d99378c2db0eaecfc30db8e28643c2505e0f9366f23b2168ced03a190b9f778e7abb22c2147cdc4758743c95c125c5a7eab6bdf4747502f8d4dde74eb7
7
+ data.tar.gz: 12d1de434f0f6933f948d9c2b664f2e88e94cdfbb9e01d7ef4488bcba989a6f2ec3b4ab5fe5dd2635dd753fa2197c4f21a4bb412252852a1250312964883ed68
data/ext/bson/read.c CHANGED
@@ -27,15 +27,31 @@ static VALUE pvt_get_double(byte_buffer_t *b);
27
27
  static VALUE pvt_get_string(byte_buffer_t *b, const char *data_type);
28
28
  static VALUE pvt_get_symbol(byte_buffer_t *b, VALUE rb_buffer, int argc, VALUE *argv);
29
29
  static VALUE pvt_get_boolean(byte_buffer_t *b);
30
- static VALUE pvt_read_field(byte_buffer_t *b, VALUE rb_buffer, uint8_t type, int argc, VALUE *argv);
30
+ static VALUE pvt_read_field(byte_buffer_t *b, VALUE rb_buffer, uint8_t type, int argc, VALUE *argv, int depth);
31
+ static VALUE pvt_get_hash_at_depth(int argc, VALUE *argv, VALUE self, int depth);
32
+ static VALUE pvt_get_array_at_depth(int argc, VALUE *argv, VALUE self, int depth);
33
+ static void pvt_check_nesting_depth(int depth);
31
34
  static void pvt_skip_cstring(byte_buffer_t *b);
32
35
  static size_t pvt_strnlen(const byte_buffer_t *b);
33
36
 
37
+ /* Maximum number of nested BSON documents or arrays the decoder will accept.
38
+ * Mirrors BSON::MAX_NESTING_DEPTH in lib/bson.rb. */
39
+ #define BSON_RUBY_MAX_NESTING_DEPTH 200
40
+
34
41
  void pvt_raise_decode_error(volatile VALUE msg) {
35
42
  VALUE klass = pvt_const_get_3("BSON", "Error", "BSONDecodeError");
36
43
  rb_exc_raise(rb_exc_new_str(klass, msg));
37
44
  }
38
45
 
46
+ /* Raise BSON::Error::BSONDecodeError if the depth exceeds the cap. */
47
+ void pvt_check_nesting_depth(int depth) {
48
+ if (depth > BSON_RUBY_MAX_NESTING_DEPTH) {
49
+ pvt_raise_decode_error(rb_sprintf(
50
+ "BSON document nesting depth exceeds maximum of %d",
51
+ BSON_RUBY_MAX_NESTING_DEPTH));
52
+ }
53
+ }
54
+
39
55
  /**
40
56
  * validate the buffer contains the amount of bytes the array / hash claimns
41
57
  * and that it is null terminated
@@ -66,9 +82,10 @@ int32_t pvt_validate_length(byte_buffer_t *b)
66
82
  }
67
83
 
68
84
  /**
69
- * Read a single field from a hash or array
85
+ * Read a single field from a hash or array. `depth` is the current nesting
86
+ * depth; nested documents/arrays bump it before recursing.
70
87
  */
71
- VALUE pvt_read_field(byte_buffer_t *b, VALUE rb_buffer, uint8_t type, int argc, VALUE *argv)
88
+ VALUE pvt_read_field(byte_buffer_t *b, VALUE rb_buffer, uint8_t type, int argc, VALUE *argv, int depth)
72
89
  {
73
90
  switch(type) {
74
91
  case BSON_TYPE_INT32: return pvt_get_int32(b);
@@ -76,8 +93,8 @@ VALUE pvt_read_field(byte_buffer_t *b, VALUE rb_buffer, uint8_t type, int argc,
76
93
  case BSON_TYPE_DOUBLE: return pvt_get_double(b);
77
94
  case BSON_TYPE_STRING: return pvt_get_string(b, "String");
78
95
  case BSON_TYPE_SYMBOL: return pvt_get_symbol(b, rb_buffer, argc, argv);
79
- case BSON_TYPE_ARRAY: return rb_bson_byte_buffer_get_array(argc, argv, rb_buffer);
80
- case BSON_TYPE_DOCUMENT: return rb_bson_byte_buffer_get_hash(argc, argv, rb_buffer);
96
+ case BSON_TYPE_ARRAY: return pvt_get_array_at_depth(argc, argv, rb_buffer, depth + 1);
97
+ case BSON_TYPE_DOCUMENT: return pvt_get_hash_at_depth(argc, argv, rb_buffer, depth + 1);
81
98
  case BSON_TYPE_BOOLEAN: return pvt_get_boolean(b);
82
99
  default:
83
100
  {
@@ -397,6 +414,10 @@ static int pvt_is_dbref(VALUE doc) {
397
414
  }
398
415
 
399
416
  VALUE rb_bson_byte_buffer_get_hash(int argc, VALUE *argv, VALUE self){
417
+ return pvt_get_hash_at_depth(argc, argv, self, 1);
418
+ }
419
+
420
+ VALUE pvt_get_hash_at_depth(int argc, VALUE *argv, VALUE self, int depth){
400
421
  VALUE doc = Qnil;
401
422
  byte_buffer_t *b = NULL;
402
423
  uint8_t type;
@@ -404,6 +425,8 @@ VALUE rb_bson_byte_buffer_get_hash(int argc, VALUE *argv, VALUE self){
404
425
  int32_t length;
405
426
  char *start_ptr;
406
427
 
428
+ pvt_check_nesting_depth(depth);
429
+
407
430
  TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
408
431
 
409
432
  start_ptr = READ_PTR(b);
@@ -413,7 +436,7 @@ VALUE rb_bson_byte_buffer_get_hash(int argc, VALUE *argv, VALUE self){
413
436
 
414
437
  while((type = pvt_get_type_byte(b)) != 0){
415
438
  VALUE field = rb_bson_byte_buffer_get_cstring(self);
416
- rb_hash_aset(doc, field, pvt_read_field(b, self, type, argc, argv));
439
+ rb_hash_aset(doc, field, pvt_read_field(b, self, type, argc, argv, depth));
417
440
  RB_GC_GUARD(field);
418
441
  }
419
442
 
@@ -430,12 +453,18 @@ VALUE rb_bson_byte_buffer_get_hash(int argc, VALUE *argv, VALUE self){
430
453
  }
431
454
 
432
455
  VALUE rb_bson_byte_buffer_get_array(int argc, VALUE *argv, VALUE self){
456
+ return pvt_get_array_at_depth(argc, argv, self, 1);
457
+ }
458
+
459
+ VALUE pvt_get_array_at_depth(int argc, VALUE *argv, VALUE self, int depth){
433
460
  byte_buffer_t *b;
434
461
  VALUE array = Qnil;
435
462
  uint8_t type;
436
463
  int32_t length;
437
464
  char *start_ptr;
438
465
 
466
+ pvt_check_nesting_depth(depth);
467
+
439
468
  TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
440
469
 
441
470
  start_ptr = READ_PTR(b);
@@ -444,7 +473,7 @@ VALUE rb_bson_byte_buffer_get_array(int argc, VALUE *argv, VALUE self){
444
473
  array = rb_ary_new();
445
474
  while((type = pvt_get_type_byte(b)) != 0){
446
475
  pvt_skip_cstring(b);
447
- rb_ary_push(array, pvt_read_field(b, self, type, argc, argv));
476
+ rb_ary_push(array, pvt_read_field(b, self, type, argc, argv, depth));
448
477
  }
449
478
  RB_GC_GUARD(array);
450
479
 
data/ext/bson/write.c CHANGED
@@ -34,6 +34,17 @@ static void pvt_put_bson_key(byte_buffer_t *b, VALUE string);
34
34
  static VALUE pvt_bson_byte_buffer_put_bson_partial_string(VALUE self, const char *str, int32_t length);
35
35
  static VALUE pvt_bson_byte_buffer_put_binary_string(VALUE self, const char *str, int32_t length);
36
36
 
37
+ /* Raise ArgumentError if length exceeds the BSON int32_t string-length limit.
38
+ * The bound is INT32_MAX - 5 rather than INT32_MAX because the binary-string
39
+ * path writes length + 5 bytes total (4-byte int32 length prefix + payload +
40
+ * 1-byte null terminator). Allowing length == INT32_MAX would make that
41
+ * addition overflow signed 32-bit arithmetic (undefined behavior in C). */
42
+ static void pvt_check_string_length(long length) {
43
+ if (length > INT32_MAX - 5) {
44
+ rb_raise(rb_eArgError, "String length %ld exceeds BSON maximum", length);
45
+ }
46
+ }
47
+
37
48
  static int fits_int32(int64_t i64){
38
49
  return i64 >= INT32_MIN && i64 <= INT32_MAX;
39
50
  }
@@ -211,7 +222,7 @@ static VALUE pvt_bson_encode_to_utf8(VALUE string) {
211
222
  VALUE encoding;
212
223
  VALUE utf8_string;
213
224
  const char *str;
214
- int32_t length;
225
+ long length;
215
226
 
216
227
  existing_encoding_name = rb_funcall(
217
228
  rb_funcall(string, rb_intern("encoding"), 0),
@@ -222,6 +233,7 @@ static VALUE pvt_bson_encode_to_utf8(VALUE string) {
222
233
 
223
234
  str = RSTRING_PTR(utf8_string);
224
235
  length = RSTRING_LEN(utf8_string);
236
+ pvt_check_string_length(length);
225
237
 
226
238
  rb_bson_utf8_validate(str, length, true, "String");
227
239
  } else {
@@ -238,17 +250,18 @@ VALUE rb_bson_byte_buffer_put_string(VALUE self, VALUE string)
238
250
  {
239
251
  VALUE utf8_string;
240
252
  const char *str;
241
- int32_t length;
253
+ long length;
242
254
 
243
255
  utf8_string = pvt_bson_encode_to_utf8(string);
244
256
  /* At this point utf8_string contains valid utf-8 byte sequences only */
245
257
 
246
258
  str = RSTRING_PTR(utf8_string);
247
259
  length = RSTRING_LEN(utf8_string);
260
+ pvt_check_string_length(length);
248
261
 
249
262
  RB_GC_GUARD(utf8_string);
250
263
 
251
- return pvt_bson_byte_buffer_put_binary_string(self, str, length);
264
+ return pvt_bson_byte_buffer_put_binary_string(self, str, (int32_t)length);
252
265
  }
253
266
 
254
267
  /**
@@ -271,7 +284,7 @@ VALUE rb_bson_byte_buffer_put_cstring(VALUE self, VALUE obj)
271
284
  {
272
285
  VALUE string;
273
286
  const char *str;
274
- int32_t length;
287
+ long length;
275
288
 
276
289
  switch (TYPE(obj)) {
277
290
  case T_STRING:
@@ -289,8 +302,9 @@ VALUE rb_bson_byte_buffer_put_cstring(VALUE self, VALUE obj)
289
302
 
290
303
  str = RSTRING_PTR(string);
291
304
  length = RSTRING_LEN(string);
305
+ pvt_check_string_length(length);
292
306
  RB_GC_GUARD(string);
293
- return pvt_bson_byte_buffer_put_bson_partial_string(self, str, length);
307
+ return pvt_bson_byte_buffer_put_bson_partial_string(self, str, (int32_t)length);
294
308
  }
295
309
 
296
310
  /**
@@ -317,10 +331,11 @@ VALUE rb_bson_byte_buffer_put_symbol(VALUE self, VALUE symbol)
317
331
  {
318
332
  VALUE symbol_str = rb_sym_to_s(symbol);
319
333
  const char *str = RSTRING_PTR(symbol_str);
320
- const int32_t length = RSTRING_LEN(symbol_str);
334
+ long length = RSTRING_LEN(symbol_str);
335
+ pvt_check_string_length(length);
321
336
 
322
337
  RB_GC_GUARD(symbol_str);
323
- return pvt_bson_byte_buffer_put_binary_string(self, str, length);
338
+ return pvt_bson_byte_buffer_put_binary_string(self, str, (int32_t)length);
324
339
  }
325
340
 
326
341
  /**
@@ -328,9 +343,10 @@ VALUE rb_bson_byte_buffer_put_symbol(VALUE self, VALUE symbol)
328
343
  */
329
344
  void pvt_put_bson_key(byte_buffer_t *b, VALUE string){
330
345
  char *c_str = RSTRING_PTR(string);
331
- size_t length = RSTRING_LEN(string);
346
+ long length = RSTRING_LEN(string);
347
+ pvt_check_string_length(length);
332
348
 
333
- pvt_put_cstring(b, c_str, length, "Key");
349
+ pvt_put_cstring(b, c_str, (int32_t)length, "Key");
334
350
  }
335
351
 
336
352
  void pvt_replace_int32(byte_buffer_t *b, int32_t position, int32_t newval)
@@ -649,7 +665,6 @@ VALUE rb_bson_byte_buffer_put_array(VALUE self, VALUE array){
649
665
  size_t new_position = 0;
650
666
  int32_t new_length = 0;
651
667
  size_t position = 0;
652
- VALUE *array_element = NULL;
653
668
  TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
654
669
  Check_Type(array, T_ARRAY);
655
670
 
@@ -657,12 +672,19 @@ VALUE rb_bson_byte_buffer_put_array(VALUE self, VALUE array){
657
672
  /* insert length placeholder */
658
673
  pvt_put_int32(b, 0);
659
674
 
660
- array_element = RARRAY_PTR(array);
675
+ long original_len = RARRAY_LEN(array);
676
+ if (original_len > INT32_MAX) {
677
+ rb_raise(rb_eRangeError, "array too large for BSON serialization");
678
+ }
661
679
 
662
- for(int32_t index=0; index < RARRAY_LEN(array); index++, array_element++){
663
- pvt_put_type_byte(b, *array_element);
680
+ for (int32_t index = 0; index < (int32_t)original_len; index++) {
681
+ if (RARRAY_LEN(array) != original_len) {
682
+ rb_raise(rb_eRuntimeError, "array modified during BSON serialization");
683
+ }
684
+ volatile VALUE element = rb_ary_entry(array, index);
685
+ pvt_put_type_byte(b, element);
664
686
  pvt_put_array_index(b, index);
665
- pvt_put_field(b, self, *array_element);
687
+ pvt_put_field(b, self, element);
666
688
  }
667
689
  pvt_put_byte(b, 0);
668
690
 
data/lib/bson/array.rb CHANGED
@@ -143,15 +143,20 @@ module BSON
143
143
  # @raise [ BSON::Error::BSONDecodeError ] if the expected number of
144
144
  # bytes were not read from the buffer
145
145
  def parse_array_from_buffer(buffer, **options)
146
- new.tap do |array|
147
- start_position = buffer.read_position
148
- expected_byte_size = buffer.get_int32
149
- parse_array_elements_from_buffer(array, buffer, **options)
150
- actual_byte_size = buffer.read_position - start_position
151
- if actual_byte_size != expected_byte_size
152
- raise Error::BSONDecodeError,
153
- "Expected array to take #{expected_byte_size} bytes but it took #{actual_byte_size} bytes"
146
+ BSON.enter_nesting_depth
147
+ begin
148
+ new.tap do |array|
149
+ start_position = buffer.read_position
150
+ expected_byte_size = buffer.get_int32
151
+ parse_array_elements_from_buffer(array, buffer, **options)
152
+ actual_byte_size = buffer.read_position - start_position
153
+ if actual_byte_size != expected_byte_size
154
+ raise Error::BSONDecodeError,
155
+ "Expected array to take #{expected_byte_size} bytes but it took #{actual_byte_size} bytes"
156
+ end
154
157
  end
158
+ ensure
159
+ BSON.leave_nesting_depth
155
160
  end
156
161
  end
157
162
 
data/lib/bson/binary.rb CHANGED
@@ -306,7 +306,20 @@ module BSON
306
306
  type = type_byte
307
307
  end
308
308
 
309
- length = buffer.get_int32 if type == :old
309
+ if type == :old
310
+ inner_length = buffer.get_int32
311
+ unless inner_length == length - 4
312
+ raise Error::BSONDecodeError,
313
+ "BSON binary subtype 0x02 length mismatch: outer=#{length}, inner=#{inner_length}"
314
+ end
315
+ length = inner_length
316
+ end
317
+
318
+ if length.negative?
319
+ raise Error::BSONDecodeError,
320
+ "BSON binary length is negative: #{length}"
321
+ end
322
+
310
323
  data = buffer.get_bytes(length)
311
324
  new(data, type)
312
325
  end
@@ -28,9 +28,25 @@ module BSON
28
28
 
29
29
  # Create a new DBPointer object.
30
30
  #
31
+ # The types are enforced because the DBPointer wire format is a string
32
+ # followed by exactly 12 raw bytes. #to_bson writes the id duck-typed, so
33
+ # an id of any other type emits the wrong number of bytes and reframes the
34
+ # enclosing document for every conforming reader.
35
+ #
31
36
  # @param [ String ] ref The database collection name.
32
37
  # @param [ BSON::ObjectId ] id The DBPointer id.
38
+ #
39
+ # @raise [ ArgumentError ] If ref is not a String or id is not a
40
+ # BSON::ObjectId.
33
41
  def initialize(ref, id)
42
+ unless ref.is_a?(::String)
43
+ raise ArgumentError, "DBPointer ref must be a String: #{ref.inspect}"
44
+ end
45
+
46
+ unless id.is_a?(ObjectId)
47
+ raise ArgumentError, "DBPointer id must be a BSON::ObjectId: #{id.inspect}"
48
+ end
49
+
34
50
  @ref = ref
35
51
  @id = id
36
52
  end
data/lib/bson/dbref.rb CHANGED
@@ -21,21 +21,6 @@ module BSON
21
21
  class DBRef < Document
22
22
  include JSON
23
23
 
24
- # The constant for the collection reference field.
25
- #
26
- # @deprecated
27
- COLLECTION = '$ref'.freeze
28
-
29
- # The constant for the id field.
30
- #
31
- # @deprecated
32
- ID = '$id'.freeze
33
-
34
- # The constant for the database field.
35
- #
36
- # @deprecated
37
- DATABASE = '$db'.freeze
38
-
39
24
  # @return [ String ] collection The collection name.
40
25
  def collection
41
26
  self['$ref']
@@ -34,18 +34,5 @@ module BSON
34
34
  def jruby?
35
35
  @jruby ||= defined?(JRUBY_VERSION)
36
36
  end
37
-
38
- # Determine if we are using Ruby version 1.9.
39
- #
40
- # @example Are we running with Ruby version 1.9?
41
- # Environment.ruby_1_9?
42
- #
43
- # @return [ true, false ] If the Ruby version is 1.9.
44
- #
45
- # @since 4.2.0
46
- # @deprecated
47
- def ruby_1_9?
48
- @ruby_1_9 ||= RUBY_VERSION < '2.0.0'
49
- end
50
37
  end
51
38
  end
data/lib/bson/ext_json.rb CHANGED
@@ -115,8 +115,13 @@ module BSON
115
115
  when Hash
116
116
  parse_hash(value, **options)
117
117
  when Array
118
- value.map do |item|
119
- parse_obj(item, **options)
118
+ BSON.enter_nesting_depth
119
+ begin
120
+ value.map do |item|
121
+ parse_obj(item, **options)
122
+ end
123
+ ensure
124
+ BSON.leave_nesting_depth
120
125
  end
121
126
  else
122
127
  raise Error::ExtJSONParseError, "Unknown value type: #{value}"
@@ -136,6 +141,15 @@ module BSON
136
141
  end].freeze
137
142
 
138
143
  module_function def parse_hash(hash, **options)
144
+ BSON.enter_nesting_depth
145
+ begin
146
+ return parse_hash_body(hash, **options)
147
+ ensure
148
+ BSON.leave_nesting_depth
149
+ end
150
+ end
151
+
152
+ module_function def parse_hash_body(hash, **options)
139
153
  if hash.empty?
140
154
  return {}
141
155
  end
@@ -246,10 +260,25 @@ module BSON
246
260
  # TODO consider returning Ruby regular expression object here
247
261
  create_regexp(value['pattern'], value['options'])
248
262
  when '$dbPointer'
249
- unless value.keys.sort == %w($id $ref)
263
+ unless value.is_a?(Hash) && value.keys.sort == %w($id $ref)
250
264
  raise Error::ExtJSONParseError, "Invalid $dbPointer value: #{value}"
251
265
  end
252
- DbPointer.new(value['$ref'], parse_hash(value['$id']))
266
+ ref = value['$ref']
267
+ unless ref.is_a?(String)
268
+ raise Error::ExtJSONParseError, "Invalid $dbPointer, $ref must be a string: #{value}"
269
+ end
270
+ # The DBPointer wire format is a string followed by exactly 12 raw
271
+ # bytes, so the id must be an ObjectId. Any other type serializes to
272
+ # a different number of bytes and reframes the enclosing document.
273
+ id = value['$id']
274
+ unless id.is_a?(Hash)
275
+ raise Error::ExtJSONParseError, "Invalid $dbPointer, $id must be an ObjectId: #{value}"
276
+ end
277
+ id = parse_hash(id, **options)
278
+ unless id.is_a?(ObjectId)
279
+ raise Error::ExtJSONParseError, "Invalid $dbPointer, $id must be an ObjectId: #{value}"
280
+ end
281
+ DbPointer.new(ref, id)
253
282
  when '$date'
254
283
  case value
255
284
  when String
data/lib/bson/float.rb CHANGED
@@ -32,7 +32,10 @@ module BSON
32
32
  # The pack directive is for 8 byte floating points.
33
33
  #
34
34
  # @since 2.0.0
35
+ # @deprecated No longer used internally; retained for backward
36
+ # compatibility and slated for removal in a future major version.
35
37
  PACK = "E"
38
+ deprecate_constant :PACK
36
39
 
37
40
  # Get the floating point as encoded BSON.
38
41
  #
data/lib/bson/hash.rb CHANGED
@@ -167,17 +167,22 @@ module BSON
167
167
  #
168
168
  # @return [ Hash ] the hash parsed from the buffer
169
169
  def parse_hash_from_buffer(buffer, **options)
170
- hash = Document.allocate
171
- start_position = buffer.read_position
172
- expected_byte_size = buffer.get_int32
170
+ BSON.enter_nesting_depth
171
+ begin
172
+ hash = Document.allocate
173
+ start_position = buffer.read_position
174
+ expected_byte_size = buffer.get_int32
173
175
 
174
- parse_hash_contents(hash, buffer, **options)
176
+ parse_hash_contents(hash, buffer, **options)
175
177
 
176
- actual_byte_size = buffer.read_position - start_position
177
- return hash unless actual_byte_size != expected_byte_size
178
+ actual_byte_size = buffer.read_position - start_position
179
+ return hash unless actual_byte_size != expected_byte_size
178
180
 
179
- raise Error::BSONDecodeError,
180
- "Expected hash to take #{expected_byte_size} bytes but it took #{actual_byte_size} bytes"
181
+ raise Error::BSONDecodeError,
182
+ "Expected hash to take #{expected_byte_size} bytes but it took #{actual_byte_size} bytes"
183
+ ensure
184
+ BSON.leave_nesting_depth
185
+ end
181
186
  end
182
187
 
183
188
  # Given an empty hash and a byte buffer, parse the key/value pairs from
data/lib/bson/int32.rb CHANGED
@@ -32,12 +32,18 @@ module BSON
32
32
  # The number of bytes constant.
33
33
  #
34
34
  # @since 4.0.0
35
+ # @deprecated No longer used internally; retained for backward
36
+ # compatibility and slated for removal in a future major version.
35
37
  BYTES_LENGTH = 4
38
+ deprecate_constant :BYTES_LENGTH
36
39
 
37
40
  # Constant for the int 32 pack directive.
38
41
  #
39
42
  # @since 2.0.0
43
+ # @deprecated No longer used internally; retained for backward
44
+ # compatibility and slated for removal in a future major version.
40
45
  PACK = "l<"
46
+ deprecate_constant :PACK
41
47
 
42
48
  # Deserialize an Integer from BSON.
43
49
  #
data/lib/bson/int64.rb CHANGED
@@ -32,7 +32,10 @@ module BSON
32
32
  # Constant for the int 64 pack directive.
33
33
  #
34
34
  # @since 2.0.0
35
+ # @deprecated No longer used internally; retained for backward
36
+ # compatibility and slated for removal in a future major version.
35
37
  PACK = "q<"
38
+ deprecate_constant :PACK
36
39
 
37
40
  # Deserialize an Integer from BSON.
38
41
  #
data/lib/bson/integer.rb CHANGED
@@ -47,15 +47,24 @@ module BSON
47
47
  # The BSON index size.
48
48
  #
49
49
  # @since 2.0.0
50
+ # @deprecated No longer used internally; retained for backward
51
+ # compatibility and slated for removal in a future major version.
50
52
  BSON_INDEX_SIZE = 1024
51
53
 
52
54
  # A hash of index values for array optimization.
53
55
  #
54
56
  # @since 2.0.0
57
+ # @deprecated No longer used internally; retained for backward
58
+ # compatibility and slated for removal in a future major version.
55
59
  BSON_ARRAY_INDEXES = ::Array.new(BSON_INDEX_SIZE) do |i|
56
60
  (i.to_s.b << NULL_BYTE).freeze
57
61
  end.freeze
58
62
 
63
+ # Deprecated after BSON_ARRAY_INDEXES is built, since that (also
64
+ # deprecated) constant references BSON_INDEX_SIZE at load time.
65
+ deprecate_constant :BSON_INDEX_SIZE
66
+ deprecate_constant :BSON_ARRAY_INDEXES
67
+
59
68
  # Is this integer a valid BSON 32 bit value?
60
69
  #
61
70
  # @example Is the integer a valid 32 bit value?
@@ -35,11 +35,7 @@ module BSON
35
35
  #
36
36
  # @since 4.2.0
37
37
  def to_bson(buffer = ByteBuffer.new)
38
- if Environment.ruby_1_9?
39
- marshal_dump.dup
40
- else
41
- to_h
42
- end.to_bson(buffer)
38
+ to_h.to_bson(buffer)
43
39
  end
44
40
 
45
41
  # The BSON type for OpenStruct objects is the Hash type of 0x03.
data/lib/bson/regexp.rb CHANGED
@@ -50,13 +50,6 @@ module BSON
50
50
  # @since 3.2.6
51
51
  NEWLINE_VALUE = 's'
52
52
 
53
- # Ruby multiline constant.
54
- #
55
- # @since 3.2.6
56
- #
57
- # @deprecated Will be removed in 5.0
58
- RUBY_MULTILINE_VALUE = 'ms'
59
-
60
53
  # Get the regexp as JSON hash data.
61
54
  #
62
55
  # @example Get the regexp as a JSON hash.
data/lib/bson/string.rb CHANGED
@@ -30,11 +30,6 @@ module BSON
30
30
  # @since 2.0.0
31
31
  BSON_TYPE = ::String.new(2.chr, encoding: BINARY).freeze
32
32
 
33
- # Regex for matching illegal BSON keys.
34
- #
35
- # @since 4.1.0
36
- ILLEGAL_KEY = /(\A[$])|(\.)/
37
-
38
33
  # Get the string as encoded BSON.
39
34
  #
40
35
  # @example Get the string as encoded BSON.
@@ -86,7 +86,6 @@ module BSON
86
86
  # @return [ Hash ] The timestamp as a JSON hash.
87
87
  #
88
88
  # @since 2.0.0
89
- # @deprecated Use as_extended_json instead.
90
89
  def as_json(*args)
91
90
  as_extended_json
92
91
  end
data/lib/bson/version.rb CHANGED
@@ -5,5 +5,5 @@ module BSON
5
5
  #
6
6
  # NOTE: this file is automatically updated via `rake candidate:create`.
7
7
  # Manual changes to this file may be overwritten by that rake task.
8
- VERSION = '5.2.0'
8
+ VERSION = '5.2.1'
9
9
  end
data/lib/bson.rb CHANGED
@@ -21,20 +21,25 @@ require "bson/environment"
21
21
  # @since 0.0.0
22
22
  module BSON
23
23
 
24
- # Create a new object id from a string using ObjectId.from_string
24
+ # Interprets the argument as an ObjectId. If given a BSON::ObjectId, this method
25
+ # returns it directly without creating a new one. Otherwise, the argument is
26
+ # interpreted as a string and parsed accordingly.
25
27
  #
26
28
  # @example Create an object id from the string.
27
- # BSON::ObjectId(id)
29
+ # BSON::ObjectId('...')
28
30
  #
29
- # @param [ String ] string The string to create the id from.
31
+ # @param [ String | BSON::ObjectId ] string_or_object_id The string to create the id from,
32
+ # or the existing object id to return.
30
33
  #
31
34
  # @raise [ BSON::Error::InvalidObjectId ] If the provided string is invalid.
32
35
  #
33
- # @return [ BSON::ObjectId ] The new object id.
36
+ # @return [ BSON::ObjectId ] The new or existing object id.
34
37
  #
35
38
  # @see ObjectId.from_string
36
- def self.ObjectId(string)
37
- self::ObjectId.from_string(string)
39
+ def self.ObjectId(string_or_object_id)
40
+ return string_or_object_id if string_or_object_id.is_a?(self::ObjectId)
41
+
42
+ self::ObjectId.from_string(string_or_object_id)
38
43
  end
39
44
 
40
45
  # Constant for binary string encoding.
@@ -56,6 +61,29 @@ module BSON
56
61
  #
57
62
  # @since 2.0.0
58
63
  UTF8 = "UTF-8"
64
+
65
+ # Maximum number of nested BSON documents or arrays the decoder will accept.
66
+ # Prevents stack-overflow DoS on adversarial input. Matches libbson and the
67
+ # Go driver's ExtJSON parser.
68
+ MAX_NESTING_DEPTH = 200
69
+
70
+ # Bump the per-thread BSON decode nesting counter and raise if it exceeds
71
+ # MAX_NESTING_DEPTH. Pair every call with `leave_nesting_depth` in an
72
+ # `ensure` block. The bump is inlined at each callsite (rather than a
73
+ # block-yielding helper) to keep JRuby JVM stack frame counts low enough
74
+ # that the check fires before the JVM stack overflows on adversarial input.
75
+ def self.enter_nesting_depth
76
+ depth = (Thread.current[:_bson_nesting_depth] ||= 0) + 1
77
+ if depth > MAX_NESTING_DEPTH
78
+ raise Error::BSONDecodeError,
79
+ "BSON document nesting depth exceeds maximum of #{MAX_NESTING_DEPTH}"
80
+ end
81
+ Thread.current[:_bson_nesting_depth] = depth
82
+ end
83
+
84
+ def self.leave_nesting_depth
85
+ Thread.current[:_bson_nesting_depth] -= 1
86
+ end
59
87
  end
60
88
 
61
89
  require "bson/config"