| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 22 | ▶ | my $is_ssl = ( 'https' eq $env->{'psgi.url_scheme'} ) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 22 | ▶ | if ( $is_ssl xor $do_ssl ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%4/4
$is_ssl xor $do_ssl
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 9 | my $m = $env->{'REQUEST_METHOD'}; | |||||||||||||||||||||||||||||||||||||||||||||
| 25 | 9 | ▶ | return [ 400, [qw( Content-Type text/plain )], [ 'Bad Request' ] ] | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
"GET" ne $m and "HEAD" ne $m
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$is_ssl and my $hsts = $self->hsts_header
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 16 | $self->hsts_header( render_sts_policy( $policy ) ); | |||||||||||||||||||||||||||||||||||||||||||||
| 45 | 15 | ▶ | $self->{'hsts'} = $policy ? $policy->{'max_age'} || '00' : 0; # legacy compat | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$policy->{"max_age"} or "00"
MC/DC100%1/1
$policy->{"max_age"} or "00"
$policy->{"max_age"}
Truth table100%2/2
A
A= $policy->{"max_age"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 52 | $self->hsts_policy( ( $value or not defined $value ) | ||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 8 | ▶ | ? { ( map %$_, $self->{'hsts_policy'} || () ), max_age => $value } | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$value or not defined $value
$self->{'hsts_policy'} || ()
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
Truth table66%2/3
A || B
A= $self->{'hsts_policy'}B= ()
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 11 | ▶ | if ( exists $self->{'hsts_policy'} ) { $self->hsts_policy( $self->{'hsts_policy'} ) } | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%6/6
1 != @_ or defined $opt and "HASH" ne ref $opt
defined $opt and "HASH" ne ref $opt
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 76 | if 1 != @_ or defined $opt and 'HASH' ne ref $opt; | ||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 35 | ▶ | return undef if not defined $opt; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%6/6
0 == $max_age and $include_subdomains || $preload
$include_subdomains or $preload
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 95 | if 0 == $max_age and ( $include_subdomains or $preload ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 25 | ▶ | if ( $preload ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 7 | ▶ | $include_subdomains = 1 unless defined $include_subdomains; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 7 | ▶ | die 'HSTS preload conflicts with disabled include_subdomains', _callsite unless $include_subdomains; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 6 | ▶ | die "HSTS preload requires longer max_age (got $max_age; minimum ".MIN_STS_PRELOAD_MAXAGE.')', _callsite | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||||||