Skip to content

Commit

Permalink
Merge branch 'hotfix/47'
Browse files Browse the repository at this point in the history
Close #47
Fixes #48
  • Loading branch information
weierophinney committed Jun 1, 2017
2 parents 613263f + 779a9e4 commit 13ea806
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ matrix:
- php: 7
env:
- DEPS=latest
- php: 7.1
env:
- DEPS=lowest
- php: 7.1
env:
- DEPS=locked
- CS_CHECK=true
- php: 7.1
env:
- DEPS=latest
- php: hhvm
env:
- DEPS=lowest
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

- ZfrCors now properly disallows `Access-Control-Allow-Origin: *` when the
credentials flag is true. [#35]
- The `CorsRequestListener` now no longer raises an exception when triggered
during `EVENT_FINISH` if the `Origin` header is invalid, and instead just
returns early. That condition is already found during pre-flight, which allows
ignoring it when returning the response. [#47]

# 1.4.0

Expand Down
9 changes: 8 additions & 1 deletion src/ZfrCors/Mvc/CorsRequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,17 @@ public function onCorsRequest(MvcEvent $event)
return;
}

try {
$isCorsRequest = $this->corsService->isCorsRequest($request);
} catch (InvalidOriginException $exception) {
// InvalidOriginException should already be handled by `CorsRequestListener::onCorsPreflight`
return;
}

// Also ensure that the vary header is set when no origin is set
// to prevent reverse proxy caching a wrong request; causing all of the following
// requests to fail due to missing CORS headers.
if (! $this->corsService->isCorsRequest($request)) {
if (! $isCorsRequest) {
if (! $request->getHeader('Origin')) {
$this->corsService->ensureVaryHeader($response);
}
Expand Down
21 changes: 21 additions & 0 deletions tests/ZfrCorsTest/Mvc/CorsRequestListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,25 @@ public function testImmediatelyReturnBadRequestResponseForInvalidOriginHeaderVal
$this->assertEquals(400, $response->getStatusCode());
$this->assertEquals('', $response->getContent());
}

/**
* Application always triggers `MvcEvent::EVENT_FINISH` and since the `CorsRequestListener` is listening on it, we
* should handle the exception aswell.
*
*
* @return void
*/
public function testOnCorsRequestCanHandleInvalidOriginHeaderValue()
{
$mvcEvent = new MvcEvent();
$request = new HttpRequest();
$response = new HttpResponse();

$request->getHeaders()->addHeaderLine('Origin', 'file:');

$mvcEvent->setRequest($request)
->setResponse($response);

$this->corsListener->onCorsRequest($mvcEvent);
}
}

0 comments on commit 13ea806

Please sign in to comment.