1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 sw=2 et tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "ExpandedPrincipal.h"
8 #include "nsIClassInfoImpl.h"
10 using namespace mozilla
;
12 NS_IMPL_CLASSINFO(ExpandedPrincipal
, nullptr, nsIClassInfo::MAIN_THREAD_ONLY
,
13 NS_EXPANDEDPRINCIPAL_CID
)
14 NS_IMPL_QUERY_INTERFACE_CI(ExpandedPrincipal
, nsIPrincipal
,
15 nsIExpandedPrincipal
, nsISerializable
)
16 NS_IMPL_CI_INTERFACE_GETTER(ExpandedPrincipal
, nsIPrincipal
,
17 nsIExpandedPrincipal
, nsISerializable
)
19 struct OriginComparator
{
20 bool LessThan(nsIPrincipal
* a
, nsIPrincipal
* b
) const {
21 nsAutoCString originA
;
22 DebugOnly
<nsresult
> rv
= a
->GetOrigin(originA
);
23 MOZ_ASSERT(NS_SUCCEEDED(rv
));
24 nsAutoCString originB
;
25 rv
= b
->GetOrigin(originB
);
26 MOZ_ASSERT(NS_SUCCEEDED(rv
));
27 return originA
< originB
;
30 bool Equals(nsIPrincipal
* a
, nsIPrincipal
* b
) const {
31 nsAutoCString originA
;
32 DebugOnly
<nsresult
> rv
= a
->GetOrigin(originA
);
33 MOZ_ASSERT(NS_SUCCEEDED(rv
));
34 nsAutoCString originB
;
35 rv
= b
->GetOrigin(originB
);
36 MOZ_ASSERT(NS_SUCCEEDED(rv
));
41 ExpandedPrincipal::ExpandedPrincipal(
42 nsTArray
<nsCOMPtr
<nsIPrincipal
>>& aAllowList
)
43 : BasePrincipal(eExpandedPrincipal
) {
44 // We force the principals to be sorted by origin so that ExpandedPrincipal
45 // origins can have a canonical form.
47 for (size_t i
= 0; i
< aAllowList
.Length(); ++i
) {
48 mPrincipals
.InsertElementSorted(aAllowList
[i
], c
);
52 ExpandedPrincipal::ExpandedPrincipal() : BasePrincipal(eExpandedPrincipal
) {}
54 ExpandedPrincipal::~ExpandedPrincipal() {}
56 already_AddRefed
<ExpandedPrincipal
> ExpandedPrincipal::Create(
57 nsTArray
<nsCOMPtr
<nsIPrincipal
>>& aAllowList
,
58 const OriginAttributes
& aAttrs
) {
59 RefPtr
<ExpandedPrincipal
> ep
= new ExpandedPrincipal(aAllowList
);
62 origin
.AssignLiteral("[Expanded Principal [");
63 for (size_t i
= 0; i
< ep
->mPrincipals
.Length(); ++i
) {
65 origin
.AppendLiteral(", ");
68 nsAutoCString subOrigin
;
69 DebugOnly
<nsresult
> rv
= ep
->mPrincipals
.ElementAt(i
)->GetOrigin(subOrigin
);
70 MOZ_ASSERT(NS_SUCCEEDED(rv
));
71 origin
.Append(subOrigin
);
73 origin
.AppendLiteral("]]");
75 ep
->FinishInit(origin
, aAttrs
);
80 ExpandedPrincipal::GetDomain(nsIURI
** aDomain
) {
86 ExpandedPrincipal::SetDomain(nsIURI
* aDomain
) { return NS_OK
; }
88 bool ExpandedPrincipal::SubsumesInternal(
90 BasePrincipal::DocumentDomainConsideration aConsideration
) {
91 // If aOther is an ExpandedPrincipal too, we break it down into its component
92 // nsIPrincipals, and check subsumes on each one.
93 if (Cast(aOther
)->Is
<ExpandedPrincipal
>()) {
94 auto* expanded
= Cast(aOther
)->As
<ExpandedPrincipal
>();
96 for (auto& other
: expanded
->AllowList()) {
97 // Use SubsumesInternal rather than Subsumes here, since OriginAttribute
98 // checks are only done between non-expanded sub-principals, and we don't
99 // need to incur the extra virtual call overhead.
100 if (!SubsumesInternal(other
, aConsideration
)) {
107 // We're dealing with a regular principal. One of our principals must subsume
109 for (uint32_t i
= 0; i
< mPrincipals
.Length(); ++i
) {
110 if (Cast(mPrincipals
[i
])->Subsumes(aOther
, aConsideration
)) {
118 bool ExpandedPrincipal::MayLoadInternal(nsIURI
* uri
) {
119 for (uint32_t i
= 0; i
< mPrincipals
.Length(); ++i
) {
120 if (BasePrincipal::Cast(mPrincipals
[i
])->MayLoadInternal(uri
)) {
128 uint32_t ExpandedPrincipal::GetHashValue() {
129 MOZ_CRASH("extended principal should never be used as key in a hash map");
133 ExpandedPrincipal::GetURI(nsIURI
** aURI
) {
138 const nsTArray
<nsCOMPtr
<nsIPrincipal
>>& ExpandedPrincipal::AllowList() {
143 ExpandedPrincipal::GetBaseDomain(nsACString
& aBaseDomain
) {
144 return NS_ERROR_NOT_AVAILABLE
;
148 ExpandedPrincipal::GetAddonId(nsAString
& aAddonId
) {
153 bool ExpandedPrincipal::AddonHasPermission(const nsAtom
* aPerm
) {
154 for (size_t i
= 0; i
< mPrincipals
.Length(); ++i
) {
155 if (BasePrincipal::Cast(mPrincipals
[i
])->AddonHasPermission(aPerm
)) {
162 bool ExpandedPrincipal::AddonAllowsLoad(nsIURI
* aURI
,
163 bool aExplicit
/* = false */) {
164 for (const auto& principal
: mPrincipals
) {
165 if (Cast(principal
)->AddonAllowsLoad(aURI
, aExplicit
)) {
172 nsIPrincipal
* ExpandedPrincipal::PrincipalToInherit(nsIURI
* aRequestedURI
) {
174 // If a given sub-principal subsumes the given URI, use that principal for
175 // inheritance. In general, this only happens with certain CORS modes, loads
176 // with forced principal inheritance, and creation of XML documents from
177 // XMLHttpRequests or fetch requests. For URIs that normally inherit a
178 // principal (such as data: URIs), we fall back to the last principal in the
180 for (const auto& principal
: mPrincipals
) {
181 if (Cast(principal
)->MayLoadInternal(aRequestedURI
)) {
186 return mPrincipals
.LastElement();
189 nsresult
ExpandedPrincipal::GetScriptLocation(nsACString
& aStr
) {
190 aStr
.AssignLiteral("[Expanded Principal [");
191 for (size_t i
= 0; i
< mPrincipals
.Length(); ++i
) {
193 aStr
.AppendLiteral(", ");
198 nsJSPrincipals::get(mPrincipals
.ElementAt(i
))->GetScriptLocation(spec
);
199 NS_ENSURE_SUCCESS(rv
, rv
);
203 aStr
.AppendLiteral("]]");
207 //////////////////////////////////////////
208 // Methods implementing nsISerializable //
209 //////////////////////////////////////////
211 // We've had way too many issues with unversioned serializations, so
212 // explicitly version this one.
213 static const uint32_t kSerializationVersion
= 1;
216 ExpandedPrincipal::Read(nsIObjectInputStream
* aStream
) {
218 nsresult rv
= aStream
->Read32(&version
);
219 if (version
!= kSerializationVersion
) {
221 "We really need to add handling of the old(?) version here");
222 return NS_ERROR_UNEXPECTED
;
226 rv
= aStream
->Read32(&count
);
231 if (!mPrincipals
.SetCapacity(count
, fallible
)) {
232 return NS_ERROR_OUT_OF_MEMORY
;
236 for (uint32_t i
= 0; i
< count
; ++i
) {
237 nsCOMPtr
<nsISupports
> read
;
238 rv
= aStream
->ReadObject(true, getter_AddRefs(read
));
243 nsCOMPtr
<nsIPrincipal
> principal
= do_QueryInterface(read
);
245 return NS_ERROR_UNEXPECTED
;
248 // Play it safe and InsertElementSorted, in case the sort order
249 // changed for some bizarre reason.
250 mPrincipals
.InsertElementSorted(std::move(principal
), c
);
257 ExpandedPrincipal::Write(nsIObjectOutputStream
* aStream
) {
258 nsresult rv
= aStream
->Write32(kSerializationVersion
);
263 rv
= aStream
->Write32(mPrincipals
.Length());
268 for (auto& principal
: mPrincipals
) {
269 rv
= aStream
->WriteObject(principal
, true);
278 nsresult
ExpandedPrincipal::GetSiteIdentifier(SiteIdentifier
& aSite
) {
279 // Call GetSiteIdentifier on each of our principals and return a new
280 // ExpandedPrincipal.
282 nsTArray
<nsCOMPtr
<nsIPrincipal
>> allowlist
;
283 for (const auto& principal
: mPrincipals
) {
285 nsresult rv
= Cast(principal
)->GetSiteIdentifier(site
);
286 NS_ENSURE_SUCCESS(rv
, rv
);
287 allowlist
.AppendElement(site
.GetPrincipal());
290 RefPtr
<ExpandedPrincipal
> expandedPrincipal
=
291 ExpandedPrincipal::Create(allowlist
, OriginAttributesRef());
292 MOZ_ASSERT(expandedPrincipal
, "ExpandedPrincipal::Create returned nullptr?");
294 aSite
.Init(expandedPrincipal
);