Bug 557047 - Replace mailnews specific ifdef (MOZ_MAIL_NEWS) in cookie code with tests for a protocol flag. r=sdwilsh,sr=bzbarsky
authorMark Banner <bugzilla@standard8.plus.com>
Mon, 05 Dec 2011 12:35:23 +0000
changeset 81990 cb70391c86d9e69a76971518496dbc22214c7c5d
parent 81989 1bd7482ad4d195aaa4511f241b3de74eac9e8f2c
child 82058 fafaf614791f698e9f66902560206e37065eee39
push idunknown
push userunknown
push dateunknown
reviewerssdwilsh, bzbarsky
bugs557047
milestone11.0a1
Bug 557047 - Replace mailnews specific ifdef (MOZ_MAIL_NEWS) in cookie code with tests for a protocol flag. r=sdwilsh,sr=bzbarsky
config/autoconf.mk.in
extensions/cookie/Makefile.in
extensions/cookie/nsCookiePermission.cpp
netwerk/base/public/nsIProtocolHandler.idl
netwerk/test/TestCookie.cpp
--- a/config/autoconf.mk.in
+++ b/config/autoconf.mk.in
@@ -124,17 +124,16 @@ DEHYDRA_PATH    = @DEHYDRA_PATH@
 NS_TRACE_MALLOC = @NS_TRACE_MALLOC@
 USE_ELF_DYNSTR_GC = @USE_ELF_DYNSTR_GC@
 USE_ELF_HACK = @USE_ELF_HACK@
 STDCXX_COMPAT = @STDCXX_COMPAT@
 MOZ_LIBSTDCXX_TARGET_VERSION=@MOZ_LIBSTDCXX_TARGET_VERSION@
 MOZ_LIBSTDCXX_HOST_VERSION=@MOZ_LIBSTDCXX_HOST_VERSION@
 INCREMENTAL_LINKER = @INCREMENTAL_LINKER@
 MACOSX_DEPLOYMENT_TARGET = @MACOSX_DEPLOYMENT_TARGET@
-MOZ_MAIL_NEWS	= @MOZ_MAIL_NEWS@
 ENABLE_TESTS	= @ENABLE_TESTS@
 IBMBIDI = @IBMBIDI@
 MOZ_UNIVERSALCHARDET = @MOZ_UNIVERSALCHARDET@
 ACCESSIBILITY = @ACCESSIBILITY@
 MOZ_BRANDING_DIRECTORY = @MOZ_BRANDING_DIRECTORY@
 XPCOM_USE_LEA = @XPCOM_USE_LEA@
 MOZ_INSTALLER	= @MOZ_INSTALLER@
 MOZ_UPDATER	= @MOZ_UPDATER@
--- a/extensions/cookie/Makefile.in
+++ b/extensions/cookie/Makefile.in
@@ -60,19 +60,15 @@ CPPSRCS		= \
 		nsCookiePermission.cpp \
 		$(NULL)
 
 XPIDLSRCS	= \
 		nsICookieAcceptDialog.idl \
 		nsICookiePromptService.idl \
 		$(NULL)
 
-ifdef MOZ_MAIL_NEWS
-DEFINES += -DMOZ_MAIL_NEWS
-endif
-
 ifdef ENABLE_TESTS
 TOOL_DIRS		+= test
 endif
 
 include $(topsrcdir)/config/config.mk
 include $(topsrcdir)/ipc/chromium/chromium-config.mk
 include $(topsrcdir)/config/rules.mk
--- a/extensions/cookie/nsCookiePermission.cpp
+++ b/extensions/cookie/nsCookiePermission.cpp
@@ -85,33 +85,16 @@ static const char kCookiesAlwaysAcceptSe
 static const char kCookiesPrefsMigrated[] = "network.cookie.prefsMigrated";
 // obsolete pref names for migration
 static const char kCookiesLifetimeEnabled[] = "network.cookie.lifetime.enabled";
 static const char kCookiesLifetimeBehavior[] = "network.cookie.lifetime.behavior";
 static const char kCookiesAskPermission[] = "network.cookie.warnAboutCookies";
 
 static const char kPermissionType[] = "cookie";
 
-#ifdef MOZ_MAIL_NEWS
-// returns true if URI appears to be the URI of a mailnews protocol
-// XXXbz this should be a protocol flag, not a scheme list, dammit!
-static bool
-IsFromMailNews(nsIURI *aURI)
-{
-  static const char *kMailNewsProtocols[] =
-      { "imap", "news", "snews", "mailbox", nsnull };
-  bool result;
-  for (const char **p = kMailNewsProtocols; *p; ++p) {
-    if (NS_SUCCEEDED(aURI->SchemeIs(*p, &result)) && result)
-      return true;
-  }
-  return false;
-}
-#endif
-
 NS_IMPL_ISUPPORTS2(nsCookiePermission,
                    nsICookiePermission,
                    nsIObserver)
 
 bool
 nsCookiePermission::Init()
 {
   // Initialize nsIPermissionManager and fetch relevant prefs. This is only
@@ -201,31 +184,32 @@ nsCookiePermission::SetAccess(nsIURI    
                        nsIPermissionManager::EXPIRE_NEVER, 0);
 }
 
 NS_IMETHODIMP
 nsCookiePermission::CanAccess(nsIURI         *aURI,
                               nsIChannel     *aChannel,
                               nsCookieAccess *aResult)
 {
-#ifdef MOZ_MAIL_NEWS
-  // If this URI is a mailnews one (e.g. imap etc), don't allow cookies for
-  // it.
-  if (IsFromMailNews(aURI)) {
+  // Check this protocol doesn't allow cookies
+  bool hasFlags;
+  nsresult rv =
+    NS_URIChainHasFlags(aURI, nsIProtocolHandler::URI_FORBIDS_COOKIE_ACCESS,
+                        &hasFlags);
+  if (NS_FAILED(rv) || hasFlags) {
     *aResult = ACCESS_DENY;
     return NS_OK;
   }
-#endif // MOZ_MAIL_NEWS
 
   // Lazily initialize ourselves
   if (!EnsureInitialized())
     return NS_ERROR_UNEXPECTED;
 
   // finally, check with permission manager...
-  nsresult rv = mPermMgr->TestPermission(aURI, kPermissionType, (PRUint32 *) aResult);
+  rv = mPermMgr->TestPermission(aURI, kPermissionType, (PRUint32 *) aResult);
   if (NS_SUCCEEDED(rv)) {
     switch (*aResult) {
     // if we have one of the publicly-available values, just return it
     case nsIPermissionManager::UNKNOWN_ACTION: // ACCESS_DEFAULT
     case nsIPermissionManager::ALLOW_ACTION:   // ACCESS_ALLOW
     case nsIPermissionManager::DENY_ACTION:    // ACCESS_DENY
       break;
 
--- a/netwerk/base/public/nsIProtocolHandler.idl
+++ b/netwerk/base/public/nsIProtocolHandler.idl
@@ -256,16 +256,22 @@ interface nsIProtocolHandler : nsISuppor
 
     /**
      * This protocol handler can be proxied using a http proxy (e.g., http,
      * ftp, etc.).  nsIIOService::newChannelFromURI will feed URIs from this
      * protocol handler to the HTTP protocol handler instead.  This flag is
      * ignored if ALLOWS_PROXY is not set.
      */
     const unsigned long ALLOWS_PROXY_HTTP = (1<<3);
+
+    /**
+     * This protocol handler forbids accessing cookies e.g. for mail related
+     * protocols.
+     */
+    const unsigned long URI_FORBIDS_COOKIE_ACCESS = (1<<15);
 };
 
 %{C++
 /**
  * Protocol handlers are registered with XPCOM under the following CONTRACTID prefix:
  */
 #define NS_NETWORK_PROTOCOL_CONTRACTID_PREFIX "@mozilla.org/network/protocol;1?name="
 /**
--- a/netwerk/test/TestCookie.cpp
+++ b/netwerk/test/TestCookie.cpp
@@ -571,39 +571,16 @@ main(PRInt32 argc, char *argv[])
       rv[7] = CheckResult(cookie.get(), MUST_EQUAL, "eight");
       SetACookie(cookieService, "http://parser.test/", nsnull, "test=six", nsnull);
       GetACookie(cookieService, "http://parser.test/", nsnull, getter_Copies(cookie));
       rv[9] = CheckResult(cookie.get(), MUST_CONTAIN, "test=six");
 
       allTestsPassed = PrintResult(rv, 10) && allTestsPassed;
 
 
-      // *** mailnews tests
-      sBuffer = PR_sprintf_append(sBuffer, "*** Beginning mailnews tests...\n");
-
-      // test some mailnews cookies to ensure blockage.
-      // we use null firstURI's deliberately, since we have hacks to deal with
-      // this situation...
-      SetACookie(cookieService, "mailbox://mail.co.uk/", nsnull, "test=mailnews", nsnull);
-      GetACookie(cookieService, "mailbox://mail.co.uk/", nsnull, getter_Copies(cookie));
-      rv[0] = CheckResult(cookie.get(), MUST_BE_NULL);
-      GetACookie(cookieService, "http://mail.co.uk/", nsnull, getter_Copies(cookie));
-      rv[1] = CheckResult(cookie.get(), MUST_BE_NULL);
-      SetACookie(cookieService, "http://mail.co.uk/", nsnull, "test=mailnews", nsnull);
-      GetACookie(cookieService, "mailbox://mail.co.uk/", nsnull, getter_Copies(cookie));
-      rv[2] = CheckResult(cookie.get(), MUST_BE_NULL);
-      GetACookie(cookieService, "http://mail.co.uk/", nsnull, getter_Copies(cookie));
-      rv[3] = CheckResult(cookie.get(), MUST_EQUAL, "test=mailnews");
-      SetACookie(cookieService, "http://mail.co.uk/", nsnull, "test=mailnews; max-age=0", nsnull);
-      GetACookie(cookieService, "http://mail.co.uk/", nsnull, getter_Copies(cookie));
-      rv[4] = CheckResult(cookie.get(), MUST_BE_NULL);
-
-      allTestsPassed = PrintResult(rv, 5) && allTestsPassed;
-
-
       // *** path ordering tests
       sBuffer = PR_sprintf_append(sBuffer, "*** Beginning path ordering tests...\n");
 
       // test that cookies are returned in path order - longest to shortest.
       // if the header doesn't specify a path, it's taken from the host URI.
       SetACookie(cookieService, "http://multi.path.tests/", nsnull, "test1=path; path=/one/two/three", nsnull);
       SetACookie(cookieService, "http://multi.path.tests/", nsnull, "test2=path; path=/one \n test3=path; path=/one/two/three/four \n test4=path; path=/one/two \n test5=path; path=/one/two/", nsnull);
       SetACookie(cookieService, "http://multi.path.tests/one/two/three/four/five/", nsnull, "test6=path", nsnull);