Bug 1758468 - Factor out the function to generate stable, anonymous IDs r=padenot

Differential Revision: https://phabricator.services.mozilla.com/D142549
This commit is contained in:
Gabriele Svelto 2022-04-12 12:31:27 +00:00
parent 9eb3258eb9
commit 91ba6f771f
4 changed files with 58 additions and 49 deletions

View File

@ -269,6 +269,7 @@
#include "nsIContentSecurityPolicy.h"
#include "nsIContentSink.h"
#include "nsIContentViewer.h"
#include "nsICryptoHMAC.h"
#include "nsIDOMWindowUtils.h"
#include "nsIDocShell.h"
#include "nsIDocShellTreeItem.h"
@ -289,6 +290,7 @@
#include "nsIInputStream.h"
#include "nsIInterfaceRequestor.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsIKeyModule.h"
#include "nsILoadContext.h"
#include "nsILoadGroup.h"
#include "nsILoadInfo.h"
@ -10624,6 +10626,48 @@ nsCString nsContentUtils::TruncatedURLForDisplay(nsIURI* aURL, size_t aMaxLen) {
return spec;
}
/* static */
nsresult nsContentUtils::AnonymizeId(nsAString& aId,
const nsACString& aOriginKey,
OriginFormat aFormat) {
MOZ_ASSERT(NS_IsMainThread());
nsresult rv;
nsCOMPtr<nsIKeyObjectFactory> factory =
do_GetService("@mozilla.org/security/keyobjectfactory;1", &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCString rawKey;
if (aFormat == OriginFormat::Base64) {
rv = Base64Decode(aOriginKey, rawKey);
NS_ENSURE_SUCCESS(rv, rv);
} else {
rawKey = aOriginKey;
}
nsCOMPtr<nsIKeyObject> key;
rv = factory->KeyFromString(nsIKeyObject::HMAC, rawKey, getter_AddRefs(key));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsICryptoHMAC> hasher =
do_CreateInstance(NS_CRYPTO_HMAC_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
rv = hasher->Init(nsICryptoHMAC::SHA256, key);
NS_ENSURE_SUCCESS(rv, rv);
NS_ConvertUTF16toUTF8 id(aId);
rv = hasher->Update(reinterpret_cast<const uint8_t*>(id.get()), id.Length());
NS_ENSURE_SUCCESS(rv, rv);
nsCString mac;
rv = hasher->Finish(true, mac);
NS_ENSURE_SUCCESS(rv, rv);
CopyUTF8toUTF16(mac, aId);
return NS_OK;
}
namespace mozilla {
std::ostream& operator<<(std::ostream& aOut,
const PreventDefaultResult aPreventDefaultResult) {

View File

@ -3318,6 +3318,18 @@ class nsContentUtils {
*/
static nsCString TruncatedURLForDisplay(nsIURI* aURL, size_t aMaxLen = 128);
/**
* Anonymize the given id by hashing it with the provided origin. The
* resulting id will have the same length as the one that was passed in.
*/
enum class OriginFormat {
Base64,
Plain,
};
static nsresult AnonymizeId(nsAString& aId, const nsACString& aOriginKey,
OriginFormat aFormat = OriginFormat::Base64);
private:
static bool InitializeEventTable();

View File

@ -2870,7 +2870,7 @@ RefPtr<LocalDeviceSetPromise> MediaManager::AnonymizeDevices(
RefPtr anonymized = new LocalMediaDeviceSetRefCnt();
for (const RefPtr<MediaDevice>& device : *rawDevices) {
nsString id = device->mRawID;
AnonymizeId(id, aOriginKey);
nsContentUtils::AnonymizeId(id, aOriginKey);
nsString groupId = device->mRawGroupID;
// Use window id to salt group id in order to make it session
@ -2879,7 +2879,7 @@ RefPtr<LocalDeviceSetPromise> MediaManager::AnonymizeDevices(
// against the spec. Furthermore, since device ids are the same
// after a browser restart the fingerprint is not bigger.
groupId.AppendInt(windowId);
AnonymizeId(groupId, aOriginKey);
nsContentUtils::AnonymizeId(groupId, aOriginKey);
nsString name = device->mRawName;
if (name.Find(u"AirPods"_ns) != -1) {
@ -2899,52 +2899,6 @@ RefPtr<LocalDeviceSetPromise> MediaManager::AnonymizeDevices(
});
}
/* static */
nsresult MediaManager::AnonymizeId(nsAString& aId,
const nsACString& aOriginKey) {
MOZ_ASSERT(NS_IsMainThread());
nsresult rv;
nsCOMPtr<nsIKeyObjectFactory> factory =
do_GetService("@mozilla.org/security/keyobjectfactory;1", &rv);
if (NS_FAILED(rv)) {
return rv;
}
nsCString rawKey;
rv = Base64Decode(aOriginKey, rawKey);
if (NS_FAILED(rv)) {
return rv;
}
nsCOMPtr<nsIKeyObject> key;
rv = factory->KeyFromString(nsIKeyObject::HMAC, rawKey, getter_AddRefs(key));
if (NS_FAILED(rv)) {
return rv;
}
nsCOMPtr<nsICryptoHMAC> hasher =
do_CreateInstance(NS_CRYPTO_HMAC_CONTRACTID, &rv);
if (NS_FAILED(rv)) {
return rv;
}
rv = hasher->Init(nsICryptoHMAC::SHA256, key);
if (NS_FAILED(rv)) {
return rv;
}
NS_ConvertUTF16toUTF8 id(aId);
rv = hasher->Update(reinterpret_cast<const uint8_t*>(id.get()), id.Length());
if (NS_FAILED(rv)) {
return rv;
}
nsCString mac;
rv = hasher->Finish(true, mac);
if (NS_FAILED(rv)) {
return rv;
}
CopyUTF8toUTF16(mac, aId);
return NS_OK;
}
RefPtr<LocalDeviceSetPromise> MediaManager::EnumerateDevicesImpl(
nsPIDOMWindowInner* aWindow, MediaSourceEnum aVideoInputType,
MediaSourceEnum aAudioInputType, EnumerationFlags aFlags) {

View File

@ -300,7 +300,6 @@ class MediaManager final : public nsIMediaManagerService,
private:
static nsresult GenerateUUID(nsAString& aResult);
static nsresult AnonymizeId(nsAString& aId, const nsACString& aOriginKey);
public:
/**