blib/lib/Mojolicious/Plugin/SecureCORS.pm

filter: Statementstmt 86.8% 99 / 114 Branchbran 68.9% 40 / 58 Conditioncond 63.6% 7 / 11 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 87.5% 14 / 16 Podpod 100% 1 / 1 totaltotal 79.6% 164 / 206 CC 36 SCAR 27.9
CC
36
Cov
80%
_preflight
37.6
_request
9.0
__ANON__
6.0
CRAP 16.3
↑ Summary
1 package Mojolicious::Plugin::SecureCORS;
2  
3 3 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 our $VERSION = 'v2.0.5';
7  
8 3 ▶use List::MoreUtils qw( any none );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use constant DEFAULT_MAX_AGE => 1800;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 1 ▶sub register {
Subroutine register: called CC=2 SCAR=6.9
Pod documented
14 1 my ($self, $app, $conf) = @_;
15 1 ▶ if (!exists $conf->{max_age}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $conf->{"max_age"}) 1 0 1
16 1 $conf->{max_age} = DEFAULT_MAX_AGE;
17 }
18  
19 1 my $root = $app->routes;
20  
21 1 ▶ $root->add_shortcut(under_strict_cors => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
22 1 my ($r, @args) = @_;
23 1 return $r->under(@args)->to(cb => \&_strict);
24 1 });
25  
26 1 ▶ $root->add_shortcut(cors => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
27 1 my ($r, @args) = @_;
28 return $r->any(@args)
29 ->methods('OPTIONS')
30 ->requires(
31 headers => {
32 'Origin' => qr/\S/ms,
33 'Access-Control-Request-Method' => qr/\S/ms,
34 },
35 )
36 1 ▶ ->to(cb => sub { _preflight($conf, @_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
37 1 });
38  
39 1 $app->hook(after_render => \&_request);
40  
41 1 return;
42 }
43  
44 4 ▶sub _strict {
Subroutine _strict: called CC=4 SCAR=13.9
45 4 my ($c) = @_;
46  
47 4 ▶ if (!defined $c->req->headers->origin) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $c->req->headers->origin) 2 2 4
48 2 return 1; # Not a CORS request, pass
49 }
50  
51 2 my $r = $c->match->endpoint;
52 2 ▶ while ($r) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r) 4 1 5
53 4 ▶ if ($r->to->{'cors.origin'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->to->{"cors.origin"}) 1 3 4
54 1 return 1; # Endpoint configured for CORS, pass
55 }
56 3 $r = $r->parent;
57 }
58 # Endpoint not configured for CORS, block
59 1 $c->render(status => 403, text => 'CORS Forbidden');
60 1 return;
61 }
62  
63 8 ▶sub _preflight {
Subroutine _preflight: called CC=18 SCAR=36.3
64 8 my ($conf, $c) = @_;
65  
66 8 my $method = $c->req->headers->header('Access-Control-Request-Method');
67 8 my $match;
68 # use options defined on this route, if available
69 8 ▶ if ($c->match->endpoint->to->{'cors.origin'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->match->endpoint->to->{'cors.origin'}) { } 0 8 8
70 0 $match = $c->match;
71 0 my $opt_methods = $match->endpoint->to->{'cors.methods'};
72 0 ▶ if ($opt_methods) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_methods) 0 0 0
73 0 my %good_methods = map {lc $_ => 1} split /,\s*/ms, $opt_methods;
74 0 ▶ if (!$good_methods{lc $method}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($good_methods{lc $method}) 0 0 0
75 0 return $c->render(status => 204, data => q{}); # Endpoint not found, ignore
76 }
77 }
78 }
79 # otherwise try to find route for actual request and use it options
80 else {
81 8 $match = Mojolicious::Routes::Match->new(root => $c->app->routes);
82 8 $match->find($c, {
83 method => $method,
84 path => $c->req->url->path,
85 });
86 8 ▶ if (!$match->endpoint) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($match->endpoint) 4 4 8
87 4 return $c->render(status => 204, data => q{}); # Endpoint not found, ignore
88 }
89 }
90  
91 4 my %opt = _get_opt($match->endpoint);
92  
93 4 ▶ if (!$opt{origin}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt{"origin"}) 0 4 4
94 0 return $c->render(status => 204, data => q{}); # Endpoint not configured for CORS, ignore
95 }
96  
97 4 my $h = $c->res->headers;
98 4 $h->append(Vary => 'Origin');
99  
100 4 my $origin = $c->req->headers->origin;
101 4 ▶ if (ref $opt{origin} eq 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opt{'origin'} eq 'Regexp') { } 4 0 4
102 4 ▶ if ($origin !~ /$opt{origin}/ms) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($origin =~ /$opt{"origin"}/msu) 1 3 4
103 1 return $c->render(status => 204, data => q{}); # Bad Origin:
104 }
105 } else {
106 0 ▶ if (none {$_ eq q{*} || $_ eq $origin} split q{ }, $opt{origin} // q{}) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 2 branches
BranchTrueFalseTotal
if (&none(sub { $_ eq "*" or $_ eq $origin; } , split(' ', $opt{"origin"} // "", 0))) 0 0 0
$_ eq "*" or $_ eq $origin 0 0 0
Condition0%0/2
$opt{"origin"} // ""
l!l
00
MC/DC0%0/1
$opt{"origin"} // ""
$opt{"origin"}
Truth table0%0/2
A
A= $opt{"origin"}
Aresult
11
00
107 0 return $c->render(status => 204, data => q{}); # Bad Origin:
108 }
109 }
110  
111 3 my $headers = $c->req->headers->header('Access-Control-Request-Headers');
112 3 ▶ my @want_headers = map {lc} split /,\s*/ms, $headers // q{};
Condition100%2/2
$headers // ""
l!l
21
MC/DC100%1/1
$headers // ""
$headers
Truth table100%2/2
A
A= $headers
Aresult
11
00
113 3 ▶ if (ref $opt{headers} eq 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opt{'headers'} eq 'Regexp') { } 0 3 3
114 0 ▶ if (any {!/$opt{headers}/ms} @want_headers) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { not /$opt{"headers"}/msu; } , @want_headers)) 0 0 0
115 0 return $c->render(status => 204, data => q{}); # Bad Access-Control-Request-Headers:
116 }
117 } else {
118 3 ▶ my %good_headers = map {lc $_ => 1} split /,\s*/ms, $opt{headers} // q{};
Condition50%1/2
$opt{"headers"} // ""
l!l
30
MC/DC0%0/1
$opt{"headers"} // ""
$opt{"headers"}
Truth table50%1/2
A
A= $opt{"headers"}
Aresult
11
00
119 3 ▶ if (any {!exists $good_headers{$_}} @want_headers) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { not exists $good_headers{$_}; } , @want_headers)) 1 2 3
120 1 return $c->render(status => 204, data => q{}); # Bad Access-Control-Request-Headers:
121 }
122 }
123  
124 2 $h->header('Access-Control-Allow-Origin' => $origin);
125 2 $h->header('Access-Control-Allow-Methods' => $method);
126 2 ▶ if (defined $headers) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $headers) 1 1 2
127 1 $h->header('Access-Control-Allow-Headers' => $headers);
128 }
129 2 ▶ if ($opt{credentials}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"credentials"}) 2 0 2
130 2 $h->header('Access-Control-Allow-Credentials' => 'true');
131 }
132 2 ▶ if (defined $conf->{max_age}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf->{"max_age"}) 2 0 2
133 2 $h->header('Access-Control-Max-Age' => $conf->{max_age});
134 }
135 2 return $c->render(status => 204, data => q{});
136 }
137  
138 24 ▶sub _request {
Subroutine _request: called CC=9 SCAR=22.0
139 24 my ($c, $output, $format) = @_;
140  
141 24 my %opt = _get_opt($c->match->endpoint);
142  
143 24 ▶ if (!$opt{origin}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt{"origin"}) 16 8 24
144 16 return; # Endpoint not configured for CORS, ignore
145 }
146  
147 8 my $h = $c->res->headers;
148 8 $h->append(Vary => 'Origin');
149  
150 8 my $origin = $c->req->headers->origin;
151 8 ▶ if (!defined $origin) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $origin) 2 6 8
152 2 return; # Not a CORS
153 }
154  
155 6 ▶ if (ref $opt{origin} eq 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opt{'origin'} eq 'Regexp') { } 2 4 6
156 2 ▶ if ($origin !~ /$opt{origin}/ms) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($origin =~ /$opt{"origin"}/msu) 1 1 2
157 1 return; # Bad Origin:
158 }
159 } else {
160 4 ▶ if (none {$_ eq q{*} || $_ eq $origin} split q{ }, $opt{origin} // q{}) {
Subroutine __ANON__: called CC=2 SCAR=7.0
Branch: 2 branches
BranchTrueFalseTotal
if (&none(sub { $_ eq "*" or $_ eq $origin; } , split(' ', $opt{"origin"} // "", 0))) 1 3 4
$_ eq "*" or $_ eq $origin 1 5 6
Condition50%1/2
$opt{"origin"} // ""
l!l
40
MC/DC0%0/1
$opt{"origin"} // ""
$opt{"origin"}
Truth table50%1/2
A
A= $opt{"origin"}
Aresult
11
00
161 1 return; # Bad Origin:
162 }
163 }
164  
165 4 $h->header('Access-Control-Allow-Origin' => $origin);
166 4 ▶ if ($opt{credentials}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"credentials"}) 1 3 4
167 1 $h->header('Access-Control-Allow-Credentials' => 'true');
168 }
169 4 ▶ if ($opt{expose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"expose"}) 0 4 4
170 0 $h->header('Access-Control-Expose-Headers' => $opt{expose});
171 }
172 4 return;
173 }
174  
175 28 ▶sub _get_opt {
Subroutine _get_opt: called CC=5 SCAR=16.1
176 28 my ($r) = @_;
177 28 my %opt;
178 28 ▶ while ($r) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r) 130 28 158
179 130 for my $name (qw( origin credentials expose headers )) {
180 520 ▶ if (!exists $opt{$name} && exists $r->to->{"cors.$name"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $opt{$name} and exists $r->to->{"cors.$name"}) 50 470 520
Condition100%3/3
not exists $opt{$name} and exists $r->to->{"cors.$name"}
!ll&&!rl&&r
13433650
MC/DC100%2/2
not exists $opt{$name} and exists $r->to->{"cors.$name"}
not exists $opt{$name} exists $r->to->{"cors.$name"}
Truth table100%3/3
not(A) and B
A= not exists $opt{$name}B= exists $r->to->{"cors.$name"}
ABresult
0X0
100
111
181 50 $opt{$name} = $r->to->{"cors.$name"};
182 }
183 }
184 130 $r = $r->parent;
185 }
186 28 return %opt;
187 }
188  
189  
190 3 1; # Magic true value required at end of module
191 __END__
↑ Summary