Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
- Upgrade Web Components version to v5.1.8
- Add template to suggest element

### Fix
- Secure proxy endpoint

## [v6.5.0] - 2025.08.06
### Add
- Add to cart button for record list
Expand Down
57 changes: 47 additions & 10 deletions spec/Storefront/Controller/ProxyControllerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\HeaderBag;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -30,21 +31,49 @@ class ProxyControllerSpec extends ObjectBehavior
public function let(
Communication $config,
ClientInterface $client,
ClientBuilder $clientBuilder
ClientBuilder $clientBuilder,
Request $request,
HeaderBag $headerBag
): void {
$serverUrl = 'https://example.fact-finder.de/fact-finder';
$config->getServerUrl()->willReturn($serverUrl);
$config->getVersion()->willReturn('ng');
$config->getApiKey()->willReturn('api-key-123');
$config->isProxyEnabled()->willReturn(true);

$this->beConstructedWith($config);

$clientBuilder->build()->willReturn($client);
$clientBuilder->withServerUrl(Argument::any())->willReturn($clientBuilder);
$clientBuilder->withApiKey(Argument::any())->willReturn($clientBuilder);
$clientBuilder->withVersion(Argument::any())->willReturn($clientBuilder);

$request->headers = $headerBag;
$headerBag->get('x-ff-api-key')->willReturn('api-key-123');

$this->client = $client;
$this->clientBuilder = $clientBuilder;
}

public function it_should_return_unauthorized_if_api_key_header_is_missing(
Request $request,
HeaderBag $headerBag,
ClientBuilder $clientBuilder,
EventDispatcherInterface $eventDispatcher
): void {
// Given
$request->headers = $headerBag;
$headerBag->get('x-ff-api-key')->willReturn(null);

// When
$response = $this->execute('some/endpoint', $request, $clientBuilder, $eventDispatcher);

// Then
$response->shouldBeAnInstanceOf(JsonResponse::class);
Assert::assertEquals(Response::HTTP_UNAUTHORIZED, $response->getWrappedObject()->getStatusCode());
Assert::assertStringContainsString('UNAUTHORIZED', $response->getWrappedObject()->getContent());
}

public function it_should_return_success_response(
Request $request,
ResponseInterface $response,
Expand All @@ -56,8 +85,10 @@ public function it_should_return_success_response(
$request->getMethod()->willReturn(Request::METHOD_GET);
$uri = 'rest/v5/search/example_channel?query=bag&sid=123&format=json';
$_SERVER['REQUEST_URI'] = sprintf('/fact-finder/proxy/%s', $uri);
$this->clientBuilder->withApiKey('api-key-123')->shouldBeCalled()->willReturn($this->clientBuilder);

$this->client->request(Request::METHOD_GET, $uri)->willReturn($response);
$jsonResponse = file_get_contents(dirname(__DIR__, 2) . '/data/proxy/search-bag.json');
$jsonResponse = '{"results": []}'; // Uproszczone dla przykładu, możesz zostawić file_get_contents
$responseData = json_decode($jsonResponse, true);
$stream->__toString()->willReturn($jsonResponse);
$response->getBody()->willReturn($stream);
Expand All @@ -82,21 +113,27 @@ public function it_should_return_error_response(
$request->getMethod()->willReturn(Request::METHOD_GET);
$uri = 'rest/v5/search/example_channel?query=bag&sid=123&format=json';
$_SERVER['REQUEST_URI'] = sprintf('/fact-finder/proxy/%s', $uri);

$this->client->request(Request::METHOD_GET, $uri)->willThrow(new ConnectException('Unable to connect with server.', $requestInterface->getWrappedObject()));
$eventDispatcher->dispatch(Argument::type(BeforeProxyErrorResponseEvent::class))->willReturn($event);
$event->getResponse()->willReturn(new JsonResponse(['message' => 'Unable to connect with server.'], Response::HTTP_BAD_REQUEST));

// When
$response = $this->execute('rest/v5/search/example_channel', $request, $this->clientBuilder, $eventDispatcher);

// Then
$response->shouldBeAnInstanceOf(JsonResponse::class);
Assert::assertEquals(
['message' => 'Unable to connect with server.'],
json_decode($response->getWrappedObject()->getContent(), true)
);
Assert::assertEquals(
Response::HTTP_BAD_REQUEST,
$response->getWrappedObject()->getStatusCode()
);
Assert::assertEquals(Response::HTTP_BAD_REQUEST, $response->getWrappedObject()->getStatusCode());
}

public function it_should_throw_exception_if_proxy_is_disabled(
Communication $config,
Request $request,
ClientBuilder $clientBuilder,
EventDispatcherInterface $eventDispatcher
): void {
$config->isProxyEnabled()->willReturn(false);
$this->shouldThrow(\Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class)
->during('execute', ['some-endpoint', $request, $clientBuilder, $eventDispatcher]);
}
}
16 changes: 15 additions & 1 deletion src/Storefront/Controller/ProxyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;

#[Route(defaults: ['_routeScope' => ['storefront']])]
Expand All @@ -39,9 +40,22 @@ public function execute(
ClientBuilder $clientBuilder,
EventDispatcherInterface $eventDispatcher,
): Response {
if (!$this->config->isProxyEnabled()) {
throw new NotFoundHttpException('Proxy is disabled.');
}

$apiKey = $request->headers->get('x-ff-api-key');

if (!$apiKey) {
return new JsonResponse(
['message' => 'UNAUTHORIZED'],
Response::HTTP_UNAUTHORIZED
);
}

$client = $clientBuilder
->withServerUrl($this->config->getServerUrl() . '/')
->withApiKey($this->config->getApiKey())
->withApiKey($apiKey)
->withVersion($this->config->getVersion())
->build();
$query = (string) parse_url($_SERVER['REQUEST_URI'] ?? '', PHP_URL_QUERY);
Expand Down
Loading