Skip to content

Commit daca277

Browse files
authored
Fix grammar
1 parent 5ce4b3a commit daca277

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

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

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ Cookies are usually set by a web-server using the response `Set-Cookie` HTTP-hea
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 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.
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 options. In this chapter, we'll cover them in detail.
1616

1717
## Reading from document.cookie
1818

@@ -35,7 +35,7 @@ The value of `document.cookie` consists of `name=value` pairs, delimited by `;
3535

3636
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.
3737

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.
3939

4040
## Writing to document.cookie
4141

@@ -50,12 +50,12 @@ document.cookie = "user=John"; // update only cookie named 'user'
5050
alert(document.cookie); // show all cookies
5151
```
5252

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`.
5454

5555
Technically, name and value can have any characters. To keep the valid formatting, they should be escaped using a built-in `encodeURIComponent` function:
5656

5757
```js run
58-
// special characters (spaces), need encoding
58+
// special characters (spaces) need encoding
5959
let name = "my name";
6060
let value = "John Smith"
6161

@@ -67,12 +67,12 @@ alert(document.cookie); // ...; my%20name=John%20Smith
6767

6868

6969
```warn header="Limitations"
70-
There are few limitations:
70+
There are a few limitations:
7171
- The `name=value` pair, after `encodeURIComponent`, should not exceed 4KB. So we can't store anything huge in a cookie.
7272
- The total number of cookies per domain is limited to around 20+, the exact limit depends on the browser.
7373
```
7474

75-
Cookies have several options, many of them are important and should be set.
75+
Cookies have several options, many of which are important and should be set.
7676

7777
The options are listed after `key=value`, delimited by `;`, like this:
7878

@@ -86,7 +86,7 @@ document.cookie = "user=John; path=/; expires=Tue, 19 Jan 2038 03:14:07 GMT"
8686

8787
The url path prefix must be absolute. It makes the cookie accessible for pages under that path. By default, it's the current path.
8888

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`.
9090

9191
Usually, we should set `path` to the root: `path=/` to make the cookie accessible from all website pages.
9292

@@ -102,7 +102,7 @@ It's a safety restriction, to allow us to store sensitive data in cookies that s
102102

103103
By default, a cookie is accessible only at the domain that set it.
104104

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`.
106106

107107
```js
108108
// if we set a cookie at site.com website...
@@ -114,7 +114,7 @@ alert(document.cookie); // no user
114114

115115
...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.
116116

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.
118118

119119
For example:
120120

@@ -141,7 +141,7 @@ To let cookies survive a browser close, we can set either the `expires` or `max-
141141

142142
- **`expires=Tue, 19 Jan 2038 03:14:07 GMT`**
143143

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.
145145

146146
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:
147147

@@ -194,17 +194,17 @@ To understand how it works and when it's useful, let's take a look at XSRF attac
194194

195195
### XSRF attack
196196

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.
198198

199199
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.
200200

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.
202202

203203
![](cookie-xsrf.svg)
204204

205205
That's a so-called "Cross-Site Request Forgery" (in short, XSRF) attack.
206206

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.
208208

209209
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.
210210

@@ -218,17 +218,17 @@ It has two possible values:
218218

219219
A cookie with `samesite=strict` is never sent if the user comes from outside the same site.
220220

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.
222222

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.
224224

225225
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`.
226226

227227
Although, there's a small inconvenience.
228228

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.
230230

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.
232232

233233
- **`samesite=lax` (same as `samesite` without value)**
234234

@@ -239,13 +239,13 @@ Lax mode, just like `strict`, forbids the browser to send cookies when coming fr
239239
A `samesite=lax` cookie is sent if both of these conditions are true:
240240
1. The HTTP method is "safe" (e.g. GET, but not POST).
241241

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.
243243

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

246246
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.
247247

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.
249249

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

@@ -259,7 +259,7 @@ There's a drawback:
259259

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

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.
263263

264264
## httpOnly
265265

0 commit comments

Comments
 (0)