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

[11.x] Auto-secure cookies #52422

Merged
merged 2 commits into from
Aug 8, 2024
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
2 changes: 1 addition & 1 deletion src/Illuminate/Session/Middleware/StartSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ protected function addCookieToResponse(Response $response, Session $session)
$this->getCookieExpirationDate(),
$config['path'],
$config['domain'],
$config['secure'] ?? false,
$config['secure'],
Copy link
Contributor

@Jubeki Jubeki Aug 9, 2024

Choose a reason for hiding this comment

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

Wouldn't it be better if it has a default value:

$config['secure'] ?? null,

Because if the config key currently does not exist, it would break existing applications. (I think it is present in almost all applications, but the default value was probably there for a reason)

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay, I just thought about it again, the ?? false was there, because of the default null value for the secure key. So should be fine to be left out completely.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. Back in the days, Symfony Cookie didn't support null for secure. null was introduced for this "auto-secure" behaviour.

$config['http_only'] ?? true,
false,
$config['same_site'] ?? null,
Expand Down
18 changes: 18 additions & 0 deletions tests/Http/HttpResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,24 @@ public function testWithCookie()
$this->assertSame('bar', $cookies[0]->getValue());
}

public function testResponseCookiesInheritRequestSecureState()
{
$cookie = Cookie::create('foo', 'bar');

$response = new Response('foo');
$response->headers->setCookie($cookie);

$request = Request::create('/', 'GET');
$response->prepare($request);

$this->assertFalse($cookie->isSecure());

$request = Request::create('https://localhost/', 'GET');
$response->prepare($request);

$this->assertTrue($cookie->isSecure());
}

public function testGetOriginalContent()
{
$arr = ['foo' => 'bar'];
Expand Down
19 changes: 19 additions & 0 deletions tests/Integration/Session/CookieSessionHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@ public function testCookieSessionDriverCookiesCanExpireOnClose()
$this->assertEquals(0, $sessionValueCookie->getExpiresTime());
}

public function testCookieSessionInheritsRequestSecureState()
{
Route::get('/', fn () => '')->middleware('web');

$unsecureResponse = $this->get('/');
$unsecureSessionIdCookie = $unsecureResponse->getCookie('laravel_session');
$unsecureSessionValueCookie = $unsecureResponse->getCookie($unsecureSessionIdCookie->getValue());

$this->assertFalse($unsecureSessionIdCookie->isSecure());
$this->assertFalse($unsecureSessionValueCookie->isSecure());

$secureResponse = $this->get('https://localhost/');
$secureSessionIdCookie = $secureResponse->getCookie('laravel_session');
$secureSessionValueCookie = $secureResponse->getCookie($secureSessionIdCookie->getValue());

$this->assertTrue($secureSessionIdCookie->isSecure());
$this->assertTrue($secureSessionValueCookie->isSecure());
}

protected function defineEnvironment($app)
{
$app['config']->set('app.key', Str::random(32));
Expand Down