ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CBOR-XS/XS.pm
(Generate patch)

Comparing CBOR-XS/XS.pm (file contents):
Revision 1.68 by root, Wed Jul 17 09:37:16 2019 UTC vs.
Revision 1.89 by root, Fri Sep 8 20:03:06 2023 UTC

64 64
65package CBOR::XS; 65package CBOR::XS;
66 66
67use common::sense; 67use common::sense;
68 68
69our $VERSION = 1.71; 69our $VERSION = 1.87;
70our @ISA = qw(Exporter); 70our @ISA = qw(Exporter);
71 71
72our @EXPORT = qw(encode_cbor decode_cbor); 72our @EXPORT = qw(encode_cbor decode_cbor);
73 73
74use Exporter; 74use Exporter;
121but configures the coder object to be safe to use with untrusted 121but configures the coder object to be safe to use with untrusted
122data. Currently, this is equivalent to: 122data. Currently, this is equivalent to:
123 123
124 my $cbor = CBOR::XS 124 my $cbor = CBOR::XS
125 ->new 125 ->new
126 ->validate_utf8
126 ->forbid_objects 127 ->forbid_objects
127 ->filter (\&CBOR::XS::safe_filter) 128 ->filter (\&CBOR::XS::safe_filter)
128 ->max_size (1e8); 129 ->max_size (1e8);
129 130
130But is more future proof (it is better to crash because of a change than 131But is more future proof (it is better to crash because of a change than
133=cut 134=cut
134 135
135sub new_safe { 136sub new_safe {
136 CBOR::XS 137 CBOR::XS
137 ->new 138 ->new
139 ->validate_utf8
138 ->forbid_objects 140 ->forbid_objects
139 ->filter (\&CBOR::XS::safe_filter) 141 ->filter (\&CBOR::XS::safe_filter)
140 ->max_size (1e8) 142 ->max_size (1e8)
141} 143}
142 144
214communication partner supports the value sharing extensions to CBOR 216communication partner supports the value sharing extensions to CBOR
215(L<http://cbor.schmorp.de/value-sharing>), as without decoder support, the 217(L<http://cbor.schmorp.de/value-sharing>), as without decoder support, the
216resulting data structure might be unusable. 218resulting data structure might be unusable.
217 219
218Detecting shared values incurs a runtime overhead when values are encoded 220Detecting shared values incurs a runtime overhead when values are encoded
219that have a reference counter large than one, and might unnecessarily 221that have a reference counter larger than one, and might unnecessarily
220increase the encoded size, as potentially shared values are encode as 222increase the encoded size, as potentially shared values are encoded as
221shareable whether or not they are actually shared. 223shareable whether or not they are actually shared.
222 224
223At the moment, only targets of references can be shared (e.g. scalars, 225At the moment, only targets of references can be shared (e.g. scalars,
224arrays or hashes pointed to by a reference). Weirder constructs, such as 226arrays or hashes pointed to by a reference). Weirder constructs, such as
225an array with multiple "copies" of the I<same> string, which are hard but 227an array with multiple "copies" of the I<same> string, which are hard but
243isn't prepared for this will not leak memory. 245isn't prepared for this will not leak memory.
244 246
245If C<$enable> is false (the default), then C<decode> will throw an error 247If C<$enable> is false (the default), then C<decode> will throw an error
246when it encounters a self-referential/cyclic data structure. 248when it encounters a self-referential/cyclic data structure.
247 249
248FUTURE DIRECTION: the motivation behind this option is to avoid I<real> 250This option does not affect C<encode> in any way - shared values and
249cycles - future versions of this module might chose to decode cyclic data 251references will always be encoded properly if present.
250structures using weak references when this option is off, instead of 252
251throwing an error. 253=item $cbor = $cbor->allow_weak_cycles ([$enable])
254
255=item $enabled = $cbor->get_allow_weak_cycles
256
257This works like C<allow_cycles> in that it allows the resulting data
258structures to contain cycles, but unlike C<allow_cycles>, those cyclic
259rreferences will be weak. That means that code that recurrsively walks
260the data structure must be prepared with cycles, but at least not special
261precautions must be implemented to free these data structures.
262
263Only those references leading to actual cycles will be weakened - other
264references, e.g. when the same hash or arrray is referenced multiple times
265in an arrray, will be normal references.
252 266
253This option does not affect C<encode> in any way - shared values and 267This option does not affect C<encode> in any way - shared values and
254references will always be encoded properly if present. 268references will always be encoded properly if present.
255 269
256=item $cbor = $cbor->forbid_objects ([$enable]) 270=item $cbor = $cbor->forbid_objects ([$enable])
330strings as CBOR byte strings. 344strings as CBOR byte strings.
331 345
332This option does not affect C<decode> in any way. 346This option does not affect C<decode> in any way.
333 347
334This option has similar advantages and disadvantages as C<text_keys>. In 348This option has similar advantages and disadvantages as C<text_keys>. In
335addition, this option effectively removes the ability to encode byte 349addition, this option effectively removes the ability to automatically
336strings, which might break some C<FREEZE> and C<TO_CBOR> methods that rely 350encode byte strings, which might break some C<FREEZE> and C<TO_CBOR>
337on this, such as bignum encoding, so this option is mainly useful for very 351methods that rely on this.
338simple data. 352
353A workaround is to use explicit type casts, which are unaffected by this option.
339 354
340=item $cbor = $cbor->validate_utf8 ([$enable]) 355=item $cbor = $cbor->validate_utf8 ([$enable])
341 356
342=item $enabled = $cbor->get_validate_utf8 357=item $enabled = $cbor->get_validate_utf8
343 358
453when there is trailing garbage after the CBOR string, it will silently 468when there is trailing garbage after the CBOR string, it will silently
454stop parsing there and return the number of characters consumed so far. 469stop parsing there and return the number of characters consumed so far.
455 470
456This is useful if your CBOR texts are not delimited by an outer protocol 471This is useful if your CBOR texts are not delimited by an outer protocol
457and you need to know where the first CBOR string ends amd the next one 472and you need to know where the first CBOR string ends amd the next one
458starts. 473starts - CBOR strings are self-delimited, so it is possible to concatenate
474CBOR strings without any delimiters or size fields and recover their data.
459 475
460 CBOR::XS->new->decode_prefix ("......") 476 CBOR::XS->new->decode_prefix ("......")
461 => ("...", 3) 477 => ("...", 3)
462 478
463=back 479=back
469Perl data structure in memory at one time, it does allow you to parse a 485Perl data structure in memory at one time, it does allow you to parse a
470CBOR stream incrementally, using a similar to using "decode_prefix" to see 486CBOR stream incrementally, using a similar to using "decode_prefix" to see
471if a full CBOR object is available, but is much more efficient. 487if a full CBOR object is available, but is much more efficient.
472 488
473It basically works by parsing as much of a CBOR string as possible - if 489It basically works by parsing as much of a CBOR string as possible - if
474the CBOR data is not complete yet, the pasrer will remember where it was, 490the CBOR data is not complete yet, the parser will remember where it was,
475to be able to restart when more data has been accumulated. Once enough 491to be able to restart when more data has been accumulated. Once enough
476data is available to either decode a complete CBOR value or raise an 492data is available to either decode a complete CBOR value or raise an
477error, a real decode will be attempted. 493error, a real decode will be attempted.
478 494
479A typical use case would be a network protocol that consists of sending 495A typical use case would be a network protocol that consists of sending
631create such objects. 647create such objects.
632 648
633=item Types::Serialiser::true, Types::Serialiser::false, Types::Serialiser::error 649=item Types::Serialiser::true, Types::Serialiser::false, Types::Serialiser::error
634 650
635These special values become CBOR true, CBOR false and CBOR undefined 651These special values become CBOR true, CBOR false and CBOR undefined
636values, respectively. You can also use C<\1>, C<\0> and C<\undef> directly 652values, respectively.
637if you want.
638 653
639=item other blessed objects 654=item other blessed objects
640 655
641Other blessed objects are serialised via C<TO_CBOR> or C<FREEZE>. See 656Other blessed objects are serialised via C<TO_CBOR> or C<FREEZE>. See
642L<TAG HANDLING AND EXTENSIONS> for specific classes handled by this 657L<TAG HANDLING AND EXTENSIONS> for specific classes handled by this
667 "$x"; # stringified 682 "$x"; # stringified
668 $x .= ""; # another, more awkward way to stringify 683 $x .= ""; # another, more awkward way to stringify
669 print $x; # perl does it for you, too, quite often 684 print $x; # perl does it for you, too, quite often
670 685
671You can force whether a string is encoded as byte or text string by using 686You can force whether a string is encoded as byte or text string by using
672C<utf8::upgrade> and C<utf8::downgrade> (if C<text_strings> is disabled): 687C<utf8::upgrade> and C<utf8::downgrade> (if C<text_strings> is disabled).
673 688
674 utf8::upgrade $x; # encode $x as text string 689 utf8::upgrade $x; # encode $x as text string
675 utf8::downgrade $x; # encode $x as byte string 690 utf8::downgrade $x; # encode $x as byte string
691
692More options are available, see L<TYPE CASTS>, below, and the C<text_keys>
693and C<text_strings> options.
676 694
677Perl doesn't define what operations up- and downgrade strings, so if the 695Perl doesn't define what operations up- and downgrade strings, so if the
678difference between byte and text is important, you should up- or downgrade 696difference between byte and text is important, you should up- or downgrade
679your string as late as possible before encoding. You can also force the 697your string as late as possible before encoding. You can also force the
680use of CBOR text strings by using C<text_keys> or C<text_strings>. 698use of CBOR text strings by using C<text_keys> or C<text_strings>.
695format will be used. Perls that use formats other than IEEE double to 713format will be used. Perls that use formats other than IEEE double to
696represent numerical values are supported, but might suffer loss of 714represent numerical values are supported, but might suffer loss of
697precision. 715precision.
698 716
699=back 717=back
718
719=head2 TYPE CASTS
720
721B<EXPERIMENTAL>: As an experimental extension, C<CBOR::XS> allows you to
722force specific CBOR types to be used when encoding. That allows you to
723encode types not normally accessible (e.g. half floats) as well as force
724string types even when C<text_strings> is in effect.
725
726Type forcing is done by calling a special "cast" function which keeps a
727copy of the value and returns a new value that can be handed over to any
728CBOR encoder function.
729
730The following casts are currently available (all of which are unary
731operators, that is, have a prototype of C<$>):
732
733=over
734
735=item CBOR::XS::as_int $value
736
737Forces the value to be encoded as some form of (basic, not bignum) integer
738type.
739
740=item CBOR::XS::as_text $value
741
742Forces the value to be encoded as (UTF-8) text values.
743
744=item CBOR::XS::as_bytes $value
745
746Forces the value to be encoded as a (binary) string value.
747
748Example: encode a perl string as binary even though C<text_strings> is in
749effect.
750
751 CBOR::XS->new->text_strings->encode ([4, "text", CBOR::XS::bytes "bytevalue"]);
752
753=item CBOR::XS::as_bool $value
754
755Converts a Perl boolean (which can be any kind of scalar) into a CBOR
756boolean. Strictly the same, but shorter to write, than:
757
758 $value ? Types::Serialiser::true : Types::Serialiser::false
759
760=item CBOR::XS::as_float16 $value
761
762Forces half-float (IEEE 754 binary16) encoding of the given value.
763
764=item CBOR::XS::as_float32 $value
765
766Forces single-float (IEEE 754 binary32) encoding of the given value.
767
768=item CBOR::XS::as_float64 $value
769
770Forces double-float (IEEE 754 binary64) encoding of the given value.
771
772=item CBOR::XS::as_cbor $cbor_text
773
774Not a type cast per-se, this type cast forces the argument to be encoded
775as-is. This can be used to embed pre-encoded CBOR data.
776
777Note that no checking on the validity of the C<$cbor_text> is done - it's
778the callers responsibility to correctly encode values.
779
780=item CBOR::XS::as_map [key => value...]
781
782Treat the array reference as key value pairs and output a CBOR map. This
783allows you to generate CBOR maps with arbitrary key types (or, if you
784don't care about semantics, duplicate keys or pairs in a custom order),
785which is otherwise hard to do with Perl.
786
787The single argument must be an array reference with an even number of
788elements.
789
790Note that only the reference to the array is copied, the array itself is
791not. Modifications done to the array before calling an encoding function
792will be reflected in the encoded output.
793
794Example: encode a CBOR map with a string and an integer as keys.
795
796 encode_cbor CBOR::XS::as_map [string => "value", 5 => "value"]
797
798=back
799
800=cut
801
802sub CBOR::XS::as_cbor ($) { bless [$_[0], 0, undef], CBOR::XS::Tagged:: }
803sub CBOR::XS::as_int ($) { bless [$_[0], 1, undef], CBOR::XS::Tagged:: }
804sub CBOR::XS::as_bytes ($) { bless [$_[0], 2, undef], CBOR::XS::Tagged:: }
805sub CBOR::XS::as_text ($) { bless [$_[0], 3, undef], CBOR::XS::Tagged:: }
806sub CBOR::XS::as_float16 ($) { bless [$_[0], 4, undef], CBOR::XS::Tagged:: }
807sub CBOR::XS::as_float32 ($) { bless [$_[0], 5, undef], CBOR::XS::Tagged:: }
808sub CBOR::XS::as_float64 ($) { bless [$_[0], 6, undef], CBOR::XS::Tagged:: }
809
810sub CBOR::XS::as_bool ($) { $_[0] ? $Types::Serialiser::true : $Types::Serialiser::false }
811
812sub CBOR::XS::as_map ($) {
813 ARRAY:: eq ref $_[0]
814 and $#{ $_[0] } & 1
815 or do { require Carp; Carp::croak ("CBOR::XS::as_map only acepts array references with an even number of elements, caught") };
816
817 bless [$_[0], 7, undef], CBOR::XS::Tagged::
818}
700 819
701=head2 OBJECT SERIALISATION 820=head2 OBJECT SERIALISATION
702 821
703This module implements both a CBOR-specific and the generic 822This module implements both a CBOR-specific and the generic
704L<Types::Serialier> object serialisation protocol. The following 823L<Types::Serialier> object serialisation protocol. The following
1057 1176
1058 1177
1059=head1 SECURITY CONSIDERATIONS 1178=head1 SECURITY CONSIDERATIONS
1060 1179
1061Tl;dr... if you want to decode or encode CBOR from untrusted sources, you 1180Tl;dr... if you want to decode or encode CBOR from untrusted sources, you
1062should start with a coder object created via C<new_safe>: 1181should start with a coder object created via C<new_safe> (which implements
1182the mitigations explained below):
1063 1183
1064 my $coder = CBOR::XS->new_safe; 1184 my $coder = CBOR::XS->new_safe;
1065 1185
1066 my $data = $coder->decode ($cbor_text); 1186 my $data = $coder->decode ($cbor_text);
1067 my $cbor = $coder->encode ($data); 1187 my $cbor = $coder->encode ($data);
1089even if all your C<THAW> methods are secure, encoding data structures from 1209even if all your C<THAW> methods are secure, encoding data structures from
1090untrusted sources can invoke those and trigger bugs in those. 1210untrusted sources can invoke those and trigger bugs in those.
1091 1211
1092So, if you are not sure about the security of all the modules you 1212So, if you are not sure about the security of all the modules you
1093have loaded (you shouldn't), you should disable this part using 1213have loaded (you shouldn't), you should disable this part using
1094C<forbid_objects>. 1214C<forbid_objects> or using C<new_safe>.
1095 1215
1096=item CBOR can be extended with tags that call library code 1216=item CBOR can be extended with tags that call library code
1097 1217
1098CBOR can be extended with tags, and C<CBOR::XS> has a registry of 1218CBOR can be extended with tags, and C<CBOR::XS> has a registry of
1099conversion functions for many existing tags that can be extended via 1219conversion functions for many existing tags that can be extended via
1100third-party modules (see the C<filter> method). 1220third-party modules (see the C<filter> method).
1101 1221
1102If you don't trust these, you should configure the "safe" filter function, 1222If you don't trust these, you should configure the "safe" filter function,
1103C<CBOR::XS::safe_filter>, which by default only includes conversion 1223C<CBOR::XS::safe_filter> (C<new_safe> does this), which by default only
1104functions that are considered "safe" by the author (but again, they can be 1224includes conversion functions that are considered "safe" by the author
1105extended by third party modules). 1225(but again, they can be extended by third party modules).
1106 1226
1107Depending on your level of paranoia, you can use the "safe" filter: 1227Depending on your level of paranoia, you can use the "safe" filter:
1108 1228
1109 $cbor->filter (\&CBOR::XS::safe_filter); 1229 $cbor->filter (\&CBOR::XS::safe_filter);
1110 1230
1125the size of CBOR data you accept, or make sure then when your resources 1245the size of CBOR data you accept, or make sure then when your resources
1126run out, that's just fine (e.g. by using a separate process that can 1246run out, that's just fine (e.g. by using a separate process that can
1127crash safely). The size of a CBOR string in octets is usually a good 1247crash safely). The size of a CBOR string in octets is usually a good
1128indication of the size of the resources required to decode it into a Perl 1248indication of the size of the resources required to decode it into a Perl
1129structure. While CBOR::XS can check the size of the CBOR text (using 1249structure. While CBOR::XS can check the size of the CBOR text (using
1130C<max_size>), it might be too late when you already have it in memory, so 1250C<max_size> - done by C<new_safe>), it might be too late when you already
1131you might want to check the size before you accept the string. 1251have it in memory, so you might want to check the size before you accept
1252the string.
1132 1253
1133As for encoding, it is possible to construct data structures that are 1254As for encoding, it is possible to construct data structures that are
1134relatively small but result in large CBOR texts (for example by having an 1255relatively small but result in large CBOR texts (for example by having an
1135array full of references to the same big data structure, which will all be 1256array full of references to the same big data structure, which will all be
1136deep-cloned during encoding by default). This is rarely an actual issue 1257deep-cloned during encoding by default). This is rarely an actual issue
1149method. 1270method.
1150 1271
1151=item Resource-starving attacks: CPU en-/decoding complexity 1272=item Resource-starving attacks: CPU en-/decoding complexity
1152 1273
1153CBOR::XS will use the L<Math::BigInt>, L<Math::BigFloat> and 1274CBOR::XS will use the L<Math::BigInt>, L<Math::BigFloat> and
1154L<Math::BigRat> libraries to represent encode/decode bignums. These can 1275L<Math::BigRat> libraries to represent encode/decode bignums. These can be
1155be very slow (as in, centuries of CPU time) and can even crash your 1276very slow (as in, centuries of CPU time) and can even crash your program
1156program (and are generally not very trustworthy). See the next section for 1277(and are generally not very trustworthy). See the next section on bignum
1157details. 1278security for details.
1158 1279
1159=item Data breaches: leaking information in error messages 1280=item Data breaches: leaking information in error messages
1160 1281
1161CBOR::XS might leak contents of your Perl data structures in its error 1282CBOR::XS might leak contents of your Perl data structures in its error
1162messages, so when you serialise sensitive information you might want to 1283messages, so when you serialise sensitive information you might want to
1225=head1 LIMITATIONS ON PERLS WITHOUT 64-BIT INTEGER SUPPORT 1346=head1 LIMITATIONS ON PERLS WITHOUT 64-BIT INTEGER SUPPORT
1226 1347
1227On perls that were built without 64 bit integer support (these are rare 1348On perls that were built without 64 bit integer support (these are rare
1228nowadays, even on 32 bit architectures, as all major Perl distributions 1349nowadays, even on 32 bit architectures, as all major Perl distributions
1229are built with 64 bit integer support), support for any kind of 64 bit 1350are built with 64 bit integer support), support for any kind of 64 bit
1230integer in CBOR is very limited - most likely, these 64 bit values will 1351value in CBOR is very limited - most likely, these 64 bit values will
1231be truncated, corrupted, or otherwise not decoded correctly. This also 1352be truncated, corrupted, or otherwise not decoded correctly. This also
1232includes string, array and map sizes that are stored as 64 bit integers. 1353includes string, float, array and map sizes that are stored as 64 bit
1354integers.
1233 1355
1234 1356
1235=head1 THREADS 1357=head1 THREADS
1236 1358
1237This module is I<not> guaranteed to be thread safe and there are no 1359This module is I<not> guaranteed to be thread safe and there are no

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines