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
This commit is contained in:
Emilio Cobos Álvarez 2022-05-09 10:11:40 +00:00
parent 027d21e97d
commit d91ccf6978
5 changed files with 42 additions and 20 deletions

View File

@ -221,8 +221,8 @@ PortalLocationProvider::Startup() {
// Call CreateSession of the location portal // Call CreateSession of the location portal
GVariantBuilder builder; GVariantBuilder builder;
nsAutoCString appName(gAppData->remotingName); nsAutoCString appName;
appName.ReplaceChar("+/=-", '_'); gAppData->GetDBusAppName(appName);
g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT); g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
g_variant_builder_add(&builder, "{sv}", "session_handle_token", g_variant_builder_add(&builder, "{sv}", "session_handle_token",
g_variant_new_string(appName.get())); g_variant_new_string(appName.get()));

View File

@ -8,6 +8,7 @@
#include "nsDBusRemoteClient.h" #include "nsDBusRemoteClient.h"
#include "RemoteUtils.h" #include "RemoteUtils.h"
#include "mozilla/XREAppData.h"
#include "mozilla/Logging.h" #include "mozilla/Logging.h"
#include "mozilla/Base64.h" #include "mozilla/Base64.h"
#include "nsPrintfCString.h" #include "nsPrintfCString.h"
@ -92,7 +93,8 @@ bool nsDBusRemoteClient::GetRemoteDestinationName(const char* aProgram,
nsAutoCString profileName; nsAutoCString profileName;
nsresult rv = mozilla::Base64Encode(nsAutoCString(aProfile), profileName); nsresult rv = mozilla::Base64Encode(nsAutoCString(aProfile), profileName);
NS_ENSURE_SUCCESS(rv, false); NS_ENSURE_SUCCESS(rv, false);
profileName.ReplaceChar("+/=-", '_');
mozilla::XREAppData::SanitizeNameForDBus(profileName);
aDestinationName = aDestinationName =
nsPrintfCString("org.mozilla.%s.%s", aProgram, profileName.get()); nsPrintfCString("org.mozilla.%s.%s", aProgram, profileName.get());
@ -128,7 +130,7 @@ nsresult nsDBusRemoteClient::DoSendDBusCommandLine(const char* aProgram,
LOG("nsDBusRemoteClient::DoSendDBusCommandLine()"); LOG("nsDBusRemoteClient::DoSendDBusCommandLine()");
nsAutoCString appName(aProgram); nsAutoCString appName(aProgram);
appName.ReplaceChar("+/=-", '_'); mozilla::XREAppData::SanitizeNameForDBus(appName);
nsAutoCString destinationName; nsAutoCString destinationName;
if (!GetRemoteDestinationName(appName.get(), aProfile, destinationName)) { if (!GetRemoteDestinationName(appName.get(), aProfile, destinationName)) {

View File

@ -7,6 +7,8 @@
#include "nsDBusRemoteServer.h" #include "nsDBusRemoteServer.h"
#include "nsCOMPtr.h"
#include "mozilla/XREAppData.h"
#include "mozilla/Base64.h" #include "mozilla/Base64.h"
#include "nsPrintfCString.h" #include "nsPrintfCString.h"
@ -35,9 +37,7 @@ static const char* introspect_template =
"</node>\n"; "</node>\n";
DBusHandlerResult nsDBusRemoteServer::Introspect(DBusMessage* msg) { DBusHandlerResult nsDBusRemoteServer::Introspect(DBusMessage* msg) {
DBusMessage* reply; DBusMessage* reply = dbus_message_new_method_return(msg);
reply = dbus_message_new_method_return(msg);
if (!reply) return DBUS_HANDLER_RESULT_NEED_MEMORY; if (!reply) return DBUS_HANDLER_RESULT_NEED_MEMORY;
nsAutoCString introspect_xml; nsAutoCString introspect_xml;
@ -135,8 +135,7 @@ nsresult nsDBusRemoteServer::Startup(const char* aAppName,
aProfileName[0] == '\0') aProfileName[0] == '\0')
return NS_ERROR_INVALID_ARG; return NS_ERROR_INVALID_ARG;
mConnection = mConnection = dont_AddRef(dbus_bus_get(DBUS_BUS_SESSION, nullptr));
already_AddRefed<DBusConnection>(dbus_bus_get(DBUS_BUS_SESSION, nullptr));
if (!mConnection) { if (!mConnection) {
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
} }
@ -144,21 +143,16 @@ nsresult nsDBusRemoteServer::Startup(const char* aAppName,
dbus_connection_setup_with_g_main(mConnection, nullptr); dbus_connection_setup_with_g_main(mConnection, nullptr);
mAppName = aAppName; 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; nsAutoCString profileName;
nsresult rv = MOZ_TRY(
mozilla::Base64Encode(aProfileName, strlen(aProfileName), profileName); mozilla::Base64Encode(aProfileName, strlen(aProfileName), profileName));
NS_ENSURE_SUCCESS(rv, rv);
profileName.ReplaceChar("+/=-", '_'); mozilla::XREAppData::SanitizeNameForDBus(profileName);
mAppName.ReplaceChar("+/=-", '_');
nsAutoCString busName; nsPrintfCString busName("org.mozilla.%s.%s", mAppName.get(),
busName = profileName.get());
nsPrintfCString("org.mozilla.%s.%s", mAppName.get(), profileName.get());
if (busName.Length() > DBUS_MAXIMUM_NAME_LENGTH) if (busName.Length() > DBUS_MAXIMUM_NAME_LENGTH)
busName.Truncate(DBUS_MAXIMUM_NAME_LENGTH); busName.Truncate(DBUS_MAXIMUM_NAME_LENGTH);

View File

@ -12,6 +12,7 @@
#include "mozilla/UniquePtrExtensions.h" #include "mozilla/UniquePtrExtensions.h"
#include "nsCOMPtr.h" #include "nsCOMPtr.h"
#include "nsCRTGlue.h" #include "nsCRTGlue.h"
#include "nsStringFwd.h"
#include "nsIFile.h" #include "nsIFile.h"
#if defined(XP_WIN) && defined(MOZ_SANDBOX) #if defined(XP_WIN) && defined(MOZ_SANDBOX)
@ -190,6 +191,10 @@ class XREAppData {
sandbox::BrokerServices* sandboxBrokerServices = nullptr; sandbox::BrokerServices* sandboxBrokerServices = nullptr;
mozilla::sandboxing::PermissionsService* sandboxPermissionsService; mozilla::sandboxing::PermissionsService* sandboxPermissionsService;
#endif #endif
// Returns a name suitable for DBUS services.
static void SanitizeNameForDBus(nsACString&);
void GetDBusAppName(nsACString&) const;
}; };
/** /**

View File

@ -31,4 +31,25 @@ XREAppData& XREAppData::operator=(const StaticXREAppData& aOther) {
XREAppData& XREAppData::operator=(const XREAppData& aOther) = default; 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 } // namespace mozilla