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.67 by root, Thu Nov 15 19:52:41 2018 UTC vs.
Revision 1.70 by root, Sat Nov 9 07:30:36 2019 UTC

38with the added ability of supporting serialisation of Perl objects. (JSON 38with the added ability of supporting serialisation of Perl objects. (JSON
39often compresses better than CBOR though, so if you plan to compress the 39often compresses better than CBOR though, so if you plan to compress the
40data later and speed is less important you might want to compare both 40data later and speed is less important you might want to compare both
41formats first). 41formats first).
42 42
43The primary goal of this module is to be I<correct> and the secondary goal
44is to be I<fast>. To reach the latter goal it was written in C.
45
43To give you a general idea about speed, with texts in the megabyte range, 46To give you a general idea about speed, with texts in the megabyte range,
44C<CBOR::XS> usually encodes roughly twice as fast as L<Storable> or 47C<CBOR::XS> usually encodes roughly twice as fast as L<Storable> or
45L<JSON::XS> and decodes about 15%-30% faster than those. The shorter the 48L<JSON::XS> and decodes about 15%-30% faster than those. The shorter the
46data, the worse L<Storable> performs in comparison. 49data, the worse L<Storable> performs in comparison.
47 50
51 54
52In addition to the core CBOR data format, this module implements a 55In addition to the core CBOR data format, this module implements a
53number of extensions, to support cyclic and shared data structures 56number of extensions, to support cyclic and shared data structures
54(see C<allow_sharing> and C<allow_cycles>), string deduplication (see 57(see C<allow_sharing> and C<allow_cycles>), string deduplication (see
55C<pack_strings>) and scalar references (always enabled). 58C<pack_strings>) and scalar references (always enabled).
56
57The primary goal of this module is to be I<correct> and the secondary goal
58is to be I<fast>. To reach the latter goal it was written in C.
59 59
60See MAPPING, below, on how CBOR::XS maps perl values to CBOR values and 60See MAPPING, below, on how CBOR::XS maps perl values to CBOR values and
61vice versa. 61vice versa.
62 62
63=cut 63=cut
215(L<http://cbor.schmorp.de/value-sharing>), as without decoder support, the 215(L<http://cbor.schmorp.de/value-sharing>), as without decoder support, the
216resulting data structure might be unusable. 216resulting data structure might be unusable.
217 217
218Detecting shared values incurs a runtime overhead when values are encoded 218Detecting shared values incurs a runtime overhead when values are encoded
219that have a reference counter large than one, and might unnecessarily 219that have a reference counter large than one, and might unnecessarily
220increase the encoded size, as potentially shared values are encode as 220increase the encoded size, as potentially shared values are encoded as
221shareable whether or not they are actually shared. 221shareable whether or not they are actually shared.
222 222
223At the moment, only targets of references can be shared (e.g. scalars, 223At the moment, only targets of references can be shared (e.g. scalars,
224arrays or hashes pointed to by a reference). Weirder constructs, such as 224arrays 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 225an array with multiple "copies" of the I<same> string, which are hard but
453when there is trailing garbage after the CBOR string, it will silently 453when there is trailing garbage after the CBOR string, it will silently
454stop parsing there and return the number of characters consumed so far. 454stop parsing there and return the number of characters consumed so far.
455 455
456This is useful if your CBOR texts are not delimited by an outer protocol 456This 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 457and you need to know where the first CBOR string ends amd the next one
458starts. 458starts - CBOR strings are self-delimited, so it is possible to concatenate
459CBOR strings without any delimiters or size fields and recover their data.
459 460
460 CBOR::XS->new->decode_prefix ("......") 461 CBOR::XS->new->decode_prefix ("......")
461 => ("...", 3) 462 => ("...", 3)
462 463
463=back 464=back
1057 1058
1058 1059
1059=head1 SECURITY CONSIDERATIONS 1060=head1 SECURITY CONSIDERATIONS
1060 1061
1061Tl;dr... if you want to decode or encode CBOR from untrusted sources, you 1062Tl;dr... if you want to decode or encode CBOR from untrusted sources, you
1062should start with a coder object created via C<new_safe>: 1063should start with a coder object created via C<new_safe> (which implements
1064the mitigations explained below):
1063 1065
1064 my $coder = CBOR::XS->new_safe; 1066 my $coder = CBOR::XS->new_safe;
1065 1067
1066 my $data = $coder->decode ($cbor_text); 1068 my $data = $coder->decode ($cbor_text);
1067 my $cbor = $coder->encode ($data); 1069 my $cbor = $coder->encode ($data);
1089even if all your C<THAW> methods are secure, encoding data structures from 1091even if all your C<THAW> methods are secure, encoding data structures from
1090untrusted sources can invoke those and trigger bugs in those. 1092untrusted sources can invoke those and trigger bugs in those.
1091 1093
1092So, if you are not sure about the security of all the modules you 1094So, if you are not sure about the security of all the modules you
1093have loaded (you shouldn't), you should disable this part using 1095have loaded (you shouldn't), you should disable this part using
1094C<forbid_objects>. 1096C<forbid_objects> or using C<new_safe>.
1095 1097
1096=item CBOR can be extended with tags that call library code 1098=item CBOR can be extended with tags that call library code
1097 1099
1098CBOR can be extended with tags, and C<CBOR::XS> has a registry of 1100CBOR can be extended with tags, and C<CBOR::XS> has a registry of
1099conversion functions for many existing tags that can be extended via 1101conversion functions for many existing tags that can be extended via
1100third-party modules (see the C<filter> method). 1102third-party modules (see the C<filter> method).
1101 1103
1102If you don't trust these, you should configure the "safe" filter function, 1104If you don't trust these, you should configure the "safe" filter function,
1103C<CBOR::XS::safe_filter>, which by default only includes conversion 1105C<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 1106includes conversion functions that are considered "safe" by the author
1105extended by third party modules). 1107(but again, they can be extended by third party modules).
1106 1108
1107Depending on your level of paranoia, you can use the "safe" filter: 1109Depending on your level of paranoia, you can use the "safe" filter:
1108 1110
1109 $cbor->filter (\&CBOR::XS::safe_filter); 1111 $cbor->filter (\&CBOR::XS::safe_filter);
1110 1112
1125the size of CBOR data you accept, or make sure then when your resources 1127the 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 1128run 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 1129crash 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 1130indication 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 1131structure. 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 1132C<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. 1133have it in memory, so you might want to check the size before you accept
1134the string.
1132 1135
1133As for encoding, it is possible to construct data structures that are 1136As for encoding, it is possible to construct data structures that are
1134relatively small but result in large CBOR texts (for example by having an 1137relatively 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 1138array 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 1139deep-cloned during encoding by default). This is rarely an actual issue
1149method. 1152method.
1150 1153
1151=item Resource-starving attacks: CPU en-/decoding complexity 1154=item Resource-starving attacks: CPU en-/decoding complexity
1152 1155
1153CBOR::XS will use the L<Math::BigInt>, L<Math::BigFloat> and 1156CBOR::XS will use the L<Math::BigInt>, L<Math::BigFloat> and
1154L<Math::BigRat> libraries to represent encode/decode bignums. These can 1157L<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 1158very 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 1159(and are generally not very trustworthy). See the next section on bignum
1157details. 1160security for details.
1158 1161
1159=item Data breaches: leaking information in error messages 1162=item Data breaches: leaking information in error messages
1160 1163
1161CBOR::XS might leak contents of your Perl data structures in its error 1164CBOR::XS might leak contents of your Perl data structures in its error
1162messages, so when you serialise sensitive information you might want to 1165messages, so when you serialise sensitive information you might want to

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines