Skip to content

[HttpFoundation] Update "[Session] Overwrite invalid session id" to only validate when files session storage is used #46678

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

Merged
merged 1 commit into from
Jun 19, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function start()
}

$sessionId = $_COOKIE[session_name()] ?? null;
if ($sessionId && !preg_match('/^[a-zA-Z0-9,-]{22,}$/', $sessionId)) {
if ($sessionId && $this->saveHandler instanceof AbstractProxy && 'files' === $this->saveHandler->getSaveHandlerName() && !preg_match('/^[a-zA-Z0-9,-]{22,}$/', $sessionId)) {
// the session ID in the header is invalid, create a new one
session_id(session_create_id());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,31 @@ public function testGetBagsOnceSessionStartedIsIgnored()
$this->assertEquals($storage->getBag('flashes'), $bag);
}

public function testRegenerateInvalidSessionId()
public function testRegenerateInvalidSessionIdForNativeFileSessionHandler()
{
$_COOKIE[session_name()] = '&~[';
$started = (new NativeSessionStorage())->start();
session_id('&~[');
$storage = new NativeSessionStorage([], new NativeFileSessionHandler());
$started = $storage->start();

$this->assertTrue($started);
$this->assertMatchesRegularExpression('/^[a-zA-Z0-9,-]{22,}$/', session_id());
$storage->save();

$_COOKIE[session_name()] = '&~[';
session_id('&~[');
$storage = new NativeSessionStorage([], new SessionHandlerProxy(new NativeFileSessionHandler()));
$started = $storage->start();

$this->assertTrue($started);
$this->assertMatchesRegularExpression('/^[a-zA-Z0-9,-]{22,}$/', session_id());
$storage->save();

$_COOKIE[session_name()] = '&~[';
session_id('&~[');
$storage = new NativeSessionStorage([], new NullSessionHandler());
$started = $storage->start();
$this->assertTrue($started);
$this->assertSame('&~[', session_id());
}
}