Skip to content

Commit 7b15962

Browse files
authored
Merge pull request #559 from FriendsOfSymfony/fix-flash-cookie-handling
array_merge_recursive rather than delay setting the flash messages cookie
2 parents 5df759a + 4970863 commit 7b15962

File tree

4 files changed

+83
-18
lines changed

4 files changed

+83
-18
lines changed

.github/workflows/php.yml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
name: CI
2+
3+
env:
4+
SYMFONY_PHPUNIT_DIR: "$HOME/symfony-bridge/.phpunit"
5+
6+
on:
7+
push:
8+
branches:
9+
- master
10+
pull_request:
11+
12+
jobs:
13+
build:
14+
runs-on: ubuntu-latest
15+
strategy:
16+
matrix:
17+
include:
18+
# Test the latest stable release
19+
- php-version: '7.2'
20+
- php-version: '7.3'
21+
- php-version: '7.4'
22+
# Test Symfony LTS versions. Read more at https://github.com/symfony/lts
23+
- php-version: '7.4'
24+
dependencies: 'symfony/lts:^3'
25+
- php-version: '7.4'
26+
symfony-version: '^4'
27+
- php-version: '7.4'
28+
symfony-version: '^5'
29+
# Test latest unreleased versions
30+
- php-version: '7.4'
31+
symfony-version: '^5'
32+
stability: 'dev'
33+
name: PHP ${{ matrix.php-version }} Test on Symfony ${{ matrix.symfony-version }} ${{ matrix.dependencies}} ${{ matrix.stability }} ${{ matrix.composer-flag }}
34+
steps:
35+
- name: Pull the code
36+
uses: actions/checkout@v2
37+
- name: Install PHP and Composer
38+
uses: shivammathur/setup-php@v2
39+
with:
40+
php-version: ${{ matrix.php-version }}
41+
tools: composer:v2
42+
- name: Check PHP Version
43+
run: php -v
44+
- name: Stability
45+
run: composer config minimum-stability ${{ matrix.stability }}
46+
if: ${{ matrix.stability }}
47+
- name: Additional require
48+
run: composer require --no-update ${{ matrix.dependencies }}
49+
if: ${{ matrix.dependencies }}
50+
- name: Symfony version
51+
run: composer require --no-update symfony/flex && composer config extra.symfony.require ${{ matrix.symfony-version}}
52+
if: ${{ matrix.symfony-version }}
53+
- name: Composer update
54+
run: composer update ${{ matrix.composer-flag }} --prefer-dist --no-interaction
55+
- name: Composer validate
56+
run: composer validate --strict --no-check-lock
57+
- name: Run tests
58+
run: php vendor/bin/simple-phpunit -v
59+

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
Changelog
22
=========
33

4+
2.9.2
5+
-----
6+
7+
### Fixed
8+
9+
* 2.9.1 fixed overwriting flash messages on multiple redirects, but introduced
10+
a risk to lose flash messages when redirecting to a path that is outside the
11+
firewall or destroys the session.
12+
This version hopefully fixes both cases. Existing flash messages in a request
13+
cookie are merged with new flash messages from the session.
14+
415
2.9.1
516
-----
617

src/EventListener/FlashMessageListener.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,26 +87,28 @@ public function onKernelResponse(FlashMessageResponseEvent $event)
8787
return;
8888
}
8989

90-
$response = $event->getResponse();
91-
92-
// If the response is a redirect, we should wait until the final response
93-
// is reached
94-
if ($response->isRedirect()) {
95-
return;
96-
}
97-
9890
$flashBag = $this->session->getFlashBag();
9991
$flashes = $flashBag->all();
10092

10193
if (empty($flashes)) {
10294
return;
10395
}
10496

97+
$response = $event->getResponse();
98+
10599
$cookies = $response->headers->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
106100
$host = (null === $this->options['host']) ? '' : $this->options['host'];
107101
if (isset($cookies[$host][$this->options['path']][$this->options['name']])) {
108102
$rawCookie = $cookies[$host][$this->options['path']][$this->options['name']]->getValue();
109-
$flashes = array_merge($flashes, json_decode($rawCookie));
103+
$flashes = array_merge_recursive($flashes, json_decode($rawCookie, true));
104+
}
105+
106+
// Preserve existing flash message cookie from previous redirect if there was one.
107+
// This covers multiple redirects where each redirect adds flash messages.
108+
$request = $event->getRequest();
109+
if ($request->cookies->has($this->options['name'])) {
110+
$rawCookie = $request->cookies->get($this->options['name']);
111+
$flashes = array_merge_recursive($flashes, json_decode($rawCookie, true));
110112
}
111113

112114
$cookie = new Cookie(

tests/Functional/EventListener/FlashMessageListenerTest.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
1515
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
16-
use Symfony\Component\HttpFoundation\Cookie;
1716

1817
class FlashMessageListenerTest extends WebTestCase
1918
{
@@ -33,7 +32,6 @@ public function testFlashMessageCookieIsSet()
3332

3433
$found = false;
3534
foreach ($cookies as $cookie) {
36-
/** @var Cookie $cookie */
3735
if ('flash_cookie_name' !== $cookie->getName()) {
3836
continue;
3937
}
@@ -45,9 +43,7 @@ public function testFlashMessageCookieIsSet()
4543
$found = true;
4644
}
4745

48-
if (!$found) {
49-
$this->fail('Cookie flash_cookie_name not found in the cookie response header: '.implode(',', $cookies));
50-
}
46+
$this->assertTrue($found, 'Cookie "flash_cookie_name" not found in response cookies');
5147
}
5248

5349
public function testFlashMessageCookieIsSetOnRedirect()
@@ -65,7 +61,6 @@ public function testFlashMessageCookieIsSetOnRedirect()
6561

6662
$found = false;
6763
foreach ($cookies as $cookie) {
68-
/** @var Cookie $cookie */
6964
if ('flash_cookie_name' !== $cookie->getName()) {
7065
continue;
7166
}
@@ -77,8 +72,6 @@ public function testFlashMessageCookieIsSetOnRedirect()
7772
$found = true;
7873
}
7974

80-
if (!$found) {
81-
$this->fail('Cookie flash_cookie_name not found in the cookie response header: '.implode(',', $cookies));
82-
}
75+
$this->assertTrue($found, 'Cookie "flash_cookie_name" not found in response cookies');
8376
}
8477
}

0 commit comments

Comments
 (0)