blib/lib/Mojolicious/Plugin/SecurityHeader.pm

filter: Statementstmt 100% 114 / 114 Branchbran 100% 98 / 98 Conditioncond 96.3% 26 / 27 MC/DCmcdc 88.2% 15 / 17 Subroutinesub 100% 11 / 11 Podpod 100% 1 / 1 totaltotal 98.8% 265 / 268 CC 63 SCAR 22.5
CC
63
Cov
100%
register
17.0
_check_xp
12.0
_check_methods
9.0
CRAP 9.5
↑ Summary
1 package Mojolicious::Plugin::SecurityHeader;
2  
3 # ABSTRACT: Mojolicious Plugin
4  
5 44 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 44 our $VERSION = '0.07';
8  
9 66 ▶sub register {
Subroutine register: called CC=17 SCAR=28.3
Pod documented
10 66 my ($self, $app, $headers) = @_;
11  
12 66 ▶ return if !$headers;
Branch: 1 branch
BranchTrueFalseTotal
unless $headers 2 64 66
13 64 ▶ return if !ref $headers;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $headers 1 63 64
14 63 ▶ return if 'ARRAY' ne ref $headers;
Branch: 1 branch
BranchTrueFalseTotal
if "ARRAY" ne ref $headers 1 62 63
15  
16 62 my @headers_list = qw(
17 Strict-Transport-Security Referrer-Policy
18 X-Content-Type-Options X-Frame-Options X-Xss-Protection
19 Content-Security-Policy Access-Control-Allow-Origin
20 Access-Control-Expose-Headers Access-Control-Max-Age
21 Access-Control-Allow-Credentials Access-Control-Allow-Methods
22 Access-Control-Allow-Headers
23 );
24  
25 62 my %valid_headers;
26 62 @valid_headers{@headers_list} = (1) x @headers_list;
27  
28 62 my %values = (
29 'X-Content-Type-Options' => 'nosniff',
30 'X-Xss-Protection' => \&_check_xp,
31 'X-Frame-Options' => \&_check_fo,
32 'Content-Security-Policy' => \&_check_csp,
33 'Access-Control-Allow-Methods' => \&_check_methods,
34 'Access-Control-Allow-Origin' => \&_is_url,
35 'Access-Control-Allow-Headers' => \&_check_list,
36 'Access-Control-Expose-Headers' => \&_check_list,
37 'Access-Control-Max-Age' => \&_is_int,
38 'Access-Control-Allow-Credentials' => 'true',
39 'Strict-Transport-Security' => \&_check_sts,
40 'Referrer-Policy' => [
41 "",
42 "no-referrer",
43 "no-referrer-when-downgrade",
44 "same-origin",
45 "origin",
46 "strict-origin",
47 "origin-when-cross-origin",
48 "strict-origin-when-cross-origin",
49 "unsafe-url"
50 ],
51 );
52  
53 62 my %options = (
54 'Strict-Transport-Security' => { includeSubDomains => 1, preload => 1 },
55 );
56  
57 62 my %headers_default = (
58 'Referrer-Policy' => "",
59 'Strict-Transport-Security' => "max-age=31536000",
60 'X-Content-Type-Options' => "nosniff",
61 'X-Xss-Protection' => '1; mode=block',
62 'X-Frame-Options' => 'DENY',
63 'Content-Security-Policy' => "default-src 'self'",
64 'Access-Control-Allow-Origin' => '*',
65 'Access-Control-Allow-Credentials' => 'true',
66 );
67  
68 62 my %security_headers;
69  
70 my $last_header;
71 62 my $header_value;
72  
73 HEADER:
74 62 for my $header ( @{ $headers } ) {
75 117 ▶ next HEADER if !defined $header;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $header 1 116 117
76  
77 116 ▶ if ( $valid_headers{$header} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($valid_headers{$header}) { } 64 52 116
elsif ($last_header) { } 51 1 52
78 64 ▶ if ( $last_header ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_header) 3 61 64
79 3 ▶ $security_headers{$last_header} = $header_value // $headers_default{$last_header};
Condition66%2/3
$header_value // $headers_default{$last_header}
l!l&&r!l&&!r
120
MC/DC0%0/2
$header_value // $headers_default{$last_header}
$header_value $headers_default{$last_header}
Truth table66%2/3
A // B
A= $header_valueB= $headers_default{$last_header}
ABresult
1X1
011
000
80 }
81  
82 64 undef $header_value;
83 64 $last_header = $header;
84 }
85 elsif ( $last_header ) {
86 51 $header_value = $header;
87  
88 51 my $ref = ref $values{$last_header};
89  
90 51 ▶ if ( $ref eq 'CODE' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'CODE') { } 37 14 51
elsif ($ref eq 'ARRAY') { } 9 5 14
91 37 $header_value = $values{$last_header}->($header_value, $options{$last_header});
92  
93 37 ▶ undef $last_header if !defined $header_value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $header_value 6 31 37
94 }
95 elsif ( $ref eq 'ARRAY' ) {
96 9 ($header_value) = grep{ $header_value eq $_ }@{ $values{$last_header} };
97  
98 9 ▶ undef $last_header if !$header_value;
Branch: 1 branch
BranchTrueFalseTotal
unless $header_value 1 8 9
99 }
100 else {
101 5 ▶ undef $last_header if $header_value ne $values{$last_header};
Branch: 1 branch
BranchTrueFalseTotal
if $header_value ne $values{$last_header} 2 3 5
102 }
103 }
104 }
105  
106 62 ▶ $security_headers{$last_header} = $header_value // $headers_default{$last_header} if $last_header;
Branch: 1 branch
BranchTrueFalseTotal
if $last_header 52 10 62
Condition100%3/3
$header_value // $headers_default{$last_header}
l!l&&r!l&&!r
4174
MC/DC100%2/2
$header_value // $headers_default{$last_header}
$header_value $headers_default{$last_header}
Truth table100%3/3
A // B
A= $header_valueB= $headers_default{$last_header}
ABresult
1X1
011
000
107  
108 116 ▶ $app->hook( before_dispatch => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
109 116 my $c = shift;
110  
111 HEADER_NAME:
112 116 for my $header_name ( keys %security_headers ) {
113 108 ▶ next HEADER_NAME if !defined $security_headers{$header_name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $security_headers{$header_name} 4 104 108
114 104 $c->res->headers->header( $header_name => $security_headers{$header_name} );
115 }
116 62 });
117 }
118  
119 10 ▶sub _is_int {
Subroutine _is_int: called CC=4 SCAR=13.9
120 10 my ($value, $options) = @_;
121  
122 10 ▶ return if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 9 10
123 9 ▶ return if ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 2 7 9
124 7 ▶ return if $value !~ m{\A[0-9]+\z};
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /\A[0-9]+\z/u 3 4 7
125 4 return $value;
126 }
127  
128 17 ▶sub _check_methods {
Subroutine _check_methods: called CC=9 SCAR=22.0
129 17 my ($value, $options) = @_;
130  
131 17 ▶ return if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 16 17
132  
133 16 my @methods = qw(GET DELETE POST PATCH OPTIONS HEAD CONNECT TRACE PUT);
134 16 ▶ if ( !ref $value && $value eq '*' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $value and $value eq "*") 1 15 16
Condition100%3/3
not ref $value and $value eq "*"
!ll&&!rl&&r
1141
MC/DC100%2/2
not ref $value and $value eq "*"
not ref $value $value eq "*"
Truth table100%3/3
not(A) and B
A= not ref $valueB= $value eq "*"
ABresult
0X0
100
111
135 1 return join ', ', @methods;
136 }
137  
138 15 ▶ return uc $value if !ref $value;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value 4 11 15
139 11 ▶ return if 'ARRAY' ne ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if "ARRAY" ne ref $value 1 10 11
140  
141 10 my %allowed = map{ $_ => 1 }@methods;
142 10 ▶ my $return = join ', ', map{ defined $_ && $allowed{uc $_} ? uc $_ : () }@{$value};
Branch: 1 branch
BranchTrueFalseTotal
defined $_ && $allowed{uc $_} ? : 11 5 16
Condition100%3/3
defined $_ and $allowed{uc $_}
!ll&&!rl&&r
1411
MC/DC100%2/2
defined $_ and $allowed{uc $_}
defined $_ $allowed{uc $_}
Truth table100%3/3
A and B
A= defined $_B= $allowed{uc $_}
ABresult
0X0
100
111
143  
144 10 ▶ return $return || undef;
Condition100%2/2
$return || undef
l!l
64
MC/DC100%1/1
$return || undef
$return
Truth table100%2/2
A
A= $return
Aresult
11
00
145 }
146  
147 15 ▶sub _check_list {
Subroutine _check_list: called CC=5 SCAR=16.1
148 15 my ($value, $options) = @_;
149  
150 15 ▶ return if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 14 15
151 14 ▶ return $value if !ref $value;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value 5 9 14
152 9 ▶ return if 'ARRAY' ne ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if "ARRAY" ne ref $value 2 7 9
153  
154 7 my $return = join ', ', @{$value};
155  
156 7 ▶ return $return || undef;
Condition100%2/2
$return || undef
l!l
61
MC/DC100%1/1
$return || undef
$return
Truth table100%2/2
A
A= $return
Aresult
11
00
157 }
158  
159 10 ▶sub _is_url {
Subroutine _is_url: called CC=5 SCAR=16.1
160 10 my ($value, $options) = @_;
161  
162 10 ▶ return if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 9 10
163 9 ▶ return if ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 2 7 9
164 7 ▶ return '*' if $value eq '*';
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "*" 2 5 7
165  
166 5 ▶ return $value if $value =~ m{\Ahttps?://\S+\z}xms;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ m[\Ahttps?://\S+\z]msux 2 3 5
167 3 return;
168 }
169  
170 12 ▶sub _check_csp {
Subroutine _check_csp: called CC=4 SCAR=13.9
171 12 my ($value, $options) = @_;
172  
173 12 my $option = '';
174  
175 12 ▶ return $option if !ref $value;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value 3 9 12
176 9 ▶ return $option if 'HASH' ne ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if "HASH" ne ref $value 2 7 9
177  
178 7 for my $key ( reverse sort keys %{ $value } ) {
179 10 my $tmp_value = $value->{$key};
180 10 $option .= sprintf "%s-src %s; ", $key, $tmp_value;
181 }
182  
183 7 return $option;
184 }
185  
186 8 ▶sub _check_sts {
Subroutine _check_sts: called CC=7 SCAR=19.5
187 8 my ($value, $options) = @_;
188  
189 8 my $option = '';
190  
191 8 ▶ if ( ref $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 3 5 8
192 3 $option = $value->{opt};
193 3 $value = $value->{maxage};
194  
195 3 ▶ $option = '' if !$options->{$option};
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{$option} 1 2 3
196 }
197  
198 8 ▶ $option = '; ' . $option if $option;
Branch: 1 branch
BranchTrueFalseTotal
if $option 2 6 8
199  
200 8 ▶ return 'max-age=31536000' . $option if $value == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $value == -1 2 6 8
201 6 ▶ return if $value < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $value < 0 1 5 6
202 5 ▶ return if $value ne int $value;
Branch: 1 branch
BranchTrueFalseTotal
if $value ne int $value 1 4 5
203 4 return 'max-age=' . $value . $option;
204 }
205  
206 14 ▶sub _check_fo {
Subroutine _check_fo: called CC=6 SCAR=17.9
207 14 my ($value) = @_;
208  
209 14 my %allowed = ('DENY' => 1, 'SAMEORIGIN' => 1);
210
211 14 ▶ return 'DENY' if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 13 14
212 13 ▶ return $value if $allowed{$value};
Branch: 1 branch
BranchTrueFalseTotal
if $allowed{$value} 4 9 13
213 9 ▶ return if !ref $value;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value 2 7 9
214 7 ▶ return if 'HASH' ne ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if "HASH" ne ref $value 1 6 7
215  
216 6 ▶ return if !$value->{'ALLOW-FROM'};
Branch: 1 branch
BranchTrueFalseTotal
unless $value->{"ALLOW-FROM"} 3 3 6
217 3 return 'ALLOW-FROM ' . $value->{'ALLOW-FROM'};
218 }
219  
220 17 ▶sub _check_xp {
Subroutine _check_xp: called CC=12 SCAR=24.8
221 17 my ($value, $options) = @_;
222  
223 17 ▶ if ( !ref $value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $value) 6 11 17
224 6 ▶ $value //= '';
Condition100%2/2
$value //= ""
l!l
51
MC/DC100%1/1
$value //= ""
$value
Truth table100%2/2
A
A= $value
Aresult
11
00
225  
226 6 ▶ return if $value ne '1' && $value ne '0';
Branch: 1 branch
BranchTrueFalseTotal
if $value ne 1 and $value ne 0 3 3 6
Condition100%3/3
$value ne 1 and $value ne 0
!ll&&!rl&&r
213
MC/DC100%2/2
$value ne 1 and $value ne 0
$value ne 1 $value ne 0
Truth table100%3/3
A and B
A= $value ne 1B= $value ne 0
ABresult
0X0
100
111
227 3 return $value;
228 }
229  
230 11 ▶ return if 'HASH' ne ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if "HASH" ne ref $value 1 10 11
231 10 ▶ return if !exists $value->{value} || $value->{value} ne '1';
Branch: 1 branch
BranchTrueFalseTotal
if not exists $value->{"value"} or $value->{"value"} ne 1 3 7 10
Condition100%3/3
not exists $value->{"value"} or $value->{"value"} ne 1
l!l&&r!l&&!r
217
MC/DC100%2/2
not exists $value->{"value"} or $value->{"value"} ne 1
not exists $value->{"value"} $value->{"value"} ne 1
Truth table100%3/3
not(A) or B
A= not exists $value->{"value"}B= $value->{"value"} ne 1
ABresult
1X1
011
000
232  
233 7 my $option = '';
234  
235 7 ▶ if ( $value->{mode} && $value->{mode} eq 'block' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($value->{'mode'} and $value->{'mode'} eq 'block') { } 2 5 7
elsif ($value->{'report'}) { } 2 3 5
Condition100%3/3
$value->{"mode"} and $value->{"mode"} eq "block"
!ll&&!rl&&r
412
MC/DC100%2/2
$value->{"mode"} and $value->{"mode"} eq "block"
$value->{"mode"} $value->{"mode"} eq "block"
Truth table100%3/3
A and B
A= $value->{"mode"}B= $value->{"mode"} eq "block"
ABresult
0X0
100
111
236 2 $option = 'mode=block';
237 }
238 elsif ( $value->{report} ) {
239 2 $option = 'report=' . $value->{report};
240 }
241  
242 7 $value = '1; ';
243 7 ▶ $value .= $option if $option;
Branch: 1 branch
BranchTrueFalseTotal
if $option 4 3 7
244  
245 7 return $value;
246 }
247  
248 44 1;
249  
250 __END__
↑ Summary