Bug 1255474 - Cut off path from URLs passed to PAC scripts. r=mcmanus
authorDaniel Stenberg <daniel@haxx.se>
Tue, 20 Sep 2016 21:42:52 -0400
changeset 314557 fd291895b378fba58c48631972e66638c2e8dd21
parent 314556 dcebf2b98e14523c02905b82f03733086487a7fb
child 314558 8a9995559d5e817ce68b9911df3036aad2bb2143
push id81931
push userryanvm@gmail.com
push dateWed, 21 Sep 2016 01:49:11 +0000
treeherdermozilla-inbound@6fb622c938de [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmcmanus
bugs1255474
milestone52.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 1255474 - Cut off path from URLs passed to PAC scripts. r=mcmanus
modules/libpref/init/all.js
netwerk/base/ProxyAutoConfig.cpp
netwerk/base/ProxyAutoConfig.h
netwerk/base/nsPACMan.cpp
netwerk/base/nsPACMan.h
--- a/modules/libpref/init/all.js
+++ b/modules/libpref/init/all.js
@@ -1981,16 +1981,18 @@ pref("network.cookie.cookieBehavior",   
 #endif
 pref("network.cookie.thirdparty.sessionOnly", false);
 pref("network.cookie.lifetimePolicy",       0); // 0-accept, 1-dontUse 2-acceptForSession, 3-acceptForNDays
 pref("network.cookie.prefsMigrated",        false);
 pref("network.cookie.lifetime.days",        90); // Ignored unless network.cookie.lifetimePolicy is 3.
 
 // The PAC file to load.  Ignored unless network.proxy.type is 2.
 pref("network.proxy.autoconfig_url", "");
+// Strip off paths when sending URLs to PAC scripts
+pref("network.proxy.autoconfig_url.include_path", false);
 
 // If we cannot load the PAC file, then try again (doubling from interval_min
 // until we reach interval_max or the PAC file is successfully loaded).
 pref("network.proxy.autoconfig_retry_interval_min", 5);    // 5 seconds
 pref("network.proxy.autoconfig_retry_interval_max", 300);  // 5 minutes
 
 // Use the HSTS preload list by default
 pref("network.stricttransportsecurity.preloadlist", true);
--- a/netwerk/base/ProxyAutoConfig.cpp
+++ b/netwerk/base/ProxyAutoConfig.cpp
@@ -6,16 +6,17 @@
 
 #include "ProxyAutoConfig.h"
 #include "nsICancelable.h"
 #include "nsIDNSListener.h"
 #include "nsIDNSRecord.h"
 #include "nsIDNSService.h"
 #include "nsThreadUtils.h"
 #include "nsIConsoleService.h"
+#include "nsIURLParser.h"
 #include "nsJSUtils.h"
 #include "jsfriendapi.h"
 #include "prnetdb.h"
 #include "nsITimer.h"
 #include "mozilla/net/DNS.h"
 #include "nsServiceManagerUtils.h"
 #include "nsNetCID.h"
 
@@ -383,16 +384,17 @@ bool PACResolve(const nsCString &aHostNa
 
   return GetRunning()->ResolveAddress(aHostName, aNetAddr, aTimeout);
 }
 
 ProxyAutoConfig::ProxyAutoConfig()
   : mJSContext(nullptr)
   , mJSNeedsSetup(false)
   , mShutdown(false)
+  , mIncludePath(false)
 {
   MOZ_COUNT_CTOR(ProxyAutoConfig);
 }
 
 bool
 ProxyAutoConfig::ResolveAddress(const nsCString &aHostName,
                                 NetAddr *aNetAddr,
                                 unsigned int aTimeout)
@@ -727,25 +729,27 @@ const JSClass JSContextWrapper::sGlobalC
 void
 ProxyAutoConfig::SetThreadLocalIndex(uint32_t index)
 {
   sRunningIndex = index;
 }
 
 nsresult
 ProxyAutoConfig::Init(const nsCString &aPACURI,
-                      const nsCString &aPACScript)
+                      const nsCString &aPACScript,
+                      bool aIncludePath)
 {
   mPACURI = aPACURI;
   mPACScript = sPacUtils;
   mPACScript.Append(aPACScript);
 
   if (!GetRunning())
     return SetupJS();
 
+  mIncludePath = aIncludePath;
   mJSNeedsSetup = true;
   return NS_OK;
 }
 
 nsresult
 ProxyAutoConfig::SetupJS()
 {
   mJSNeedsSetup = false;
@@ -833,17 +837,43 @@ ProxyAutoConfig::GetProxyForURI(const ns
   // while the event loop is spinning on a DNS function. Don't early return.
   SetRunning(this);
   mRunningHost = aTestHost;
   mRunningAppId = aAppId;
   mRunningAppOrigin = aAppOrigin;
   mRunningIsInIsolatedMozBrowser = aIsInIsolatedMozBrowser;
 
   nsresult rv = NS_ERROR_FAILURE;
-  JS::RootedString uriString(cx, JS_NewStringCopyZ(cx, aTestURI.get()));
+  nsCString clensedURI = aTestURI;
+
+  if (!mIncludePath) {
+    nsCOMPtr<nsIURLParser> urlParser =
+      do_GetService(NS_STDURLPARSER_CONTRACTID);
+    int32_t pathLen = 0;
+    if (urlParser) {
+      uint32_t schemePos;
+      int32_t schemeLen;
+      uint32_t authorityPos;
+      int32_t authorityLen;
+      uint32_t pathPos;
+      rv = urlParser->ParseURL(aTestURI.get(), aTestURI.Length(),
+                               &schemePos, &schemeLen,
+                               &authorityPos, &authorityLen,
+                               &pathPos, &pathLen);
+    }
+    if (NS_SUCCEEDED(rv)) {
+      if (pathLen) {
+        // cut off the path but leave the initial slash
+        pathLen--;
+      }
+      aTestURI.Left(clensedURI, aTestURI.Length() - pathLen);
+    }
+  }
+
+  JS::RootedString uriString(cx, JS_NewStringCopyZ(cx, clensedURI.get()));
   JS::RootedString hostString(cx, JS_NewStringCopyZ(cx, aTestHost.get()));
 
   if (uriString && hostString) {
     JS::AutoValueArray<2> args(cx);
     args[0].setString(uriString);
     args[1].setString(hostString);
 
     JS::Rooted<JS::Value> rval(cx);
--- a/netwerk/base/ProxyAutoConfig.h
+++ b/netwerk/base/ProxyAutoConfig.h
@@ -25,17 +25,18 @@ union NetAddr;
 // thread and running nested event loops. GetProxyForURI is not re-entrant.
 
 class ProxyAutoConfig  {
 public:
   ProxyAutoConfig();
   ~ProxyAutoConfig();
 
   nsresult Init(const nsCString &aPACURI,
-                const nsCString &aPACScript);
+                const nsCString &aPACScript,
+                bool aIncludePath);
   void     SetThreadLocalIndex(uint32_t index);
   void     Shutdown();
   void     GC();
   bool     MyIPAddress(const JS::CallArgs &aArgs);
   bool     MyAppId(const JS::CallArgs &aArgs);
   bool     MyAppOrigin(const JS::CallArgs &aArgs);
   bool     IsInIsolatedMozBrowser(const JS::CallArgs &aArgs);
   bool     ResolveAddress(const nsCString &aHostName,
@@ -103,16 +104,17 @@ private:
   bool MyIPAddressTryHost(const nsCString &hostName, unsigned int timeout,
                           const JS::CallArgs &aArgs, bool* aResult);
 
   JSContextWrapper *mJSContext;
   bool              mJSNeedsSetup;
   bool              mShutdown;
   nsCString         mPACScript;
   nsCString         mPACURI;
+  bool              mIncludePath;
   nsCString         mRunningHost;
   uint32_t          mRunningAppId;
   nsString          mRunningAppOrigin;
   bool              mRunningIsInIsolatedMozBrowser;
   nsCOMPtr<nsITimer> mTimer;
 };
 
 } // namespace net
--- a/netwerk/base/nsPACMan.cpp
+++ b/netwerk/base/nsPACMan.cpp
@@ -12,16 +12,17 @@
 #include "nsIPromptFactory.h"
 #include "nsIHttpChannel.h"
 #include "nsIPrefService.h"
 #include "nsIPrefBranch.h"
 #include "nsNetUtil.h"
 #include "nsIAsyncVerifyRedirectCallback.h"
 #include "nsISystemProxySettings.h"
 #include "nsContentUtils.h"
+#include "mozilla/Preferences.h"
 
 //-----------------------------------------------------------------------------
 
 namespace mozilla {
 namespace net {
 
 LazyLogModule gProxyLog("proxy");
 
@@ -202,17 +203,18 @@ public:
       mCancel = false;
       return NS_OK;
     }
 
     if (mSetupPAC) {
       mSetupPAC = false;
 
       mPACMan->mPAC.Init(mSetupPACURI,
-                         mSetupPACData);
+                         mSetupPACData,
+                         mPACMan->mIncludePath);
 
       RefPtr<PACLoadComplete> runnable = new PACLoadComplete(mPACMan);
       NS_DispatchToMainThread(runnable);
       return NS_OK;
     }
 
     mPACMan->ProcessPendingQ();
     return NS_OK;
@@ -294,28 +296,32 @@ PendingPACQuery::Run()
   return NS_OK;
 }
 
 //-----------------------------------------------------------------------------
 
 static bool sThreadLocalSetup = false;
 static uint32_t sThreadLocalIndex = 0xdeadbeef; // out of range
 
+static const char *kPACIncludePath =
+  "network.proxy.autoconfig_url.include_path";
+
 nsPACMan::nsPACMan()
   : mLoadPending(false)
   , mShutdown(false)
   , mLoadFailureCount(0)
   , mInProgress(false)
 {
   MOZ_ASSERT(NS_IsMainThread(), "pacman must be created on main thread");
   if (!sThreadLocalSetup){
     sThreadLocalSetup = true;
     PR_NewThreadPrivateIndex(&sThreadLocalIndex, nullptr);
   }
   mPAC.SetThreadLocalIndex(sThreadLocalIndex);
+  mIncludePath = Preferences::GetBool(kPACIncludePath, false);
 }
 
 nsPACMan::~nsPACMan()
 {
   if (mPACThread) {
     if (NS_IsMainThread()) {
       mPACThread->Shutdown();
     }
--- a/netwerk/base/nsPACMan.h
+++ b/netwerk/base/nsPACMan.h
@@ -242,16 +242,17 @@ private:
 
   nsCOMPtr<nsIStreamLoader>    mLoader;
   bool                         mLoadPending;
   Atomic<bool, Relaxed>        mShutdown;
   TimeStamp                    mScheduledReload;
   uint32_t                     mLoadFailureCount;
 
   bool                         mInProgress;
+  bool                         mIncludePath;
 };
 
 extern LazyLogModule gProxyLog;
 
 } // namespace net
 } // namespace mozilla
 
 #endif  // nsPACMan_h__