blib/lib/Catalyst/Plugin/HashedCookies.pm

filter: Statementstmt 99.1% 112 / 113 Branchbran 84.2% 32 / 38 Conditioncond 87.5% 7 / 8 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 4 totaltotal 92.9% 170 / 183 CC 24 SCAR 17.9
CC
24
Cov
95%
prepare_cookies
11.0
finalize
7.1
setup
6.0
CRAP 6.0
↑ Summary
1 package Catalyst::Plugin::HashedCookies;
2 {
3 15 $Catalyst::Plugin::HashedCookies::VERSION = '1.131710';
4 }
5  
6 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 15 ▶use MRO::Compat;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use Tie::IxHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 ▶use CGI::Simple::Cookie;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 15 ▶use Digest::HMAC_MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 15 ▶use Digest::HMAC_SHA1;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 {
17 package Catalyst::Request::HashedCookies;
18 {
19 15 $Catalyst::Request::HashedCookies::VERSION = '1.131710';
20 }
21 15 ▶ use base 'Catalyst::Request';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 15 __PACKAGE__->mk_accessors(qw/validhashedcookies invalidhashedcookies/);
24  
25 # reveal whether a hashed cookie passed its integrity check
26 9 ▶ sub valid_cookie {
Subroutine valid_cookie: called CC=1 SCAR=0.0
27 9 my $self = shift;
28 9 my $name = shift;
29  
30 9 return exists $self->validhashedcookies->{$name};
31 }
32  
33 # reveal whether a hashed cookie passed its integrity check
34 5 ▶ sub invalid_cookie {
Subroutine invalid_cookie: called CC=1 SCAR=0.0
35 5 my $self = shift;
36 5 my $name = shift;
37  
38 5 return exists $self->invalidhashedcookies->{$name};
39 }
40 }
41  
42 15 ▶sub setup {
Subroutine setup: called CC=6 SCAR=18.0
Pod undocumented
43 13 my $self = shift;
44  
45 # fix request class - thanks once again to mst
46 13 ▶ if ($self->request_class eq 'Catalyst::Request') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->request_class eq 'Catalyst::Request') { } 13 0 13
47 13 $self->request_class('Catalyst::Request::HashedCookies');
48 }
49 else {
50 0 die 'Please make a Request subclass for your application which '.
51 'isa Catalyst::Request::HashedCookies';
52 }
53  
54 13 ▶ $self->config->{hashedcookies}->{algorithm} ||= 'SHA1';
Condition100%2/2
$self->config->{"hashedcookies"}{"algorithm"} ||= "SHA1"
l!l
211
MC/DC100%1/1
$self->config->{"hashedcookies"}{"algorithm"} ||= "SHA1"
$self->config->{"hashedcookies"}{"algorithm"}
Truth table100%2/2
A
A= $self->config->{"hashedcookies"}{"algorithm"}
Aresult
11
00
55 13 ▶ ( $self->config->{hashedcookies}->{algorithm} =~ m/^(?:SHA1|MD5)$/ )
Branch: 1 branch
BranchTrueFalseTotal
$self->config->{"hashedcookies"}{"algorithm"} =~ /^(?:SHA1|MD5)$/ or die "Request for unknown digest algorithm to Catalyst::Plugin::HashedCookies" 12 1 13
56 or die 'Request for unknown digest algorithm to '. __PACKAGE__;
57  
58 exists $self->config->{hashedcookies}->{required}
59 12 ▶ or $self->config->{hashedcookies}->{required} = 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->config->{"hashedcookies"}{"required"} or $self->config->{"hashedcookies"}{"required"} = 1 1 11 12
60 # not checked - perl's handling of truth will make junk values 'work'
61  
62 defined $self->config->{hashedcookies}->{key}
63 12 ▶ or die '"key" is a required configuration parameter to '. __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->config->{"hashedcookies"}{"key"} or die "\"key\" is a required configuration parameter to Catalyst::Plugin::HashedCookies" 11 1 12
64  
65 11 return $self->next::method(@_);
66 }
67  
68  
69 # remove and check hash in Cookie Values
70 13 ▶sub prepare_cookies {
Subroutine prepare_cookies: called CC=11 SCAR=24.0
Pod undocumented
71 13 my $c = shift;
72 13 $c->next::method(@_);
73 13 $c->request->validhashedcookies( {} );
74 13 $c->request->invalidhashedcookies( {} );
75  
76 13 my $hasher = 'Digest::HMAC_'. $c->config->{hashedcookies}->{algorithm};
77 13 my $hmac = $hasher->new( $c->config->{hashedcookies}->{key} );
78  
79 13 ▶ while ( my ( $name, $cgicookie ) = each %{ $c->request->cookies } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$c->request->cookies;}) 14 13 27
80 14 my @values = @{ [ $cgicookie->value ] };
81 14 my $digest = '';
82  
83 # restore cookie to original Value set by user
84 14 ▶ if ( scalar @values % 2 == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @values % 2 == 0) 12 2 14
85 12 my $t = Tie::IxHash->new(@values);
86 12 my $d = $t->Indices('_hashedcookies_digest');
87 12 my $p = $t->Indices('_hashedcookies_padding');
88  
89 12 ▶ if ( defined $d ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $d) 8 4 12
90 8 $digest = $t->Values($d);
91 8 splice( @values, $d * 2, 2 );
92 }
93  
94 12 ▶ if ( defined $p ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $p) 2 10 12
95 2 splice( @values, $p * 2, 1 );
96 }
97  
98 12 $cgicookie->value( \@values );
99 }
100  
101 14 my $required = $c->config->{hashedcookies}->{required};
102 14 ▶ if ( not $digest and not $required ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($digest or $required) 1 13 14
Condition100%3/3
$digest or $required
l!l&&r!l&&!r
851
MC/DC100%2/2
$digest or $required
$digest $required
Truth table100%3/3
A or B
A= $digestB= $required
ABresult
1X1
011
000
103 1 ▶ $c->log->debug("HashedCookies skipping cookie: $name")
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 1 1
104 if $c->debug;
105 1 $hmac->reset;
106 1 next;
107 }
108  
109 # now, we either have no digest but one is required,
110 # or we have a digest that needs checking
111  
112 # $c->log->debug( "HashedCookies is hashing: ". $cgicookie->as_string );
113 13 $hmac->add( $cgicookie->as_string );
114 13 my $result = $hmac->hexdigest; # WARNING!!! $hmac has now been RESET
115  
116 # $c->log->debug( "HashedCookies retrieved digest: '$digest'" )
117 # if $c->debug;
118 # $c->log->debug( "HashedCookies generated digest: '$result'" )
119 # if $c->debug;
120  
121 13 ▶ if ( $digest eq $result ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digest eq $result) { } 5 8 13
122 5 ▶ $c->log->debug("HashedCookies adding valid cookie: $name")
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 5 5
123 if $c->debug;
124 5 ++$c->request->validhashedcookies->{$name};
125 }
126 else {
127 8 ▶ $c->log->debug("HashedCookies found INVALID cookie: $name")
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 8 8
128 if $c->debug;
129 8 ++$c->request->invalidhashedcookies->{$name};
130 }
131  
132 13 $hmac->reset;
133 }
134  
135 13 return $c;
136 }
137  
138  
139 # check for illegal parameters in cookie set by App, and raise hell if found
140 13 ▶sub finalize {
Subroutine finalize: called CC=7 SCAR=19.6
Pod undocumented
141 # need to hook in here, early in the finalize sequence, because Catalyst has
142 # been written to check $c->error *before* it goes on to call finalize_headers
143 # and hence finalize_cookies.
144 13 my $c = shift;
145  
146 13 ▶ while ( my ( $name, $cookie ) = each %{ $c->response->cookies } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$c->response->cookies;}) 8 13 21
147  
148 # see finalize_cookies hook, below, for comments
149 my $cgicookie = CGI::Simple::Cookie->new(
150 -name => $name,
151 -value => $cookie->{value},
152 8 );
153  
154 8 ▶ if (defined $cgicookie->value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cgicookie->value) 8 0 8
155 8 foreach ( @{ [ $cgicookie->value ] } ) {
156 12 ▶ if (defined and m/^_hashedcookies_/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_ and /^_hashedcookies_/) 1 11 12
Condition66%2/3
defined $_ and /^_hashedcookies_/
!ll&&!rl&&r
0111
MC/DC50%1/2
defined $_ and /^_hashedcookies_/
defined $_ /^_hashedcookies_/
Truth table66%2/3
A and B
A= defined $_B= /^_hashedcookies_/
ABresult
0X0
100
111
157 1 ▶ $c->log->debug('HashedCookies setting $c->error, illegal cookie param from App')
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 1 1
158 if $c->debug;
159 1 $c->error('Attempted use of restricted ("_hashedcookies_*") value in cookie');
160  
161 # don't want to have dud cookie sent to client browser
162 1 delete $c->response->cookies->{$cgicookie->name};
163 }
164 }
165 }
166 }
167  
168 13 $c->next::method(@_);
169 13 return $c;
170 }
171  
172  
173 # alter all Cookie Values to include a hash
174 13 ▶sub finalize_cookies {
Subroutine finalize_cookies: called CC=3 SCAR=11.0
Pod undocumented
175 13 my $c = shift;
176  
177 13 my $hasher = 'Digest::HMAC_'. $c->config->{hashedcookies}->{algorithm};
178 13 my $hmac = $hasher->new( $c->config->{hashedcookies}->{key} );
179  
180 13 ▶ while ( my ( $name, $cookie ) = each %{ $c->response->cookies } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$c->response->cookies;}) 7 13 20
181  
182 # creating a tmp CGI::Simple::Cookie is handy for as_string,
183 # and also because we can consistenly use ->value as a list
184 #
185 # only -name and -value are used because this is what CGI::Simple::Cookie->parse()
186 # will pass back from an HTTP header - prepare_cookies needs identical hash
187 my $cgicookie = CGI::Simple::Cookie->new(
188 -name => $name,
189 -value => $cookie->{value},
190 7 );
191  
192 # $c->log->debug( "HashedCookies is hashing: ". $cgicookie->as_string );
193 7 $hmac->add( $cgicookie->as_string );
194  
195 # make sure that cookie ->value can be coerced into a hash upon retrieval
196 7 ▶ if ( scalar @{ [ $cgicookie->value ] } % 2 == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{[$cgicookie->value];} % 2 == 1) { } 4 3 7
197 $cookie->{value} = [
198 4 '_hashedcookies_padding' => @{ [ $cgicookie->value ] },
199 '_hashedcookies_digest' => $hmac->hexdigest,
200 ];
201 }
202 else {
203 $cookie->{value} = [
204 3 @{ [ $cgicookie->value ] },
205 '_hashedcookies_digest' => $hmac->hexdigest,
206 ];
207 }
208  
209 7 $hmac->reset;
210 }
211  
212 13 $c->next::method(@_);
213 13 return $c;
214 }
215  
216 # ABSTRACT: Tamper-resistant HTTP Cookies
217  
218  
219 15 1;
220  
221 __END__
↑ Summary