Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HttpFoundation] fix accessing session bags #32137

Merged
merged 1 commit into from Jun 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Symfony/Component/HttpFoundation/Session/Session.php
Expand Up @@ -253,7 +253,9 @@ public function registerBag(SessionBagInterface $bag)
*/
public function getBag($name)
{
return $this->storage->getBag($name)->getBag();
$bag = $this->storage->getBag($name);

return method_exists($bag, 'getBag') ? $bag->getBag() : $bag;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not test against SessionBagProxy to make the intention clear?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, otherwise i'd prefer to deprecate the situation (calling getBag on a non-proxy bag).

But i tend to believe one relies on a bug in this case.

Copy link
Member Author

@xabbuh xabbuh Jun 23, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how many people out there created their own SessionBagInterface implementations and also implemented this method this method to work around this issue. IMO we should play safe here to not risk any existing application.

}

/**
Expand Down
25 changes: 25 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/Session/SessionTest.php
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\SessionBagProxy;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;

/**
Expand Down Expand Up @@ -260,4 +261,28 @@ public function testIsEmpty()
$flash->get('hello');
$this->assertTrue($this->session->isEmpty());
}

public function testGetBagWithBagImplementingGetBag()
{
$bag = new AttributeBag();
$bag->setName('foo');

$storage = new MockArraySessionStorage();
$storage->registerBag($bag);

$this->assertSame($bag, (new Session($storage))->getBag('foo'));
}

public function testGetBagWithBagNotImplementingGetBag()
{
$data = [];

$bag = new AttributeBag();
$bag->setName('foo');

$storage = new MockArraySessionStorage();
$storage->registerBag(new SessionBagProxy($bag, $data, $usageIndex));

$this->assertSame($bag, (new Session($storage))->getBag('foo'));
}
}