| 1 | package PAGI::Middleware::CORS; | ||||||||||||||||||||||||||||
| 2 | 2 | $PAGI::Middleware::CORS::VERSION = '0.002002'; | |||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use parent 'PAGI::Middleware'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | PAGI::Middleware::CORS - Cross-Origin Resource Sharing middleware | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | use PAGI::Middleware::Builder; | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | my $app = builder { | ||||||||||||||||||||||||||||
| 17 | enable 'CORS', | ||||||||||||||||||||||||||||
| 18 | origins => ['https://example.com', 'https://app.example.com'], | ||||||||||||||||||||||||||||
| 19 | methods => ['GET', 'POST', 'PUT', 'DELETE'], | ||||||||||||||||||||||||||||
| 20 | headers => ['Content-Type', 'Authorization'], | ||||||||||||||||||||||||||||
| 21 | credentials => 1, | ||||||||||||||||||||||||||||
| 22 | max_age => 86400; | ||||||||||||||||||||||||||||
| 23 | $my_app; | ||||||||||||||||||||||||||||
| 24 | }; | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | PAGI::Middleware::CORS implements Cross-Origin Resource Sharing (CORS) | ||||||||||||||||||||||||||||
| 29 | for PAGI applications. It handles preflight OPTIONS requests and adds | ||||||||||||||||||||||||||||
| 30 | the appropriate CORS headers to responses. | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =over 4 | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =item * origins (default: ['*']) | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | Array of allowed origins, or ['*'] for any origin. | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =item * methods (default: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS']) | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | Array of allowed HTTP methods. | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =item * headers (default: ['Content-Type', 'Authorization', 'X-Requested-With']) | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | Array of allowed request headers. | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =item * expose_headers (default: []) | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | Array of headers to expose to the client. | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =item * credentials (default: 0) | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | If true, allow credentials (cookies, auth headers). | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =item * max_age (default: 86400) | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | Max age for preflight cache in seconds. | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =back | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =cut | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 7 | ▶ | sub _init { | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 65 | 7 | my ($self, $config) = @_; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 7 | ▶ | $self->{origins} = $config->{origins} // ['*']; | ||||||||||||||||||||||||||
|
Condition100%2/2
$config->{"origins"} // ["*"]
MC/DC100%1/1
$config->{"origins"} // ["*"]
$config->{"origins"}
Truth table100%2/2
A
A= $config->{"origins"}
| |||||||||||||||||||||||||||||
| 68 | 7 | ▶ | $self->{methods} = $config->{methods} // [qw(GET POST PUT DELETE PATCH OPTIONS)]; | ||||||||||||||||||||||||||
|
Condition100%2/2
$config->{"methods"} // ["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"]
MC/DC100%1/1
$config->{"methods"} // ["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"]
$config->{"methods"}
Truth table100%2/2
A
A= $config->{"methods"}
| |||||||||||||||||||||||||||||
| 69 | 7 | ▶ | $self->{headers} = $config->{headers} // [qw(Content-Type Authorization X-Requested-With)]; | ||||||||||||||||||||||||||
|
Condition100%2/2
$config->{"headers"} // ["Content-Type", "Authorization", "X-Requested-With"]
MC/DC100%1/1
$config->{"headers"} // ["Content-Type", "Authorization", "X-Requested-With"]
$config->{"headers"}
Truth table100%2/2
A
A= $config->{"headers"}
| |||||||||||||||||||||||||||||
| 70 | 7 | ▶ | $self->{expose_headers} = $config->{expose_headers} // []; | ||||||||||||||||||||||||||
|
Condition50%1/2
$config->{"expose_headers"} // []
MC/DC0%0/1
$config->{"expose_headers"} // []
$config->{"expose_headers"}
Truth table50%1/2
A
A= $config->{"expose_headers"}
| |||||||||||||||||||||||||||||
| 71 | 7 | ▶ | $self->{credentials} = $config->{credentials} // 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$config->{"credentials"} // 0
MC/DC100%1/1
$config->{"credentials"} // 0
$config->{"credentials"}
Truth table100%2/2
A
A= $config->{"credentials"}
| |||||||||||||||||||||||||||||
| 72 | 7 | ▶ | $self->{max_age} = $config->{max_age} // 86400; | ||||||||||||||||||||||||||
|
Condition50%1/2
$config->{"max_age"} // 86400
MC/DC0%0/1
$config->{"max_age"} // 86400
$config->{"max_age"}
Truth table50%1/2
A
A= $config->{"max_age"}
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 7 | ▶ | if ($self->{credentials} && grep { $_ eq '*' } @{$self->{origins}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"credentials"} and grep {$_ eq "*";} @{$$self{"origins"};}
MC/DC100%2/2
$self->{"credentials"} and grep {$_ eq "*";} @{$$self{"origins"};}
$self->{"credentials"}
grep {$_ eq "*";} @{$$self{"origins"};}
Truth table100%3/3
A and B
A= $self->{"credentials"}B= grep {$_ eq "*";} @{$$self{"origins"};}
| |||||||||||||||||||||||||||||
| 75 | 1 | warn "PAGI::Middleware::CORS: wildcard origins ('*') with credentials " | |||||||||||||||||||||||||||
| 76 | . "enabled reflects any Origin with Access-Control-Allow-Credentials. " | ||||||||||||||||||||||||||||
| 77 | . "This allows any website to make credentialed cross-origin requests. " | ||||||||||||||||||||||||||||
| 78 | . "Consider specifying explicit origins.\n"; | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 3 | ▶ | sub wrap { | ||||||||||||||||||||||||||
|
Subroutine
wrap: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 83 | 3 | my ($self, $app) = @_; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 3 | ▶ | return async sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=18.1
| |||||||||||||||||||||||||||||
| 86 | 3 | my ($scope, $receive, $send) = @_; | |||||||||||||||||||||||||||
| 87 | # Only handle HTTP requests | ||||||||||||||||||||||||||||
| 88 | 3 | ▶ | if ($scope->{type} ne 'http') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 0 | await $app->($scope, $receive, $send); | |||||||||||||||||||||||||||
| 90 | 0 | return; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # Get Origin header | ||||||||||||||||||||||||||||
| 94 | 3 | my $origin = $self->_get_header($scope, 'origin'); | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # Check if this is a preflight request | ||||||||||||||||||||||||||||
| 97 | 3 | ▶ | if ($scope->{method} eq 'OPTIONS' && $origin) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$scope->{"method"} eq "OPTIONS" and $origin
MC/DC50%1/2
$scope->{"method"} eq "OPTIONS" and $origin
$scope->{"method"} eq "OPTIONS"
$origin
Truth table66%2/3
A and B
A= $scope->{"method"} eq "OPTIONS"B= $origin
| |||||||||||||||||||||||||||||
| 98 | 1 | await $self->_handle_preflight($scope, $send, $origin); | |||||||||||||||||||||||||||
| 99 | 1 | return; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | # For actual requests, add CORS headers to response | ||||||||||||||||||||||||||||
| 103 | 2 | ▶ | if ($origin && $self->_is_origin_allowed($origin)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$origin and $self->_is_origin_allowed($origin)
MC/DC50%1/2
$origin and $self->_is_origin_allowed($origin)
$origin
$self->_is_origin_allowed($origin)
Truth table66%2/3
A and B
A= $originB= $self->_is_origin_allowed($origin)
| |||||||||||||||||||||||||||||
| 104 | 2 | ▶ | my $wrapped_send = async sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 105 | 2 | my ($event) = @_; | |||||||||||||||||||||||||||
| 106 | 2 | ▶ | if ($event->{type} eq 'http.response.start') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 1 | $self->_add_cors_headers($event->{headers}, $origin); | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | 2 | await $send->($event); | |||||||||||||||||||||||||||
| 110 | 1 | }; | |||||||||||||||||||||||||||
| 111 | 1 | await $app->($scope, $receive, $wrapped_send); | |||||||||||||||||||||||||||
| 112 | } else { | ||||||||||||||||||||||||||||
| 113 | 1 | await $app->($scope, $receive, $send); | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | 3 | }; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 1 | ▶ | async sub _handle_preflight { | ||||||||||||||||||||||||||
|
Subroutine
_handle_preflight: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 119 | 1 | my ($self, $scope, $send, $origin) = @_; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 1 | my @headers; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 1 | ▶ | if ($self->_is_origin_allowed($origin)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 1 | $self->_add_cors_headers(\@headers, $origin); | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | # Add preflight-specific headers | ||||||||||||||||||||||||||||
| 127 | 1 | push @headers, ['Access-Control-Allow-Methods', join(', ', @{$self->{methods}})]; | |||||||||||||||||||||||||||
| 128 | 1 | push @headers, ['Access-Control-Allow-Headers', join(', ', @{$self->{headers}})]; | |||||||||||||||||||||||||||
| 129 | 1 | push @headers, ['Access-Control-Max-Age', $self->{max_age}]; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | await $send->({ | ||||||||||||||||||||||||||||
| 133 | type => 'http.response.start', | ||||||||||||||||||||||||||||
| 134 | status => 204, | ||||||||||||||||||||||||||||
| 135 | headers => \@headers, | ||||||||||||||||||||||||||||
| 136 | 1 | }); | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 1 | await $send->({ | |||||||||||||||||||||||||||
| 139 | type => 'http.response.body', | ||||||||||||||||||||||||||||
| 140 | body => '', | ||||||||||||||||||||||||||||
| 141 | more => 0, | ||||||||||||||||||||||||||||
| 142 | }); | ||||||||||||||||||||||||||||
| 143 | 2 | } | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 2 | ▶ | sub _add_cors_headers { | ||||||||||||||||||||||||||
|
Subroutine
_add_cors_headers: called CC=5 SCAR=17.4
| |||||||||||||||||||||||||||||
| 146 | 2 | my ($self, $headers, $origin) = @_; | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | # Determine origin to return | ||||||||||||||||||||||||||||
| 149 | 2 | my $allowed_origin; | |||||||||||||||||||||||||||
| 150 | 2 | ▶ | if (grep { $_ eq '*' } @{$self->{origins}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 0 | ▶ | $allowed_origin = $self->{credentials} ? $origin : '*'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | } else { | ||||||||||||||||||||||||||||
| 153 | 2 | $allowed_origin = $origin; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 2 | push @$headers, ['Access-Control-Allow-Origin', $allowed_origin]; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 2 | ▶ | if ($self->{credentials}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 1 | push @$headers, ['Access-Control-Allow-Credentials', 'true']; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 2 | ▶ | if (@{$self->{expose_headers}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 0 | push @$headers, ['Access-Control-Expose-Headers', join(', ', @{$self->{expose_headers}})]; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | # Vary header for caching | ||||||||||||||||||||||||||||
| 167 | 2 | push @$headers, ['Vary', 'Origin']; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 3 | ▶ | sub _is_origin_allowed { | ||||||||||||||||||||||||||
|
Subroutine
_is_origin_allowed: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 171 | 3 | my ($self, $origin) = @_; | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 3 | ▶ | return 1 if grep { $_ eq '*' } @{$self->{origins}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 3 | ▶ | return 1 if grep { $_ eq $origin } @{$self->{origins}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | 1 | return 0; | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 3 | ▶ | sub _get_header { | ||||||||||||||||||||||||||
|
Subroutine
_get_header: called CC=4 SCAR=14.9
| |||||||||||||||||||||||||||||
| 179 | 3 | my ($self, $scope, $name) = @_; | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 3 | $name = lc($name); | |||||||||||||||||||||||||||
| 182 | 3 | ▶ | for my $h (@{$scope->{headers} // []}) { | ||||||||||||||||||||||||||
|
Condition50%1/2
$scope->{"headers"} // []
MC/DC0%0/1
$scope->{"headers"} // []
$scope->{"headers"}
Truth table50%1/2
A
A= $scope->{"headers"}
| |||||||||||||||||||||||||||||
| 183 | 3 | ▶ | return $h->[1] if lc($h->[0]) eq $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | 0 | return; | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 2 | 1; | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | __END__ | ||||||||||||||||||||||||||||