cbor 0.6.1.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: 81901bcce887dfccdd18d87f3f965a39fd227ae17efd65f8542ff00664e2d9db
4
- data.tar.gz: b18c012f042203da87aa7a6cfc67162fab835fbb3fac175c6ca9c4b848955b35
3
+ metadata.gz: 7f5496d45110e3e4bacb9c4cd725ab7331e3bbbf873a8264099cb101612e13be
4
+ data.tar.gz: ed19073bb8575be84f9ba68f3d21e5aeb5633cd914302d2e386178c2a9d0312d
5
5
  SHA512:
6
- metadata.gz: fba1eb8572bf19ea27ff9ca433161f53081f40a6fb283119a84741691439de10c390e030fe59f1038599f4299b2d958655a9b975d33346c09704d46d8c340d19
7
- data.tar.gz: 97e16103db5fccb67c319541624ff4d7a6333b5d932baccea3a594d1e65e586e683e941dd0042fc58d1b4fb82e1934b655efc754f3489212af7e24195ab69607
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
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:
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
 
@@ -103,6 +103,24 @@ static VALUE Unpacker_alloc(VALUE klass)
103
103
  return self;
104
104
  }
105
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
+
106
124
  static VALUE Unpacker_initialize(int argc, VALUE* argv, VALUE self)
107
125
  {
108
126
  VALUE io = Qnil;
@@ -137,6 +155,7 @@ static VALUE Unpacker_initialize(int argc, VALUE* argv, VALUE self)
137
155
  VALUE v;
138
156
  v = rb_hash_aref(options, ID2SYM(rb_intern("symbolize_keys")));
139
157
  uk->keys_as_symbols = RTEST(v);
158
+ set_reconstruct_from_tags(uk, options);
140
159
  }
141
160
  }
142
161
 
@@ -380,7 +399,8 @@ VALUE MessagePack_unpack(int argc, VALUE* argv)
380
399
  msgpack_buffer_set_write_reference_threshold(UNPACKER_BUFFER_(uk), 0);
381
400
 
382
401
  uk->keys_as_symbols = keys_as_symbols;
383
-
402
+ set_reconstruct_from_tags(uk, options);
403
+
384
404
  if(io != Qnil) {
385
405
  MessagePack_Buffer_initialize(UNPACKER_BUFFER_(uk), io, Qnil);
386
406
  }
data/lib/cbor/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module CBOR
2
- VERSION = "0.6.1.1"
2
+ VERSION = "0.6.2.2"
3
3
  end
data/spec/format_spec.rb CHANGED
@@ -450,17 +450,25 @@ describe MessagePack do
450
450
  end
451
451
 
452
452
  it "URI" do
453
- # check_decode "\xd8\x20\x78\x13http://www.ietf.org", CBOR::Tagged.new(32, "http://www.ietf.org")
454
453
  require 'uri'
455
- check_decode "\xd8\x20\x78\x13http://www.ietf.org", URI.parse("http://www.ietf.org")
456
- # This doesn't work yet if 'uri' is not required before 'cbor':
457
- # 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
458
462
  end
459
463
 
460
464
  it "regexp" do
461
- check_decode "\xd8\x23\x63foo", /foo/
462
- check 14, /(?-mix:foo)/
463
- 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]
464
472
  end
465
473
 
466
474
  it "Unknown simple 0" do
@@ -559,10 +567,10 @@ describe MessagePack do
559
567
  # #check_map 5, (1<<32)-1 # memory error
560
568
  # end
561
569
 
562
- def check(len, obj)
570
+ def check(len, obj, opts = nil)
563
571
  raw = obj.to_cbor.to_s
564
572
  raw.length.should == len
565
- obj2 = MessagePack.unpack(raw)
573
+ obj2 = opts ? MessagePack.unpack(raw, opts) : MessagePack.unpack(raw)
566
574
  obj2.should == obj
567
575
  if obj.respond_to? :encoding
568
576
  obj2.encoding.should == obj.encoding
@@ -588,8 +596,8 @@ describe MessagePack do
588
596
  raw.should == buf
589
597
  end
590
598
 
591
- def check_decode(c, obj)
592
- obj2 = MessagePack.unpack(c)
599
+ def check_decode(c, obj, opts = nil)
600
+ obj2 = opts ? MessagePack.unpack(c, opts) : MessagePack.unpack(c)
593
601
  obj2.should == obj
594
602
  if obj.respond_to? :encoding
595
603
  obj2.encoding.should == obj.encoding
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cbor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1.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