HTML/ValidationRules/Legacy.pm

filter: Statementstmt 31.1% 33 / 106 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 63.6% 7 / 11 Podpod 100% 2 / 2 totaltotal 18.6% 42 / 225 CC 51 SCAR 64.9
CC
51
Cov
17%
validate
1056.0
__ANON__
342.0
extract
12.0
CRAP 655.6
↑ Summary Mojolicious/Plugin/FormTamperingProtector.pm »
1 package HTML::ValidationRules::Legacy;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Mojo::Base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Mojo::JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Mojo::Util qw{decode};
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Mojo::Parameters;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 our @EXPORT_OK = qw(extract validate),
11  
12 our $TERM_PROPERTIES = 'properties';
13 3 our $TERM_REQUIRED = 'required';
14 3 our $TERM_MAXLENGTH = 'maxLength';
15 3 our $TERM_MIN_LENGTH = 'minLength';
16 3 our $TERM_OPTIONS = 'options';
17 3 our $TERM_PATTERN = 'pattern';
18 3 our $TERM_MIN = 'maximam';
19 3 our $TERM_MAX = 'minimum';
20 3 our $TERM_TYPE = 'type';
21 3 our $TERM_ADD_PROPS = 'additionalProperties';
22 3 our $TERM_NUMBER = 'number';
23  
24 0 ▶sub extract {
Subroutine extract: not called CC=3 SCAR=24.8
Pod documented
25 0 my ($form, $charset) = @_;
26 0 my $props = {};
27 0 my @required;
28
29 0 ▶ if (! ref $form) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $form) 0 0 0
30 0 ▶ $form = Mojo::DOM->new($charset ? decode($charset, $form) : $form);
Branch: 1 branch
BranchTrueFalseTotal
$charset ? : 0 0 0
31 }
32
33 0 ▶ $form->find("*[name]")->each(sub {
Subroutine __ANON__: not called CC=18 SCAR=58.3
34 0 my $tag = shift;
35 0 ▶ my $type = $tag->attr('type') || '';
Condition0%0/2
$tag->attr('type') || ''
l!l
00
MC/DC0%0/1
$tag->attr('type') || ''
$tag->attr('type')
Truth table0%0/2
A
A= $tag->attr('type')
Aresult
11
00
36 0 my $name = $tag->attr('name');
37 0 ▶ $props->{$name} ||= {};
Condition0%0/2
$props->{$name} ||= {}
l!l
00
MC/DC0%0/1
$props->{$name} ||= {}
$props->{$name}
Truth table0%0/2
A
A= $props->{$name}
Aresult
11
00
38
39 0 ▶ if (grep {$_ eq $type} qw{hidden checkbox radio submit image}) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $type;} "hidden", "checkbox", "radio", "submit", "image") 0 0 0
40 0 push(@{$props->{$name}->{$TERM_OPTIONS}}, $tag->attr('value'));
41 }
42
43 0 ▶ if ($tag->tag eq 'select') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag->tag eq "select") 0 0 0
44 0 ▶ $tag->find('option')->each(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
45 0 push(@{$props->{$name}->{$TERM_OPTIONS}}, shift->attr('value'));
46 0 });
47 }
48
49 0 ▶ if ($type eq 'number') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "number") 0 0 0
50 0 $props->{$name}->{$TERM_TYPE} = $TERM_NUMBER;
51 0 ▶ if (my $val = $tag->attr->{min}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $val = $tag->attr->{"min"}) 0 0 0
52 0 $props->{$name}->{$TERM_MIN} = $val;
53 }
54 0 ▶ if (my $val = $tag->attr->{max}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $val = $tag->attr->{"max"}) 0 0 0
55 0 $props->{$name}->{$TERM_MAX} = $val;
56 }
57 }
58
59 0 ▶ if (! exists $tag->attr->{disabled}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $tag->attr->{"disabled"}) 0 0 0
60 0 ▶ if ($type ne 'submit' && $type ne 'image' && $type ne 'checkbox' &&
Branch: 1 branch
BranchTrueFalseTotal
if ($type ne "submit" and $type ne "image" and $type ne "checkbox" and $type ne "radio" || exists $tag->attr->{"checked"}) 0 0 0
Condition0%0/12
$type ne "submit" and $type ne "image" and $type ne "checkbox" and $type ne "radio" || exists $tag->attr->{"checked"}
!ll&&!rl&&r
000
$type ne "submit" and $type ne "image" and $type ne "checkbox"
!ll&&!rl&&r
000
$type ne "submit" and $type ne "image"
!ll&&!rl&&r
000
$type ne "radio" or exists $tag->attr->{"checked"}
l!l&&r!l&&!r
000
MC/DC0%0/5
$type ne "submit" and $type ne "image" and $type ne "checkbox" and $type ne "radio" || exists $tag->attr->{"checked"}
$type ne "submit" $type ne "image" $type ne "checkbox" $type ne "radio" exists $tag->attr->{"checked"}
Truth table0%0/6
A and B and C and D or E
A= $type ne "submit"B= $type ne "image"C= $type ne "checkbox"D= $type ne "radio"E= exists $tag->attr->{"checked"}
ABCDEresult
0XXXX0
10XXX0
110XX0
111000
1111X1
111011
61 ($type ne 'radio' || exists $tag->attr->{checked})) {
62 0 $props->{$name}->{$TERM_REQUIRED} = Mojo::JSON->true;
63 }
64 }
65
66 0 ▶ if (exists $tag->attr->{maxlength}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $tag->attr->{"maxlength"}) 0 0 0
67 0 ▶ $props->{$name}->{$TERM_MAXLENGTH} = $tag->attr->{maxlength} || 0;
Condition0%0/2
$tag->attr->{'maxlength'} || 0
l!l
00
MC/DC0%0/1
$tag->attr->{'maxlength'} || 0
$tag->attr->{'maxlength'}
Truth table0%0/2
A
A= $tag->attr->{'maxlength'}
Aresult
11
00
68 }
69
70 0 ▶ if (exists $tag->attr->{required}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $tag->attr->{"required"}) 0 0 0
71 0 $props->{$name}->{$TERM_MIN_LENGTH} = 1;
72 }
73
74 0 ▶ if (exists $tag->attr->{pattern}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $tag->attr->{"pattern"}) 0 0 0
75 0 $props->{$name}->{$TERM_PATTERN} = $tag->attr->{pattern};
76 }
77 0 });
78
79 return {
80 0 $TERM_PROPERTIES => $props,
81 $TERM_ADD_PROPS => Mojo::JSON->false,
82 };
83 }
84  
85 0 ▶sub validate {
Subroutine validate: not called CC=32 SCAR=69.6
Pod documented
86 0 my ($schema, $params, $charset) = @_;
87
88 0 ▶ if (! (blessed($params) && $params->isa('Mojo::Parameters'))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($params) and $params->isa("Mojo::Parameters")) 0 0 0
Condition0%0/3
builtin::blessed($params) and $params->isa("Mojo::Parameters")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($params) and $params->isa("Mojo::Parameters")
builtin::blessed($params) $params->isa("Mojo::Parameters")
Truth table0%0/3
A and B
A= builtin::blessed($params)B= $params->isa("Mojo::Parameters")
ABresult
0X0
100
111
89 0 my $wrapper = Mojo::Parameters->new;
90 0 $wrapper->charset($charset);
91 0 ▶ if (blessed($params) && $params->isa('Hash::MultiValue')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($params) and $params->isa('Hash::MultiValue')) { } 0 0 0
Condition0%0/3
builtin::blessed($params) and $params->isa("Hash::MultiValue")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($params) and $params->isa("Hash::MultiValue")
builtin::blessed($params) $params->isa("Hash::MultiValue")
Truth table0%0/3
A and B
A= builtin::blessed($params)B= $params->isa("Hash::MultiValue")
ABresult
0X0
100
111
92 0 $wrapper->append($params->flatten);
93 } else {
94 0 $wrapper->append($params);
95 }
96 0 $params = $wrapper;
97 }
98
99 0 my $props = $schema->{$TERM_PROPERTIES};
100
101 0 ▶ if (! $schema->{$TERM_ADD_PROPS}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($schema->{$HTML::ValidationRules::Legacy::TERM_ADD_PROPS}) 0 0 0
102 0 for my $name (@{$params->names}) {
103 0 ▶ return "Field $name is injected" if (! $props->{$name});
Branch: 1 branch
BranchTrueFalseTotal
unless $props->{$name} 0 0 0
104 }
105 }
106
107 0 for my $name (keys %$props) {
108 0 my @params = grep {defined $_} $params->param($name);
109
110 0 ▶ if (($props->{$name}->{$TERM_REQUIRED} || '') eq Mojo::JSON->true) {
Branch: 1 branch
BranchTrueFalseTotal
if (($props->{$name}{$HTML::ValidationRules::Legacy::TERM_REQUIRED} || "") eq "Mojo::JSON"->true) 0 0 0
Condition0%0/2
$props->{$name}{$HTML::ValidationRules::Legacy::TERM_REQUIRED} || ''
l!l
00
MC/DC0%0/1
$props->{$name}{$HTML::ValidationRules::Legacy::TERM_REQUIRED} || ''
$props->{$name}{$HTML::ValidationRules::Legacy::TERM_REQUIRED}
Truth table0%0/2
A
A= $props->{$name}{$HTML::ValidationRules::Legacy::TERM_REQUIRED}
Aresult
11
00
111 0 ▶ return "Field $name is required" if (! scalar @params);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @params 0 0 0
112 }
113
114 0 ▶ if (my $allowed = $props->{$name}->{$TERM_OPTIONS}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $allowed = $props->{$name}{$HTML::ValidationRules::Legacy::TERM_OPTIONS}) 0 0 0
115 0 for my $given (@params) {
116 return "Field $name has been tampered"
117 0 ▶ if (! grep {$_ eq $given} @$allowed);
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $given;} @$allowed 0 0 0
118 }
119 }
120 0 ▶ if (exists $props->{$name}->{$TERM_MAXLENGTH}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $props->{$name}{$HTML::ValidationRules::Legacy::TERM_MAXLENGTH}) 0 0 0
121 0 for my $given (@params) {
122 return "Field $name is too long"
123 0 ▶ if (length($given) > $props->{$name}->{$TERM_MAXLENGTH});
Branch: 1 branch
BranchTrueFalseTotal
if length $given > $props->{$name}{$HTML::ValidationRules::Legacy::TERM_MAXLENGTH} 0 0 0
124 }
125 }
126 0 ▶ if (defined $props->{$name}->{$TERM_MIN_LENGTH}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $props->{$name}{$HTML::ValidationRules::Legacy::TERM_MIN_LENGTH}) 0 0 0
127 0 for my $given (@params) {
128 return "Field $name cannot be empty"
129 0 ▶ if (length($given) < $props->{$name}->{$TERM_MIN_LENGTH});
Branch: 1 branch
BranchTrueFalseTotal
if length $given < $props->{$name}{$HTML::ValidationRules::Legacy::TERM_MIN_LENGTH} 0 0 0
130 }
131 }
132 0 ▶ if (my $pattern = $props->{$name}->{$TERM_PATTERN}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pattern = $props->{$name}{$HTML::ValidationRules::Legacy::TERM_PATTERN}) 0 0 0
133 0 for my $given (@params) {
134 0 ▶ return "Field $name not match pattern"
Branch: 1 branch
BranchTrueFalseTotal
unless $given =~ /\A$pattern\Z/u 0 0 0
135 if ($given !~ /\A$pattern\Z/);
136 }
137 }
138 0 ▶ if (($props->{$name}->{$TERM_TYPE} || '') eq $TERM_NUMBER) {
Branch: 1 branch
BranchTrueFalseTotal
if (($props->{$name}{$HTML::ValidationRules::Legacy::TERM_TYPE} || "") eq $HTML::ValidationRules::Legacy::TERM_NUMBER) 0 0 0
Condition0%0/2
$props->{$name}{$HTML::ValidationRules::Legacy::TERM_TYPE} || ''
l!l
00
MC/DC0%0/1
$props->{$name}{$HTML::ValidationRules::Legacy::TERM_TYPE} || ''
$props->{$name}{$HTML::ValidationRules::Legacy::TERM_TYPE}
Truth table0%0/2
A
A= $props->{$name}{$HTML::ValidationRules::Legacy::TERM_TYPE}
Aresult
11
00
139 0 for my $given (@params) {
140 0 ▶ return "Field $name not match pattern"
Branch: 1 branch
BranchTrueFalseTotal
unless $given =~ /\A[\d\+\-\.]+\Z/u 0 0 0
141 if ($given !~ /\A[\d\+\-\.]+\Z/);
142 0 ▶ if (my $min = $props->{$name}->{$TERM_MIN}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $min = $props->{$name}{$HTML::ValidationRules::Legacy::TERM_MIN}) 0 0 0
143 0 ▶ return "Field $name too low" if ($given < $min);
Branch: 1 branch
BranchTrueFalseTotal
if $given < $min 0 0 0
144 }
145 0 ▶ if (my $max = $props->{$name}->{$TERM_MAX}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $max = $props->{$name}{$HTML::ValidationRules::Legacy::TERM_MAX}) 0 0 0
146 0 ▶ return "Field $name too great" if ($given > $max);
Branch: 1 branch
BranchTrueFalseTotal
if $given > $max 0 0 0
147 }
148 }
149 }
150 }
151 0 return;
152 }
153  
154 3 1;
155  
156 __END__
↑ Summary Mojolicious/Plugin/FormTamperingProtector.pm »