{"id":7951,"date":"2020-09-02T09:24:46","date_gmt":"2020-09-02T07:24:46","guid":{"rendered":"https:\/\/www.recolize.com\/?p=7951"},"modified":"2020-09-17T11:06:48","modified_gmt":"2020-09-17T09:06:48","slug":"browsers-restrict-cookies-2020","status":"publish","type":"post","link":"https:\/\/www.recolize.com\/en\/blog\/browsers-restrict-cookies-2020\/","title":{"rendered":"How Browsers Restrict Cookies in 2020"},"content":{"rendered":"<section class=\"section  text-normal section-text-no-shadow section-inner-no-shadow section-normal section-opaque\"  >\n    \n    <div class=\"background-overlay grid-overlay-0 \" style=\"background-color: rgba(0,0,0,0);\"><\/div>\n\n    <div class=\"container container-vertical-default\">\n        <div class=\"row vertical-default\">\n            <div class=\"col-md-12     text-default small-screen-default\"  ><div class=\"col-text-1 text-normal  element-top-20 element-bottom-20\" data-os-animation=\"none\" data-os-animation-delay=\"0s\">\n    <p>A lot of things are changing in the online world these days at a very fast pace. Especially the trend regarding privacy concerns that has widely been started with the GDPR in Europe, now makes its way to the rest of the world. As a result more and more browser vendors are starting to put privacy at their top priority. This results in a lot of changes on how browsers restrict cookies which we will have a deeper look at.<\/p>\n<p><!--more--><\/p>\n<p>Note: if you think it&#8217;s a smart idea to circumvent cookies by using browser local storage, please be aware that browser vendors are also imposing restrictions on them, so this won&#8217;t be a long-lasting solution. In addition local storage is a same origin solution, which means you will have different storages for www.recolize.com and tool.recolize.com.<\/p>\n<h2>The goal of browser privacy<\/h2>\n<p>Apple&#8217;s Safari was one of the first browsers enabling more privacy-friendly settings by default. With their so-called &#8220;<a href=\"https:\/\/webkit.org\/blog\/7675\/intelligent-tracking-prevention\/\" target=\"_blank\" rel=\"noopener noreferrer\">Intelligent Tracking Prevention<\/a>&#8221; logic (ITP) in 2017 they started to limit tracking across different sites. Lately in 2020 also they are the ones pushing mobile advertisers to find new solutions for example by <a href=\"https:\/\/www.wired.com\/story\/apple-ios-14-safari-privacy-ad-tracking\/\" target=\"_blank\" rel=\"noopener noreferrer\">restricting the use of the App Advertising ID<\/a> on iPhones in iOS 14.<br \/>\nOther browser vendors like Google Chrome or Mozilla Firefox are also already doing similar approaches or looking into it.<\/p>\n<p>Basically what all measures by the browser vendors have in common is to limit tracking of their users across different sites. Therefore there are 3 basic concepts that all browsers more or less do follow:<\/p>\n<ol>\n<li>Drastically limiting expiry time of third-party cookies<\/li>\n<li>Prohibiting cookies in unsecure contexts<\/li>\n<li>Limiting expiry times of content created in JavaScript context (e.g. cookies or browser local storage)<\/li>\n<\/ol>\n<p>As it is very difficult to stay on top on how the different browsers restrict cookies and local storage, the website <a href=\"https:\/\/www.cookiestatus.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">cookiestatus.com<\/a> is a very good information source:<\/p>\n<\/div>\n<div class=\"figure element-top-0 element-bottom-0  \" data-os-animation=\"none\" data-os-animation-delay=\"0s\">\n            <a href=\"https:\/\/www.recolize.com\/wp-content\/uploads\/cookiestatus_overview.png\" class=\"figure-image magnific\" target=\"_self\">\n        <img width=\"1751\" height=\"826\" src=\"https:\/\/www.recolize.com\/wp-content\/uploads\/cookiestatus_overview.png\" class=\"attachment-full size-full\" alt=\"\" decoding=\"async\" loading=\"lazy\" srcset=\"https:\/\/www.recolize.com\/wp-content\/uploads\/cookiestatus_overview.png 1751w, https:\/\/www.recolize.com\/wp-content\/uploads\/cookiestatus_overview-300x142.png 300w, https:\/\/www.recolize.com\/wp-content\/uploads\/cookiestatus_overview-1024x483.png 1024w, https:\/\/www.recolize.com\/wp-content\/uploads\/cookiestatus_overview-768x362.png 768w, https:\/\/www.recolize.com\/wp-content\/uploads\/cookiestatus_overview-1536x725.png 1536w\" sizes=\"(max-width: 1751px) 100vw, 1751px\" \/>            <\/a>\n    <\/div>\n<div class=\"col-text-1 text-normal  element-top-20 element-bottom-20\" data-os-animation=\"none\" data-os-animation-delay=\"0s\">\n    <h2>What to do with your cookies now ?<\/h2>\n<p>Basically there are 3 important cookie attributes:<\/p>\n<ol>\n<li><code>HttpOnly<\/code>: this cookie attribute guarantees that the cookie cannot be access by JavaScript at all.<\/li>\n<li><code>Secure<\/code>: this cookie attribute transmits cookies only in case that you are accessing the URL via HTTPS<\/li>\n<li><code>SameSite<\/code>:<br \/>\nThe attribute can have the values <code>Strict<\/code>, <code>Lax<\/code> or <code>None<\/code> that tell the browser in which context the cookie is allowed to be accessed, with the first one being restricted to the same site requests. Whereas the latter has no restrictions for cross site requests, that means the cookie will be attached to any request which imposes the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Glossary\/CSRF\" target=\"_blank\" rel=\"noopener noreferrer\">security risk of CRSF<\/a>.<br \/>\nFor a detailed explanation see the <a href=\"https:\/\/web.dev\/samesite-cookies-explained\/\" target=\"_blank\" rel=\"noopener noreferrer\">excellent post on the Google Blog<\/a> and the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/Headers\/Set-Cookie\/SameSite\" target=\"_blank\" rel=\"noopener noreferrer\">Mozilla documentation<\/a>.<\/li>\n<\/ol>\n<p>With the information derived from above we can conclude the following measures for your cookies:<\/p>\n<ul>\n<li>Use first-party cookies whereever possible<\/li>\n<li>Session cookies, e.g. for your shopping cart, should be set to <code>HttpOnly<\/code>, <code>Secure<\/code> and <code>SameSite=None<\/code>. This prevents the browsers from blocking unsecure cookies while still allowing cookies after being redirected from external links so that the shopping cart remains available.<\/li>\n<li>Session cookies e.g. for your secure admin interface should be set to <code>HttpOnly<\/code>, <code>Secure<\/code> and <code>SameSite=Strict<\/code>. In contrast to the session cookies above this does not allow requests from non-first party context to steal your session.<\/li>\n<li>all other cookies, especially the ones required by JavaScript, should at least be set to <code>Secure<\/code> and <code>SameSite=None<\/code>. If these cookies are created via JavaScript, the expiry time will be only 7 days e.g. in Safari browser in general.<\/li>\n<\/ul>\n<p>Please also pay attention to the fact that browsers do have different default settings for cookies that are missing e.g. the <code>SameSite<\/code> attribute. Newer browser versions of Firefox and Chrome do set <code>SameSite=Lax<\/code> by default which could result in issues e.g. that your Magento shopping carts gets cleared after redirect from payment provider!<\/p>\n<h2>How to implement the cookie attributes in PHP?<\/h2>\n<p>So as written above to mitigate how browser restrict cookies, in most cases you are good to go if you set your cookies to <code>Secure<\/code> and <code>SameSite=Lax<\/code>. In PHP starting from 7.3 this can be done very easily by using the following method:<\/p>\n<\/div>\n<div class=\" element-top-20 element-bottom-20\" data-os-animation=\"none\" data-os-animation-delay=\"0s\">\n    <pre>setcookie(&#039;cookie-name&#039;, &#039;value&#039;, [ &#039;secure&#039; =&amp;gt; true, &#039;samesite&#039; =&amp;gt; &#039;None&#039; ]);<\/pre>\n<\/div><div class=\"col-text-1 text-normal  element-top-20 element-bottom-20\" data-os-animation=\"none\" data-os-animation-delay=\"0s\">\n    <p>If you are using an older PHP version below 7.3 (which you obviously should not do <a href=\"https:\/\/www.php.net\/supported-versions.php\" target=\"_blank\" rel=\"noopener noreferrer\">from a security perspective<\/a>) the method parameters are a little bit different and you have to &#8220;tweak&#8221; it a little bit like this:<\/p>\n<\/div>\n<div class=\" element-top-20 element-bottom-20\" data-os-animation=\"none\" data-os-animation-delay=\"0s\">\n    <pre>setcookie(&#039;cookie-name&#039;, &#039;value&#039;, 0, &#039;\/; SameSite=Lax; Secure&#039;, &#039;&#039;, true);<\/pre>\n<\/div><div class=\"col-text-1 text-normal  element-top-20 element-bottom-20\" data-os-animation=\"none\" data-os-animation-delay=\"0s\">\n    <p>As many of our customers are still using the popular <a href=\"https:\/\/www.recolize.com\/en\/integrations\/magento-1\/\">Magento 1 online shop software<\/a> here is a small trick you can use:<br \/>\nUpdate Magento&#8217;s <code>core_config_data<\/code> table and set <code>web\/cookie\/cookie_path<\/code> value to <code>\/; SameSite=None; Secure<\/code><br \/>\nThis is not a permanent solution, but it might be useful for a temporary workaround.<\/p>\n<\/div>\n<div class=\"col-text-1 text-normal  element-top-20 element-bottom-20\" data-os-animation=\"none\" data-os-animation-delay=\"0s\">\n    <p>Of course this blog post can only highlight some changes of how browsers restrict cookies in 2020 and this is also subject to constant change. But you should have gotten a glimpse on where the trend goes and what to do to update your implementation.<\/p>\n<\/div>\n<\/div>        <\/div>\n    <\/div>\n<\/section>\n\n","protected":false},"excerpt":{"rendered":"<p>A lot of things are changing in the online world these days at a very fast pace. Especially the trend regarding privacy concerns that has widely been started with the GDPR in Europe, now makes its way to the rest of the world. As a result more and more browser vendors are starting to put<\/p>\n","protected":false},"author":3,"featured_media":7970,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[40],"tags":[],"_links":{"self":[{"href":"https:\/\/www.recolize.com\/en\/wp-json\/wp\/v2\/posts\/7951"}],"collection":[{"href":"https:\/\/www.recolize.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.recolize.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.recolize.com\/en\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.recolize.com\/en\/wp-json\/wp\/v2\/comments?post=7951"}],"version-history":[{"count":17,"href":"https:\/\/www.recolize.com\/en\/wp-json\/wp\/v2\/posts\/7951\/revisions"}],"predecessor-version":[{"id":7993,"href":"https:\/\/www.recolize.com\/en\/wp-json\/wp\/v2\/posts\/7951\/revisions\/7993"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.recolize.com\/en\/wp-json\/wp\/v2\/media\/7970"}],"wp:attachment":[{"href":"https:\/\/www.recolize.com\/en\/wp-json\/wp\/v2\/media?parent=7951"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.recolize.com\/en\/wp-json\/wp\/v2\/categories?post=7951"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.recolize.com\/en\/wp-json\/wp\/v2\/tags?post=7951"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}