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

[5.2] Set HttpOnly flag #12809

Closed
wants to merge 7 commits into from
Closed
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
23 changes: 16 additions & 7 deletions src/Illuminate/Cookie/CookieJar.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ class CookieJar implements JarContract
*/
protected $secure = false;

/**
* The default httpOnly setting (defaults to true).
*
* @var bool
*/
protected $httpOnly = true;
Copy link
Member

Choose a reason for hiding this comment

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

As I mentioned in #12818 (comment)

Making this default to true while previously it's default to false in a patch release should be consider as breaking change.

Copy link
Author

Choose a reason for hiding this comment

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

The default value was true, and it can be override by passing another value to make function. Now all we make that we read this value from session config

Copy link
Member

Choose a reason for hiding this comment

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

The default value was true

How can you say that when the changelog clearly shows the opposite.

screen shot 2016-03-23 at 10 44 27 am

Copy link
Member

Choose a reason for hiding this comment

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

and it can be override by passing another value to make function.

When anyone upgrade from say v5.2.24 to v5.2.25 (if this get accepted). People shouldn't be worry about any configuration change and everything should works as it was before.

Copy link
Author

Choose a reason for hiding this comment

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

OK that,s right . I,ll update the cookie service provider file to keep it as previously settings.


/**
* All of the cookies queued for sending.
*
Expand All @@ -48,9 +55,9 @@ class CookieJar implements JarContract
* @param bool $httpOnly
* @return \Symfony\Component\HttpFoundation\Cookie
*/
public function make($name, $value, $minutes = 0, $path = null, $domain = null, $secure = false, $httpOnly = true)
public function make($name, $value, $minutes = 0, $path = null, $domain = null, $secure = null, $httpOnly = null)
{
list($path, $domain, $secure) = $this->getPathAndDomain($path, $domain, $secure);
list($path, $domain, $secure, $httpOnly) = $this->getPathAndDomain($path, $domain, $secure, $httpOnly);

$time = ($minutes == 0) ? 0 : time() + ($minutes * 60);

Expand All @@ -68,7 +75,7 @@ public function make($name, $value, $minutes = 0, $path = null, $domain = null,
* @param bool $httpOnly
* @return \Symfony\Component\HttpFoundation\Cookie
*/
public function forever($name, $value, $path = null, $domain = null, $secure = false, $httpOnly = true)
public function forever($name, $value, $path = null, $domain = null, $secure = null, $httpOnly = null)
{
return $this->make($name, $value, 2628000, $path, $domain, $secure, $httpOnly);
}
Expand Down Expand Up @@ -143,11 +150,12 @@ public function unqueue($name)
* @param string $path
* @param string $domain
* @param bool $secure
* @param bool $httpOnly
* @return array
*/
protected function getPathAndDomain($path, $domain, $secure = false)
protected function getPathAndDomain($path, $domain, $secure = null, $httpOnly = null)
{
return [$path ?: $this->path, $domain ?: $this->domain, $secure ?: $this->secure];
return [$path ?: $this->path, $domain ?: $this->domain, isset($secure) ? $secure : $this->secure, isset($httpOnly) ? $httpOnly : $this->httpOnly];
}

/**
Expand All @@ -156,11 +164,12 @@ protected function getPathAndDomain($path, $domain, $secure = false)
* @param string $path
* @param string $domain
* @param bool $secure
* @param bool $httpOnly
* @return $this
*/
public function setDefaultPathAndDomain($path, $domain, $secure = false)
public function setDefaultPathAndDomain($path, $domain, $secure = false, $httpOnly = true)
{
list($this->path, $this->domain, $this->secure) = [$path, $domain, $secure];
list($this->path, $this->domain, $this->secure, $this->httpOnly) = [$path, $domain, $secure, $httpOnly];

return $this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Cookie/CookieServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function register()
$this->app->singleton('cookie', function ($app) {
$config = $app['config']['session'];

return (new CookieJar)->setDefaultPathAndDomain($config['path'], $config['domain'], $config['secure']);
return (new CookieJar)->setDefaultPathAndDomain($config['path'], $config['domain'], $config['secure'], data_get($config, 'http_only', true));
});
}
}
7 changes: 3 additions & 4 deletions src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Illuminate\Foundation\Http\Middleware;

use Closure;
use Illuminate\Support\Arr;
use Illuminate\Foundation\Application;
use Symfony\Component\HttpFoundation\Cookie;
use Illuminate\Contracts\Encryption\Encrypter;
use Illuminate\Session\TokenMismatchException;

Expand Down Expand Up @@ -133,9 +133,8 @@ protected function addCookieToResponse($request, $response)
$config = config('session');

$response->headers->setCookie(
new Cookie(
'XSRF-TOKEN', $request->session()->token(), time() + 60 * 120,
$config['path'], $config['domain'], $config['secure'], false
cookie()->make(
'XSRF-TOKEN', $request->session()->token(), 120, null, null, null, Arr::get($config, 'http_only', false)
)
);

Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Foundation/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ function config_path($path = '')
* @param bool $httpOnly
* @return \Symfony\Component\HttpFoundation\Cookie
*/
function cookie($name = null, $value = null, $minutes = 0, $path = null, $domain = null, $secure = false, $httpOnly = true)
function cookie($name = null, $value = null, $minutes = 0, $path = null, $domain = null, $secure = null, $httpOnly = null)
{
$cookie = app(CookieFactory::class);

Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Session/Middleware/StartSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ protected function addCookieToResponse(Response $response, SessionInterface $ses
if ($this->sessionIsPersistent($config = $this->manager->getSessionConfig())) {
$response->headers->setCookie(new Cookie(
$session->getName(), $session->getId(), $this->getCookieExpirationDate(),
$config['path'], $config['domain'], Arr::get($config, 'secure', false)
$config['path'], $config['domain'], Arr::get($config, 'secure', false), Arr::get($config, 'http_only', true)
));
}
}
Expand Down