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