Skip to content

Commit

Permalink
BUGFIX: The CORS middleware will create the response for an OPTIONS r…
Browse files Browse the repository at this point in the history
…equest without passing the request down through the process chain

Without this change the preflight lead to a 404 as the OPTIONS request could not be routed which yielded a 404 exception.
  • Loading branch information
mficzel committed Dec 10, 2024
1 parent 8ca6c1b commit 32cd53c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
12 changes: 10 additions & 2 deletions Classes/Http/CorsHeaderMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Lmc\HttpConstants\Header;
use Neos\Flow\Annotations as Flow;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
Expand All @@ -14,6 +15,12 @@

class CorsHeaderMiddleware implements MiddlewareInterface
{
/**
* @Flow\Inject
* @var ResponseFactoryInterface
*/
protected $responseFactory;

/**
* @Flow\InjectConfiguration("enabled")
*/
Expand Down Expand Up @@ -73,15 +80,16 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
}

$this->initializeConfiguration();

$response = $handler->handle($request);
$method = $request->getMethod();

// method type is not options, return early
if ($method == 'OPTIONS') {
$this->logger->debug('CORS Component: Preflight request');
$response = $this->responseFactory->createResponse();
return $this->handlePreflight($request, $response);
}

$response = $handler->handle($request);
return $this->handleRequest($request, $response);
}

Expand Down
12 changes: 8 additions & 4 deletions Tests/Unit/Http/CorsHeaderMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,11 @@ public function testMiddlewarePreflightWithConfig(): void
};
});

$this->responseMock->expects($this->exactly(5))->method('withHeader')->willReturnSelf();
$this->responseMock->expects($this->never())->method('withHeader');

$this->middleware->process($this->requestMock, $this->handlerMock);
$response = $this->middleware->process($this->requestMock, $this->handlerMock);
$this->assertSame(200, $response->getStatusCode());

Check failure on line 71 in Tests/Unit/Http/CorsHeaderMiddlewareTest.php

View workflow job for this annotation

GitHub Actions / phpstan

Dynamic call to static method PHPUnit\Framework\Assert::assertSame().
$this->assertSame('GET', $response->getHeaderLine(Header::ACCESS_CONTROL_ALLOW_METHODS));

Check failure on line 72 in Tests/Unit/Http/CorsHeaderMiddlewareTest.php

View workflow job for this annotation

GitHub Actions / phpstan

Dynamic call to static method PHPUnit\Framework\Assert::assertSame().
}

public function testMiddlewarePreflightWithWildcardConfig(): void
Expand All @@ -83,9 +85,11 @@ public function testMiddlewarePreflightWithWildcardConfig(): void
};
});

$this->responseMock->expects($this->exactly(4))->method('withHeader')->willReturnSelf();
$this->responseMock->expects($this->never())->method('withHeader');

$this->middleware->process($this->requestMock, $this->handlerMock);
$response = $this->middleware->process($this->requestMock, $this->handlerMock);
$this->assertSame(200, $response->getStatusCode());

Check failure on line 91 in Tests/Unit/Http/CorsHeaderMiddlewareTest.php

View workflow job for this annotation

GitHub Actions / phpstan

Dynamic call to static method PHPUnit\Framework\Assert::assertSame().
$this->assertSame('GET', $response->getHeaderLine(Header::ACCESS_CONTROL_ALLOW_METHODS));

Check failure on line 92 in Tests/Unit/Http/CorsHeaderMiddlewareTest.php

View workflow job for this annotation

GitHub Actions / phpstan

Dynamic call to static method PHPUnit\Framework\Assert::assertSame().
}

public function testMiddlewareActualRequestWithConfig(): void
Expand Down

0 comments on commit 32cd53c

Please sign in to comment.