Skip to content

Commit a63464e

Browse files
committed
bug #2461 [LiveComponent] Check secret is not empty + add [SensitiveParameter] (smnandre)
This PR was merged into the 2.x branch. Discussion ---------- [LiveComponent] Check secret is not empty + add [SensitiveParameter] | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Issues | Fix #... | License | MIT Improve security before we allow secret customization for LiveComponents (cf #2453) I consider this a fix as passing an empty string for secret produces the same hash as passing null... which is deprecated for obvious reasons. Commits ------- 3c3d097 [LiveComponent] Check secret is not empty + add missing [SensitiveParameter]
2 parents 58f8069 + 3c3d097 commit a63464e

File tree

4 files changed

+70
-3
lines changed

4 files changed

+70
-3
lines changed

src/LiveComponent/src/LiveComponentHydrator.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,11 @@ public function __construct(
5050
private PropertyAccessorInterface $propertyAccessor,
5151
private LiveComponentMetadataFactory $liveComponentMetadataFactory,
5252
private NormalizerInterface|DenormalizerInterface|null $serializer,
53-
private string $secret,
53+
#[\SensitiveParameter] private string $secret,
5454
) {
55+
if (!$secret) {
56+
throw new \InvalidArgumentException('A non-empty secret is required.');
57+
}
5558
}
5659

5760
public function dehydrate(object $component, ComponentAttributes $attributes, LiveComponentMetadata $componentMetadata): DehydratedProps

src/LiveComponent/src/Util/FingerprintCalculator.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@
2424
*
2525
* @internal
2626
*/
27-
class FingerprintCalculator
27+
final class FingerprintCalculator
2828
{
2929
public function __construct(
30-
private string $secret,
30+
#[\SensitiveParameter] private string $secret,
3131
) {
32+
if (!$secret) {
33+
throw new \InvalidArgumentException('A non-empty secret is required.');
34+
}
3235
}
3336

3437
public function calculateFingerprint(array $inputProps, LiveComponentMetadata $liveMetadata): string
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\UX\LiveComponent\Tests\Unit;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
16+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
17+
use Symfony\UX\LiveComponent\LiveComponentHydrator;
18+
use Symfony\UX\LiveComponent\Metadata\LiveComponentMetadataFactory;
19+
20+
final class LiveComponentHydratorTest extends TestCase
21+
{
22+
public function testConstructWithEmptySecret(): void
23+
{
24+
$this->expectException(\InvalidArgumentException::class);
25+
$this->expectExceptionMessage('A non-empty secret is required.');
26+
27+
new LiveComponentHydrator(
28+
[],
29+
$this->createMock(PropertyAccessorInterface::class),
30+
$this->createMock(LiveComponentMetadataFactory::class),
31+
$this->createMock(NormalizerInterface::class),
32+
'',
33+
);
34+
}
35+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\UX\LiveComponent\Tests\Unit\Util;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\UX\LiveComponent\Util\FingerprintCalculator;
16+
17+
final class FingerprintCalculatorTest extends TestCase
18+
{
19+
public function testConstructWithEmptySecret(): void
20+
{
21+
$this->expectException(\InvalidArgumentException::class);
22+
$this->expectExceptionMessage('A non-empty secret is required.');
23+
24+
new FingerprintCalculator('');
25+
}
26+
}

0 commit comments

Comments
 (0)