Mojolicious/Plugin/FormTamperingProtector.pm

filter: Statementstmt 64.2% 45 / 70 Branchbran 29.1% 7 / 24 Conditioncond 44.4% 8 / 18 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 87.5% 14 / 16 Podpod 33.3% 2 / 6 totaltotal 54.1% 78 / 144 CC 20 SCAR 23.3
CC
20
Cov
54%
__ANON__
32.1
inject
12.0
__ANON__
9.8
CRAP 10.3
« HTML/ValidationRules/Legacy.pm ↑ Summary
1 package Mojolicious::Plugin::FormTamperingProtector;
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 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 our $VERSION = '0.03';
6 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Mojo::JSON qw(decode_json encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Mojo::Util qw{encode decode xml_escape hmac_sha1_sum secure_compare
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 b64_decode b64_encode};
10 3 ▶use HTML::ValidationRules::Legacy qw{validate extract};
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 our $TERM_ACTION = 0;
13 3 our $TERM_SCHEMA = 1;
14  
15 ### ---
16 ### register
17 ### ---
18 1 ▶sub register {
Subroutine register: called CC=2 SCAR=11.5
Pod documented
19 1 my ($self, $app, $opt) = @_;
20
21 1 my $schema_key = $opt->{namespace}. "-schema";
22 1 my $sess_key = $opt->{namespace}. '-sessid';
23
24 1 ▶ my $actions = ref $opt->{action} ? $opt->{action} : [$opt->{action}];
Branch: 1 branch
BranchTrueFalseTotal
ref $opt->{'action'} ? : 1 0 1
25
26 1 ▶ $app->hook(before_dispatch => sub {
Subroutine __ANON__: called CC=7 SCAR=34.7
27 1 my $c = shift;
28 1 my $req = $c->req;
29
30 0 ▶ if ($req->method eq 'POST' && grep {$_ eq $req->url->path} @$actions) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->method eq "POST" and grep {$_ eq $req->url->path;} @$actions) 0 1 1
Condition33%1/3
$req->method eq "POST" and grep {$_ eq $req->url->path;} @$actions
!ll&&!rl&&r
100
MC/DC0%0/2
$req->method eq "POST" and grep {$_ eq $req->url->path;} @$actions
$req->method eq "POST" grep {$_ eq $req->url->path;} @$actions
Truth table33%1/3
A and B
A= $req->method eq "POST"B= grep {$_ eq $req->url->path;} @$actions
ABresult
0X0
100
111
31
32 0 ▶ my $wrapper = deserialize(unsign(
Condition0%0/2
$c->session($sess_key) || ''
l!l
00
MC/DC0%0/1
$c->session($sess_key) || ''
$c->session($sess_key)
Truth table0%0/2
A
A= $c->session($sess_key)
Aresult
11
00
33 $req->param($schema_key),
34 ($c->session($sess_key) || ''). $app->secrets->[0]
35 ));
36
37 0 $req->params->remove($schema_key);
38
39 0 ▶ if (!$wrapper) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($wrapper) 0 0 0
40 0 return $opt->{blackhole}->($c,
41 'Form schema is missing, possible hacking attempt');
42 }
43 0 ▶ if ($req->url->path ne $wrapper->{$TERM_ACTION}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->url->path ne $wrapper->{$TERM_ACTION}) 0 0 0
44 0 return $opt->{blackhole}->($c,
45 'Action attribute has been tampered');
46 }
47
48 0 ▶ if (my $err = validate($wrapper->{$TERM_SCHEMA}, $req->params)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = validate($wrapper->{$TERM_SCHEMA}, $req->params)) 0 0 0
49 0 return $opt->{blackhole}->($c, $err);
50 }
51 }
52 1 });
53
54 1 ▶ $app->hook(after_dispatch => sub {
Subroutine __ANON__: called CC=4 SCAR=22.9
55 1 my $c = shift;
56
57 1 ▶ if ($c->res->headers->content_type =~ qr{^text/html} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($c->res->headers->content_type =~ qr"^text/html"u and $c->res->body =~ qr/<form\b/iu) 0 1 1
Condition33%1/3
$c->res->headers->content_type =~ qr"^text/html"u and $c->res->body =~ qr/<form\b/iu
!ll&&!rl&&r
010
MC/DC0%0/2
$c->res->headers->content_type =~ qr"^text/html"u and $c->res->body =~ qr/<form\b/iu
$c->res->headers->content_type =~ qr"^text/html"u $c->res->body =~ qr/<form\b/iu
Truth table33%1/3
A and B
A= $c->res->headers->content_type =~ qr"^text/html"uB= $c->res->body =~ qr/<form\b/iu
ABresult
0X0
100
111
58 $c->res->body =~ qr{<form\b}i) {
59
60 0 my $sessid = $c->session($sess_key);
61
62 0 ▶ if (! $sessid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sessid) 0 0 0
63 0 $sessid = hmac_sha1_sum(time(). {}. rand(), $$);
64 0 $c->session($sess_key => $sessid);
65 }
66
67 0 $c->res->body(inject(
68 $c->res->body,
69 $actions,
70 $schema_key,
71 $sessid. $app->secrets->[0],
72 $c->res->content->charset)
73 );
74 }
75 1 });
76 }
77  
78 0 ▶sub inject {
Subroutine inject: not called CC=3 SCAR=24.8
Pod documented
79 0 my ($html, $actions, $token_key, $secret, $charset) = @_;
80
81 0 ▶ if (! ref $html) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $html) 0 0 0
82 0 ▶ $html = Mojo::DOM->new($charset ? decode($charset, $html) : $html);
Branch: 1 branch
BranchTrueFalseTotal
$charset ? : 0 0 0
83 }
84  
85 0 ▶ $html->find(qq{form[action][method="post"]})->each(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
86 0 my $form = shift;
87 0 my $action = $form->attr('action');
88
89 0 ▶ return if (! grep {$_ eq $action} @$actions);
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $action;} @$actions 0 0 0
90
91 0 my $wrapper = sign(serialize({
92 $TERM_ACTION => $action,
93 $TERM_SCHEMA => extract($form, $charset),
94 }), $secret);
95
96 0 $form->append_content(sprintf(<<"EOF", $token_key, xml_escape $wrapper));
97 <div style="display:none">
98 <input type="hidden" name="%s" value="%s">
99 </div>
100 EOF
101 0 });
102
103 0 return encode($charset, $html);
104 }
105  
106 19 ▶sub serialize {
Subroutine serialize: called CC=2 SCAR=6.9
Pod undocumented
107 19 ▶ return b64_encode(encode_json(shift // return), '');
Condition100%2/2
shift() // (return)
l!l
172
MC/DC100%1/1
shift() // (return)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
108 }
109  
110 13 ▶sub deserialize {
Subroutine deserialize: called CC=2 SCAR=7.6
Pod undocumented
111 13 ▶ return decode_json(b64_decode(shift // return));
Condition50%1/2
shift() // (return)
l!l
130
MC/DC0%0/1
shift() // (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
112 }
113  
114 4 ▶sub sign {
Subroutine sign: called CC=1 SCAR=0.0
Pod undocumented
115 4 my ($value, $secret) = @_;
116 4 return $value. '--' . hmac_sha1_sum($value, $secret);
117 }
118  
119 5 ▶sub unsign {
Subroutine unsign: called CC=5 SCAR=16.5
Pod undocumented
120 5 my ($value, $secret) = @_;
121 5 ▶ if ($value && $secret && $value =~ s/--([^\-]+)$//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value and $secret and $value =~ s/--([^\-]+)$//u) 3 2 5
Condition50%3/6
$value and $secret and $value =~ s/--([^\-]+)$//u
!ll&&!rl&&r
023
$value and $secret
!ll&&!rl&&r
005
MC/DC33%1/3
$value and $secret and $value =~ s/--([^\-]+)$//u
$value $secret $value =~ s/--([^\-]+)$//u
Truth table50%2/4
A and B and C
A= $valueB= $secretC= $value =~ s/--([^\-]+)$//u
ABCresult
0XX0
10X0
1100
1111
122 3 my $sig = $1;
123 3 ▶ return $value if (secure_compare($sig, hmac_sha1_sum($value, $secret)));
Branch: 1 branch
BranchTrueFalseTotal
if secure_compare($sig, &hmac_sha1_sum($value, $secret)) 2 1 3
124 }
125 3 return;
126 }
127  
128 3 1;
129  
130 __END__
« HTML/ValidationRules/Legacy.pm ↑ Summary