Bug 526789 - UMR in nsCookieService::CountCookiesFromHostInternal, GetCookiesFromHost, and

GetCookieInternal when given empty host string. r=biesi
This commit is contained in:
Dan Witte 2009-11-18 10:52:33 -08:00
parent 8a9047a307
commit 44267422bf
3 changed files with 124 additions and 3 deletions

View File

@ -0,0 +1,95 @@
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;
function do_check_throws(f, result, stack)
{
if (!stack)
stack = Components.stack.caller;
try {
f();
} catch (exc) {
if (exc.result == result)
return;
do_throw("expected result " + result + ", caught " + exc, stack);
}
do_throw("expected result " + result + ", none thrown", stack);
}
function run_test() {
var cs = Cc["@mozilla.org/cookieService;1"].getService(Ci.nsICookieService);
var cm = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager2);
var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
cm.removeAll();
// test that an empty host results in a no-op
var uri = ios.newURI("http://baz.com/", null, null);
var emptyuri = ios.newURI("http:///", null, null);
var doturi = ios.newURI("http://./", null, null);
do_check_eq(emptyuri.asciiHost, "");
do_check_eq(doturi.asciiHost, ".");
cs.setCookieString(emptyuri, null, "foo2=bar", null);
do_check_eq(getCookieCount(), 0);
cs.setCookieString(doturi, null, "foo3=bar", null);
do_check_eq(getCookieCount(), 0);
cs.setCookieString(uri, null, "foo=bar", null);
do_check_eq(getCookieCount(), 1);
do_check_eq(cs.getCookieString(uri, null), "foo=bar");
do_check_eq(cs.getCookieString(emptyuri, null), null);
do_check_eq(cs.getCookieString(doturi, null), null);
// test that an empty host to add() or remove() throws
var expiry = (Date.now() + 1000) * 1000;
do_check_throws(function() {
cm.add("", "/", "foo2", "bar", false, false, true, expiry);
}, Cr.NS_ERROR_ILLEGAL_VALUE);
do_check_eq(getCookieCount(), 1);
do_check_throws(function() {
cm.add(".", "/", "foo3", "bar", false, false, true, expiry);
}, Cr.NS_ERROR_ILLEGAL_VALUE);
do_check_eq(getCookieCount(), 1);
cm.add("test.com", "/", "foo", "bar", false, false, true, expiry);
do_check_eq(getCookieCount(), 2);
do_check_throws(function() {
cm.remove("", "foo2", "/", false);
}, Cr.NS_ERROR_ILLEGAL_VALUE);
do_check_eq(getCookieCount(), 2);
do_check_throws(function() {
cm.remove(".", "foo3", "/", false);
}, Cr.NS_ERROR_ILLEGAL_VALUE);
do_check_eq(getCookieCount(), 2);
cm.remove("test.com", "foo", "/", false);
do_check_eq(getCookieCount(), 1);
do_check_eq(cm.countCookiesFromHost("baz.com"), 1);
do_check_eq(cm.countCookiesFromHost(""), 0);
do_check_eq(cm.countCookiesFromHost("."), 0);
var e = cm.getCookiesFromHost("baz.com");
do_check_true(e.hasMoreElements());
do_check_eq(e.getNext().QueryInterface(Ci.nsICookie2).name, "foo");
do_check_false(e.hasMoreElements());
e = cm.getCookiesFromHost("");
do_check_false(e.hasMoreElements());
e = cm.getCookiesFromHost(".");
do_check_false(e.hasMoreElements());
cm.removeAll();
}
function getCookieCount() {
var count = 0;
var cm = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager2);
var enumerator = cm.enumerator;
while (enumerator.hasMoreElements()) {
if (!(enumerator.getNext() instanceof Ci.nsICookie2))
throw new Error("not a cookie");
++count;
}
return count;
}

View File

@ -66,6 +66,7 @@ interface nsICookieManager : nsISupports
*
* @param aDomain The host or domain for which the cookie was set
* @param aName The name specified in the cookie
* @param aPath The path for which the cookie was set
* @param aBlocked Indicates if cookies from this host should be permanently blocked
*
*/

View File

@ -57,6 +57,7 @@
#include "nsCOMArray.h"
#include "nsArrayEnumerator.h"
#include "nsEnumeratorUtils.h"
#include "nsAutoPtr.h"
#include "nsReadableUtils.h"
#include "nsCRT.h"
@ -920,6 +921,9 @@ nsCookieService::Add(const nsACString &aDomain,
PRBool aIsSession,
PRInt64 aExpiry)
{
NS_ENSURE_TRUE(!aDomain.IsEmpty() && !aDomain.EqualsLiteral("."),
NS_ERROR_INVALID_ARG);
PRInt64 currentTimeInUsec = PR_Now();
nsRefPtr<nsCookie> cookie =
@ -944,6 +948,9 @@ nsCookieService::Remove(const nsACString &aHost,
const nsACString &aPath,
PRBool aBlocked)
{
NS_ENSURE_TRUE(!aHost.IsEmpty() && !aHost.EqualsLiteral("."),
NS_ERROR_INVALID_ARG);
nsListIter matchIter;
if (FindCookie(PromiseFlatCString(aHost),
PromiseFlatCString(aName),
@ -960,7 +967,7 @@ nsCookieService::Remove(const nsACString &aHost,
nsCAutoString host(NS_LITERAL_CSTRING("http://"));
// strip off the domain dot, if necessary
if (!aHost.IsEmpty() && aHost.First() == '.')
if (aHost.First() == '.')
host.Append(Substring(aHost, 1, aHost.Length() - 1));
else
host.Append(aHost);
@ -1247,6 +1254,10 @@ nsCookieService::GetCookieInternal(nsIURI *aHostURI,
}
// trim trailing dots
hostFromURI.Trim(".");
if (hostFromURI.IsEmpty()) {
COOKIE_LOGFAILURE(GET_COOKIE, aHostURI, nsnull, "empty host");
return;
}
// insert a leading dot, so we begin the hash lookup with the
// equivalent domain cookie host
hostFromURI.Insert(NS_LITERAL_CSTRING("."), 0);
@ -1835,6 +1846,8 @@ nsCookieService::IsForeign(nsIURI *aHostURI,
// trim trailing dots
currentHost.Trim(".");
firstHost.Trim(".");
if (currentHost.IsEmpty() || firstHost.IsEmpty())
return PR_TRUE;
// fast path: check if the two hosts are identical.
// this also covers two special cases:
@ -1941,6 +1954,8 @@ nsCookieService::CheckDomain(nsCookieAttributes &aCookieAttributes,
}
// trim trailing dots
hostFromURI.Trim(".");
if (hostFromURI.IsEmpty())
return PR_FALSE;
// if a domain is given, check the host has permission
if (!aCookieAttributes.host.IsEmpty()) {
@ -2169,8 +2184,9 @@ PRUint32
nsCookieService::CountCookiesFromHostInternal(const nsACString &aHost,
nsEnumerationData &aData)
{
PRUint32 countFromHost = 0;
NS_ASSERTION(!aHost.IsEmpty() && !aHost.EqualsLiteral("."), "empty host");
PRUint32 countFromHost = 0;
nsCAutoString hostWithDot(NS_LITERAL_CSTRING(".") + aHost);
const char *currentDot = hostWithDot.get();
@ -2205,9 +2221,13 @@ NS_IMETHODIMP
nsCookieService::CountCookiesFromHost(const nsACString &aHost,
PRUint32 *aCountFromHost)
{
if (aHost.IsEmpty() || aHost.EqualsLiteral(".")) {
*aCountFromHost = 0;
return NS_OK;
}
// we don't care about finding the oldest cookie here, so disable the search
nsEnumerationData data(PR_Now() / PR_USEC_PER_SEC, LL_MININT);
*aCountFromHost = CountCookiesFromHostInternal(aHost, data);
return NS_OK;
}
@ -2218,6 +2238,9 @@ NS_IMETHODIMP
nsCookieService::GetCookiesFromHost(const nsACString &aHost,
nsISimpleEnumerator **aEnumerator)
{
if (aHost.IsEmpty() || aHost.EqualsLiteral("."))
return NS_NewEmptyEnumerator(aEnumerator);
nsCOMArray<nsICookie> cookieList(mMaxCookiesPerHost);
nsCAutoString hostWithDot(NS_LITERAL_CSTRING(".") + aHost);
PRInt64 currentTime = PR_Now() / PR_USEC_PER_SEC;
@ -2249,6 +2272,8 @@ nsCookieService::FindCookie(const nsAFlatCString &aHost,
nsListIter &aIter,
PRInt64 aCurrentTime)
{
NS_ASSERTION(!aHost.IsEmpty() && !aHost.EqualsLiteral("."), "empty host");
nsCookieEntry *entry = mDBState->hostTable.GetEntry(aHost.get());
for (aIter = nsListIter(entry); aIter.current; ++aIter) {
if (aIter.current->Expiry() > aCurrentTime &&