Skip to content

Commit 607dad2

Browse files
committed
Fix default session cookie name for non-alphanumeric APP_NAME
Since the switch to Str::snake() for the default session cookie name, APP_NAME values containing characters like brackets or dots produced cookie names with characters that browsers refuse to round-trip, breaking session persistence and silently logging users out. Wrap the result in Str::slug(..., '_') so the resolved cookie name is always RFC 6265 safe while preserving the snake_case style for normal APP_NAME values.
1 parent 451fd57 commit 607dad2

2 files changed

Lines changed: 58 additions & 1 deletion

File tree

config/session.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@
129129

130130
'cookie' => env(
131131
'SESSION_COOKIE',
132-
Str::snake((string) env('APP_NAME', 'laravel')).'_session'
132+
Str::slug(Str::snake((string) env('APP_NAME', 'laravel')), '_').'_session'
133133
),
134134

135135
/*
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Integration\Session;
4+
5+
use Illuminate\Support\Str;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class SessionCookieNameTest extends TestCase
9+
{
10+
/**
11+
* Resolve the default session cookie name the same way config/session.php does
12+
* for a given APP_NAME value.
13+
*/
14+
protected function resolveCookieName(string $appName): string
15+
{
16+
return Str::slug(Str::snake($appName), '_').'_session';
17+
}
18+
19+
public function testSimpleAppNameProducesSnakeCasedCookie()
20+
{
21+
$this->assertSame('my_app_session', $this->resolveCookieName('My App'));
22+
$this->assertSame('laravel_session', $this->resolveCookieName('laravel'));
23+
}
24+
25+
public function testAppNameWithBracketsIsStrippedToSafeCharacters()
26+
{
27+
$this->assertSame(
28+
'l_o_c_a_l_my_awesome_app_session',
29+
$this->resolveCookieName('[LOCAL] My Awesome App'),
30+
);
31+
}
32+
33+
public function testAppNameWithDotIsStrippedToSafeCharacters()
34+
{
35+
$this->assertSame('admindomain_session', $this->resolveCookieName('admin.domain'));
36+
$this->assertSame('examplecom_session', $this->resolveCookieName('example.com'));
37+
}
38+
39+
public function testResolvedCookieNameOnlyContainsRfc6265SafeCharacters()
40+
{
41+
$names = [
42+
'[LOCAL] My Awesome App',
43+
'admin.domain',
44+
'My App!',
45+
'foo/bar',
46+
'one;two',
47+
];
48+
49+
foreach ($names as $name) {
50+
$this->assertMatchesRegularExpression(
51+
'/^[A-Za-z0-9_]+$/',
52+
$this->resolveCookieName($name),
53+
"Cookie name for [$name] contained unsafe characters.",
54+
);
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)