Bug 1300720 - Part 1: Prevent file:, chrome: and resource: URIs from using an external protocol handler. r=honza draft
authorCameron McCormack <cam@mcc.id.au>
Mon, 03 Oct 2016 12:09:47 +0800
changeset 419988 984f3282608d2ef105f2b077715384e96ccf0188
parent 419914 7c576fe3279d87543f0a03b844eba7bc215e17f1
child 419989 a51323abb5c2a330bede0a7bb8eefe0dc323b22a
push id31063
push userbmo:cam@mcc.id.au
push dateMon, 03 Oct 2016 04:10:31 +0000
reviewershonza
bugs1300720
milestone52.0a1
Bug 1300720 - Part 1: Prevent file:, chrome: and resource: URIs from using an external protocol handler. r=honza MozReview-Commit-ID: Fgu2UDGOFHG
netwerk/base/nsIOService.cpp
--- a/netwerk/base/nsIOService.cpp
+++ b/netwerk/base/nsIOService.cpp
@@ -497,43 +497,49 @@ nsIOService::GetCachedProtocolHandler(co
                 : (!nsCRT::strcasecmp(scheme, gScheme[i])))
         {
             return CallQueryReferent(mWeakHandler[i].get(), result);
         }
     }
     return NS_ERROR_FAILURE;
 }
  
+static bool
+UsesExternalProtocolHandler(const char* aScheme)
+{
+    if (NS_LITERAL_CSTRING("file").Equals(aScheme) ||
+        NS_LITERAL_CSTRING("chrome").Equals(aScheme) ||
+        NS_LITERAL_CSTRING("resource").Equals(aScheme)) {
+        // Don't allow file:, chrome: or resource: URIs to be handled with
+        // nsExternalProtocolHandler, since internally we rely on being able to
+        // use and read from these URIs.
+        return false;
+    }
+
+    nsAutoCString pref("network.protocol-handler.external.");
+    pref += aScheme;
+
+    return Preferences::GetBool(pref.get(), false);
+}
+
 NS_IMETHODIMP
 nsIOService::GetProtocolHandler(const char* scheme, nsIProtocolHandler* *result)
 {
     nsresult rv;
 
     NS_ENSURE_ARG_POINTER(scheme);
     // XXX we may want to speed this up by introducing our own protocol 
     // scheme -> protocol handler mapping, avoiding the string manipulation
     // and service manager stuff
 
     rv = GetCachedProtocolHandler(scheme, result);
     if (NS_SUCCEEDED(rv))
         return rv;
 
-    bool externalProtocol = false;
-    nsCOMPtr<nsIPrefBranch> prefBranch;
-    GetPrefBranch(getter_AddRefs(prefBranch));
-    if (prefBranch) {
-        nsAutoCString externalProtocolPref("network.protocol-handler.external.");
-        externalProtocolPref += scheme;
-        rv = prefBranch->GetBoolPref(externalProtocolPref.get(), &externalProtocol);
-        if (NS_FAILED(rv)) {
-            externalProtocol = false;
-        }
-    }
-
-    if (!externalProtocol) {
+    if (!UsesExternalProtocolHandler(scheme)) {
         nsAutoCString contractID(NS_NETWORK_PROTOCOL_CONTRACTID_PREFIX);
         contractID += scheme;
         ToLowerCase(contractID);
 
         rv = CallGetService(contractID.get(), result);
         if (NS_SUCCEEDED(rv)) {
             CacheProtocolHandler(scheme, *result);
             return rv;