Bug 1420124 - X Remote client: Encode DBus interface strings by base64 to avoid failure/crashes, r?jhorak draft
authorMartin Stransky <stransky@redhat.com>
Thu, 23 Nov 2017 12:28:07 +0100
changeset 705011 85006d6ea9dea0da30f924ddf5d933ddba22ed16
parent 703160 3b5a3afbe5e08fc9229a5c227f36613421eb10a6
child 742222 ad8e9e568e34d3cada5f564d357cc10dcda57dc0
push id91319
push userstransky@redhat.com
push dateWed, 29 Nov 2017 09:21:18 +0000
reviewersjhorak
bugs1420124, 1418985
milestone59.0a1
Bug 1420124 - X Remote client: Encode DBus interface strings by base64 to avoid failure/crashes, r?jhorak DBus strings can contain only [a-z][A-Z][0-9]_ chars. Encode profile name (used as part of interface name) by base64 and adjust it to avoid crashes (Bug 1418985). MozReview-Commit-ID: J2SQHYRcWDW
widget/xremoteclient/DBusRemoteClient.cpp
--- a/widget/xremoteclient/DBusRemoteClient.cpp
+++ b/widget/xremoteclient/DBusRemoteClient.cpp
@@ -4,16 +4,17 @@
 /* vim:set ts=8 sw=2 et cindent: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "DBusRemoteClient.h"
 #include "RemoteUtils.h"
 #include "mozilla/Logging.h"
+#include "mozilla/Base64.h"
 #include "nsPrintfCString.h"
 
 using mozilla::LogLevel;
 static mozilla::LazyLogModule sRemoteLm("DBusRemoteClient");
 
 DBusRemoteClient::DBusRemoteClient()
 {
   mConnection = nullptr;
@@ -76,30 +77,39 @@ DBusRemoteClient::SendCommandLine (const
                                        static_cast<uint32_t>(rv)));
   return rv;
 }
 
 nsresult
 DBusRemoteClient::DoSendDBusCommandLine(const char *aProgram, const char *aProfile,
                                         const char* aBuffer, int aLength)
 {
-  NS_ASSERTION(aProfile && aProfile[0] != '\0', "Missing user profile!");
+  if(!aProfile || aProfile[0] == '\0') {
+    return NS_ERROR_INVALID_ARG;
+  }
+
+  // D-Bus names can contain only [a-z][A-Z][0-9]_
+  // characters so adjust the profile string properly.
+  nsAutoCString profileName;
+  nsresult rv = mozilla::Base64Encode(nsAutoCString(aProfileName), profileName);
+  NS_ENSURE_SUCCESS(rv, rv);
+  profileName.ReplaceChar("+/=", '_');
 
   nsAutoCString destinationName;
-  destinationName = nsPrintfCString("org.mozilla.%s.%s", aProgram, aProfile);
+  destinationName = nsPrintfCString("org.mozilla.%s.%s", aProgram, profileName.get());
 
-  nsAutoCString objectName;
-  objectName = nsPrintfCString("/org/mozilla/%s/Remote", aProgram);
+  nsAutoCString pathName;
+  pathName = nsPrintfCString("/org/mozilla/%s/Remote", aProgram);
 
   nsAutoCString remoteInterfaceName;
   remoteInterfaceName = nsPrintfCString("org.mozilla.%s", aProgram);
 
   RefPtr<DBusMessage> msg = already_AddRefed<DBusMessage>(
       dbus_message_new_method_call(destinationName.get(),
-                                   objectName.get(), // object to call on
+                                   pathName.get(), // object to call on
                                    remoteInterfaceName.get(), // interface to call on
                                    "OpenURL")); // method name
   if (!msg) {
     return NS_ERROR_FAILURE;
   }
 
   // append arguments
   if (!dbus_message_append_args(msg, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,