Skip to content

Commit 9a02660

Browse files
minor symfony#57746 do not use uniqid() for generating dev tool tokens (xabbuh)
This PR was merged into the 7.2 branch. Discussion ---------- do not use `uniqid()` for generating dev tool tokens | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | part of symfony#57588 | License | MIT Commits ------- 5ad7ab9 do not use uniqid() for generating dev tool tokens
2 parents 051cd95 + 5ad7ab9 commit 9a02660

File tree

4 files changed

+9
-9
lines changed

4 files changed

+9
-9
lines changed

src/Symfony/Bundle/FrameworkBundle/EventListener/ConsoleProfilerListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function initialize(ConsoleCommandEvent $event): void
7777
return;
7878
}
7979

80-
$request->attributes->set('_stopwatch_token', substr(hash('xxh128', uniqid(mt_rand(), true)), 0, 6));
80+
$request->attributes->set('_stopwatch_token', bin2hex(random_bytes(3)));
8181
$this->stopwatch->openSection();
8282
}
8383

src/Symfony/Component/HttpKernel/Debug/TraceableEventDispatcher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ protected function beforeDispatch(string $eventName, object $event): void
2727
{
2828
switch ($eventName) {
2929
case KernelEvents::REQUEST:
30-
$event->getRequest()->attributes->set('_stopwatch_token', substr(hash('xxh128', uniqid(mt_rand(), true)), 0, 6));
30+
$event->getRequest()->attributes->set('_stopwatch_token', bin2hex(random_bytes(3)));
3131
$this->stopwatch->openSection();
3232
break;
3333
case KernelEvents::VIEW:

src/Symfony/Component/HttpKernel/Profiler/Profiler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public function collect(Request $request, Response $response, ?\Throwable $excep
133133
return null;
134134
}
135135

136-
$profile = new Profile(substr(hash('xxh128', uniqid(mt_rand(), true)), 0, 6));
136+
$profile = new Profile(bin2hex(random_bytes(3)));
137137
$profile->setTime(time());
138138
$profile->setUrl($request->getUri());
139139
$profile->setMethod($request->getMethod());

src/Symfony/Component/Serializer/Debug/TraceableSerializer.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function __construct(
3737

3838
public function serialize(mixed $data, string $format, array $context = []): string
3939
{
40-
$context[self::DEBUG_TRACE_ID] = $traceId = uniqid('', true);
40+
$context[self::DEBUG_TRACE_ID] = $traceId = bin2hex(random_bytes(4));
4141

4242
$startTime = microtime(true);
4343
$result = $this->serializer->serialize($data, $format, $context);
@@ -52,7 +52,7 @@ public function serialize(mixed $data, string $format, array $context = []): str
5252

5353
public function deserialize(mixed $data, string $type, string $format, array $context = []): mixed
5454
{
55-
$context[self::DEBUG_TRACE_ID] = $traceId = uniqid('', true);
55+
$context[self::DEBUG_TRACE_ID] = $traceId = bin2hex(random_bytes(4));
5656

5757
$startTime = microtime(true);
5858
$result = $this->serializer->deserialize($data, $type, $format, $context);
@@ -67,7 +67,7 @@ public function deserialize(mixed $data, string $type, string $format, array $co
6767

6868
public function normalize(mixed $object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
6969
{
70-
$context[self::DEBUG_TRACE_ID] = $traceId = uniqid('', true);
70+
$context[self::DEBUG_TRACE_ID] = $traceId = bin2hex(random_bytes(4));
7171

7272
$startTime = microtime(true);
7373
$result = $this->serializer->normalize($object, $format, $context);
@@ -82,7 +82,7 @@ public function normalize(mixed $object, ?string $format = null, array $context
8282

8383
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed
8484
{
85-
$context[self::DEBUG_TRACE_ID] = $traceId = uniqid('', true);
85+
$context[self::DEBUG_TRACE_ID] = $traceId = bin2hex(random_bytes(4));
8686

8787
$startTime = microtime(true);
8888
$result = $this->serializer->denormalize($data, $type, $format, $context);
@@ -97,7 +97,7 @@ public function denormalize(mixed $data, string $type, ?string $format = null, a
9797

9898
public function encode(mixed $data, string $format, array $context = []): string
9999
{
100-
$context[self::DEBUG_TRACE_ID] = $traceId = uniqid('', true);
100+
$context[self::DEBUG_TRACE_ID] = $traceId = bin2hex(random_bytes(4));
101101

102102
$startTime = microtime(true);
103103
$result = $this->serializer->encode($data, $format, $context);
@@ -112,7 +112,7 @@ public function encode(mixed $data, string $format, array $context = []): string
112112

113113
public function decode(string $data, string $format, array $context = []): mixed
114114
{
115-
$context[self::DEBUG_TRACE_ID] = $traceId = uniqid('', true);
115+
$context[self::DEBUG_TRACE_ID] = $traceId = bin2hex(random_bytes(4));
116116

117117
$startTime = microtime(true);
118118
$result = $this->serializer->decode($data, $format, $context);

0 commit comments

Comments
 (0)