blib/lib/DBIO/EncodedColumn.pm

filter: Statementstmt 87.2% 184 / 211 Branchbran 60.4% 87 / 144 Conditioncond 46.4% 39 / 84 MC/DCmcdc 25.4% 13 / 51 Subroutinesub 90.6% 29 / 32 Podpod 100% 2 / 2 totaltotal 67.5% 354 / 524 CC 104 SCAR 38.5
CC
104
Cov
71%
_encode_info_for
160.0
_verify_encoded_value
15.8
_encoded_value_format
13.9
CRAP 47.0
« blib/lib/DBIO/ResultSource/RowParser.pm ↑ Summary blib/lib/DBIO/Storage/DBI/DataTypeClassifier.pm »
1 package DBIO::EncodedColumn;
2 # ABSTRACT: One-way encode selected columns: passwords with Bcrypt or Argon2, tokens with Digest
3  
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 our $VERSION = '0.900007';
8  
9 4 ▶use base qw/DBIO::Base/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 ▶use Digest ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Crypt::URandom ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use DBIO::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use DBIO::Util qw(is_literal_value);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 4 __PACKAGE__->load_components(qw/FilterColumn/);
19  
20  
21  
22 10 ▶sub add_columns {
Subroutine add_columns: called CC=8 SCAR=21.7
Pod documented
23 10 my ($self, @cols) = @_;
24 10 my @columns;
25  
26 10 ▶ while (my $col = shift @cols) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $col = shift @cols) 20 10 30
27 20 ▶ my $info = ref $cols[0] ? shift @cols : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $cols[0] ? : 20 0 20
28  
29 20 ▶ if (ref $info eq 'HASH' && $info->{encode_column}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $info eq "HASH" and $info->{"encode_column"}) 10 10 20
Condition66%2/3
ref $info eq "HASH" and $info->{"encode_column"}
!ll&&!rl&&r
01010
MC/DC50%1/2
ref $info eq "HASH" and $info->{"encode_column"}
ref $info eq "HASH" $info->{"encode_column"}
Truth table66%2/3
A and B
A= ref $info eq "HASH"B= $info->{"encode_column"}
ABresult
0X0
100
111
30 10 $info = { %$info };
31 10 ▶ $info->{is_serializable} = 0 unless exists $info->{is_serializable};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $info->{"is_serializable"} 10 0 10
32 $info->{_changelog_exclude} = 1
33 10 ▶ unless exists $info->{changelog} || exists $info->{_changelog_exclude};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $info->{"changelog"} or exists $info->{"_changelog_exclude"} 10 0 10
Condition33%1/3
exists $info->{"changelog"} or exists $info->{"_changelog_exclude"}
l!l&&r!l&&!r
0010
MC/DC0%0/2
exists $info->{"changelog"} or exists $info->{"_changelog_exclude"}
exists $info->{"changelog"} exists $info->{"_changelog_exclude"}
Truth table33%1/3
A or B
A= exists $info->{"changelog"}B= exists $info->{"_changelog_exclude"}
ABresult
1X1
011
000
34 }
35  
36 20 push @columns, $col => $info;
37 }
38  
39 10 return $self->next::method(@columns);
40 }
41  
42  
43 20 ▶sub register_column {
Subroutine register_column: called CC=2 SCAR=7.0
Pod documented
44 20 my ($self, $column, $info, @rest) = @_;
45  
46 20 $self->next::method($column, $info, @rest);
47  
48 20 ▶ return unless $info->{encode_column};
Branch: 1 branch
BranchTrueFalseTotal
unless $info->{"encode_column"} 10 10 20
49  
50 10 my $encode_info = $self->_encode_info_for($column, $info);
51  
52 $self->filter_column($column => {
53 0 ▶ filter_from_storage => sub { $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
54 20 ▶ filter_to_storage => sub {
Subroutine __ANON__: called CC=4 SCAR=14.1
55 20 my ($row, $value) = @_;
56  
57 20 ▶ return $value if !defined($value) || ref($value) || is_literal_value($value);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or ref $value or &is_literal_value($value) 1 19 20
Condition66%4/6
not defined $value or ref $value or &is_literal_value($value)
l!l&&r!l&&!r
1019
not defined $value or ref $value
l!l&&r!l&&!r
1019
MC/DC33%1/3
not defined $value or ref $value or &is_literal_value($value)
not defined $value ref $value &is_literal_value($value)
Truth table50%2/4
not(A) or B or C
A= not defined $valueB= ref $valueC= &is_literal_value($value)
ABCresult
1XX1
01X1
0011
0000
58  
59 19 return $row->_encode_column_value($value, $encode_info);
60 },
61 5 });
62  
63 5 $self->_install_check_method($column, $encode_info->{check_method}, $encode_info);
64  
65 5 return;
66 }
67  
68 my %known_encode_args = (
69 8 Bcrypt => { map { $_ => 1 } qw(cost accept_unencoded) },
70 16 Argon2 => { map { $_ => 1 } qw(t_cost m_cost parallelism accept_unencoded) },
71 4 Digest => { map { $_ => 1 } qw(algorithm salt_length accept_unencoded) },
72 );
73  
74 4 my %class_module = (
75 Bcrypt => 'Crypt::Bcrypt',
76 Argon2 => 'Crypt::Argon2',
77 );
78  
79 # What may be written; every other digest stays readable.
80 4 my $writable_digest = qr/\A(?:SHA-(?:256|384|512)|SHA3-(?:224|256|384|512))\z/;
81  
82 10 ▶sub _encode_info_for {
Subroutine _encode_info_for: called CC=35 SCAR=50.8
83 10 my ($self, $column, $info) = @_;
84  
85 10 ▶ my $class_name = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
0100
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
86 10 my $where = "column '$column' of $class_name";
87  
88 10 ▶ my $class = defined $info->{encode_class} ? $info->{encode_class} : 'Bcrypt';
Branch: 1 branch
BranchTrueFalseTotal
defined $info->{'encode_class'} ? : 10 0 10
89 $self->throw_exception(
90 "encode_class '$class' of $where is not supported (known: "
91 . join(', ', sort keys %known_encode_args) . ')'
92 10 ▶ ) unless !ref($class) && $known_encode_args{$class};
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $class and $known_encode_args{$class} 1 9 10
Condition66%2/3
not ref $class and $known_encode_args{$class}
!ll&&!rl&&r
019
MC/DC50%1/2
not ref $class and $known_encode_args{$class}
not ref $class $known_encode_args{$class}
Truth table66%2/3
not(A) and B
A= not ref $classB= $known_encode_args{$class}
ABresult
0X0
100
111
93  
94 9 my $args = $info->{encode_args};
95 9 ▶ $self->throw_exception("encode_args of $where must be a hashref")
Branch: 1 branch
BranchTrueFalseTotal
if defined $args and ref $args ne "HASH" 1 8 9
Condition100%3/3
defined $args and ref $args ne "HASH"
!ll&&!rl&&r
171
MC/DC100%2/2
defined $args and ref $args ne "HASH"
defined $args ref $args ne "HASH"
Truth table100%3/3
A and B
A= defined $argsB= ref $args ne "HASH"
ABresult
0X0
100
111
96 if defined $args && ref($args) ne 'HASH';
97 8 ▶ $args = { %{ $args || {} } };
Condition100%2/2
$args || {}
l!l
71
MC/DC100%1/1
$args || {}
$args
Truth table100%2/2
A
A= $args
Aresult
11
00
98  
99 8 my $known = $known_encode_args{$class};
100 8 ▶ if (my @unknown = sort grep { !$known->{$_} } keys %$args) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@unknown) = sort(grep({not $known->{$_};} keys %$args))) 1 7 8
101 1 $self->throw_exception(
102 "Unknown encode_args key(s) of $where: " . join(', ', @unknown)
103 . " (known for $class: " . join(', ', sort keys %$known) . ')'
104 );
105 }
106  
107 7 ▶ if (my $module = $class_module{$class}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $module = $class_module{$class}) 0 7 7
108 0 ▶ $self->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
unless _module_loadable($module) 0 0 0
109 "encode_class '$class' of $where needs $module, which is not installed"
110 ) unless _module_loadable($module);
111 }
112  
113 7 ▶ if ($class eq 'Bcrypt') {
Branch: 2 branches
BranchTrueFalseTotal
if ($class eq 'Bcrypt') { } 0 7 7
elsif ($class eq 'Argon2') { } 0 7 7
114 0 ▶ $args->{cost} = 12 unless defined $args->{cost};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"cost"} 0 0 0
115 $self->throw_exception("encode_args cost of $where must be an integer from 4 to 31")
116 unless !ref($args->{cost}) && $args->{cost} =~ /\A[0-9]+\z/
117 0 ▶ && $args->{cost} >= 4 && $args->{cost} <= 31;
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $args->{"cost"} and $args->{"cost"} =~ /\A[0-9]+\z/ and $args->{"cost"} >= 4 and $args->{"cost"} <= 31 0 0 0
Condition0%0/9
not ref $args->{"cost"} and $args->{"cost"} =~ /\A[0-9]+\z/ and $args->{"cost"} >= 4 and $args->{"cost"} <= 31
!ll&&!rl&&r
000
not ref $args->{"cost"} and $args->{"cost"} =~ /\A[0-9]+\z/ and $args->{"cost"} >= 4
!ll&&!rl&&r
000
not ref $args->{"cost"} and $args->{"cost"} =~ /\A[0-9]+\z/
!ll&&!rl&&r
000
MC/DC0%0/4
not ref $args->{"cost"} and $args->{"cost"} =~ /\A[0-9]+\z/ and $args->{"cost"} >= 4 and $args->{"cost"} <= 31
not ref $args->{"cost"} $args->{"cost"} =~ /\A[0-9]+\z/ $args->{"cost"} >= 4 $args->{"cost"} <= 31
Truth table0%0/5
not(A) and B and C and D
A= not ref $args->{"cost"}B= $args->{"cost"} =~ /\A[0-9]+\z/C= $args->{"cost"} >= 4D= $args->{"cost"} <= 31
ABCDresult
0XXX0
10XX0
110X0
11100
11111
118 }
119 elsif ($class eq 'Argon2') {
120 0 ▶ $args->{t_cost} = 3 unless defined $args->{t_cost};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"t_cost"} 0 0 0
121 0 ▶ $args->{m_cost} = '32M' unless defined $args->{m_cost};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"m_cost"} 0 0 0
122 0 ▶ $args->{parallelism} = 1 unless defined $args->{parallelism};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"parallelism"} 0 0 0
123  
124 0 for my $key (qw(t_cost parallelism)) {
125 $self->throw_exception("encode_args $key of $where must be a positive integer")
126 0 ▶ unless !ref($args->{$key}) && $args->{$key} =~ /\A[1-9][0-9]*\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $args->{$key} and $args->{$key} =~ /\A[1-9][0-9]*\z/ 0 0 0
Condition0%0/3
not ref $args->{$key} and $args->{$key} =~ /\A[1-9][0-9]*\z/
!ll&&!rl&&r
000
MC/DC0%0/2
not ref $args->{$key} and $args->{$key} =~ /\A[1-9][0-9]*\z/
not ref $args->{$key} $args->{$key} =~ /\A[1-9][0-9]*\z/
Truth table0%0/3
not(A) and B
A= not ref $args->{$key}B= $args->{$key} =~ /\A[1-9][0-9]*\z/
ABresult
0X0
100
111
127 }
128 $self->throw_exception("encode_args m_cost of $where must be a positive number with an optional k, M or G suffix")
129 0 ▶ unless !ref($args->{m_cost}) && $args->{m_cost} =~ /\A[1-9][0-9]*[kMG]?\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $args->{"m_cost"} and $args->{"m_cost"} =~ /\A[1-9][0-9]*[kMG]?\z/ 0 0 0
Condition0%0/3
not ref $args->{"m_cost"} and $args->{"m_cost"} =~ /\A[1-9][0-9]*[kMG]?\z/
!ll&&!rl&&r
000
MC/DC0%0/2
not ref $args->{"m_cost"} and $args->{"m_cost"} =~ /\A[1-9][0-9]*[kMG]?\z/
not ref $args->{"m_cost"} $args->{"m_cost"} =~ /\A[1-9][0-9]*[kMG]?\z/
Truth table0%0/3
not(A) and B
A= not ref $args->{"m_cost"}B= $args->{"m_cost"} =~ /\A[1-9][0-9]*[kMG]?\z/
ABresult
0X0
100
111
130 }
131 else {
132 7 ▶ $args->{algorithm} = 'SHA-256' unless defined $args->{algorithm};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"algorithm"} 4 3 7
133 $self->throw_exception(
134 "encode_args algorithm '$args->{algorithm}' of $where is not allowed for writing: "
135 . 'SHA-256, SHA-384, SHA-512 and SHA3-224/256/384/512 are'
136 7 ▶ ) unless !ref($args->{algorithm}) && $args->{algorithm} =~ $writable_digest;
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $args->{"algorithm"} and $args->{"algorithm"} =~ /$writable_digest/ 1 6 7
Condition66%2/3
not ref $args->{"algorithm"} and $args->{"algorithm"} =~ /$writable_digest/
!ll&&!rl&&r
016
MC/DC50%1/2
not ref $args->{"algorithm"} and $args->{"algorithm"} =~ /$writable_digest/
not ref $args->{"algorithm"} $args->{"algorithm"} =~ /$writable_digest/
Truth table66%2/3
not(A) and B
A= not ref $args->{"algorithm"}B= $args->{"algorithm"} =~ /$writable_digest/
ABresult
0X0
100
111
137 $self->throw_exception("Unknown encode_args algorithm '$args->{algorithm}' of $where")
138 6 ▶ unless defined _digest_ctx($args->{algorithm});
Branch: 1 branch
BranchTrueFalseTotal
unless defined _digest_ctx($args->{"algorithm"}) 0 6 6
139  
140 6 ▶ if (defined $args->{salt_length}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"salt_length"}) 4 2 6
141 $self->throw_exception("encode_args salt_length of $where must be a non-negative integer")
142 4 ▶ unless !ref($args->{salt_length}) && $args->{salt_length} =~ /\A[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $args->{"salt_length"} and $args->{"salt_length"} =~ /\A[0-9]+\z/ 1 3 4
Condition66%2/3
not ref $args->{"salt_length"} and $args->{"salt_length"} =~ /\A[0-9]+\z/
!ll&&!rl&&r
013
MC/DC50%1/2
not ref $args->{"salt_length"} and $args->{"salt_length"} =~ /\A[0-9]+\z/
not ref $args->{"salt_length"} $args->{"salt_length"} =~ /\A[0-9]+\z/
Truth table66%2/3
not(A) and B
A= not ref $args->{"salt_length"}B= $args->{"salt_length"} =~ /\A[0-9]+\z/
ABresult
0X0
100
111
143 }
144 }
145  
146 5 ▶ $args->{accept_unencoded} = $args->{accept_unencoded} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$args->{'accept_unencoded'} ? : 1 4 5
147  
148 return {
149 encode_class => $class,
150 encode_args => $args,
151 5 ▶ check_method => $info->{encode_check_method} || "check_$column",
Condition100%2/2
$info->{'encode_check_method'} || "check_$column"
l!l
41
MC/DC100%1/1
$info->{'encode_check_method'} || "check_$column"
$info->{'encode_check_method'}
Truth table100%2/2
A
A= $info->{'encode_check_method'}
Aresult
11
00
152 };
153 }
154  
155 1 ▶sub _module_loadable {
Subroutine _module_loadable: called CC=2 SCAR=7.4
156 1 my ($module) = @_;
157 1 (my $file = "$module.pm") =~ s{::}{/}g;
158 0 ▶ return eval { require $file; 1 } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require $file; 1 } } ? : 0 1 1
159 }
160  
161 5 ▶sub _install_check_method {
Subroutine _install_check_method: called CC=2 SCAR=6.9
162 5 my ($self, $column, $method, $encode_info) = @_;
163  
164 5 ▶ return if $self->can($method);
Branch: 1 branch
BranchTrueFalseTotal
if $self->can($method) 0 5 5
165  
166 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
167 5 ▶ *{"${self}::${method}"} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
168 39 my ($row, $candidate) = @_;
169  
170 39 my $stored = $row->get_column($column);
171 39 ▶ return 0 unless defined $stored;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stored 2 37 39
172  
173 37 return $row->_verify_encoded_value($stored, $candidate, $encode_info, $column);
174 5 };
175 }
176  
177 # The bytes of the UTF-8 encoding of a value: what every method gets, on
178 # writing and on checking alike.
179 46 ▶sub _utf8_bytes {
Subroutine _utf8_bytes: called CC=1 SCAR=0.0
180 46 my ($value) = @_;
181 46 utf8::encode($value);
182 46 return $value;
183 }
184  
185 0 ▶sub _assert_bcrypt_input {
Subroutine _assert_bcrypt_input: not called CC=2 SCAR=17.9
186 0 my ($self, $bytes) = @_;
187  
188 0 ▶ $self->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
unless _bcrypt_takes($bytes) 0 0 0
189 'bcrypt takes at most 72 bytes and no NUL byte: use encode_class => \'Argon2\' for this column'
190 ) unless _bcrypt_takes($bytes);
191 }
192  
193 1 ▶sub _bcrypt_takes {
Subroutine _bcrypt_takes: called CC=2 SCAR=8.1
194 1 my ($bytes) = @_;
195 1 ▶ return length($bytes) <= 72 && index($bytes, "\0") < 0;
Condition33%1/3
length $bytes <= 72 && index($bytes, "\000") == -1
!ll&&!rl&&r
001
MC/DC0%0/2
length $bytes <= 72 && index($bytes, "\000") == -1
length $bytes <= 72 index($bytes, "\000") == -1
Truth table33%1/3
A && B
A= length $bytes <= 72B= index($bytes, "\000") == -1
ABresult
0X0
100
111
196 }
197  
198 21 ▶sub _encode_column_value {
Subroutine _encode_column_value: called CC=7 SCAR=20.8
199 21 my ($self, $value, $encode_info) = @_;
200  
201 21 my $class = $encode_info->{encode_class};
202 21 ▶ my $args = $encode_info->{encode_args} || {};
Condition50%1/2
$encode_info->{'encode_args'} || {}
l!l
210
MC/DC0%0/1
$encode_info->{'encode_args'} || {}
$encode_info->{'encode_args'}
Truth table50%1/2
A
A= $encode_info->{'encode_args'}
Aresult
11
00
203 21 my $bytes = _utf8_bytes($value);
204  
205 21 ▶ if ($class eq 'Bcrypt') {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq "Bcrypt") 0 21 21
206 0 $self->_assert_bcrypt_input($bytes);
207 0 return Crypt::Bcrypt::bcrypt($bytes, '2b', $args->{cost}, $self->_random_bytes(16));
208 }
209  
210 21 ▶ if ($class eq 'Argon2') {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq "Argon2") 0 21 21
211 return Crypt::Argon2::argon2id_pass(
212 $bytes, $self->_random_bytes(16),
213 0 $args->{t_cost}, $args->{m_cost}, $args->{parallelism}, 32,
214 );
215 }
216  
217 21 my $algorithm = $args->{algorithm};
218 21 ▶ my $ctx = _digest_ctx($algorithm)
Branch: 1 branch
BranchTrueFalseTotal
my $ctx = _digest_ctx($algorithm) or $self->throw_exception("Unknown encode_args algorithm '${algorithm}'") 21 0 21
219 or $self->throw_exception("Unknown encode_args algorithm '$algorithm'");
220  
221 21 ▶ my $salt_len = defined $args->{salt_length} ? int($args->{salt_length}) : 16;
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'salt_length'} ? : 18 3 21
222  
223 21 ▶ my $salt = $salt_len ? $self->_random_bytes($salt_len) : '';
Branch: 1 branch
BranchTrueFalseTotal
$salt_len ? : 21 0 21
224  
225 21 $ctx->add($salt);
226 21 $ctx->add($bytes);
227 21 my $digest = $ctx->digest;
228  
229 21 return join(
230 '$',
231 'dbio',
232 $algorithm,
233 MIME::Base64::encode_base64($salt, ''),
234 MIME::Base64::encode_base64($digest, ''),
235 );
236 }
237  
238 37 ▶sub _verify_encoded_value {
Subroutine _verify_encoded_value: called CC=14 SCAR=27.6
239 37 my ($self, $stored, $candidate, $encode_info, $column) = @_;
240  
241 37 ▶ return 0 if !defined $candidate || ref $candidate;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $candidate or ref $candidate 2 35 37
Condition66%2/3
not defined $candidate or ref $candidate
l!l&&r!l&&!r
2035
MC/DC50%1/2
not defined $candidate or ref $candidate
not defined $candidate ref $candidate
Truth table66%2/3
not(A) or B
A= not defined $candidateB= ref $candidate
ABresult
1X1
011
000
242  
243 # no hash format at all: a plain value, accepted only on request
244 35 ▶ unless ($self->_encoded_value_looks_like_hash($stored)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_encoded_value_looks_like_hash($stored)) 5 30 35
245 5 ▶ return 0 unless $encode_info && $encode_info->{encode_args}{accept_unencoded};
Branch: 1 branch
BranchTrueFalseTotal
unless $encode_info and $encode_info->{"encode_args"}{"accept_unencoded"} 2 3 5
Condition66%2/3
$encode_info and $encode_info->{"encode_args"}{"accept_unencoded"}
!ll&&!rl&&r
023
MC/DC50%1/2
$encode_info and $encode_info->{"encode_args"}{"accept_unencoded"}
$encode_info $encode_info->{"encode_args"}{"accept_unencoded"}
Truth table66%2/3
A and B
A= $encode_infoB= $encode_info->{"encode_args"}{"accept_unencoded"}
ABresult
0X0
100
111
246  
247 3 my $match = _constant_time_eq($stored, $candidate);
248 3 ▶ $self->_rehash_unencoded($column, $stored, $candidate, $encode_info) if $match;
Branch: 1 branch
BranchTrueFalseTotal
if $match 2 1 3
249  
250 3 return $match;
251 }
252  
253 30 my ($format, @parts) = $self->_encoded_value_format($stored);
254 30 ▶ return 0 unless $format;
Branch: 1 branch
BranchTrueFalseTotal
unless $format 5 25 30
255  
256 25 my $bytes = _utf8_bytes($candidate);
257  
258 25 ▶ if ($format eq 'bcrypt') {
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq "bcrypt") 1 24 25
259 1 ▶ return 0 unless _bcrypt_takes($bytes);
Branch: 1 branch
BranchTrueFalseTotal
unless _bcrypt_takes($bytes) 0 1 1
260 1 ▶ return $self->_bcrypt_check($bytes, $stored) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_bcrypt_check($bytes, $stored) ? : 0 0 0
261 }
262  
263 24 ▶ if ($format eq 'argon2id') {
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq "argon2id") 0 24 24
264 0 ▶ return $self->_argon2_check($bytes, $stored) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_argon2_check($bytes, $stored) ? : 0 0 0
265 }
266  
267 24 my ($algorithm, $salt, $expected) = @parts;
268 24 ▶ my $ctx = _digest_ctx($algorithm) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $ctx = _digest_ctx($algorithm) or return 0 24 0 24
269 24 $ctx->add($salt);
270 24 $ctx->add($bytes);
271  
272 24 return _constant_time_eq($expected, $ctx->digest);
273 }
274  
275 # A plain stored value that matched: write the hash of the column's class back.
276 # Only this column, only while the database still holds the value that was
277 # compared; a row that is not in the database yet just takes the value.
278 2 ▶sub _rehash_unencoded {
Subroutine _rehash_unencoded: called CC=5 SCAR=17.3
279 2 my ($self, $column, $stored, $candidate, $encode_info) = @_;
280  
281 2 ▶ return unless defined $column;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $column 0 2 2
282  
283 carp_unique(
284 "Column '$column' held a value without hash format; it matched and is written back as "
285 2 . $encode_info->{encode_class} . ' hash'
286 );
287  
288 2 my $hash = $self->_encode_column_value($candidate, $encode_info);
289  
290 2 ▶ unless ($self->in_storage) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->in_storage) 0 2 2
291 0 $self->set_column($column, $hash);
292 0 return;
293 }
294  
295 2 my $rs = $self->result_source->resultset;
296 2 my $alias = $rs->current_source_alias;
297  
298 my $updated = $rs->search({
299 2 %{ $self->ident_condition($alias) },
300 "$alias.$column" => $stored,
301 })->update({ $column => \[ '?', [ $column => $hash ] ] });
302  
303 2 ▶ if ($updated && $updated > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($updated and $updated > 0) 2 0 2
Condition33%1/3
$updated and $updated > 0
!ll&&!rl&&r
002
MC/DC0%0/2
$updated and $updated > 0
$updated $updated > 0
Truth table33%1/3
A and B
A= $updatedB= $updated > 0
ABresult
0X0
100
111
304 2 $self->store_column($column, $hash);
305 2 delete $self->{_dirty_columns}{$column};
306 }
307  
308 2 return;
309 }
310  
311 # What a value claims to be: anything that starts like a dbio, a bcrypt or an
312 # argon2id hash.
313 35 ▶sub _encoded_value_looks_like_hash {
Subroutine _encoded_value_looks_like_hash: called CC=6 SCAR=18.7
314 35 my ($self, $value) = @_;
315  
316 35 ▶ return 0 unless defined $value && !ref($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and not ref $value 0 35 35
Condition33%1/3
defined $value and not ref $value
!ll&&!rl&&r
0035
MC/DC0%0/2
defined $value and not ref $value
defined $value not ref $value
Truth table33%1/3
A and not(B)
A= defined $valueB= not ref $value
ABresult
0X0
100
111
317  
318 35 ▶ return 1 if $value =~ /\Adbio\$[^\$]+\$[^\$]*\$[^\$]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /\Adbio\$[^\$]+\$[^\$]*\$[^\$]+\z/ 26 9 35
319 9 ▶ return 1 if $value =~ /\A\$2[abxy]?\$/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /\A\$2[abxy]?\$/ 4 5 9
320 5 ▶ return 1 if $value =~ /\A\$argon2id\$/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /\A\$argon2id\$/ 0 5 5
321  
322 5 return 0;
323 }
324  
325 # What a value is: () unless it is a complete, valid hash, else
326 # ('dbio', $algorithm, $salt, $digest), ('bcrypt') or ('argon2id').
327 30 ▶sub _encoded_value_format {
Subroutine _encoded_value_format: called CC=12 SCAR=26.3
328 30 my ($self, $value) = @_;
329  
330 30 ▶ return unless defined $value && !ref($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and not ref $value 0 30 30
Condition33%1/3
defined $value and not ref $value
!ll&&!rl&&r
0030
MC/DC0%0/2
defined $value and not ref $value
defined $value not ref $value
Truth table33%1/3
A and not(B)
A= defined $valueB= not ref $value
ABresult
0X0
100
111
331  
332 30 ▶ return ('bcrypt')
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ m[\A\$2[abxy]\$(?:0[4-9]|[12][0-9]|3[01])\$[./A-Za-z0-9]{53}\z] 1 29 30
333 if $value =~ m{\A\$2[abxy]\$(?:0[4-9]|[12][0-9]|3[01])\$[./A-Za-z0-9]{53}\z};
334  
335 29 ▶ return ('argon2id')
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ m[\A\$argon2id\$v=[0-9]+\$m=[0-9]+,t=[0-9]+,p=[0-9]+\$[A-Za-z0-9+/]+\$[A-Za-z0-9+/]+\z] 0 29 29
336 if $value =~ m{\A\$argon2id\$v=[0-9]+\$m=[0-9]+,t=[0-9]+,p=[0-9]+\$[A-Za-z0-9+/]+\$[A-Za-z0-9+/]+\z};
337  
338 29 my ($prefix, $algorithm, $salt_b64, $digest_b64) = split /\$/, $value, 4;
339 29 ▶ return unless defined $digest_b64 && $prefix eq 'dbio' && $value =~ /\Adbio\$[^\$]+\$[^\$]*\$[^\$]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $digest_b64 and $prefix eq "dbio" and $value =~ /\Adbio\$[^\$]+\$[^\$]*\$[^\$]+\z/ 3 26 29
Condition83%5/6
defined $digest_b64 and $prefix eq "dbio" and $value =~ /\Adbio\$[^\$]+\$[^\$]*\$[^\$]+\z/
!ll&&!rl&&r
3026
defined $digest_b64 and $prefix eq "dbio"
!ll&&!rl&&r
2126
MC/DC66%2/3
defined $digest_b64 and $prefix eq "dbio" and $value =~ /\Adbio\$[^\$]+\$[^\$]*\$[^\$]+\z/
defined $digest_b64 $prefix eq "dbio" $value =~ /\Adbio\$[^\$]+\$[^\$]*\$[^\$]+\z/
Truth table75%3/4
A and B and C
A= defined $digest_b64B= $prefix eq "dbio"C= $value =~ /\Adbio\$[^\$]+\$[^\$]*\$[^\$]+\z/
ABCresult
0XX0
10X0
1100
1111
340 26 ▶ return unless _is_canonical_base64($salt_b64) && _is_canonical_base64($digest_b64);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_canonical_base64($salt_b64) and _is_canonical_base64($digest_b64) 0 26 26
Condition33%1/3
_is_canonical_base64($salt_b64) and _is_canonical_base64($digest_b64)
!ll&&!rl&&r
0026
MC/DC0%0/2
_is_canonical_base64($salt_b64) and _is_canonical_base64($digest_b64)
_is_canonical_base64($salt_b64) _is_canonical_base64($digest_b64)
Truth table33%1/3
A and B
A= _is_canonical_base64($salt_b64)B= _is_canonical_base64($digest_b64)
ABresult
0X0
100
111
341  
342 26 ▶ my $ctx = _digest_ctx($algorithm) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $ctx = _digest_ctx($algorithm) or return 24 2 26
343  
344 24 my $salt = MIME::Base64::decode_base64($salt_b64);
345 24 my $digest = MIME::Base64::decode_base64($digest_b64);
346  
347 24 ▶ return unless length($digest) == length($ctx->digest);
Branch: 1 branch
BranchTrueFalseTotal
unless length $digest == length $ctx->digest 0 24 24
348  
349 24 return ('dbio', $algorithm, $salt, $digest);
350 }
351  
352 52 ▶sub _is_canonical_base64 {
Subroutine _is_canonical_base64: called CC=3 SCAR=11.7
353 52 my ($b64) = @_;
354 52 ▶ return 0 unless defined $b64;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $b64 0 52 52
355 52 ▶ return $b64 =~ m{\A(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?\z} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$b64 =~ m[\A(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?\z] ? : 52 0 52
356 }
357  
358 # A Digest context for exactly this algorithm, or undef. Never a substitute.
359 77 ▶sub _digest_ctx {
Subroutine _digest_ctx: called CC=4 SCAR=17.9
360 77 my ($algorithm) = @_;
361  
362 77 ▶ return unless defined $algorithm && $algorithm =~ m{\A[A-Za-z0-9][A-Za-z0-9/-]*\z};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $algorithm and $algorithm =~ m[\A[A-Za-z0-9][A-Za-z0-9/-]*\z] 0 77 77
Condition33%1/3
defined $algorithm and $algorithm =~ m[\A[A-Za-z0-9][A-Za-z0-9/-]*\z]
!ll&&!rl&&r
0077
MC/DC0%0/2
defined $algorithm and $algorithm =~ m[\A[A-Za-z0-9][A-Za-z0-9/-]*\z]
defined $algorithm $algorithm =~ m[\A[A-Za-z0-9][A-Za-z0-9/-]*\z]
Truth table33%1/3
A and B
A= defined $algorithmB= $algorithm =~ m[\A[A-Za-z0-9][A-Za-z0-9/-]*\z]
ABresult
0X0
100
111
363  
364 # Digest knows the SHA3 names only from 1.20 on; the context comes from
365 # Digest::SHA3 itself, which is what 1.20 would load for them.
366 77 ▶ if ($algorithm =~ m{\ASHA3-(224|256|384|512)\z}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($algorithm =~ /\ASHA3-(224|256|384|512)\z/) 0 77 77
367 0 my $bits = $1;
368 0 return eval { require Digest::SHA3; Digest::SHA3->new($bits) };
369 }
370  
371 77 return eval { Digest->new($algorithm) };
372 }
373  
374 1 ▶sub _bcrypt_check {
Subroutine _bcrypt_check: called CC=2 SCAR=8.1
375 1 my ($self, $bytes, $hash) = @_;
376  
377 1 ▶ $self->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
unless _module_loadable("Crypt::Bcrypt") 1 0 1
378 'Checking a bcrypt hash needs Crypt::Bcrypt, which is not installed '
379 . '(dependency group encoded_column)'
380 ) unless _module_loadable('Crypt::Bcrypt');
381  
382 0 return Crypt::Bcrypt::bcrypt_check($bytes, $hash);
383 }
384  
385 0 ▶sub _argon2_check {
Subroutine _argon2_check: not called CC=2 SCAR=17.9
386 0 my ($self, $bytes, $hash) = @_;
387  
388 0 ▶ $self->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
unless _module_loadable("Crypt::Argon2") 0 0 0
389 'Checking an argon2id hash needs Crypt::Argon2, which is not installed '
390 . '(dependency group encoded_column)'
391 ) unless _module_loadable('Crypt::Argon2');
392  
393 0 return Crypt::Argon2::argon2id_verify($hash, $bytes);
394 }
395  
396 21 ▶sub _random_bytes {
Subroutine _random_bytes: called CC=3 SCAR=11.8
397 21 my ($self, $len) = @_;
398  
399 21 my $bytes = eval { Crypt::URandom::urandom($len) };
400  
401 21 ▶ $self->throw_exception('no secure random source -- install Crypt::URandom')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes and length $bytes == $len 0 21 21
Condition33%1/3
defined $bytes and length $bytes == $len
!ll&&!rl&&r
0021
MC/DC0%0/2
defined $bytes and length $bytes == $len
defined $bytes length $bytes == $len
Truth table33%1/3
A and B
A= defined $bytesB= length $bytes == $len
ABresult
0X0
100
111
402 unless defined($bytes) && length($bytes) == $len;
403  
404 21 return $bytes;
405 }
406  
407 26 ▶sub _constant_time_eq {
Subroutine _constant_time_eq: called CC=6 SCAR=18.3
408 26 my ($a, $b) = @_;
409  
410 26 ▶ return 0 if !defined($a) || !defined($b);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a and defined $b 0 26 26
Condition33%1/3
defined $a and defined $b
!ll&&!rl&&r
0026
MC/DC0%0/2
defined $a and defined $b
defined $a defined $b
Truth table33%1/3
A and B
A= defined $aB= defined $b
ABresult
0X0
100
111
411 26 ▶ return 0 if length($a) != length($b);
Branch: 1 branch
BranchTrueFalseTotal
if length $a != length $b 1 25 26
412  
413 25 my $diff = 0;
414 25 for my $i (0 .. length($a) - 1) {
415 736 $diff |= ord(substr($a, $i, 1)) ^ ord(substr($b, $i, 1));
416 }
417  
418 25 ▶ return $diff == 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$diff == 0 ? : 17 8 25
419 }
420  
421 4 1;
422  
423 __END__
« blib/lib/DBIO/ResultSource/RowParser.pm ↑ Summary blib/lib/DBIO/Storage/DBI/DataTypeClassifier.pm »