Highlights

Contents

Part VI
06

SameSite and third-party cookies

We learn how the browser decides whether a cross-site request carries a cookie. SameSite is the setting that quietly blocks most CSRF and most cross-site tracking, but it has gaps that attackers know well. We will separate first-party from third-party cookies, compare Strict, Lax and None, and list the cases Lax does not cover.

CHAPTER ATLAS / FOLLOW THE READING PATHCHAPTER ATLAS / FOLLOW THE READING PATH1Origins and thesame-origin policy2CORS: simple andpreflight requests3CORS credentials andcaching4CORS vulnerabilities5Cookies6SameSite and third-partycookies7CSRF8The browser boundary endto end

#Section 19First-party and third-party cookies

Whether a cookie counts as first-party or third-party depends on where it is being sent, not on who set it. A cookie is first-party when it goes to the site shown in the address bar. It is third-party when it goes to a different site, typically because the page embeds something from that site: an ad iframe, a social “like” button, an analytics script or a one-pixel tracking image. The same cookie can be first-party on one request and third-party on the next.

Tracking works through third-party cookies. Suppose tracker.example is embedded on both news.example and shop.example. When you read the news, the tracker’s embed sets a cookie with a random id. When you later visit the shop, the shop’s page loads the tracker’s embed again, and the browser sends the same cookie along. The tracker now knows that the person who read that article also looked at those shoes, and with enough embeds it builds a profile of your browsing across thousands of sites.

Browsers responded. Safari has blocked third-party cookies by default since 2020, and Firefox partitions them so that the tracker gets a separate cookie jar for each top-level site. Chrome announced their removal in 2020, delayed it several times, and in 2024 and 2025 dropped the plan in favour of user controls. For a backend engineer the lesson is practical rather than political. Never design a login or checkout that depends on a third-party cookie, such as an identity iframe embedded in a partner’s page, because a large share of users will not send it.

#Section 20SameSite: Strict, Lax and None

The SameSite attribute tells the browser whether to send a cookie on cross-site requests. It has three values. With Strict, the cookie goes only on same-site requests. Even clicking a link to Wren from an email or a search result arrives without the cookie, so the user looks logged out on the first page and then logged in after the next click, which confuses people. With Lax, the cookie also goes on cross-site top-level navigations that use safe methods, meaning link clicks, typed URLs and GET redirects. It is not sent on cross-site POST forms, iframes, images, scripts or fetches. With None, the cookie goes on every request, cross-site or not, and browsers require the Secure attribute alongside it.

WHICH CROSS-SITE REQUESTS CARRY THE COOKIE, BY SameSite VALUEWHICH CROSS-SITE REQUESTS CARRY THE COOKIE, BY SameSite VALUEStrictLaxNone; Secureevillink click (top-level GET)form POST from evil.examplefetch, img, iframe from evilsame-site requests always carry it; "site" ignores subdomains
Figure 2.25Strict sends only same-site, Lax adds link clicks, None sends everything.

Since 2020 Chromium has treated a cookie with no SameSite attribute as Lax, and other browsers followed with their own versions of the default. That single change blocked the classic CSRF attack, a hidden form that POSTs to the target, across most of the web, without any site changing its code. It also broke a fair number of single sign-on and payment flows that relied on cookies arriving with cross-site POSTs, which had to be updated to SameSite=None; Secure.

Wren uses two values for two jobs. The session cookie is SameSite=Lax, so a user following a link from an email arrives logged in. The CSRF token cookie from Part VII is SameSite=Strict, because it never needs to travel with a cross-site request.

#Section 21Where Lax still leaks

Lax blocks forged cross-site POSTs, but four gaps remain, and each has caused real attacks. First, a GET that changes state still works. If Wren had GET /orders/7/cancel, the attacker’s page could redirect the browser there with window.location, and since that is a top-level GET navigation, the Lax session cookie goes along and the order is cancelled. Section 5’s rule about keeping GET safe is also a security rule.

Second, sibling subdomains are same-site, as the note above says. An attacker with script running on blog.wren.example can send any request to the API with cookies attached, and SameSite does not even notice. Third, browser support is not universal. Old browsers ignore SameSite entirely, and some embedded web views behave differently from the browser on the same device. Fourth, Chrome shipped a compatibility exception called “Lax plus POST”, which let a cookie set within the last two minutes, and having no explicit SameSite, travel on a cross-site top-level POST. It existed to keep single sign-on flows working, and it gave attackers a two-minute window right after login.

WHERE SameSite=Lax STILL LETS A FORGED REQUEST THROUGHWHERE SameSite=Lax STILL LETS A FORGED REQUEST THROUGHGET that writes/cancel?id=7top-level navigationcarries Lax cookiessibling subdomainblogapisame site as api:SameSite does not applybrowser exceptionsolder browsers ignore it;Chrome once allowed POSTin the first 2 minutesSameSite reduces CSRF; it does not replace a token or an Origin check
Figure 2.26Three gaps that survive SameSite=Lax. Each needs a defence from Part VII.

The conclusion is that SameSite is a strong default layer, not a complete CSRF defence. Keep GET requests free of side effects, treat each subdomain as a separate trust zone with host-only cookies, and add an Origin check or a token on every state-changing request.