blib/lib/Plack/Middleware/CrossOrigin.pm

filter: Statementstmt 100% 117 / 117 Branchbran 100% 42 / 42 Conditioncond 77.7% 28 / 36 MC/DCmcdc 45.0% 9 / 20 Subroutinesub 100% 14 / 14 Podpod 100% 2 / 2 totaltotal 91.7% 212 / 231 CC 34 SCAR 26.0
CC
34
Cov
96%
call
23.0
prepare_app
9.2
__ANON__
3.0
CRAP 13.5
↑ Summary
1 package Plack::Middleware::CrossOrigin;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = '0.014';
6 1 $VERSION =~ tr/_//d;
7  
8 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use parent qw(Plack::Middleware);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Plack::Util::Accessor qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
13 origins
14 headers
15 methods
16 max_age
17 expose_headers
18 credentials
19 continue_on_failure
20 1 );
21  
22 1 my @simple_headers = qw(
23 Accept
24 Accept-Language
25 Content-Language
26 );
27 1 my @simple_response_headers = qw(
28 Cache-Control
29 Content-Language
30 Content-Type
31 Expires
32 Last-Modified
33 Pragma
34 );
35 1 my @common_headers = qw(
36 Cache-Control
37 Depth
38 If-Modified-Since
39 User-Agent
40 X-File-Name
41 X-File-Size
42 X-Requested-With
43 X-Prototype-Version
44 );
45  
46 # RFC 7231
47 1 my @http_methods = qw(
48 GET
49 HEAD
50 POST
51 PUT
52 DELETE
53 CONNECT
54 OPTIONS
55 TRACE
56 );
57  
58 # RFC 5789
59 1 my @rfc_5789_methods = qw(
60 PATCH
61 );
62  
63 1 my @webdav_methods = qw(
64 CANCELUPLOAD
65 CHECKIN
66 CHECKOUT
67 COPY
68 DELETE
69 GETLIB
70 LOCK
71 MKCOL
72 MOVE
73 OPTIONS
74 PROPFIND
75 PROPPATCH
76 PUT
77 REPORT
78 UNCHECKOUT
79 UNLOCK
80 UPDATE
81 VERSION-CONTROL
82 );
83  
84 1 my @all_methods = ( @http_methods, @rfc_5789_methods, @webdav_methods );
85  
86 8 ▶sub prepare_app {
Subroutine prepare_app: called CC=9 SCAR=22.2
Pod documented
87 8 my ($self) = @_;
88  
89 8 ▶ $self->origins([$self->origins || ()])
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->origins 5 3 8
Condition33%1/3
$self->origins || ()
l!l&&r!l&&!r
500
MC/DC0%0/2
$self->origins || ()
$self->origins ()
Truth table33%1/3
A || B
A= $self->originsB= ()
ABresult
1X1
011
000
90 unless ref $self->origins;
91  
92 8 ▶ $self->methods([$self->methods || @all_methods])
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->methods 6 2 8
Condition66%2/3
$self->methods || @all_methods
l!l&&r!l&&!r
240
MC/DC0%0/2
$self->methods || @all_methods
$self->methods @all_methods
Truth table66%2/3
A || B
A= $self->methodsB= @all_methods
ABresult
1X1
011
000
93 unless ref $self->methods;
94  
95 8 ▶ $self->headers([$self->headers || @common_headers])
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->headers 6 2 8
Condition66%2/3
$self->headers || @common_headers
l!l&&r!l&&!r
150
MC/DC0%0/2
$self->headers || @common_headers
$self->headers @common_headers
Truth table66%2/3
A || B
A= $self->headersB= @common_headers
ABresult
1X1
011
000
96 unless ref $self->headers;
97  
98 8 ▶ $self->expose_headers([$self->expose_headers || ()])
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->expose_headers 7 1 8
Condition66%2/3
$self->expose_headers || ()
l!l&&r!l&&!r
250
MC/DC0%0/2
$self->expose_headers || ()
$self->expose_headers ()
Truth table66%2/3
A || B
A= $self->expose_headersB= ()
ABresult
1X1
011
000
99 unless ref $self->expose_headers;
100  
101 8 $self->{origins_h} = { map { $_ => 1 } @{ $self->origins } };
102 ($self->{origins_re}) =
103 map qr/\A(?:$_)\z/,
104 join '|',
105 map +(
106 join '[a-z0-9.-]*',
107 map quotemeta,
108 split /\*/, $_, -1
109 ),
110 8 @{ $self->origins };
111  
112 8 $self->{methods_h} = { map { $_ => 1 } @{ $self->methods } };
113 8 $self->{headers_h} = { map { lc $_ => 1 } @{ $self->headers } };
114 8 $self->{expose_headers_h} = { map { $_ => 1 } @{ $self->expose_headers } };
115 }
116  
117 25 ▶sub call {
Subroutine call: called CC=23 SCAR=31.4
Pod documented
118 25 my ($self, $env) = @_;
119 25 my $origin = $env->{HTTP_ORIGIN};
120 25 my $continue_on_failure;
121 25 ▶ if ($origin) {
Branch: 2 branches
BranchTrueFalseTotal
if ($origin) { } 18 7 25
elsif ($env->{'REQUEST_METHOD'} eq 'GET' and $env->{'HTTP_USER_AGENT'} and $env->{'HTTP_USER_AGENT'} =~ m[\bAppleWebKit/(\d+\.\d+)] and $1 < 534.19 and $env->{'HTTP_REFERER'} and $env->{'HTTP_REFERER'} =~ m[\A ( \w+://[^/]+ )]msx) { } 3 4 7
122 18 $continue_on_failure = $self->continue_on_failure;
123 }
124 # for preflighted GET requests, some WebKit versions don't
125 # include Origin with the actual request. Fixed in current versions
126 # of WebKit, Chrome, and Safari.
127 # Work around it using the Referer header.
128 # https://bugs.webkit.org/show_bug.cgi?id=50773
129 # http://code.google.com/p/chromium/issues/detail?id=57836
130 ▶ elsif ($env->{REQUEST_METHOD} eq 'GET'
Condition80%12/15
$env->{"REQUEST_METHOD"} eq "GET" and $env->{"HTTP_USER_AGENT"} and $env->{"HTTP_USER_AGENT"} =~ m[\bAppleWebKit/(\d+\.\d+)] and $1 < "534.19" and $env->{"HTTP_REFERER"} and $env->{"HTTP_REFERER"} =~ m[\A ( \w+://[^/]+ )]msx
!ll&&!rl&&r
403
$env->{"REQUEST_METHOD"} eq "GET" and $env->{"HTTP_USER_AGENT"} and $env->{"HTTP_USER_AGENT"} =~ m[\bAppleWebKit/(\d+\.\d+)] and $1 < "534.19" and $env->{"HTTP_REFERER"}
!ll&&!rl&&r
313
$env->{"REQUEST_METHOD"} eq "GET" and $env->{"HTTP_USER_AGENT"} and $env->{"HTTP_USER_AGENT"} =~ m[\bAppleWebKit/(\d+\.\d+)] and $1 < "534.19"
!ll&&!rl&&r
124
$env->{"REQUEST_METHOD"} eq "GET" and $env->{"HTTP_USER_AGENT"} and $env->{"HTTP_USER_AGENT"} =~ m[\bAppleWebKit/(\d+\.\d+)]
!ll&&!rl&&r
106
$env->{"REQUEST_METHOD"} eq "GET" and $env->{"HTTP_USER_AGENT"}
!ll&&!rl&&r
016
MC/DC50%3/6
$env->{"REQUEST_METHOD"} eq "GET" and $env->{"HTTP_USER_AGENT"} and $env->{"HTTP_USER_AGENT"} =~ m[\bAppleWebKit/(\d+\.\d+)] and $1 < "534.19" and $env->{"HTTP_REFERER"} and $env->{"HTTP_REFERER"} =~ m[\A ( \w+://[^/]+ )]msx
$env->{"REQUEST_METHOD"} eq "GET" $env->{"HTTP_USER_AGENT"} $env->{"HTTP_USER_AGENT"} =~ m[\bAppleWebKit/(\d+\.\d+)] $1 < "534.19" $env->{"HTTP_REFERER"} $env->{"HTTP_REFERER"} =~ m[\A ( \w+://[^/]+ )]msx
Truth table57%4/7
A and B and C and D and E and F
A= $env->{"REQUEST_METHOD"} eq "GET"B= $env->{"HTTP_USER_AGENT"}C= $env->{"HTTP_USER_AGENT"} =~ m[\bAppleWebKit/(\d+\.\d+)]D= $1 < "534.19"E= $env->{"HTTP_REFERER"}F= $env->{"HTTP_REFERER"} =~ m[\A ( \w+://[^/]+ )]msx
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
131 && $env->{HTTP_USER_AGENT}
132 && $env->{HTTP_USER_AGENT} =~ m{\bAppleWebKit/(\d+\.\d+)}
133 && $1 < 534.19
134 && $env->{HTTP_REFERER}
135 && $env->{HTTP_REFERER} =~ m{\A ( \w+://[^/]+ )}msx
136 ) {
137 3 $origin = $1;
138 3 $continue_on_failure = 1;
139 }
140 else {
141 4 return _with_vary($self->app->($env));
142 }
143  
144 21 my $request_method = $env->{HTTP_ACCESS_CONTROL_REQUEST_METHOD};
145 21 my $request_headers = $env->{HTTP_ACCESS_CONTROL_REQUEST_HEADERS};
146 21 ▶ my @request_headers = $request_headers ? (split /,\s*/, $request_headers) : ();
Branch: 1 branch
BranchTrueFalseTotal
$request_headers ? : 3 18 21
147 21 ▶ my $preflight = $env->{REQUEST_METHOD} eq 'OPTIONS' && $request_method;
Condition100%3/3
$env->{'REQUEST_METHOD'} eq 'OPTIONS' && $request_method
!ll&&!rl&&r
1119
MC/DC100%2/2
$env->{'REQUEST_METHOD'} eq 'OPTIONS' && $request_method
$env->{'REQUEST_METHOD'} eq 'OPTIONS' $request_method
Truth table100%3/3
A && B
A= $env->{'REQUEST_METHOD'} eq 'OPTIONS'B= $request_method
ABresult
0X0
100
111
148  
149 21 ▶ my $fail = $continue_on_failure && !$preflight ? $self->app : \&_response_forbidden;
Branch: 1 branch
BranchTrueFalseTotal
$continue_on_failure && !$preflight ? : 4 17 21
Condition100%3/3
$continue_on_failure and not $preflight
!ll&&!rl&&r
1614
MC/DC100%2/2
$continue_on_failure and not $preflight
$continue_on_failure not $preflight
Truth table100%3/3
A and not(B)
A= $continue_on_failureB= not $preflight
ABresult
0X0
100
111
150  
151 21 my $allowed_origins_h = $self->{origins_h};
152 21 my $allowed_methods = $self->methods;
153 21 my $allowed_methods_h = $self->{methods_h};
154 21 my $allowed_headers = $self->headers;
155 21 my $allowed_headers_h = $self->{headers_h};
156 21 my $expose_headers = $self->expose_headers;
157 21 my $expose_headers_h = $self->{expose_headers_h};
158  
159 21 my @headers;
160  
161 21 ▶ if (not ($allowed_origins_h->{'*'} || $origin =~ $self->{origins_re} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($allowed_origins_h->{"*"} or $origin =~ /$self->{"origins_re"}/) 6 15 21
Condition100%3/3
$allowed_origins_h->{"*"} or $origin =~ /$self->{"origins_re"}/
l!l&&r!l&&!r
5106
MC/DC100%2/2
$allowed_origins_h->{"*"} or $origin =~ /$self->{"origins_re"}/
$allowed_origins_h->{"*"} $origin =~ /$self->{"origins_re"}/
Truth table100%3/3
A or B
A= $allowed_origins_h->{"*"}B= $origin =~ /$self->{"origins_re"}/
ABresult
1X1
011
000
162 6 return _with_vary($fail->($env));
163 }
164  
165 15 ▶ if ($preflight) {
Branch: 1 branch
BranchTrueFalseTotal
if ($preflight) 7 8 15
166 7 ▶ if ( $allowed_methods_h->{'*'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($allowed_methods_h->{'*'}) { } 3 4 7
elsif (not $allowed_methods_h->{$request_method}) { } 1 3 4
167 3 $allowed_methods = [$request_method];
168 }
169 elsif ( ! $allowed_methods_h->{$request_method} ) {
170 1 return _response_forbidden();
171 }
172 6 ▶ if ( $allowed_headers_h->{'*'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($allowed_headers_h->{'*'}) { } 2 4 6
elsif (grep {not defined $_;} @{$allowed_headers_h;}{map lc($_), @request_headers}) { } 1 3 4
173 2 $allowed_headers = \@request_headers;
174 }
175 2 elsif ( grep { ! defined } @{$allowed_headers_h}{map lc, @request_headers} ) {
176 1 return _response_forbidden();
177 }
178 }
179 13 ▶ if ($self->credentials) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->credentials) { } 1 12 13
elsif ($allowed_origins_h->{'*'}) { } 4 8 12
180 1 push @headers, 'Access-Control-Allow-Credentials' => 'true';
181 }
182 elsif ($allowed_origins_h->{'*'}) {
183 4 $origin = '*';
184 }
185 13 push @headers, 'Access-Control-Allow-Origin' => $origin;
186  
187 13 my $res;
188 13 ▶ if ($preflight) {
Branch: 1 branch
BranchTrueFalseTotal
if ($preflight) { } 5 8 13
189 5 ▶ if (defined $self->max_age) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->max_age) 3 2 5
190 3 push @headers, 'Access-Control-Max-Age' => $self->max_age;
191 }
192 5 push @headers, 'Access-Control-Allow-Methods' => join ', ', @$allowed_methods;
193 5 push @headers, 'Access-Control-Allow-Headers' => join ', ', @$allowed_headers;
194  
195 5 $res = _response_success();
196 }
197 else {
198 8 $res = $self->app->($env);
199 }
200  
201 13 ▶ return $self->response_cb($res, sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
202 13 my $res = shift;
203  
204 13 ▶ if (! _vary_headers($res->[1])->{origin}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_vary_headers($res->[1])->{"origin"}) 12 1 13
205 12 push @{ $res->[1] }, 'Vary' => 'Origin';
206 }
207  
208 13 ▶ if ($expose_headers_h->{'*'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expose_headers_h->{"*"}) 3 10 13
209 3 my %headers = @{ $res->[1] };
210 3 delete @headers{@simple_response_headers};
211 3 $expose_headers = [sort keys %headers];
212 }
213  
214 13 push @headers, 'Access-Control-Expose-Headers' => join ', ', @$expose_headers;
215  
216 13 push @{ $res->[1] }, @headers;
217 13 });
218 }
219  
220 6 ▶sub _response_forbidden {
Subroutine _response_forbidden: called CC=1 SCAR=0.0
221 6 [403, ['Content-Type' => 'text/plain', 'Content-Length' => 9, 'Vary' => 'Origin'], ['forbidden']];
222 }
223  
224 5 ▶sub _response_success {
Subroutine _response_success: called CC=1 SCAR=0.0
225 5 [200, [ 'Content-Type' => 'text/plain' ], [] ];
226 }
227  
228 10 ▶sub _with_vary {
Subroutine _with_vary: called CC=1 SCAR=0.0
229 10 my ($res) = @_;
230 10 ▶ return Plack::Util::response_cb($res, sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
231 10 my $res = shift;
232  
233 10 ▶ if (! _vary_headers($res->[1])->{origin}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not _vary_headers($res->[1])->{"origin"}) 6 4 10
234 6 push @{ $res->[1] }, 'Vary' => 'Origin';
235 }
236 10 });
237 }
238  
239 23 ▶sub _vary_headers {
Subroutine _vary_headers: called CC=1 SCAR=0.0
240 23 my ($headers) = @_;
241  
242 my %vary =
243 23 map { s/\A\s+//; s/\s+\z//; ( lc, 1) }
244 map +(split /,/),
245 Plack::Util::header_get($headers, 'Vary');
246  
247 23 return \%vary;
248 }
249  
250 1 1;
251 __END__
↑ Summary