KDBX/Safe.pm

filter: Statementstmt 87.3% 131 / 150 Branchbran 64.2% 36 / 56 Conditioncond 62.5% 5 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 90.4% 19 / 21 Podpod 100% 9 / 9 totaltotal 80.3% 200 / 249 SCAR 37.9
CC
35
Cov
80%
add_protected
14.3
add
13.2
unlock
7.2
CRAP 44.3
« KDBX/Iterator.pm ↑ Summary KDBX/IO/HashBlock.pm »
1 package File::KDBX::Safe;
2 # ABSTRACT: Keep strings encrypted while in memory
3  
4 35 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 35 ▶use Crypt::PRNG qw(random_bytes);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 35 ▶use Devel::GlobalDestruction;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 35 ▶use Encode qw(encode decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 35 ▶use File::KDBX::Constants qw(:random_stream);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 35 ▶use File::KDBX::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 35 ▶use File::KDBX::Util qw(erase erase_scoped);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 35 ▶use Ref::Util qw(is_arrayref is_coderef is_hashref is_scalarref);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 35 ▶use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 35 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 35 our $VERSION = '0.904'; # VERSION
18  
19  
20 sub new {
21 94 ▶ my $class = shift;
Subroutine new: called CC=5 SCAR=16.4
Pod documented
22 94 ▶ my %args = @_ % 2 == 0 ? @_ : (strings => shift, @_);
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 == 0 ? : 93 1 94
23  
24 94 ▶ if (!$args{cipher} && $args{key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $args{"cipher"} and $args{"key"}) 0 94 94
Condition66%2/3
not $args{"cipher"} and $args{"key"}
!ll&&!rl&&r
12820
MC/DC0%0/2
not $args{"cipher"} and $args{"key"}
not $args{"cipher"} $args{"key"}
Truth table66%2/3
not(A) and B
A= not $args{"cipher"}B= $args{"key"}
ABresult
0X0
100
111
25 0 require File::KDBX::Cipher;
26 0 $args{cipher} = File::KDBX::Cipher->new(stream_id => STREAM_ID_CHACHA20, key => $args{key});
27 }
28  
29 94 my $self = bless \%args, $class;
30 94 $self->cipher->finish;
31 94 $self->{counter} = 0;
32  
33 94 my $strings = delete $args{strings};
34 94 $self->{items} = [];
35 94 $self->{index} = {};
36 94 ▶ $self->add($strings) if $strings;
Branch: 1 branch
BranchTrueFalseTotal
if $strings 1 93 94
37  
38 94 return $self;
39 }
40  
41 94 ▶sub DESTROY { local ($., $@, $!, $^E, $?); !in_global_destruction and $_[0]->unlock }
Subroutine DESTROY: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
not &in_global_destruction() and $_[0]->unlock 94 0 94
42  
43  
44 sub clear {
45 180 ▶ my $self = shift;
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
46 180 $self->{items} = [];
47 180 $self->{index} = {};
48 180 $self->{counter} = 0;
49 180 return $self;
50 }
51  
52  
53 0 ▶sub lock { shift->add(@_) }
Subroutine lock: not called CC=1 SCAR=6.9
Pod documented
54  
55 sub add {
56 82 ▶ my $self = shift;
Subroutine add: called CC=12 SCAR=25.8
Pod documented
57 82 ▶ my @strings = map { is_arrayref($_) ? @$_ : $_ } @_;
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_arrayref($_) ? : 1 81 82
58  
59 82 ▶ @strings or throw 'Must provide strings to lock';
Branch: 1 branch
BranchTrueFalseTotal
@strings or throw("Must provide strings to lock") 0 82 82
60  
61 82 my $cipher = $self->cipher;
62  
63 82 for my $string (@strings) {
64 85 my $item = {str => $string, off => $self->{counter}};
65 85 ▶ if (is_scalarref($string)) {
Branch: 2 branches
BranchTrueFalseTotal
if (Ref::Util::XS::is_scalarref($string)) { } 82 3 85
elsif (Ref::Util::XS::is_hashref($string)) { } 3 0 3
66 82 ▶ next if !defined $$string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$string 0 82 82
67 82 ▶ $item->{enc} = 'UTF-8' if utf8::is_utf8($$string);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($$string) 0 82 82
68 82 ▶ if (my $encoding = $item->{enc}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $encoding = $item->{'enc'}) { } 0 82 82
69 0 my $encoded = encode($encoding, $$string);
70 0 $item->{val} = $cipher->crypt(\$encoded);
71 0 erase $encoded;
72 }
73 else {
74 82 $item->{val} = $cipher->crypt($string);
75 }
76 82 erase $string;
77 }
78 elsif (is_hashref($string)) {
79 3 ▶ next if !defined $string->{value};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string->{"value"} 0 3 3
80 3 ▶ $item->{enc} = 'UTF-8' if utf8::is_utf8($string->{value});
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($string->{"value"}) 1 2 3
81 3 ▶ if (my $encoding = $item->{enc}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $encoding = $item->{'enc'}) { } 1 2 3
82 1 my $encoded = encode($encoding, $string->{value});
83 1 $item->{val} = $cipher->crypt(\$encoded);
84 1 erase $encoded;
85 }
86 else {
87 2 $item->{val} = $cipher->crypt(\$string->{value});
88 }
89 3 erase \$string->{value};
90 }
91 else {
92 0 throw 'Safe strings must be a hashref or stringref', type => ref $string;
93 }
94 85 push @{$self->{items}}, $item;
95 85 $self->{index}{refaddr($string)} = $item;
96 85 $self->{counter} += length($item->{val});
97 }
98  
99 82 return $self;
100 }
101  
102  
103 0 ▶sub lock_protected { shift->add_protected(@_) }
Subroutine lock_protected: not called CC=1 SCAR=6.9
Pod documented
104  
105 sub add_protected {
106 27 ▶ my $self = shift;
Subroutine add_protected: called CC=10 SCAR=26.6
Pod documented
107 27 ▶ my $filter = is_coderef($_[0]) ? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_coderef($_[0]) ? : 27 0 27
108 27 ▶ my @strings = map { is_arrayref($_) ? @$_ : $_ } @_;
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_arrayref($_) ? : 0 27 27
109  
110 27 ▶ @strings or throw 'Must provide strings to lock';
Branch: 1 branch
BranchTrueFalseTotal
@strings or throw("Must provide strings to lock") 0 27 27
111  
112 27 for my $string (@strings) {
113 27 my $item = {str => $string, off => $self->{counter}};
114 27 ▶ $item->{filter} = $filter if defined $filter;
Branch: 1 branch
BranchTrueFalseTotal
if defined $filter 27 0 27
115 27 ▶ if (is_scalarref($string)) {
Branch: 2 branches
BranchTrueFalseTotal
if (Ref::Util::XS::is_scalarref($string)) { } 0 27 27
elsif (Ref::Util::XS::is_hashref($string)) { } 27 0 27
116 0 ▶ next if !defined $$string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$string 0 0 0
117 0 $item->{val} = $$string;
118 0 erase $string;
119 }
120 elsif (is_hashref($string)) {
121 27 ▶ next if !defined $string->{value};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string->{"value"} 0 27 27
122 27 $item->{val} = $string->{value};
123 27 erase \$string->{value};
124 }
125 else {
126 0 throw 'Safe strings must be a hashref or stringref', type => ref $string;
127 }
128 27 push @{$self->{items}}, $item;
129 27 $self->{index}{refaddr($string)} = $item;
130 27 $self->{counter} += length($item->{val});
131 }
132  
133 27 return $self;
134 }
135  
136  
137 sub unlock {
138 99 ▶ my $self = shift;
Subroutine unlock: called CC=7 SCAR=19.7
Pod documented
139  
140 99 my $cipher = $self->cipher;
141 99 $cipher->finish;
142 99 $self->{counter} = 0;
143  
144 99 for my $item (@{$self->{items}}) {
145 31 my $string = $item->{str};
146 31 my $cleanup = erase_scoped \$item->{val};
147 31 my $str_ref;
148 31 ▶ if (is_scalarref($string)) {
Branch: 2 branches
BranchTrueFalseTotal
if (Ref::Util::XS::is_scalarref($string)) { } 1 30 31
elsif (Ref::Util::XS::is_hashref($string)) { } 30 0 30
149 1 $$string = $cipher->crypt(\$item->{val});
150 1 ▶ if (my $encoding = $item->{enc}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $encoding = $item->{"enc"}) 0 1 1
151 0 my $decoded = decode($encoding, $string->{value});
152 0 erase $string;
153 0 $$string = $decoded;
154 }
155 1 $str_ref = $string;
156 }
157 elsif (is_hashref($string)) {
158 30 $string->{value} = $cipher->crypt(\$item->{val});
159 30 ▶ if (my $encoding = $item->{enc}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $encoding = $item->{"enc"}) 1 29 30
160 1 my $decoded = decode($encoding, $string->{value});
161 1 erase \$string->{value};
162 1 $string->{value} = $decoded;
163 }
164 30 $str_ref = \$string->{value};
165 }
166 else {
167 0 die 'Unexpected';
168 }
169 31 ▶ if (my $filter = $item->{filter}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $filter = $item->{"filter"}) 27 4 31
170 27 my $filtered = $filter->($$str_ref);
171 27 erase $str_ref;
172 27 $$str_ref = $filtered;
173 }
174 }
175  
176 99 return $self->clear;
177 }
178  
179  
180 sub peek {
181 91 ▶ my $self = shift;
Subroutine peek: called CC=3 SCAR=12.3
Pod documented
182 91 my $string = shift;
183  
184 91 ▶ my $item = $self->{index}{refaddr($string)} // return;
Condition50%1/2
$self->{"index"}{builtin::refaddr($string)} // (return)
l!l
910
MC/DC0%0/1
$self->{"index"}{builtin::refaddr($string)} // (return)
$self->{"index"}{builtin::refaddr($string)}
Truth table50%1/2
A
A= $self->{"index"}{builtin::refaddr($string)}
Aresult
11
00
185  
186 91 my $cipher = $self->cipher->dup(offset => $item->{off});
187  
188 91 my $value = $cipher->crypt(\$item->{val});
189 91 ▶ if (my $encoding = $item->{enc}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $encoding = $item->{"enc"}) 0 91 91
190 0 my $decoded = decode($encoding, $value);
191 0 erase $value;
192 0 return $decoded;
193 }
194 91 return $value;
195 }
196  
197  
198 sub cipher {
199 366 ▶ my $self = shift;
Subroutine cipher: called CC=2 SCAR=7.0
Pod documented
200 366 ▶ $self->{cipher} //= do {
Condition66%2/3
$self->{"cipher"} //= do { require File::KDBX::Cipher; "File::KDBX::Cipher"->new("stream_id", 3, "key", &random_bytes(64)) }
l!l&&r!l&&!r
284820
MC/DC0%0/2
$self->{"cipher"} //= do { require File::KDBX::Cipher; "File::KDBX::Cipher"->new("stream_id", 3, "key", &random_bytes(64)) }
$self->{"cipher"} do { require File::KDBX::Cipher; "File::KDBX::Cipher"->new("stream_id", 3, "key", &random_bytes(64)) }
Truth table66%2/3
A //= B
A= $self->{"cipher"}B= do { require File::KDBX::Cipher; "File::KDBX::Cipher"->new("stream_id", 3, "key", &random_bytes(64)) }
ABresult
1X1
011
000
201 82 require File::KDBX::Cipher;
202 82 File::KDBX::Cipher->new(stream_id => STREAM_ID_CHACHA20, key => random_bytes(64));
203 };
204 }
205  
206 35 1;
207  
208 __END__
« KDBX/Iterator.pm ↑ Summary KDBX/IO/HashBlock.pm »