c260e0
From 5f543b36b2b05cbe52a9861ad7cb15e0a7c78c80 Mon Sep 17 00:00:00 2001
c260e0
From: Daniel Stenberg <daniel@haxx.se>
c260e0
Date: Tue, 21 May 2013 23:28:59 +0200
c260e0
Subject: [PATCH] Curl_cookie_add: handle IPv6 hosts
c260e0
c260e0
1 - don't skip host names with a colon in them in an attempt to bail out
c260e0
on HTTP headers in the cookie file parser. It was only a shortcut anyway
c260e0
and trying to parse a file with HTTP headers will still be handled, only
c260e0
slightly slower.
c260e0
c260e0
2 - don't skip domain names based on number of dots. The original
c260e0
netscape cookie spec had this oddity mentioned and while our code
c260e0
decreased the check to only check for two, the existing cookie spec has
c260e0
no such dot counting required.
c260e0
c260e0
Bug: http://curl.haxx.se/bug/view.cgi?id=1221
c260e0
Reported-by: Stefan Neis
c260e0
c260e0
Upstream-commit: 85b9dc80232d1d7d48ee4dea6db5a2263ee68efd
c260e0
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
c260e0
---
c260e0
 lib/cookie.c | 93 +++++++++++++++++-------------------------------------------
c260e0
 1 file changed, 26 insertions(+), 67 deletions(-)
c260e0
c260e0
diff --git a/lib/cookie.c b/lib/cookie.c
c260e0
index 764bbc9..956efd4 100644
c260e0
--- a/lib/cookie.c
c260e0
+++ b/lib/cookie.c
c260e0
@@ -347,6 +347,9 @@ static bool isip(const char *domain)
c260e0
  *
c260e0
  * Add a single cookie line to the cookie keeping object.
c260e0
  *
c260e0
+ * Be aware that sometimes we get an IP-only host name, and that might also be
c260e0
+ * a numerical IPv6 address.
c260e0
+ *
c260e0
  ***************************************************************************/
c260e0
 
c260e0
 struct Cookie *
c260e0
@@ -458,73 +461,35 @@ Curl_cookie_add(struct SessionHandle *data,
c260e0
           }
c260e0
         }
c260e0
         else if(Curl_raw_equal("domain", name)) {
c260e0
-          /* note that this name may or may not have a preceding dot, but
c260e0
-             we don't care about that, we treat the names the same anyway */
c260e0
-
c260e0
-          const char *domptr=whatptr;
c260e0
-          const char *nextptr;
c260e0
-          int dotcount=1;
c260e0
+          bool is_ip;
c260e0
 
c260e0
-          /* Count the dots, we need to make sure that there are enough
c260e0
-             of them. */
c260e0
+          /* Now, we make sure that our host is within the given domain,
c260e0
+             or the given domain is not valid and thus cannot be set. */
c260e0
 
c260e0
           if('.' == whatptr[0])
c260e0
-            /* don't count the initial dot, assume it */
c260e0
-            domptr++;
c260e0
-
c260e0
-          do {
c260e0
-            nextptr = strchr(domptr, '.');
c260e0
-            if(nextptr) {
c260e0
-              if(domptr != nextptr)
c260e0
-                dotcount++;
c260e0
-              domptr = nextptr+1;
c260e0
+            whatptr++; /* ignore preceding dot */
c260e0
+
c260e0
+          is_ip = isip(domain ? domain : whatptr);
c260e0
+
c260e0
+          if(!domain
c260e0
+             || (is_ip && !strcmp(whatptr, domain))
c260e0
+             || (!is_ip && tailmatch(whatptr, domain))) {
c260e0
+            strstore(&co->domain, whatptr);
c260e0
+            if(!co->domain) {
c260e0
+              badcookie = TRUE;
c260e0
+              break;
c260e0
             }
c260e0
-          } while(nextptr);
c260e0
-
c260e0
-          /* The original Netscape cookie spec defined that this domain name
c260e0
-             MUST have three dots (or two if one of the seven holy TLDs),
c260e0
-             but it seems that these kinds of cookies are in use "out there"
c260e0
-             so we cannot be that strict. I've therefore lowered the check
c260e0
-             to not allow less than two dots. */
c260e0
-
c260e0
-          if(dotcount < 2) {
c260e0
-            /* Received and skipped a cookie with a domain using too few
c260e0
-               dots. */
c260e0
-            badcookie=TRUE; /* mark this as a bad cookie */
c260e0
-            infof(data, "skipped cookie with illegal dotcount domain: %s\n",
c260e0
-                  whatptr);
c260e0
+            if(!is_ip)
c260e0
+              co->tailmatch=TRUE; /* we always do that if the domain name was
c260e0
+                                     given */
c260e0
           }
c260e0
           else {
c260e0
-            bool is_ip;
c260e0
-
c260e0
-            /* Now, we make sure that our host is within the given domain,
c260e0
-               or the given domain is not valid and thus cannot be set. */
c260e0
-
c260e0
-            if('.' == whatptr[0])
c260e0
-              whatptr++; /* ignore preceding dot */
c260e0
-
c260e0
-            is_ip = isip(domain ? domain : whatptr);
c260e0
-
c260e0
-            if(!domain
c260e0
-               || (is_ip && !strcmp(whatptr, domain))
c260e0
-               || (!is_ip && tailmatch(whatptr, domain))) {
c260e0
-              strstore(&co->domain, whatptr);
c260e0
-              if(!co->domain) {
c260e0
-                badcookie = TRUE;
c260e0
-                break;
c260e0
-              }
c260e0
-              if(!is_ip)
c260e0
-                co->tailmatch=TRUE; /* we always do that if the domain name was
c260e0
-                                       given */
c260e0
-            }
c260e0
-            else {
c260e0
-              /* we did not get a tailmatch and then the attempted set domain
c260e0
-                 is not a domain to which the current host belongs. Mark as
c260e0
-                 bad. */
c260e0
-              badcookie=TRUE;
c260e0
-              infof(data, "skipped cookie with bad tailmatch domain: %s\n",
c260e0
-                    whatptr);
c260e0
-            }
c260e0
+            /* we did not get a tailmatch and then the attempted set domain
c260e0
+               is not a domain to which the current host belongs. Mark as
c260e0
+               bad. */
c260e0
+            badcookie=TRUE;
c260e0
+            infof(data, "skipped cookie with bad tailmatch domain: %s\n",
c260e0
+                  whatptr);
c260e0
           }
c260e0
         }
c260e0
         else if(Curl_raw_equal("version", name)) {
c260e0
@@ -696,12 +661,6 @@ Curl_cookie_add(struct SessionHandle *data,
c260e0
 
c260e0
     firstptr=strtok_r(lineptr, "\t", &tok_buf); /* tokenize it on the TAB */
c260e0
 
c260e0
-    /* Here's a quick check to eliminate normal HTTP-headers from this */
c260e0
-    if(!firstptr || strchr(firstptr, ':')) {
c260e0
-      free(co);
c260e0
-      return NULL;
c260e0
-    }
c260e0
-
c260e0
     /* Now loop through the fields and init the struct we already have
c260e0
        allocated */
c260e0
     for(ptr=firstptr, fields=0; ptr && !badcookie;
c260e0
-- 
c260e0
2.5.5
c260e0