Skip to content

Commit 8737cb7

Browse files
committed
Merge branch '5.4' into 6.0
* 5.4: Update ExceptionInterface.php Revert removal of Stringable check Flush with flush() after ob_end_flush() [Validator] : Fix "PHP Warning: Undefined array key 1" in NotCompromisedPasswordValidator [Validator] Fix traverse option on Valid constraint when used as Attribute [HttpFoundation] Fix deleteFileAfterSend on client abortion Prevent that bad Ignore method annotations lead to incorrect results also fix the test fix deprecation fix sending request to paths containing multiple slashes Fix generated validation error message for wrong exception mapping status code
2 parents c158a6a + ee2eb1c commit 8737cb7

File tree

14 files changed

+204
-7
lines changed

14 files changed

+204
-7
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1207,7 +1207,7 @@ private function addExceptionsSection(ArrayNodeDefinition $rootNode)
12071207
->info('The status code of the response. Null to let Symfony decide.')
12081208
->validate()
12091209
->ifTrue(function ($v) { return $v < 100 || $v > 599; })
1210-
->thenInvalid('The log level is not valid. Pick a value between 100 and 599.')
1210+
->thenInvalid('The status code is not valid. Pick a value between 100 and 599.')
12111211
->end()
12121212
->defaultNull()
12131213
->end()

src/Symfony/Component/BrowserKit/AbstractBrowser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ protected function getAbsoluteUri(string $uri): string
618618
}
619619

620620
// protocol relative URL
621-
if (0 === strpos($uri, '//')) {
621+
if ('' !== trim($uri, '/') && str_starts_with($uri, '//')) {
622622
return parse_url($currentUri, \PHP_URL_SCHEME).':'.$uri;
623623
}
624624

src/Symfony/Component/BrowserKit/Tests/HttpBrowserTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,30 @@ public function testMultiPartRequestWithAdditionalParametersOfTheSameName()
180180
]);
181181
}
182182

183+
/**
184+
* @dataProvider forwardSlashesRequestPathProvider
185+
*/
186+
public function testMultipleForwardSlashesRequestPath(string $requestPath)
187+
{
188+
$client = $this->createMock(HttpClientInterface::class);
189+
$client
190+
->expects($this->once())
191+
->method('request')
192+
->with('GET', 'http://localhost'.$requestPath)
193+
->willReturn($this->createMock(ResponseInterface::class));
194+
$browser = new HttpBrowser($client);
195+
$browser->request('GET', $requestPath);
196+
}
197+
198+
public function forwardSlashesRequestPathProvider()
199+
{
200+
return [
201+
'one slash' => ['/'],
202+
'two slashes' => ['//'],
203+
'multiple slashes' => ['////'],
204+
];
205+
}
206+
183207
private function uploadFile(string $data): string
184208
{
185209
$path = tempnam(sys_get_temp_dir(), 'http');

src/Symfony/Component/HttpFoundation/BinaryFileResponse.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class BinaryFileResponse extends Response
3434
protected $offset = 0;
3535
protected $maxlen = -1;
3636
protected $deleteFileAfterSend = false;
37+
protected $chunkSize = 8 * 1024;
3738

3839
/**
3940
* @param \SplFileInfo|string $file The file to stream
@@ -101,6 +102,22 @@ public function getFile(): File
101102
return $this->file;
102103
}
103104

105+
/**
106+
* Sets the response stream chunk size.
107+
*
108+
* @return $this
109+
*/
110+
public function setChunkSize(int $chunkSize): self
111+
{
112+
if ($chunkSize < 1 || $chunkSize > \PHP_INT_MAX) {
113+
throw new \LogicException('The chunk size of a BinaryFileResponse cannot be less than 1 or greater than PHP_INT_MAX.');
114+
}
115+
116+
$this->chunkSize = $chunkSize;
117+
118+
return $this;
119+
}
120+
104121
/**
105122
* Automatically sets the Last-Modified header according the file modification date.
106123
*
@@ -282,7 +299,23 @@ public function sendContent(): static
282299
$out = fopen('php://output', 'w');
283300
$file = fopen($this->file->getPathname(), 'r');
284301

285-
stream_copy_to_stream($file, $out, $this->maxlen, $this->offset);
302+
ignore_user_abort(true);
303+
304+
if (0 !== $this->offset) {
305+
fseek($file, $this->offset);
306+
}
307+
308+
$length = $this->maxlen;
309+
while ($length && !feof($file)) {
310+
$read = ($length > $this->chunkSize) ? $this->chunkSize : $length;
311+
$length -= $read;
312+
313+
stream_copy_to_stream($file, $out, $read);
314+
315+
if (connection_aborted()) {
316+
break;
317+
}
318+
}
286319

287320
fclose($out);
288321
fclose($file);

src/Symfony/Component/HttpFoundation/Response.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,6 +1221,7 @@ public static function closeOutputBuffers(int $targetLevel, bool $flush): void
12211221
while ($level-- > $targetLevel && ($s = $status[$level]) && (!isset($s['del']) ? !isset($s['flags']) || ($s['flags'] & $flags) === $flags : $s['del'])) {
12221222
if ($flush) {
12231223
ob_end_flush();
1224+
flush();
12241225
} else {
12251226
ob_end_clean();
12261227
}

src/Symfony/Component/Messenger/Exception/ExceptionInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Symfony\Component\Messenger\Exception;
1313

1414
/**
15-
* Base Message component's exception.
15+
* Base Messenger component's exception.
1616
*
1717
* @author Samuel Roze <[email protected]>
1818
*/

src/Symfony/Component/Serializer/Mapping/Loader/AnnotationLoader.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata): bool
134134

135135
$attributeMetadata->setSerializedName($annotation->getSerializedName());
136136
} elseif ($annotation instanceof Ignore) {
137+
if (!$accessorOrMutator) {
138+
throw new MappingException(sprintf('Ignore on "%s::%s()" cannot be added. Ignore can only be added on methods beginning with "get", "is", "has" or "set".', $className, $method->name));
139+
}
140+
137141
$attributeMetadata->setIgnore(true);
138142
} elseif ($annotation instanceof Context) {
139143
if (!$accessorOrMutator) {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symfony\Component\Serializer\Tests\Fixtures\Annotations;
6+
7+
use Symfony\Component\Serializer\Annotation\Ignore;
8+
9+
class Entity45016
10+
{
11+
/**
12+
* @var int
13+
*/
14+
private $id = 1234;
15+
16+
public function getId(): int
17+
{
18+
return $this->id;
19+
}
20+
21+
/**
22+
* @Ignore()
23+
*/
24+
public function badIgnore(): bool
25+
{
26+
return true;
27+
}
28+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symfony\Component\Serializer\Tests\Fixtures\Attributes;
6+
7+
use Symfony\Component\Serializer\Annotation\Ignore;
8+
9+
class Entity45016
10+
{
11+
/**
12+
* @var int
13+
*/
14+
private $id = 1234;
15+
16+
public function getId(): int
17+
{
18+
return $this->id;
19+
}
20+
21+
#[Ignore]
22+
public function badIgnore(): bool
23+
{
24+
return true;
25+
}
26+
}

src/Symfony/Component/Serializer/Tests/Mapping/Loader/AnnotationLoaderTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,19 @@ public function testThrowsOnContextOnInvalidMethod()
137137
$loader->loadClassMetadata($classMetadata);
138138
}
139139

140+
public function testCanHandleUnrelatedIgnoredMethods()
141+
{
142+
$class = $this->getNamespace().'\Entity45016';
143+
144+
$this->expectException(MappingException::class);
145+
$this->expectExceptionMessage(sprintf('Ignore on "%s::badIgnore()" cannot be added', $class));
146+
147+
$metadata = new ClassMetadata($class);
148+
$loader = $this->getLoaderForContextMapping();
149+
150+
$loader->loadClassMetadata($metadata);
151+
}
152+
140153
abstract protected function createLoader(): AnnotationLoader;
141154

142155
abstract protected function getNamespace(): string;

src/Symfony/Component/Validator/Constraints/NotCompromisedPasswordValidator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ public function validate(mixed $value, Constraint $constraint)
9191
}
9292

9393
foreach (explode("\r\n", $result) as $line) {
94+
if (!str_contains($line, ':')) {
95+
continue;
96+
}
97+
9498
[$hashSuffix, $count] = explode(':', $line);
9599

96100
if ($hashPrefix.$hashSuffix === $hash && $constraint->threshold <= (int) $count) {

src/Symfony/Component/Validator/Constraints/Valid.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ class Valid extends Constraint
2424
{
2525
public $traverse = true;
2626

27+
public function __construct(array $options = null, array $groups = null, $payload = null, bool $traverse = null)
28+
{
29+
parent::__construct($options ?? [], $groups, $payload);
30+
31+
$this->traverse = $traverse ?? $this->traverse;
32+
}
33+
2734
public function __get(string $option): mixed
2835
{
2936
if ('groups' === $option) {

src/Symfony/Component/Validator/Tests/Constraints/NotCompromisedPasswordValidatorTest.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,31 @@ public function testInvalidPasswordCustomEndpoint()
162162
->assertRaised();
163163
}
164164

165+
public function testEndpointWithInvalidValueInReturn()
166+
{
167+
$returnValue = implode(
168+
"\r\n",
169+
[
170+
'36039744C253F9B2A4E90CBEDB02EBFB82D:5',
171+
'This should not break the validator',
172+
'3686792BBC66A72D40D928ED15621124CFE:7',
173+
'36EEC709091B810AA240179A44317ED415C:2',
174+
'',
175+
]
176+
);
177+
178+
$validator = new NotCompromisedPasswordValidator(
179+
$this->createHttpClientStub($returnValue),
180+
'UTF-8',
181+
true,
182+
'https://password-check.internal.example.com/range/%s'
183+
);
184+
185+
$validator->validate(self::PASSWORD_NOT_LEAKED, new NotCompromisedPassword());
186+
187+
$this->assertNoViolation();
188+
}
189+
165190
public function testInvalidConstraint()
166191
{
167192
$this->expectException(UnexpectedTypeException::class);
@@ -196,11 +221,11 @@ public function provideErrorSkippingConstraints(): iterable
196221
yield 'named arguments' => [new NotCompromisedPassword(skipOnError: true)];
197222
}
198223

199-
private function createHttpClientStub(): HttpClientInterface
224+
private function createHttpClientStub(?string $returnValue = null): HttpClientInterface
200225
{
201226
$httpClientStub = $this->createMock(HttpClientInterface::class);
202227
$httpClientStub->method('request')->willReturnCallback(
203-
function (string $method, string $url): ResponseInterface {
228+
function (string $method, string $url) use ($returnValue): ResponseInterface {
204229
if (self::PASSWORD_TRIGGERING_AN_ERROR_RANGE_URL === $url) {
205230
throw new class('Problem contacting the Have I been Pwned API.') extends \Exception implements ServerExceptionInterface {
206231
public function getResponse(): ResponseInterface
@@ -213,7 +238,7 @@ public function getResponse(): ResponseInterface
213238
$responseStub = $this->createMock(ResponseInterface::class);
214239
$responseStub
215240
->method('getContent')
216-
->willReturn(implode("\r\n", self::RETURN));
241+
->willReturn($returnValue ?? implode("\r\n", self::RETURN));
217242

218243
return $responseStub;
219244
}

src/Symfony/Component/Validator/Tests/Constraints/ValidTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Validator\Constraints\Valid;
16+
use Symfony\Component\Validator\Mapping\ClassMetadata;
17+
use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
1618

1719
/**
1820
* @author Bernhard Schussek <[email protected]>
@@ -32,4 +34,34 @@ public function testGroupsAreNullByDefault()
3234

3335
$this->assertNull($constraint->groups);
3436
}
37+
38+
/**
39+
* @requires PHP 8
40+
*/
41+
public function testAttributes()
42+
{
43+
$metadata = new ClassMetaData(ValidDummy::class);
44+
$loader = new AnnotationLoader();
45+
self::assertTrue($loader->loadClassMetadata($metadata));
46+
47+
[$bConstraint] = $metadata->properties['b']->getConstraints();
48+
self::assertFalse($bConstraint->traverse);
49+
self::assertSame(['traverse_group'], $bConstraint->groups);
50+
51+
[$cConstraint] = $metadata->properties['c']->getConstraints();
52+
self::assertSame(['my_group'], $cConstraint->groups);
53+
self::assertSame('some attached data', $cConstraint->payload);
54+
}
55+
}
56+
57+
class ValidDummy
58+
{
59+
#[Valid]
60+
private $a;
61+
62+
#[Valid(groups: ['traverse_group'], traverse: false)] // Needs a group to work at all for this test
63+
private $b;
64+
65+
#[Valid(groups: ['my_group'], payload: 'some attached data')]
66+
private $c;
3567
}

0 commit comments

Comments
 (0)