Skip to content

Commit 3fca255

Browse files
authored
Merge pull request #4 from andrew-demb/more-strict-validate-serialized-message
More strict check for serialized data. More clear psalm annotations usage
2 parents 569a3ec + 07ada5e commit 3fca255

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

src/Symfony/SymfonyMessageSerializer.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ public function decode(string $serializedMessage): object
9292
{
9393
try
9494
{
95-
/** @psalm-var array{message:array<string, string|int|float|null>, namespace:class-string} $data */
9695
$data = $this->serializer->unserialize($serializedMessage);
9796

9897
self::validateUnserializedData($data);
@@ -162,7 +161,7 @@ public function normalize(object $message): array
162161
}
163162

164163
/**
165-
* @psalm-param array{message:array<string, string|int|float|null>, namespace:class-string} $data
164+
* @psalm-assert array{message:array<string, mixed>, namespace:class-string} $data
166165
*
167166
* @throws \UnexpectedValueException
168167
*/
@@ -178,12 +177,22 @@ private static function validateUnserializedData(array $data): void
178177
);
179178
}
180179

180+
if (false === \is_array($data['message']))
181+
{
182+
throw new \UnexpectedValueException('"message" field from serialized data should be an array');
183+
}
184+
185+
if (false === \is_string($data['namespace']))
186+
{
187+
throw new \UnexpectedValueException('"namespace" field from serialized data should be a string');
188+
}
189+
181190
/**
182191
* Let's check if the specified class exists.
183192
*
184193
* @psalm-suppress DocblockTypeContradiction
185194
*/
186-
if ($data['namespace'] === '' || \class_exists((string) $data['namespace']) === false)
195+
if ($data['namespace'] === '' || \class_exists($data['namespace']) === false)
187196
{
188197
throw new \UnexpectedValueException(
189198
\sprintf('Class "%s" not found', $data['namespace'])

tests/Symfony/SymfonyMessageSerializerTest.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,32 @@ public static function classWithClosedConstructor(): void
7272
);
7373
}
7474

75+
/**
76+
* @test
77+
*
78+
* @throws \Throwable
79+
*/
80+
public function messageNotArray(): void
81+
{
82+
$this->expectException(DecodeMessageFailed::class);
83+
$this->expectExceptionMessage('"message" field from serialized data should be an array');
84+
85+
(new SymfonyMessageSerializer())->decode(\json_encode(['message' => 'someValue', 'namespace' => \SomeClass::class]));
86+
}
87+
88+
/**
89+
* @test
90+
*
91+
* @throws \Throwable
92+
*/
93+
public function namespaceNotString(): void
94+
{
95+
$this->expectException(DecodeMessageFailed::class);
96+
$this->expectExceptionMessage('"namespace" field from serialized data should be a string');
97+
98+
(new SymfonyMessageSerializer())->decode(\json_encode(['message' => [], 'namespace' => new \stdClass]));
99+
}
100+
75101
/**
76102
* @test
77103
*
@@ -82,7 +108,7 @@ public function classNotFound(): void
82108
$this->expectException(DecodeMessageFailed::class);
83109
$this->expectExceptionMessage('Class "SomeClass" not found');
84110

85-
(new SymfonyMessageSerializer())->decode(\json_encode(['message' => 'someValue', 'namespace' => \SomeClass::class]));
111+
(new SymfonyMessageSerializer())->decode(\json_encode(['message' => [], 'namespace' => \SomeClass::class]));
86112
}
87113

88114
/**

0 commit comments

Comments
 (0)