Skip to content

Commit b6e7472

Browse files
authored
Update samesite content
1 parent 3e92613 commit b6e7472

File tree

1 file changed

+48
-48
lines changed

1 file changed

+48
-48
lines changed

6-data-storage/01-cookie/article.md

+48-48
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
Cookies are small strings of data that are stored directly in the browser. They are a part of the HTTP protocol, defined by the [RFC 6265](https://tools.ietf.org/html/rfc6265) specification.
44

5-
Cookies are usually set by a web-server using the response `Set-Cookie` HTTP-header. Then, the browser automatically adds them to (almost) every request to the same domain using the `Cookie` HTTP-header.
5+
Cookies are usually set by a web server using the response `Set-Cookie` HTTP header. Then, the browser automatically adds them to (almost) every request to the same domain using the `Cookie` HTTP header.
66

77
One of the most widespread use cases is authentication:
88

9-
1. Upon sign-in, the server uses the `Set-Cookie` HTTP-header in the response to set a cookie with a unique "session identifier".
10-
2. Next time the request is sent to the same domain, the browser sends the cookie over the net using the `Cookie` HTTP-header.
9+
1. Upon sign-in, the server uses the `Set-Cookie` HTTP header in the response to set a cookie with a unique "session identifier".
10+
2. Next time the request is sent to the same domain, the browser sends the cookie over the net using the `Cookie` HTTP header.
1111
3. So the server knows who made the request.
1212

1313
We can also access cookies from the browser, using `document.cookie` property.
1414

15-
There are many tricky things about cookies and their options. In this chapter, we'll cover them in detail.
15+
There are many tricky things about cookies and their attributes. In this chapter, we'll cover them in detail.
1616

1717
## Reading from document.cookie
1818

@@ -73,9 +73,9 @@ There are a few limitations:
7373
- The total number of cookies per domain is limited to around 20+, the exact limit depends on the browser.
7474
```
7575

76-
Cookies have several options, many of which are important and should be set.
76+
Cookies have several attributes, many of which are important and should be set.
7777

78-
The options are listed after `key=value`, delimited by `;`, like this:
78+
The attributes are listed after `key=value`, delimited by `;`, like this:
7979

8080
```js run
8181
document.cookie = "user=John; path=/; expires=Tue, 19 Jan 2038 03:14:07 GMT"
@@ -105,7 +105,7 @@ alert(document.cookie); // no user
105105

106106
...But this can be changed. If we'd like to allow subdomains like `forum.site.com` to get a cookie set at `site.com`, that's possible.
107107

108-
For that to happen, when setting a cookie at `site.com`, we should explicitly set the `domain` option to the root domain: `domain=site.com`. Then all subdomains will see such a cookie.
108+
For that to happen, when setting a cookie at `site.com`, we should explicitly set the `domain` attribute to the root domain: `domain=site.com`. Then all subdomains will see such a cookie.
109109

110110
For example:
111111

@@ -124,23 +124,23 @@ alert(document.cookie); // has cookie user=John
124124
Historically, `domain=.site.com` (with a dot before `site.com`) used to work the same way, allowing access to the cookie from subdomains. Leading dots in domain names are now ignored, but some browsers may decline to set the cookie containing such dots.
125125
```
126126

127-
To summarize, the `domain` option allows to make a cookie accessible at subdomains.
127+
To summarize, the `domain` attribute allows to make a cookie accessible at subdomains.
128128

129129
## path
130130

131131
- **`path=/mypath`**
132132

133-
The url path prefix must be absolute. It makes the cookie accessible for pages under that path. By default, it's the current path.
133+
The URL path prefix must be absolute. It makes the cookie accessible for pages under that path. By default, it's the current path.
134134

135135
If a cookie is set with `path=/admin`, it's visible on pages `/admin` and `/admin/something`, but not at `/home`, `/home/admin` or `/`.
136136

137-
Usually, we should set `path` to the root: `path=/` to make the cookie accessible from all website pages. If this option is not set the default is calculated using [this method](https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#path_default_value).
137+
Usually, we should set `path` to the root: `path=/` to make the cookie accessible from all website pages. If this attribute is not set the default is calculated using [this method](https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#path_default_value).
138138

139139
## expires, max-age
140140

141-
By default, if a cookie doesn't have one of these options, it disappears when the browser/tab is closed. Such cookies are called "session cookies"
141+
By default, if a cookie doesn't have one of these attributes, it disappears when the browser/tab is closed. Such cookies are called "session cookies"
142142

143-
To let cookies survive a browser close, we can set either the `expires` or `max-age` option. `max-Age` has precedence if both are set.
143+
To let cookies survive a browser close, we can set either the `expires` or `max-age` attribute. `max-Age` has precedence if both are set.
144144

145145
- **`expires=Tue, 19 Jan 2038 03:14:07 GMT`**
146146

@@ -181,7 +181,7 @@ The cookie should be transferred only over HTTPS.
181181

182182
That is, cookies are domain-based, they do not distinguish between the protocols.
183183

184-
With this option, if a cookie is set by `https://site.com`, then it doesn't appear when the same site is accessed by HTTP, as `http://site.com`. So if a cookie has sensitive content that should never be sent over unencrypted HTTP, the `secure` flag is the right thing.
184+
With this attribute, if a cookie is set by `https://site.com`, then it doesn't appear when the same site is accessed by HTTP, as `http://site.com`. So if a cookie has sensitive content that should never be sent over unencrypted HTTP, the `secure` flag is the right thing.
185185

186186
```js
187187
// assuming we're on https:// now
@@ -191,7 +191,7 @@ document.cookie = "user=John; secure";
191191

192192
## samesite
193193

194-
That's another security attribute `samesite`. It's designed to protect from so-called XSRF (cross-site request forgery) attacks.
194+
This is another security attribute `samesite`. It's designed to protect from so-called XSRF (cross-site request forgery) attacks.
195195

196196
To understand how it works and when it's useful, let's take a look at XSRF attacks.
197197

@@ -205,15 +205,15 @@ The browser sends cookies every time you visit the site `bank.com`, even if the
205205

206206
![](cookie-xsrf.svg)
207207

208-
That's a so-called "Cross-Site Request Forgery" (in short, XSRF) attack.
208+
This is a so-called "Cross-Site Request Forgery" (in short, XSRF) attack.
209209

210210
Real banks are protected from it of course. All forms generated by `bank.com` have a special field, a so-called "XSRF protection token", that an evil page can't generate or extract from a remote page. It can submit a form there, but can't get the data back. The site `bank.com` checks for such a token in every form it receives.
211211

212212
Such a protection takes time to implement though. We need to ensure that every form has the required token field, and we must also check all requests.
213213

214-
### Enter cookie samesite option
214+
### Use cookie samesite attribute
215215

216-
The cookie `samesite` option provides another way to protect from such attacks, that (in theory) should not require "xsrf protection tokens".
216+
The cookie `samesite` attribute provides another way to protect from such attacks, that (in theory) should not require "xsrf protection tokens".
217217

218218
It has two possible values:
219219

@@ -223,9 +223,9 @@ A cookie with `samesite=strict` is never sent if the user comes from outside the
223223

224224
In other words, whether a user follows a link from their email, submits a form from `evil.com`, or does any operation that originates from another domain, the cookie is not sent.
225225

226-
If authentication cookies have the `samesite` option, then an XSRF attack has no chance of succeeding, because a submission from `evil.com` comes without cookies. So `bank.com` will not recognize the user and will not proceed with the payment.
226+
If authentication cookies have the `samesite=strict` attribute, then an XSRF attack has no chance of succeeding, because a submission from `evil.com` comes without cookies. So `bank.com` will not recognize the user and will not proceed with the payment.
227227

228-
The protection is quite reliable. Only operations that come from `bank.com` will send the `samesite` cookie, e.g. a form submission from another page at `bank.com`.
228+
The protection is quite reliable. Only operations that come from `bank.com` will send the `samesite=strict` cookie, e.g. a form submission from another page at `bank.com`.
229229

230230
Although, there's a small inconvenience.
231231

@@ -246,36 +246,36 @@ A `samesite=lax` cookie is sent if both of these conditions are true:
246246

247247
2. The operation performs a top-level navigation (changes URL in the browser address bar).
248248

249-
That's usually true, but if the navigation is performed in an `<iframe>`, then it's not top-level. Also, JavaScript methods for network requests do not perform any navigation, hence they don't fit.
249+
This is usually true, but if the navigation is performed in an `<iframe>`, then it is not top-level. Additionally, JavaScript methods for network requests do not perform any navigation.
250250

251251
So, what `samesite=lax` does, is to allow the most common "go to URL" operation to have cookies. E.g. opening a website link from notes that satisfy these conditions.
252252

253253
But anything more complicated, like a network request from another site or a form submission, loses cookies.
254254

255255
If that's fine for you, then adding `samesite=lax` will probably not break the user experience and add protection.
256256

257-
Overall, `samesite` is a great option.
257+
Overall, `samesite` is a great attribute.
258258

259259
There's a drawback:
260260

261261
- `samesite` is ignored (not supported) by very old browsers, the year 2017 or so.
262262

263263
**So if we solely rely on `samesite` to provide protection, then old browsers will be vulnerable.**
264264

265-
But we surely can use `samesite` together with other protection measures, like xsrf tokens, to add layer of defence and then, in the future, when old browsers die out, we'll probably be able to drop xsrf tokens.
265+
But we surely can use `samesite` together with other protection measures, like xsrf tokens, to add a layer of defence and then, in the future, when old browsers die out, we'll probably be able to drop xsrf tokens.
266266

267267
## httpOnly
268268

269-
This option has nothing to do with JavaScript, but we have to mention it for completeness.
269+
This attribute has nothing to do with JavaScript, but we have to mention it for completeness.
270270

271-
The web-server uses the `Set-Cookie` header to set a cookie. Also, it may set the `httpOnly` option.
271+
The web server uses the `Set-Cookie` header to set a cookie. Also, it may set the `httpOnly` attribute.
272272

273-
This option forbids any JavaScript access to the cookie. We can't see such a cookie or manipulate it using `document.cookie`.
273+
This attribute forbids any JavaScript access to the cookie. We can't see such a cookie or manipulate it using `document.cookie`.
274274

275275
This is used as a precautionary measure, to protect from certain attacks when a hacker injects his own JavaScript code into a page and waits for a user to visit that page. That shouldn't be possible at all, hackers should not be able to inject their code into our site, but there may be bugs that let them do it.
276276

277277

278-
Normally, if such a thing happens, and a user visits a web-page with hacker's JavaScript code, then that code executes and gains access to `document.cookie` with user cookies containing authentication information. That's bad.
278+
Normally, if such a thing happens, and a user visits a web-page with a hacker's JavaScript code, then that code executes and gains access to `document.cookie` with user cookies containing authentication information. That's bad.
279279

280280
But if a cookie is `httpOnly`, then `document.cookie` doesn't see it, so it is protected.
281281

@@ -306,30 +306,30 @@ Here `new RegExp` is generated dynamically, to match `; name=<value>`.
306306

307307
Please note that a cookie value is encoded, so `getCookie` uses a built-in `decodeURIComponent` function to decode it.
308308

309-
### setCookie(name, value, options)
309+
### setCookie(name, value, attributes)
310310

311311
Sets the cookie's `name` to the given `value` with `path=/` by default (can be modified to add other defaults):
312312

313313
```js run
314-
function setCookie(name, value, options = {}) {
314+
function setCookie(name, value, attributes = {}) {
315315

316-
options = {
316+
attributes = {
317317
path: '/',
318318
// add other defaults here if necessary
319-
...options
319+
...attributes
320320
};
321321

322-
if (options.expires instanceof Date) {
323-
options.expires = options.expires.toUTCString();
322+
if (attributes.expires instanceof Date) {
323+
attributes.expires = attributes.expires.toUTCString();
324324
}
325325

326326
let updatedCookie = encodeURIComponent(name) + "=" + encodeURIComponent(value);
327327

328-
for (let optionKey in options) {
329-
updatedCookie += "; " + optionKey;
330-
let optionValue = options[optionKey];
331-
if (optionValue !== true) {
332-
updatedCookie += "=" + optionValue;
328+
for (let attributeKey in attributes) {
329+
updatedCookie += "; " + attributeKey;
330+
let attributeValue = attributes[attributeKey];
331+
if (attributeValue !== true) {
332+
updatedCookie += "=" + attributeValue;
333333
}
334334
}
335335

@@ -353,7 +353,7 @@ function deleteCookie(name) {
353353
```
354354

355355
```warn header="Updating or deleting must use same path and domain"
356-
Please note: when we update or delete a cookie, we should use exactly the same path and domain options as when we set it.
356+
Please note: when we update or delete a cookie, we should use exactly the same path and domain attributes as when we set it.
357357
```
358358

359359
Together: [cookie.js](cookie.js).
@@ -380,7 +380,7 @@ For instance:
380380

381381
Third-party cookies are traditionally used for tracking and ads services, due to their nature. They are bound to the originating domain, so `ads.com` can track the same user between different sites, if they all access it.
382382

383-
Naturally, some people don't like being tracked, so browsers allow to disable such cookies.
383+
Naturally, some people don't like being tracked, so browsers allow them to disable such cookies.
384384

385385
Also, some modern browsers employ special policies for such cookies:
386386
- Safari does not allow third-party cookies at all.
@@ -395,28 +395,28 @@ If a script sets a cookie, then no matter where the script came from -- the cook
395395

396396
## Appendix: GDPR
397397

398-
This topic is not related to JavaScript at all, just something to keep in mind when setting cookies.
398+
This topic is not related to JavaScript at all, it is just something to keep in mind when setting cookies.
399399

400-
There's a legislation in Europe called GDPR, that enforces a set of rules for websites to respect the users' privacy. One of these rules is to require an explicit permission for tracking cookies from the user.
400+
There's a legislation in Europe called GDPR, that enforces a set of rules for websites to respect the users' privacy. One of these rules is to require explicit permission for tracking cookies from the user.
401401

402402
Please note, that's only about tracking/identifying/authorizing cookies.
403403

404404
So, if we set a cookie that just saves some information, but neither tracks nor identifies the user, then we are free to do it.
405405

406-
But if we are going to set a cookie with an authentication session or a tracking id, then a user must allow that.
406+
But if we are going to set a cookie with an authentication session or a tracking ID, then a user must allow that.
407407

408-
Websites generally have two variants of following GDPR. You must have seen them both already in the web:
408+
Websites generally have two variants of complying with GDPR. You are likely to have seen them both on the web:
409409

410410
1. If a website wants to set tracking cookies only for authenticated users.
411411

412412
To do so, the registration form should have a checkbox like "accept the privacy policy" (that describes how cookies are used), the user must check it, and then the website is free to set auth cookies.
413413

414414
2. If a website wants to set tracking cookies for everyone.
415415

416-
To do so legally, a website shows a modal "splash screen" for newcomers, and requires them to agree to the cookies. Then the website can set them and let people see the content. That can be disturbing for new visitors though. No one likes to see such "must-click" modal splash screens instead of the content. But GDPR requires an explicit agreement.
416+
To do so legally, a website shows a modal "splash screen" for newcomers and requires them to agree to the cookies. Then the website can set them and let people see the content. That can be disturbing for new visitors though. No one likes to see such "must-click" modal splash screens instead of the content. But GDPR requires an explicit agreement.
417417

418418

419-
GDPR is not only about cookies, it's about other privacy-related issues too, but that's too much beyond our scope.
419+
GDPR is not only about cookies, it is about other privacy-related issues too, but that is beyond our scope.
420420

421421

422422
## Summary
@@ -426,13 +426,13 @@ GDPR is not only about cookies, it's about other privacy-related issues too, but
426426
- Name/value must be encoded.
427427
- One cookie may not exceed 4KB in size. The number of cookies allowed on a domain is around 20+ (varies by browser).
428428

429-
Cookie options:
429+
Cookie attributes:
430430
- `path=/`, by default current path, makes the cookie visible only under that path.
431431
- `domain=site.com`, by default a cookie is visible on the current domain only. If the domain is set explicitly, the cookie becomes visible on subdomains.
432-
- `expires` or `max-age` sets the cookie expiration time. Without them the cookie dies when the browser is closed.
432+
- `expires` or `max-age` sets the cookie expiration time. Without them, the cookie dies when the browser is closed.
433433
- `secure` makes the cookie HTTPS-only.
434434
- `samesite` forbids the browser to send the cookie with requests coming from outside the site. This helps to prevent XSRF attacks.
435435

436436
Additionally:
437-
- Third-party cookies may be forbidden by the browser, e.g. Safari does that by default.
437+
- The browser may forbid third-party cookies, e.g. Safari does that by default. There is also work in progress to implement this in Chrome.
438438
- When setting a tracking cookie for EU citizens, GDPR requires to ask for permission.

0 commit comments

Comments
 (0)