Bug 1567614: Part 3 - Use ShellExecuteByExplorer in nsMIMEInfoWin; r=jmathies, a=jcristau
authorAaron Klotz <aklotz@mozilla.com>
Tue, 23 Jul 2019 20:18:21 +0000
changeset 537280 cb3f768f7cd8d65be5234efa064b21a45a95c261
parent 537279 51aa10f2e418efada7ba24bc47cf63a8664d6955
child 537281 e635e759e439d595ef59d3aa9befbbe8da0fcfb6
push id2122
push userarchaeopteryx@coole-files.de
push dateWed, 07 Aug 2019 14:22:14 +0000
treeherdermozilla-release@02d9056ba76c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjmathies, jcristau
bugs1567614
milestone68.0.2
Bug 1567614: Part 3 - Use ShellExecuteByExplorer in nsMIMEInfoWin; r=jmathies, a=jcristau Now that we have built up the required primatives in previous patches, this patch simply replaces the previous code with a new version that uses the new APIs from ShellHeaderOnlyUtils.h. Differential Revision: https://phabricator.services.mozilla.com/D38945
uriloader/exthandler/win/nsMIMEInfoWin.cpp
--- a/uriloader/exthandler/win/nsMIMEInfoWin.cpp
+++ b/uriloader/exthandler/win/nsMIMEInfoWin.cpp
@@ -16,16 +16,17 @@
 #include "nsTArray.h"
 #include "shlobj.h"
 #include "windows.h"
 #include "nsIWindowsRegKey.h"
 #include "nsIProcess.h"
 #include "nsUnicharUtils.h"
 #include "nsITextToSubURI.h"
 #include "nsVariant.h"
+#include "mozilla/ShellHeaderOnlyUtils.h"
 #include "mozilla/UniquePtrExtensions.h"
 
 #define RUNDLL32_EXE L"\\rundll32.exe"
 
 NS_IMPL_ISUPPORTS_INHERITED(nsMIMEInfoWin, nsMIMEInfoBase, nsIPropertyBag)
 
 nsMIMEInfoWin::~nsMIMEInfoWin() {}
 
@@ -219,43 +220,35 @@ nsresult nsMIMEInfoWin::LoadUriInternal(
         do_GetService(NS_ITEXTTOSUBURI_CONTRACTID, &rv);
     NS_ENSURE_SUCCESS(rv, rv);
 
     if (NS_FAILED(textToSubURI->UnEscapeNonAsciiURI(NS_LITERAL_CSTRING("UTF-8"),
                                                     urlSpec, utf16Spec))) {
       CopyASCIItoUTF16(urlSpec, utf16Spec);
     }
 
-    static const wchar_t cmdVerb[] = L"open";
-    SHELLEXECUTEINFOW sinfo;
-    memset(&sinfo, 0, sizeof(sinfo));
-    sinfo.cbSize = sizeof(sinfo);
-    sinfo.fMask = SEE_MASK_FLAG_DDEWAIT;
-    sinfo.hwnd = nullptr;
-    sinfo.lpVerb = (LPWSTR)&cmdVerb;
-    sinfo.nShow = SW_SHOWNORMAL;
-
-    LPITEMIDLIST pidl = nullptr;
-    SFGAOF sfgao;
+    // Ask the shell to parse |utf16Spec| to avoid malformed URLs. Failure is
+    // indicative of a potential security issue so we should bail out if so.
+    UniqueAbsolutePidl pidl = ShellParseDisplayName(utf16Spec.get());
+    if (!pidl) {
+      return NS_ERROR_FAILURE;
+    }
 
-    // Bug 394974
-    if (SUCCEEDED(
-            SHParseDisplayName(utf16Spec.get(), nullptr, &pidl, 0, &sfgao))) {
-      sinfo.lpIDList = pidl;
-      sinfo.fMask |= SEE_MASK_INVOKEIDLIST;
-    } else {
-      // SHParseDisplayName failed. Bailing out as work around for
-      // Microsoft Security Bulletin MS07-061
-      rv = NS_ERROR_FAILURE;
+    _variant_t args;
+    _variant_t verb(L"open");
+    _variant_t workingDir;
+    _variant_t showCmd(SW_SHOWNORMAL);
+
+    // Ask Explorer to ShellExecute on our behalf, as some URL handlers do not
+    // start correctly when inheriting our process's process migitations.
+    mozilla::LauncherVoidResult shellExecuteOk =
+        mozilla::ShellExecuteByExplorer(pidl, args, verb, workingDir, showCmd);
+    if (shellExecuteOk.isErr()) {
+      return NS_ERROR_FAILURE;
     }
-    if (NS_SUCCEEDED(rv)) {
-      BOOL result = ShellExecuteExW(&sinfo);
-      if (!result || ((LONG_PTR)sinfo.hInstApp) < 32) rv = NS_ERROR_FAILURE;
-    }
-    if (pidl) CoTaskMemFree(pidl);
   }
 
   return rv;
 }
 
 // Given a path to a local file, return its nsILocalHandlerApp instance.
 bool nsMIMEInfoWin::GetLocalHandlerApp(const nsAString& aCommandHandler,
                                        nsCOMPtr<nsILocalHandlerApp>& aApp) {