1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 * Test whether the given domain is valid for a cookie.
8 * @param {string} domain Domain for a cookie.
9 * @return {boolean} True if the domain is valid, otherwise false.
11 function isDomainValid(domain
) {
12 var dummyCookie
= 'ChromeDriverwjers908fljsdf37459fsdfgdfwru=';
14 document
.cookie
= dummyCookie
+ '; domain=' + domain
;
15 if (document
.cookie
.indexOf(dummyCookie
) != -1) {
16 // Expire the dummy cookie if it is added successfully.
17 document
.cookie
= dummyCookie
+ '; Max-Age=0';
24 * Add the given cookie to the current web page.
26 * If path is not specified, default to '/'.
27 * If domain is not specified, default to document.domain, otherwise remove its
30 * Validate name, value, domain and path of the cookie in the same way as the
31 * method CanonicalCookie::Create in src/net/cookies/canonical_cookie.cc. Besides
32 * the following requirements, name, value, domain and path of the cookie should
33 * not start or end with ' ' or '\t', and should not contain '\n', '\r', or '\0'.
35 * <li>name: no ';' or '='
37 * <li>path: starts with '/', no ';'
40 * @param {!Object} cookie An object representing a Cookie JSON Object as
41 * specified in https://code.google.com/p/selenium/wiki/JsonWireProtocol.
43 function addCookie(cookie
) {
44 function isNameInvalid(value
) {
45 return /(^[ \t])|([;=\n\r\0])|([ \t]$)/.test(value
);
47 function isValueInvalid(value
) {
48 return /(^[ \t])|([;\n\r\0])|([ \t]$)/.test(value
);
50 function isPathInvalid(path
) {
51 return path
[0] != '/' || /([;\n\r\0])|([ \t]$)/.test(path
);
54 var name
= cookie
['name'];
55 if (!name
|| isNameInvalid(name
))
56 throw new Error('name of cookie is missing or invalid:"' + name
+ '"');
58 var value
= cookie
['value'] || '';
59 if (isValueInvalid(value
))
60 throw new Error('value of cookie is invalid:"' + value
+ '"');
62 var domain
= cookie
['domain'];
63 // Remove the port number from domain.
65 var domain_parts
= domain
.split(':');
66 if (domain_parts
.length
> 2)
67 throw new Error('domain of cookie has too many colons');
68 else if (domain_parts
.length
== 2)
69 domain
= domain_parts
[0];
72 if (domain
&& (isValueInvalid(domain
) || !isDomainValid(domain
))) {
73 var error
= new Error();
74 error
.code
= 24; // Error code for InvalidCookieDomain.
75 error
.message
= 'invalid domain:"' + domain
+ '"';
79 var path
= cookie
['path'];
80 if (path
&& isPathInvalid(path
))
81 throw new Error('path of cookie is invalid:"' + path
+ '"');
83 var newCookie
= name
+ '=' + value
;
84 newCookie
+= '; path=' + (path
|| '/');
86 newCookie
+= '; domain=' + domain
;
87 if (cookie
['expiry']) {
88 var expiredDate
= new Date(cookie
['expiry'] * 1000);
89 newCookie
+= '; expires=' + expiredDate
.toUTCString();
92 newCookie
+= '; secure';
94 document
.cookie
= newCookie
;