blib/lib/Plack/Middleware/RedirectSSL.pm

filter: Statementstmt 98.6% 74 / 75 Branchbran 98.0% 51 / 52 Conditioncond 96.6% 29 / 30 MC/DCmcdc 88.2% 15 / 17 Subroutinesub 100% 15 / 15 Podpod 100% 5 / 5 totaltotal 97.4% 189 / 194 CC 37 SCAR 20.8
CC
37
Cov
98%
render_sts_policy
15.0
call
10.0
hsts
5.0
CRAP 8.0
↑ Summary
1 3 ▶use 5.006; use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Plack::Middleware::RedirectSSL;
4  
5 3 our $VERSION = '1.302';
6  
7 3 ▶BEGIN { require Plack::Middleware; our @ISA = 'Plack::Middleware' }
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use Plack::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Plack::Util::Accessor qw( ssl hsts_header );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Plack::Request ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # seconds minutes hours days weeks
14 sub DEFAULT_STS_MAXAGE () { 60 * 60 * 24 * 7 * 26 }
15 sub MIN_STS_PRELOAD_MAXAGE () { 60 * 60 * 24 * 365 }
16  
17 22 ▶sub call {
Subroutine call: called CC=10 SCAR=23.0
Pod documented
18 22 my ( $self, $env ) = ( shift, @_ );
19  
20 22 ▶ my $do_ssl = $self->ssl ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->ssl ? : 15 7 22
21 22 ▶ my $is_ssl = ( 'https' eq $env->{'psgi.url_scheme'} ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
'https' eq $env->{'psgi.url_scheme'} ? : 14 8 22
22  
23 22 ▶ if ( $is_ssl xor $do_ssl ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_ssl xor $do_ssl) 9 13 22
Condition100%4/4
$is_ssl xor $do_ssl
l&&rl&&!r!l&&r!l&&!r
10453
MC/DC100%2/2
$is_ssl xor $do_ssl
$is_ssl $do_ssl
Truth table100%4/4
A xor B
A= $is_sslB= $do_ssl
ABresult
110
101
011
000
24 9 my $m = $env->{'REQUEST_METHOD'};
25 9 ▶ return [ 400, [qw( Content-Type text/plain )], [ 'Bad Request' ] ]
Branch: 1 branch
BranchTrueFalseTotal
if "GET" ne $m and "HEAD" ne $m 2 7 9
Condition100%3/3
"GET" ne $m and "HEAD" ne $m
!ll&&!rl&&r
522
MC/DC100%2/2
"GET" ne $m and "HEAD" ne $m
"GET" ne $m "HEAD" ne $m
Truth table100%3/3
A and B
A= "GET" ne $mB= "HEAD" ne $m
ABresult
0X0
100
111
26 if 'GET' ne $m and 'HEAD' ne $m;
27 7 my $uri = Plack::Request->new( $env )->uri;
28 7 ▶ $uri->scheme( $do_ssl ? 'https' : 'http' );
Branch: 1 branch
BranchTrueFalseTotal
$do_ssl ? : 4 3 7
29 7 return [ 301, [ Location => $uri ], [] ];
30 }
31  
32 13 my $res = $self->app->( $env );
33  
34 13 ▶ return $res unless $is_ssl and my $hsts = $self->hsts_header;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_ssl and my $hsts = $self->hsts_header 4 9 13
Condition100%3/3
$is_ssl and my $hsts = $self->hsts_header
!ll&&!rl&&r
319
MC/DC100%2/2
$is_ssl and my $hsts = $self->hsts_header
$is_ssl my $hsts = $self->hsts_header
Truth table100%3/3
A and B
A= $is_sslB= my $hsts = $self->hsts_header
ABresult
0X0
100
111
35  
36 9 ▶ Plack::Util::response_cb( $res, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
37 9 Plack::Util::header_set( $_[0][1], 'Strict-Transport-Security', $hsts );
38 9 } );
39 }
40  
41 20 ▶sub hsts_policy {
Subroutine hsts_policy: called CC=4 SCAR=13.9
Pod documented
42 20 my ( $self, $policy ) = ( shift, @_ );
43 20 ▶ return $self->{'hsts_policy'} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 4 16 20
44 16 $self->hsts_header( render_sts_policy( $policy ) );
45 15 ▶ $self->{'hsts'} = $policy ? $policy->{'max_age'} || '00' : 0; # legacy compat
Branch: 1 branch
BranchTrueFalseTotal
$policy ? : 10 5 15
Condition100%2/2
$policy->{"max_age"} or "00"
l!l
91
MC/DC100%1/1
$policy->{"max_age"} or "00"
$policy->{"max_age"}
Truth table100%2/2
A
A= $policy->{"max_age"}
Aresult
11
00
46 15 $self->{'hsts_policy'} = $policy;
47 }
48  
49 10 ▶sub hsts {
Subroutine hsts: called CC=5 SCAR=16.1
Pod documented
50 10 my ( $self, $value ) = ( shift, @_ );
51 10 ▶ return $self->{'hsts'} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 2 8 10
52 $self->hsts_policy( ( $value or not defined $value )
53 8 ▶ ? { ( map %$_, $self->{'hsts_policy'} || () ), max_age => $value }
Branch: 1 branch
BranchTrueFalseTotal
$value || !defined($value) ? : 6 2 8
Condition83%5/6
$value or not defined $value
l!l&&r!l&&!r
512
$self->{'hsts_policy'} || ()
l!l&&r!l&&!r
330
MC/DC100%2/2
$value or not defined $value
$value not defined $value
MC/DC0%0/2
$self->{'hsts_policy'} || ()
$self->{'hsts_policy'} ()
Truth table100%3/3
A or not(B)
A= $valueB= not defined $value
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= $self->{'hsts_policy'}B= ()
ABresult
1X1
011
000
54 : undef
55 );
56 8 $self->{'hsts'} = $value;
57 }
58  
59 11 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
60 11 my $self = shift->SUPER::new( @_ );
61 11 ▶ $self->ssl(1) if not defined $self->ssl;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->ssl 10 1 11
62 11 ▶ if ( exists $self->{'hsts_policy'} ) { $self->hsts_policy( $self->{'hsts_policy'} ) }
Branch: 3 branches
BranchTrueFalseTotal
if (exists $self->{'hsts_policy'}) { } 5 6 11
elsif (exists $self->{'hsts'}) { } 2 4 6
elsif (not $self->hsts_header) { } 3 1 4
63 2 elsif ( exists $self->{'hsts'} ) { $self->hsts ( $self->{'hsts'} ) }
64 3 elsif ( not $self->hsts_header ) { $self->hsts_policy( {} ) }
65 10 $self;
66 }
67  
68 ########################################################################
69  
70 0 ▶sub _callsite () { my $i; while ( my ( $p, $f, $l ) = caller ++$i ) { return " at $f line $l.\n" if __PACKAGE__ ne $p } '' }
Subroutine _callsite: called CC=3 SCAR=11.4
Branch: 2 branches
BranchTrueFalseTotal
if (() = caller ++$i) 12 0 12
if __PACKAGE__ ne $p 10 2 12
71  
72 40 ▶sub render_sts_policy {
Subroutine render_sts_policy: called CC=15 SCAR=27.1
Pod documented
73 40 my ( $opt ) = @_;
74  
75 40 ▶ die 'HSTS policy must be a single undef value or hash ref', _callsite
Branch: 1 branch
BranchTrueFalseTotal
if 1 != @_ or defined $opt and "HASH" ne ref $opt 5 35 40
Condition100%6/6
1 != @_ or defined $opt and "HASH" ne ref $opt
l!l&&r!l&&!r
2335
defined $opt and "HASH" ne ref $opt
!ll&&!rl&&r
7283
MC/DC100%3/3
1 != @_ or defined $opt and "HASH" ne ref $opt
1 != @_ defined $opt "HASH" ne ref $opt
Truth table100%4/4
A or B and C
A= 1 != @_B= defined $optC= "HASH" ne ref $opt
ABCresult
1XX1
0111
00X0
0100
76 if 1 != @_ or defined $opt and 'HASH' ne ref $opt;
77  
78 35 ▶ return undef if not defined $opt;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt 7 28 35
79  
80 28 my @directive = qw( max_age include_subdomains preload );
81  
82 {
83 28 my %known = map +( $_, 1 ), @directive;
84 28 my $unknown = join ', ', map "'$_'", sort grep !$known{ $_ }, keys %$opt;
85 28 ▶ die "HSTS policy contains unknown directive(s) $unknown", _callsite if $unknown;
Branch: 1 branch
BranchTrueFalseTotal
if $unknown 1 27 28
86 }
87  
88 27 my ( $max_age, $include_subdomains, $preload ) = @$opt{ @directive };
89  
90 $max_age = defined $max_age
91 3 ▶ ? do { no warnings 'numeric'; int $max_age }
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 2 branches
BranchTrueFalseTotal
defined $max_age ? : 13 14 27
$preload ? : 5 9 14
92 : $preload ? MIN_STS_PRELOAD_MAXAGE : DEFAULT_STS_MAXAGE;
93  
94 27 ▶ die 'HSTS max_age 0 conflicts with setting other directives', _callsite
Branch: 1 branch
BranchTrueFalseTotal
if 0 == $max_age and $include_subdomains || $preload 2 25 27
Condition100%6/6
0 == $max_age and $include_subdomains || $preload
!ll&&!rl&&r
2142
$include_subdomains or $preload
l!l&&r!l&&!r
114
MC/DC100%3/3
0 == $max_age and $include_subdomains || $preload
0 == $max_age $include_subdomains $preload
Truth table100%4/4
A and B or C
A= 0 == $max_ageB= $include_subdomainsC= $preload
ABCresult
0XX0
1000
11X1
1011
95 if 0 == $max_age and ( $include_subdomains or $preload );
96  
97 25 ▶ if ( $preload ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($preload) 7 18 25
98 7 ▶ $include_subdomains = 1 unless defined $include_subdomains;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $include_subdomains 5 2 7
99 7 ▶ die 'HSTS preload conflicts with disabled include_subdomains', _callsite unless $include_subdomains;
Branch: 1 branch
BranchTrueFalseTotal
unless $include_subdomains 1 6 7
100 6 ▶ die "HSTS preload requires longer max_age (got $max_age; minimum ".MIN_STS_PRELOAD_MAXAGE.')', _callsite
Branch: 1 branch
BranchTrueFalseTotal
if 31536000 > $max_age 1 5 6
101 if MIN_STS_PRELOAD_MAXAGE > $max_age;
102 }
103  
104 # expose computed values back to the caller
105 23 @$opt{ @directive } = ( $max_age, !!$include_subdomains, !!$preload );
106  
107 23 join '; ', "max-age=$max_age", ('includeSubDomains') x !!$include_subdomains, ('preload') x !!$preload;
108 }
109  
110 3 1;
111  
112 __END__
↑ Summary