Skip to content

Commit

Permalink
Fix allowing regular requests with "openapi_cors_allowed_origins", cl…
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Henning Thorsen committed Jan 26, 2019
1 parent 31eb14d commit 586d11c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
Revision history for perl distribution Mojolicious-Plugin-OpenAPI

2.11 Not Released
- Fix allowing regular requests with "openapi_cors_allowed_origins" #103

2.10 2019-01-25T12:49:55+0900
- Add "plugins" as a documented feature for register()
- Add Mojolicious::Plugin::OpenAPI::SpecRenderer
Expand Down
3 changes: 2 additions & 1 deletion lib/Mojolicious/Plugin/OpenAPI/Cors.pm
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ sub _exchange {

# Not a CORS request
unless (defined $c->req->headers->origin) {
my $method = $c->req->method;
_render_bad_request($c, 'OPTIONS is only for preflighted CORS requests.')
if $c->match->endpoint->to->{'openapi.cors_preflighted'};
if $method eq 'OPTIONS' and $c->match->endpoint->to->{'openapi.cors_preflighted'};
return $c;
}

Expand Down
9 changes: 9 additions & 0 deletions t/cors.t
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ $t->options_ok('/api/user')->status_is(400)
$t->put_ok('/api/user', {'Origin' => 'http://bar.example'})->status_is(200)
->header_is('Access-Control-Allow-Origin' => 'http://bar.example')->json_has('/created');

$t->get_ok('/api/user')->status_is(200)->header_is('Access-Control-Allow-Origin' => undef)
->json_is('/origin', undef);

$t->put_ok('/api/user')->status_is(200)->header_is('Access-Control-Allow-Origin' => undef)
->json_has('/created');

$t->put_ok('/api/headers')->status_is(200)->header_is('Access-Control-Allow-Origin' => undef)
->json_is('/h' => 42);

note 'Using the spec';
$t->options_ok('/api/headers')->status_is(400)->json_is('/errors/0/path' => '/Origin');
$t->put_ok('/api/headers', {'Origin' => 'https://foo.example'})->status_is(400)
Expand Down

0 comments on commit 586d11c

Please sign in to comment.