Skip to content

Commit 53959a9

Browse files
committed
Merge branch '6.4' into 7.3
* 6.4: [Security] Fix `HttpUtils::createRequest()` when the base request is forwarded fix setup to actually run Redis Sentinel/Cluster integration tests
2 parents e265f33 + b04eb43 commit 53959a9

File tree

5 files changed

+45
-11
lines changed

5 files changed

+45
-11
lines changed

.github/workflows/integration-tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ jobs:
261261
REDIS_SENTINEL_SERVICE: redis_sentinel
262262
REDIS_REPLICATION_HOSTS: 'localhost:16382 localhost:16381'
263263
MESSENGER_REDIS_DSN: redis://127.0.0.1:7006/messages
264+
MESSENGER_REDIS_SENTINEL_MASTER: redis_sentinel
264265
MESSENGER_AMQP_DSN: amqp://localhost/%2f/messages
265266
MESSENGER_SQS_DSN: "sqs://localhost:4566/messages?sslmode=disable&poll_timeout=0.01"
266267
MESSENGER_SQS_FIFO_QUEUE_DSN: "sqs://localhost:4566/messages.fifo?sslmode=disable&poll_timeout=0.01"

src/Symfony/Component/Messenger/Bridge/Redis/Tests/Transport/ConnectionTest.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -404,10 +404,19 @@ public static function provideIdPatterns(): \Generator
404404
yield '100ms delay' => ['/^[A-Z\d\/+]+$/i', 100, 'rawCommand', '1'];
405405
}
406406

407+
/**
408+
* @group integration
409+
*/
407410
public function testInvalidSentinelMasterName()
408411
{
412+
if (!$hosts = getenv('REDIS_SENTINEL_HOSTS')) {
413+
$this->markTestSkipped('REDIS_SENTINEL_HOSTS env var is not defined.');
414+
}
415+
416+
$dsn = 'redis:?host['.str_replace(' ', ']&host[', $hosts).']';
417+
409418
try {
410-
Connection::fromDsn(getenv('MESSENGER_REDIS_DSN'), ['delete_after_ack' => true], null);
419+
Connection::fromDsn($dsn, ['delete_after_ack' => true]);
411420
} catch (\Exception $e) {
412421
self::markTestSkipped($e->getMessage());
413422
}
@@ -416,14 +425,12 @@ public function testInvalidSentinelMasterName()
416425
self::markTestSkipped('Redis sentinel is not configured');
417426
}
418427

419-
$master = getenv('MESSENGER_REDIS_DSN');
420428
$uid = random_int(1, \PHP_INT_MAX);
421429

422-
$exp = explode('://', $master, 2)[1];
423430
$this->expectException(\InvalidArgumentException::class);
424-
$this->expectExceptionMessage(\sprintf('Failed to retrieve master information from master name "%s" and address "%s".', $uid, $exp));
431+
$this->expectExceptionMessage(\sprintf('Failed to retrieve master information from sentinel "%s".', $uid));
425432

426-
Connection::fromDsn(\sprintf('%s/messenger-clearlasterror', $master), ['delete_after_ack' => true, 'sentinel' => $uid], null);
433+
Connection::fromDsn(\sprintf('%s/messenger-clearlasterror', $dsn), ['delete_after_ack' => true, 'sentinel' => $uid]);
427434
}
428435

429436
public function testFromDsnOnUnixSocketWithUserAndPassword()

src/Symfony/Component/Messenger/Bridge/Redis/Tests/Transport/RedisExtIntegrationTest.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ protected function setUp(): void
3939

4040
try {
4141
$this->redis = $this->createRedisClient();
42-
$this->connection = Connection::fromDsn(getenv('MESSENGER_REDIS_DSN'), ['sentinel' => getenv('MESSENGER_REDIS_SENTINEL_MASTER') ?: null], $this->redis);
42+
$this->connection = Connection::fromDsn(getenv('MESSENGER_REDIS_DSN'), [], $this->redis);
4343
$this->connection->cleanup();
4444
$this->connection->setup();
4545
} catch (\Exception $e) {
@@ -147,7 +147,7 @@ public function testConnectionSendDelayedMessagesWithSameContent()
147147
public function testConnectionBelowRedeliverTimeout()
148148
{
149149
// lower redeliver timeout and claim interval
150-
$connection = Connection::fromDsn(getenv('MESSENGER_REDIS_DSN'), ['sentinel' => getenv('MESSENGER_REDIS_SENTINEL_MASTER') ?: null], $this->redis);
150+
$connection = Connection::fromDsn(getenv('MESSENGER_REDIS_DSN'), [], $this->redis);
151151

152152
$connection->cleanup();
153153
$connection->setup();
@@ -175,7 +175,7 @@ public function testConnectionClaimAndRedeliver()
175175
// lower redeliver timeout and claim interval
176176
$connection = Connection::fromDsn(
177177
getenv('MESSENGER_REDIS_DSN'),
178-
['redeliver_timeout' => 0, 'claim_interval' => 500, 'sentinel' => getenv('MESSENGER_REDIS_SENTINEL_MASTER') ?: null],
178+
['redeliver_timeout' => 0, 'claim_interval' => 500],
179179

180180
$this->redis
181181
);
@@ -261,7 +261,17 @@ public static function sentinelOptionNames(): \Generator
261261

262262
public function testLazySentinel()
263263
{
264-
$connection = Connection::fromDsn(getenv('MESSENGER_REDIS_DSN'),
264+
if (!$hosts = getenv('REDIS_SENTINEL_HOSTS')) {
265+
$this->markTestSkipped('REDIS_SENTINEL_HOSTS env var is not defined.');
266+
}
267+
268+
if (!getenv('MESSENGER_REDIS_SENTINEL_MASTER')) {
269+
$this->markTestSkipped('MESSENGER_REDIS_SENTINEL_MASTER env var is not defined.');
270+
}
271+
272+
$dsn = 'redis:?host['.str_replace(' ', ']&host[', $hosts).']';
273+
274+
$connection = Connection::fromDsn($dsn,
265275
['lazy' => true,
266276
'delete_after_ack' => true,
267277
'sentinel' => getenv('MESSENGER_REDIS_SENTINEL_MASTER') ?: null,
@@ -336,7 +346,7 @@ public function testFromDsnWithMultipleHosts()
336346
$dsn = array_map(fn ($host) => 'redis://'.$host, $hosts);
337347
$dsn = implode(',', $dsn);
338348

339-
$this->assertInstanceOf(Connection::class, Connection::fromDsn($dsn, ['sentinel' => getenv('MESSENGER_REDIS_SENTINEL_MASTER') ?: null]));
349+
$this->assertInstanceOf(Connection::class, Connection::fromDsn($dsn, []));
340350
}
341351

342352
public function testJsonError()
@@ -460,7 +470,7 @@ private function getConnectionStream(Connection $connection): string
460470
private function skipIfRedisClusterUnavailable()
461471
{
462472
try {
463-
new \RedisCluster(null, getenv('REDIS_CLUSTER_HOST') ? explode(' ', getenv('REDIS_CLUSTER_HOST')) : []);
473+
new \RedisCluster(null, getenv('REDIS_CLUSTER_HOSTS') ? explode(' ', getenv('REDIS_CLUSTER_HOSTS')) : []);
464474
} catch (\Exception $e) {
465475
self::markTestSkipped($e->getMessage());
466476
}

src/Symfony/Component/Security/Http/HttpUtils.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,13 @@ public function createRedirectResponse(Request $request, string $path, int $stat
6565
*/
6666
public function createRequest(Request $request, string $path): Request
6767
{
68+
if ($trustedProxies = Request::getTrustedProxies()) {
69+
Request::setTrustedProxies([], Request::getTrustedHeaderSet());
70+
}
6871
$newRequest = Request::create($this->generateUri($request, $path), 'get', [], $request->cookies->all(), [], $request->server->all());
72+
if ($trustedProxies) {
73+
Request::setTrustedProxies($trustedProxies, Request::getTrustedHeaderSet());
74+
}
6975

7076
static $setSession;
7177

src/Symfony/Component/Security/Http/Tests/HttpUtilsTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,16 @@ public static function provideSecurityRequestAttributes()
233233
];
234234
}
235235

236+
public function testCreateRequestHandlesTrustedHeaders()
237+
{
238+
Request::setTrustedProxies(['127.0.0.1'], Request::HEADER_X_FORWARDED_PREFIX);
239+
240+
$this->assertSame(
241+
'http://localhost/foo/',
242+
(new HttpUtils())->createRequest(Request::create('/', server: ['HTTP_X_FORWARDED_PREFIX' => '/foo']), '/')->getUri(),
243+
);
244+
}
245+
236246
public function testCheckRequestPath()
237247
{
238248
$utils = new HttpUtils($this->getUrlGenerator());

0 commit comments

Comments
 (0)