Skip to content

Commit

Permalink
Remove null 'Access-Control-Allow-Origin' if origin check has failed (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
prosalov authored and Seldaek committed Nov 6, 2019
1 parent db42ecb commit 1f20109
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion EventListener/CorsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ protected function getPreflightResponse(Request $request, array $options)
}

if (!$this->checkOrigin($request, $options)) {
$response->headers->set('Access-Control-Allow-Origin', 'null');
$response->headers->remove('Access-Control-Allow-Origin');

return $response;
}
Expand Down
23 changes: 23 additions & 0 deletions Tests/CorsListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,29 @@ public function testSameHostRequest()
$this->assertNull($event->getResponse());
}

public function testPreflightedRequestWithOriginButNo()
{
$options = array(
'allow_origin' => array(),
'allow_methods' => array('POST', 'PUT'),
);

$req = Request::create('/foo', 'OPTIONS');
$req->headers->set('Host', 'example.com');
$req->headers->set('Origin', 'http://evil.com');
$req->headers->set('Access-Control-Request-Method', 'POST');

$dispatcher = m::mock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
$dispatcher->shouldReceive('addListener')->times(0);

$event = new GetResponseEvent(m::mock('Symfony\Component\HttpKernel\HttpKernelInterface'), $req, HttpKernelInterface::MASTER_REQUEST);
$this->getListener($dispatcher, $options)->onKernelRequest($event);
$resp = $event->getResponse();
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $resp);
$this->assertEquals(200, $resp->getStatusCode());
$this->assertNull($resp->headers->get('Access-Control-Allow-Origin'));
}

public function testRequestWithOriginButNo()
{
// Request with same host as origin
Expand Down

0 comments on commit 1f20109

Please sign in to comment.