blib/lib/Plack/Middleware/SecureHeaders.pm

filter: Statementstmt 100% 30 / 30 Branchbran 100% 8 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod 100% 2 / 2 totaltotal 100% 47 / 47 CC 5 SCAR 8.2
CC
5
Cov
100%
call
4.0
prepare_app
2.0
BEGIN
1.0
CRAP 2.3
↑ Summary
1 package Plack::Middleware::SecureHeaders;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use parent qw( Plack::Middleware );
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 our $VERSION = "0.01";
7  
8 5 ▶use HTTP::SecureHeaders;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Plack::Util::Accessor qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
10 secure_headers
11 5 );
12  
13 12 ▶sub prepare_app {
Subroutine prepare_app: called CC=2 SCAR=6.9
Pod documented
14 12 my $self = shift;
15 12 ▶ unless (defined $self->secure_headers) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->secure_headers) 6 6 12
16 6 $self->secure_headers(HTTP::SecureHeaders->new)
17 }
18 }
19  
20 12 ▶sub call {
Subroutine call: called CC=4 SCAR=13.9
Pod documented
21 12 my($self, $env) = @_;
22  
23 12 my $res = $self->app->($env);
24 12 my $headers = Plack::Util::headers($res->[1]);
25  
26 12 ▶ unless ($headers->exists('Content-Type')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($headers->exists("Content-Type")) 1 11 12
27 1 die sprintf('Required Content-Type header. %s %s', $env->{REQUEST_METHOD}, $env->{PATH_INFO});
28 }
29  
30 # NOTE: the charset attribute is necessary to prevent XSS in HTML pages
31 # https://cheatsheetseries.owasp.org/cheatsheets/HTTP_Headers_Cheat_Sheet.html#content-type
32 11 ▶ if ($headers->get('Content-Type') =~ qr!^text/html!i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($headers->get("Content-Type") =~ qr"^text/html"i) 2 9 11
33 2 ▶ unless ($headers->get('Content-Type') =~ qr!charset=\w+!) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($headers->get("Content-Type") =~ qr/charset=\w+/) 1 1 2
34 die sprintf('Required charset for text/html. %s %s', $env->{REQUEST_METHOD}, $env->{PATH_INFO})
35 1 }
36 }
37  
38 10 $self->secure_headers->apply($headers);
39  
40 10 return $res;
41 }
42  
43 5 1;
44 __END__
↑ Summary