You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 6-data-storage/01-cookie/article.md
+22-22Lines changed: 22 additions & 22 deletions
Original file line number
Diff line number
Diff line change
@@ -6,13 +6,13 @@ Cookies are usually set by a web-server using the response `Set-Cookie` HTTP-hea
6
6
7
7
One of the most widespread use cases is authentication:
8
8
9
-
1. Upon signin, the server uses the `Set-Cookie` HTTP-header in the response to set a cookie with a unique "session identifier".
10
-
2. Next time when 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.
11
11
3. So the server knows who made the request.
12
12
13
13
We can also access cookies from the browser, using `document.cookie` property.
14
14
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 options. In this chapter, we'll cover them in detail.
16
16
17
17
## Reading from document.cookie
18
18
@@ -35,7 +35,7 @@ The value of `document.cookie` consists of `name=value` pairs, delimited by `;
35
35
36
36
To find a particular cookie, we can split `document.cookie` by `; `, and then find the right name. We can use either a regular expression or array functions to do that.
37
37
38
-
We leave it as an exercise for the reader. Also, at the end of the chapter you'll find helper functions to manipulate cookies.
38
+
We leave it as an exercise for the reader. Also, at the end of the chapter, you'll find helper functions to manipulate cookies.
39
39
40
40
## Writing to document.cookie
41
41
@@ -50,12 +50,12 @@ document.cookie = "user=John"; // update only cookie named 'user'
50
50
alert(document.cookie); // show all cookies
51
51
```
52
52
53
-
If you run it, then probably you'll see multiple cookies. That's because the `document.cookie=` operation does not overwrite all cookies. It only sets the mentioned cookie `user`.
53
+
If you run it, you will likely see multiple cookies. That's because the `document.cookie=` operation does not overwrite all cookies. It only sets the mentioned cookie `user`.
54
54
55
55
Technically, name and value can have any characters. To keep the valid formatting, they should be escaped using a built-in `encodeURIComponent` function:
The url path prefix must be absolute. It makes the cookie accessible for pages under that path. By default, it's the current path.
88
88
89
-
If a cookie is set with `path=/admin`, it's visible at pages `/admin` and `/admin/something`, but not at `/home` or `/adminpage`.
89
+
If a cookie is set with `path=/admin`, it's visible on pages `/admin` and `/admin/something`, but not at `/home` or `/adminpage`.
90
90
91
91
Usually, we should set `path` to the root: `path=/` to make the cookie accessible from all website pages.
92
92
@@ -102,7 +102,7 @@ It's a safety restriction, to allow us to store sensitive data in cookies that s
102
102
103
103
By default, a cookie is accessible only at the domain that set it.
104
104
105
-
Please note, by default a cookie is also not shared to a subdomain as well, such as `forum.site.com`.
105
+
Please note, by default, a cookie is also not shared to a subdomain as well, such as `forum.site.com`.
106
106
107
107
```js
108
108
// if we set a cookie at site.com website...
@@ -114,7 +114,7 @@ alert(document.cookie); // no user
114
114
115
115
...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.
116
116
117
-
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 cookie.
117
+
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.
118
118
119
119
For example:
120
120
@@ -141,7 +141,7 @@ To let cookies survive a browser close, we can set either the `expires` or `max-
141
141
142
142
-**`expires=Tue, 19 Jan 2038 03:14:07 GMT`**
143
143
144
-
The cookie expiration date defines the time, when the browser will automatically delete it.
144
+
The cookie expiration date defines the time when the browser will automatically delete it.
145
145
146
146
The date must be exactly in this format, in the GMT timezone. We can use `date.toUTCString` to get it. For instance, we can set the cookie to expire in 1 day:
147
147
@@ -194,17 +194,17 @@ To understand how it works and when it's useful, let's take a look at XSRF attac
194
194
195
195
### XSRF attack
196
196
197
-
Imagine, you are logged into the site `bank.com`. That is: you have an authentication cookie from that site. Your browser sends it to `bank.com` with every request, so that it recognizes you and performs all sensitive financial operations.
197
+
Imagine, you are logged into the site `bank.com`. That is: you have an authentication cookie from that site. Your browser sends it to `bank.com` with every request so that it recognizes you and performs all sensitive financial operations.
198
198
199
199
Now, while browsing the web in another window, you accidentally come to another site `evil.com`. That site has JavaScript code that submits a form `<form action="https://bank.com/pay">` to `bank.com` with fields that initiate a transaction to the hacker's account.
200
200
201
-
The browser sends cookies every time you visit the site `bank.com`, even if the form was submitted from `evil.com`. So the bank recognizes you and actually performs the payment.
201
+
The browser sends cookies every time you visit the site `bank.com`, even if the form was submitted from `evil.com`. So the bank recognizes you and performs the payment.
202
202
203
203

204
204
205
205
That's a so-called "Cross-Site Request Forgery" (in short, XSRF) attack.
206
206
207
-
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 token in every form it receives.
207
+
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.
208
208
209
209
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.
210
210
@@ -218,17 +218,17 @@ It has two possible values:
218
218
219
219
A cookie with `samesite=strict` is never sent if the user comes from outside the same site.
220
220
221
-
In other words, whether a user follows a link from their mail or submits a form from `evil.com`, or does any operation that originates from another domain, the cookie is not sent.
221
+
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.
222
222
223
-
If authentication cookies have the `samesite` option, then a XSRF attack has no chances to succeed, because a submission from `evil.com` comes without cookies. So `bank.com` will not recognize the user and will not proceed with the payment.
223
+
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.
224
224
225
225
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`.
226
226
227
227
Although, there's a small inconvenience.
228
228
229
-
When a user follows a legitimate link to `bank.com`, like from their own notes, they'll be surprised that `bank.com` does not recognize them. Indeed, `samesite=strict` cookies are not sent in that case.
229
+
When a user follows a legitimate link to `bank.com`, like from their notes, they'll be surprised that `bank.com` does not recognize them. Indeed, `samesite=strict` cookies are not sent in that case.
230
230
231
-
We could work around that by using two cookies: one for "general recognition", only for the purposes of saying: "Hello, John", and the other one for data-changing operations with `samesite=strict`. Then, a person coming from outside of the site will see a welcome, but payments must be initiated from the bank's website, for the second cookie to be sent.
231
+
We could work around that by using two cookies: one for "general recognition", only to say: "Hello, John", and the other one for data-changing operations with `samesite=strict`. Then, a person coming from outside of the site will see a welcome, but payments must be initiated from the bank's website, for the second cookie to be sent.
232
232
233
233
-**`samesite=lax` (same as `samesite` without value)**
234
234
@@ -239,13 +239,13 @@ Lax mode, just like `strict`, forbids the browser to send cookies when coming fr
239
239
A `samesite=lax` cookie is sent if both of these conditions are true:
240
240
1. The HTTP method is "safe" (e.g. GET, but not POST).
241
241
242
-
The full list of safe HTTP methods is in the [RFC7231 specification](https://tools.ietf.org/html/rfc7231#section-4.2.1). Basically, these are the methods that should be used for reading, but not writing the data. They must not perform any data-changing operations. Following a link is always GET, the safe method.
242
+
The full list of safe HTTP methods is in the [RFC7231 specification](https://tools.ietf.org/html/rfc7231#section-4.2.1). These are the methods that should be used for reading, but not writing the data. They must not perform any data-changing operations. Following a link is always GET, the safe method.
243
243
244
244
2. The operation performs a top-level navigation (changes URL in the browser address bar).
245
245
246
246
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.
247
247
248
-
So, what `samesite=lax` does, is to basically allow the most common "go to URL" operation to have cookies. E.g. opening a website link from notes that satisfy these conditions.
248
+
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.
249
249
250
250
But anything more complicated, like a network request from another site or a form submission, loses cookies.
251
251
@@ -259,7 +259,7 @@ There's a drawback:
259
259
260
260
**So if we solely rely on `samesite` to provide protection, then old browsers will be vulnerable.**
261
261
262
-
But we surely can use `samesite` together with other protection measures, like xsrf tokens, to add an additional layer of defence and then, in the future, when old browsers die out, we'll probably be able to drop xsrf tokens.
262
+
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.
0 commit comments