Commit d91ccf69 authored by Emilio Cobos Álvarez's avatar Emilio Cobos Álvarez
Browse files

Bug 1762356 - Introduce XREAppData::GetDBUSAppName. r=jhorak

And do a more in-depth sanitization than what we were doing.

Differential Revision: https://phabricator.services.mozilla.com/D145863
parent 027d21e9
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -221,8 +221,8 @@ PortalLocationProvider::Startup() {
  // Call CreateSession of the location portal
  GVariantBuilder builder;

  nsAutoCString appName(gAppData->remotingName);
  appName.ReplaceChar("+/=-", '_');
  nsAutoCString appName;
  gAppData->GetDBusAppName(appName);
  g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
  g_variant_builder_add(&builder, "{sv}", "session_handle_token",
                        g_variant_new_string(appName.get()));
+4 −2
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@

#include "nsDBusRemoteClient.h"
#include "RemoteUtils.h"
#include "mozilla/XREAppData.h"
#include "mozilla/Logging.h"
#include "mozilla/Base64.h"
#include "nsPrintfCString.h"
@@ -92,7 +93,8 @@ bool nsDBusRemoteClient::GetRemoteDestinationName(const char* aProgram,
  nsAutoCString profileName;
  nsresult rv = mozilla::Base64Encode(nsAutoCString(aProfile), profileName);
  NS_ENSURE_SUCCESS(rv, false);
  profileName.ReplaceChar("+/=-", '_');

  mozilla::XREAppData::SanitizeNameForDBus(profileName);

  aDestinationName =
      nsPrintfCString("org.mozilla.%s.%s", aProgram, profileName.get());
@@ -128,7 +130,7 @@ nsresult nsDBusRemoteClient::DoSendDBusCommandLine(const char* aProgram,
  LOG("nsDBusRemoteClient::DoSendDBusCommandLine()");

  nsAutoCString appName(aProgram);
  appName.ReplaceChar("+/=-", '_');
  mozilla::XREAppData::SanitizeNameForDBus(appName);

  nsAutoCString destinationName;
  if (!GetRemoteDestinationName(appName.get(), aProfile, destinationName)) {
+10 −16
Original line number Diff line number Diff line
@@ -7,6 +7,8 @@

#include "nsDBusRemoteServer.h"

#include "nsCOMPtr.h"
#include "mozilla/XREAppData.h"
#include "mozilla/Base64.h"
#include "nsPrintfCString.h"

@@ -35,9 +37,7 @@ static const char* introspect_template =
    "</node>\n";

DBusHandlerResult nsDBusRemoteServer::Introspect(DBusMessage* msg) {
  DBusMessage* reply;

  reply = dbus_message_new_method_return(msg);
  DBusMessage* reply = dbus_message_new_method_return(msg);
  if (!reply) return DBUS_HANDLER_RESULT_NEED_MEMORY;

  nsAutoCString introspect_xml;
@@ -135,8 +135,7 @@ nsresult nsDBusRemoteServer::Startup(const char* aAppName,
      aProfileName[0] == '\0')
    return NS_ERROR_INVALID_ARG;

  mConnection =
      already_AddRefed<DBusConnection>(dbus_bus_get(DBUS_BUS_SESSION, nullptr));
  mConnection = dont_AddRef(dbus_bus_get(DBUS_BUS_SESSION, nullptr));
  if (!mConnection) {
    return NS_ERROR_FAILURE;
  }
@@ -144,21 +143,16 @@ nsresult nsDBusRemoteServer::Startup(const char* aAppName,
  dbus_connection_setup_with_g_main(mConnection, nullptr);

  mAppName = aAppName;
  ToLowerCase(mAppName);
  mozilla::XREAppData::SanitizeNameForDBus(mAppName);

  // 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(aProfileName, strlen(aProfileName), profileName);
  NS_ENSURE_SUCCESS(rv, rv);
  MOZ_TRY(
      mozilla::Base64Encode(aProfileName, strlen(aProfileName), profileName));

  profileName.ReplaceChar("+/=-", '_');
  mAppName.ReplaceChar("+/=-", '_');
  mozilla::XREAppData::SanitizeNameForDBus(profileName);

  nsAutoCString busName;
  busName =
      nsPrintfCString("org.mozilla.%s.%s", mAppName.get(), profileName.get());
  nsPrintfCString busName("org.mozilla.%s.%s", mAppName.get(),
                          profileName.get());
  if (busName.Length() > DBUS_MAXIMUM_NAME_LENGTH)
    busName.Truncate(DBUS_MAXIMUM_NAME_LENGTH);

+5 −0
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@
#include "mozilla/UniquePtrExtensions.h"
#include "nsCOMPtr.h"
#include "nsCRTGlue.h"
#include "nsStringFwd.h"
#include "nsIFile.h"

#if defined(XP_WIN) && defined(MOZ_SANDBOX)
@@ -190,6 +191,10 @@ class XREAppData {
  sandbox::BrokerServices* sandboxBrokerServices = nullptr;
  mozilla::sandboxing::PermissionsService* sandboxPermissionsService;
#endif

  // Returns a name suitable for DBUS services.
  static void SanitizeNameForDBus(nsACString&);
  void GetDBusAppName(nsACString&) const;
};

/**
+21 −0
Original line number Diff line number Diff line
@@ -31,4 +31,25 @@ XREAppData& XREAppData::operator=(const StaticXREAppData& aOther) {

XREAppData& XREAppData::operator=(const XREAppData& aOther) = default;

void XREAppData::SanitizeNameForDBus(nsACString& aName) {
  auto IsValidDBusNameChar = [](char aChar) {
    return IsAsciiAlpha(aChar) || IsAsciiDigit(aChar) || aChar == '_';
  };

  // D-Bus names can contain only [a-z][A-Z][0-9]_, so we replace all characters
  // that aren't in that range with underscores.
  char* cur = aName.BeginWriting();
  char* end = aName.EndWriting();
  for (; cur != end; cur++) {
    if (!IsValidDBusNameChar(*cur)) {
      *cur = '_';
    }
  }
}

void XREAppData::GetDBusAppName(nsACString& aName) const {
  aName.Assign(remotingName);
  SanitizeNameForDBus(aName);
}

}  // namespace mozilla