-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
2_cookies_spec.ts
284 lines (236 loc) · 7.54 KB
/
2_cookies_spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import moment from 'moment'
import parser from 'cookie-parser'
import e2e from '../support/helpers/e2e'
import humanInterval from 'human-interval'
const onServer = function (app) {
app.use(parser())
app.get('/logout', (req, res) => {
return res.send('<html>logged out</html>')
})
app.all('/requestCookies', (req, res) => {
return res.send(req.cookies)
})
app.all('/requestCookiesHtml', (req, res) => {
return res.type('html').send(req.cookies)
})
app.get('/set', (req, res) => {
res.cookie('shouldExpire', 'endOfsession')
return res.send('<html></html>')
})
app.get('/setOneHourFromNowAndSecure', (req, res) => {
res.cookie('shouldExpire', 'oneHour', {
secure: true,
maxAge: humanInterval('1 hour'),
})
return res.send('<html></html>')
})
app.get('/expirationRedirect', (req, res) => {
res.cookie('shouldExpire', 'now', {
// express maxAge is relative to current time
// in milliseconds
maxAge: 0,
})
return res.redirect('/logout')
})
app.get('/expirationMaxAge', (req, res) => {
res.header('Set-Cookie',
'shouldExpire=; Max-Age=0; Path=/; Expires=Sun, 24 Jun 1997 20:36:13 GMT')
// response to set
// auth=p1_2FruNr1entizk9QEGHFYQlWjIK5LULzdDj17lkYhZTz7XA5GOfnVgbbeBDAqnfImkwof2qz0M3yi3AUVusKPqh1BRK6253h0kiBENwdjWDsx3mYQQKpHn6o3XOXX7poSkzrHThnrDlH4w9zoLItwIVNhR2hQrCYhQhtHuw20YM_3D; Domain=.surveymonkey.com;Max-Age=3600; Path=/; expires=Fri, 26-Oct-2018 06:13:48 GMT; secure; HttpOnly'
// response to clear
// auth=; Domain=.surveymonkey.com; Max-Age=0; Path=/; expires=Wed, 31-Dec-97 23:59:59 GMT
return res.send('<html></html>')
})
app.get('/expirationExpires', (req, res) => {
res.cookie('shouldExpire', 'now', {
expires: moment().subtract(1, 'day').toDate(),
})
return res.send('<html></html>')
})
app.get('/cookieWithNoName', (req, res) => {
res.header('Set-Cookie',
'=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/')
return res.send('<html></html>')
})
app.get('/invalidCookies', (req, res) => {
res.header('Set-Cookie', 'foo=bar; domain=nope.not.this.one')
return res.send('<html></html>')
})
app.get('/setCascadingCookies', (req, res) => {
const n = Number(req.query.n)
// alternates between base URLs
const {
a,
} = req.query
const {
b,
} = req.query
res.header('Set-Cookie', [
`namefoo${n}=valfoo${n}`,
`namebar${n}=valbar${n}`,
])
console.log('to', a, 'from', b)
if (n > 0) {
res.redirect(`${a}/setCascadingCookies?n=${n - 1}&a=${b}&b=${a}`)
}
return res.send('<html>finished setting cookies</html>')
})
app.get('/setDomainCookie', (req, res) => {
res.setHeader('Set-Cookie', `domaincookie=foo; Domain=${req.query.domain}`)
if (req.query.redirect) {
return res.redirect(req.query.redirect).end()
}
return res.type('html').end()
})
app.get('/samesite/:value', (req, res) => {
const { value } = req.params
let header = `ss${value}=someval; Path=/; SameSite=${value}`
if (value === 'None') {
header += '; Secure'
}
res.setHeader('Set-Cookie', header)
return res.type('html').end()
})
return app.get('/invalidControlCharCookie', (req, res) => {
// `http` lib throws an error if we use .setHeader to set this
return res.connection.end(`\
HTTP/1.1 200 OK
Content-Type: text/html
Set-Cookie: ___utmvaFvuoaRv=TkE\u0001sCvZ; path=/; Max-Age=900
Set-Cookie: _valid=true; path=/; Max-Age=900
foo\
`)
})
}
const haveRoot = !process.env.USE_HIGH_PORTS && (process.geteuid() === 0)
if (!haveRoot) {
console.warn('(e2e tests warning) You are not running as root; therefore, 2_cookies_spec cannot cover the case where the default (80/443) HTTP(s) port is used. Alternate ports (2121/2323) will be used instead.')
}
let httpPort = 2121
let httpsPort = 2323
if (haveRoot) {
httpPort = 80
httpsPort = 443
}
const otherDomain = 'quux.bar.net'
const otherUrl = `http://${otherDomain}${haveRoot ? '' : `:${httpPort}`}`
const otherHttpsUrl = `https://${otherDomain}${haveRoot ? '' : `:${httpsPort}`}`
const chromiumSameSiteFeatures = '--enable-features=SameSiteByDefaultCookies,CookiesWithoutSameSiteMustBeSecure'
// SameSite is loosely enforced in some versions of FF/Electron/Chromium,
// but all have options we can use to force it to be strict
const FORCED_SAMESITE_ENV = {
ELECTRON_EXTRA_LAUNCH_ARGS: chromiumSameSiteFeatures,
CHROMIUM_EXTRA_LAUNCH_ARGS: chromiumSameSiteFeatures,
FIREFOX_FORCE_STRICT_SAMESITE: 1,
}
const sharedBaseUrlSpecSnapshot = 'e2e cookies with baseurl'
const sharedNoBaseUrlSpecSnapshot = 'e2e cookies with no baseurl'
describe('e2e cookies', () => {
e2e.setup({
servers: [{
onServer,
port: httpPort,
}, {
onServer,
port: httpsPort,
https: true,
}],
settings: {
hosts: {
'*.foo.com': '127.0.0.1',
'*.bar.net': '127.0.0.1',
'*.cypress.test': '127.0.0.1',
},
},
})
context('SameSite', () => {
const baseUrl = `http://localhost${haveRoot ? '' : `:${httpPort}`}`
// once browsers are shipping with the options in FORCED_SAMESITE_ENV as default,
// we can remove this extra test case
e2e.it('with forced SameSite strictness', {
config: {
baseUrl,
env: {
baseUrl,
expectedDomain: 'localhost',
https: false,
httpUrl: baseUrl,
httpsUrl: `https://localhost${haveRoot ? '' : `:${httpsPort}`}`,
otherUrl,
otherHttpsUrl,
},
},
processEnv: FORCED_SAMESITE_ENV,
spec: 'cookies_spec_baseurl.coffee',
snapshot: true,
onRun: (exec) => {
return exec({
originalTitle: sharedBaseUrlSpecSnapshot,
})
},
})
})
// this is a big chunky test that runs cookies_spec with all combinations of these:
// - cookies on `localhost`, fully-qualified-domain-name, and IP address domains
// - `https` baseurls, `http` baseurls, and no baseurls set
// - both default port 80/443 and custom ports (if you are running as root)
// - all browsers
// snapshots are combined to ensure that there is no difference in any of these situations
return [
['localhost', 'localhost'],
['FQDN', 'www.bar.foo.com'],
['private FQDN', 'local.cypress.test'],
['IP', '127.0.0.1'],
]
.forEach(([
format,
baseDomain,
]) => {
context(`with ${format} urls`, () => {
const httpUrl = `http://${baseDomain}${haveRoot ? '' : `:${httpPort}`}`
const httpsUrl = `https://${baseDomain}${haveRoot ? '' : `:${httpsPort}`}`;
[
[httpUrl, false],
[httpsUrl, true],
].forEach((
[
baseUrl,
https,
],
) => {
e2e.it(`passes with baseurl: ${baseUrl}`, {
config: {
baseUrl,
env: {
baseUrl,
expectedDomain: baseDomain,
https,
httpUrl,
httpsUrl,
otherUrl,
otherHttpsUrl,
},
},
spec: 'cookies_spec_baseurl.coffee',
snapshot: true,
onRun: (exec) => {
return exec({
originalTitle: sharedBaseUrlSpecSnapshot,
})
},
})
})
e2e.it('passes with no baseurl', {
config: {
env: {
httpUrl,
httpsUrl,
},
},
originalTitle: sharedNoBaseUrlSpecSnapshot,
spec: 'cookies_spec_no_baseurl.coffee',
snapshot: true,
})
})
})
})