File-SOPS
view release on metacpan or search on metacpan
t/24-float-precision.t view on Meta::CPAN
# value_to_bytes, which is what the digest and the next ciphertext
# would be taken over.
my $data = File::SOPS->decrypt(
encrypted => $content, identities => [$secret], format => $format,
);
ok(signbit($data->{negzero}),
'our decrypt keeps the sign (measured before the fix: signbit 0)');
is(File::SOPS::Encrypted->value_to_bytes($data->{negzero}), '-0',
'and the value re-derives the plaintext -0, not 0');
is(File::SOPS::Encrypted->detect_type($data->{negzero}), 'float',
'still a float, so the next write keeps the type:float label too');
# The write side, which is where the drift became a changed document.
File::SOPS->rotate(file => $enc_file, identities => [$secret]);
my $after = `$sops_bin -d $enc_file 2>&1`;
is($? >> 8, 0, 'sops -d accepts the rotated document') or diag("sops: $after");
like($after, qr/negzero"?\s*:\s*-0\b/,
'and it still says -0 (measured before the fix: 0, exit 0, silently)')
or diag("sops output after rotate:\n$after");
like($after, qr/other"?\s*:\s*1\.5\b/, 'the neighbouring float is untouched');
# The plaintext emitters carried the same loss: decrypt_file wrote a
# bare 0 for this leaf before the fix. -0.0 is section 9's spelling,
# and it parses back to the double sops prints as -0.
my $plain_out = scratch_file($format);
File::SOPS->decrypt_file(
input => $enc_file,
output => $plain_out,
identities => [$secret],
format => $format,
);
like(read_binary($plain_out), qr/negzero"?\s*:?\s*:\s*-0\.0\b/,
'decrypt_file writes the sign out too');
};
}
subtest 'the rest of the type:float read ladder does not move (k72)' => sub {
# Straight through File::SOPS::Encrypted, because the claim is about one
# plaintext -> one value -> one set of wire bytes, and a document would
# only add noise. encrypt_value(value => $text, type => 'float') writes
# $text verbatim as the plaintext -- a Perl string is never renormalised --
# which is the documented way to reproduce what another producer wrote.
my $key = "\x01" x 32;
# plaintext => the bytes value_to_bytes must re-derive from the value
# decrypt_value hands back. Every row but the first five is the measured
# behaviour from BEFORE the fix, unchanged.
my @ladder = (
[ '-0', '-0' ], [ '-0.0', '-0' ], [ '-0.00', '-0' ],
[ '-0e0', '-0' ], [ '-0.0e10','-0' ],
[ '0', '0' ], [ '0.0', '0' ], [ '+0', '0' ],
[ '+0.0', '0' ],
[ '-1', '-1' ], [ '1', '1' ], [ '-0.5', '-0.5' ],
[ '0.5', '0.5' ], [ '1.5', '1.5' ], [ '-1.5', '-1.5' ],
[ '3.14', '3.14' ], [ '-3.14','-3.14'], [ '0.1', '0.1' ],
[ '-0.1', '-0.1' ],
[ '0.30000000000000004', '0.30000000000000004' ],
[ '1e20', '100000000000000000000' ],
[ '-1e20', '-100000000000000000000' ],
[ '1e-20', '0.00000000000000000001' ],
[ 'NaN', 'NaN' ], [ 'Inf', '+Inf' ], [ '-Inf', '-Inf' ],
);
for my $row (@ladder) {
my ($plaintext, $expected) = @$row;
my $enc = File::SOPS::Encrypted->encrypt_value(
value => $plaintext, type => 'float', key => $key, aad => '',
);
is($enc->decrypt_bytes(key => $key, aad => ''), $plaintext,
"[$plaintext] the fixture really carries that plaintext");
my $value = $enc->decrypt_value(key => $key, aad => '');
is(File::SOPS::Encrypted->value_to_bytes($value), $expected,
"[$plaintext] re-derives $expected");
}
# The sign is restored from the plaintext, so it must be restored for
# every spelling that names the same double -- and for a negative that
# underflows to zero, which Go's ParseFloat also returns as -0.
for my $negative_zero (qw( -0 -0.0 -0.00 -0e0 -0.0e10 -1e-400 )) {
my $enc = File::SOPS::Encrypted->encrypt_value(
value => $negative_zero, type => 'float', key => $key, aad => '',
);
ok(signbit($enc->decrypt_value(key => $key, aad => '')),
"[$negative_zero] comes back negative");
}
# And a POSITIVE zero must not acquire one.
for my $positive_zero (qw( 0 0.0 +0 +0.0 1e-400 )) {
my $enc = File::SOPS::Encrypted->encrypt_value(
value => $positive_zero, type => 'float', key => $key, aad => '',
);
ok(!signbit($enc->decrypt_value(key => $key, aad => '')),
"[$positive_zero] stays positive");
}
};
###############################################################################
# 11. k61, first half: decrypt_file on an ENCRYPTED float. The ticket
# measured a document the real sops wrote, carrying
# ENC[...,type:float] 0.30000000000000004:
#
# sops -d -> ratio: 0.30000000000000004
# File::SOPS decrypt_file -> ratio: 0.3
#
# Nothing failed. decrypt_value converts the authenticated plaintext with
# + 0.0, producing a bare NV with no PV, and both plaintext emitters then
# rendered that NV at 15 significant digits. Anyone doing decrypt_file,
# hand-edit, encrypt_file had silently changed the number.
#
# ADR 0006 fixed this as a side effect -- decrypt_file goes through the
# same emit() as everything else -- so this section adds no code, only the
# net. The point of the ticket is the SILENT relapse at the next emitter
# rebuild, so the assertion is on the literal digits on the wire, not on a
# round-trip: a round-trip through our own reader would agree with a
# 15-digit emitter about what it had written.
#
# Section 8 is the neighbouring case and deliberately not this one: it
# uses an UNENCRYPTED leaf, which is a different path (the value is the
# parser's, never the cipher's). k61 is specifically about the
# encrypted one, which k58 excluded from its own scope.
#
t/24-float-precision.t view on Meta::CPAN
cmp_ok($ours->{ratio}, '==', $theirs->{ratio},
'decrypt_file and sops -d decode to the same double');
cmp_ok($ours->{ratio}, '==', $full_precision_value,
'and that double is 0.1+0.2, not its 15-digit truncation');
is($ours->{other}, 'hello', 'the neighbouring string leaf came through too');
};
}
###############################################################################
# 12. k73: an encrypted type:float whose plaintext is a WHOLE number came
# back as a value detect_type calls int, so the next write relabelled the
# leaf type:int -- on a document sops itself had written.
#
# _deserialize_value converted with `$data + 0.0`. For an integral result
# that addition sets the PUBLIC SVf_IOK flag (grok_number settles a text
# like `2` as an integer; pp_add calls SvIV_please), and SVf_IOK is exactly
# what _sv_kind reads. ADR 0002 was working as specified; the SV it read
# had been manufactured by our own conversion rather than by a parser.
#
# Nothing failed, which is why it lived: the plaintext is `2` under either
# label, the digest covers `2` either way, and `sops -d` exits 0 before and
# after. What changed was the document's own type field. Measured, both
# formats, three of five type:float leaves relabelled by our rotate.
#
# The fix is `unpack('d', pack('d', $data))` -- a conversion that goes
# through the float64 Go parses into and leaves the SV NOK and nothing
# else. ADR 0009. The ladder below is the k72 ladder with the type
# asserted as well: 12 of its 37 rows reported int before the fix.
###############################################################################
subtest 'a type:float plaintext stays a float, whatever its digits spell (k73)' => sub {
# Straight through File::SOPS::Encrypted for the same reason section 10
# does it: one plaintext -> one value -> one type and one set of wire
# bytes, with no document in between to add noise.
my $key = "\x02" x 32;
# plaintext => the bytes value_to_bytes must re-derive. Every row is
# type:float, because the row's own label says so and nothing this library
# does to the value may contradict it.
my @ladder = (
# The twelve rows that reported int before the fix.
[ '0', '0' ], [ '+0', '0' ], [ '1', '1' ],
[ '-1', '-1' ], [ '2', '2' ], [ '-2', '-2' ],
[ '1e2', '100' ], [ '100', '100' ], [ '-100', '-100' ],
[ '1e-400', '0' ],
# A float64 cannot hold either of these integers, and Go's
# strconv.ParseFloat does not pretend otherwise -- it returns the
# nearest double, which is what we now return and digest. Before the
# fix Perl kept the exact integer in an IV and wrote it out under a
# type:int label, a number and a type the reference implementation
# would never have produced from this plaintext.
[ '9007199254740993', '9007199254740992' ],
[ '9223372036854775807', '9223372036854776000' ],
# Controls: rows that were already float and must not move.
[ '2.0', '2' ], [ '0.0', '0' ], [ '1.5', '1.5' ],
[ '-1.5', '-1.5' ], [ '0.1', '0.1' ],
[ '0.30000000000000004', '0.30000000000000004' ],
[ '1e20', '100000000000000000000' ],
[ '1e-20', '0.00000000000000000001' ],
[ '-0', '-0' ], [ '-1e-400', '-0' ],
[ 'NaN', 'NaN' ], [ 'Inf', '+Inf' ], [ '-Inf', '-Inf' ],
);
for my $row (@ladder) {
my ($plaintext, $expected) = @$row;
my $enc = File::SOPS::Encrypted->encrypt_value(
value => $plaintext, type => 'float', key => $key, aad => '',
);
my $value = $enc->decrypt_value(key => $key, aad => '');
is(File::SOPS::Encrypted->detect_type($value), 'float',
"[$plaintext] comes back as a float, so the next write keeps type:float");
is(File::SOPS::Encrypted->value_to_bytes($value), $expected,
"[$plaintext] re-derives $expected");
}
};
for my $format (qw(yaml json)) {
subtest "[$format] an integral type:float keeps its label through sops -> rotate -> sops (k73)" => sub {
my $plain = scratch_file($format);
write_binary($plain, $format eq 'json'
? qq({\n "whole": 2.0,\n "negwhole": -2.0,\n "zero": 0.0,\n "half": 1.5\n}\n)
: "whole: 2.0\nnegwhole: -2.0\nzero: 0.0\nhalf: 1.5\n");
my $enc_file = scratch_file($format);
system("$sops_bin -e --age $public $plain > $enc_file 2>/dev/null");
is($? >> 8, 0, 'sops -e wrote the fixture') or return;
my $fixture = read_binary($enc_file);
for my $leaf (qw( whole negwhole zero half )) {
like($fixture, qr/\Q$leaf\E"?\s*:\s*"?ENC\[AES256_GCM,[^\]]*type:float\]/,
"sops wrote $leaf as an encrypted type:float")
or diag("fixture:\n$fixture");
}
# The read side, which is where the label was lost.
my $data = File::SOPS->decrypt(
encrypted => $fixture, identities => [$secret], format => $format,
);
for my $leaf (qw( whole negwhole zero half )) {
is(File::SOPS::Encrypted->detect_type($data->{$leaf}), 'float',
"$leaf is still a float after decrypt (measured before the fix: "
. ($leaf eq 'half' ? 'float' : 'int') . ')');
}
cmp_ok($data->{whole}, '==', 2, 'and it is still the number 2');
# The write side, which is where it became a changed document.
File::SOPS->rotate(file => $enc_file, identities => [$secret]);
my $rotated = read_binary($enc_file);
for my $leaf (qw( whole negwhole zero half )) {
like($rotated, qr/\Q$leaf\E"?\s*:\s*"?ENC\[AES256_GCM,[^\]]*type:float\]/,
"$leaf is still type:float after our rotate")
or diag("rotated document:\n$rotated");
}
unlike($rotated, qr/type:int/,
'no leaf acquired a type:int label (measured before the fix: three did)');
my $out = `$sops_bin -d $enc_file 2>&1`;
is($? >> 8, 0, 'sops -d accepts the rotated document') or diag("sops: $out");
like($out, qr/whole"?\s*:\s*2\b/, 'and reads the same value back');
like($out, qr/negwhole"?\s*:\s*-2\b/, 'including the negative one');
like($out, qr/half"?\s*:\s*1\.5\b/, 'and the neighbouring non-integral float');
t/24-float-precision.t view on Meta::CPAN
# Fed back in, it writes the same wire bytes the bare NV would: the
# digest reads SVf_NOK before POK, so the value is still a float and
# value_to_bytes re-derives the text from its numeric half.
is(File::SOPS::Encrypted->detect_type($ours), 'float',
'still a float to the type ladder');
is(File::SOPS::Encrypted->value_to_bytes($ours), $full_precision_text,
'and re-derives the same plaintext for the next write');
# Non-float leaves are untouched.
my $name = File::SOPS->extract(file => $enc_file, path => '["name"]',
identities => [$secret], format => $format);
is($name, 'db', 'a string leaf comes back as itself');
ok(!isdual($name), 'and is not wrapped');
my $port = File::SOPS->extract(file => $enc_file, path => '["port"]',
identities => [$secret], format => $format);
is($port, 5432, 'an int leaf comes back as itself');
is(File::SOPS::Encrypted->detect_type($port), 'int', 'still an int');
my $on = File::SOPS->extract(file => $enc_file, path => '["on"]',
identities => [$secret], format => $format);
is(File::SOPS::Encrypted->detect_type($on), 'bool', 'a bool stays a bool');
};
}
subtest 'the dualvar stops at the leaf extract returns (ADR 0010)' => sub {
# The boundary, asserted so that moving the wrapping into the tree -- the
# obvious "simplification" -- fails here rather than in a document. A
# dualvar in a tree reaches the emitters: measured, Cpanel::JSON::XS writes
# one as a quoted string, so an unencrypted JSON float would silently
# become a string in the file (k78).
my $plain = scratch_file('json');
write_binary($plain, qq({\n "db": { "ratio": $full_precision_text }\n}\n));
my $enc_file = scratch_file('json');
system("$sops_bin -e --age $public $plain > $enc_file 2>/dev/null");
is($? >> 8, 0, 'sops -e wrote the fixture') or return;
my $leaf = File::SOPS->extract(file => $enc_file, path => '["db"]["ratio"]',
identities => [$secret], format => 'json');
is("$leaf", $full_precision_text, 'the leaf itself carries the canonical text');
my $branch = File::SOPS->extract(file => $enc_file, path => '["db"]',
identities => [$secret], format => 'json');
is(ref $branch, 'HASH', 'a branch comes back as the structure it is');
ok(!isdual($branch->{ratio}),
'and the float inside it is a plain scalar, so the emitters keep '
. 'writing it as a number');
cmp_ok($branch->{ratio}, '==', $full_precision_value,
'with the value unchanged');
# decrypt() is the same boundary from the other side. read_file in a
# scalar, because in list context it returns LINES and would shift every
# named argument after it.
my $document = read_binary($enc_file);
my $data = File::SOPS->decrypt(encrypted => $document,
identities => [$secret], format => 'json');
ok(!isdual($data->{db}{ratio}), 'decrypt returns plain scalars too');
};
subtest 'a non-finite float is returned unwrapped (ADR 0010)' => sub {
# +Inf / NaN are value_to_bytes wire spellings, not a number decimal, and
# no emitter has an agreed form for them. A caller printing one gets Perl's
# own text, as before.
my $key = "\x03" x 32;
for my $plaintext (qw( NaN Inf -Inf )) {
my $enc = File::SOPS::Encrypted->encrypt_value(
value => $plaintext, type => 'float', key => $key, aad => '',
);
my $value = $enc->decrypt_value(key => $key, aad => '');
my $wrapped = File::SOPS::Encrypted->canonical_float_dualvar($value);
ok(!isdual($wrapped), "[$plaintext] comes back unwrapped");
}
# And everything that is not a float scalar at all.
for my $case ([ 'a string', 'hello' ], [ 'an int', 42 ],
[ 'undef', undef ], [ 'a hashref', { a => 1 } ]) {
my ($what, $value) = @$case;
my $wrapped = File::SOPS::Encrypted->canonical_float_dualvar($value);
is_deeply($wrapped, $value, "$what is returned unchanged");
}
};
###############################################################################
# 14. k78 / ADR 0011: a float leaf carrying its own string form -- a
# dualvar -- went into an UNENCRYPTED JSON slot as a QUOTED STRING.
# Cpanel::JSON::XS writes a scalar with a public string half as a JSON
# string whenever that half differs from its own rendering of the number,
# and Format::JSON::_float_roundtrips could not see it: it reparsed the
# quoted string, value_to_bytes re-derived that same text from it, the two
# compared equal, and the walk left the leaf alone.
#
# Nothing failed, which is why it needed measuring rather than reasoning
# about. The digest covers the canonical decimal, Go's ToBytes of the JSON
# string is the same bytes, so the MAC holds and `sops -d` exits 0 -- and
# hands back a STRING where the caller passed a number. Measured, 3.13.3:
#
# encrypt(data => { ratio_unencrypted => <extracted float> }, json)
# -> "ratio_unencrypted" : "0.30000000000000004"
# -> sops -d exit 0, value read back as a string
#
# The route in is ordinary caller code, not a contrivance: extract() has
# returned exactly that dualvar for a float leaf since ADR 0010, so
# `my $v = extract(...); encrypt(data => { x_unencrypted => $v })` lands
# there. A YAML parse is the second route -- YAML::XS keeps the source text
# of every scalar it parses, so every float from a YAML document carries
# one.
#
# 0.003 first REFUSED such a leaf. ADR 0011 replaces that with the repair
# the emitter already had: the round-trip check answers no, and the
# Math::BigFloat carrier writes the canonical decimal as a BARE NUMBER --
# the document YAML has always produced for the same leaf, and, for this
# case, byte-identical to the one a bare NV of the same value produces.
# Measured against sops 3.13.3, and pinned below together with the halves
# that must not have moved: the ADR 0005 and ADR 0006 cases keep their
# exact bytes, and a merely-stringified float (which does NOT set the
# public SVf_POK Cpanel reads) never went near any of this.
###############################################################################
subtest 'the extract -> encrypt caller path writes a NUMBER in a JSON plain slot (k78)' => sub {
my $plain = scratch_file('json');
write_binary($plain, qq({\n "ratio": $full_precision_text\n}\n));
my $enc_file = scratch_file('json');
system("$sops_bin -e --age $public $plain > $enc_file 2>/dev/null");
is($? >> 8, 0, 'sops -e wrote the fixture') or return;
( run in 3.042 seconds using v1.01-cache-2.11-cpan-60884a8df76 )