1<!doctype html>
2<html>
3 <head>
4 <meta charset=utf-8>
5 <title>Test cookie domain attribute parsing</title>
6 <meta name=help href="https://tools.ietf.org/html/rfc6265#section-5.2.3">
7 <meta name="timeout" content="long">
8 <script src="/resources/testharness.js"></script>
9 <script src="/cookies/resources/cookie-test.js"></script>
10 </head>
11 <body>
12 <script>
13 const path = "path=/cookies/attributes"
14 const port = "{{ports[http][0]}}";
15 const host = "{{host}}"; // example.org
16 const wwwHost = "{{hosts[alt][]}}"; // home.example.org
17 const www1Host = "{{domains[www1]}}"; // sibling.example.org
18 const www2wwwHost = "{{domains[www2.www]}}"; // subdomain.home.example.org
19
20 // naive helper method to return the TLD for a given domain
21 const getTLD = domain => {
22 let match = /\.[a-z]+$/.exec(domain);
23 if (match) {
24 return match[0];
25 } else {
26 throw 'Domain is malformed!';
27 }
28 }
29
30 // helper to take a domain like "www.example.org"
31 // and return a string like "www.eXaMpLe.org"
32 const makeBizarre = domain => {
33 let bizarre = "";
34 let domainArray = domain.split(".");
35 let secondLevel = domainArray[domainArray.length - 2];
36 for (let i in secondLevel) {
37 if (i % 2 == 1) {
38 bizarre += secondLevel[i].toUpperCase();
39 } else {
40 bizarre += secondLevel[i];
41 }
42 }
43 domainArray[domainArray.length - 2] = bizarre;
44 return domainArray.join(".");
45 }
46
47 // helper to change the current TLD to a TLD that doesn't exist, and is
48 // unlikely to exist in the future. (the main point is that the TLD
49 // *changes*, so there is no domain match, but we cant' predict how WPT
50 // servers may be set up in the wild so picking any valid TLD has the risk
51 // of future (unintentional) domain matching.
52 const changeTLD = domain => {
53 let domainArray = domain.split(".");
54 domainArray[domainArray.length - 1] += "zzz";
55 return domainArray.join(".");
56 }
57
58 const domainTests = [
59 {
60 cookie: `test=1; domain=${wwwHost}`,
61 expected: "test=1",
62 name: "Return cookie for a domain match",
63 location: `http://${wwwHost}:${port}/cookies/attributes/resources/path.html`,
64 },
65 {
66 cookie: `test=2; domain=${wwwHost}`,
67 expected: "",
68 name: "No cookie returned for domain mismatch (subdomains differ post-redirect)",
69 location: `http://${www1Host}:${port}/cookies/attributes/resources/path.html`,
70 },
71 {
72 cookie: `test=3; domain=.${wwwHost}`,
73 expected: "test=3",
74 name: "Return cookie for a domain match with leading '.'",
75 location: `http://${wwwHost}:${port}/cookies/attributes/resources/path.html`,
76 },
77 {
78 cookie: `test=4; domain=${wwwHost}`,
79 expected: "test=4",
80 name: "Return cookie for domain match (domain attribute is suffix of the host name and first level subdomain)",
81 location: `http://${www2wwwHost}:${port}/cookies/attributes/resources/path.html`,
82 },
83 {
84 cookie: `test=5; domain=.${wwwHost}`,
85 expected: "test=5",
86 name: "Return cookie for domain match (domain attribute is suffix of the host name and first level subdomain, with leading '.')",
87 location: `http://${www2wwwHost}:${port}/cookies/attributes/resources/path.html`,
88 },
89 {
90 cookie: `test=6; domain=.${wwwHost}`,
91 expected: "",
92 name: "No cookie returned for domain mismatch (subdomains differ, with leading '.')",
93 location: `http://${www1Host}:${port}/cookies/attributes/resources/path.html`,
94 },
95 {
96 cookie: `test=7; domain=${www1Host}`,
97 expected: "",
98 name: "No cookie returned for domain mismatch when cookie was created (which would match after the redirect, with one subdomain level)",
99 location: `http://${www1Host}:${port}/cookies/attributes/resources/path.html`,
100 },
101 {
102 cookie: `test=8; domain=.${host}`,
103 expected: "test=8",
104 name: "Return cookie for domain match (domain attribute is suffix of the host name, with leading '.')",
105 location: `http://${wwwHost}:${port}/cookies/attributes/resources/path.html`,
106 },
107 {
108 cookie: `test=9; domain=${host}`,
109 expected: "test=9",
110 name: "Return cookie for domain match (domain attribute is suffix of the host name)",
111 location: `http://${wwwHost}:${port}/cookies/attributes/resources/path.html`,
112 },
113 {
114 cookie: `test=10; domain=..${wwwHost}`,
115 expected: "",
116 name: "No cookie returned for domain attribute with double leading '.'",
117 location: `http://${wwwHost}:${port}/cookies/attributes/resources/path.html`,
118 },
119 {
120 cookie: `test=11; domain=www..${host}`,
121 expected: "",
122 name: "No cookie returned for domain attribute with subdomain followed by ..",
123 location: `http://${wwwHost}:${port}/cookies/attributes/resources/path.html`,
124 },
125 {
126 cookie: `test=12; domain= .${wwwHost}`,
127 expected: "test=12",
128 name: "Return cookie for a domain match with leading whitespace and '.'",
129 location: `http://${wwwHost}:${port}/cookies/attributes/resources/path.html`,
130 },
131 {
132 cookie: `test=13; domain= . ${wwwHost}`,
133 expected: "",
134 name: "No cookie returned for domain attribute with whitespace that surrounds a leading '.'",
135 location: `http://${wwwHost}:${port}/cookies/attributes/resources/path.html`,
136 },
137 {
138 cookie: `test=14; domain=${wwwHost}.`,
139 expected: "",
140 name: "No cookie returned for domain attribute with trailing '.'",
141 location: `http://${wwwHost}:${port}/cookies/attributes/resources/path.html`,
142 },
143 {
144 cookie: `test=15; domain=${wwwHost}..`,
145 expected: "",
146 name: "No cookie returned for domain attribute with trailing '..'",
147 location: `http://${wwwHost}:${port}/cookies/attributes/resources/path.html`,
148 },
149 {
150 cookie: `test=16; domain=${wwwHost} .`,
151 expected: "",
152 name: "No cookie returned for domain attribute with trailing whitespace and '.'",
153 location: `http://${wwwHost}:${port}/cookies/attributes/resources/path.html`,
154 },
155 {
156 cookie: `test=17; domain=${getTLD(host)}`,
157 expected: "",
158 name: "No cookie returned for domain attribute with TLD as value",
159 location: `http://${wwwHost}:${port}/cookies/attributes/resources/path.html`,
160 },
161 {
162 cookie: `test=18; domain=.${getTLD(host)}`,
163 expected: "",
164 name: "No cookie returned for domain attribute with TLD as value, with leading '.'",
165 location: `http://${wwwHost}:${port}/cookies/attributes/resources/path.html`,
166 },
167 {
168 cookie: `test=18b; domain=.${getTLD(host)}.`,
169 expected: "",
170 name: "No cookie returned for domain attribute with TLD as value, with leading and trailing '.'",
171 location: `http://${wwwHost}:${port}/cookies/attributes/resources/path.html`,
172 },
173 {
174 cookie: [`testA=19; domain=${wwwHost}`, `testB=19; domain=.${wwwHost}`],
175 expected: "testA=19; testB=19",
176 name: "Return multiple cookies that match on domain (without and with leading '.')",
177 location: `http://${wwwHost}:${port}/cookies/attributes/resources/path.html`,
178 },
179 {
180 cookie: [`testB=20; domain=.${wwwHost}`, `testA=20; domain=${wwwHost}`],
181 expected: "testB=20; testA=20",
182 name: "Return multiple cookies that match on domain (with and without leading '.')",
183 location: `http://${wwwHost}:${port}/cookies/attributes/resources/path.html`,
184 },
185 {
186 cookie: `test=21; domain="${wwwHost}"`,
187 expected: "",
188 name: "No cookie returned for domain attribute value between quotes",
189 location: `http://${wwwHost}:${port}/cookies/attributes/resources/path.html`,
190 },
191 {
192 cookie: [`testA=22; domain=${wwwHost}`, `testB=22; domain=.${host}`],
193 expected: "testA=22; testB=22",
194 name: "Return multiple cookies that match on subdomain and domain (without and with leading '.')",
195 location: `http://${wwwHost}:${port}/cookies/attributes/resources/path.html`,
196 },
197 {
198 cookie: [`testB=23; domain=.${host}`, `testA=23; domain=${wwwHost}`],
199 expected: "testB=23; testA=23",
200 name: "Return multiple cookies that match on domain and subdomain (with and without leading '.')",
201 location: `http://${wwwHost}:${port}/cookies/attributes/resources/path.html`,
202 },
203 {
204 cookie: `test=24; domain=.${host}; domain=${wwwHost}`,
205 expected: "",
206 name: "No cookie returned when domain attribute does not domain-match (and first does)",
207 location: `http://${www1Host}:${port}/cookies/attributes/resources/path.html`,
208 },
209 {
210 cookie: `test=25; domain=${wwwHost}; domain=.${host}`,
211 expected: "test=25",
212 name: "Return cookie for domain attribute match (first does not, but second does)",
213 location: `http://${www1Host}:${port}/cookies/attributes/resources/path.html`,
214 },
215 {
216 cookie: `test=26; domain=${makeBizarre(wwwHost)}`,
217 expected: "test=26",
218 name: "Return cookie for domain match (with bizarre capitalization for domain attribute value)",
219 location: `http://${wwwHost}:${port}/cookies/attributes/resources/path.html`,
220 },
221 {
222 cookie: `test=27; domain="${wwwHost}:${port}"`,
223 expected: "",
224 name: "No cookie returned for domain attribute value with port",
225 location: `http://${wwwHost}:${port}/cookies/attributes/resources/path.html`,
226 },
227 {
228 cookie: `test=28; domain=${www2wwwHost}`,
229 expected: "",
230 name: "No cookie returned for domain mismatch when cookie was created (which would match after the redirect, with two subdomain levels)",
231 location: `http://${www2wwwHost}:${port}/cookies/attributes/resources/path.html`,
232 },
233 {
234 cookie: `test=29`,
235 expected: "",
236 name: "No cookie returned for cookie set on different domain (with no domain attribute)",
237 location: `http://${www2wwwHost}:${port}/cookies/attributes/resources/path.html`,
238 },
239 {
240 cookie: "test=30; domain=",
241 expected: "test=30",
242 name: "Return cookie set with bare domain= attribute",
243 location: `http://${wwwHost}:${port}/cookies/attributes/resources/path.html`,
244 },
245 {
246 cookie: `test=31; domain=${wwwHost}`,
247 expected: "test=31",
248 name: "Return cookie that domain-matches with bizarre-cased URL",
249 location: `http://${makeBizarre(wwwHost)}:${port}/cookies/attributes/resources/path.html`,
250 },
251 {
252 cookie: `test=32; domain=${wwwHost}; domain=${changeTLD(wwwHost)}`,
253 expected: "",
254 name: "No cookie returned for domain attribute mismatch (first attribute matches, but second does not)",
255 location: `http://${wwwHost}:${port}/cookies/attributes/resources/path.html`,
256 },
257 {
258 cookie: `test=33; domain=${changeTLD(wwwHost)}; domain=${wwwHost}`,
259 expected: "test=33",
260 name: "Return cookie for domain match (first attribute doesn't, but second does)",
261 location: `http://${wwwHost}:${port}/cookies/attributes/resources/path.html`,
262 },
263 {
264 cookie: `test=34; domain=${wwwHost}; domain=${changeTLD(wwwHost)}; domain=${wwwHost}`,
265 expected: "test=34",
266 name: "Return cookie for domain match (first attribute matches, second doesn't, third does)",
267 location: `http://${wwwHost}:${port}/cookies/attributes/resources/path.html`,
268 },
269 {
270 cookie: `test=35; domain=${changeTLD(wwwHost)}; domain=${wwwHost}; domain=${changeTLD(wwwHost)}`,
271 expected: "",
272 name: "No cookie returned for domain attribute mismatch (first attribute doesn't, second does, third doesn't)",
273 location: `http://${wwwHost}:${port}/cookies/attributes/resources/path.html`,
274 },
275 {
276 cookie: `test=36; domain=${wwwHost}; domain=${wwwHost}`,
277 expected: "test=36",
278 name: "Return cookie for domain match (with two identical domain attributes)",
279 location: `http://${wwwHost}:${port}/cookies/attributes/resources/path.html`,
280 },
281 {
282 cookie: `test=37; domain=${wwwHost}; domain=${host}`,
283 expected: "test=37",
284 name: "Return cookie for domain match (with first domain attribute a match for host name and second as suffix of host name)",
285 location: `http://${wwwHost}:${port}/cookies/attributes/resources/path.html`,
286 },
287 {
288 cookie: `test=38; domain=${host}; domain=${wwwHost}`,
289 expected: "test=38",
290 name: "Return cookie for domain match (with first domain attribute as suffix of host name and second a match for host name)",
291 location: `http://${wwwHost}:${port}/cookies/attributes/resources/path.html`,
292 },
293 {
294 cookie: `test=39; domain=.${www1Host}`,
295 expected: "",
296 name: "No cookie set on domain mismatch before a (domain matching) redirect",
297 location: `http://${www1Host}:${port}/cookies/attributes/resources/path.html`,
298 },
299 {
300 cookie: `test=40; domain=.${www2wwwHost}`,
301 expected: "",
302 name: "No cookie set on domain mismatch before a (domain matching) redirect (for second level subdomain)",
303 location: `http://${www2wwwHost}:${port}/cookies/attributes/resources/path.html`,
304 },
305 {
306 cookie: `test=41; domain=${host}; domain=`,
307 expected: "test=41",
308 name: "Return cookie for domain match (with first domain attribute as suffix of host name and second a bare attribute)",
309 location: `http://${wwwHost}:${port}/cookies/attributes/resources/path.html`,
310 },
311 {
312 cookie: `test=42; domain=${www1Host}; domain=`,
313 expected: "",
314 name: "No cookie returned for domain mismatch (with domain mismatch as first domain attribute and second a bare attribute)",
315 location: `http://${wwwHost}:${port}/cookies/attributes/resources/path.html`,
316 },
317 {
318 cookie: `test=43 domain=${www1Host}; domain=`,
319 expected: "",
320 name: "No cookie returned for domain mismatch (first attribute is a different subdomain and second is bare)",
321 location: `http://${www2wwwHost}:${port}/cookies/attributes/resources/path.html`,
322 },
323 {
324 cookie: [`test=not44; domain=${wwwHost}`, `test=44; domain=.${wwwHost}`],
325 expected: "test=44",
326 name: "Cookies with same name, path, and domain (differing only in leading '.') overwrite each other ('.' second)",
327 location: `http://${wwwHost}:${port}/cookies/attributes/resources/path.html`,
328 },
329 {
330 cookie: [`test=not45; domain=.${wwwHost}`, `test=45; domain=${wwwHost}`],
331 expected: "test=45",
332 name: "Cookies with same name, path, and domain (differing only in leading '.') overwrite each other ('.' first)",
333 location: `http://${wwwHost}:${port}/cookies/attributes/resources/path.html`,
334 },
335 ];
336
337 for (const test of domainTests) {
338 if (Array.isArray(test.cookie)) {
339 for (let i in test.cookie) {
340 test.cookie[i] += `; ${path}`;
341 }
342 } else {
343 test.cookie += `; ${path}`;
344 }
345
346 httpRedirectCookieTest(test.cookie, test.expected, test.name,
347 test.location);
348 }
349 </script>
350 </body>
351</html>