Skip to content

Commit 461d082

Browse files
committed
Merge branch '6.0' into 6.1
* 6.0: 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 2004dbb + 8737cb7 commit 461d082

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
@@ -1227,7 +1227,7 @@ private function addExceptionsSection(ArrayNodeDefinition $rootNode)
12271227
->info('The status code of the response. Null to let Symfony decide.')
12281228
->validate()
12291229
->ifTrue(function ($v) { return $v < 100 || $v > 599; })
1230-
->thenInvalid('The log level is not valid. Pick a value between 100 and 599.')
1230+
->thenInvalid('The status code is not valid. Pick a value between 100 and 599.')
12311231
->end()
12321232
->defaultNull()
12331233
->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 (str_starts_with($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
@@ -1263,6 +1263,7 @@ public static function closeOutputBuffers(int $targetLevel, bool $flush): void
12631263
while ($level-- > $targetLevel && ($s = $status[$level]) && (!isset($s['del']) ? !isset($s['flags']) || ($s['flags'] & $flags) === $flags : $s['del'])) {
12641264
if ($flush) {
12651265
ob_end_flush();
1266+
flush();
12661267
} else {
12671268
ob_end_clean();
12681269
}

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
@@ -134,6 +134,19 @@ public function testThrowsOnContextOnInvalidMethod()
134134
$loader->loadClassMetadata($classMetadata);
135135
}
136136

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

139152
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
@@ -161,6 +161,31 @@ public function testInvalidPasswordCustomEndpoint()
161161
->assertRaised();
162162
}
163163

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

198-
private function createHttpClientStub(): HttpClientInterface
223+
private function createHttpClientStub(?string $returnValue = null): HttpClientInterface
199224
{
200225
$httpClientStub = $this->createMock(HttpClientInterface::class);
201226
$httpClientStub->method('request')->willReturnCallback(
202-
function (string $method, string $url): ResponseInterface {
227+
function (string $method, string $url) use ($returnValue): ResponseInterface {
203228
if (self::PASSWORD_TRIGGERING_AN_ERROR_RANGE_URL === $url) {
204229
throw new class('Problem contacting the Have I been Pwned API.') extends \Exception implements ServerExceptionInterface {
205230
public function getResponse(): ResponseInterface
@@ -212,7 +237,7 @@ public function getResponse(): ResponseInterface
212237
$responseStub = $this->createMock(ResponseInterface::class);
213238
$responseStub
214239
->method('getContent')
215-
->willReturn(implode("\r\n", self::RETURN));
240+
->willReturn($returnValue ?? implode("\r\n", self::RETURN));
216241

217242
return $responseStub;
218243
}

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)