Bug 826159 - Turn nsICookie.value into AUTF8String. r=bz
authorMasatoshi Kimura <VYV03354@nifty.ne.jp>
Fri, 04 Jan 2013 12:49:25 +0900
changeset 126662 5029e7c89abd3d716a0a55eb6495bc1934d5a8c3
parent 126661 62fbd50f404788c4d0bce0e9f33701e5013b7945
child 126663 2e003bb70f9c149063990b67a44969c000ef23e3
push id2151
push userlsblakk@mozilla.com
push dateTue, 19 Feb 2013 18:06:57 +0000
treeherdermozilla-beta@4952e88741ec [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbz
bugs826159
milestone20.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 826159 - Turn nsICookie.value into AUTF8String. r=bz
content/html/document/test/test_non-ascii-cookie.html
netwerk/cookie/Makefile.in
netwerk/cookie/nsCookie.cpp
netwerk/cookie/nsICookie.idl
--- a/content/html/document/test/test_non-ascii-cookie.html
+++ b/content/html/document/test/test_non-ascii-cookie.html
@@ -1,33 +1,58 @@
 <!DOCTYPE HTML>
 <html>
 <!--
 https://bugzilla.mozilla.org/show_bug.cgi?id=784367
 -->
 <head>
   <meta charset="utf-8">
-  <title>Test for non-ASCII document.cookie</title>
+  <title>Test for non-ASCII cookie values</title>
   <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
 </head>
 <body>
 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=784367">Mozilla Bug 784367</a>
 <p id="display"></p>
 <div id="content" style="display: none">
   
 </div>
 <pre id="test">
 <script type="application/javascript">
 
-/** Test for non-ASCII document.cookie **/
+/** Test for non-ASCII cookie values **/
+
+var [Cc, Ci] = [SpecialPowers.Cc, SpecialPowers.Ci];
+
+var cm = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager);
+function getCookieFromManager() {
+  var values = [];
+  var host = location.hostname;
+  var path = location.pathname;
+  path = path.substring(0, path.lastIndexOf("/") + 1);
+  var e = cm.enumerator;
+  while (e.hasMoreElements()) {
+    var cookie = e.getNext().QueryInterface(Ci.nsICookie);
+    if (!cookie) {
+      break;
+    }
+    if (host != cookie.host || path != cookie.path) {
+      continue;
+    }
+    values.push(cookie.name + "=" + cookie.value);
+  }
+  return values.join("; ");
+}
+
 var c = document.cookie;
 is(document.cookie, 'abc=012©ABC\ufffdDEF', "document.cookie should be decoded as UTF-8");
+is(getCookieFromManager(), document.cookie, "nsICookieManager should be consistent with document.cookie");
 var newCookie = 'def=∼≩≭≧∯≳≲≣∽≸≸∺≸∠≯≮≥≲≲≯≲∽≡≬≥≲≴∨∱∩∾';
 document.cookie = newCookie;
 is(document.cookie, c + '; ' + newCookie, "document.cookie should be encoded as UTF-8");
+is(getCookieFromManager(), document.cookie, "nsICookieManager should be consistent with document.cookie");
 var date1 = new Date();
 date1.setTime(0);
 document.cookie = newCookie + 'def=;expires=' + date1.toGMTString();
 </script>
 </pre>
 </body>
 </html>
--- a/netwerk/cookie/Makefile.in
+++ b/netwerk/cookie/Makefile.in
@@ -49,16 +49,20 @@ EXPORTS_mozilla/net = \
 		CookieServiceChild.h  \
 		$(NULL)
 
 CPPSRCS += \
 		CookieServiceParent.cpp \
 		CookieServiceChild.cpp \
 		$(NULL)
 
+LOCAL_INCLUDES	= \
+		-I$(topsrcdir)/intl/uconv/src \
+		$(NULL)
+
 # FIXME/bug 575918: out-of-process xpcshell is broken on OS X
 ifneq ($(OS_ARCH),Darwin)
 XPCSHELL_TESTS += test/unit_ipc
 endif
 endif
 
 
 include $(topsrcdir)/config/config.mk
--- a/netwerk/cookie/nsCookie.cpp
+++ b/netwerk/cookie/nsCookie.cpp
@@ -1,14 +1,15 @@
 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "nsCookie.h"
+#include "nsUTF8ConverterService.h"
 #include <stdlib.h>
 
 /******************************************************************************
  * nsCookie:
  * string helper impl
  ******************************************************************************/
 
 // copy aSource strings into contiguous storage provided in aDest1,
@@ -72,30 +73,36 @@ nsCookie::Create(const nsACString &aName
                  const nsACString &aPath,
                  int64_t           aExpiry,
                  int64_t           aLastAccessed,
                  int64_t           aCreationTime,
                  bool              aIsSession,
                  bool              aIsSecure,
                  bool              aIsHttpOnly)
 {
+  // Ensure mValue contains a valid UTF-8 sequence. Otherwise XPConnect will
+  // truncate the string after the first invalid octet.
+  nsUTF8ConverterService converter;
+  nsAutoCString aUTF8Value;
+  converter.ConvertStringToUTF8(aValue, "UTF-8", false, true, 1, aUTF8Value);
+
   // find the required string buffer size, adding 4 for the terminating nulls
-  const uint32_t stringLength = aName.Length() + aValue.Length() +
+  const uint32_t stringLength = aName.Length() + aUTF8Value.Length() +
                                 aHost.Length() + aPath.Length() + 4;
 
   // allocate contiguous space for the nsCookie and its strings -
   // we store the strings in-line with the nsCookie to save allocations
   void *place = ::operator new(sizeof(nsCookie) + stringLength);
   if (!place)
     return nullptr;
 
   // assign string members
   char *name, *value, *host, *path, *end;
   name = static_cast<char *>(place) + sizeof(nsCookie);
-  StrBlockCopy(aName, aValue, aHost, aPath,
+  StrBlockCopy(aName, aUTF8Value, aHost, aPath,
                name, value, host, path, end);
 
   // If the creationTime given to us is higher than the running maximum, update
   // our maximum.
   if (aCreationTime > gLastCreationTime)
     gLastCreationTime = aCreationTime;
 
   // construct the cookie. placement new, oh yeah!
--- a/netwerk/cookie/nsICookie.idl
+++ b/netwerk/cookie/nsICookie.idl
@@ -9,29 +9,29 @@
 /** 
  * An optional interface for accessing the HTTP or
  * javascript cookie object
  */
 
 typedef long nsCookieStatus;
 typedef long nsCookiePolicy;
 
-[scriptable, uuid(E9FCB9A4-D376-458f-B720-E65E7DF593BC)]
+[scriptable, uuid(8684966b-1877-4f0f-8155-be4490b96bf7)]
 
 interface nsICookie : nsISupports {
 
     /**
      * the name of the cookie
      */
     readonly attribute ACString name;
 
     /**
      * the cookie value
      */
-    readonly attribute ACString value;
+    readonly attribute AUTF8String value;
 
     /**
      * true if the cookie is a domain cookie, false otherwise
      */
     readonly attribute boolean isDomain;
 
     /**
      * the host (possibly fully qualified) of the cookie