--- CBOR-XS/XS.pm 2016/04/21 16:24:03 1.49 +++ CBOR-XS/XS.pm 2016/04/26 16:25:49 1.59 @@ -66,7 +66,7 @@ use common::sense; -our $VERSION = 1.41; +our $VERSION = 1.5; our @ISA = qw(Exporter); our @EXPORT = qw(encode_cbor decode_cbor); @@ -249,6 +249,50 @@ This option does not affect C in any way - string references will always be decoded properly if present. +=item $cbor = $cbor->text_keys ([$enable]) + +=item $enabled = $cbor->get_text_keys + +If C<$enabled> is true (or missing), then C will encode all +perl hash keys as CBOR text strings/UTF-8 string, upgrading them as needed. + +If C<$enable> is false (the default), then C will encode hash keys +normally - upgraded perl strings (strings internally encoded as UTF-8) as +CBOR text strings, and downgraded perl strings as CBOR byte strings. + +This option does not affect C in any way. + +This option is useful for interoperability with CBOR decoders that don't +treat byte strings as a form of text. It is especially useful as Perl +gives very little control over hash keys. + +Enabling this option can be slow, as all downgraded hash keys that are +encoded need to be scanned and converted to UTF-8. + +=item $cbor = $cbor->text_strings ([$enable]) + +=item $enabled = $cbor->get_text_strings + +This option works similar to C, above, but works on all strings +(including hash keys), so C has no further effect after +enabling C. + +If C<$enabled> is true (or missing), then C will encode all perl +strings as CBOR text strings/UTF-8 strings, upgrading them as needed. + +If C<$enable> is false (the default), then C will encode strings +normally (but see C) - upgraded perl strings (strings +internally encoded as UTF-8) as CBOR text strings, and downgraded perl +strings as CBOR byte strings. + +This option does not affect C in any way. + +This option has similar advantages and disadvantages as C. In +addition, this option effectively removes the ability to encode byte +strings, which might break some C and C methods that rely +on this, such as bignum encoding, so this option is mainly useful for very +simple data. + =item $cbor = $cbor->validate_utf8 ([$enable]) =item $enabled = $cbor->get_validate_utf8 @@ -263,7 +307,7 @@ If C<$enable> is false (the default), then C will blindly accept UTF-8 data, marking them as valid UTF-8 in the resulting data structure -regardless of whether thats true or not. +regardless of whether that's true or not. Perl isn't too happy about corrupted UTF-8 in strings, but should generally not crash or do similarly evil things. Extensions might be not @@ -546,15 +590,16 @@ $x .= ""; # another, more awkward way to stringify print $x; # perl does it for you, too, quite often -You can force whether a string ie encoded as byte or text string by using -C and C): +You can force whether a string is encoded as byte or text string by using +C and C (if C is disabled): utf8::upgrade $x; # encode $x as text string utf8::downgrade $x; # encode $x as byte string Perl doesn't define what operations up- and downgrade strings, so if the difference between byte and text is important, you should up- or downgrade -your string as late as possible before encoding. +your string as late as possible before encoding. You can also force the +use of CBOR text strings by using C or C. You can force the type to be a CBOR number by numifying it: @@ -665,7 +710,6 @@ sub URI::THAW { my ($class, $serialiser, $uri) = @_; - $class->new ($uri) } @@ -806,7 +850,7 @@ =head2 ENFORCED TAGS These tags are always handled when decoding, and their handling cannot be -overriden by the user. +overridden by the user. =over 4 @@ -844,7 +888,7 @@ =item 22098 (indirection, L) This tag is automatically generated when a reference are encountered (with -the exception of hash and array refernces). It is converted to a reference +the exception of hash and array references). It is converted to a reference when decoding. =item 55799 (self-describe CBOR, RFC 7049) @@ -857,7 +901,7 @@ =head2 NON-ENFORCED TAGS These tags have default filters provided when decoding. Their handling can -be overriden by changing the C<%CBOR::XS::FILTER> entry for the tag, or by +be overridden by changing the C<%CBOR::XS::FILTER> entry for the tag, or by providing a custom C callback when decoding. When they result in decoding into a specific Perl class, the module @@ -885,16 +929,25 @@ C method encodes "small" bigints into normal CBOR integers, and others into positive/negative CBOR bignums. -=item 4, 5 (decimal fraction/bigfloat) +=item 4, 5, 264, 265 (decimal fraction/bigfloat) Both decimal fractions and bigfloats are decoded into L objects. The corresponding C method I -encodes into a decimal fraction. +encodes into a decimal fraction (either tag 4 or 264). + +NaN and infinities are not encoded properly, as they cannot be represented +in CBOR. -CBOR cannot represent bigfloats with I large exponents - conversion -of such big float objects is undefined. +See L for more info. -Also, NaN and infinities are not encoded properly. +=item 30 (rational numbers) + +These tags are decoded into L objects. The corresponding +C method encodes rational numbers with denominator +C<1> via their numerator only, i.e., they become normal integers or +C. + +See L for more info. =item 21, 22, 23 (expected later JSON conversion) @@ -910,48 +963,6 @@ =cut -our %FILTER = ( - # 0 # rfc4287 datetime, utf-8 - # 1 # unix timestamp, any - - 2 => sub { # pos bigint - require Math::BigInt; - Math::BigInt->new ("0x" . unpack "H*", pop) - }, - - 3 => sub { # neg bigint - require Math::BigInt; - -Math::BigInt->new ("0x" . unpack "H*", pop) - }, - - 4 => sub { # decimal fraction, array - require Math::BigFloat; - Math::BigFloat->new ($_[1][1] . "E" . $_[1][0]) - }, - - 5 => sub { # bigfloat, array - require Math::BigFloat; - scalar Math::BigFloat->new ($_[1][1])->blsft ($_[1][0], 2) - }, - - 21 => sub { pop }, # expected conversion to base64url encoding - 22 => sub { pop }, # expected conversion to base64 encoding - 23 => sub { pop }, # expected conversion to base16 encoding - - # 24 # embedded cbor, byte string - - 32 => sub { - require URI; - URI->new (pop) - }, - - # 33 # base64url rfc4648, utf-8 - # 34 # base64 rfc46484, utf-8 - # 35 # regex pcre/ecma262, utf-8 - # 36 # mime message rfc2045, utf-8 -); - - =head1 CBOR and JSON CBOR is supposed to implement a superset of the JSON data model, and is, @@ -1002,6 +1013,39 @@ information you might want to make sure that exceptions thrown by CBOR::XS will not end up in front of untrusted eyes. + +=head1 BIGNUM SECURITY CONSIDERATIONS + +CBOR::XS provides a C method for both L and +L that tries to encode the number in the simplest possible +way, that is, either a CBOR integer, a CBOR bigint/decimal fraction (tag +4) or an arbitrary-exponent decimal fraction (tag 264). Rational numbers +(L, tag 30) can also contain bignums as members. + +CBOR::XS will also understand base-2 bigfloat or arbitrary-exponent +bigfloats (tags 5 and 265), but it will never generate these on its own. + +Using the built-in L support, encoding and decoding +decimal fractions is generally fast. Decoding bigints can be slow for very +big numbers (tens of thousands of digits, something that could potentially +be caught by limiting the size of CBOR texts), and decoding bigfloats or +arbitrary-exponent bigfloats can be I slow (minutes, decades) +for large exponents (roughly 40 bit and longer). + +Additionally, L can take advantage of other bignum +libraries, such as L, which cannot handle big floats with large +exponents, and might simply abort or crash your program, due to their code +quality. + +This can be a concern if you want to parse untrusted CBOR. If it is, you +might want to disable decoding of tag 2 (bigint) and 3 (negative bigint) +types. You should also disable types 5 and 265, as these can be slow even +without bigints. + +Disabling bigints will also partially or fully disable types that rely on +them, e.g. rational numbers that use bignums. + + =head1 CBOR IMPLEMENTATION NOTES This section contains some random implementation notes. They do not @@ -1096,9 +1140,24 @@ Math::BigFloat->new ($_[1][1] . "E" . $_[1][0]) }, + 264 => sub { # decimal fraction with arbitrary exponent + require Math::BigFloat; + Math::BigFloat->new ($_[1][1] . "E" . $_[1][0]) + }, + 5 => sub { # bigfloat, array require Math::BigFloat; - scalar Math::BigFloat->new ($_[1][1])->blsft ($_[1][0], 2) + scalar Math::BigFloat->new ($_[1][1]) * Math::BigFloat->new (2)->bpow ($_[1][0]) + }, + + 265 => sub { # bigfloat with arbitrary exponent + require Math::BigFloat; + scalar Math::BigFloat->new ($_[1][1]) * Math::BigFloat->new (2)->bpow ($_[1][0]) + }, + + 30 => sub { # rational number + require Math::BigRat; + Math::BigRat->new ("$_[1][0]/$_[1][1]") # separate parameters only work in recent versons }, 21 => sub { pop }, # expected conversion to base64url encoding @@ -1129,7 +1188,7 @@ } sub Math::BigInt::TO_CBOR { - if ($_[0] >= -2147483648 && $_[0] <= 2147483647) { + if (-2147483648 <= $_[0] && $_[0] <= 2147483647) { $_[0]->numify } else { my $hex = substr $_[0]->as_hex, 2; @@ -1140,7 +1199,20 @@ sub Math::BigFloat::TO_CBOR { my ($m, $e) = $_[0]->parts; - tag 4, [$e->numify, $m] + + -9223372036854775808 <= $e && $e <= 18446744073709551615 + ? tag 4, [$e->numify, $m] + : tag 264, [$e, $m] +} + +sub Math::BigRat::TO_CBOR { + my ($n, $d) = $_[0]->parts; + + # older versions of BigRat need *=1, as they not always return numbers + + $d*1 == 1 + ? $n*1 + : tag 30, [$n*1, $d*1] } sub Time::Piece::TO_CBOR {