cbor 0.5.10.1 → 0.6.2.2

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: 350f51028ed72c54fb5adf9ab595f08e18384cecb8c82d874d195aab6eb16a65
4
- data.tar.gz: 1eb340e507d590530f0c2d9a3cfaf723fb9210596a312443e7b2dc95b912614f
3
+ metadata.gz: 7f5496d45110e3e4bacb9c4cd725ab7331e3bbbf873a8264099cb101612e13be
4
+ data.tar.gz: ed19073bb8575be84f9ba68f3d21e5aeb5633cd914302d2e386178c2a9d0312d
5
5
  SHA512:
6
- metadata.gz: 30fb71f7d3fce1bbf5f40c84d7ca389a1e30051c186c01553d0be07e784a4029dc27cdf6c01a70dd1a155c6bddd050d21705c642e5df6727633d15b39808d153
7
- data.tar.gz: 75783b66fccc0dd03ce323411f81848964e0904805e5eeb9218cc59214d1bfd1a3011df255de2b6a95a92b1d42475d5fb770a0029d6486c6c4060605a3e24f94
6
+ metadata.gz: 9e4c6576cd258c83d89acaefdd92017c2d0f572c5e8fd2e3c41fdf69df0e665801c7c7083cbeee4eaf88a5ac5cf767792f8c3b8445887ee0a3144f673eb5aaa9
7
+ data.tar.gz: c1367b8a9f37bbb5ddda5ccc4a73be44c448b240f49ea323a0cbc003b7bf2a33f089f7190ebb40870d955f1a4c1f0aee9de0182dde243df70c198da2944936f9
data/ChangeLog CHANGED
@@ -1,3 +1,24 @@
1
+ 2026-11-08 Carsten Bormann <cabo@tzi.org>
2
+
3
+ * Safer decode defaults (BREAKING). Object-constructing
4
+ tags are no longer reconstructed into live Ruby objects by default,
5
+ because CBOR.decode is commonly run on input from untrusted sources.
6
+ Tag 35 (Regexp) and tag 32 (URI) now decode to a generic CBOR::Tagged
7
+ value instead of building a Regexp or URI from the decoded value. A
8
+ caller that trusts the source opts in per tag:
9
+
10
+ CBOR.decode(data) # CBOR::Tagged
11
+ CBOR.decode(data, reconstruct_from_tags: [CBOR::TAG_REGEXP])
12
+ CBOR.decode(data, reconstruct_from_tags: [CBOR::TAG_URI])
13
+
14
+ Module constants CBOR::TAG_REGEXP (35) and CBOR::TAG_URI (32) are
15
+ provided; bare integers are also accepted. Tag 0/1 (time) stays on
16
+ by default, as reconstructing a Time from an epoch is a plain value
17
+ conversion. The option works for both CBOR.decode and Unpacker.new,
18
+ like the existing symbolize_keys option. This mirrors the principle
19
+ behind YAML.safe_load and JSON.parse(create_additions: false):
20
+ turning wire data into live objects should be opt-in.
21
+
1
22
  2016-04-11 Carsten Bormann <cabo@tzi.org>
2
23
 
3
24
  * 0.5.9.1: Fix WIN32 problem inherited from upstream
data/README.rdoc CHANGED
@@ -15,6 +15,32 @@ Use RubyGems to install:
15
15
 
16
16
  gem install cbor
17
17
 
18
+ == Decoding untrusted input
19
+
20
+ +CBOR.decode+ is commonly run on data from the network. By default it
21
+ does not turn object-constructing tags into live Ruby objects, since
22
+ decoding data from an untrusted source should not build objects the
23
+ caller never asked for. Tag 35 (Regexp) and tag 32 (URI) decode to a
24
+ generic +CBOR::Tagged+ value instead of building a +Regexp+ or +URI+
25
+ from the decoded value:
26
+
27
+ CBOR.decode(data) #=> #<struct CBOR::Tagged tag=35, value="foo">
28
+
29
+ A caller that trusts the source opts in, one tag at a time:
30
+
31
+ CBOR.decode(data, reconstruct_from_tags: [CBOR::TAG_REGEXP]) #=> /foo/
32
+ CBOR.decode(data, reconstruct_from_tags: [CBOR::TAG_URI]) #=> #<URI::HTTP ...>
33
+ CBOR.decode(data, reconstruct_from_tags: [CBOR::TAG_REGEXP, CBOR::TAG_URI])
34
+
35
+ +CBOR::TAG_REGEXP+ (35) and +CBOR::TAG_URI+ (32) are module constants;
36
+ bare integers work too. Tag 0/1 (time) stays on by default, since
37
+ reconstructing a +Time+ from an epoch is a plain value conversion. The
38
+ option works for both +CBOR.decode+ and <tt>CBOR::Unpacker.new</tt>.
39
+
40
+ This follows the same principle as <tt>YAML.safe_load</tt> and
41
+ <tt>JSON.parse(create_additions: false)</tt>: deserializing wire data
42
+ into live objects should be opt-in.
43
+
18
44
  CBOR is an object representation format defined by the IETF[http://ietf.org].
19
45
  The specification[http://tools.ietf.org/html/rfc8949]
20
46
  is an IETF Standards-Track specification
@@ -38,9 +38,10 @@ static ID s_write;
38
38
  static ID s_append;
39
39
  static ID s_close;
40
40
 
41
+ static msgpack_buffer_t* Buffer_get(VALUE self);
42
+
41
43
  #define BUFFER(from, name) \
42
- msgpack_buffer_t *name = NULL; \
43
- Data_Get_Struct(from, msgpack_buffer_t, name); \
44
+ msgpack_buffer_t *name = Buffer_get(from); \
44
45
  if(name == NULL) { \
45
46
  rb_raise(rb_eArgError, "NULL found for " # name " when shouldn't be."); \
46
47
  }
@@ -51,30 +52,70 @@ static ID s_close;
51
52
  rb_raise(rb_eTypeError, "instance of String needed"); \
52
53
  }
53
54
 
55
+ static void Buffer_mark(void* data)
56
+ {
57
+ msgpack_buffer_t* b = data;
58
+ msgpack_buffer_mark(b);
59
+ }
60
+
54
61
  static void Buffer_free(void* data)
55
62
  {
56
63
  if(data == NULL) {
57
64
  return;
58
65
  }
59
- msgpack_buffer_t* b = (msgpack_buffer_t*) data;
66
+ msgpack_buffer_t* b = data;
60
67
  msgpack_buffer_destroy(b);
61
- xfree(b);
68
+ xfree(data);
69
+ }
70
+
71
+ #ifdef TypedData_Make_Struct
72
+ static size_t Buffer_memsize(const void* data)
73
+ {
74
+ return data == NULL ? 0 : sizeof(msgpack_buffer_t);
75
+ }
76
+
77
+ static const rb_data_type_t buffer_data_type = {
78
+ "CBOR::Buffer",
79
+ {Buffer_mark, Buffer_free, Buffer_memsize,},
80
+ NULL, NULL, 0
81
+ };
82
+
83
+ static const rb_data_type_t wrapped_buffer_data_type = {
84
+ "CBOR::WrappedBuffer",
85
+ {Buffer_mark, NULL, Buffer_memsize,},
86
+ &buffer_data_type, NULL, 0
87
+ };
88
+ #endif
89
+
90
+ static msgpack_buffer_t* Buffer_get(VALUE self)
91
+ {
92
+ msgpack_buffer_t* b = NULL;
93
+ #ifdef TypedData_Make_Struct
94
+ TypedData_Get_Struct(self, msgpack_buffer_t, &buffer_data_type, b);
95
+ #else
96
+ Data_Get_Struct(self, msgpack_buffer_t, b);
97
+ #endif
98
+ return b;
62
99
  }
63
100
 
64
101
  static VALUE Buffer_alloc(VALUE klass)
65
102
  {
66
- msgpack_buffer_t* b = ALLOC_N(msgpack_buffer_t, 1);
103
+ msgpack_buffer_t* b;
104
+ VALUE self;
105
+ #ifdef TypedData_Make_Struct
106
+ self = TypedData_Make_Struct(klass, msgpack_buffer_t, &buffer_data_type, b);
107
+ #else
108
+ self = Data_Make_Struct(klass, msgpack_buffer_t, Buffer_mark, Buffer_free, b);
109
+ #endif
67
110
  msgpack_buffer_init(b);
68
111
 
69
- return Data_Wrap_Struct(klass, msgpack_buffer_mark, Buffer_free, b);
112
+ return self;
70
113
  }
71
114
 
72
115
  static ID get_partial_read_method(VALUE io)
73
116
  {
74
117
  if(rb_respond_to(io, s_readpartial)) {
75
118
  return s_readpartial;
76
- } else if(rb_respond_to(io, s_read)) {
77
- return s_read;
78
119
  } else {
79
120
  return s_read;
80
121
  }
@@ -120,7 +161,11 @@ void MessagePack_Buffer_initialize(msgpack_buffer_t* b, VALUE io, VALUE options)
120
161
  VALUE MessagePack_Buffer_wrap(msgpack_buffer_t* b, VALUE owner)
121
162
  {
122
163
  b->owner = owner;
123
- return Data_Wrap_Struct(cMessagePack_Buffer, msgpack_buffer_mark, NULL, b);
164
+ #ifdef TypedData_Make_Struct
165
+ return TypedData_Wrap_Struct(cMessagePack_Buffer, &wrapped_buffer_data_type, b);
166
+ #else
167
+ return Data_Wrap_Struct(cMessagePack_Buffer, Buffer_mark, NULL, b);
168
+ #endif
124
169
  }
125
170
 
126
171
  static VALUE Buffer_initialize(int argc, VALUE* argv, VALUE self)
@@ -514,4 +559,3 @@ void MessagePack_Buffer_module_init(VALUE mMessagePack)
514
559
  rb_define_alias(cMessagePack_Buffer, "to_s", "to_str");
515
560
  rb_define_method(cMessagePack_Buffer, "to_a", Buffer_to_a, 0);
516
561
  }
517
-
data/ext/cbor/core_ext.c CHANGED
@@ -49,8 +49,7 @@ static inline VALUE delegete_to_pack(int argc, VALUE* argv, VALUE self)
49
49
  return delegete_to_pack(argc, argv, self); \
50
50
  } \
51
51
  VALUE packer = argv[0]; \
52
- msgpack_packer_t *pk; \
53
- Data_Get_Struct(packer, msgpack_packer_t, pk);
52
+ msgpack_packer_t *pk = MessagePack_Packer_get(packer);
54
53
 
55
54
  static VALUE NilClass_to_msgpack(int argc, VALUE* argv, VALUE self)
56
55
  {
@@ -198,4 +197,3 @@ void MessagePack_core_ext_module_init()
198
197
  rb_define_method(rb_cCBOR_Simple, "to_cbor", Simple_to_msgpack, -1);
199
198
  rb_define_method(rb_cCBOR_Tagged, "to_cbor", Tagged_to_msgpack, -1);
200
199
  }
201
-
@@ -39,15 +39,23 @@ static ID s_write;
39
39
  //static VALUE s_packer_value;
40
40
  //static msgpack_packer_t* s_packer;
41
41
 
42
+ static msgpack_packer_t* Packer_get(VALUE self);
43
+
42
44
  #define PACKER(from, name) \
43
- msgpack_packer_t* name; \
44
- Data_Get_Struct(from, msgpack_packer_t, name); \
45
+ msgpack_packer_t* name = Packer_get(from); \
45
46
  if(name == NULL) { \
46
47
  rb_raise(rb_eArgError, "NULL found for " # name " when shouldn't be."); \
47
48
  }
48
49
 
49
- static void Packer_free(msgpack_packer_t* pk)
50
+ static void Packer_mark(void* data)
50
51
  {
52
+ msgpack_packer_t* pk = data;
53
+ msgpack_packer_mark(pk);
54
+ }
55
+
56
+ static void Packer_free(void* data)
57
+ {
58
+ msgpack_packer_t* pk = data;
51
59
  if(pk == NULL) {
52
60
  return;
53
61
  }
@@ -55,13 +63,47 @@ static void Packer_free(msgpack_packer_t* pk)
55
63
  xfree(pk);
56
64
  }
57
65
 
66
+ #ifdef TypedData_Make_Struct
67
+ static size_t Packer_memsize(const void* data)
68
+ {
69
+ return data == NULL ? 0 : sizeof(msgpack_packer_t);
70
+ }
71
+
72
+ static const rb_data_type_t packer_data_type = {
73
+ "CBOR::Packer",
74
+ {Packer_mark, Packer_free, Packer_memsize,},
75
+ NULL, NULL, 0
76
+ };
77
+ #endif
78
+
79
+ static msgpack_packer_t* Packer_get(VALUE self)
80
+ {
81
+ msgpack_packer_t* pk = NULL;
82
+ #ifdef TypedData_Make_Struct
83
+ TypedData_Get_Struct(self, msgpack_packer_t, &packer_data_type, pk);
84
+ #else
85
+ Data_Get_Struct(self, msgpack_packer_t, pk);
86
+ #endif
87
+ return pk;
88
+ }
89
+
90
+ msgpack_packer_t* MessagePack_Packer_get(VALUE self)
91
+ {
92
+ PACKER(self, pk);
93
+ return pk;
94
+ }
95
+
58
96
  static VALUE Packer_alloc(VALUE klass)
59
97
  {
60
- msgpack_packer_t* pk = ALLOC_N(msgpack_packer_t, 1);
98
+ msgpack_packer_t* pk;
99
+ VALUE self;
100
+ #ifdef TypedData_Make_Struct
101
+ self = TypedData_Make_Struct(klass, msgpack_packer_t, &packer_data_type, pk);
102
+ #else
103
+ self = Data_Make_Struct(klass, msgpack_packer_t, Packer_mark, Packer_free, pk);
104
+ #endif
61
105
  msgpack_packer_init(pk);
62
106
 
63
- VALUE self = Data_Wrap_Struct(klass, msgpack_packer_mark, Packer_free, pk);
64
-
65
107
  msgpack_packer_set_to_msgpack_method(pk, s_to_msgpack, self);
66
108
  pk->buffer_ref = MessagePack_Buffer_wrap(PACKER_BUFFER_(pk), self);
67
109
 
@@ -301,4 +343,3 @@ void MessagePack_Packer_module_init(VALUE mMessagePack)
301
343
  rb_define_module_function(mMessagePack, "encode", MessagePack_pack_module_method, -1);
302
344
  rb_define_module_function(mMessagePack, "dump", MessagePack_dump_module_method, -1);
303
345
  }
304
-
@@ -31,9 +31,10 @@
31
31
 
32
32
  extern VALUE cMessagePack_Packer;
33
33
 
34
+ msgpack_packer_t* MessagePack_Packer_get(VALUE self);
35
+
34
36
  void MessagePack_Packer_module_init(VALUE mMessagePack);
35
37
 
36
38
  VALUE MessagePack_pack(int argc, VALUE* argv);
37
39
 
38
40
  #endif
39
-
data/ext/cbor/rbinit.c CHANGED
@@ -43,6 +43,12 @@ void Init_cbor(void)
43
43
  rb_cCBOR_Simple = rb_struct_define(NULL, "value", NULL);
44
44
  rb_define_const(mMessagePack, "Simple", rb_cCBOR_Simple);
45
45
 
46
+ /* CBOR tag numbers accepted by the decode option reconstruct_from_tags:,
47
+ so callers can write reconstruct_from_tags: [CBOR::TAG_REGEXP] instead of
48
+ a bare integer. */
49
+ rb_define_const(mMessagePack, "TAG_URI", INT2FIX(TAG_URI));
50
+ rb_define_const(mMessagePack, "TAG_REGEXP", INT2FIX(TAG_RE));
51
+
46
52
  MessagePack_Buffer_module_init(mMessagePack);
47
53
  MessagePack_Packer_module_init(mMessagePack);
48
54
  MessagePack_Unpacker_module_init(mMessagePack);
data/ext/cbor/unpacker.c CHANGED
@@ -30,10 +30,10 @@
30
30
  #include <math.h> /* for ldexp */
31
31
 
32
32
  /* work around https://bugs.ruby-lang.org/issues/15779 for now
33
- * by limiting preallocation to about a Tebibyte
34
- * limit is 2**n-1 (n==28) so we can avoid a conditional
33
+ * by limiting preallocation to about a half a Mebibyte
34
+ * limit is 2**n-1 (n==16) items so we can avoid a conditional
35
35
  */
36
- #define SANE_PREALLOCATION_MAX 0xFFFFFFFUL
36
+ #define SANE_PREALLOCATION_MAX 0xFFFFUL
37
37
  #define SANE_PREALLOCATE(n) (n & SANE_PREALLOCATION_MAX)
38
38
 
39
39
  #if !defined(DISABLE_RMEM) && !defined(DISABLE_UNPACKER_STACK_RMEM) && \
@@ -515,16 +515,27 @@ int msgpack_unpacker_read_map_header(msgpack_unpacker_t* uk, uint64_t* result_si
515
515
  return msgpack_unpacker_read_container_header(uk, result_size, IB_MAP);
516
516
  }
517
517
 
518
- static VALUE msgpack_unpacker_process_tag(uint64_t tag, VALUE v) {
518
+ static VALUE msgpack_unpacker_process_tag(msgpack_unpacker_t* uk, uint64_t tag, VALUE v) {
519
519
  VALUE res = v;
520
520
  switch (tag) {
521
521
  case TAG_TIME_EPOCH: {
522
522
  return rb_funcall(rb_cTime, rb_intern("at"), 1, v);
523
523
  }
524
524
  case TAG_RE: {
525
+ /* Reconstructing a Regexp runs the decoded data through Regexp.new,
526
+ which the caller may not want when decoding input from an untrusted
527
+ source. Only do it when the caller opts in with
528
+ reconstruct_from_tags: [35]; otherwise return the value as a generic
529
+ tagged object. */
530
+ if (!uk->reconstruct_regexp)
531
+ goto unknown_tag;
525
532
  return rb_funcall(rb_cRegexp, rb_intern("new"), 1, v);
526
533
  }
527
534
  case TAG_URI: {
535
+ /* Likewise, building a URI from the decoded value is opt-in via
536
+ reconstruct_from_tags: [32]. */
537
+ if (!uk->reconstruct_uri)
538
+ goto unknown_tag;
528
539
  if (!rb_const_defined(rb_cObject, rb_intern("URI")))
529
540
  goto unknown_tag;
530
541
  return rb_funcall(rb_const_get(rb_cObject, rb_intern("URI")),
@@ -642,7 +653,7 @@ int msgpack_unpacker_read(msgpack_unpacker_t* uk, size_t target_stack_depth)
642
653
  top->type = STACK_TYPE_MAP_KEY;
643
654
  break;
644
655
  case STACK_TYPE_TAG:
645
- object_complete(uk, msgpack_unpacker_process_tag(top->tag, uk->last_object));
656
+ object_complete(uk, msgpack_unpacker_process_tag(uk, top->tag, uk->last_object));
646
657
  goto done;
647
658
 
648
659
  case STACK_TYPE_ARRAY_INDEF:
@@ -683,6 +694,7 @@ int msgpack_unpacker_read(msgpack_unpacker_t* uk, size_t target_stack_depth)
683
694
  if(msgpack_unpacker_stack_pop(uk) <= target_stack_depth) {
684
695
  return PRIMITIVE_OBJECT_COMPLETE;
685
696
  }
697
+ r = PRIMITIVE_OBJECT_COMPLETE;
686
698
  goto container_completed;
687
699
  }
688
700
  }
data/ext/cbor/unpacker.h CHANGED
@@ -73,7 +73,9 @@ struct msgpack_unpacker_t {
73
73
  int textflag;
74
74
 
75
75
  bool keys_as_symbols; /* Experimental */
76
-
76
+ bool reconstruct_regexp; /* reconstruct tag 35 (Regexp); opt-in */
77
+ bool reconstruct_uri; /* reconstruct tag 32 (URI); opt-in */
78
+
77
79
  VALUE buffer_ref;
78
80
  };
79
81
 
@@ -39,15 +39,23 @@ static VALUE eMalformedFormatError;
39
39
  static VALUE eStackError;
40
40
  static VALUE eTypeError;
41
41
 
42
+ static msgpack_unpacker_t* Unpacker_get(VALUE self);
43
+
42
44
  #define UNPACKER(from, name) \
43
- msgpack_unpacker_t *name = NULL; \
44
- Data_Get_Struct(from, msgpack_unpacker_t, name); \
45
+ msgpack_unpacker_t *name = Unpacker_get(from); \
45
46
  if(name == NULL) { \
46
47
  rb_raise(rb_eArgError, "NULL found for " # name " when shouldn't be."); \
47
48
  }
48
49
 
49
- static void Unpacker_free(msgpack_unpacker_t* uk)
50
+ static void Unpacker_mark(void* data)
51
+ {
52
+ msgpack_unpacker_t* uk = data;
53
+ msgpack_unpacker_mark(uk);
54
+ }
55
+
56
+ static void Unpacker_free(void* data)
50
57
  {
58
+ msgpack_unpacker_t* uk = data;
51
59
  if(uk == NULL) {
52
60
  return;
53
61
  }
@@ -55,18 +63,64 @@ static void Unpacker_free(msgpack_unpacker_t* uk)
55
63
  xfree(uk);
56
64
  }
57
65
 
66
+ #ifdef TypedData_Make_Struct
67
+ static size_t Unpacker_memsize(const void* data)
68
+ {
69
+ return data == NULL ? 0 : sizeof(msgpack_unpacker_t);
70
+ }
71
+
72
+ static const rb_data_type_t unpacker_data_type = {
73
+ "CBOR::Unpacker",
74
+ {Unpacker_mark, Unpacker_free, Unpacker_memsize,},
75
+ NULL, NULL, 0
76
+ };
77
+ #endif
78
+
79
+ static msgpack_unpacker_t* Unpacker_get(VALUE self)
80
+ {
81
+ msgpack_unpacker_t* uk = NULL;
82
+ #ifdef TypedData_Make_Struct
83
+ TypedData_Get_Struct(self, msgpack_unpacker_t, &unpacker_data_type, uk);
84
+ #else
85
+ Data_Get_Struct(self, msgpack_unpacker_t, uk);
86
+ #endif
87
+ return uk;
88
+ }
89
+
58
90
  static VALUE Unpacker_alloc(VALUE klass)
59
91
  {
60
- msgpack_unpacker_t* uk = ALLOC_N(msgpack_unpacker_t, 1);
92
+ msgpack_unpacker_t* uk;
93
+ VALUE self;
94
+ #ifdef TypedData_Make_Struct
95
+ self = TypedData_Make_Struct(klass, msgpack_unpacker_t, &unpacker_data_type, uk);
96
+ #else
97
+ self = Data_Make_Struct(klass, msgpack_unpacker_t, Unpacker_mark, Unpacker_free, uk);
98
+ #endif
61
99
  msgpack_unpacker_init(uk);
62
100
 
63
- VALUE self = Data_Wrap_Struct(klass, msgpack_unpacker_mark, Unpacker_free, uk);
64
-
65
101
  uk->buffer_ref = MessagePack_Buffer_wrap(UNPACKER_BUFFER_(uk), self);
66
102
 
67
103
  return self;
68
104
  }
69
105
 
106
+ /* reconstruct_from_tags: a list of CBOR tag numbers whose decoded value should be
107
+ reconstructed into a live Ruby object. Examined with #include? .
108
+ Only the object-constructing tags
109
+ are gated (35 -> Regexp, 32 -> URI); anything else decodes to CBOR::Tagged.
110
+ Absent or not responding to include? means "none", the safe default. */
111
+ static void set_reconstruct_from_tags(msgpack_unpacker_t* uk, VALUE options)
112
+ {
113
+ VALUE tags;
114
+ if (!RB_TYPE_P(options, T_HASH))
115
+ return;
116
+ tags = rb_hash_aref(options, ID2SYM(rb_intern("reconstruct_from_tags")));
117
+ ID include_p = rb_intern("include?");
118
+ if (rb_respond_to(tags, include_p)) {
119
+ uk->reconstruct_regexp = RTEST(rb_funcall(tags, include_p, 1, INT2FIX(TAG_RE)));
120
+ uk->reconstruct_uri = RTEST(rb_funcall(tags, include_p, 1, INT2FIX(TAG_URI)));
121
+ }
122
+ }
123
+
70
124
  static VALUE Unpacker_initialize(int argc, VALUE* argv, VALUE self)
71
125
  {
72
126
  VALUE io = Qnil;
@@ -101,6 +155,7 @@ static VALUE Unpacker_initialize(int argc, VALUE* argv, VALUE self)
101
155
  VALUE v;
102
156
  v = rb_hash_aref(options, ID2SYM(rb_intern("symbolize_keys")));
103
157
  uk->keys_as_symbols = RTEST(v);
158
+ set_reconstruct_from_tags(uk, options);
104
159
  }
105
160
  }
106
161
 
@@ -344,7 +399,8 @@ VALUE MessagePack_unpack(int argc, VALUE* argv)
344
399
  msgpack_buffer_set_write_reference_threshold(UNPACKER_BUFFER_(uk), 0);
345
400
 
346
401
  uk->keys_as_symbols = keys_as_symbols;
347
-
402
+ set_reconstruct_from_tags(uk, options);
403
+
348
404
  if(io != Qnil) {
349
405
  MessagePack_Buffer_initialize(UNPACKER_BUFFER_(uk), io, Qnil);
350
406
  }
@@ -426,4 +482,3 @@ void MessagePack_Unpacker_module_init(VALUE mMessagePack)
426
482
  rb_define_module_function(mMessagePack, "unpack", MessagePack_unpack_module_method, -1);
427
483
  rb_define_module_function(mMessagePack, "decode", MessagePack_unpack_module_method, -1);
428
484
  }
429
-
data/lib/cbor/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module CBOR
2
- VERSION = "0.5.10.1"
2
+ VERSION = "0.6.2.2"
3
3
  end
data/spec/format_spec.rb CHANGED
@@ -334,6 +334,66 @@ describe MessagePack do
334
334
  }.should raise_error(MessagePack::MalformedFormatError)
335
335
  end
336
336
 
337
+ # Regression tests for https://github.com/cabo/cbor-ruby/issues/20
338
+ # Nested indefinite-length containers should decode correctly.
339
+
340
+ it "{_ 'a' => [_ 1]}" do
341
+ # indefinite map containing an indefinite array
342
+ check_decode "\xbf\x61a\x9f\x01\xff\xff", {"a" => [1]}
343
+ end
344
+
345
+ it "{_ 'a' => [_ 1], 'b' => [2, 3]}" do
346
+ # indefinite map containing an indefinite and a definite array
347
+ check_decode "\xbf\x61a\x9f\x01\xff\x61b\x82\x02\x03\xff", {"a" => [1], "b" => [2, 3]}
348
+ end
349
+
350
+ it "{_ 'a' => {_ 'b' => 1}}" do
351
+ # indefinite map containing an indefinite map
352
+ check_decode "\xbf\x61a\xbf\x61b\x01\xff\xff", {"a" => {"b" => 1}}
353
+ end
354
+
355
+ it "{_ 'a' => {_ }}" do
356
+ # indefinite map containing an empty indefinite map
357
+ check_decode "\xbf\x61a\xbf\xff\xff", {"a" => {}}
358
+ end
359
+
360
+ it "{_ 'a' => {_ NO BREAK}" do
361
+ # indefinite map containing an empty indefinite map
362
+ lambda {
363
+ check_decode "\xbf\x61a\xbf\xff", {"a" => {}}
364
+ }.should raise_error(EOFError)
365
+ end
366
+
367
+ it "{_ 'a' => {} NO BREAK" do
368
+ # indefinite map containing an empty indefinite map
369
+ lambda {
370
+ check_decode "\xbf\x61a\xa0", {"a" => {}}
371
+ }.should raise_error(EOFError)
372
+ end
373
+
374
+ it "{_ 'a' => {_ 'b' => 1}, 'b' => {'c' => 2}}" do
375
+ # indefinite map containing an indefinite and a definite map
376
+ check_decode "\xbf\x61a\xbf\x61b\x01\xff\x61b\xa1\x61c\x02\xff", {"a" => {"b" => 1}, "b" => {"c" => 2}}
377
+ end
378
+
379
+ it "[_ {_ 'a' => 1}]" do
380
+ # indefinite array containing an indefinite map
381
+ check_decode "\x9f\xbf\x61a\x01\xff\xff", [{"a" => 1}]
382
+ end
383
+
384
+ it "{_ 'files' => [{_ 'name' => 'foo'}]}" do
385
+ # indefinite map containing a definite array of indefinite maps
386
+ # (the exact pattern produced by Jackson's CBOR serializer)
387
+ check_decode "\xbf\x65files\x81\xbf\x64name\x63foo\xff\xff",
388
+ {"files" => [{"name" => "foo"}]}
389
+ end
390
+
391
+ it "deeply nested indefinite containers" do
392
+ # {_ "a" => {_ "b" => [_ {_ "c" => 1}]}}
393
+ check_decode "\xbf\x61a\xbf\x61b\x9f\xbf\x61c\x01\xff\xff\xff\xff",
394
+ {"a" => {"b" => [{"c" => 1}]}}
395
+ end
396
+
337
397
  it "(_ a b)" do
338
398
  check_decode "\x7f\xff", "".encode_as_utf8
339
399
  check_decode "\x5f\xff", "".b
@@ -390,17 +450,25 @@ describe MessagePack do
390
450
  end
391
451
 
392
452
  it "URI" do
393
- # check_decode "\xd8\x20\x78\x13http://www.ietf.org", CBOR::Tagged.new(32, "http://www.ietf.org")
394
453
  require 'uri'
395
- check_decode "\xd8\x20\x78\x13http://www.ietf.org", URI.parse("http://www.ietf.org")
396
- # This doesn't work yet if 'uri' is not required before 'cbor':
397
- # check 6, URI.parse("http://www.ietf.org")
454
+ # Safe default: object-constructing tags are not reconstructed.
455
+ check_decode "\xd8\x20\x78\x13http://www.ietf.org", CBOR::Tagged.new(32, "http://www.ietf.org")
456
+ # Opt in to reconstruct the URI by listing its tag.
457
+ check_decode "\xd8\x20\x78\x13http://www.ietf.org", URI.parse("http://www.ietf.org"), reconstruct_from_tags: [CBOR::TAG_URI]
458
+ expect { check_decode "\xd8\x20\x78\x13h``p://www.ietf.org", URI.parse("http://www.ietf.org"), reconstruct_from_tags: [CBOR::TAG_URI] }.to raise_error(URI::InvalidURIError)
459
+ check_decode "\xd8\x20\x78\x13http://www.ietf.org", URI.parse("http://www.ietf.org"), reconstruct_from_tags: {CBOR::TAG_URI => true}
460
+ check_decode "\xd8\x20\x78\x13http://www.ietf.org", CBOR::Tagged.new(32, "http://www.ietf.org"), reconstruct_from_tags: {4711 => true}
461
+ check_decode "\xd8\x20\x78\x13http://www.ietf.org", CBOR::Tagged.new(32, "http://www.ietf.org"), reconstruct_from_tags: 4711
398
462
  end
399
463
 
400
464
  it "regexp" do
401
- check_decode "\xd8\x23\x63foo", /foo/
402
- check 14, /(?-mix:foo)/
403
- check 6, /foo/
465
+ # Safe default: tag 35 is not compiled into a Regexp.
466
+ check_decode "\xd8\x23\x63foo", CBOR::Tagged.new(35, "foo")
467
+ # Opt in to reconstruct the Regexp by listing its tag.
468
+ check_decode "\xd8\x23\x63foo", /foo/, reconstruct_from_tags: [CBOR::TAG_REGEXP]
469
+ expect { check_decode "\xd8\x23\x63*oo", /foo/, reconstruct_from_tags: [CBOR::TAG_REGEXP] }.to raise_error(RegexpError)
470
+ check 14, /(?-mix:foo)/, reconstruct_from_tags: [CBOR::TAG_REGEXP]
471
+ check 6, /foo/, reconstruct_from_tags: [CBOR::TAG_REGEXP]
404
472
  end
405
473
 
406
474
  it "Unknown simple 0" do
@@ -499,10 +567,10 @@ describe MessagePack do
499
567
  # #check_map 5, (1<<32)-1 # memory error
500
568
  # end
501
569
 
502
- def check(len, obj)
570
+ def check(len, obj, opts = nil)
503
571
  raw = obj.to_cbor.to_s
504
572
  raw.length.should == len
505
- obj2 = MessagePack.unpack(raw)
573
+ obj2 = opts ? MessagePack.unpack(raw, opts) : MessagePack.unpack(raw)
506
574
  obj2.should == obj
507
575
  if obj.respond_to? :encoding
508
576
  obj2.encoding.should == obj.encoding
@@ -528,8 +596,8 @@ describe MessagePack do
528
596
  raw.should == buf
529
597
  end
530
598
 
531
- def check_decode(c, obj)
532
- obj2 = MessagePack.unpack(c)
599
+ def check_decode(c, obj, opts = nil)
600
+ obj2 = opts ? MessagePack.unpack(c, opts) : MessagePack.unpack(c)
533
601
  obj2.should == obj
534
602
  if obj.respond_to? :encoding
535
603
  obj2.encoding.should == obj.encoding
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cbor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.10.1
4
+ version: 0.6.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carsten Bormann, standing on the tall shoulders of Sadayuki Furuhashi
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-08-07 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: bundler
@@ -173,7 +173,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
173
173
  - !ruby/object:Gem::Version
174
174
  version: '0'
175
175
  requirements: []
176
- rubygems_version: 3.6.2
176
+ rubygems_version: 4.0.8
177
177
  specification_version: 4
178
178
  summary: CBOR, Concise Binary Object Representation.
179
179
  test_files: