diff --git a/src/LiveComponent/src/LiveComponentHydrator.php b/src/LiveComponent/src/LiveComponentHydrator.php index a5b23ce091b..95f4bf91f9c 100644 --- a/src/LiveComponent/src/LiveComponentHydrator.php +++ b/src/LiveComponent/src/LiveComponentHydrator.php @@ -50,8 +50,11 @@ public function __construct( private PropertyAccessorInterface $propertyAccessor, private LiveComponentMetadataFactory $liveComponentMetadataFactory, private NormalizerInterface|DenormalizerInterface|null $serializer, - private string $secret, + #[\SensitiveParameter] private string $secret, ) { + if (!$secret) { + throw new \InvalidArgumentException('A non-empty secret is required.'); + } } public function dehydrate(object $component, ComponentAttributes $attributes, LiveComponentMetadata $componentMetadata): DehydratedProps diff --git a/src/LiveComponent/src/Util/FingerprintCalculator.php b/src/LiveComponent/src/Util/FingerprintCalculator.php index 0f3da0e9cd3..9134e1ca6be 100644 --- a/src/LiveComponent/src/Util/FingerprintCalculator.php +++ b/src/LiveComponent/src/Util/FingerprintCalculator.php @@ -24,11 +24,14 @@ * * @internal */ -class FingerprintCalculator +final class FingerprintCalculator { public function __construct( - private string $secret, + #[\SensitiveParameter] private string $secret, ) { + if (!$secret) { + throw new \InvalidArgumentException('A non-empty secret is required.'); + } } public function calculateFingerprint(array $inputProps, LiveComponentMetadata $liveMetadata): string diff --git a/src/LiveComponent/tests/Unit/LiveComponentHydratorTest.php b/src/LiveComponent/tests/Unit/LiveComponentHydratorTest.php new file mode 100644 index 00000000000..f14ff4a7b72 --- /dev/null +++ b/src/LiveComponent/tests/Unit/LiveComponentHydratorTest.php @@ -0,0 +1,35 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\UX\LiveComponent\Tests\Unit; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\PropertyAccess\PropertyAccessorInterface; +use Symfony\Component\Serializer\Normalizer\NormalizerInterface; +use Symfony\UX\LiveComponent\LiveComponentHydrator; +use Symfony\UX\LiveComponent\Metadata\LiveComponentMetadataFactory; + +final class LiveComponentHydratorTest extends TestCase +{ + public function testConstructWithEmptySecret(): void + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('A non-empty secret is required.'); + + new LiveComponentHydrator( + [], + $this->createMock(PropertyAccessorInterface::class), + $this->createMock(LiveComponentMetadataFactory::class), + $this->createMock(NormalizerInterface::class), + '', + ); + } +} diff --git a/src/LiveComponent/tests/Unit/Util/FingerprintCalculatorTest.php b/src/LiveComponent/tests/Unit/Util/FingerprintCalculatorTest.php new file mode 100644 index 00000000000..71f115d97e1 --- /dev/null +++ b/src/LiveComponent/tests/Unit/Util/FingerprintCalculatorTest.php @@ -0,0 +1,26 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\UX\LiveComponent\Tests\Unit\Util; + +use PHPUnit\Framework\TestCase; +use Symfony\UX\LiveComponent\Util\FingerprintCalculator; + +final class FingerprintCalculatorTest extends TestCase +{ + public function testConstructWithEmptySecret(): void + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('A non-empty secret is required.'); + + new FingerprintCalculator(''); + } +}