changeset 1501:6fd1c55d8022

cookies use server date to interpret Expires attr
author corvid <corvid@lavabit.com>
date Sat, 09 Jan 2010 20:32:07 +0000
parents 335f1b3692c9
children 0064b6fc4566
files dpi/cookies.c src/cache.c src/cookies.c src/cookies.h
diffstat 4 files changed, 47 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/dpi/cookies.c	Sat Jan 09 20:20:43 2010 +0000
+++ b/dpi/cookies.c	Sat Jan 09 20:32:07 2010 +0000
@@ -632,7 +632,7 @@
  * Parse cookie. A cookie might look something like:
  * "Name=Val; Domain=example.com; Max-Age=3600; HttpOnly"
  */
-static CookieData_t *Cookies_parse(char *cookie_str)
+static CookieData_t *Cookies_parse(char *cookie_str, const char *server_date)
 {
    CookieData_t *cookie = NULL;
    char *str = cookie_str;
@@ -687,6 +687,29 @@
          if (!max_age) {
             value = Cookies_parse_value(&str);
             cookie->expires_at = Cookies_create_timestamp(value);
+            if (cookie->expires_at && server_date) {
+               time_t server_time = Cookies_create_timestamp(server_date);
+
+               if (server_time) {
+                  time_t now = time(NULL);
+                  time_t client_time = cookie->expires_at + now - server_time;
+
+                  if (server_time == cookie->expires_at) {
+                      cookie->expires_at = now;
+                  } else if ((cookie->expires_at > now) ==
+                             (client_time > now)) {
+                     cookie->expires_at = client_time;
+                  } else {
+                     /* It seems not at all unlikely that bad server code will
+                      * fail to take normal clock skew into account when
+                      * setting max/min cookie values.
+                      */
+                     MSG("At %ld, %ld was trying to turn into %ld\n",
+                         (long)now, (long)cookie->expires_at,
+                         (long)client_time);
+                  }
+               }
+            }
             expires = TRUE;
             dFree(value);
             MSG("Expires in %ld seconds, at %s",
@@ -918,7 +941,7 @@
  * Set the value corresponding to the cookie string
  */
 static void Cookies_set(char *cookie_string, char *url_host,
-                        char *url_path)
+                        char *url_path, char *server_date)
 {
    CookieControlAction action;
    CookieData_t *cookie;
@@ -934,7 +957,7 @@
 
    _MSG("%s setting: %s\n", url_host, cookie_string);
 
-   if ((cookie = Cookies_parse(cookie_string))) {
+   if ((cookie = Cookies_parse(cookie_string, server_date))) {
       if (Cookies_validate_domain(cookie, url_host)) {
          Cookies_validate_path(cookie, url_path);
          if (action == COOKIE_ACCEPT_SESSION)
@@ -1211,13 +1234,17 @@
       exit(0);
 
    } else if (cmd && strcmp(cmd, "set_cookie") == 0) {
+      char *date;
+
       dFree(cmd);
       cookie = a_Dpip_get_attr_l(Buf, BufSize, "cookie");
       host = a_Dpip_get_attr_l(Buf, BufSize, "host");
       path = a_Dpip_get_attr_l(Buf, BufSize, "path");
+      date = a_Dpip_get_attr_l(Buf, BufSize, "date");
 
-      Cookies_set(cookie, host, path);
+      Cookies_set(cookie, host, path, date);
 
+      dFree(date);
       dFree(path);
       dFree(host);
       dFree(cookie);
--- a/src/cache.c	Sat Jan 09 20:20:43 2010 +0000
+++ b/src/cache.c	Sat Jan 09 20:32:07 2010 +0000
@@ -733,10 +733,13 @@
     * know if that is a real issue though. */
    if ((Cookies = Cache_parse_multiple_fields(header, "Set-Cookie2")) ||
        (Cookies = Cache_parse_multiple_fields(header, "Set-Cookie"))) {
-      a_Cookies_set(Cookies, entry->Url);
+      char *server_date = Cache_parse_field(header, "Date");
+
+      a_Cookies_set(Cookies, entry->Url, server_date);
       for (i = 0; (data = dList_nth_data(Cookies, i)); ++i)
          dFree(data);
       dList_free(Cookies);
+      dFree(server_date);
    }
 #endif /* !DISABLE_COOKIES */
 
--- a/src/cookies.c	Sat Jan 09 20:20:43 2010 +0000
+++ b/src/cookies.c	Sat Jan 09 20:32:07 2010 +0000
@@ -138,7 +138,8 @@
 /*
  * Set the value corresponding to the cookie string
  */
-void a_Cookies_set(Dlist *cookie_strings, const DilloUrl *set_url)
+void a_Cookies_set(Dlist *cookie_strings, const DilloUrl *set_url,
+                   const char *date)
 {
    CookieControlAction action;
    char *cmd, *cookie_string, *dpip_tag;
@@ -156,9 +157,14 @@
 
    for (i = 0; (cookie_string = dList_nth_data(cookie_strings, i)); ++i) {
       path = URL_PATH_(set_url);
-      cmd = a_Dpip_build_cmd("cmd=%s cookie=%s host=%s path=%s",
-                             "set_cookie", cookie_string, URL_HOST_(set_url),
-                             path ? path : "/");
+      if (date)
+         cmd = a_Dpip_build_cmd("cmd=%s cookie=%s host=%s path=%s date=%s",
+                                "set_cookie", cookie_string,
+                                URL_HOST_(set_url), path ? path : "/", date);
+      else
+         cmd = a_Dpip_build_cmd("cmd=%s cookie=%s host=%s path=%s",
+                                "set_cookie", cookie_string,
+                                URL_HOST_(set_url), path ? path : "/");
 
       _MSG("Cookies.c: a_Cookies_set \n\t \"%s\" \n",cmd );
       /* This call is commented because it doesn't guarantee the order
--- a/src/cookies.h	Sat Jan 09 20:20:43 2010 +0000
+++ b/src/cookies.h	Sat Jan 09 20:32:07 2010 +0000
@@ -12,7 +12,8 @@
 # define a_Cookies_freeall() ;
 #else
   char *a_Cookies_get_query(const DilloUrl *request_url);
-  void  a_Cookies_set(Dlist *cookie_string, const DilloUrl *set_url);
+  void  a_Cookies_set(Dlist *cookie_string, const DilloUrl *set_url,
+                      const char *server_date);
   void  a_Cookies_init( void );
   void  a_Cookies_freeall( void );
 #endif