blib/lib/File/SOPS/Format/ENV.pm

filter: Statementstmt 100% 153 / 153 Branchbran 84.8% 73 / 86 Conditioncond 88.2% 15 / 17 MC/DCmcdc 77.7% 7 / 9 Subroutinesub 100% 25 / 25 Podpod 100% 8 / 8 totaltotal 94.3% 281 / 298 CC 58 SCAR 18.0
CC
58
Cov
94%
parse
10.0
parse_in_document_order
9.0
_comment_text
8.1
CRAP 6.0
« blib/lib/File/SOPS/Format/JSON.pm ↑ Summary blib/lib/File/SOPS/Format/YAML.pm »
1 package File::SOPS::Format::ENV;
2 # ABSTRACT: dotenv (.env) format handler for SOPS
3 82 our $VERSION = '0.005';
4 82 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 82 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 82 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 82 ▶use File::SOPS::Comment;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 82 ▶use File::SOPS::Encrypted;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 82 ▶use File::SOPS::Format::ENV::Ordered;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 82 ▶use File::SOPS::Metadata;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 82 ▶use File::SOPS::Metadata::Flat;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 82 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # Same reason as in the other two handlers: every frame between a caller and
15 # this module is this distribution's own, so a refusal raised here reported a
16 # line inside File::SOPS rather than the line the caller wrote encrypt() or
17 # emit() on. See the note in File::SOPS::Format::JSON.
18 82 our @CARP_NOT = qw( File::SOPS File::SOPS::Encrypted );
19  
20 # THE COMMENT SLOT, and it is not a name -- it is the PATH sops authenticates a
21 # comment under.
22 #
23 # Measured against sops 3.13.3: every comment leaf in an env document decrypts
24 # under the AAD `:` and under nothing else, which is _path_to_aad([ '' ]) --
25 # an empty key component. (The same holds for a top-level comment in a YAML
26 # document; a comment inside `map:` is `map:` and inside `list:` is `list:`.
27 # Go joins the path with ':' and appends one, so the root is ':' where this
28 # distribution's _path_to_aad answers '' for a genuinely empty path -- a
29 # difference no YAML or JSON document can reach, because their leaves are
30 # never at the root.)
31 #
32 # So a comment cannot live under an invented key: any other spelling changes
33 # the AAD and the leaf stops decrypting. It cannot live in a mapping VALUE slot
34 # either -- File::SOPS::_encrypt_tree refuses that, because sops reads such a
35 # document at exit 0 and writes Go's comment struct into the key (docs/adr/0041,
36 # measurement 7). What is left is a sequence under the empty key, and a sequence
37 # adds no path component, so every element authenticates under `:` exactly as
38 # sops writes it.
39 82 our $COMMENT_KEY = '';
40  
41 # ONE Flat object for the whole handler, and the only spelling of `sops_` in
42 # this file. is_metadata_key and flatten have to agree about the prefix, and
43 # the way they stay agreeing is by being the same string in one place
44 # (docs/adr/0022).
45 82 my $FLAT = File::SOPS::Metadata::Flat->new(prefix => 'sops_');
46  
47  
48 # THE ONE LINE SCANNER. parse() and parse_in_document_order() both go through
49 # it, deliberately: the second has to classify every line exactly as the first
50 # does or the shapes it returns cannot line up with the tree the MAC walk holds
51 # (docs/adr/0036, condition 1). A second copy of "what is a comment, what is a
52 # key, where does the metadata start" is how those two drift.
53 #
54 # Faithful to the store's own reader, measured: only a truly EMPTY line is
55 # skipped (a line of blanks is refused), a `#` first byte is a comment whatever
56 # follows, the key is everything before the FIRST `=`, and a line with no `=`
57 # is an error. Nothing is trimmed anywhere.
58 34 ▶sub _scan {
Subroutine _scan: called CC=6 SCAR=17.9
59 34 my ($class, $content) = @_;
60  
61 34 my @items;
62 34 my $lineno = 0;
63 34 for my $line (split /\n/, $content, -1) {
64 215 $lineno++;
65 215 ▶ next unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 33 182 215
66  
67 182 ▶ if (substr($line, 0, 1) eq '#') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($line, 0, 1) eq "#") 11 171 182
68 11 push @items, {
69 kind => 'comment',
70 text => substr($line, 1),
71 line => $lineno,
72 };
73 11 next;
74 }
75  
76 171 my $eq = index($line, '=');
77 # The line is NOT quoted into the message: an unreadable line in an
78 # encrypted document is still the document's content.
79 171 ▶ croak "line $lineno is neither a comment nor a KEY=VALUE pair: it has "
Branch: 1 branch
BranchTrueFalseTotal
if $eq < 0 3 168 171
80 . "no '=' and does not start with '#'. sops refuses the same line "
81 . "with 'invalid dotenv input line'"
82 if $eq < 0;
83  
84 168 my $key = substr($line, 0, $eq);
85 168 ▶ push @items, {
Branch: 1 branch
BranchTrueFalseTotal
$FLAT->is_metadata_key($key) ? : 104 64 168
86 kind => ($FLAT->is_metadata_key($key) ? 'metadata' : 'data'),
87 key => $key,
88 value => substr($line, $eq + 1),
89 line => $lineno,
90 };
91 }
92  
93 31 return \@items;
94 }
95  
96 # The wire is UTF-8 bytes and this library's boundary is characters, so every
97 # key, value and comment crossing in gets decoded exactly once -- the crossing
98 # YAML::XS and Cpanel::JSON::XS(utf8) do for the other two formats, and the one
99 # the emitters below undo with an unconditional encode (docs/adr/0003).
100 #
101 # A refusal rather than a silent pass-through of the bytes: leaving them would
102 # make the very next emit double-encode them, so the document would fail its
103 # own MAC with no wrong byte anywhere to point at. utf8::decode leaves the
104 # string untouched when it fails.
105 142 ▶sub _characters {
Subroutine _characters: called CC=2 SCAR=6.9
106 142 my ($bytes, $what) = @_;
107  
108 142 my $chars = $bytes;
109 142 ▶ croak "$what is not valid UTF-8, and a dotenv document has no other way to "
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::decode($chars) 1 141 142
110 . "say what its bytes mean. Both other parsers here refuse the same "
111 . "document; sops, whose strings are byte slices, reads it"
112 unless utf8::decode($chars);
113  
114 141 return $chars;
115 }
116  
117 # The bytes a key goes onto the wire as. The value's half of this is
118 # Encrypted::value_to_bytes, which encodes the same way and for the same
119 # reason; a key has no type ladder to go through, so it is the encode alone.
120 128 ▶sub _key_bytes {
Subroutine _key_bytes: called CC=1 SCAR=0.0
121 128 my ($key) = @_;
122 128 my $bytes = $key;
123 128 utf8::encode($bytes); # unconditional: no-op for ASCII, correct for the rest
124 128 return $bytes;
125 }
126  
127 23 ▶sub parse {
Subroutine parse: called CC=10 SCAR=23.0
Pod documented
128 23 my ($class, $content) = @_;
129 23 ▶ croak "content required" unless defined $content;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 0 23 23
130  
131 23 my (%data, %flat, @comments);
132  
133 23 for my $item (@{ $class->_scan($content) }) {
134 107 my $kind = $item->{kind};
135  
136 107 ▶ if ($kind eq 'comment') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "comment") 8 99 107
137 8 push @comments, _comment_leaf($item);
138 8 next;
139 }
140  
141 99 ▶ if ($kind eq 'metadata') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "metadata") 60 39 99
142 croak "the flat metadata key '$item->{key}' is set twice "
143 . "(line $item->{line})"
144 60 ▶ if exists $flat{ $item->{key} };
Branch: 1 branch
BranchTrueFalseTotal
if exists $flat{$item->{"key"}} 0 60 60
145 60 $flat{ $item->{key} } = $item->{value};
146 60 next;
147 }
148  
149 39 my $key = _characters($item->{key}, "the key on line $item->{line}");
150  
151 39 ▶ croak "line $item->{line} has an EMPTY key, which is where this "
Branch: 1 branch
BranchTrueFalseTotal
if $key eq $File::SOPS::Format::ENV::COMMENT_KEY 1 38 39
152 . "handler keeps the document's comments -- they authenticate "
153 . "under that path and cannot be moved to another one. sops reads "
154 . "such a document, this one cannot represent it. Give the entry a "
155 . "name"
156 if $key eq $COMMENT_KEY;
157  
158 croak "the key '$key' is set twice (line $item->{line}). sops keeps "
159 . "both entries -- its tree is an ordered list, not a map -- and a "
160 . "Perl hash cannot, so this document is refused rather than "
161 . "silently losing one of them"
162 38 ▶ if exists $data{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $data{$key} 1 37 38
163  
164 # Unescape first, decode second: that is the order the store reads in
165 # (bytes, replace, string), and backslash-n is ASCII either way.
166 $data{$key} = _characters(
167 37 $FLAT->unescape_value($item->{value}),
168 "the value of '$key' on line $item->{line}",
169 );
170 }
171  
172 18 ▶ $data{$COMMENT_KEY} = \@comments if @comments;
Branch: 1 branch
BranchTrueFalseTotal
if @comments 6 12 18
173  
174 my $metadata = %flat
175 ? File::SOPS::Metadata->from_hash($FLAT->unflatten({
176 18 ▶ map { $_ => _characters($flat{$_}, "the metadata key '$_'") }
Branch: 1 branch
BranchTrueFalseTotal
%flat ? : 10 8 18
177 keys %flat
178 }))
179 : undef;
180  
181 18 return (\%data, $metadata);
182 }
183  
184 # ENCRYPTED or PLAINTEXT, decided by the type LABEL and not by the position.
185 #
186 # sops types a comment by position -- every `#` line is a Comment item, whatever
187 # it holds -- and this handler cannot, because the same method has to read a
188 # plaintext .env and an encrypted one. Asking for `type:comment` specifically
189 # (rather than "does this look encrypted at all") makes the two answers agree
190 # everywhere it matters: a plaintext comment whose text spells
191 # ENC[...,type:str] becomes an ordinary comment here as it does in sops, and is
192 # excluded from the digest on both sides. What is left ambiguous is a plaintext
193 # comment whose text is a well-formed ENC[...,type:comment] value, which is
194 # somebody pasting an encrypted comment leaf into a plaintext file.
195 8 ▶sub _comment_leaf {
Subroutine _comment_leaf: called CC=3 SCAR=11.0
196 8 my ($item) = @_;
197  
198 8 my $text = $item->{text};
199  
200 8 ▶ return $text
Branch: 1 branch
BranchTrueFalseTotal
if ("File::SOPS::Encrypted"->encrypted_type($text) // "") eq "comment" 2 6 8
Condition100%2/2
"File::SOPS::Encrypted"->encrypted_type($text) // ""
l!l
26
MC/DC100%1/1
"File::SOPS::Encrypted"->encrypted_type($text) // ""
"File::SOPS::Encrypted"->encrypted_type($text)
Truth table100%2/2
A
A= "File::SOPS::Encrypted"->encrypted_type($text)
Aresult
11
00
201 if (File::SOPS::Encrypted->encrypted_type($text) // '') eq 'comment';
202  
203 # NOT unescaped: measured, a comment is verbatim in both directions, where
204 # a data value is not.
205 6 return File::SOPS::Comment->new(
206 text => _characters($text, "the comment on line $item->{line}"));
207 }
208  
209  
210 11 ▶sub parse_in_document_order {
Subroutine parse_in_document_order: called CC=9 SCAR=22.0
Pod documented
211 11 my ($class, $content) = @_;
212 11 ▶ return unless defined $content;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 0 11 11
213  
214 # Declining is safe, guessing is not: a document this handler cannot scan
215 # must not turn a MAC check into an error the caller reads as corruption.
216 # parse() will raise the real message a moment later if the document is
217 # being read at all.
218 11 ▶ my $items = eval { $class->_scan($content) } or return;
Branch: 1 branch
BranchTrueFalseTotal
my $items = eval { do { $class->_scan($content) } } or return 10 1 11
219  
220 10 my %doc;
221 10 tie %doc, 'File::SOPS::Format::ENV::Ordered';
222  
223 10 for my $item (@$items) {
224 69 ▶ if ($item->{kind} eq 'comment') {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->{"kind"} eq "comment") 3 66 69
225 # The bucket takes the position of the FIRST comment, which is
226 # where parse() would put it in a hash that had positions. Only the
227 # shape is read, so what matters is that the sequence is here and
228 # is as long as the tree's.
229 3 ▶ $doc{$COMMENT_KEY} = [] unless ref $doc{$COMMENT_KEY} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $doc{$File::SOPS::Format::ENV::COMMENT_KEY} eq "ARRAY" 2 1 3
230 3 push @{ $doc{$COMMENT_KEY} }, undef;
231 3 next;
232 }
233  
234 66 ▶ next if $item->{kind} eq 'metadata'; # the handler drops it, condition 3
Branch: 1 branch
BranchTrueFalseTotal
if $item->{"kind"} eq "metadata" 44 22 66
235  
236 22 my $key = $item->{key};
237 22 utf8::decode($key);
238  
239 # Both of these are documents parse() refuses. Decline rather than
240 # answer for them.
241 22 ▶ return if $key eq $COMMENT_KEY;
Branch: 1 branch
BranchTrueFalseTotal
if $key eq $File::SOPS::Format::ENV::COMMENT_KEY 0 22 22
242 22 ▶ return if exists $doc{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $doc{$key} 1 21 22
243  
244 21 $doc{$key} = undef;
245 }
246  
247 9 return \%doc;
248 }
249  
250  
251 17 ▶sub serialize {
Subroutine serialize: called CC=4 SCAR=14.2
Pod documented
252 17 my ($class, %args) = @_;
253 17 ▶ my $data = $args{data} // croak "data required";
Condition50%1/2
$args{"data"} // croak("data required")
l!l
170
MC/DC0%0/1
$args{"data"} // croak("data required")
$args{"data"}
Truth table50%1/2
A
A= $args{"data"}
Aresult
11
00
254 17 ▶ my $metadata = $args{metadata} // croak "metadata required";
Condition50%1/2
$args{"metadata"} // croak("metadata required")
l!l
170
MC/DC0%0/1
$args{"metadata"} // croak("metadata required")
$args{"metadata"}
Truth table50%1/2
A
A= $args{"metadata"}
Aresult
11
00
255  
256 # emit() refuses a top-level sops_ key, so the metadata written below
257 # cannot be shadowed by one -- which is the same job Format::JSON's
258 # `sops` refusal does, moved to where this format's collision is.
259 17 my $out = $class->emit($data);
260  
261 # flatten() has already prefixed the keys and escaped the values, in the
262 # order sops writes them: maps sorted, lists ascending. The section goes
263 # LAST, where sops puts it; line order is not load-bearing (measured: a
264 # file with its sops_ lines reversed still decrypts), and the metadata is
265 # excluded from the MAC structurally.
266 $out .= _key_bytes($_->[0]) . '=' . _value_bytes($_->[1]) . "\n"
267 15 for $FLAT->flatten($metadata->to_hash);
268  
269 15 return $out;
270 }
271  
272 # The metadata's values cross the same boundary the data's do -- characters in
273 # the model, UTF-8 on the wire. escape_value has already run and only touches
274 # newlines, so the order of the two is free.
275 88 ▶sub _value_bytes {
Subroutine _value_bytes: called CC=1 SCAR=0.0
276 88 my ($value) = @_;
277 88 my $bytes = $value;
278 88 utf8::encode($bytes);
279 88 return $bytes;
280 }
281  
282  
283 # The one place this distribution turns a Perl tree into a dotenv document.
284 # serialize() is this plus the metadata section, and File::SOPS::_serialize_
285 # plaintext -- what decrypt_file writes and what edit hands the editor -- is
286 # this on its own.
287 #
288 # So this is ON THE WIRE PATH and is not a plaintext formatting knob. In
289 # particular the sorted key order below is not a diff-readability choice: the
290 # MAC's encrypt side hashes leaves in `sort keys` order, and that is the
291 # document's own order only because this loop sorts. Measured on a file sops
292 # wrote, whose order is the document's: sorting its data lines makes `sops -d`
293 # fail at exit 51.
294 26 ▶sub emit {
Subroutine emit: called CC=7 SCAR=19.9
Pod documented
295 26 my ($class, $data) = @_;
296 26 ▶ croak "data required" unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 26 26
297 26 ▶ croak "a dotenv document is a flat mapping of keys to values, so emit "
Branch: 2 branches
BranchTrueFalseTotal
unless ref $data eq "HASH" 0 26 26
ref $data ? : 0 0 0
298 . "needs a HashRef, not " . (ref($data) ? ref($data) . " reference"
299 : "a scalar")
300 unless ref $data eq 'HASH';
301  
302 26 my $out = '';
303  
304 26 for my $key (sort keys %$data) {
305 # The empty key sorts first, so the comment block heads the document.
306 47 ▶ if ($key eq $COMMENT_KEY) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq $File::SOPS::Format::ENV::COMMENT_KEY) 6 41 47
307 6 $out .= $class->_comment_lines($data->{$key});
308 3 next;
309 }
310  
311 41 ▶ croak "$key: a top-level key starting with '" . $FLAT->prefix . "' is "
Branch: 1 branch
BranchTrueFalseTotal
if $FLAT->is_metadata_key($key) 1 40 41
312 . "where the SOPS metadata section goes in a dotenv document, so "
313 . "this entry would be read back as metadata instead of as a "
314 . "value. sops refuses such a document too (exit 203). Rename the "
315 . "entry"
316 if $FLAT->is_metadata_key($key);
317  
318 40 $out .= _key_bytes($key) . '=' . _leaf_line($data->{$key}, $key) . "\n";
319 }
320  
321 18 return $out;
322 }
323  
324 6 ▶sub _comment_lines {
Subroutine _comment_lines: called CC=4 SCAR=13.9
325 6 my ($class, $bucket) = @_;
326  
327 6 ▶ croak "'': the empty key is reserved for this document's comments and "
Branch: 2 branches
BranchTrueFalseTotal
unless ref $bucket eq "ARRAY" 1 5 6
ref $bucket ? : 0 1 1
328 . "holds a sequence of them; a "
329 . (ref($bucket) ? lc(ref $bucket) . " reference" : "scalar")
330 . " here has no line to be written as. See the Comments section of "
331 . "File::SOPS::Format::ENV"
332 unless ref $bucket eq 'ARRAY';
333  
334 5 my $out = '';
335 5 $out .= '#' . _comment_text($_) . "\n" for @$bucket;
336 3 return $out;
337 }
338  
339 6 ▶sub _comment_text {
Subroutine _comment_text: called CC=8 SCAR=20.9
340 6 my ($comment) = @_;
341  
342 # An encrypted comment is already the line's text.
343 6 ▶ return $comment
Branch: 1 branch
BranchTrueFalseTotal
if not ref $comment and ("File::SOPS::Encrypted"->encrypted_type($comment) // "") eq "comment" 2 4 6
Condition100%5/5
not ref $comment and ("File::SOPS::Encrypted"->encrypted_type($comment) // "") eq "comment"
!ll&&!rl&&r
312
"File::SOPS::Encrypted"->encrypted_type($comment) // ""
l!l
21
MC/DC100%2/2
not ref $comment and ("File::SOPS::Encrypted"->encrypted_type($comment) // "") eq "comment"
not ref $comment "File::SOPS::Encrypted"->encrypted_type($comment)
Truth table100%3/3
not(A) and B
A= not ref $commentB= "File::SOPS::Encrypted"->encrypted_type($comment)
ABresult
0X0
100
111
344 if !ref $comment
345 && (File::SOPS::Encrypted->encrypted_type($comment) // '') eq 'comment';
346  
347 4 ▶ croak "'': the empty key holds this document's comments, and "
Branch: 3 branches
BranchTrueFalseTotal
unless "File::SOPS::Encrypted"->is_comment($comment) 1 3 4
ref $comment ? : 0 1 1
defined $comment ? : 1 0 1
348 . (ref($comment) ? "a " . lc(ref $comment) . " reference"
349 : defined $comment ? "a plain scalar"
350 : "an undef")
351 . " is not one. Put File::SOPS::Comment objects there, or leave the "
352 . "key out"
353 unless File::SOPS::Encrypted->is_comment($comment);
354  
355 3 my $text = File::SOPS::Encrypted->value_to_bytes($comment);
356  
357 # sops neither escapes nor unescapes a comment (measured: `a\nb` survives
358 # as `a\nb`, where the same bytes in a data value come back as a newline),
359 # so a comment holding a real newline has no spelling in this format at
360 # all -- written out it would become a second line that is not even a valid
361 # dotenv line. docs/adr/0008's rule, applied to the one leaf whose text
362 # goes to the file unescaped.
363 3 ▶ croak "'': a comment holding a newline cannot be written into a dotenv "
Branch: 1 branch
BranchTrueFalseTotal
if index($text, "\n") != -1 1 2 3
364 . "document -- a comment is one line, and sops does not escape a "
365 . "comment the way it escapes a value (measured). Split it into one "
366 . "comment per line"
367 if index($text, "\n") >= 0;
368  
369 2 return $text;
370 }
371  
372 40 ▶sub _leaf_line {
Subroutine _leaf_line: called CC=7 SCAR=19.5
373 40 my ($value, $key) = @_;
374  
375 # An ENCRYPTED leaf is an ENC[...] string by the time the emitter sees it.
376 # Its alphabet is base64 plus []:,= -- no backslash, no newline -- so the
377 # store's escape is the identity on it (measured), and the guard below has
378 # nothing to catch. docs/adr/0030: the guard is for unencrypted leaves and
379 # for the write direction, and refusing an encrypted one would reject
380 # documents that work.
381 40 ▶ return $value
Branch: 1 branch
BranchTrueFalseTotal
if not ref $value and "File::SOPS::Encrypted"->is_encrypted($value) 20 20 40
Condition100%3/3
not ref $value and "File::SOPS::Encrypted"->is_encrypted($value)
!ll&&!rl&&r
41620
MC/DC100%2/2
not ref $value and "File::SOPS::Encrypted"->is_encrypted($value)
not ref $value "File::SOPS::Encrypted"->is_encrypted($value)
Truth table100%3/3
not(A) and B
A= not ref $valueB= "File::SOPS::Encrypted"->is_encrypted($value)
ABresult
0X0
100
111
382 if !ref $value && File::SOPS::Encrypted->is_encrypted($value);
383  
384 20 ▶ croak "$key: cannot write a " . lc(ref $value) . " into a dotenv document; "
Branch: 1 branch
BranchTrueFalseTotal
if ref $value and not builtin::blessed($value) 3 17 20
Condition100%3/3
ref $value and not builtin::blessed($value)
!ll&&!rl&&r
1613
MC/DC100%2/2
ref $value and not builtin::blessed($value)
ref $value not builtin::blessed($value)
Truth table100%3/3
A and not(B)
A= ref $valueB= not builtin::blessed($value)
ABresult
0X0
100
111
385 . "the format is one level deep and has no spelling for a nested "
386 . "value. sops refuses the same tree with 'cannot use complex value "
387 . "in dotenv file'"
388 if ref $value && !blessed($value);
389  
390 17 ▶ croak "$key: a comment cannot be a mapping value. In a dotenv document a "
Branch: 1 branch
BranchTrueFalseTotal
if "File::SOPS::Encrypted"->is_comment($value) 0 17 17
391 . "comment is a line of its own, which this handler keeps under the "
392 . "empty key -- see the Comments section of File::SOPS::Format::ENV"
393 if File::SOPS::Encrypted->is_comment($value);
394  
395 # docs/adr/0035: in an untyped store an unencrypted leaf is written as
396 # exactly the bytes the digest covers. ASKED of the single source of truth
397 # rather than re-derived, so the emitter and the digest cannot drift apart.
398 17 my $bytes = File::SOPS::Encrypted->value_to_bytes($value);
399 17 my $escaped = $FLAT->escape_value($bytes);
400  
401 # docs/adr/0030, verbatim. It asks the escape instead of testing for a
402 # character, because a second spelling of the rule is how a guard and the
403 # thing it guards drift.
404 17 ▶ croak "$key: cannot write this value into a dotenv document; the format's "
Branch: 1 branch
BranchTrueFalseTotal
unless $FLAT->unescape_value($escaped) eq $bytes 1 16 17
405 . "newline escape does not round-trip it. The value holds the two "
406 . "characters backslash and 'n', which the store's reader turns into a "
407 . "newline, so the document would say one thing and its own MAC "
408 . "another. sops writes such a file at exit 0 and then refuses to read "
409 . "it (MAC mismatch, exit 51)"
410 unless $FLAT->unescape_value($escaped) eq $bytes;
411  
412 16 return $escaped;
413 }
414  
415  
416 147 ▶sub detect_content {
Subroutine detect_content: called CC=7 SCAR=19.5
Pod documented
417 147 my ($class, $content) = @_;
418 147 ▶ return 0 unless defined $content;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 0 147 147
419  
420 # BOTH halves of what an encrypted dotenv document is, because either alone
421 # is too weak: a YAML block scalar can hold a line that spells
422 # `sops_version=3.13.3`, and every plaintext .env has no metadata to find.
423 147 my $metadata = 0;
424 147 for my $line (split /\n/, $content, -1) {
425 198 ▶ next unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 6 192 198
426 192 ▶ next if substr($line, 0, 1) eq '#';
Branch: 1 branch
BranchTrueFalseTotal
if substr($line, 0, 1) eq "#" 1 191 192
427  
428 191 my $eq = index($line, '=');
429 191 ▶ return 0 if $eq < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $eq < 0 141 50 191
430  
431 50 ▶ $metadata = 1 if $FLAT->is_metadata_key(substr($line, 0, $eq));
Branch: 1 branch
BranchTrueFalseTotal
if $FLAT->is_metadata_key(substr($line, 0, $eq)) 36 14 50
432 }
433  
434 6 return $metadata;
435 }
436  
437  
438 20 ▶sub format_name { 'env' }
Subroutine format_name: called CC=1 SCAR=0.0
Pod documented
439  
440  
441 1 ▶sub file_extensions { qw(env) }
Subroutine file_extensions: called CC=1 SCAR=0.0
Pod documented
442  
443  
444 3 ▶sub detect {
Subroutine detect: called CC=2 SCAR=6.9
Pod documented
445 3 my ($class, $filename) = @_;
446 3 ▶ return 1 if $filename =~ /\.env$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $filename =~ /\.env$/i 2 1 3
447 1 return 0;
448 }
449  
450  
451  
452 82 1;
453  
454 __END__
« blib/lib/File/SOPS/Format/JSON.pm ↑ Summary blib/lib/File/SOPS/Format/YAML.pm »