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

Fixes #2157, Explicitly set SameSite value for cookies #2159

Merged
merged 5 commits into from
Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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 composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"php": ">=7.2",
"axy/sourcemap": "^0.1.4",
"components/font-awesome": "5.9.*",
"dflydev/fig-cookies": "^1.0.2",
"dflydev/fig-cookies": "^2.0.1",
"doctrine/dbal": "^2.7",
"franzl/whoops-middleware": "^0.4.0",
"illuminate/bus": "5.7.*",
Expand Down
18 changes: 18 additions & 0 deletions src/Http/CookieFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Flarum\Http;

use Dflydev\FigCookies\Modifier\SameSite;
use Dflydev\FigCookies\SetCookie;
use Flarum\Foundation\Application;
use Illuminate\Support\Arr;
Expand Down Expand Up @@ -43,6 +44,13 @@ class CookieFactory
*/
protected $secure;

/**
* Same Site cookie value.
*
* @var string
*/
protected $samesite;

/**
* @param Application $app
*/
Expand All @@ -56,6 +64,7 @@ public function __construct(Application $app)
$this->path = $app->config('cookie.path', Arr::get($url, 'path') ?: '/');
$this->domain = $app->config('cookie.domain');
$this->secure = $app->config('cookie.secure', Arr::get($url, 'scheme') === 'https');
$this->samesite = $app->config('cookie.samesite', 'lax');
}

/**
Expand Down Expand Up @@ -85,6 +94,15 @@ public function make($name, $value = null, $maxAge = null)
$cookie = $cookie->withDomain($this->domain);
}

// Explicitly set SameSite value, use sensible default if no value provided
if ($this->samesite === 'strict') {
$cookie = $cookie->withSameSite(SameSite::strict());
} elseif ($this->samesite === 'none') {
$cookie = $cookie->withSameSite(SameSite::none());
} else {
$cookie = $cookie->withSameSite(SameSite::lax());
}
tankerkiller125 marked this conversation as resolved.
Show resolved Hide resolved

return $cookie
->withPath($this->path)
->withSecure($this->secure)
Expand Down