blib/lib/Mojolicious/Plugin/StrictCORS.pm

filter: Statementstmt 7.0% 9 / 127 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 29 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 18.1% 2 / 11 Podpod 100% 1 / 1 totaltotal 4.9% 12 / 244 CC 45 SCAR 46.1
CC
45
Cov
4%
__ANON__
240.0
__ANON__
110.0
__ANON__
56.0
CRAP 100.9
↑ Summary
1 package Mojolicious::Plugin::StrictCORS;
2 1 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 our $VERSION = "0.02";
5 1 $VERSION = eval $VERSION;
6  
7 1 ▶use constant DEFAULT_MAX_AGE => 3600;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 0 ▶sub register {
Subroutine register: not called CC=3 SCAR=24.8
Pod documented
10 0 my ($self, $app, $conf) = @_;
11  
12 0 ▶ $conf->{max_age} //= DEFAULT_MAX_AGE;
Condition0%0/2
$conf->{"max_age"} //= 3600
l!l
00
MC/DC0%0/1
$conf->{"max_age"} //= 3600
$conf->{"max_age"}
Truth table0%0/2
A
A= $conf->{"max_age"}
Aresult
11
00
13 0 ▶ $conf->{expose} //= [];
Condition0%0/2
$conf->{"expose"} //= []
l!l
00
MC/DC0%0/1
$conf->{"expose"} //= []
$conf->{"expose"}
Truth table0%0/2
A
A= $conf->{"expose"}
Aresult
11
00
14  
15 # Recursive get route params
16 0 ▶ my $route_params = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
17 0 my ($c) = @_;
18  
19 0 my $route = $c->match->endpoint;
20  
21 0 my %params;
22 0 my @fields = qw/origin credentials expose methods headers/;
23  
24 0 ▶ while ($route) {
Branch: 1 branch
BranchTrueFalseTotal
if ($route) 0 0 0
25 0 for my $name (@fields) {
26 0 ▶ next if exists $params{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{$name} 0 0 0
27 0 ▶ next unless exists $route->to->{"cors_${name}"};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $route->to->{"cors_$name"} 0 0 0
28  
29 0 $params{$name} = $route->to->{"cors_${name}"};
30 }
31  
32 0 $route = $route->parent;
33 }
34  
35 0 return \%params;
36 0 };
37  
38 0 ▶ my $check_preflight = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
39 0 my ($c) = @_;
40  
41 0 my $h = $c->req->headers;
42  
43 0 ▶ return 1 if $h->header('Origin') =~ qr/\S/ms
Branch: 1 branch
BranchTrueFalseTotal
if $h->header("Origin") =~ qr/\S/msu and $h->header("Access-Control-Request-Method") =~ qr/\S/msu 0 0 0
Condition0%0/3
$h->header("Origin") =~ qr/\S/msu and $h->header("Access-Control-Request-Method") =~ qr/\S/msu
!ll&&!rl&&r
000
MC/DC0%0/2
$h->header("Origin") =~ qr/\S/msu and $h->header("Access-Control-Request-Method") =~ qr/\S/msu
$h->header("Origin") =~ qr/\S/msu $h->header("Access-Control-Request-Method") =~ qr/\S/msu
Truth table0%0/3
A and B
A= $h->header("Origin") =~ qr/\S/msuB= $h->header("Access-Control-Request-Method") =~ qr/\S/msu
ABresult
0X0
100
111
44 and $h->header('Access-Control-Request-Method') =~ qr/\S/ms;
45  
46 0 return; # Fail
47 0 };
48  
49 # Check Origin header
50 0 ▶ my $check_origin = sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
51 0 my ($c, @allow) = @_;
52  
53 0 my $h = $c->req->headers;
54  
55 0 my $origin = $h->origin;
56 0 ▶ return unless defined $origin;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $origin 0 0 0
57  
58 0 ▶ return $origin if grep { not ref $_ and $_ eq '*' } @allow;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not ref $_ and $_ eq "*";} @allow 0 0 0
Condition0%0/3
!ref($_) && $_ eq '*'
!ll&&!rl&&r
000
MC/DC0%0/2
!ref($_) && $_ eq '*'
!ref($_) $_ eq '*'
Truth table0%0/3
not(A) && B
A= !ref($_)B= $_ eq '*'
ABresult
0X0
100
111
59  
60 return $origin if grep {
61 0 ▶ if (not ref $_) { lc $origin eq lc $_ }
Branch: 3 branches
BranchTrueFalseTotal
if grep {if (not ref $_) { lc $origin eq lc $_; } elsif (ref $_ eq "Regexp") { $origin =~ /$_/u; } else { die "Router 'cors_origin' param must be scalar or Regexp\n"; } ;} @allow 0 0 0
if (not ref $_) { } 0 0 0
elsif (ref $_ eq 'Regexp') { } 0 0 0
62 0 elsif (ref $_ eq 'Regexp') { $origin =~ $_ }
63 0 else { die "Router 'cors_origin' param must be scalar or Regexp\n" }
64 } @allow;
65  
66 0 $app->log->debug("Reject CORS Origin '$origin'");
67  
68 0 return; # Fail
69 0 };
70  
71 # Check request method
72 0 ▶ my $check_methods = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
73 0 my ($c, @allow) = @_;
74  
75 0 my $h = $c->req->headers;
76  
77 0 my $method = $h->header('Access-Control-Request-Method');
78 0 ▶ return unless defined $method;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $method 0 0 0
79  
80 0 my $allow = join ", ", @allow;
81  
82 return $allow if grep {
83 0 ▶ if (not ref $_) { uc $method eq uc $_ }
Branch: 2 branches
BranchTrueFalseTotal
if grep {if (not ref $_) { uc $method eq uc $_; } else { die "Router 'cors_methods' param must be scalar\n"; } ;} @allow 0 0 0
if (not ref $_) { } 0 0 0
84 0 else { die "Router 'cors_methods' param must be scalar\n" }
85 } @allow;
86  
87 0 $app->log->debug("Reject CORS Method '$method'");
88  
89 0 return; # Fail
90 0 };
91  
92 # Check request headers
93 0 ▶ my $check_headers = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
94 0 my ($c, @allow) = @_;
95  
96 0 my $h = $c->req->headers;
97  
98 0 my @safe_headers = qw/
99 Cache-Control
100 Content-Language
101 Content-Type
102 Expires
103 Last-Modified
104 Pragma
105 /;
106  
107 0 my %safe_headers = map { lc $_ => 1 } @safe_headers;
108 0 my $allow = join ", ", @allow;
109  
110 0 my $headers = $h->header('Access-Control-Request-Headers');
111 0 ▶ my @headers = map { lc } grep { $_ } split /,\s*/ms, $headers || '';
Condition0%0/2
$headers || ''
l!l
00
MC/DC0%0/1
$headers || ''
$headers
Truth table0%0/2
A
A= $headers
Aresult
11
00
112  
113 0 ▶ return $allow unless @headers;
Branch: 1 branch
BranchTrueFalseTotal
unless @headers 0 0 0
114  
115 return $allow unless grep {
116 0 ▶ if (not ref $_) { not $safe_headers{ lc $_ } }
Branch: 2 branches
BranchTrueFalseTotal
unless grep {if (not ref $_) { not $safe_headers{lc $_}; } else { die "Router 'cors_headers' param must be scalar\n"; } ;} @allow 0 0 0
if (not ref $_) { } 0 0 0
117 0 else { die "Router 'cors_headers' param must be scalar\n" }
118 } @allow;
119  
120 0 $app->log->debug("Reject CORS Headers '$headers'");
121  
122 0 return; # Fail
123 0 };
124  
125 0 ▶ $app->hook(around_action => sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
126 0 my ($next, $c, $action, $last) = @_;
127  
128 # Only endpoints intrested
129 0 ▶ return $next->() unless $last;
Branch: 1 branch
BranchTrueFalseTotal
unless $last 0 0 0
130  
131 # Do not process preflight requests
132 0 ▶ return $next->() if $c->req->method eq 'OPTIONS';
Branch: 1 branch
BranchTrueFalseTotal
if $c->req->method eq "OPTIONS" 0 0 0
133  
134 0 my $params = $route_params->($c);
135  
136 # Do not process routes without cors_origin configured
137 0 ▶ my @params_origin = @{$params->{origin} //= []};
Condition0%0/2
$params->{"origin"} //= []
l!l
00
MC/DC0%0/1
$params->{"origin"} //= []
$params->{"origin"}
Truth table0%0/2
A
A= $params->{"origin"}
Aresult
11
00
138 0 ▶ return $next->() unless @params_origin;
Branch: 1 branch
BranchTrueFalseTotal
unless @params_origin 0 0 0
139  
140 0 my $h = $c->res->headers;
141 0 $h->append('Vary' => 'Origin');
142  
143 0 my $origin = $check_origin->($c, @params_origin);
144 0 ▶ return $next->() unless defined $origin;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $origin 0 0 0
145  
146 0 $h->header('Access-Control-Allow-Origin' => $origin);
147  
148 $h->header('Access-Control-Allow-Credentials' => 'true')
149 0 ▶ if $params->{credentials} //= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"credentials"} //= 0 0 0 0
Condition0%0/2
$params->{"credentials"} //= 0
l!l
00
MC/DC0%0/1
$params->{"credentials"} //= 0
$params->{"credentials"}
Truth table0%0/2
A
A= $params->{"credentials"}
Aresult
11
00
150  
151 0 ▶ my @params_expose = (@{$conf->{expose}}, @{$params->{expose} //= []});
Condition0%0/2
$params->{"expose"} //= []
l!l
00
MC/DC0%0/1
$params->{"expose"} //= []
$params->{"expose"}
Truth table0%0/2
A
A= $params->{"expose"}
Aresult
11
00
152 0 ▶ if (@params_expose) {
Branch: 1 branch
BranchTrueFalseTotal
if (@params_expose) 0 0 0
153 0 my $params_expose = join ", ", @params_expose;
154 0 $h->header('Access-Control-Expose-Headers' => $params_expose);
155 }
156  
157 0 $app->log->debug("Allow CORS Origin '$origin'");
158  
159 0 return $next->();
160 0 });
161  
162 # CORS Preflight
163 0 ▶ $app->routes->add_shortcut(cors => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
164 0 my ($r, @args) = @_;
165  
166 $r->options(@args)->to(
167 0 ▶ cb => sub {
Subroutine __ANON__: not called CC=15 SCAR=54.8
168 0 my ($c) = @_;
169  
170 0 ▶ return $c->render(status => 204, data => '')
Branch: 1 branch
BranchTrueFalseTotal
unless &$check_preflight($c) 0 0 0
171 unless $check_preflight->($c);
172  
173 0 my $params = $route_params->($c);
174  
175 0 ▶ my @params_origin = @{$params->{origin} //= []};
Condition0%0/2
$params->{"origin"} //= []
l!l
00
MC/DC0%0/1
$params->{"origin"} //= []
$params->{"origin"}
Truth table0%0/2
A
A= $params->{"origin"}
Aresult
11
00
176 0 ▶ return $c->render(status => 204, data => '')
Branch: 1 branch
BranchTrueFalseTotal
unless @params_origin 0 0 0
177 unless @params_origin;
178  
179 0 my $h = $c->res->headers;
180 0 $h->append('Vary' => 'Origin');
181  
182 0 my $origin = $check_origin->($c, @params_origin);
183 0 ▶ return $c->render(status => 204, data => '')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $origin 0 0 0
184 unless defined $origin;
185  
186 0 ▶ my @params_methods = @{$params->{methods} //= []};
Condition0%0/2
$params->{"methods"} //= []
l!l
00
MC/DC0%0/1
$params->{"methods"} //= []
$params->{"methods"}
Truth table0%0/2
A
A= $params->{"methods"}
Aresult
11
00
187 push @params_methods, 'HEAD'
188 0 if grep { uc $_ eq 'GET' } @params_methods
189 0 ▶ and not grep { uc $_ eq 'HEAD' } @params_methods;
Branch: 1 branch
BranchTrueFalseTotal
if grep {uc $_ eq "GET";} @params_methods and not grep({uc $_ eq "HEAD";} @params_methods) 0 0 0
Condition0%0/3
grep {uc $_ eq "GET";} @params_methods and not grep({uc $_ eq "HEAD";} @params_methods)
!ll&&!rl&&r
000
MC/DC0%0/2
grep {uc $_ eq "GET";} @params_methods and not grep({uc $_ eq "HEAD";} @params_methods)
grep {uc $_ eq "GET";} @params_methods not grep({uc $_ eq "HEAD";} @params_methods)
Truth table0%0/3
A and not(B)
A= grep {uc $_ eq "GET";} @params_methodsB= not grep({uc $_ eq "HEAD";} @params_methods)
ABresult
0X0
100
111
190 0 ▶ return $c->render(status => 204, data => '')
Branch: 1 branch
BranchTrueFalseTotal
unless @params_methods 0 0 0
191 unless @params_methods;
192  
193 0 my $methods = $check_methods->($c, @params_methods);
194 0 ▶ return $c->render(status => 204, data => '')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $methods 0 0 0
195 unless defined $methods;
196  
197 0 ▶ my @params_headers = @{$params->{headers} //= []};
Condition0%0/2
$params->{"headers"} //= []
l!l
00
MC/DC0%0/1
$params->{"headers"} //= []
$params->{"headers"}
Truth table0%0/2
A
A= $params->{"headers"}
Aresult
11
00
198  
199 0 my $headers = $check_headers->($c, @params_headers);
200 0 ▶ return $c->render(status => 204, data => '')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $headers 0 0 0
201 unless defined $headers;
202  
203 0 $h->header('Access-Control-Allow-Origin' => $origin);
204 0 $h->header('Access-Control-Allow-Methods' => $methods);
205  
206 0 ▶ $h->header('Access-Control-Allow-Headers' => $headers)
Branch: 1 branch
BranchTrueFalseTotal
if $headers 0 0 0
207 if $headers;
208  
209 $h->header('Access-Control-Allow-Credentials' => 'true')
210 0 ▶ if $params->{credentials} //= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"credentials"} //= 0 0 0 0
Condition0%0/2
$params->{"credentials"} //= 0
l!l
00
MC/DC0%0/1
$params->{"credentials"} //= 0
$params->{"credentials"}
Truth table0%0/2
A
A= $params->{"credentials"}
Aresult
11
00
211  
212 0 $h->header('Access-Control-Max-Age' => $conf->{max_age});
213  
214 0 $app->log->debug("Accept CORS '$origin' => '$methods'");
215 0 return $c->render(status => 204, data => '');
216 }
217 0 );
218 0 });
219 }
220  
221 1 1;
222  
223 __END__
↑ Summary