Skip to content

Instantly share code, notes, and snippets.

@jedimdan
Last active June 21, 2021 09:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jedimdan/e6c78a2665f788eda9a3e3f3b4e1f982 to your computer and use it in GitHub Desktop.
Save jedimdan/e6c78a2665f788eda9a3e3f3b4e1f982 to your computer and use it in GitHub Desktop.
A Laravel middleware that will set cookies to SameSite=None and account for incompatible clients. Note that it relies on https://github.com/jenssegers/agent as a dependency
<?php
namespace App\Http\Middleware;
use Closure;
use Jenssegers\Agent\Agent;
class SameSiteNone
{
/**
* Sets SameSite=None while checking for incompatible browsers
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
// parse the User Agent
$agent = new Agent;
$browser = $agent->browser();
$browserVersion = $agent->version($browser, Agent::VERSION_TYPE_FLOAT);
// check for incompatible browsers based on https://www.chromium.org/updates/same-site/incompatible-clients
$browserIsCompatible = true;
if ($browser == 'Safari' && $agent->match('Mac OS X 10_14_*|iP.+; CPU .*OS 12_')) {
$browserIsCompatible = false;
} elseif ($browser == 'Chrome' && $browserVersion > 50 && $browserVersion < 67) {
$browserIsCompatible = false;
} elseif ($browser == 'UCBrowser' && $browserVersion < 12.13) {
$browserIsCompatible = false;
}
// set SameSite none to supported browsers only
if ($browserIsCompatible) {
config(['session.secure' => true]);
config(['session.same_site' => 'none']);
} else {
config(['session.secure' => false]);
config(['session.same_site' => null]);
}
return $next($request);
}
}
@muhammadasfar
Copy link

@jedimdan is this a working solution for ohmybrew shopify-laravel apps?

@jedimdan
Copy link
Author

jedimdan commented Jan 31, 2020

@muhammadasfar it's working in as far as I'm using it on my production. You might also want to check out this PR that we are working on gnikyt/laravel-shopify#375

@muhammadasfar
Copy link

@jedimdan Thanks

Yes, I know that you guys are working hard over it and I really appreciate it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment