File-SOPS
view release on metacpan or search on metacpan
lib/File/SOPS/Format/YAML.pm view on Meta::CPAN
push @stack, { kind => 'seq' };
return;
}
if ($name eq 'mapping_end_event' || $name eq 'sequence_end_event') {
pop @stack;
$close_value->();
return;
}
return unless $name eq 'scalar_event' || $name eq 'alias_event';
if (@stack && $stack[-1]{kind} eq 'map' && $stack[-1]{expect} eq 'key') {
$inspect->($event) if $name eq 'scalar_event';
$stack[-1]{key} = $event->{value};
$stack[-1]{expect} = 'value';
return;
}
$inspect->($event) if $name eq 'scalar_event';
$close_value->();
})->parse_string($text);
1;
};
return unless $ok && $found;
return @$found;
}
###############################################################################
# A comment sops wrote as a list element (k108, k76, docs/adr/0041)
#
# sops attaches a YAML comment to the node that FOLLOWS it. Above a mapping key
# that is a `#ENC[...,type:comment]` line, which YAML::XS discards before this
# module sees it -- and sops does not hash it either, so both sides agree and
# the document reads correctly. Above a SEQUENCE entry there is no comment line
# to write, so sops emits the comment as a real element:
#
# list:
# - ENC[AES256_GCM,...,type:comment]
# - ENC[AES256_GCM,...,type:str]
#
# and every parser keeps it. THIS MODULE NO LONGER GUARDS AGAINST THAT. Under
# docs/adr/0024 parse croaked on any type:comment leaf, because reading one as a
# value put a string in the caller's list that the file does not contain
# (k108). Under docs/adr/0041 the leaf is PRESERVED instead: File::SOPS
# decrypts it into a File::SOPS::Comment, keeps it at its index, leaves it out
# of the digest -- measured, that is exactly the digest sops computes -- and
# writes it back as a type:comment element. There is nothing left for a parse
# guard to catch, and the two refusals that remain are on the WRITE side, where
# the shape is one no SOPS store writes (File::SOPS::_encrypt_tree, and
# _reject_unwritable_leaf below for a comment this emitter is asked to write as
# plain text). The read-side twin of the first lives in File::SOPS::_decrypt_tree
# rather than here, because the same document is read through the JSON handler
# too -- which never had this guard, and carried k108's defect unnoticed
# the whole time (measured against sops 3.13.3: `sops -e --output-type json`
# writes type:comment leaves into JSON).
#
# Mapping-position comments are unchanged and remain the open half of k76:
# YAML::XS drops them on the way in and no emitter here can write one.
###############################################################################
# A literal libyaml numifies past the end of a double (k102, docs/adr/0023)
#
# `1e400`, a 401-digit integer, `Inf`, `NaN`: libyaml resolves each of them to a
# number, and the number it lands on is +Inf, -Inf or NaN. go-yaml resolves none
# of them -- strconv.ParseFloat answers ErrRange and yaml.v3 keeps a STRING --
# so sops writes `type:str` and digests the token's own bytes. Ours said `float`
# and digested `+Inf`, which is a document sops wrote that we cannot read and a
# value we cannot write.
#
# The repair is at PARSE time and nowhere else, because what is wrong is our
# parse result and not any guard downstream. _go_scalar_bytes already models the
# Go side correctly for all 29 spellings measured; detect_type already reads the
# SV and nothing else (ADR 0002); the non-finite guard from k59 is right
# about every value it was written for and is untouched here. What this does is
# hand the rest of the distribution the leaf go-yaml sees.
#
# The predicate is the SV, not the text (ADR 0002): a leaf whose PUBLIC SVf_NOK
# and SVf_POK are both set and whose NV is NaN or +-Inf. It cannot collide with
# the tokens Go DOES resolve to a non-finite float -- `.inf .Inf .INF +.inf
# +.Inf +.INF -.inf -.Inf -.INF .nan .NaN .NAN`, the twelve in %GO_CONSTANT --
# because YAML::XS hands every one of those back POK-ONLY. Measured against
# sops 3.13.3: those twelve are `type:float` to sops and fire nothing here; the
# 29 spellings that do fire are `type:str` to sops, every one. The two sets are
# disjoint, and that disjointness is what keeps this from retyping a leaf Go
# reads as a float.
#
# JSON is deliberately NOT walked. sops refuses such a JSON document at
# unmarshal time (`strconv.ParseFloat: value out of range`, exit 2), so the
# croak Format::JSON's leaf earns there is the reference behaviour -- see
# ADR 0020, which predicted this lever and expected it to answer for both
# parsers at once. Measured, it must not.
my $POSITIVE_INFINITY = 9**9**9;
sub _restring_non_finite_leaves {
no warnings 'recursion';
my ($node, $seen) = @_;
# A recursive YAML anchor (`root: &a\n b: *a`) really does come back from
# YAML::XS as a cycle, so this walk carries its own visited set. That keeps
# THIS walk terminating; the encrypt and decrypt walks do not and hang on
# such a document today, which is k110 and not widened into here.
return if $seen->{refaddr($node)}++;
if (ref $node eq 'HASH') {
for my $key (keys %$node) {
ref $node->{$key}
? _restring_non_finite_leaves($node->{$key}, $seen)
: _restring_non_finite_leaf($node->{$key});
}
}
elsif (ref $node eq 'ARRAY') {
for my $entry (@$node) {
ref $entry
? _restring_non_finite_leaves($entry, $seen)
: _restring_non_finite_leaf($entry);
}
}
return;
}
# $_[0] is the caller's element by alias, deliberately: the flags are read off
# the SV the tree holds rather than off a copy, and the replacement is written
# back into the same slot. Nothing here numifies anything -- B reads the NV and
# the PV out of the SV's own slots, so this cannot retype a scalar the way a
# numeric comparison on it would (k32).
sub _restring_non_finite_leaf {
return unless defined $_[0];
my $sv = B::svref_2object(\$_[0]);
my $flags = $sv->FLAGS;
return unless ($flags & B::SVf_NOK()) && ($flags & B::SVf_POK());
my $nv = $sv->NV;
return unless $nv != $nv
|| $nv == $POSITIVE_INFINITY
|| $nv == -$POSITIVE_INFINITY;
# The string half is what go-yaml kept, so it is what replaces the number.
# An empty one is not a token Go reads as anything but a null, and there is
lib/File/SOPS/Format/YAML.pm view on Meta::CPAN
# a dualvar(493, "0755") is exactly the shape Encrypted::assert_representable
# refuses (a "value that carries its own, different string form" -- YAML::XS
# writes the string half, the digest covers the int, and the two disagree).
# So the repaired scalar is a plain integer Go's number, with no public PV,
# and the assert_representable check at Encrypted.pm line 1596 skips it
# entirely: _has_public_pv is false and the int branch is bypassed. The same
# is what sops itself does on the parse side -- it loses the source spelling
# of `0755` because Go's resolver reads it as the integer 493, and that is
# the trailing edge of k127.
sub _go_repair_int_leaf {
return unless defined $_[0] and !ref $_[0];
my $sv = B::svref_2object(\$_[0]);
return unless $sv->FLAGS & B::SVf_IOK;
my $pv = $sv->PV;
return unless length $pv;
return unless $pv =~ /\A[+-]?0\d+\z/;
my $go_bytes = _go_scalar_bytes($pv);
return unless defined $go_bytes && $go_bytes =~ /\A-?\d+\z/;
return if $go_bytes + 0 == $sv->IV; # already agrees; no-op
$_[0] = $go_bytes + 0;
return;
}
###############################################################################
# A YAML infinity the DOCUMENT wrote plain (k105, docs/adr/0026)
#
# The mirror image of the walk above, and it needs a different authority.
# libyaml leaves `.inf` a STRING; gopkg.in/yaml.v3 resolves it to the float
# +Inf, and sops digests `+Inf`. So a document sops writes and `sops -d`
# verifies failed its MAC here, naming nothing.
#
# What makes this repair unlike every other one in this distribution is that
# the missing fact is NOT in the SV. Measured against sops 3.13.3, one
# unencrypted slot per row:
#
# v_unencrypted: .inf Go: float +Inf, digested `+Inf` <- we digested `.inf`
# v_unencrypted: ".inf" Go: string, digested `.inf` <- we agree, today
#
# and YAML::XS hands back the SAME POK-only scalar for both. Both spellings
# also occur in ONE document as easily as in two -- measured, sops writes this
# and reads it back at exit 0:
#
# list_unencrypted:
# - .inf <- digested `+Inf`
# - ".inf" <- digested `.inf`
#
# So a repair keyed on the leaf's TEXT fixes the first row and silently breaks
# the second, which works today. The question is not what the value is; it is
# whether the document wrote the scalar PLAIN, and that is a fact about the
# bytes.
#
# YAML::PP -- already this distribution's second parser (ADR 0001) -- answers
# it. Its Core schema resolves a scalar to a non-finite number IFF the scalar
# was written plain and its token is one of the twelve in %GO_CONSTANT.
# Measured: 12 of 12 plain tokens yes; 0 of 24 quoted; 0 of 12 near misses
# (`.INf` `.iNF` `.Nan` `.NAn` `+.nan` `-.nan` `-.NAN` `.infinity` `.Infinity`
# `Inf` `inf` `NaN`), every one of which is a type:str to sops and reads
# correctly here today. On the WIRE -- which is what this path sees -- sops has
# already normalised `!!float .inf` and `&a .inf` to a bare `.inf` and
# `!!str .inf` and `'.inf'` to a quoted `".inf"`, so only those two spellings
# arrive, and the oracle separates them in all four cases.
#
# This is NOT the "ask YAML::PP instead of modelling Go" that ADR 0013 and
# ADR 0023 rejected. There the question was what a value IS, and YAML::PP
# answers it the way libyaml does -- `0755` is 755 to both and 493 to Go, so it
# agrees with the side that is already wrong. Here it is asked whether a scalar
# was written plain, which is syntax rather than resolution; the VALUE still
# comes from %GO_CONSTANT, and the token has to be in %GO_CONSTANT before
# YAML::PP is consulted at all.
#
# FOR EVERY DOCUMENT, PLAINTEXT INCLUDED (k123, docs/adr/0034). This was
# gated on a `sops:` section until 0.003, on the argument that a plaintext has
# no MAC for a foreign reader to disagree with and that ADR 0013's guard gives
# a better error on the encrypt path anyway -- "a worse message for no gain,
# since neither path can write the document".
#
# ADR 0031 removed the premise: an unencrypted YAML slot holding one of these
# tokens IS written now, byte-identical to what sops writes. So the gate was
# refusing three documents this library can produce, and the two parses
# disagreed about identical bytes -- measured, `decrypt_file` wrote
# `v_unencrypted: .inf` and `encrypt_file` refused to read its own output back.
# sops resolves a plain scalar the same way on every parse; so does this now.
#
# WHAT IS WRITTEN BACK is a dualvar, not a bare infinity, and that is measured
# rather than tidy: YAML::XS writes a bare non-finite NV as `Inf` / `-Inf` /
# `NaN`, tokens go-yaml resolves as STRINGS, and writes dualvar(+Inf, '.inf')
# as `.inf`, the token sops itself writes. So decrypt_file reproduces sops's
# own plaintext byte for byte instead of degrading the value on the first
# round trip. Same shape as ADR 0011's carrier: a float leaf whose string half
# is the text the document contains.
#
# Since k113 (docs/adr/0031) such a document is written back as well: the
# non-finite guard lets a leaf carrying one of these tokens through to
# ADR 0013's foreign-resolution guard, which measures the token the emitter
# really writes. An ENCRYPTED slot is still refused there (k122).
my $PLAIN_STYLE_LOADER = YAML::PP->new(schema => [qw( Core )]);
sub _restore_plain_infinities {
my ($data, $content) = @_;
# The cheapest gate first, and a sound one: a PLAIN scalar is literal text,
# so a token this walk could repair is in the raw bytes or it is nowhere.
# The only way the string `.inf` reaches a leaf without those four bytes
# being in the document is an escape in a QUOTED scalar, which this walk
# does not repair anyway -- so the pre-filter can only be conservative in
# the direction that changes nothing. A document that never mentions one
# pays a single scan and no tree walk at all.
return unless _go_non_finite_in_text($content);
# Then the tree, because `config.info` and `.infrastructure` carry those
# bytes too: one flag per leaf and, for a leaf starting `.`, `+` or `-`,
# one hash lookup. YAML::PP parses nothing unless a candidate is really
# there.
return unless _has_plain_infinity_candidate($data, {});
# FAIL SAFE, deliberately: YAML::PP is a second parser and refuses things
# YAML::XS accepts. Measured, the one that really occurs is a recursive
# anchor -- `Found cyclic ref for alias 'a'` where YAML::XS hands back a
# real Perl cycle -- and such a document is refused downstream anyway
# (k110). Nothing is repaired then, which is exactly today's behaviour
# and today's MAC error, never a partially repaired tree whose digest would
# be wrong in a new way.
my $theirs = eval { $PLAIN_STYLE_LOADER->load_string($content) };
return unless ref $theirs eq 'HASH';
delete $theirs->{sops};
# Collected first and applied only if the two trees turned out structurally
# identical, for the same reason.
my @fix;
return unless _pair_plain_infinities($data, $theirs, \@fix, {});
for my $entry (@fix) {
my ($slot, $token) = @$entry;
$$slot = dualvar(_go_non_finite_double($token), $token);
}
return;
}
# The multi-document counterpart of _restore_plain_infinities (docs/adr/0033
# Decision 4, ADR 0026). The single-document version reparses $content in scalar
# context, which yields the FIRST document only -- on a stream that would pair
# the first reparsed document against every value tree. Here the reparse is in
# LIST context, N documents, and each is paired against its own value tree by
# index. Everything else is the single-document version's mechanism unchanged:
# the same pre-filters, the same structural pairing that abandons the whole
lib/File/SOPS/Format/YAML.pm view on Meta::CPAN
#
# sops itself writes lastmodified: "2026-08-08T21:28:58Z" -- quoted. YAML::XS
# exposes no per-scalar style control (no tag, no forced-quote hook), so the
# only way to get a quoted scalar out of this emitter is to quote it after the
# dump. Kept deliberately narrow: it rewrites one key, only inside the
# top-level `sops:` block, so a user's own `lastmodified:` in the data section
# is never touched. JSON needs none of this -- JSON has no timestamp type and
# its strings are always quoted.
sub _quote_sops_timestamp {
my ($yaml) = @_;
my @lines = split /\n/, $yaml, -1;
my $in_sops = 0;
for my $line (@lines) {
if ($line =~ /\A sops : \s* \z/x) {
$in_sops = 1;
next;
}
# any other column-0 line ends the sops block (Dump sorts keys, so a
# data key sorting after "sops" can follow it)
$in_sops = 0 if $in_sops && $line =~ /\A\S/;
next unless $in_sops;
# already-quoted values are left alone
$line =~ s{
\A (\s+ lastmodified: [ \t]+) (?!['"]) (\S.*?) [ \t]* \z
}{$1"$2"}x;
}
return join "\n", @lines;
}
# The one place this distribution turns a Perl tree into YAML. serialize() is
# this plus the metadata section, and File::SOPS::_serialize_plaintext (what
# decrypt_file writes and what edit hands the editor) is this on its own.
#
# It stays ONE sub because the emitter options are not local taste: the MAC's
# encrypt side walks the tree in sorted order and is correct only because this
# emitter writes keys sorted, and the boolean mode decides whether a
# JSON::PP::Boolean reaches the file as `true` or as
# `!!perl/scalar:JSON::PP::Boolean 1`. A second copy of those options is a
# second answer to a question that has one -- which is what k35 found: the
# plaintext emitter used to work only because this module set
# $YAML::XS::Boolean process-wide at load time.
#
# Consequence, deliberately accepted: this sub is on the wire path. Changing
# what it emits changes the encrypted document too, so it is not a plaintext
# formatting knob.
# The safely-quotable non-finite spellings (docs/adr/0070), scoped to EXACTLY
# the seven ADR 0038/0039 measured. A str leaf whose value is one of these came
# unambiguously from a QUOTED source or a caller's own Perl string, because
# _restore_plain_infinities (docs/adr/0026, 0034) already resolved a bare one to
# a float at parse -- so writing it double-quoted states the type it already
# has, and sops writes and reads the same bytes (measured, sops -d exit 0). The
# other five non-finite tokens in %GO_CONSTANT (`+.Inf +.INF -.Inf -.INF .NAN`)
# are deliberately NOT here: the corpus did not measure them, and "no wider than
# the nine rows" keeps them refused as they are today.
my %QUOTABLE_NON_FINITE =
map { $_ => 1 } ( '.inf', '.Inf', '.INF', '+.inf', '-.inf', '.nan', '.NaN' );
sub emit {
my ($class, $data, %args) = @_;
croak "data required" unless defined $data;
# A multi-document stream is an ArrayRef of document trees (docs/adr/0033,
# k31). A bare HashRef is one document and stays byte-identical.
my @docs = ref $data eq 'ARRAY' ? @$data : ($data);
croak "data required" unless @docs;
# Only a document that a reader re-derives values from has anything to
# disagree with. serialize sets one of these; the plaintext emitters
# (decrypt_file, edit) set neither, and must not -- refusing there would
# refuse to WRITE OUT a document this module reads correctly, and warning
# would warn about a file with no MAC and no second reader. The two differ in
# the verdict only: croak where the MAC covers the leaf, carp where
# mac_only_encrypted means it does not. See docs/adr/0013 and docs/adr/0018.
my $reject_scalar =
$args{mac_covered} ? \&_reject_foreign_resolution
: $args{warn_foreign_resolution} ? \&_warn_foreign_resolution
: undef;
# Force-quoting runs on EVERY path except mac_only_encrypted (warn):
#
# * MAC-covered (docs/adr/0070): a divergent leaf makes the file fail its
# own MAC (the non-finite class) or silently retypes a caller's string
# (the True/False class), and quoting the safe subset is what lets such a
# document be written at all.
# * Plaintext -- decrypt_file, edit (k186): sops writes `".inf"` and
# `"True"` double-quoted, and without this the plaintext emitter wrote
# them bare, so a decrypt_file -> re-encrypt round trip flipped the leaf
# from string to float/bool (a bare `.inf` resolves to +Inf at the next
# parse, docs/adr/0026). Quoting the same safe subset makes the plaintext
# emitter a faithful inverse of what sops wrote.
#
# The mac_only_encrypted (warn) path is the one exception, kept as ADR 0070
# left it: there the document already works and the leaf is not MAC-covered,
# so its bytes are unchanged. Neither the MAC-covered digest (computed over
# the original tree before emit) nor the plaintext output (never hashed) has
# a digest quoting could move, so this is safe on every path it runs.
my $force_quote = !$args{warn_foreign_resolution};
return _emit_docs($class, \@docs, $reject_scalar) unless $force_quote;
# Replace exactly the safely-quotable divergent leaves (the True/False type
# divergence, and the seven parse-unambiguous non-finite str leaves) with
# unique random sentinels, in a COPY of each document. A sentinel is an
# ordinary string the guard passes; every OTHER divergent leaf is left in
# place and still reaches the guard below and is refused. The digest was
# computed over the original tree in File::SOPS::_compute_mac before emit, so
# this copy touches no digest byte and no key's sort position.
my %sentinel;
my @quoted = map {
_sentinel_quotable_leaves($docs[$_], \%sentinel, [], $_)
} 0 .. $#docs;
# Nothing to quote -> today's single Dump, byte-identical.
return _emit_docs($class, \@docs, $reject_scalar) unless %sentinel;
# The guard runs here on the sentinel'd copy. A remaining non-safe divergent
lib/File/SOPS/Format/YAML.pm view on Meta::CPAN
. "different things and neither sops nor this module could read the "
. "file. Pass a plain Perl scalar, or the string you want stored. A "
. "boolean has to be an exact JSON::PP::Boolean (JSON->true / "
. "JSON->false); a subclass of it is written as a tag as well";
}
###############################################################################
# The reader on the other side of the file (k86, docs/adr/0013)
#
# Everything above asks THIS distribution's emitter what it does. This asks what
# Go's gopkg.in/yaml.v3 -- the parser sops uses -- makes of the bytes we are
# about to write, because sops recomputes the MAC from the values ITS parser
# resolves. YAML::XS is libyaml, whose resolver is YAML 1.1 as libyaml
# implements it; yaml.v3's is neither 1.1 nor 1.2 but its own. Where the two
# land on different values, the document and its own MAC state different things.
#
# Measured against sops 3.13.3, leaf under _unencrypted, one document per row:
#
# source we read Go reads sops -d
# 0755 int 755 493 exit 51 <- `mode: 0755`, the real case
# 010 int 10 8 exit 51
# 007 int 7 7 exit 0 <- agrees, 7 is 7 in both bases
# 08 int 8 float 8 exit 0 <- agrees, same digest bytes
# 0o10 str "0o10" int 8 exit 51 <- the mirror case: WE say str
# 0x1f str int 31 exit 51
# 1_000 str int 1000 exit 51
# .inf str +Inf exit 51
# Null str null exit 51
# TRUE str bool exit 51
# True str "True" bool exit 0 <- agrees, Go digests `True`
# 2015-01-01 str time exit 51 <- rendered 2015-01-01T00:00:00Z
# 2015-01-01T12:00:00Z str time exit 0 <- rendered identically
#
# sops itself resolves a plaintext `mode: 0755` to the INTEGER 493 and writes
# that, so the spelling never survives a `sops -e` -- which is why the read path
# needs nothing here, and why the message can name 493 as what to pass instead.
#
# This is NOT typing by text (ADR 0002). detect_type still reads the SV and
# nothing else, and this runs after it. What is inspected here is what a foreign
# parser will make of bytes -- a question the SV cannot answer, because the SV is
# on this side of the file. _quote_sops_timestamp two hundred lines up has
# matched a text pattern in the emitted document for the same reason since 0.003.
# Go's resolveTable: the first byte decides whether the resolver looks at a
# plain scalar at all. Anything else is a string to it, immediately -- which is
# what keeps `localhost` and `supersecret` from paying for any of this.
my $GO_LOOKS_AT = qr/\A[-+.0-9yYnNtTfFoO~]/;
# Go's resolveMap, mapped to the bytes sops's ToBytes derives from each value.
# A bool is Title-cased (the same rule Encrypted::value_to_bytes follows) and a
# null contributes nothing -- measured: a sops-written `x_unencrypted: null`
# verifies against this module's own digest, which covers the empty string.
my %GO_CONSTANT = (
'true' => 'True', 'True' => 'True', 'TRUE' => 'True',
'false' => 'False', 'False' => 'False', 'FALSE' => 'False',
'' => '', '~' => '',
'null' => '', 'Null' => '', 'NULL' => '',
'.inf' => '+Inf', '.Inf' => '+Inf', '.INF' => '+Inf',
'+.inf' => '+Inf', '+.Inf' => '+Inf', '+.INF' => '+Inf',
'-.inf' => '-Inf', '-.Inf' => '-Inf', '-.INF' => '-Inf',
'.nan' => 'NaN', '.NaN' => 'NaN', '.NAN' => 'NaN',
);
# The DOUBLE go-yaml resolves one of those tokens to, or undef for a token it
# does not resolve to a number at all. Derived from %GO_CONSTANT's own byte
# string rather than from a second list of spellings: there is one token list
# in this module, and a copy of it is how a repair and the guard it depends on
# drift apart (ADR 0002's defect class, and k89's shape one level down).
#
# It lives HERE rather than beside its caller because it reads %GO_CONSTANT,
# and a `my` hash is not in scope above its own declaration. See
# _restore_plain_infinities, k105 and docs/adr/0026.
# The four bytes a repairable token cannot be written without. Built from the
# same %GO_CONSTANT keys, so widening that table widens this with it.
my $GO_NON_FINITE_TEXT = do {
my @token = grep { $GO_CONSTANT{$_} =~ /\A(?:[-+]Inf|NaN)\z/ } keys %GO_CONSTANT;
my %tail = map { (substr($_, -4) => 1) } @token;
my $alt = join '|', map { quotemeta } sort keys %tail;
qr/(?:$alt)/;
};
sub _go_non_finite_in_text {
return $_[0] =~ $GO_NON_FINITE_TEXT ? 1 : 0;
}
sub _go_non_finite_double {
my ($token) = @_;
my $bytes = $GO_CONSTANT{$token};
return undef unless defined $bytes;
return $bytes eq '+Inf' ? $POSITIVE_INFINITY
: $bytes eq '-Inf' ? -$POSITIVE_INFINITY
: $bytes eq 'NaN' ? $POSITIVE_INFINITY - $POSITIVE_INFINITY
: undef;
}
# strconv.ParseInt/ParseUint with base 0, as a digit string. The magnitude is
# accumulated in Perl's own integer space, guarded by a STRING comparison
# against the base's uint64 maximum -- dividing to test for overflow is exactly
# where a double stops being exact, and a wrong answer here is a document
# refused or written for the wrong reason.
my %GO_BASE_MAX = (
2 => '1111111111111111111111111111111111111111111111111111111111111111',
8 => '1777777777777777777777',
10 => '18446744073709551615',
16 => 'ffffffffffffffff',
);
my $INT64_MAGNITUDE = 9223372036854775808; # 2**63: int64 min is -this
sub _go_digits {
my ($digits, $base) = @_;
(my $d = lc $digits) =~ s/\A0+(?=.)//;
my $max = $GO_BASE_MAX{$base};
return undef if length($d) > length($max);
return undef if length($d) == length($max) && $d gt $max;
my $v = 0;
$v = $v * $base + index('0123456789abcdef', $_) for split //, $d;
return $v;
}
# undef: not an integer to Go. 'RANGE': an integer it reads as a uint64, which
# sops has no case for -- measured, `sops -e` on a plaintext 9223372036854775808
# fails with `Cannot walk value, unknown type: uint64`, exit 23. Otherwise the
# decimal text sops would digest.
sub _go_int {
my ($p) = @_;
my $neg = ($p =~ s/\A-//) ? 1 : 0;
$p =~ s/\A\+//;
return undef unless length $p;
my $v;
if ($p =~ /\A0[xX]([0-9a-fA-F]+)\z/) { $v = _go_digits($1, 16) }
elsif ($p =~ /\A0[bB]([01]+)\z/) { $v = _go_digits($1, 2) }
elsif ($p =~ /\A0[oO]([0-7]+)\z/) { $v = _go_digits($1, 8) }
elsif ($p =~ /\A0([0-7]*)\z/) { $v = _go_digits($1, 8) }
elsif ($p =~ /\A[1-9][0-9]*\z/) { $v = _go_digits($p, 10) }
else { return undef }
return undef unless defined $v; # past uint64: Go falls through to float
# `-0` is the integer 0 to Go, sign and all: strconv.Itoa(0) is `0`, and a
# document holding `-0` verifies against a digest covering `0` (measured,
# exit 0). The sign only survives on a FLOAT, which is ADR 0006's -0.0.
return $neg && $v ? "-$v" : "$v"
if $neg ? $v <= $INT64_MAGNITUDE : $v < $INT64_MAGNITUDE;
return undef if $neg; # ParseUint rejects a sign
return 'RANGE';
}
# strconv.ParseFloat, through the one conversion this distribution has for a
# double. The CANONICAL TEXT decides whether Go got a number, never a numeric
lib/File/SOPS/Format/YAML.pm view on Meta::CPAN
sops itself does support multi-document YAML, so this is a gap to close rather
than a rule to keep. Its model, measured against sops 3.13.3, is not "several
independent files in one":
=over 4
=item * One metadata section for the whole stream, written into B<every>
document -- the same age blob, C<lastmodified> and C<mac> byte for byte. On
read it is taken from the B<first> document; a stream carrying it only in a
later document is rejected with C<sops metadata not found>.
=item * B<One MAC spanning all documents, in order.> Removing a document or
swapping two of them fails verification.
=item * The AAD carries B<no> document index. A given key path has the same
AAD in every document, so a value encrypted in one document decrypts in
another document's slot at that path.
=item * Documents are joined by C<--->; a B<leading> separator is dropped,
while a trailing one is preserved as a real (empty) document. An empty document
anywhere is a document: it gets its own metadata block and reads back as C<{}>.
=item * Every document must be a mapping. sops rejects a top-level sequence or
scalar itself (C<YAML documents that are sequences are not supported>), which
is the same rule as the HashRef check in L</parse>.
=back
Supporting that means one tree of N branches with a shared metadata and a
digest spanning all of them, which reaches into encryption, MAC computation and
the shape of the public API -- not this parser alone.
=head2 parse
my ($data, $metadata) = File::SOPS::Format::YAML->parse($yaml_string);
Class method to parse a YAML string.
Returns a two-element list:
=over 4
=item 1. C<$data> - HashRef of the data (without the C<sops> section)
=item 2. C<$metadata> - L<File::SOPS::Metadata> object, or C<undef> if no C<sops> section
=back
Dies if the YAML is invalid, doesn't parse to a HashRef, or contains more
than one document. See L</Multi-document YAML>.
Dies too if the document has a top-level C<sops> entry that is B<not> a
mapping -- C<sops: mine>, a list, or an explicit C<null>. Until 0.003 that
entry was deleted from the tree and reported as no metadata at all, so
L<File::SOPS/encrypt_file> wrote the document back without it. See
L<File::SOPS::Metadata/from_hash>, which is where the refusal lives.
=head3 A literal that overflows a double comes back as a string
C<1e400>, a 401-digit integer, and the bare spellings C<Inf>, C<inf>, C<INF>,
C<Infinity>, C<NaN>, C<nan>, C<NAN>, C<-Inf> and C<+Inf> are all resolved to a
B<number> by libyaml, and the number each of them lands on is C<+Inf>, C<-Inf>
or C<NaN>. go-yaml -- the parser sops reads a document with -- resolves none of
them: C<strconv.ParseFloat> answers C<ErrRange> and it keeps a B<string>. So
sops writes C<type:str> for such a leaf and digests the literal's own text.
Since 0.003 this method hands back the string go-yaml sees, so that
L<File::SOPS::Encrypted/detect_type> and the MAC agree with sops. Before it,
such a document could be neither read (17 of 20 measured documents that sops
writes failed MAC verification here) nor written (all 20 hit the non-finite
refusal in L<File::SOPS::Encrypted/assert_representable>).
Three things this is B<not>. It does not touch the twelve spellings go-yaml
really does resolve to a non-finite float -- C<.inf .Inf .INF +.inf +.Inf
+.INF -.inf -.Inf -.INF .nan .NaN .NAN> -- because L<YAML::XS> returns every
one of those as a plain string with no numeric half at all; those have a
repair of their own since 0.003, and a different one, described in
L</A plain infinity comes back as the float go-yaml reads>. It does not loosen
L<File::SOPS::Encrypted/assert_representable>: a caller who passes
L<File::SOPS/encrypt> a real C<9**9**9> still gets the refusal, because what is
repaired here is a parse result and not a rule about values. And it does not
apply to L<File::SOPS::Format::JSON>, where sops refuses the equivalent
document itself, at unmarshal time.
The cost is that the leaf is written back B<quoted> -- C<v: '1e400'> where sops
writes C<v: 1e400>. Both are a string to both parsers, so the digest is the same
text either way. See
L<docs/adr/0023|https://github.com/Getty/p5-file-sops/blob/main/docs/adr/0023-a-yaml-literal-that-overflows-a-double-is-a-string-not-a-float.md>.
=head3 A plain infinity comes back as the float go-yaml reads
The mirror image of the section above, and it needs a different authority.
L<YAML::XS> leaves C<.inf> a B<string>; go-yaml resolves it to the float
C<+Inf>, and sops digests C<+Inf>. So a document sops writes and C<sops -d>
verifies used to fail MAC verification here, with an error that named nothing.
Since 0.003 a leaf the document wrote as a B<plain> scalar, and whose token is
one of the twelve above, comes back as a L<Scalar::Util/dualvar>: the float
go-yaml resolved, carrying the document's own token as its text. So
L<File::SOPS::Encrypted/value_to_bytes> derives C<+Inf> / C<-Inf> / C<NaN> --
the bytes sops put in the MAC -- while every emitter writes the file back
exactly as it read it, and L<File::SOPS/decrypt_file> reproduces sops's own
plaintext byte for byte.
B<The word "plain" is the whole of it>, and it is why this repair is not the
one above. Measured against sops 3.13.3, in one document:
list_unencrypted:
- .inf # go-yaml: a float, digested +Inf
- ".inf" # go-yaml: a string, digested .inf
Both spellings arrive here as the same L<YAML::XS> string, and the two have
different digests. Anything that decided from the leaf's B<text> would fix the
first element and silently break the second, which reads correctly today. So
the document is asked instead: L<YAML::PP>, already this distribution's second
parser, resolves such a scalar to a non-finite number exactly when it was
written plain, and only a token that is already in this module's model of
go-yaml is repaired at all. If L<YAML::PP> refuses the document, or the two
parse trees disagree about its shape, B<nothing> is repaired.
B<This runs on every document, plaintext included>, and the plaintext half is
newer than the rest. Until 0.003 it was gated on the C<sops:> section, on the
argument that a plaintext has no MAC for anyone to disagree with; that gate is
gone, because the document such a plaintext turns into I<can> now be written
(see below), so the gate was refusing a document this module produces. sops
resolves a plain scalar the same way on every parse it makes, and so does this.
B<Such a document is written back> since 0.003: an B<unencrypted> YAML slot
holding one of the twelve tokens reaches the file as that token, with the
digest covering C<+Inf> / C<-Inf> / C<NaN>, which is what sops writes and what
sops digests for the same document. An B<encrypted> slot is still refused --
the wire form there is C<type:float> with the plaintext C<+Inf>, which
L<File::SOPS::Encrypted/encrypt_value> refuses because it cannot see which
format is being written (k122).
See
L<docs/adr/0026|https://github.com/Getty/p5-file-sops/blob/main/docs/adr/0026-a-plain-yaml-infinity-is-the-float-go-yaml-reads.md>,
L<docs/adr/0031|https://github.com/Getty/p5-file-sops/blob/main/docs/adr/0031-a-non-finite-float-that-carries-go-yamls-own-token-is-written.md>
and
L<docs/adr/0034|https://github.com/Getty/p5-file-sops/blob/main/docs/adr/0034-a-plain-scalar-is-resolved-the-same-way-on-every-parse.md>.
=head3 A plain C<lastmodified> in the C<sops> section is warned about
The read-side mirror of what L</serialize> does on the way out. sops writes
C<lastmodified: "2026-08-21T09:05:08Z"> B<quoted>, and so does this
distribution, because go-yaml resolves a B<bare> RFC3339 scalar to a Go
C<time.Time> where sops's own decoder wants a string. Measured against sops
3.13.3:
lastmodified: "2026-08-21T09:05:08Z" sops -d exit 0
lastmodified: 2026-08-21T09:05:08Z sops -d exit 1
'lastmodified' expected type 'string', got unconvertible type
'time.Time'
This method reads both, and since 0.003 it B<carps> on the second: the values
and the MAC are unaffected, but the file is one no sops can open. It is not
refused, because every write path here re-stamps and quotes the timestamp --
so C<rotate>, C<edit>, C<encrypt_in_place> and C<decrypt_file> plus
C<encrypt_file> all turn such a document into one C<sops -d> reads at exit 0,
and a refusal would take that away and leave the file unopenable by every tool.
The warning cannot fire on a document sops reads. All 15 RFC3339 spellings sops
accepts quoted were measured refused bare, and a B<tagged> plain scalar --
C<!!str 2026-08-21T09:05:08Z>, where the explicit tag stops go-yaml's implicit
resolver -- is read by sops at exit 0 and is not warned about here.
The question is B<plain or quoted>, which is a fact about the source bytes and
not about the value: both spellings arrive at
L<File::SOPS::Metadata/from_hash> as the same Perl string, which is why this
lives here and not there. Unlike the infinity above, the answer does not come
from a resolver -- YAML 1.2's Core schema has no timestamp type, so L<YAML::PP>
reads both spellings as the same string too -- but from L<YAML::PP>'s B<parser>,
asked for one scalar's style. A document that parser will not read is not warned
about. See
L<docs/adr/0050|https://github.com/Getty/p5-file-sops/blob/main/docs/adr/0050-a-plain-lastmodified-is-warned-about-because-only-we-can-still-repair-it.md>
and
L<docs/adr/0044|https://github.com/Getty/p5-file-sops/blob/main/docs/adr/0044-the-macs-aad-is-the-timestamp-go-re-formats-not-the-text-the-document-holds.md>.
=head3 A merge key keeps its C<< << >>, and loses its C<!!merge> tag
sops does not expand a YAML merge key. It unmarshals into a C<yaml.Node> tree,
where go-yaml resolves no merges, so C<< << >> survives as an ordinary mapping
key -- and go-yaml's emitter writes the tag its resolver assigned back out
B<explicitly>:
derived:
!!merge <<:
x: ENC[AES256_GCM,...,type:int]
"y": ENC[AES256_GCM,...,type:int]
lib/File/SOPS/Format/YAML.pm view on Meta::CPAN
my $yaml = File::SOPS::Format::YAML->serialize(
data => \%data, # one document
metadata => $metadata_obj,
);
my $stream = File::SOPS::Format::YAML->serialize(
data => [ \%doc1, \%doc2 ], # a multi-document stream
metadata => $metadata_obj,
);
Class method to serialize data and metadata to YAML.
The C<data> parameter is a HashRef for a single document, or an B<ArrayRef of
HashRefs> for a multi-document stream (docs/adr/0033, k31); a one-element
ArrayRef is byte-identical to the bare HashRef. The C<metadata> parameter must be
a L<File::SOPS::Metadata> object.
For a stream, the B<same> metadata section is written into B<every> document,
byte-identical (same age blob, C<lastmodified> and C<mac>) -- the exact inverse
of the read-side detach in L</parse>, which takes the metadata from the first
document only. The documents are joined by C<--->; an empty document in the list
is a real document and comes out as C<{}> carrying only its own C<sops:> section.
Dies if any document has a top-level C<sops> key: that is where the metadata
section is written, so the value would be overwritten. Until 0.003 it was,
silently, and the resulting document failed its own MAC because the digest had
already covered the discarded value.
Returns a YAML string with the C<sops> section appended to each document.
B<A leaf whose YAML spelling Go's parser resolves differently is refused here,
and only here.> The document this method writes carries a MAC, and sops
recomputes that MAC from the values C<gopkg.in/yaml.v3> resolves out of these
bytes -- so a leaf that libyaml and Go read differently makes the file
disagree with its own MAC. C<mode: 0755> is the realistic case: this module
reads 755, Go reads 493, and the file was written silently and rejected later
with C<MAC mismatch>. Refused as well: C<0o10>, C<0x1f>, C<0b101>, C<1_000>,
C<Null>, C<TRUE> and a date that is not already exactly RFC3339 -- all spellings
libyaml leaves a string and Go resolves to something else. C<007>, C<08>,
C<1e3>, C<True>, C<null>, C<yes>, C<1:30> and C<2015-01-01T12:00:00Z> are B<not>
refused: measured, the two resolvers derive the same digest bytes from each of
them. C<.inf> and C<.nan> are B<no longer> refused either -- since
C<docs/adr/0070> they belong to the nine-leaf safe set this emitter force-quotes
instead (see below).
B<Where the refused leaf is already a string, fifteen of these spellings stay
refused as a limitation this distribution states; the seven parse-unambiguous
non-finite ones are the exception and are written double-quoted.> sops does not
resolve a string away: given the string C<".inf">, C<"1_000"> or C<"2015-01-01">
it writes it double-quoted and reads it back -- measured against sops 3.13.3, 22
such spellings, C<sops -d> exit 0 for every one, and this module reads all 22 of
those documents correctly, in both slots, with the MAC verified. For fifteen of
them it cannot B<write> the leaf: a bare C<2015-01-01> and a quoted
C<"2015-01-01"> arrive as the same Perl string, while sops writes
C<2015-01-01T00:00:00Z> for the first and C<"2015-01-01"> for the second, so
quoting a leaf whose source may have been bare would turn a loud refusal into a
silent value divergence. Those fifteen stay refused, and the message names the
two remedies measured to work for them -- encrypt the leaf, or write the
document as JSON. The other seven -- the non-finite str spellings C<.inf>,
C<.Inf>, C<.INF>, C<+.inf>, C<-.inf>, C<.nan> and C<.NaN> -- parse
B<unambiguously>: a bare one is resolved to a float at parse (C<docs/adr/0026>),
so a leaf still holding the string can only have come from a quoted source or a
caller's own Perl string, and double-quoting it states the type it already has.
Since C<docs/adr/0070> this emitter writes those seven double-quoted through a
fail-closed sentinel substitution -- the same nine-leaf safe set as C<True> and
C<False> below -- byte-identical to what sops writes. See
L<docs/adr/0039|https://github.com/Getty/p5-file-sops/blob/main/docs/adr/0039-a-string-leaf-this-emitter-cannot-quote-stays-refused-and-says-so.md>,
k135,
L<docs/adr/0070|https://github.com/Getty/p5-file-sops/blob/main/docs/adr/0070-a-scoped-per-scalar-quote-is-feasible-for-the-non-ambiguous-divergent-string-leaves.md>
and k99.
B<A C<True> or C<False> string is written double-quoted where the MAC covers
it, and warned about where it does not.> The digest bytes agree -- sops renders
a boolean Title-cased, which is the same text this module derives from the
string -- so the MAC holds and C<sops -d> exits 0. What differs is the B<type>:
L<YAML::XS> writes the string as a bare C<True> because libyaml's resolver knows
only C<true> and C<false>, and Go's yaml.v3 reads a boolean out of it. Measured,
sops 3.13.3: from a bare C<True> C<sops -d> hands the value on as C<true>, and
C<sops rotate>, C<sops set> and C<sops edit> each rewrite the leaf to a bare
C<true>, after which this module reads a C<JSON::PP::Boolean> where the caller
put a string. Until C<docs/adr/0019> this was a C<carp> in both MAC modes;
since C<docs/adr/0070> the MAC-covered path double-quotes the leaf instead --
through the same fail-closed sentinel substitution as the seven non-finite
spellings above -- so it stays a string on both sides and a sops write-back
keeps it (C<sops -e "True"> writes C<"True">). In a C<mac_only_encrypted>
document the leaf is B<not> MAC-covered and the document already works, so the
safe-set force-quoting is deliberately not run there and the C<carp> remains:
its message names the two remedies -- encrypt the leaf, or write the document as
JSON, where every string is quoted. Neighbours that look like this one do
B<not> warn or quote, because measured they do not diverge: C<Yes>, C<No>,
C<on>, C<off>, C<y>, C<n> and the rest of YAML 1.1's boolean family are strings
to yaml.v3 and to libyaml alike, C<~> and C<null> are written quoted, and an
RFC3339 timestamp -- a string here and a C<time.Time> to Go -- comes back from
C<sops rotate> as the identical token. See
L<docs/adr/0019|https://github.com/Getty/p5-file-sops/blob/main/docs/adr/0019-a-string-go-resolves-as-a-boolean-is-warned-about-in-both-modes.md>,
k92,
L<docs/adr/0070|https://github.com/Getty/p5-file-sops/blob/main/docs/adr/0070-a-scoped-per-scalar-quote-is-feasible-for-the-non-ambiguous-divergent-string-leaves.md>
and k99.
The refuse-or-warn rule does not apply to an B<encrypted> slot (an C<ENC[...]>
string carries any spelling verbatim), to L</emit> on its own (a plaintext
document has no MAC for a reader to disagree with -- though the nine-leaf
safe-set force-quoting still runs there, so that C<decrypt_file> and C<edit>
write what sops writes, C<docs/adr/0071>; see L</emit>), or to the C<sops>
metadata section (the digest does not cover it). See
L<docs/adr/0013|https://github.com/Getty/p5-file-sops/blob/main/docs/adr/0013-a-yaml-spelling-the-go-parser-resolves-differently-is-refused.md>
and k86.
B<In a C<mac_only_encrypted> document the same leaf is warned about rather than
refused.> There the digest covers encrypted values only, so an unencrypted leaf
cannot make the document disagree with its own MAC -- measured, the same
C<mode_unencrypted: 0755> is C<sops -d> exit 0 with the flag set. What remains
is that sops reads B<493> out of it where this module reads 755, in a file
neither of them complains about, so the check runs and C<carp>s instead of
refusing: the document is written exactly as before. The warning names the
leaf's key path and never the value. Silence it with a local C<$SIG{__WARN__}>
if the divergence is known and accepted. Measured over 217 such documents: 66
warn, all 66 really do diverge, none is refused, and 0 warn about a leaf the two
implementations agree on. See
L<docs/adr/0018|https://github.com/Getty/p5-file-sops/blob/main/docs/adr/0018-a-mac-only-encrypted-document-warns-where-it-cannot-refuse.md>
and k87.
=head2 emit
my $yaml = File::SOPS::Format::YAML->emit(\%data);
Class method to emit a data structure as YAML, with no C<sops> section and no
metadata of any kind -- a plain document. This is what L<File::SOPS/decrypt_file>
writes and what L<File::SOPS/edit> hands to the editor.
Returns UTF-8 encoded bytes, unconditionally: L<YAML::XS> encodes regardless of
whether the strings it is given carry Perl's UTF-8 flag. See
L<File::SOPS/Character encoding>.
The output B<begins with the document-start marker C<--->>, because L<YAML::XS>
always emits one. sops writes none, in either direction. The line is cosmetic --
YAML resolves a document with or without it identically, C<sops -d> accepts
these files, and the MAC covers values rather than serialized text -- and it is
kept rather than stripped, since the MAC's encrypt side rides on this emitter
(C<docs/adr/0001>). See L<File::SOPS/Every YAML file starts with C<--->, where
sops writes none> and k83.
Called on its own -- which is what the plaintext emitters do -- it writes most
YAML spellings unchanged, C<0755> and C<2015-01-01> included, and a bare
C<type:float> C<.inf> stays bare. What it does B<not> pass through untouched is
the safe set L</serialize> force-quotes: a C<True> or C<False> string and the
seven parse-unambiguous non-finite C<str> spellings (C<.inf>, C<.Inf>, C<.INF>,
C<+.inf>, C<-.inf>, C<.nan>, C<.NaN>) are double-quoted here too. That is what
sops itself writes for those leaves, so C<decrypt_file> and C<edit> are faithful
inverses of it: a decrypt then re-encrypt round trip no longer flips such a leaf
from string to float or bool (a bare C<.inf> would resolve to C<+Inf> at the
next parse). It reuses ADR 0070's fail-closed sentinel mechanism unchanged. See
L<docs/adr/0071|https://github.com/Getty/p5-file-sops/blob/main/docs/adr/0071-the-plaintext-emitter-quotes-the-same-safe-set-so-it-is-a-faithful-inverse.md>
and k186.
The refuse-or-warn guard L</serialize> installs against the rest of the divergent
class is deliberately still not here: a plaintext document carries no MAC for a
reader to disagree with, and refusing them would refuse to write out documents
this module reads correctly. L</serialize> turns it on with one of the two
arguments this method takes beyond the tree -- C<< mac_covered => 1 >> to refuse
such a leaf, or C<< warn_foreign_resolution => 1 >> to warn about it, which is
what a C<mac_only_encrypted> document gets. Force-quoting the safe set runs on
every path B<except> the warn one, so a C<True>/C<False> leaf is quoted under
C<mac_covered> and on this plaintext path but still reaches the guard, and is
warned about, under C<warn_foreign_resolution>. See L</serialize>.
L</serialize> is this method plus the metadata section, so both go through the
same emitter options rather than two copies of them. Those options are not
cosmetic -- sorted key emission is what the MAC's encrypt side relies on -- so a
change here moves the encrypted document as well as the plaintext one.
B<Floats are written in a form that parses back to the same double.> L<YAML::XS>
renders a float by Perl stringification, roughly 15 significant digits, while
the MAC digest covers the shortest decimal that round-trips -- up to 17. For a
value needing 16 or 17 the document stated one number and the digest another,
and the file failed its own verification. This method now reparses its own
output and substitutes the canonical decimal from
L<File::SOPS::Encrypted/value_to_bytes> only where the value does not survive,
so a float that already emitted faithfully keeps exactly the bytes it had. In
practice that is most of them: YAML::XS retains the text of every float it
parsed, so only bare NVs -- computed by the caller, or parsed out of JSON --
are ever rewritten. C<NaN> and C<Inf> are unchanged -- they have no YAML form
Go reads back, and L<File::SOPS::Encrypted/assert_representable> refuses them
on the encrypt path. A negative zero B<is> rewritten, and it is the one value
whose written decimal is not L<File::SOPS::Encrypted/value_to_bytes>'s output
verbatim: that is C<-0>, which YAML resolves as an B<integer> and every reader
digests as C<0>, so this emitter writes C<-0.0> instead -- the spelling
measured to read back as the same double in sops 3.13.3 and here. See
L<docs/adr/0006|https://github.com/Getty/p5-file-sops/blob/main/docs/adr/0006-floats-are-emitted-in-a-form-that-parses-back-to-the-same-double.md>.
B<An integer leaf whose string form contradicts its number is refused.>
L<YAML::XS> writes the string half bare -- C<five> for a
C<Scalar::Util::dualvar> of C<5> -- while
L<File::SOPS::Encrypted/detect_type> calls the leaf an C<int>, so the MAC
digest covers the B<number>. The document and its own MAC then state different
things: measured against sops 3.13.3, C<sops -d> exit 51. A source B<spelling>
is not refused here, and that is measured too -- a C<007>, C<+7>, C<-0> or
C<1e3> this emitter received from a YAML parse is written back exactly as it
came, and Go reads the same number the digest covers (exit 0), where
L<File::SOPS::Format::JSON> has to refuse them because it quotes them. The
refusal names the leaf's key path and neither half of the value; an
B<encrypted> slot is unaffected. See k84 and
L<docs/adr/0012|https://github.com/Getty/p5-file-sops/blob/main/docs/adr/0012-an-integer-leaf-whose-string-half-disagrees-is-refused.md>.
B<A reference as a leaf value is refused>, with one exception. L<YAML::XS>
writes a blessed reference as a Perl-specific C<!!perl/> tagged structure --
C<!!perl/hash:Math::BigFloat>, C<!!perl/scalar:Foo>, C<!!perl/regexp> -- and an
unblessed one as C<!!perl/ref> or C<!!perl/code>, while
L<File::SOPS::Encrypted/detect_type> calls the leaf C<str> so the MAC digest
covers its B<stringification>. The document and its own MAC then state
different things, and the file is unreadable to sops and to this module alike.
Until 0.003 it was written anyway, without a word; now it dies naming the class
(never the value).
The exception is an exact L<JSON::PP::Boolean>, which this emitter writes as
bare C<true> / C<false> -- the one reference whose document form and digest
agree. A B<subclass> of it is not covered: C<detect_type> calls it C<bool> but
L<YAML::XS> writes it as a tag, so it is refused like any other object.
Only leaves that reach the document B<verbatim> can trigger this -- values
excluded by the encryption rules, everything in a plaintext document, and the
C<sops> section. A value that gets encrypted is an C<ENC[...]> string by the
time this method sees it, so an object in an encrypted slot is unaffected and
still stores its stringification as C<type:str>. L<File::SOPS::Format::JSON>
has refused the same leaves all along; see
L<docs/adr/0008|https://github.com/Getty/p5-file-sops/blob/main/docs/adr/0008-a-leaf-the-emitter-cannot-write-as-what-the-digest-covers-is-refused.md>.
=head2 format_name
Returns C<'yaml'>.
=head2 file_extensions
Returns a list of file extensions: C<('yaml', 'yml')>.
=head2 detect
if (File::SOPS::Format::YAML->detect($filename)) {
# File is YAML based on extension
}
Class method to detect if a filename is YAML based on extension.
( run in 1.387 second using v1.01-cache-2.11-cpan-60884a8df76 )