Bug 1615297 - Move Localstorage Keygen into Principal r=ckerschb,baku

Differential Revision: https://phabricator.services.mozilla.com/D62757

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Sebastian Streich 2020-03-10 18:10:08 +00:00
parent 0ab9bb5418
commit 1f23aca56c
9 changed files with 22 additions and 70 deletions

View File

@ -155,6 +155,7 @@ class BasePrincipal : public nsJSPrincipals {
nsIReferrerInfo** _retval) override;
NS_IMETHOD GetIsScriptAllowedByPolicy(
bool* aIsScriptAllowedByPolicy) override;
NS_IMETHOD GetStorageOriginKey(nsACString& aOriginKey) override;
nsresult ToJSON(nsACString& aJSON);
static already_AddRefed<BasePrincipal> FromJSON(const nsACString& aJSON);
// Method populates a passed Json::Value with serializable fields

View File

@ -330,6 +330,12 @@ interface nsIPrincipal : nsISerializable
*/
uint32_t getAboutModuleFlags();
/*
* Returns the Key to access the Principals
* Origin Local/Session Storage
*/
readonly attribute ACString storageOriginKey;
/**
* Creates and Returns a new ReferrerInfo with the
* Principals URI

View File

@ -271,8 +271,9 @@ nsresult LSObject::CreateForWindow(nsPIDOMWindowInner* aWindow,
// for the check.
nsCString originAttrSuffix;
nsCString originKey;
nsresult rv =
GenerateOriginKey(storagePrincipal, originAttrSuffix, originKey);
nsresult rv = storagePrincipal->GetStorageOriginKey(originKey);
storagePrincipal->OriginAttributesRef().CreateSuffix(originAttrSuffix);
if (NS_FAILED(rv)) {
return NS_ERROR_NOT_AVAILABLE;
}
@ -355,8 +356,8 @@ nsresult LSObject::CreateForPrincipal(nsPIDOMWindowInner* aWindow,
nsCString originAttrSuffix;
nsCString originKey;
nsresult rv =
GenerateOriginKey(aStoragePrincipal, originAttrSuffix, originKey);
nsresult rv = aStoragePrincipal->GetStorageOriginKey(originKey);
aStoragePrincipal->OriginAttributesRef().CreateSuffix(originAttrSuffix);
if (NS_FAILED(rv)) {
return NS_ERROR_NOT_AVAILABLE;
}

View File

@ -269,7 +269,8 @@ LocalStorageManager2::Preload(nsIPrincipal* aPrincipal, JSContext* aContext,
nsCString originAttrSuffix;
nsCString originKey;
nsresult rv = GenerateOriginKey(aPrincipal, originAttrSuffix, originKey);
nsresult rv = aPrincipal->GetStorageOriginKey(originKey);
aPrincipal->OriginAttributesRef().CreateSuffix(originAttrSuffix);
if (NS_FAILED(rv)) {
return NS_ERROR_NOT_AVAILABLE;
}

View File

@ -45,7 +45,8 @@ already_AddRefed<nsIPrincipal> GetContentPrincipal(const char* aSpec) {
void CheckGeneratedOriginKey(nsIPrincipal* aPrincipal, const char* aOriginKey) {
nsCString originAttrSuffix;
nsCString originKey;
nsresult rv = GenerateOriginKey(aPrincipal, originAttrSuffix, originKey);
nsresult rv = aPrincipal->GetStorageOriginKey(originKey);
aPrincipal->OriginAttributesRef().CreateSuffix(originAttrSuffix);
if (aOriginKey) {
ASSERT_EQ(rv, NS_OK) << "GenerateOriginKey should not fail";
EXPECT_TRUE(originKey == nsDependentCString(aOriginKey));

View File

@ -147,8 +147,8 @@ nsresult LocalStorageManager::GetStorageInternal(
nsAutoCString originAttrSuffix;
nsAutoCString originKey;
nsresult rv =
GenerateOriginKey(aStoragePrincipal, originAttrSuffix, originKey);
nsresult rv = aStoragePrincipal->GetStorageOriginKey(originKey);
aStoragePrincipal->OriginAttributesRef().CreateSuffix(originAttrSuffix);
if (NS_FAILED(rv)) {
return NS_ERROR_NOT_AVAILABLE;
}

View File

@ -102,7 +102,8 @@ nsresult SessionStorageManager::GetSessionStorageCacheHelper(
SessionStorageCache* aCloneFrom, RefPtr<SessionStorageCache>* aRetVal) {
nsAutoCString originKey;
nsAutoCString originAttributes;
nsresult rv = GenerateOriginKey(aPrincipal, originAttributes, originKey);
nsresult rv = aPrincipal->GetStorageOriginKey(originKey);
aPrincipal->OriginAttributesRef().CreateSuffix(originAttributes);
if (NS_FAILED(rv)) {
return NS_ERROR_NOT_AVAILABLE;
}
@ -301,7 +302,8 @@ void SessionStorageManager::SendSessionStorageDataToContentProcess(
ContentParent* const aActor, nsIPrincipal* const aPrincipal) {
nsAutoCString originAttrs;
nsAutoCString originKey;
auto rv = GenerateOriginKey(aPrincipal, originAttrs, originKey);
nsresult rv = aPrincipal->GetStorageOriginKey(originKey);
aPrincipal->OriginAttributesRef().CreateSuffix(originAttrs);
if (NS_FAILED(rv)) {
return;
}

View File

@ -19,62 +19,6 @@ namespace mozilla {
namespace dom {
namespace StorageUtils {
nsresult GenerateOriginKey(nsIPrincipal* aPrincipal,
nsACString& aOriginAttrSuffix,
nsACString& aOriginKey) {
if (NS_WARN_IF(!aPrincipal)) {
return NS_ERROR_UNEXPECTED;
}
aPrincipal->OriginAttributesRef().CreateSuffix(aOriginAttrSuffix);
nsCOMPtr<nsIURI> uri;
nsresult rv = aPrincipal->GetURI(getter_AddRefs(uri));
NS_ENSURE_SUCCESS(rv, rv);
if (!uri) {
return NS_ERROR_UNEXPECTED;
}
nsAutoCString domainOrigin;
rv = uri->GetAsciiHost(domainOrigin);
NS_ENSURE_SUCCESS(rv, rv);
if (domainOrigin.IsEmpty()) {
// For the file:/// protocol use the exact directory as domain.
if (uri->SchemeIs("file")) {
nsCOMPtr<nsIURL> url = do_QueryInterface(uri, &rv);
NS_ENSURE_SUCCESS(rv, rv);
rv = url->GetDirectory(domainOrigin);
NS_ENSURE_SUCCESS(rv, rv);
}
}
// Append reversed domain
nsAutoCString reverseDomain;
rv = CreateReversedDomain(domainOrigin, reverseDomain);
if (NS_FAILED(rv)) {
return rv;
}
aOriginKey.Append(reverseDomain);
// Append scheme
nsAutoCString scheme;
rv = uri->GetScheme(scheme);
NS_ENSURE_SUCCESS(rv, rv);
aOriginKey.Append(':');
aOriginKey.Append(scheme);
// Append port if any
int32_t port = NS_GetRealPort(uri);
if (port != -1) {
aOriginKey.Append(nsPrintfCString(":%d", port));
}
return NS_OK;
}
bool PrincipalsEqual(nsIPrincipal* aObjectPrincipal,
nsIPrincipal* aSubjectPrincipal) {
if (!aSubjectPrincipal) {

View File

@ -15,10 +15,6 @@ namespace mozilla {
namespace dom {
namespace StorageUtils {
nsresult GenerateOriginKey(nsIPrincipal* aPrincipal,
nsACString& aOriginAttrSuffix,
nsACString& aOriginKey);
bool PrincipalsEqual(nsIPrincipal* aObjectPrincipal,
nsIPrincipal* aSubjectPrincipal);