blib/lib/HTTP/SecureHeaders.pm

filter: Statementstmt 81.4% 79 / 97 Branchbran 100% 36 / 36 Conditioncond 91.6% 11 / 12 MC/DCmcdc 85.7% 6 / 7 Subroutinesub 56.1% 23 / 41 Podpod 20.0% 2 / 10 totaltotal 77.3% 157 / 203 CC 26 SCAR 13.9
CC
26
Cov
87%
_apply
11.0
check_content_security_policy
6.0
new
6.0
CRAP 4.0
↑ Summary Build »
1 package HTTP::SecureHeaders;
2 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 14 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 14 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 14 our $VERSION = "0.01";
9  
10 14 our %DEFAULT_HEADERS = (
11 content_security_policy => "default-src 'self' https:; font-src 'self' https: data:; img-src 'self' https: data:; object-src 'none'; script-src https:; style-src 'self' https: 'unsafe-inline'",
12 strict_transport_security => 'max-age=631138519',
13 x_content_type_options => 'nosniff',
14 x_download_options => 'noopen',
15 x_frame_options => 'SAMEORIGIN',
16 x_permitted_cross_domain_policies => 'none',
17 x_xss_protection => '1; mode=block',
18 referrer_policy => 'strict-origin-when-cross-origin',
19 );
20  
21 14 our %HTTP_FIELD_MAP = (
22 content_security_policy => 'Content-Security-Policy',
23 strict_transport_security => 'Strict-Transport-Security',
24 x_content_type_options => 'X-Content-Type-Options',
25 x_download_options => 'X-Download-Options',
26 x_frame_options => 'X-Frame-Options',
27 x_permitted_cross_domain_policies => 'X-Permitted-Cross-Domain-Policies',
28 x_xss_protection => 'X-XSS-Protection',
29 referrer_policy => 'Referrer-Policy',
30 );
31  
32 14 ▶use constant OPT_OUT => \"";
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 sub new {
35 26 ▶ my ($class, %args) = @_;
Subroutine new: called CC=6 SCAR=17.9
Pod documented
36  
37 26 my %fields = (%DEFAULT_HEADERS, %args);
38  
39 26 for my $field (keys %fields) {
40 207 ▶ unless (exists $HTTP_FIELD_MAP{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $HTTP::SecureHeaders::HTTP_FIELD_MAP{$field}) 1 206 207
41 1 Carp::croak sprintf('unknown HTTP field. %s', $field);
42 }
43  
44 206 my $value = $fields{$field};
45 206 my $checker = $class->can("check_$field");
46 206 ▶ unless ($checker) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($checker) 1 205 206
47 1 Carp::croak sprintf('cannot find check function. %s', "check_$field")
48 }
49  
50 # undef value is available for optout from headers
51 205 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 4 201 205
52  
53 201 ▶ unless ($checker->($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&$checker($value)) 1 200 201
54 1 Carp::croak sprintf('invalid HTTP header value. %s:%s', $field, $value);
55 }
56 }
57  
58 23 bless \%fields, $class;
59 }
60  
61 sub apply {
62 22 ▶ my ($self, $headers) = @_;
Subroutine apply: called CC=2 SCAR=6.9
Pod documented
63  
64 22 my @fields = keys %$self;
65 22 for my $field (@fields) {
66 148 $self->_apply($headers, $field);
67 }
68 }
69  
70 sub _apply {
71 148 ▶ my ($self, $headers, $field) = @_;
Subroutine _apply: called CC=11 SCAR=24.0
72  
73 148 my $http_field = $HTTP_FIELD_MAP{$field};
74  
75 148 ▶ unless (Scalar::Util::blessed($headers)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($headers)) 1 147 148
76 1 Carp::croak sprintf('headers must be HTTP::Headers or HasMethods["exists","get","set"]. %s', $headers);
77 }
78  
79 147 ▶ if ($headers->isa('HTTP::Headers')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($headers->isa('HTTP::Headers')) { } 96 51 147
elsif ($headers->can('exists') and $headers->can('get') and $headers->can('set')) { } 48 3 51
80 96 ▶ if (defined $headers->header($http_field)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $headers->header($http_field)) { } 4 92 96
81 4 ▶ if ($headers->header($http_field) eq OPT_OUT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($headers->header($http_field) eq \"") 2 2 4
82 2 $headers->header($http_field, undef)
83 }
84 }
85 else {
86 92 $headers->header($http_field, $self->{$field})
87 }
88 }
89 ▶ elsif ($headers->can('exists') && $headers->can('get') && $headers->can('set')) {
Condition100%6/6
$headers->can("exists") and $headers->can("get") and $headers->can("set")
!ll&&!rl&&r
2148
$headers->can("exists") and $headers->can("get")
!ll&&!rl&&r
1149
MC/DC100%3/3
$headers->can("exists") and $headers->can("get") and $headers->can("set")
$headers->can("exists") $headers->can("get") $headers->can("set")
Truth table100%4/4
A and B and C
A= $headers->can("exists")B= $headers->can("get")C= $headers->can("set")
ABCresult
0XX0
10X0
1100
1111
90 48 ▶ if (defined $headers->get($http_field)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $headers->get($http_field)) { } 2 46 48
elsif (not $headers->exists($http_field)) { } 45 1 46
91 2 ▶ if ($headers->get($http_field) eq OPT_OUT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($headers->get($http_field) eq \"") 1 1 2
92 1 $headers->set($http_field, undef);
93 }
94 }
95 elsif (!$headers->exists($http_field)) {
96 45 $headers->set($http_field, $self->{$field})
97 }
98 }
99 else {
100 3 Carp::croak sprintf('unknown headers: %s', $headers);
101 }
102 }
103  
104 # refs https://w3c.github.io/webappsec-csp/#csp-header
105 {
106 my $directive_map = {
107 # TODO implements directive_value checker
108 0 ▶ 'child-src' => sub { 1 }, # serialized-source-list
Subroutine __ANON__: not called CC=1 SCAR=6.9
109 0 ▶ 'connect-src' => sub { 1 }, # serialized-source-list
Subroutine __ANON__: not called CC=1 SCAR=6.9
110 26 ▶ 'default-src' => sub { 1 }, # serialized-source-list
Subroutine __ANON__: called CC=1 SCAR=0.0
111 21 ▶ 'font-src' => sub { 1 }, # serialized-source-list
Subroutine __ANON__: called CC=1 SCAR=0.0
112 0 ▶ 'frame-src' => sub { 1 }, # serialized-source-list
Subroutine __ANON__: not called CC=1 SCAR=6.9
113 21 ▶ 'img-src' => sub { 1 }, # serialized-source-list
Subroutine __ANON__: called CC=1 SCAR=0.0
114 0 ▶ 'manifest-src' => sub { 1 }, # serialized-source-list
Subroutine __ANON__: not called CC=1 SCAR=6.9
115 0 ▶ 'media-src' => sub { 1 }, # serialized-source-list
Subroutine __ANON__: not called CC=1 SCAR=6.9
116 21 ▶ 'object-src' => sub { 1 }, # serialized-source-list
Subroutine __ANON__: called CC=1 SCAR=0.0
117 0 ▶ 'prefetch-src' => sub { 1 }, # serialized-source-list
Subroutine __ANON__: not called CC=1 SCAR=6.9
118 21 ▶ 'script-src' => sub { 1 }, # serialized-source-list
Subroutine __ANON__: called CC=1 SCAR=0.0
119 0 ▶ 'script-src-elem' => sub { 1 }, # serialized-source-list
Subroutine __ANON__: not called CC=1 SCAR=6.9
120 0 ▶ 'script-src-attr' => sub { 1 }, # serialized-source-list
Subroutine __ANON__: not called CC=1 SCAR=6.9
121 21 ▶ 'style-src' => sub { 1 }, # serialized-source-list
Subroutine __ANON__: called CC=1 SCAR=0.0
122 0 ▶ 'style-src-elem' => sub { 1 }, # serialized-source-list
Subroutine __ANON__: not called CC=1 SCAR=6.9
123 0 ▶ 'style-src-attr' => sub { 1 }, # serialized-source-list
Subroutine __ANON__: not called CC=1 SCAR=6.9
124 3 ▶ 'webrtc' => sub { $_[0] eq "'allow'" or $_[0] eq "'block'" },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] eq "'allow'" or $_[0] eq "'block'" 2 1 3
125 0 ▶ 'worker-src' => sub { 1 }, # serialized-source-list
Subroutine __ANON__: not called CC=1 SCAR=6.9
126 0 ▶ 'base-uri' => sub { 1 }, # serialized-source-list
Subroutine __ANON__: not called CC=1 SCAR=6.9
127 0 ▶ 'sandbox' => sub { 1 }, # "" / token *( required-ascii-whitespace token ),
Subroutine __ANON__: not called CC=1 SCAR=6.9
128 0 ▶ 'form-action' => sub { 1 }, # serialized-source-list
Subroutine __ANON__: not called CC=1 SCAR=6.9
129 0 ▶ 'frame-ancestors' => sub { 1 }, # ancestor-source-list
Subroutine __ANON__: not called CC=1 SCAR=6.9
130 0 ▶ 'navigate-to' => sub { 1 }, # serialized-source-list
Subroutine __ANON__: not called CC=1 SCAR=6.9
131 0 ▶ 'report-uri' => sub { 1 }, # uri-reference *( required-ascii-whitespace uri-reference )
Subroutine __ANON__: not called CC=1 SCAR=6.9
132 0 ▶ 'report-to' => sub { 1 }, # token
Subroutine __ANON__: not called CC=1 SCAR=6.9
133 14 };
134  
135 sub check_content_security_policy {
136 # serialized-directive *( optional-ascii-whitespace ";" [ optional-ascii-whitespace serialized-directive ] )
137  
138 # serialized-directive = directive-name [ required-ascii-whitespace directive-value ]
139 # directive-name = 1*( ALPHA / DIGIT / "-" )
140 # directive-value = *( required-ascii-whitespace / ( %x21-%x2B / %x2D-%x3A / %x3C-%x7E ) )
141 # ; Directive values may contain whitespace and VCHAR characters,
142 # ; excluding ";" and ",". The second half of the definition
143 # ; above represents all VCHAR characters (%x21-%x7E)
144 # ; without ";" and "," (%x3B and %x2C respectively)
145  
146 35 ▶ my @directives = split ';', $_[0];
Subroutine check_content_security_policy: called CC=6 SCAR=17.9
Pod undocumented
147 35 for my $directive (@directives) {
148 140 my ($name, $value) = $directive =~ m!\s?([A-Za-z0-9\-]+)\s([^\s;,][^;,]+)!;
149 140 ▶ unless ($name && $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name and $value) 4 136 140
Condition66%2/3
$name and $value
!ll&&!rl&&r
40136
MC/DC50%1/2
$name and $value
$name $value
Truth table66%2/3
A and B
A= $nameB= $value
ABresult
0X0
100
111
150 4 return !!0
151 }
152 136 my $checker = $directive_map->{$name};
153 136 ▶ unless ($checker) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($checker) 2 134 136
154 2 return !!0
155 }
156 134 ▶ unless ($checker->($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&$checker($value)) 1 133 134
157 1 return !!0
158 }
159 }
160 28 return !!1;
161 }
162 }
163  
164  
165 # refs https://datatracker.ietf.org/doc/html/rfc6797
166 # refs https://www.chromium.org/hsts/
167 14 sub check_strict_transport_security {
168 39 ▶ $_[0] =~ m!\Amax-age=(?:[0-9]+)(?:\s?;\s?includeSubDomains)?(?:\s?;\s?preload)?\z!
Subroutine check_strict_transport_security: called CC=1 SCAR=0.0
Pod undocumented
169 }
170  
171 # refs http://blogs.msdn.com/b/ie/archive/2008/07/02/ie8-security-part-v-comprehensive-protection.aspx
172 sub check_x_content_type_options {
173 28 ▶ $_[0] eq 'nosniff'
Subroutine check_x_content_type_options: called CC=1 SCAR=0.0
Pod undocumented
174 }
175  
176 # refs http://blogs.msdn.com/b/ie/archive/2008/07/02/ie8-security-part-v-comprehensive-protection.aspx
177 sub check_x_download_options {
178 26 ▶ $_[0] eq 'noopen'
Subroutine check_x_download_options: called CC=1 SCAR=0.0
Pod undocumented
179 }
180  
181 # refs https://www.rfc-editor.org/rfc/rfc7034#section-2
182 sub check_x_frame_options {
183 32 ▶ $_[0] eq 'SAMEORIGIN' or
Subroutine check_x_frame_options: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] eq "SAMEORIGIN" or $_[0] eq "DENY" 8 24 32
184 $_[0] eq 'DENY'
185 # ALLOW-FROM # deprecated
186 }
187  
188 # refs https://www.adobe.com/devnet-docs/acrobatetk/tools/AppSec/CrossDomain_PolicyFile_Specification.pdf
189 sub check_x_permitted_cross_domain_policies {
190 34 ▶ $_[0] =~ m!\A(?:none|master-only|by-content-type|by-ftp-filename|all)\z!
Subroutine check_x_permitted_cross_domain_policies: called CC=1 SCAR=0.0
Pod undocumented
191 }
192  
193 # refs https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-XSS-Protection
194 sub check_x_xss_protection {
195 31 ▶ $_[0] eq '0' or
Subroutine check_x_xss_protection: called CC=3 SCAR=11.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] eq 0 or $_[0] eq 1 or $_[0] eq "1; mode=block" 25 6 31
Condition100%3/3
$_[0] eq 0 or $_[0] eq 1
l!l&&r!l&&!r
2425
MC/DC100%2/2
$_[0] eq 0 or $_[0] eq 1
$_[0] eq 0 $_[0] eq 1
Truth table100%3/3
A or B
A= $_[0] eq 0B= $_[0] eq 1
ABresult
1X1
011
000
196 $_[0] eq '1' or
197 $_[0] eq '1; mode=block'
198  
199 # `report=<report-uri>` directive not recommend
200 }
201  
202 # refs https://w3c.github.io/webappsec-referrer-policy/#referrer-policy-header
203 {
204 14 my $referrer_policy_values = {
205 'strict-origin-when-cross-origin' => 1,
206 'no-referrer' => 1,
207 'no-referrer-when-downgrade' => 1,
208 'same-origin' => 1,
209 'origin' => 1,
210 'strict-origin' => 1,
211 'origin-when-cross-origin' => 1,
212 'unsafe-url' => 1,
213 };
214  
215 # empty string cannot pass.
216 sub check_referrer_policy {
217 36 ▶ exists $referrer_policy_values->{$_[0]}
Subroutine check_referrer_policy: called CC=1 SCAR=0.0
Pod undocumented
218 }
219 }
220  
221 14 1;
222 __END__
↑ Summary Build »