Bug 1776755 - Skip ordering ExpandedPrincipals to speed up creation; r=nika,necko-reviewers

Differential Revision: https://phabricator.services.mozilla.com/D161450
This commit is contained in:
june wilde 2023-03-13 14:52:04 +00:00
Родитель 950eb588f8
Коммит 05c279a3d2
5 изменённых файлов: 16 добавлений и 34 удалений

Просмотреть файл

@ -410,6 +410,16 @@ inline bool BasePrincipal::FastEqualsConsideringDomain(nsIPrincipal* aOther) {
return FastEquals(aOther);
}
// Principals of different kinds can't be equal.
if (Kind() != other->Kind()) {
return false;
}
// Only ContentPrincipals should have mHasExplicitDomain set to true, so test
// that we haven't ended up here instead of FastEquals by mistake.
MOZ_ASSERT(IsContentPrincipal(),
"Only content principals can set mHasExplicitDomain");
return Subsumes(aOther, ConsiderDocumentDomain) &&
other->Subsumes(this, ConsiderDocumentDomain);
}

Просмотреть файл

@ -20,28 +20,6 @@ NS_IMPL_QUERY_INTERFACE_CI(ExpandedPrincipal, nsIPrincipal,
NS_IMPL_CI_INTERFACE_GETTER(ExpandedPrincipal, nsIPrincipal,
nsIExpandedPrincipal)
struct OriginComparator {
bool LessThan(nsIPrincipal* a, nsIPrincipal* b) const {
nsAutoCString originA;
DebugOnly<nsresult> rv = a->GetOrigin(originA);
MOZ_ASSERT(NS_SUCCEEDED(rv));
nsAutoCString originB;
rv = b->GetOrigin(originB);
MOZ_ASSERT(NS_SUCCEEDED(rv));
return originA < originB;
}
bool Equals(nsIPrincipal* a, nsIPrincipal* b) const {
nsAutoCString originA;
DebugOnly<nsresult> rv = a->GetOrigin(originA);
MOZ_ASSERT(NS_SUCCEEDED(rv));
nsAutoCString originB;
rv = b->GetOrigin(originB);
MOZ_ASSERT(NS_SUCCEEDED(rv));
return a == b;
}
};
ExpandedPrincipal::ExpandedPrincipal(
nsTArray<nsCOMPtr<nsIPrincipal>>&& aPrincipals,
const nsACString& aOriginNoSuffix, const OriginAttributes& aAttrs)
@ -53,12 +31,9 @@ ExpandedPrincipal::~ExpandedPrincipal() = default;
already_AddRefed<ExpandedPrincipal> ExpandedPrincipal::Create(
const nsTArray<nsCOMPtr<nsIPrincipal>>& aAllowList,
const OriginAttributes& aAttrs) {
// We force the principals to be sorted by origin so that ExpandedPrincipal
// origins can have a canonical form.
nsTArray<nsCOMPtr<nsIPrincipal>> principals;
OriginComparator c;
for (size_t i = 0; i < aAllowList.Length(); ++i) {
principals.InsertElementSorted(aAllowList[i], c);
principals.AppendElement(aAllowList[i]);
}
nsAutoCString origin;
@ -248,7 +223,6 @@ ExpandedPrincipal::Deserializer::Read(nsIObjectInputStream* aStream) {
return NS_ERROR_OUT_OF_MEMORY;
}
OriginComparator c;
for (uint32_t i = 0; i < count; ++i) {
nsCOMPtr<nsISupports> read;
rv = aStream->ReadObject(true, getter_AddRefs(read));
@ -261,9 +235,7 @@ ExpandedPrincipal::Deserializer::Read(nsIObjectInputStream* aStream) {
return NS_ERROR_UNEXPECTED;
}
// Play it safe and InsertElementSorted, in case the sort order
// changed for some bizarre reason.
principals.InsertElementSorted(std::move(principal), c);
principals.AppendElement(std::move(principal));
}
mPrincipal = ExpandedPrincipal::Create(principals, OriginAttributes());

Просмотреть файл

@ -127,7 +127,7 @@ function run_test() {
// nsEP origins should be in lexical order.
Assert.equal(
ep.origin,
`[Expanded Principal [${exampleOrg.origin}, ${exampleCom.origin}, ${nullPrin.origin}]]`
`[Expanded Principal [${exampleCom.origin}, ${nullPrin.origin}, ${exampleOrg.origin}]]`
);
// Make sure createContentPrincipal does what the rest of gecko does.

Просмотреть файл

@ -63,7 +63,7 @@ function run_test()
var principal = res.responseXML.nodePrincipal;
Assert.ok(principal.isContentPrincipal);
var requestURL = "http://localhost:4444/simple";
var requestURL = "http://localhost:4444/redirect";
Assert.equal(principal.spec, requestURL);
// negative test sync XHR sending (to ensure that the xhr do not have chrome caps, see bug 779821)
@ -85,7 +85,7 @@ function run_test()
var principal = res.responseXML.nodePrincipal;
Assert.ok(principal.isContentPrincipal);
var requestURL = "http://localhost:4444/simple";
var requestURL = "http://localhost:4444/redirect";
Assert.equal(principal.spec, requestURL);
httpserver2.stop(finishIfDone);

Просмотреть файл

@ -1258,7 +1258,7 @@ function getOrigins(extension) {
page: Services.scriptSecurityManager.createContentPrincipal(pageURI, {})
.origin,
contentScript: Cu.getObjectPrincipal(
Cu.Sandbox([extension.principal, pageURL])
Cu.Sandbox([pageURL, extension.principal])
).origin,
extension: extension.principal.origin,
};