Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EasyApiPlatform] Add OutputSanitizerListener #1619

Merged
merged 10 commits into from
Jan 21, 2025
4 changes: 4 additions & 0 deletions packages/EasyApiPlatform/bundle/EasyApiPlatformBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public function loadExtension(array $config, ContainerConfigurator $container, C
$container->import('config/pagination.php');
}

if ($config['output_sanitizer']['enabled']) {
$container->import('config/output_sanitizer.php');
}

if ($config['return_not_found_on_read_operations']['enabled']) {
$container->import('config/state_provider.php');
}
Expand Down
3 changes: 3 additions & 0 deletions packages/EasyApiPlatform/bundle/config/definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
->arrayNode('custom_paginator')
->canBeDisabled()
->end()
->arrayNode('output_sanitizer')
->canBeEnabled()
->end()
->arrayNode('return_not_found_on_read_operations')
->canBeDisabled()
->end()
Expand Down
16 changes: 16 additions & 0 deletions packages/EasyApiPlatform/bundle/config/output_sanitizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use EonX\EasyApiPlatform\Common\Listener\OutputSanitizerListener;

return static function (ContainerConfigurator $container): void {
$services = $container->services();

$services->defaults()
->autowire()
->autoconfigure();

$services->set(OutputSanitizerListener::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);

namespace EonX\EasyApiPlatform\Common\Listener;

use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\HttpKernel\Event\ResponseEvent;

#[AsEventListener]
final class OutputSanitizerListener
{
public function __invoke(ResponseEvent $event): void
{
if ($event->isMainRequest() === false) {
return;
}

$response = $event->getResponse();

if (\str_contains((string)$response->headers->get('Content-Type'), 'application/json') === false) {
return;
}

$content = $response->getContent();

if ($content === false) {
return;
}

$response->setContent(\str_replace(['<', '>'], ['&lt;', '&gt;'], $content));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
declare(strict_types=1);

namespace EonX\EasyApiPlatform\Tests\Application\src\Common\Listener;

use Doctrine\ORM\EntityManagerInterface;
use EonX\EasyApiPlatform\Tests\Application\AbstractApplicationTestCase;
use EonX\EasyApiPlatform\Tests\Fixture\App\CustomPaginator\ApiResource\Category;

final class OutputSanitizerListenerTest extends AbstractApplicationTestCase
{
public function testItSucceeds(): void
{
self::setUpClient(['environment' => 'enable_output_sanitizer']);
$this->initDatabase();
$entityManager = self::getService(EntityManagerInterface::class);
$category = (new Category())->setTitle('<Some category>');
$entityManager->persist($category);
$entityManager->flush();

$response = self::$client->request('GET', '/categories/' . $category->getId());

$responseData = $response->toArray(false);

self::assertSame('&lt;Some category&gt;', $responseData['title']);
}

public function testItSucceedsAndDoesNothingWhenDisabled(): void
{
$this->initDatabase();
$entityManager = self::getService(EntityManagerInterface::class);
$title = '<Some category>';
$category = (new Category())->setTitle($title);
$entityManager->persist($category);
$entityManager->flush();

$response = self::$client->request('GET', '/categories/' . $category->getId());

$responseData = $response->toArray(false);

self::assertSame($title, $responseData['title']);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
declare(strict_types=1);

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Symfony\Config\EasyApiPlatformConfig;

return static function (EasyApiPlatformConfig $easyApiPlatformConfig): void {
$easyApiPlatformConfig->outputSanitizer()
->enabled(true);
};
Loading