Skip to content

Commit 6ae8c9a

Browse files
authored
[11.x] Auto-secure cookies (#52422)
* [11.x] Auto-secure cookies * Add tests
1 parent 5317d40 commit 6ae8c9a

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

src/Illuminate/Session/Middleware/StartSession.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ protected function addCookieToResponse(Response $response, Session $session)
224224
$this->getCookieExpirationDate(),
225225
$config['path'],
226226
$config['domain'],
227-
$config['secure'] ?? false,
227+
$config['secure'],
228228
$config['http_only'] ?? true,
229229
false,
230230
$config['same_site'] ?? null,

tests/Http/HttpResponseTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,24 @@ public function testWithCookie()
9090
$this->assertSame('bar', $cookies[0]->getValue());
9191
}
9292

93+
public function testResponseCookiesInheritRequestSecureState()
94+
{
95+
$cookie = Cookie::create('foo', 'bar');
96+
97+
$response = new Response('foo');
98+
$response->headers->setCookie($cookie);
99+
100+
$request = Request::create('/', 'GET');
101+
$response->prepare($request);
102+
103+
$this->assertFalse($cookie->isSecure());
104+
105+
$request = Request::create('https://localhost/', 'GET');
106+
$response->prepare($request);
107+
108+
$this->assertTrue($cookie->isSecure());
109+
}
110+
93111
public function testGetOriginalContent()
94112
{
95113
$arr = ['foo' => 'bar'];

tests/Integration/Session/CookieSessionHandlerTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,25 @@ public function testCookieSessionDriverCookiesCanExpireOnClose()
2020
$this->assertEquals(0, $sessionValueCookie->getExpiresTime());
2121
}
2222

23+
public function testCookieSessionInheritsRequestSecureState()
24+
{
25+
Route::get('/', fn () => '')->middleware('web');
26+
27+
$unsecureResponse = $this->get('/');
28+
$unsecureSessionIdCookie = $unsecureResponse->getCookie('laravel_session');
29+
$unsecureSessionValueCookie = $unsecureResponse->getCookie($unsecureSessionIdCookie->getValue());
30+
31+
$this->assertFalse($unsecureSessionIdCookie->isSecure());
32+
$this->assertFalse($unsecureSessionValueCookie->isSecure());
33+
34+
$secureResponse = $this->get('https://localhost/');
35+
$secureSessionIdCookie = $secureResponse->getCookie('laravel_session');
36+
$secureSessionValueCookie = $secureResponse->getCookie($secureSessionIdCookie->getValue());
37+
38+
$this->assertTrue($secureSessionIdCookie->isSecure());
39+
$this->assertTrue($secureSessionValueCookie->isSecure());
40+
}
41+
2342
protected function defineEnvironment($app)
2443
{
2544
$app['config']->set('app.key', Str::random(32));

0 commit comments

Comments
 (0)