master
1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24#include "curl_setup.h"
26#if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
28#include "urldata.h"
29#include "cookie.h"
30#include "psl.h"
31#include "curl_trc.h"
32#include "transfer.h"
33#include "slist.h"
34#include "curl_share.h"
35#include "strcase.h"
36#include "curl_fopen.h"
37#include "curl_get_line.h"
38#include "curl_memrchr.h"
39#include "parsedate.h"
40#include "curlx/strdup.h"
41#include "curlx/dynbuf.h"
42#include "llist.h"
43#include "curlx/strparse.h"
45/* number of seconds in 400 days */
46#define COOKIES_MAXAGE (400 * 24 * 3600)
48/* Make sure cookies never expire further away in time than 400 days into the
49 future. (from RFC6265bis draft-19)
51 For the sake of easier testing, align the capped time to an even 60 second
52 boundary. */
53static void cap_expires(time_t now, struct Cookie *co)
54{
55 if(co->expires && (TIME_T_MAX - COOKIES_MAXAGE - 30) > now) {
56 timediff_t cap = now + COOKIES_MAXAGE;
57 if(co->expires > cap) {
58 cap += 30;
59 co->expires = (cap / 60) * 60;
60 }
61 }
62}
64static void freecookie(struct Cookie *co, bool maintoo)
65{
66 curlx_free(co->domain);
67 curlx_free(co->path);
68 curlx_free(co->name);
69 curlx_free(co->value);
70 if(maintoo)
71 curlx_free(co);
72}
74static bool cookie_tailmatch(const char *cookie_domain,
75 const size_t cookie_domain_len,
76 const char *hostname)
77{
78 size_t hostname_len = strlen(hostname);
80 if(hostname_len < cookie_domain_len)
81 return FALSE;
83 if(!curl_strnequal(cookie_domain,
84 hostname + hostname_len - cookie_domain_len,
85 cookie_domain_len))
86 return FALSE;
88 /*
89 * A lead char of cookie_domain is not '.'.
90 * RFC6265 4.1.2.3. The Domain Attribute says:
91 * For example, if the value of the Domain attribute is
92 * "example.com", the user agent will include the cookie in the Cookie
93 * header when making HTTP requests to example.com, www.example.com, and
94 * www.corp.example.com.
95 */
96 if(hostname_len == cookie_domain_len)
97 return TRUE;
98 if('.' == *(hostname + hostname_len - cookie_domain_len - 1))
99 return TRUE;
100 return FALSE;
101}
103/*
104 * matching cookie path and URL path
105 * RFC6265 5.1.4 Paths and Path-Match
106 */
107static bool pathmatch(const char *cookie_path, const char *uri_path)
108{
109 size_t cookie_path_len;
110 size_t uri_path_len;
111 bool ret = FALSE;
113 /* cookie_path must not have last '/' separator. ex: /sample */
114 cookie_path_len = strlen(cookie_path);
115 if(cookie_path_len == 1) {
116 /* cookie_path must be '/' */
117 return TRUE;
118 }
120 /* #-fragments are already cut off! */
121 if(strlen(uri_path) == 0 || uri_path[0] != '/')
122 uri_path = "/";
124 /*
125 * here, RFC6265 5.1.4 says
126 * 4. Output the characters of the uri-path from the first character up
127 * to, but not including, the right-most %x2F ("/").
128 * but URL path /hoge?fuga=xxx means /hoge/index.cgi?fuga=xxx in some site
129 * without redirect.
130 * Ignore this algorithm because /hoge is uri path for this case
131 * (uri path is not /).
132 */
134 uri_path_len = strlen(uri_path);
136 if(uri_path_len < cookie_path_len)
137 goto pathmatched;
139 /* not using checkprefix() because matching should be case-sensitive */
140 if(strncmp(cookie_path, uri_path, cookie_path_len))
141 goto pathmatched;
143 /* The cookie-path and the uri-path are identical. */
144 if(cookie_path_len == uri_path_len) {
145 ret = TRUE;
146 goto pathmatched;
147 }
149 /* here, cookie_path_len < uri_path_len */
150 if(uri_path[cookie_path_len] == '/') {
151 ret = TRUE;
152 goto pathmatched;
153 }
155pathmatched:
156 return ret;
157}
159/*
160 * Return the top-level domain, for optimal hashing.
161 */
162static const char *get_top_domain(const char * const domain, size_t *outlen)
163{
164 size_t len = 0;
165 const char *first = NULL, *last;
167 if(domain) {
168 len = strlen(domain);
169 last = memrchr(domain, '.', len);
170 if(last) {
171 first = memrchr(domain, '.', (last - domain));
172 if(first)
173 len -= (++first - domain);
174 }
175 }
177 if(outlen)
178 *outlen = len;
180 return first ? first : domain;
181}
183/* Avoid C1001, an "internal error" with MSVC14 */
184#if defined(_MSC_VER) && (_MSC_VER == 1900)
185#pragma optimize("", off)
186#endif
188/*
189 * A case-insensitive hash for the cookie domains.
190 */
191static size_t cookie_hash_domain(const char *domain, const size_t len)
192{
193 const char *end = domain + len;
194 size_t h = 5381;
196 while(domain < end) {
197 size_t j = (size_t)Curl_raw_toupper(*domain++);
198 h += h << 5;
199 h ^= j;
200 }
202 return (h % COOKIE_HASH_SIZE);
203}
205#if defined(_MSC_VER) && (_MSC_VER == 1900)
206#pragma optimize("", on)
207#endif
209/*
210 * Hash this domain.
211 */
212static size_t cookiehash(const char * const domain)
213{
214 const char *top;
215 size_t len;
217 if(!domain || Curl_host_is_ipnum(domain))
218 return 0;
220 top = get_top_domain(domain, &len);
221 return cookie_hash_domain(top, len);
222}
224/*
225 * cookie path sanitize
226 */
227static char *sanitize_cookie_path(const char *cookie_path, size_t len)
228{
229 /* some sites send path attribute within '"'. */
230 if(len && (cookie_path[0] == '\"')) {
231 cookie_path++;
232 len--;
234 if(len && (cookie_path[len - 1] == '\"'))
235 len--;
236 }
238 /* RFC6265 5.2.4 The Path Attribute */
239 if(!len || (cookie_path[0] != '/'))
240 /* Let cookie-path be the default-path. */
241 return curlx_strdup("/");
243 /* remove trailing slash when path is non-empty */
244 /* convert /hoge/ to /hoge */
245 if(len > 1 && cookie_path[len - 1] == '/')
246 len--;
248 return curlx_memdup0(cookie_path, len);
249}
251/*
252 * strstore
253 *
254 * A thin wrapper around curlx_memdup0().
255 */
256static CURLcode strstore(char **str, const char *newstr, size_t len)
257{
258 DEBUGASSERT(str);
259 *str = curlx_memdup0(newstr, len);
260 if(!*str)
261 return CURLE_OUT_OF_MEMORY;
262 return CURLE_OK;
263}
265/*
266 * remove_expired
267 *
268 * Remove expired cookies from the hash by inspecting the expires timestamp on
269 * each cookie in the hash, freeing and deleting any where the timestamp is in
270 * the past. If the cookiejar has recorded the next timestamp at which one or
271 * more cookies expire, then processing will exit early in case this timestamp
272 * is in the future.
273 */
274static void remove_expired(struct CookieInfo *ci)
275{
276 struct Cookie *co;
277 curl_off_t now = (curl_off_t)time(NULL);
278 unsigned int i;
280 /*
281 * If the earliest expiration timestamp in the jar is in the future we can
282 * skip scanning the whole jar and instead exit early as there will not be
283 * any cookies to evict. If we need to evict, reset the next_expiration
284 * counter in order to track the next one. In case the recorded first
285 * expiration is the max offset, then perform the safe fallback of checking
286 * all cookies.
287 */
288 if(now < ci->next_expiration &&
289 ci->next_expiration != CURL_OFF_T_MAX)
290 return;
291 else
292 ci->next_expiration = CURL_OFF_T_MAX;
294 for(i = 0; i < COOKIE_HASH_SIZE; i++) {
295 struct Curl_llist_node *n;
296 struct Curl_llist_node *e = NULL;
298 for(n = Curl_llist_head(&ci->cookielist[i]); n; n = e) {
299 co = Curl_node_elem(n);
300 e = Curl_node_next(n);
301 if(co->expires) {
302 if(co->expires < now) {
303 Curl_node_remove(n);
304 freecookie(co, TRUE);
305 ci->numcookies--;
306 }
307 else if(co->expires < ci->next_expiration)
308 /*
309 * If this cookie has an expiration timestamp earlier than what we
310 * have seen so far then record it for the next round of expirations.
311 */
312 ci->next_expiration = co->expires;
313 }
314 }
315 }
316}
318#ifndef USE_LIBPSL
319/* Make sure domain contains a dot or is localhost. */
320static bool bad_domain(const char *domain, size_t len)
321{
322 if((len == 9) && curl_strnequal(domain, "localhost", 9))
323 return FALSE;
324 else {
325 /* there must be a dot present, but that dot must not be a trailing dot */
326 const char *dot = memchr(domain, '.', len);
327 if(dot) {
328 size_t i = dot - domain;
329 if((len - i) > 1)
330 /* the dot is not the last byte */
331 return FALSE;
332 }
333 }
334 return TRUE;
335}
336#endif
338/* RFC 6265 section 4.1.1 says a server should accept this range:
340 cookie-octet = %x21 / %x23-2B / %x2D-3A / %x3C-5B / %x5D-7E
342 Yet, Firefox and Chrome as of June 2022 accept space, comma and
343 double-quotes fine. The prime reason for filtering out control bytes is that
344 some HTTP servers return 400 for requests that contain such.
345 */
346static bool invalid_octets(const char *ptr, size_t len)
347{
348 const unsigned char *p = (const unsigned char *)ptr;
349 /* Reject all bytes \x01 - \x1f + \x7f */
350 while(len && *p) {
351 if((*p < 0x20) || (*p == 0x7f))
352 return TRUE;
353 p++;
354 len--;
355 }
356 return FALSE;
357}
359/* The maximum length we accept a date string for the 'expire' keyword. The
360 standard date formats are within the 30 bytes range. This adds an extra
361 margin to make sure it realistically works with what is used out there. */
362#define MAX_DATE_LENGTH 80
364#define COOKIE_NAME 0
365#define COOKIE_VALUE 1
366#define COOKIE_DOMAIN 2
367#define COOKIE_PATH 3
369#define COOKIE_PIECES 4 /* the list above */
371static CURLcode storecookie(struct Cookie *co, const struct Curl_str *cp,
372 const char *path, const char *domain)
373{
374 CURLcode result;
375 result = strstore(&co->name, curlx_str(&cp[COOKIE_NAME]),
376 curlx_strlen(&cp[COOKIE_NAME]));
377 if(!result)
378 result = strstore(&co->value, curlx_str(&cp[COOKIE_VALUE]),
379 curlx_strlen(&cp[COOKIE_VALUE]));
380 if(!result) {
381 size_t plen = 0;
382 if(curlx_strlen(&cp[COOKIE_PATH])) {
383 path = curlx_str(&cp[COOKIE_PATH]);
384 plen = curlx_strlen(&cp[COOKIE_PATH]);
385 }
386 else if(path) {
387 /* No path was given in the header line, set the default */
388 const char *endslash = strrchr(path, '/');
389 if(endslash)
390 plen = endslash - path + 1; /* include end slash */
391 else
392 plen = strlen(path);
393 }
395 if(path) {
396 co->path = sanitize_cookie_path(path, plen);
397 if(!co->path)
398 result = CURLE_OUT_OF_MEMORY;
399 }
400 }
401 if(!result) {
402 if(curlx_strlen(&cp[COOKIE_DOMAIN]))
403 result = strstore(&co->domain, curlx_str(&cp[COOKIE_DOMAIN]),
404 curlx_strlen(&cp[COOKIE_DOMAIN]));
405 else if(domain) {
406 /* no domain was given in the header line, set the default */
407 co->domain = curlx_strdup(domain);
408 if(!co->domain)
409 result = CURLE_OUT_OF_MEMORY;
410 }
411 }
412 return result;
413}
415/*
416 * Parse the first name/value pair of the cookie header, which is the actual
417 * cookie name and value.
418 */
419static bool parse_first_pair(struct Curl_easy *data, struct Cookie *co,
420 struct Curl_str *cookie,
421 struct Curl_str *name,
422 struct Curl_str *val,
423 bool sep)
424{
425 /* The first name/value pair is the actual cookie name */
426 if(!sep || !curlx_strlen(name)) {
427 infof(data, "invalid cookie, dropped");
428 return FALSE;
429 }
431 /*
432 * Check for too long individual name or contents. Chrome and Firefox
433 * support 4095 or 4096 bytes combo
434 */
435 if((curlx_strlen(name) + curlx_strlen(val)) > MAX_NAME) {
436 infof(data, "oversized cookie dropped, name/val %zu + %zu bytes",
437 curlx_strlen(name), curlx_strlen(val));
438 return FALSE;
439 }
441 /* Check if we have a reserved prefix set. */
442 if(!strncmp("__Secure-", curlx_str(name), 9))
443 co->prefix_secure = TRUE;
444 else if(!strncmp("__Host-", curlx_str(name), 7))
445 co->prefix_host = TRUE;
447 cookie[COOKIE_NAME] = *name;
448 cookie[COOKIE_VALUE] = *val;
449 return TRUE;
450}
452static bool parse_domain(struct Curl_easy *data, struct Cookie *co,
453 struct Curl_str *cookie_domain,
454 struct Curl_str *val,
455 const char **domainp)
456{
457 bool is_ip;
458 const char *domain = *domainp;
459 const char *v = curlx_str(val);
460 /*
461 * Now, we make sure that our host is within the given domain, or
462 * the given domain is not valid and thus cannot be set.
463 */
465 if('.' == *v)
466 curlx_str_nudge(val, 1);
468#ifndef USE_LIBPSL
469 /*
470 * Without PSL we do not know when the incoming cookie is set on a
471 * TLD or otherwise "protected" suffix. To reduce risk, we require a
472 * dot OR the exact hostname being "localhost".
473 */
474 if(bad_domain(curlx_str(val), curlx_strlen(val))) {
475 *domainp = ":";
476 domain = ":";
477 }
478#endif
480 is_ip = Curl_host_is_ipnum(domain ? domain : curlx_str(val));
482 if(!domain ||
483 (is_ip &&
484 !strncmp(curlx_str(val), domain, curlx_strlen(val)) &&
485 (curlx_strlen(val) == strlen(domain))) ||
486 (!is_ip && cookie_tailmatch(curlx_str(val),
487 curlx_strlen(val), domain))) {
488 *cookie_domain = *val;
489 if(!is_ip)
490 co->tailmatch = TRUE; /* we always do that if the domain name was
491 given */
492 }
493 else {
494 /*
495 * We did not get a tailmatch and then the attempted set domain is
496 * not a domain to which the current host belongs. Mark as bad.
497 */
498 infof(data, "skipped cookie with bad tailmatch domain: %s",
499 curlx_str(val));
500 return FALSE;
501 }
502 return TRUE;
503}
505static void parse_maxage(struct Cookie *co, struct Curl_str *val,
506 time_t *nowp)
507{
508 int rc;
509 const char *maxage = curlx_str(val);
510 if(*maxage == '\"')
511 maxage++;
512 rc = curlx_str_number(&maxage, &co->expires, CURL_OFF_T_MAX);
513 if(!*nowp)
514 *nowp = time(NULL);
515 switch(rc) {
516 case STRE_OVERFLOW:
517 /* overflow, used max value */
518 co->expires = CURL_OFF_T_MAX;
519 break;
520 default:
521 /* negative or otherwise bad, expire */
522 co->expires = 1;
523 break;
524 case STRE_OK:
525 if(!co->expires)
526 co->expires = 1; /* expire now */
527 else if(CURL_OFF_T_MAX - *nowp < co->expires)
528 /* would overflow */
529 co->expires = CURL_OFF_T_MAX;
530 else
531 co->expires += *nowp;
532 break;
533 }
534 cap_expires(*nowp, co);
535}
537static void parse_expires(struct Cookie *co, struct Curl_str *val,
538 time_t *nowp)
539{
540 /*
541 * Let max-age have priority.
542 *
543 * If the date cannot get parsed for whatever reason, the cookie
544 * will be treated as a session cookie
545 */
546 if(!co->expires && (curlx_strlen(val) < MAX_DATE_LENGTH)) {
547 char dbuf[MAX_DATE_LENGTH + 1];
548 time_t date = 0;
549 memcpy(dbuf, curlx_str(val), curlx_strlen(val));
550 dbuf[curlx_strlen(val)] = 0;
551 if(!Curl_getdate_capped(dbuf, &date)) {
552 if(!date)
553 date++;
554 co->expires = (curl_off_t)date;
555 }
556 else
557 co->expires = 0;
558 if(!*nowp)
559 *nowp = time(NULL);
560 cap_expires(*nowp, co);
561 }
562}
564/* this function returns errors on OOM etc, not for cookie format problems */
565static CURLcode
566parse_cookie_header(struct Curl_easy *data,
567 struct Cookie *co,
568 bool *okay, /* if the cookie was fine */
569 const char *ptr, /* the header */
570 const char *domain, /* default domain */
571 /* full path used when this cookie is set */
572 const char *path,
573 bool secure_origin)
574{
575 /* This line was read off an HTTP-header */
576 time_t now = 0;
577 size_t linelength = strlen(ptr);
578 CURLcode result = CURLE_OK;
579 struct Curl_str cookie[COOKIE_PIECES];
580 *okay = FALSE;
581 if(linelength > MAX_COOKIE_LINE)
582 /* discard overly long lines at once */
583 return CURLE_OK;
585 /* memset instead of initializer because gcc 4.8.1 is silly */
586 memset(cookie, 0, sizeof(cookie));
587 do {
588 struct Curl_str name;
590 /* we have a <name>=<value> pair or a stand-alone word here */
591 int rc = curlx_str_cspn(&ptr, &name, ";\r\n=");
592 if(!rc || (rc == STRE_SHORT)) {
593 struct Curl_str val;
594 bool sep = FALSE;
595 curlx_str_trimblanks(&name);
597 if(invalid_octets(curlx_str(&name), curlx_strlen(&name))) {
598 infof(data, "invalid octets in name, cookie dropped");
599 return CURLE_OK;
600 }
602 if(!curlx_str_single(&ptr, '=')) {
603 sep = TRUE; /* a '=' was used */
604 if(!curlx_str_cspn(&ptr, &val, ";\r\n"))
605 curlx_str_trimblanks(&val);
607 if(invalid_octets(curlx_str(&val), curlx_strlen(&val))) {
608 infof(data, "invalid octets in value, cookie dropped");
609 return CURLE_OK;
610 }
611 }
612 else
613 curlx_str_init(&val);
615 if(!curlx_strlen(&cookie[COOKIE_NAME])) {
616 if(!parse_first_pair(data, co, cookie, &name, &val, sep))
617 return CURLE_OK;
618 }
620 /* whether there's separator or not */
621 else if(curlx_str_casecompare(&name, "secure")) {
622 if(secure_origin)
623 co->secure = TRUE;
624 else {
625 infof(data, "skipped cookie because not 'secure'");
626 return CURLE_OK;
627 }
628 }
629 else if(curlx_str_casecompare(&name, "httponly"))
630 co->httponly = TRUE;
631 else if(sep) {
632 /* only if a separating equals sign is present */
633 if(curlx_str_casecompare(&name, "path"))
634 cookie[COOKIE_PATH] = val;
635 else if(curlx_str_casecompare(&name, "domain")) {
636 if(curlx_strlen(&val) &&
637 !parse_domain(data, co, &cookie[COOKIE_DOMAIN], &val, &domain))
638 return CURLE_OK;
639 }
640 else if(curlx_str_casecompare(&name, "max-age") && curlx_strlen(&val))
641 parse_maxage(co, &val, &now);
642 else if(curlx_str_casecompare(&name, "expires") && curlx_strlen(&val))
643 parse_expires(co, &val, &now);
644 }
645 }
646 } while(!curlx_str_single(&ptr, ';'));
648 if(curlx_strlen(&cookie[COOKIE_NAME])) {
649 /* the header was fine, now store the data */
650 result = storecookie(co, &cookie[0], path, domain);
651 if(!result)
652 *okay = TRUE;
653 }
654 return result;
655}
657static CURLcode parse_netscape(struct Cookie *co,
658 const struct CookieInfo *ci,
659 bool *okay,
660 const char *lineptr,
661 bool secure_origin)
662{
663 /*
664 * This line is NOT an HTTP header style line, we do offer support for
665 * reading the odd netscape cookies-file format here
666 */
667 const char *ptr, *next;
668 int fields;
669 size_t len;
670 *okay = FALSE;
672 /*
673 * In 2008, Internet Explorer introduced HTTP-only cookies to prevent XSS
674 * attacks. Cookies marked httpOnly are not accessible to JavaScript. In
675 * Firefox's cookie files, they are prefixed #HttpOnly_ and the rest
676 * remains as usual, so we skip 10 characters of the line.
677 */
678 if(!strncmp(lineptr, "#HttpOnly_", 10)) {
679 lineptr += 10;
680 co->httponly = TRUE;
681 }
683 if(lineptr[0] == '#')
684 /* do not even try the comments */
685 return CURLE_OK;
687 /*
688 * Now loop through the fields and init the struct we already have
689 * allocated
690 */
691 fields = 0;
692 for(next = lineptr; next; fields++) {
693 ptr = next;
694 len = strcspn(ptr, "\t\r\n");
695 next = (ptr[len] == '\t' ? &ptr[len + 1] : NULL);
696 switch(fields) {
697 case 0:
698 if(ptr[0] == '.') { /* skip preceding dots */
699 ptr++;
700 len--;
701 }
702 co->domain = curlx_memdup0(ptr, len);
703 if(!co->domain)
704 return CURLE_OUT_OF_MEMORY;
705 break;
706 case 1:
707 /*
708 * flag: A TRUE/FALSE value indicating if all machines within a given
709 * domain can access the variable. Set TRUE when the cookie says
710 * .example.com and to false when the domain is complete www.example.com
711 */
712 co->tailmatch = !!curl_strnequal(ptr, "TRUE", len);
713 break;
714 case 2:
715 /* The file format allows the path field to remain not filled in */
716 if(strncmp("TRUE", ptr, len) && strncmp("FALSE", ptr, len)) {
717 /* only if the path does not look like a boolean option! */
718 co->path = sanitize_cookie_path(ptr, len);
719 if(!co->path)
720 return CURLE_OUT_OF_MEMORY;
721 break;
722 }
723 else {
724 /* this does not look like a path, make one up! */
725 co->path = curlx_strdup("/");
726 if(!co->path)
727 return CURLE_OUT_OF_MEMORY;
728 }
729 fields++; /* add a field and fall down to secure */
730 FALLTHROUGH();
731 case 3:
732 co->secure = FALSE;
733 if(curl_strnequal(ptr, "TRUE", len)) {
734 if(secure_origin || ci->running)
735 co->secure = TRUE;
736 else
737 return CURLE_OK;
738 }
739 break;
740 case 4:
741 if(curlx_str_number(&ptr, &co->expires, CURL_OFF_T_MAX))
742 return CURLE_OK;
743 break;
744 case 5:
745 co->name = curlx_memdup0(ptr, len);
746 if(!co->name)
747 return CURLE_OUT_OF_MEMORY;
748 else {
749 /* For Netscape file format cookies we check prefix on the name.
750 These prefixes are matched case sensitively, same as on the
751 header path and as the 6265bis document specifies. */
752 if(!strncmp("__Secure-", co->name, 9))
753 co->prefix_secure = TRUE;
754 else if(!strncmp("__Host-", co->name, 7))
755 co->prefix_host = TRUE;
756 }
757 break;
758 case 6:
759 co->value = curlx_memdup0(ptr, len);
760 if(!co->value)
761 return CURLE_OUT_OF_MEMORY;
762 break;
763 }
764 }
765 if(fields == 6) {
766 /* we got a cookie with blank contents, fix it */
767 co->value = curlx_strdup("");
768 if(!co->value)
769 return CURLE_OUT_OF_MEMORY;
770 else
771 fields++;
772 }
774 if(fields != 7)
775 /* we did not find the sufficient number of fields */
776 return CURLE_OK;
778 /* Reject control octets in the name or value, matching the filtering done
779 for cookies set over HTTP. A cookie loaded from a file is later sent in
780 request headers, so the same bytes that make a server reject a request
781 must not slip in through the file. */
782 if(invalid_octets(co->name, strlen(co->name)) ||
783 invalid_octets(co->value, strlen(co->value)))
784 return CURLE_OK;
786 *okay = TRUE;
787 return CURLE_OK;
788}
790static bool is_public_suffix(struct Curl_easy *data,
791 struct Cookie *co,
792 const char *domain)
793{
794#ifdef USE_LIBPSL
795 /*
796 * Check if the domain is a Public Suffix and if yes, ignore the cookie.
797 * 'domain' is NULL when the cookie is loaded from file or
798 * CURLOPT_COOKIELIST.
799 */
800 DEBUGASSERT(data);
801 DEBUGASSERT(co);
802 DEBUGF(infof(data, "PSL check set-cookie '%s' for domain=%s in %s",
803 co->name, co->domain ? co->domain : "[blank]",
804 domain ? domain : "[file]"));
805 if(!co->domain || Curl_host_is_ipnum(co->domain))
806 return FALSE;
808 else {
809 bool acceptable = FALSE;
810 char lcase[256];
811 char lcookie[256];
812 size_t dlen = domain ? strlen(domain) : 0;
813 size_t clen = strlen(co->domain);
815 /* trim trailing dots */
816 if(dlen && (domain[dlen - 1] == '.'))
817 dlen--;
818 if(clen && (co->domain[clen - 1] == '.'))
819 clen--;
821 if((dlen < sizeof(lcase)) && (clen < sizeof(lcookie))) {
822 const psl_ctx_t *psl = Curl_psl_use(data);
823 if(psl) {
824 /* the PSL check requires lowercase domain name and pattern */
825 Curl_strntolower(lcookie, co->domain, clen);
826 lcookie[clen] = 0;
827 if(domain) {
828 Curl_strntolower(lcase, domain, dlen);
829 lcase[dlen] = 0;
830 acceptable = psl_is_cookie_domain_acceptable(psl, lcase, lcookie);
832 /* if the cookie is acceptable, but is set for a PSL domain, then it
833 cannot be tailmatching */
834 if(acceptable && co->tailmatch &&
835 curl_strequal(co->domain, domain) &&
836 psl_is_public_suffix(psl, lcookie))
837 co->tailmatch = FALSE;
838 }
839 else {
840 bool suffix = psl_is_public_suffix(psl, lcookie);
841 acceptable = TRUE;
842 if(suffix)
843 /* switch off tailmatching when set for a PSL domain */
844 co->tailmatch = FALSE;
845 }
846 Curl_psl_release(data);
847 }
848 else
849 infof(data, "libpsl problem, rejecting cookie for safety");
850 }
852 if(!acceptable) {
853 infof(data, "cookie '%s' dropped, domain '%s' must not "
854 "set cookies for '%s'", co->name,
855 domain ? domain : "[file]", co->domain);
856 return TRUE;
857 }
858 }
859#else
860 (void)data;
861 (void)co;
862 (void)domain;
863 DEBUGF(infof(data, "NO PSL to check set-cookie '%s' for domain=%s in %s",
864 co->name, co->domain, domain ? domain : "[file]"));
865#endif
866 return FALSE;
867}
869/* returns TRUE when replaced */
870static bool replace_existing(struct Curl_easy *data,
871 struct Cookie *co,
872 const struct CookieInfo *ci,
873 bool secure,
874 bool *replacep)
875{
876 bool replace_old = FALSE;
877 struct Curl_llist_node *replace_n = NULL;
878 struct Curl_llist_node *n;
879 size_t myhash = cookiehash(co->domain);
880 for(n = Curl_llist_head(&ci->cookielist[myhash]); n; n = Curl_node_next(n)) {
881 struct Cookie *clist = Curl_node_elem(n);
882 if(!strcmp(clist->name, co->name)) {
883 /* the names are identical */
884 bool matching_domains = FALSE;
886 if(clist->domain && co->domain) {
887 if(cookie_tailmatch(clist->domain, strlen(clist->domain),
888 co->domain) ||
889 cookie_tailmatch(co->domain, strlen(co->domain), clist->domain))
890 /* The existing one is a tail of the new or vice versa */
891 matching_domains = TRUE;
892 }
893 else if(!clist->domain && !co->domain)
894 matching_domains = TRUE;
896 if(matching_domains && /* the domains were identical */
897 clist->path && co->path && /* both have paths */
898 clist->secure && !co->secure && !secure) {
899 size_t cllen;
900 const char *sep = NULL;
902 /*
903 * A non-secure cookie may not overlay an existing secure cookie.
904 * For an existing cookie "a" with path "/login", refuse a new
905 * cookie "a" with for example path "/login/en", while the path
906 * "/loginhelper" is ok.
907 */
909 DEBUGASSERT(clist->path[0]);
910 if(clist->path[0])
911 sep = strchr(clist->path + 1, '/');
912 if(sep)
913 cllen = sep - clist->path;
914 else
915 cllen = strlen(clist->path);
917 if(!strncmp(clist->path, co->path, cllen)) {
918 infof(data, "cookie '%s' for domain '%s' dropped, would "
919 "overlay an existing cookie", co->name, co->domain);
920 return FALSE;
921 }
922 }
923 }
925 if(!replace_n && !strcmp(clist->name, co->name)) {
926 /* the names are identical */
928 if(clist->domain && co->domain) {
929 if(curl_strequal(clist->domain, co->domain) &&
930 (clist->tailmatch == co->tailmatch))
931 /* The domains are identical */
932 replace_old = TRUE;
933 }
934 else if(!clist->domain && !co->domain)
935 replace_old = TRUE;
937 if(replace_old) {
938 /* the domains were identical */
940 if(clist->path && co->path &&
941 strcmp(clist->path, co->path))
942 replace_old = FALSE;
943 else if(!clist->path != !co->path)
944 replace_old = FALSE;
945 }
947 if(replace_old && !co->livecookie && clist->livecookie) {
948 /*
949 * Both cookies matched fine, except that the already present cookie
950 * is "live", which means it was set from a header, while the new one
951 * was read from a file and thus is not "live". "live" cookies are
952 * preferred so the new cookie is freed.
953 */
954 return FALSE;
955 }
956 if(replace_old)
957 replace_n = n;
958 }
959 }
960 if(replace_n) {
961 struct Cookie *repl = Curl_node_elem(replace_n);
963 /* when replacing, creationtime is kept from old */
964 co->creationtime = repl->creationtime;
966 /* unlink the old */
967 Curl_node_remove(replace_n);
969 /* free the old cookie */
970 freecookie(repl, TRUE);
971 }
972 *replacep = replace_old;
973 return TRUE;
974}
976/*
977 * Curl_cookie_add
978 *
979 * Add a single cookie line to the cookie keeping object. Be aware that
980 * sometimes we get an IP-only hostname, and that might also be a numerical
981 * IPv6 address.
982 *
983 */
984CURLcode Curl_cookie_add(struct Curl_easy *data,
985 struct CookieInfo *ci,
986 const char *lineptr, /* first character of the line */
987 const char *domain, /* default domain */
988 const char *path, /* full path used when this
989 cookie is set, used to get
990 default path for the cookie
991 unless set */
992 const int flags)
993{
994 struct Cookie comem;
995 struct Cookie *co;
996 size_t myhash;
997 CURLcode result;
998 bool replaces = FALSE;
999 bool okay;
1001 DEBUGASSERT(data);
1002 DEBUGASSERT(MAX_SET_COOKIE_AMOUNT <= 255); /* counter is an unsigned char */
1003 if(data->req.setcookies >= MAX_SET_COOKIE_AMOUNT)
1004 return CURLE_OK; /* silently ignore */
1006 co = &comem;
1007 memset(co, 0, sizeof(comem));
1009 if(flags & COOKIE_HTTPHEADER)
1010 result = parse_cookie_header(data, co, &okay,
1011 lineptr, domain, path, flags & COOKIE_SECURE);
1012 else
1013 result = parse_netscape(co, ci, &okay, lineptr, flags & COOKIE_SECURE);
1015 if(result || !okay)
1016 goto fail;
1018 if(co->prefix_secure && !co->secure)
1019 /* The __Secure- prefix only requires that the cookie be set secure */
1020 goto fail;
1022 if(!(flags & COOKIE_NOPSL) && is_public_suffix(data, co, domain))
1023 goto fail;
1025 if(co->prefix_host) {
1026 /*
1027 * The __Host- prefix requires the cookie to be secure, have a "/" path
1028 * and not have a domain set.
1029 */
1030 if(co->secure && co->path && !strcmp(co->path, "/") && !co->tailmatch)
1031 ;
1032 else
1033 goto fail;
1034 }
1036 if(!ci->running && /* read from a file */
1037 ci->newsession && /* clean session cookies */
1038 !co->expires) /* this is a session cookie */
1039 goto fail;
1041 co->livecookie = ci->running;
1042 co->creationtime = ++ci->lastct;
1044 if(!(flags & COOKIE_NOEXPIRE))
1045 remove_expired(ci);
1047 /*
1048 * Now we have parsed the incoming line, we must now check if this supersedes
1049 * an already existing cookie, which it may if the previous have the same
1050 * domain and path as this.
1051 */
1052 if(!replace_existing(data, co, ci, flags & COOKIE_SECURE, &replaces))
1053 goto fail;
1055 /* clone the stack struct into heap */
1056 co = curlx_memdup(&comem, sizeof(comem));
1057 if(!co) {
1058 co = &comem;
1059 result = CURLE_OUT_OF_MEMORY;
1060 goto fail; /* bail out if we are this low on memory */
1061 }
1063 /* add this cookie to the list */
1064 myhash = cookiehash(co->domain);
1065 Curl_llist_append(&ci->cookielist[myhash], co, &co->node);
1067 if(ci->running)
1068 /* Only show this when NOT reading the cookies from a file */
1069 infof(data, "%s cookie %s=\"%s\" for domain %s, path %s, "
1070 "expire %" FMT_OFF_T,
1071 replaces ? "Replaced" : "Added", co->name, co->value,
1072 co->domain, co->path, co->expires);
1074 if(!replaces)
1075 ci->numcookies++; /* one more cookie in the jar */
1077 /*
1078 * Now that we have added a new cookie to the jar, update the expiration
1079 * tracker in case it is the next one to expire.
1080 */
1081 if(co->expires && (co->expires < ci->next_expiration))
1082 ci->next_expiration = co->expires;
1084 if(flags & COOKIE_HTTPHEADER)
1085 data->req.setcookies++;
1087 return result;
1088fail:
1089 freecookie(co, FALSE);
1090 return result;
1091}
1093/*
1094 * Curl_cookie_init()
1095 *
1096 * Inits a cookie struct to read data from a local file. This is always
1097 * called before any cookies are set. File may be NULL in which case only the
1098 * struct is initialized. Is file is "-" then STDIN is read.
1099 *
1100 * If 'newsession' is TRUE, discard all "session cookies" on read from file.
1101 *
1102 * Note that 'data' might be called as NULL pointer. If data is NULL, 'file'
1103 * will be ignored.
1104 *
1105 * Returns NULL on out of memory.
1106 */
1107struct CookieInfo *Curl_cookie_init(void)
1108{
1109 int i;
1110 struct CookieInfo *ci = curlx_calloc(1, sizeof(struct CookieInfo));
1111 if(!ci)
1112 return NULL;
1114 /* This does not use the destructor callback since we want to add
1115 and remove to lists while keeping the cookie struct intact */
1116 for(i = 0; i < COOKIE_HASH_SIZE; i++)
1117 Curl_llist_init(&ci->cookielist[i], NULL);
1118 /*
1119 * Initialize the next_expiration time to signal that we do not have enough
1120 * information yet.
1121 */
1122 ci->next_expiration = CURL_OFF_T_MAX;
1124 return ci;
1125}
1127/*
1128 * cookie_load()
1129 *
1130 * Reads cookies from a local file. This is always called before any cookies
1131 * are set. If file is "-" then STDIN is read.
1132 *
1133 * If 'flags' has the COOKIE_NOSESSION bit set, discard all "session cookies"
1134 * read from file.
1135 */
1136static CURLcode cookie_load(struct Curl_easy *data, const char *file,
1137 struct CookieInfo *ci, int flags)
1138{
1139 FILE *handle = NULL;
1140 CURLcode result = CURLE_OK;
1141 FILE *fp = NULL;
1142 DEBUGASSERT(ci);
1143 DEBUGASSERT(data);
1144 DEBUGASSERT(file);
1146 ci->newsession = !!(flags & COOKIE_NOSESSION); /* new session? */
1147 ci->running = FALSE; /* this is not running, this is init */
1149 if(file && *file) {
1150 if(!strcmp(file, "-"))
1151 fp = stdin;
1152 else {
1153 fp = curlx_fopen(file, "rb");
1154 if(!fp)
1155 infof(data, "WARNING: failed to open cookie file \"%s\"", file);
1156 else {
1157 curlx_struct_stat stat;
1158 if((curlx_fstat(fileno(fp), &stat) != -1) && S_ISDIR(stat.st_mode)) {
1159 curlx_fclose(fp);
1160 fp = NULL;
1161 infof(data, "WARNING: cookie filename points to a directory: \"%s\"",
1162 file);
1163 }
1164 else
1165 handle = fp;
1166 }
1167 }
1168 }
1170 if(fp) {
1171 struct dynbuf buf;
1172 bool eof = FALSE;
1173 /* get_netscape_format() uses curl_maprintf(), which returns at most
1174 DYN_APRINTF - 1 bytes. cookie_output() adds a newline (CRLF on Windows),
1175 and this dynbuf needs one more byte for its null-terminator. This is a
1176 cap, not an allocation. */
1177 curlx_dyn_init(&buf, DYN_APRINTF + 2);
1178 do {
1179 result = Curl_get_line(&buf, fp, &eof);
1180 if(!result) {
1181 const char *lineptr = curlx_dyn_ptr(&buf);
1182 bool headerline = FALSE;
1183 if(checkprefix("Set-Cookie:", lineptr)) {
1184 /* This is a cookie line, get it! */
1185 lineptr += 11;
1186 headerline = TRUE;
1187 curlx_str_passblanks(&lineptr);
1188 }
1190 result = Curl_cookie_add(data, ci, lineptr, NULL, NULL,
1191 (headerline ? COOKIE_HTTPHEADER : 0) |
1192 COOKIE_NOEXPIRE | COOKIE_SECURE |
1193 (flags & COOKIE_NOPSL));
1194 /* Ignore individual cookie problems unless we ran out of memory */
1195 if(result != CURLE_OUT_OF_MEMORY)
1196 result = CURLE_OK;
1197 }
1198 } while(!result && !eof);
1199 curlx_dyn_free(&buf); /* free the line buffer */
1201 /*
1202 * Remove expired cookies from the hash. We must make sure to run this
1203 * after reading the file, and not on every cookie.
1204 */
1205 remove_expired(ci);
1207 if(handle)
1208 curlx_fclose(handle);
1209 }
1210 data->state.cookie_engine = TRUE;
1211 ci->running = TRUE; /* now, we are running */
1213 return result;
1214}
1216/*
1217 * Load cookies from all given cookie files (CURLOPT_COOKIEFILE).
1218 */
1219CURLcode Curl_cookie_loadfiles(struct Curl_easy *data,
1220 int flags)
1221{
1222 CURLcode result = CURLE_OK;
1223 struct curl_slist *list = data->state.cookielist;
1224 if(list) {
1225 Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
1226 if(!data->cookies)
1227 data->cookies = Curl_cookie_init();
1228 if(!data->cookies)
1229 result = CURLE_OUT_OF_MEMORY;
1230 else {
1231 data->state.cookie_engine = TRUE;
1232 while(list) {
1233 result = cookie_load(data, list->data, data->cookies, flags);
1234 if(result)
1235 break;
1236 list = list->next;
1237 }
1238 }
1239 Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
1240 }
1241 return result;
1242}
1244/*
1245 * cookie_sort
1246 *
1247 * Helper function to sort cookies such that the longest path gets before the
1248 * shorter path. Path, domain and name lengths are considered in that order,
1249 * with the creationtime as the tiebreaker. The creationtime is guaranteed to
1250 * be unique per cookie, so we know we will get an ordering at that point.
1251 */
1252static int cookie_sort(const void *p1, const void *p2)
1253{
1254 const struct Cookie *c1 = *(const struct Cookie * const *)p1;
1255 const struct Cookie *c2 = *(const struct Cookie * const *)p2;
1256 size_t l1, l2;
1258 /* 1 - compare cookie path lengths */
1259 l1 = c1->path ? strlen(c1->path) : 0;
1260 l2 = c2->path ? strlen(c2->path) : 0;
1262 if(l1 != l2)
1263 return (l2 > l1) ? 1 : -1; /* avoid size_t <=> int conversions */
1265 /* 2 - compare cookie domain lengths */
1266 l1 = c1->domain ? strlen(c1->domain) : 0;
1267 l2 = c2->domain ? strlen(c2->domain) : 0;
1269 if(l1 != l2)
1270 return (l2 > l1) ? 1 : -1; /* avoid size_t <=> int conversions */
1272 /* 3 - compare cookie name lengths */
1273 l1 = c1->name ? strlen(c1->name) : 0;
1274 l2 = c2->name ? strlen(c2->name) : 0;
1276 if(l1 != l2)
1277 return (l2 > l1) ? 1 : -1;
1279 /* 4 - compare cookie creation time */
1280 return (c2->creationtime > c1->creationtime) ? 1 : -1;
1281}
1283/*
1284 * cookie_sort_ct
1285 *
1286 * Helper function to sort cookies according to creation time.
1287 */
1288static int cookie_sort_ct(const void *p1, const void *p2)
1289{
1290 const struct Cookie *c1 = *(const struct Cookie * const *)p1;
1291 const struct Cookie *c2 = *(const struct Cookie * const *)p2;
1293 return (c2->creationtime > c1->creationtime) ? 1 : -1;
1294}
1296bool Curl_secure_context(struct Curl_easy *data, const char *host)
1297{
1298 return Curl_xfer_is_secure(data) ||
1299 curl_strequal("localhost", host) ||
1300 !strcmp(host, "127.0.0.1") ||
1301 !strcmp(host, "::1");
1302}
1304/*
1305 * Curl_cookie_getlist
1306 *
1307 * For a given host and path, return a linked list of cookies that the client
1308 * should send to the server if used now.
1309 *
1310 * It shall only return cookies that have not expired.
1311 *
1312 * 'okay' is TRUE when there is a list returned.
1313 */
1314CURLcode Curl_cookie_getlist(struct Curl_easy *data,
1315 bool *okay,
1316 const char *host,
1317 struct Curl_llist *list)
1318{
1319 size_t matches = 0;
1320 const bool is_ip = Curl_host_is_ipnum(host);
1321 const size_t myhash = cookiehash(host);
1322 struct Curl_llist_node *n;
1323 const bool secure = Curl_secure_context(data, host);
1324 struct CookieInfo *ci = data->cookies;
1325 const char *path = data->state.up.path;
1326 CURLcode result = CURLE_OK;
1327 *okay = FALSE;
1329 Curl_llist_init(list, NULL);
1331 if(!ci || !Curl_llist_count(&ci->cookielist[myhash]))
1332 return CURLE_OK; /* no cookie struct or no cookies in the struct */
1334 /* at first, remove expired cookies */
1335 remove_expired(ci);
1337 for(n = Curl_llist_head(&ci->cookielist[myhash]); n; n = Curl_node_next(n)) {
1338 struct Cookie *co = Curl_node_elem(n);
1340 /* if the cookie requires we are secure we must only continue if we are! */
1341 if(co->secure ? secure : TRUE) {
1343 /* now check if the domain is correct */
1344 if(!co->domain ||
1345 (co->tailmatch && !is_ip &&
1346 cookie_tailmatch(co->domain, strlen(co->domain), host)) ||
1347 ((!co->tailmatch || is_ip) && curl_strequal(host, co->domain))) {
1348 /*
1349 * the right part of the host matches the domain stuff in the
1350 * cookie data
1351 */
1353 /*
1354 * now check the left part of the path with the cookies path
1355 * requirement
1356 */
1357 if(!co->path || pathmatch(co->path, path)) {
1359 /*
1360 * This is a match and we add it to the return-linked-list
1361 */
1362 Curl_llist_append(list, co, &co->getnode);
1363 matches++;
1364 if(matches >= MAX_COOKIE_SEND_AMOUNT) {
1365 infof(data, "Included max number of cookies (%zu) in request!",
1366 matches);
1367 break;
1368 }
1369 }
1370 }
1371 }
1372 }
1374 if(matches) {
1375 /*
1376 * Now we need to make sure that if there is a name appearing more than
1377 * once, the longest specified path version comes first. To make this the
1378 * swiftest way, we sort them all based on path length.
1379 */
1380 struct Cookie **array;
1381 size_t i;
1383 /* alloc an array and store all cookie pointers */
1384 array = curlx_malloc(sizeof(struct Cookie *) * matches);
1385 if(!array) {
1386 result = CURLE_OUT_OF_MEMORY;
1387 goto fail;
1388 }
1390 n = Curl_llist_head(list);
1392 for(i = 0; n; n = Curl_node_next(n))
1393 array[i++] = Curl_node_elem(n);
1395 /* now sort the cookie pointers in path length order */
1396 qsort(array, matches, sizeof(struct Cookie *), cookie_sort);
1398 /* remake the linked list order according to the new order */
1399 Curl_llist_destroy(list, NULL);
1401 for(i = 0; i < matches; i++)
1402 Curl_llist_append(list, array[i], &array[i]->getnode);
1404 curlx_free(array); /* remove the temporary data again */
1405 }
1407 *okay = TRUE;
1408 return CURLE_OK; /* success */
1410fail:
1411 /* failure, clear up the allocated chain and return NULL */
1412 Curl_llist_destroy(list, NULL);
1413 return result; /* error */
1414}
1416/*
1417 * Curl_cookie_clearall
1418 *
1419 * Clear all existing cookies and reset the counter.
1420 */
1421void Curl_cookie_clearall(struct CookieInfo *ci)
1422{
1423 if(ci) {
1424 unsigned int i;
1425 for(i = 0; i < COOKIE_HASH_SIZE; i++) {
1426 struct Curl_llist_node *n;
1427 for(n = Curl_llist_head(&ci->cookielist[i]); n;) {
1428 struct Cookie *c = Curl_node_elem(n);
1429 struct Curl_llist_node *e = Curl_node_next(n);
1430 Curl_node_remove(n);
1431 freecookie(c, TRUE);
1432 n = e;
1433 }
1434 }
1435 ci->numcookies = 0;
1436 }
1437}
1439/*
1440 * Curl_cookie_clearsess
1441 *
1442 * Free all session cookies in the cookies list.
1443 */
1444void Curl_cookie_clearsess(struct CookieInfo *ci)
1445{
1446 unsigned int i;
1448 if(!ci)
1449 return;
1451 for(i = 0; i < COOKIE_HASH_SIZE; i++) {
1452 struct Curl_llist_node *n = Curl_llist_head(&ci->cookielist[i]);
1453 struct Curl_llist_node *e = NULL;
1455 for(; n; n = e) {
1456 struct Cookie *curr = Curl_node_elem(n);
1457 e = Curl_node_next(n); /* in case the node is removed, get it early */
1458 if(!curr->expires) {
1459 Curl_node_remove(n);
1460 freecookie(curr, TRUE);
1461 ci->numcookies--;
1462 }
1463 }
1464 }
1465}
1467/*
1468 * Curl_cookie_cleanup()
1469 *
1470 * Free a "cookie object" previous created with Curl_cookie_init().
1471 */
1472void Curl_cookie_cleanup(struct CookieInfo *ci)
1473{
1474 if(ci) {
1475 Curl_cookie_clearall(ci);
1476 curlx_free(ci); /* free the base struct as well */
1477 }
1478}
1480/*
1481 * get_netscape_format()
1482 *
1483 * Formats a string for Netscape output file, w/o a newline at the end.
1484 * Function returns a char * to a formatted line. The caller is responsible
1485 * for freeing the returned pointer.
1486 */
1487static char *get_netscape_format(const struct Cookie *co)
1488{
1489 return curl_maprintf(
1490 "%s" /* httponly preamble */
1491 "%s%s\t" /* domain */
1492 "%s\t" /* tailmatch */
1493 "%s\t" /* path */
1494 "%s\t" /* secure */
1495 "%" FMT_OFF_T "\t" /* expires */
1496 "%s\t" /* name */
1497 "%s", /* value */
1498 co->httponly ? "#HttpOnly_" : "",
1499 /*
1500 * Make sure all domains are prefixed with a dot if they allow
1501 * tailmatching. This is Mozilla-style.
1502 */
1503 (co->tailmatch && co->domain && co->domain[0] != '.') ? "." : "",
1504 co->domain ? co->domain : "unknown",
1505 co->tailmatch ? "TRUE" : "FALSE",
1506 co->path ? co->path : "/",
1507 co->secure ? "TRUE" : "FALSE",
1508 co->expires,
1509 co->name,
1510 co->value ? co->value : "");
1511}
1513/*
1514 * cookie_output()
1515 *
1516 * Writes all internally known cookies to the specified file. Specify
1517 * "-" as filename to write to stdout.
1518 *
1519 * The function returns non-zero on write failure.
1520 */
1521static CURLcode cookie_output(struct Curl_easy *data,
1522 struct CookieInfo *ci,
1523 const char *filename)
1524{
1525 FILE *out = NULL;
1526 bool use_stdout = FALSE;
1527 char *tempstore = NULL;
1528 CURLcode result = CURLE_OK;
1530 if(!ci)
1531 /* no cookie engine alive */
1532 return CURLE_OK;
1534 /* at first, remove expired cookies */
1535 remove_expired(ci);
1537 if(!strcmp("-", filename)) {
1538 /* use stdout */
1539 out = stdout;
1540 use_stdout = TRUE;
1541 }
1542 else {
1543 result = Curl_fopen(data, filename, &out, &tempstore);
1544 if(result)
1545 goto error;
1546 }
1548 fputs("# Netscape HTTP Cookie File\n"
1549 "# https://curl.se/docs/http-cookies.html\n"
1550 "# This file was generated by libcurl! Edit at your own risk.\n\n",
1551 out);
1553 if(ci->numcookies) {
1554 unsigned int i;
1555 size_t nvalid = 0;
1556 struct Cookie **array;
1557 struct Curl_llist_node *n;
1559 array = curlx_calloc(1, sizeof(struct Cookie *) * ci->numcookies);
1560 if(!array) {
1561 result = CURLE_OUT_OF_MEMORY;
1562 goto error;
1563 }
1565 /* only sort the cookies with a domain property */
1566 for(i = 0; i < COOKIE_HASH_SIZE; i++) {
1567 for(n = Curl_llist_head(&ci->cookielist[i]); n; n = Curl_node_next(n)) {
1568 struct Cookie *co = Curl_node_elem(n);
1569 if(!co->domain)
1570 continue;
1571 array[nvalid++] = co;
1572 }
1573 }
1575 qsort(array, nvalid, sizeof(struct Cookie *), cookie_sort_ct);
1577 for(i = 0; i < nvalid; i++) {
1578 char *format_ptr = get_netscape_format(array[i]);
1579 if(!format_ptr) {
1580 curlx_free(array);
1581 result = CURLE_OUT_OF_MEMORY;
1582 goto error;
1583 }
1584 curl_mfprintf(out, "%s\n", format_ptr);
1585 curlx_free(format_ptr);
1586 }
1588 curlx_free(array);
1589 }
1591 if(!use_stdout) {
1592 curlx_fclose(out);
1593 out = NULL;
1594 if(tempstore && curlx_rename(tempstore, filename)) {
1595 result = CURLE_WRITE_ERROR;
1596 goto error;
1597 }
1598 }
1600 /*
1601 * If we reach here we have successfully written a cookie file so there is
1602 * no need to inspect the error, any error case should have jumped into the
1603 * error block below.
1604 */
1605 curlx_free(tempstore);
1606 return CURLE_OK;
1608error:
1609 if(out && !use_stdout)
1610 curlx_fclose(out);
1611 if(tempstore) {
1612 unlink(tempstore);
1613 curlx_free(tempstore);
1614 }
1615 return result;
1616}
1618static struct curl_slist *cookie_list(const struct Curl_easy *data)
1619{
1620 struct curl_slist *list = NULL;
1621 struct curl_slist *beg;
1622 struct curl_slist **tail = &list;
1623 unsigned int i;
1624 struct Curl_llist_node *n;
1626 if(!data->cookies || (data->cookies->numcookies == 0))
1627 return NULL;
1629 /* at first, remove expired cookies */
1630 remove_expired(data->cookies);
1632 for(i = 0; i < COOKIE_HASH_SIZE; i++) {
1633 for(n = Curl_llist_head(&data->cookies->cookielist[i]); n;
1634 n = Curl_node_next(n)) {
1635 struct Cookie *c = Curl_node_elem(n);
1636 char *line;
1637 if(!c->domain)
1638 continue;
1639 line = get_netscape_format(c);
1640 if(!line) {
1641 curl_slist_free_all(list);
1642 return NULL;
1643 }
1644 beg = Curl_slist_append_nodup(NULL, line);
1645 if(!beg) {
1646 curlx_free(line);
1647 curl_slist_free_all(list);
1648 return NULL;
1649 }
1650 *tail = beg;
1651 tail = &beg->next;
1652 }
1653 }
1655 return list;
1656}
1658struct curl_slist *Curl_cookie_list(struct Curl_easy *data)
1659{
1660 struct curl_slist *list;
1661 Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
1662 list = cookie_list(data);
1663 Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
1664 return list;
1665}
1667void Curl_flush_cookies(struct Curl_easy *data, bool cleanup)
1668{
1669 Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
1670 /* only save the cookie file if a transfer was started (cookies->running is
1671 set), as otherwise the cookies were not completely initialized and there
1672 might be cookie files that were not loaded so saving the file is the
1673 wrong thing. */
1674 if(data->cookies) {
1675 const char *cookiejar = CURL_EASY_STR(data, STRING_COOKIEJAR);
1676 if(cookiejar && data->cookies->running) {
1677 /* if we have a destination file for all the cookies to get dumped to */
1678 CURLcode result = cookie_output(data, data->cookies, cookiejar);
1679 if(result)
1680 infof(data, "WARNING: failed to save cookies in %s: %s",
1681 cookiejar, curl_easy_strerror(result));
1682 }
1684 if(cleanup && (!data->share || (data->cookies != data->share->cookies))) {
1685 Curl_cookie_cleanup(data->cookies);
1686 data->cookies = NULL;
1687 }
1688 }
1689 Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
1690}
1692void Curl_cookie_run(struct Curl_easy *data)
1693{
1694 Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
1695 if(data->cookies)
1696 data->cookies->running = TRUE;
1697 Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
1698}
1700#endif /* CURL_DISABLE_HTTP || CURL_DISABLE_COOKIES */
Diagnostics4 git calls · 27 ms
CallStatusSizeTime027 ms
discover200152 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=1 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200149 B · hit1 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=1 git.response.bytes=149 git.response.cache=HIT git.response.duration_ms=1 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=149 git.pkt.payload.max=78 git.ls_refs.count=2 git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=21 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=10 git.pkt.data=8 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=293470 git.pkt.payload.max=65516 git.sideband.band1.bytes=293354 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=87 git.pack.bytes=293354 git.pack.ofs_delta=0 git.pack.ref_delta=1 git.pack.chain.max=1 git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0 git.pack.full=86Open pack map
fetch20016 KiB · hit23 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=16118 git.response.cache=HIT git.response.duration_ms=23 git.fetch.wants=e44884be1ff5cb9392a6d0ef8722e6616ff2d01d git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=16118 git.pkt.payload.max=16097 git.sideband.band1.bytes=16096 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
- Ref
- refs/heads/master
- Commit
- 31c911efe5652725e930f4d2e60c9060e5f6646d
- cf-ray
- a5e1900b18065b65
- Request
- 5ca3e08d…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format