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/strings/string_util.h"
52 const char kPathTokenName
[] = "path";
53 const char kDomainTokenName
[] = "domain";
54 const char kExpiresTokenName
[] = "expires";
55 const char kMaxAgeTokenName
[] = "max-age";
56 const char kSecureTokenName
[] = "secure";
57 const char kHttpOnlyTokenName
[] = "httponly";
58 const char kFirstPartyOnlyTokenName
[] = "first-party-only";
59 const char kPriorityTokenName
[] = "priority";
61 const char kTerminator
[] = "\n\r\0";
62 const int kTerminatorLen
= sizeof(kTerminator
) - 1;
63 const char kWhitespace
[] = " \t";
64 const char kValueSeparator
[] = ";";
65 const char kTokenSeparator
[] = ";=";
67 // Returns true if |c| occurs in |chars|
68 // TODO(erikwright): maybe make this take an iterator, could check for end also?
69 inline bool CharIsA(const char c
, const char* chars
) {
70 return strchr(chars
, c
) != NULL
;
72 // Seek the iterator to the first occurrence of a character in |chars|.
73 // Returns true if it hit the end, false otherwise.
74 inline bool SeekTo(std::string::const_iterator
* it
,
75 const std::string::const_iterator
& end
,
77 for (; *it
!= end
&& !CharIsA(**it
, chars
); ++(*it
)) {
81 // Seek the iterator to the first occurrence of a character not in |chars|.
82 // Returns true if it hit the end, false otherwise.
83 inline bool SeekPast(std::string::const_iterator
* it
,
84 const std::string::const_iterator
& end
,
86 for (; *it
!= end
&& CharIsA(**it
, chars
); ++(*it
)) {
90 inline bool SeekBackPast(std::string::const_iterator
* it
,
91 const std::string::const_iterator
& end
,
93 for (; *it
!= end
&& CharIsA(**it
, chars
); --(*it
)) {
98 // Validate whether |value| is a valid token according to [RFC2616],
100 bool IsValidToken(const std::string
& value
) {
104 // Check that |value| has no separators.
105 std::string separators
= "()<>@,;:\\\"/[]?={} \t";
106 if (value
.find_first_of(separators
) != std::string::npos
)
109 // Check that |value| has no CTLs.
110 for (std::string::const_iterator i
= value
.begin(); i
!= value
.end(); ++i
) {
111 if ((*i
>= 0 && *i
<= 31) || *i
>= 127)
118 // Validate value, which may be according to RFC 6265
119 // cookie-value = *cookie-octet / ( DQUOTE *cookie-octet DQUOTE )
120 // cookie-octet = %x21 / %x23-2B / %x2D-3A / %x3C-5B / %x5D-7E
121 // ; US-ASCII characters excluding CTLs,
122 // ; whitespace DQUOTE, comma, semicolon,
124 bool IsValidCookieValue(const std::string
& value
) {
125 // Number of characters to skip in validation at beginning and end of string.
127 if (value
.size() >= 2 && *value
.begin() == '"' && *(value
.end() - 1) == '"')
129 for (std::string::const_iterator i
= value
.begin() + skip
;
130 i
!= value
.end() - skip
; ++i
) {
132 (*i
== 0x21 || (*i
>= 0x23 && *i
<= 0x2B) ||
133 (*i
>= 0x2D && *i
<= 0x3A) || (*i
>= 0x3C && *i
<= 0x5B) ||
134 (*i
>= 0x5D && *i
<= 0x7E));
141 bool IsControlCharacter(unsigned char c
) {
145 bool IsValidCookieAttributeValue(const std::string
& value
) {
146 // The greatest common denominator of cookie attribute values is
147 // <any CHAR except CTLs or ";"> according to RFC 6265.
148 for (std::string::const_iterator i
= value
.begin(); i
!= value
.end(); ++i
) {
149 if (IsControlCharacter(*i
) || *i
== ';')
159 ParsedCookie::ParsedCookie(const std::string
& cookie_line
)
166 firstpartyonly_index_(0),
168 if (cookie_line
.size() > kMaxCookieSize
) {
169 VLOG(1) << "Not parsing cookie, too large: " << cookie_line
.size();
173 ParseTokenValuePairs(cookie_line
);
178 ParsedCookie::~ParsedCookie() {
181 bool ParsedCookie::IsValid() const {
182 return !pairs_
.empty();
185 CookiePriority
ParsedCookie::Priority() const {
186 return (priority_index_
== 0)
187 ? COOKIE_PRIORITY_DEFAULT
188 : StringToCookiePriority(pairs_
[priority_index_
].second
);
191 bool ParsedCookie::SetName(const std::string
& name
) {
192 if (!IsValidToken(name
))
195 pairs_
.push_back(std::make_pair("", ""));
196 pairs_
[0].first
= name
;
200 bool ParsedCookie::SetValue(const std::string
& value
) {
201 if (!IsValidCookieValue(value
))
204 pairs_
.push_back(std::make_pair("", ""));
205 pairs_
[0].second
= value
;
209 bool ParsedCookie::SetPath(const std::string
& path
) {
210 return SetString(&path_index_
, kPathTokenName
, path
);
213 bool ParsedCookie::SetDomain(const std::string
& domain
) {
214 return SetString(&domain_index_
, kDomainTokenName
, domain
);
217 bool ParsedCookie::SetExpires(const std::string
& expires
) {
218 return SetString(&expires_index_
, kExpiresTokenName
, expires
);
221 bool ParsedCookie::SetMaxAge(const std::string
& maxage
) {
222 return SetString(&maxage_index_
, kMaxAgeTokenName
, maxage
);
225 bool ParsedCookie::SetIsSecure(bool is_secure
) {
226 return SetBool(&secure_index_
, kSecureTokenName
, is_secure
);
229 bool ParsedCookie::SetIsHttpOnly(bool is_http_only
) {
230 return SetBool(&httponly_index_
, kHttpOnlyTokenName
, is_http_only
);
233 bool ParsedCookie::SetIsFirstPartyOnly(bool is_first_party_only
) {
234 return SetBool(&firstpartyonly_index_
, kFirstPartyOnlyTokenName
,
235 is_first_party_only
);
238 bool ParsedCookie::SetPriority(const std::string
& priority
) {
239 return SetString(&priority_index_
, kPriorityTokenName
, priority
);
242 std::string
ParsedCookie::ToCookieLine() const {
244 for (PairList::const_iterator it
= pairs_
.begin(); it
!= pairs_
.end(); ++it
) {
247 out
.append(it
->first
);
248 if (it
->first
!= kSecureTokenName
&& it
->first
!= kHttpOnlyTokenName
&&
249 it
->first
!= kFirstPartyOnlyTokenName
) {
251 out
.append(it
->second
);
257 std::string::const_iterator
ParsedCookie::FindFirstTerminator(
258 const std::string
& s
) {
259 std::string::const_iterator end
= s
.end();
260 size_t term_pos
= s
.find_first_of(std::string(kTerminator
, kTerminatorLen
));
261 if (term_pos
!= std::string::npos
) {
262 // We found a character we should treat as an end of string.
263 end
= s
.begin() + term_pos
;
268 bool ParsedCookie::ParseToken(std::string::const_iterator
* it
,
269 const std::string::const_iterator
& end
,
270 std::string::const_iterator
* token_start
,
271 std::string::const_iterator
* token_end
) {
272 DCHECK(it
&& token_start
&& token_end
);
273 std::string::const_iterator token_real_end
;
275 // Seek past any whitespace before the "token" (the name).
276 // token_start should point at the first character in the token
277 if (SeekPast(it
, end
, kWhitespace
))
278 return false; // No token, whitespace or empty.
281 // Seek over the token, to the token separator.
282 // token_real_end should point at the token separator, i.e. '='.
283 // If it == end after the seek, we probably have a token-value.
284 SeekTo(it
, end
, kTokenSeparator
);
285 token_real_end
= *it
;
287 // Ignore any whitespace between the token and the token separator.
288 // token_end should point after the last interesting token character,
289 // pointing at either whitespace, or at '=' (and equal to token_real_end).
290 if (*it
!= *token_start
) { // We could have an empty token name.
291 --(*it
); // Go back before the token separator.
292 // Skip over any whitespace to the first non-whitespace character.
293 SeekBackPast(it
, *token_start
, kWhitespace
);
299 // Seek us back to the end of the token.
300 *it
= token_real_end
;
304 void ParsedCookie::ParseValue(std::string::const_iterator
* it
,
305 const std::string::const_iterator
& end
,
306 std::string::const_iterator
* value_start
,
307 std::string::const_iterator
* value_end
) {
308 DCHECK(it
&& value_start
&& value_end
);
310 // Seek past any whitespace that might in-between the token and value.
311 SeekPast(it
, end
, kWhitespace
);
312 // value_start should point at the first character of the value.
315 // Just look for ';' to terminate ('=' allowed).
316 // We can hit the end, maybe they didn't terminate.
317 SeekTo(it
, end
, kValueSeparator
);
319 // Will be pointed at the ; seperator or the end.
322 // Ignore any unwanted whitespace after the value.
323 if (*value_end
!= *value_start
) { // Could have an empty value
325 SeekBackPast(value_end
, *value_start
, kWhitespace
);
330 std::string
ParsedCookie::ParseTokenString(const std::string
& token
) {
331 std::string::const_iterator it
= token
.begin();
332 std::string::const_iterator end
= FindFirstTerminator(token
);
334 std::string::const_iterator token_start
, token_end
;
335 if (ParseToken(&it
, end
, &token_start
, &token_end
))
336 return std::string(token_start
, token_end
);
337 return std::string();
340 std::string
ParsedCookie::ParseValueString(const std::string
& value
) {
341 std::string::const_iterator it
= value
.begin();
342 std::string::const_iterator end
= FindFirstTerminator(value
);
344 std::string::const_iterator value_start
, value_end
;
345 ParseValue(&it
, end
, &value_start
, &value_end
);
346 return std::string(value_start
, value_end
);
349 // Parse all token/value pairs and populate pairs_.
350 void ParsedCookie::ParseTokenValuePairs(const std::string
& cookie_line
) {
353 // Ok, here we go. We should be expecting to be starting somewhere
354 // before the cookie line, not including any header name...
355 std::string::const_iterator start
= cookie_line
.begin();
356 std::string::const_iterator it
= start
;
358 // TODO(erikwright): Make sure we're stripping \r\n in the network code.
359 // Then we can log any unexpected terminators.
360 std::string::const_iterator end
= FindFirstTerminator(cookie_line
);
362 for (int pair_num
= 0; pair_num
< kMaxPairs
&& it
!= end
; ++pair_num
) {
365 std::string::const_iterator token_start
, token_end
;
366 if (!ParseToken(&it
, end
, &token_start
, &token_end
))
369 if (it
== end
|| *it
!= '=') {
370 // We have a token-value, we didn't have any token name.
372 // For the first time around, we want to treat single values
373 // as a value with an empty name. (Mozilla bug 169091).
374 // IE seems to also have this behavior, ex "AAA", and "AAA=10" will
375 // set 2 different cookies, and setting "BBB" will then replace "AAA".
377 // Rewind to the beginning of what we thought was the token name,
378 // and let it get parsed as a value.
381 // Any not-first attribute we want to treat a value as a
382 // name with an empty value... This is so something like
383 // "secure;" will get parsed as a Token name, and not a value.
384 pair
.first
= std::string(token_start
, token_end
);
387 // We have a TOKEN=VALUE.
388 pair
.first
= std::string(token_start
, token_end
);
389 ++it
; // Skip past the '='.
392 // OK, now try to parse a value.
393 std::string::const_iterator value_start
, value_end
;
394 ParseValue(&it
, end
, &value_start
, &value_end
);
396 // OK, we're finished with a Token/Value.
397 pair
.second
= std::string(value_start
, value_end
);
399 // From RFC2109: "Attributes (names) (attr) are case-insensitive."
401 base::StringToLowerASCII(&pair
.first
);
402 // Ignore Set-Cookie directives contaning control characters. See
403 // http://crbug.com/238041.
404 if (!IsValidCookieAttributeValue(pair
.first
) ||
405 !IsValidCookieAttributeValue(pair
.second
)) {
410 pairs_
.push_back(pair
);
412 // We've processed a token/value pair, we're either at the end of
413 // the string or a ValueSeparator like ';', which we want to skip.
419 void ParsedCookie::SetupAttributes() {
420 // Ignore Set-Cookie directive where name and value are both empty.
421 if (pairs_
[0].first
.empty() && pairs_
[0].second
.empty()) {
426 // We skip over the first token/value, the user supplied one.
427 for (size_t i
= 1; i
< pairs_
.size(); ++i
) {
428 if (pairs_
[i
].first
== kPathTokenName
) {
430 } else if (pairs_
[i
].first
== kDomainTokenName
) {
432 } else if (pairs_
[i
].first
== kExpiresTokenName
) {
434 } else if (pairs_
[i
].first
== kMaxAgeTokenName
) {
436 } else if (pairs_
[i
].first
== kSecureTokenName
) {
438 } else if (pairs_
[i
].first
== kHttpOnlyTokenName
) {
440 } else if (pairs_
[i
].first
== kFirstPartyOnlyTokenName
) {
441 firstpartyonly_index_
= i
;
442 } else if (pairs_
[i
].first
== kPriorityTokenName
) {
445 /* some attribute we don't know or don't care about. */
450 bool ParsedCookie::SetString(size_t* index
,
451 const std::string
& key
,
452 const std::string
& value
) {
454 ClearAttributePair(*index
);
457 return SetAttributePair(index
, key
, value
);
461 bool ParsedCookie::SetBool(size_t* index
, const std::string
& key
, bool value
) {
463 ClearAttributePair(*index
);
466 return SetAttributePair(index
, key
, std::string());
470 bool ParsedCookie::SetAttributePair(size_t* index
,
471 const std::string
& key
,
472 const std::string
& value
) {
473 if (!(IsValidToken(key
) && IsValidCookieAttributeValue(value
)))
478 pairs_
[*index
].second
= value
;
480 pairs_
.push_back(std::make_pair(key
, value
));
481 *index
= pairs_
.size() - 1;
486 void ParsedCookie::ClearAttributePair(size_t index
) {
487 // The first pair (name/value of cookie at pairs_[0]) cannot be cleared.
488 // Cookie attributes that don't have a value at the moment, are represented
489 // with an index being equal to 0.
493 size_t* indexes
[] = {&path_index_
,
499 &firstpartyonly_index_
,
501 for (size_t i
= 0; i
< arraysize(indexes
); ++i
) {
502 if (*indexes
[i
] == index
)
504 else if (*indexes
[i
] > index
)
507 pairs_
.erase(pairs_
.begin() + index
);