1 // Copyright (c) 2012 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.
5 // Portions of this code based on Mozilla:
6 // (netwerk/cookie/src/nsCookieService.cpp)
7 /* ***** BEGIN LICENSE BLOCK *****
8 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
10 * The contents of this file are subject to the Mozilla Public License Version
11 * 1.1 (the "License"); you may not use this file except in compliance with
12 * the License. You may obtain a copy of the License at
13 * http://www.mozilla.org/MPL/
15 * Software distributed under the License is distributed on an "AS IS" basis,
16 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
17 * for the specific language governing rights and limitations under the
20 * The Original Code is mozilla.org code.
22 * The Initial Developer of the Original Code is
23 * Netscape Communications Corporation.
24 * Portions created by the Initial Developer are Copyright (C) 2003
25 * the Initial Developer. All Rights Reserved.
28 * Daniel Witte (dwitte@stanford.edu)
29 * Michiel van Leeuwen (mvl@exedo.nl)
31 * Alternatively, the contents of this file may be used under the terms of
32 * either the GNU General Public License Version 2 or later (the "GPL"), or
33 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
34 * in which case the provisions of the GPL or the LGPL are applicable instead
35 * of those above. If you wish to allow use of your version of this file only
36 * under the terms of either the GPL or the LGPL, and not to allow others to
37 * use your version of this file under the terms of the MPL, indicate your
38 * decision by deleting the provisions above and replace them with the notice
39 * and other provisions required by the GPL or the LGPL. If you do not delete
40 * the provisions above, a recipient may use your version of this file under
41 * the terms of any one of the MPL, the GPL or the LGPL.
43 * ***** END LICENSE BLOCK ***** */
45 #include "net/cookies/parsed_cookie.h"
47 #include "base/logging.h"
48 #include "base/metrics/histogram.h"
49 #include "base/strings/string_util.h"
51 // TODO(jww): We are collecting several UMA statistics in this file, and they
52 // relate to http://crbug.com/238041. We are measuring stats related to control
53 // characters in cookies because, currently, we allow control characters in a
54 // variety of scenarios where various RFCs theoretically disallow them. These
55 // control characters have the potential to cause problems with certain web
56 // servers that reject HTTP requests that contain cookies with control
57 // characters. We are measuring whether disallowing such cookies would have a
58 // notable impact on our users. We want to collect these stats through 1 stable
59 // release, so these UMA stats should remain at least through the M29
64 const char kPathTokenName
[] = "path";
65 const char kDomainTokenName
[] = "domain";
66 const char kExpiresTokenName
[] = "expires";
67 const char kMaxAgeTokenName
[] = "max-age";
68 const char kSecureTokenName
[] = "secure";
69 const char kHttpOnlyTokenName
[] = "httponly";
70 const char kPriorityTokenName
[] = "priority";
72 const char kTerminator
[] = "\n\r\0";
73 const int kTerminatorLen
= sizeof(kTerminator
) - 1;
74 const char kWhitespace
[] = " \t";
75 const char kValueSeparator
[] = ";";
76 const char kTokenSeparator
[] = ";=";
78 // Returns true if |c| occurs in |chars|
79 // TODO(erikwright): maybe make this take an iterator, could check for end also?
80 inline bool CharIsA(const char c
, const char* chars
) {
81 return strchr(chars
, c
) != NULL
;
83 // Seek the iterator to the first occurrence of a character in |chars|.
84 // Returns true if it hit the end, false otherwise.
85 inline bool SeekTo(std::string::const_iterator
* it
,
86 const std::string::const_iterator
& end
,
88 for (; *it
!= end
&& !CharIsA(**it
, chars
); ++(*it
)) {}
91 // Seek the iterator to the first occurrence of a character not in |chars|.
92 // Returns true if it hit the end, false otherwise.
93 inline bool SeekPast(std::string::const_iterator
* it
,
94 const std::string::const_iterator
& end
,
96 for (; *it
!= end
&& CharIsA(**it
, chars
); ++(*it
)) {}
99 inline bool SeekBackPast(std::string::const_iterator
* it
,
100 const std::string::const_iterator
& end
,
102 for (; *it
!= end
&& CharIsA(**it
, chars
); --(*it
)) {}
106 // Validate whether |value| is a valid token according to [RFC2616],
108 bool IsValidToken(const std::string
& value
) {
112 // Check that |value| has no separators.
113 std::string separators
= "()<>@,;:\\\"/[]?={} \t";
114 if (value
.find_first_of(separators
) != std::string::npos
)
117 // Check that |value| has no CTLs.
118 for (std::string::const_iterator i
= value
.begin(); i
!= value
.end(); ++i
) {
119 if ((*i
>= 0 && *i
<= 31) || *i
>= 127)
126 // Validate value, which may be according to RFC 6265
127 // cookie-value = *cookie-octet / ( DQUOTE *cookie-octet DQUOTE )
128 // cookie-octet = %x21 / %x23-2B / %x2D-3A / %x3C-5B / %x5D-7E
129 // ; US-ASCII characters excluding CTLs,
130 // ; whitespace DQUOTE, comma, semicolon,
132 bool IsValidCookieValue(const std::string
& value
) {
133 // Number of characters to skip in validation at beginning and end of string.
135 if (value
.size() >= 2 && *value
.begin() == '"' && *(value
.end()-1) == '"')
137 for (std::string::const_iterator i
= value
.begin() + skip
;
138 i
!= value
.end() - skip
; ++i
) {
141 (*i
>= 0x23 && *i
<= 0x2B) ||
142 (*i
>= 0x2D && *i
<= 0x3A) ||
143 (*i
>= 0x3C && *i
<= 0x5B) ||
144 (*i
>= 0x5D && *i
<= 0x7E));
151 bool IsValidCookieAttributeValue(const std::string
& value
) {
152 // The greatest common denominator of cookie attribute values is
153 // <any CHAR except CTLs or ";"> according to RFC 6265.
154 for (std::string::const_iterator i
= value
.begin(); i
!= value
.end(); ++i
) {
155 if ((*i
>= 0 && *i
<= 31) || *i
== ';')
165 ParsedCookie::ParsedCookie(const std::string
& cookie_line
)
174 if (cookie_line
.size() > kMaxCookieSize
) {
175 VLOG(1) << "Not parsing cookie, too large: " << cookie_line
.size();
179 ParseTokenValuePairs(cookie_line
);
184 ParsedCookie::~ParsedCookie() {
187 bool ParsedCookie::IsValid() const {
188 return !pairs_
.empty();
191 CookiePriority
ParsedCookie::Priority() const {
192 return (priority_index_
== 0) ? COOKIE_PRIORITY_DEFAULT
:
193 StringToCookiePriority(pairs_
[priority_index_
].second
);
196 bool ParsedCookie::SetName(const std::string
& name
) {
197 bool valid_token
= IsValidToken(name
);
198 UMA_HISTOGRAM_BOOLEAN("Cookie.SetNameVaildity", valid_token
);
202 pairs_
.push_back(std::make_pair("", ""));
203 pairs_
[0].first
= name
;
207 bool ParsedCookie::SetValue(const std::string
& value
) {
208 bool valid_cookie_value
= IsValidCookieValue(value
);
209 UMA_HISTOGRAM_BOOLEAN("Cookie.SetValueCookieValueValidity",
211 if (!valid_cookie_value
)
214 pairs_
.push_back(std::make_pair("", ""));
215 pairs_
[0].second
= value
;
219 bool ParsedCookie::SetPath(const std::string
& path
) {
220 return SetString(&path_index_
, kPathTokenName
, path
);
223 bool ParsedCookie::SetDomain(const std::string
& domain
) {
224 return SetString(&domain_index_
, kDomainTokenName
, domain
);
227 bool ParsedCookie::SetExpires(const std::string
& expires
) {
228 return SetString(&expires_index_
, kExpiresTokenName
, expires
);
231 bool ParsedCookie::SetMaxAge(const std::string
& maxage
) {
232 return SetString(&maxage_index_
, kMaxAgeTokenName
, maxage
);
235 bool ParsedCookie::SetIsSecure(bool is_secure
) {
236 return SetBool(&secure_index_
, kSecureTokenName
, is_secure
);
239 bool ParsedCookie::SetIsHttpOnly(bool is_http_only
) {
240 return SetBool(&httponly_index_
, kHttpOnlyTokenName
, is_http_only
);
243 bool ParsedCookie::SetPriority(const std::string
& priority
) {
244 return SetString(&priority_index_
, kPriorityTokenName
, priority
);
247 std::string
ParsedCookie::ToCookieLine() const {
249 for (PairList::const_iterator it
= pairs_
.begin();
250 it
!= pairs_
.end(); ++it
) {
253 out
.append(it
->first
);
254 if (it
->first
!= kSecureTokenName
&& it
->first
!= kHttpOnlyTokenName
) {
256 out
.append(it
->second
);
262 std::string::const_iterator
ParsedCookie::FindFirstTerminator(
263 const std::string
& s
) {
264 std::string::const_iterator end
= s
.end();
266 s
.find_first_of(std::string(kTerminator
, kTerminatorLen
));
267 if (term_pos
!= std::string::npos
) {
268 // We found a character we should treat as an end of string.
269 end
= s
.begin() + term_pos
;
274 bool ParsedCookie::ParseToken(std::string::const_iterator
* it
,
275 const std::string::const_iterator
& end
,
276 std::string::const_iterator
* token_start
,
277 std::string::const_iterator
* token_end
) {
278 DCHECK(it
&& token_start
&& token_end
);
279 std::string::const_iterator token_real_end
;
281 // Seek past any whitespace before the "token" (the name).
282 // token_start should point at the first character in the token
283 if (SeekPast(it
, end
, kWhitespace
))
284 return false; // No token, whitespace or empty.
287 // Seek over the token, to the token separator.
288 // token_real_end should point at the token separator, i.e. '='.
289 // If it == end after the seek, we probably have a token-value.
290 SeekTo(it
, end
, kTokenSeparator
);
291 token_real_end
= *it
;
293 // Ignore any whitespace between the token and the token separator.
294 // token_end should point after the last interesting token character,
295 // pointing at either whitespace, or at '=' (and equal to token_real_end).
296 if (*it
!= *token_start
) { // We could have an empty token name.
297 --(*it
); // Go back before the token separator.
298 // Skip over any whitespace to the first non-whitespace character.
299 SeekBackPast(it
, *token_start
, kWhitespace
);
305 // Seek us back to the end of the token.
306 *it
= token_real_end
;
310 void ParsedCookie::ParseValue(std::string::const_iterator
* it
,
311 const std::string::const_iterator
& end
,
312 std::string::const_iterator
* value_start
,
313 std::string::const_iterator
* value_end
) {
314 DCHECK(it
&& value_start
&& value_end
);
316 // Seek past any whitespace that might in-between the token and value.
317 SeekPast(it
, end
, kWhitespace
);
318 // value_start should point at the first character of the value.
321 // Just look for ';' to terminate ('=' allowed).
322 // We can hit the end, maybe they didn't terminate.
323 SeekTo(it
, end
, kValueSeparator
);
325 // Will be pointed at the ; seperator or the end.
328 // Ignore any unwanted whitespace after the value.
329 if (*value_end
!= *value_start
) { // Could have an empty value
331 SeekBackPast(value_end
, *value_start
, kWhitespace
);
336 std::string
ParsedCookie::ParseTokenString(const std::string
& token
) {
337 std::string::const_iterator it
= token
.begin();
338 std::string::const_iterator end
= FindFirstTerminator(token
);
340 std::string::const_iterator token_start
, token_end
;
341 if (ParseToken(&it
, end
, &token_start
, &token_end
))
342 return std::string(token_start
, token_end
);
343 return std::string();
346 std::string
ParsedCookie::ParseValueString(const std::string
& value
) {
347 std::string::const_iterator it
= value
.begin();
348 std::string::const_iterator end
= FindFirstTerminator(value
);
350 std::string::const_iterator value_start
, value_end
;
351 ParseValue(&it
, end
, &value_start
, &value_end
);
352 return std::string(value_start
, value_end
);
355 // Parse all token/value pairs and populate pairs_.
356 void ParsedCookie::ParseTokenValuePairs(const std::string
& cookie_line
) {
357 enum ParsedCookieStatus
{
358 PARSED_COOKIE_STATUS_NOTHING
= 0x0,
359 PARSED_COOKIE_STATUS_CONTROL_CHAR
= 0x1,
360 PARSED_COOKIE_STATUS_INVALID
= 0x2,
361 PARSED_COOKIE_STATUS_BOTH
=
362 PARSED_COOKIE_STATUS_CONTROL_CHAR
| PARSED_COOKIE_STATUS_INVALID
364 int parsed_cookie_status
= PARSED_COOKIE_STATUS_NOTHING
;
368 // Ok, here we go. We should be expecting to be starting somewhere
369 // before the cookie line, not including any header name...
370 std::string::const_iterator start
= cookie_line
.begin();
371 std::string::const_iterator it
= start
;
373 // TODO(erikwright): Make sure we're stripping \r\n in the network code.
374 // Then we can log any unexpected terminators.
375 std::string::const_iterator end
= FindFirstTerminator(cookie_line
);
377 for (int pair_num
= 0; pair_num
< kMaxPairs
&& it
!= end
; ++pair_num
) {
380 std::string::const_iterator token_start
, token_end
;
381 if (!ParseToken(&it
, end
, &token_start
, &token_end
))
384 if (it
== end
|| *it
!= '=') {
385 // We have a token-value, we didn't have any token name.
387 // For the first time around, we want to treat single values
388 // as a value with an empty name. (Mozilla bug 169091).
389 // IE seems to also have this behavior, ex "AAA", and "AAA=10" will
390 // set 2 different cookies, and setting "BBB" will then replace "AAA".
392 // Rewind to the beginning of what we thought was the token name,
393 // and let it get parsed as a value.
396 // Any not-first attribute we want to treat a value as a
397 // name with an empty value... This is so something like
398 // "secure;" will get parsed as a Token name, and not a value.
399 pair
.first
= std::string(token_start
, token_end
);
402 // We have a TOKEN=VALUE.
403 pair
.first
= std::string(token_start
, token_end
);
404 ++it
; // Skip past the '='.
407 // OK, now try to parse a value.
408 std::string::const_iterator value_start
, value_end
;
409 ParseValue(&it
, end
, &value_start
, &value_end
);
410 // OK, we're finished with a Token/Value.
411 pair
.second
= std::string(value_start
, value_end
);
413 if (!IsValidCookieAttributeValue(pair
.second
))
414 parsed_cookie_status
|= PARSED_COOKIE_STATUS_CONTROL_CHAR
;
415 if (!IsValidToken(pair
.second
))
416 parsed_cookie_status
|= PARSED_COOKIE_STATUS_INVALID
;
418 // From RFC2109: "Attributes (names) (attr) are case-insensitive."
420 StringToLowerASCII(&pair
.first
);
421 pairs_
.push_back(pair
);
423 // We've processed a token/value pair, we're either at the end of
424 // the string or a ValueSeparator like ';', which we want to skip.
429 UMA_HISTOGRAM_ENUMERATION("Cookie.ParsedCookieStatus", parsed_cookie_status
,
430 PARSED_COOKIE_STATUS_BOTH
+ 1);
433 void ParsedCookie::SetupAttributes() {
434 // We skip over the first token/value, the user supplied one.
435 for (size_t i
= 1; i
< pairs_
.size(); ++i
) {
436 if (pairs_
[i
].first
== kPathTokenName
) {
438 } else if (pairs_
[i
].first
== kDomainTokenName
) {
440 } else if (pairs_
[i
].first
== kExpiresTokenName
) {
442 } else if (pairs_
[i
].first
== kMaxAgeTokenName
) {
444 } else if (pairs_
[i
].first
== kSecureTokenName
) {
446 } else if (pairs_
[i
].first
== kHttpOnlyTokenName
) {
448 } else if (pairs_
[i
].first
== kPriorityTokenName
) {
451 /* some attribute we don't know or don't care about. */
456 bool ParsedCookie::SetString(size_t* index
,
457 const std::string
& key
,
458 const std::string
& value
) {
460 ClearAttributePair(*index
);
463 return SetAttributePair(index
, key
, value
);
467 bool ParsedCookie::SetBool(size_t* index
,
468 const std::string
& key
,
471 ClearAttributePair(*index
);
474 return SetAttributePair(index
, key
, std::string());
478 bool ParsedCookie::SetAttributePair(size_t* index
,
479 const std::string
& key
,
480 const std::string
& value
) {
481 bool valid_attribute_pair
= IsValidToken(key
) &&
482 IsValidCookieAttributeValue(value
);
483 UMA_HISTOGRAM_BOOLEAN("Cookie.SetAttributePairCharsValidity",
484 valid_attribute_pair
);
485 if (!valid_attribute_pair
)
490 pairs_
[*index
].second
= value
;
492 pairs_
.push_back(std::make_pair(key
, value
));
493 *index
= pairs_
.size() - 1;
498 void ParsedCookie::ClearAttributePair(size_t index
) {
499 // The first pair (name/value of cookie at pairs_[0]) cannot be cleared.
500 // Cookie attributes that don't have a value at the moment, are represented
501 // with an index being equal to 0.
505 size_t* indexes
[] = { &path_index_
, &domain_index_
, &expires_index_
,
506 &maxage_index_
, &secure_index_
, &httponly_index_
,
508 for (size_t i
= 0; i
< arraysize(indexes
); ++i
) {
509 if (*indexes
[i
] == index
)
511 else if (*indexes
[i
] > index
)
514 pairs_
.erase(pairs_
.begin() + index
);