Bug Summary

File:root/firefox-clang/obj-x86_64-pc-linux-gnu/caps/./../../caps/ExpandedPrincipal.cpp
Warning:line 206, column 12
Value stored to 'rv' during its initialization is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -O2 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name Unified_cpp_caps0.cpp -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=all -relaxed-aliasing -ffp-contract=off -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/root/firefox-clang/obj-x86_64-pc-linux-gnu/caps -fcoverage-compilation-dir=/root/firefox-clang/obj-x86_64-pc-linux-gnu/caps -resource-dir /usr/lib/llvm-23/lib/clang/23 -include /root/firefox-clang/config/gcc_hidden.h -include /root/firefox-clang/obj-x86_64-pc-linux-gnu/mozilla-config.h -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/stl_wrappers -D _GLIBCXX_ASSERTIONS=1 -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/system_wrappers -U _FORTIFY_SOURCE -D _FORTIFY_SOURCE=2 -D DEBUG=1 -D MOZ_HAS_MOZGLUE -D MOZILLA_INTERNAL_API -D IMPL_LIBXUL -D MOZ_SUPPORT_LEAKCHECKING -D STATIC_EXPORTABLE_JS_API -I /root/firefox-clang/caps -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/caps -I /root/firefox-clang/docshell/base -I /root/firefox-clang/dom/base -I /root/firefox-clang/js/xpconnect/src -I /root/firefox-clang/netwerk/base -I /root/firefox-clang/netwerk/cookie -I /root/firefox-clang/toolkit/components/jsoncpp/include -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/ipc/ipdl/_ipdlheaders -I /root/firefox-clang/ipc/chromium/src -I /root/firefox-clang/third_party/abseil-cpp -I /root/firefox-clang/toolkit/components/telemetry -I /root/firefox-clang/xpcom/base -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/nspr -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/nss -D MOZILLA_CLIENT -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../include/c++/16 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../include/x86_64-linux-gnu/c++/16 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../include/c++/16/backward -internal-isystem /usr/lib/llvm-23/lib/clang/23/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -Wno-error=pessimizing-move -Wno-error=large-by-value-copy=128 -Wno-error=implicit-int-float-conversion -Wno-error=thread-safety-analysis -Wno-error=tautological-type-limit-compare -Wno-invalid-offsetof -Wno-range-loop-analysis -Wno-deprecated-anon-enum-enum-conversion -Wno-deprecated-enum-enum-conversion -Wno-inline-new-delete -Wno-error=deprecated-declarations -Wno-error=array-bounds -Wno-error=free-nonheap-object -Wno-error=atomic-alignment -Wno-error=deprecated-builtins -Wno-psabi -Wno-error=builtin-macro-redefined -Wno-vla-cxx-extension -Wno-unknown-warning-option -Wno-character-conversion -std=gnu++20 -fdeprecated-macro -ferror-limit 19 -fstrict-flex-arrays=1 -stack-protector 2 -fstack-clash-protection -ftrivial-auto-var-init=pattern -fno-rtti -fgnuc-version=4.2.1 -fno-implicit-modules -fskip-odr-check-in-gmf -fno-sized-deallocation -fno-aligned-allocation -fdiagnostics-absolute-paths -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -mllvm -dwarf-linkage-names=Abstract -faddrsig -fdwarf2-cfi-asm -o /tmp/scan-build-2026-09-01-224014-2642839-1 -x c++ Unified_cpp_caps0.cpp
1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5#include "ExpandedPrincipal.h"
6
7#include "ExpandedPrincipalJSONHandler.h"
8#include "SubsumedPrincipalJSONHandler.h"
9#include "js/JSON.h"
10#include "mozilla/Base64.h"
11#include "mozilla/JSONWriter.h"
12#include "mozilla/extensions/WebExtensionPolicy.h"
13#include "nsIClassInfoImpl.h"
14#include "nsIObjectInputStream.h"
15#include "nsReadableUtils.h"
16
17using namespace mozilla;
18
19NS_IMPL_CLASSINFO(ExpandedPrincipal, nullptr, 0, NS_EXPANDEDPRINCIPAL_CID)extern nsresult ExpandedPrincipal_GetInterfacesHelper(nsTArray
<nsIID> & array); static const GenericClassInfo::ClassInfoData
kExpandedPrincipalClassInfoData = { ExpandedPrincipal_GetInterfacesHelper
, nullptr, 0 | nsIClassInfo::SINGLETON_CLASSINFO, {0xe8ee88b0
, 0x5571, 0x4086, {0xa4, 0x5b, 0x39, 0xa7, 0x16, 0x90, 0x6b, 0xdb
}}, }; mozilla::AlignedStorage2<GenericClassInfo> kExpandedPrincipalClassInfoDataPlace
; nsIClassInfo* gExpandedPrincipal_classInfoGlobal = nullptr;
20NS_IMPL_QUERY_INTERFACE_CI(ExpandedPrincipal, nsIPrincipal,static_assert(2 > 0, "Need more arguments to NS_IMPL_QUERY_INTERFACE_CI"
); nsresult ExpandedPrincipal::QueryInterface(const nsIID&
aIID, void** aInstancePtr) { do { if (!(aInstancePtr)) { NS_DebugBreak
(NS_DEBUG_ASSERTION, "QueryInterface requires a non-NULL destination!"
, "aInstancePtr", "./../../caps/ExpandedPrincipal.cpp", 21); MOZ_PretendNoReturn
(); } } while (0); nsISupports* foundInterface; if (aIID.Equals
(mozilla::detail::kImplementedIID<std::remove_reference_t<
decltype(*this)>, nsIPrincipal>)) foundInterface = static_cast
<nsIPrincipal*>(this); else if (aIID.Equals(mozilla::detail
::kImplementedIID<std::remove_reference_t<decltype(*this
)>, nsIExpandedPrincipal>)) foundInterface = static_cast
<nsIExpandedPrincipal*>(this); else if (aIID.Equals(mozilla
::detail::kImplementedIID<std::remove_reference_t<decltype
(*this)>, nsISupports>)) foundInterface = static_cast<
nsISupports*>(static_cast<nsIPrincipal*>(this)); else
if (aIID.Equals((nsIClassInfo::kIID))) { if (!gExpandedPrincipal_classInfoGlobal
) gExpandedPrincipal_classInfoGlobal = new (kExpandedPrincipalClassInfoDataPlace
.addr()) GenericClassInfo(&kExpandedPrincipalClassInfoData
); foundInterface = gExpandedPrincipal_classInfoGlobal; } else
foundInterface = 0; nsresult status; if (!foundInterface) { do
{ static_assert( mozilla::detail::AssertionConditionType<
decltype(!aIID.Equals((nsISupports::kIID)))>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(!aIID.Equals((nsISupports::kIID
))))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("!aIID.Equals((nsISupports::kIID))", "./../../caps/ExpandedPrincipal.cpp"
, 21); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!aIID.Equals((nsISupports::kIID))"
")"); do { MOZ_CrashSequence(__null, 21); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false); status = NS_NOINTERFACE
; } else { (foundInterface)->AddRef(); status = NS_OK; } *
aInstancePtr = foundInterface; return status; }
21 nsIExpandedPrincipal)static_assert(2 > 0, "Need more arguments to NS_IMPL_QUERY_INTERFACE_CI"
); nsresult ExpandedPrincipal::QueryInterface(const nsIID&
aIID, void** aInstancePtr) { do { if (!(aInstancePtr)) { NS_DebugBreak
(NS_DEBUG_ASSERTION, "QueryInterface requires a non-NULL destination!"
, "aInstancePtr", "./../../caps/ExpandedPrincipal.cpp", 21); MOZ_PretendNoReturn
(); } } while (0); nsISupports* foundInterface; if (aIID.Equals
(mozilla::detail::kImplementedIID<std::remove_reference_t<
decltype(*this)>, nsIPrincipal>)) foundInterface = static_cast
<nsIPrincipal*>(this); else if (aIID.Equals(mozilla::detail
::kImplementedIID<std::remove_reference_t<decltype(*this
)>, nsIExpandedPrincipal>)) foundInterface = static_cast
<nsIExpandedPrincipal*>(this); else if (aIID.Equals(mozilla
::detail::kImplementedIID<std::remove_reference_t<decltype
(*this)>, nsISupports>)) foundInterface = static_cast<
nsISupports*>(static_cast<nsIPrincipal*>(this)); else
if (aIID.Equals((nsIClassInfo::kIID))) { if (!gExpandedPrincipal_classInfoGlobal
) gExpandedPrincipal_classInfoGlobal = new (kExpandedPrincipalClassInfoDataPlace
.addr()) GenericClassInfo(&kExpandedPrincipalClassInfoData
); foundInterface = gExpandedPrincipal_classInfoGlobal; } else
foundInterface = 0; nsresult status; if (!foundInterface) { do
{ static_assert( mozilla::detail::AssertionConditionType<
decltype(!aIID.Equals((nsISupports::kIID)))>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(!aIID.Equals((nsISupports::kIID
))))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("!aIID.Equals((nsISupports::kIID))", "./../../caps/ExpandedPrincipal.cpp"
, 21); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!aIID.Equals((nsISupports::kIID))"
")"); do { MOZ_CrashSequence(__null, 21); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false); status = NS_NOINTERFACE
; } else { (foundInterface)->AddRef(); status = NS_OK; } *
aInstancePtr = foundInterface; return status; }
22NS_IMPL_CI_INTERFACE_GETTER(ExpandedPrincipal, nsIPrincipal,static_assert(2 > 0, "Need more arguments to NS_IMPL_CI_INTERFACE_GETTER"
); nsresult ExpandedPrincipal_GetInterfacesHelper(nsTArray<
nsIID> & array) { array.Clear(); array.SetCapacity(2);
array.AppendElement((nsIPrincipal::kIID)); array.AppendElement
((nsIExpandedPrincipal::kIID)); return NS_OK; }
23 nsIExpandedPrincipal)static_assert(2 > 0, "Need more arguments to NS_IMPL_CI_INTERFACE_GETTER"
); nsresult ExpandedPrincipal_GetInterfacesHelper(nsTArray<
nsIID> & array) { array.Clear(); array.SetCapacity(2);
array.AppendElement((nsIPrincipal::kIID)); array.AppendElement
((nsIExpandedPrincipal::kIID)); return NS_OK; }
24
25ExpandedPrincipal::ExpandedPrincipal(
26 nsTArray<nsCOMPtr<nsIPrincipal>>&& aPrincipals,
27 const nsACString& aOriginNoSuffix, const OriginAttributes& aAttrs)
28 : BasePrincipal(eExpandedPrincipal, aOriginNoSuffix, aAttrs),
29 mPrincipals(std::move(aPrincipals)) {}
30
31ExpandedPrincipal::~ExpandedPrincipal() = default;
32
33already_AddRefed<ExpandedPrincipal> ExpandedPrincipal::Create(
34 const nsTArray<nsCOMPtr<nsIPrincipal>>& aAllowList,
35 const OriginAttributes& aAttrs) {
36 nsTArray<nsCOMPtr<nsIPrincipal>> principals;
37 for (size_t i = 0; i < aAllowList.Length(); ++i) {
38 principals.AppendElement(aAllowList[i]);
39 }
40
41 nsAutoCString origin;
42 origin.AssignLiteral("[Expanded Principal [");
43 StringJoinAppend(
44 origin, ", "_ns, principals,
45 [](nsACString& dest, const nsCOMPtr<nsIPrincipal>& principal) {
46 nsAutoCString subOrigin;
47 DebugOnly<nsresult> rv = principal->GetOrigin(subOrigin);
48 MOZ_ASSERT(NS_SUCCEEDED(rv))do { static_assert( mozilla::detail::AssertionConditionType<
decltype(((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1)
)))>::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1)
))))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1)))", "./../../caps/ExpandedPrincipal.cpp"
, 48); AnnotateMozCrashReason("MOZ_ASSERT" "(" "((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1)))"
")"); do { MOZ_CrashSequence(__null, 48); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
49 dest.Append(subOrigin);
50 });
51 origin.AppendLiteral("]]");
52
53 RefPtr<ExpandedPrincipal> ep =
54 new ExpandedPrincipal(std::move(principals), origin, aAttrs);
55 return ep.forget();
56}
57
58NS_IMETHODIMPnsresult
59ExpandedPrincipal::GetDomain(nsIURI** aDomain) {
60 *aDomain = nullptr;
61 return NS_OK;
62}
63
64NS_IMETHODIMPnsresult
65ExpandedPrincipal::SetDomain(nsIURI* aDomain) { return NS_OK; }
66
67bool ExpandedPrincipal::SubsumesInternal(
68 nsIPrincipal* aOther,
69 BasePrincipal::DocumentDomainConsideration aConsideration) {
70 // If aOther is an ExpandedPrincipal too, we break it down into its component
71 // nsIPrincipals, and check subsumes on each one.
72 if (Cast(aOther)->Is<ExpandedPrincipal>()) {
73 auto* expanded = Cast(aOther)->As<ExpandedPrincipal>();
74
75 for (auto& other : expanded->AllowList()) {
76 // Use SubsumesInternal rather than Subsumes here, since OriginAttribute
77 // checks are only done between non-expanded sub-principals, and we don't
78 // need to incur the extra virtual call overhead.
79 if (!SubsumesInternal(other, aConsideration)) {
80 return false;
81 }
82 }
83 return true;
84 }
85
86 // We're dealing with a regular principal. One of our principals must subsume
87 // it.
88 for (uint32_t i = 0; i < mPrincipals.Length(); ++i) {
89 if (Cast(mPrincipals[i])->Subsumes(aOther, aConsideration)) {
90 return true;
91 }
92 }
93
94 return false;
95}
96
97bool ExpandedPrincipal::MayLoadInternal(nsIURI* uri) {
98 for (uint32_t i = 0; i < mPrincipals.Length(); ++i) {
99 if (BasePrincipal::Cast(mPrincipals[i])->MayLoadInternal(uri)) {
100 return true;
101 }
102 }
103
104 return false;
105}
106
107NS_IMETHODIMPnsresult
108ExpandedPrincipal::GetURI(nsIURI** aURI) {
109 *aURI = nullptr;
110 return NS_OK;
111}
112
113const nsTArray<nsCOMPtr<nsIPrincipal>>& ExpandedPrincipal::AllowList() {
114 return mPrincipals;
115}
116
117NS_IMETHODIMPnsresult
118ExpandedPrincipal::GetBaseDomain(nsACString& aBaseDomain) {
119 return NS_ERROR_NOT_AVAILABLE;
120}
121
122NS_IMETHODIMPnsresult
123ExpandedPrincipal::GetAddonId(nsAString& aAddonId) {
124 aAddonId.Truncate();
125 return NS_OK;
126};
127
128bool ExpandedPrincipal::AddonHasPermission(const nsAtom* aPerm) {
129 for (size_t i = 0; i < mPrincipals.Length(); ++i) {
130 if (BasePrincipal::Cast(mPrincipals[i])->AddonHasPermission(aPerm)) {
131 return true;
132 }
133 }
134 return false;
135}
136
137bool ExpandedPrincipal::AddonAllowsLoad(nsIURI* aURI,
138 bool aExplicit /* = false */) {
139 for (const auto& principal : mPrincipals) {
140 if (Cast(principal)->AddonAllowsLoad(aURI, aExplicit)) {
141 return true;
142 }
143 }
144 return false;
145}
146
147void ExpandedPrincipal::SetCsp(nsIContentSecurityPolicy* aCSP) {
148 AssertIsOnMainThread();
149 mCSP = new nsMainThreadPtrHolder<nsIContentSecurityPolicy>(
150 "ExpandedPrincipal::mCSP", aCSP);
151}
152
153NS_IMETHODIMPnsresult
154ExpandedPrincipal::GetCsp(nsIContentSecurityPolicy** aCsp) {
155 AssertIsOnMainThread();
156 NS_IF_ADDREF(*aCsp = mCSP)ns_if_addref(*aCsp = mCSP);
157 return NS_OK;
158}
159
160nsIPrincipal* ExpandedPrincipal::PrincipalToInherit(nsIURI* aRequestedURI) {
161 if (aRequestedURI) {
162 // If a given sub-principal subsumes the given URI, use that principal for
163 // inheritance. In general, this only happens with certain CORS modes, loads
164 // with forced principal inheritance, and creation of XML documents from
165 // XMLHttpRequests or fetch requests. For URIs that normally inherit a
166 // principal (such as data: URIs), we fall back to the last principal in the
167 // allowlist.
168 for (const auto& principal : mPrincipals) {
169 if (Cast(principal)->MayLoadInternal(aRequestedURI)) {
170 return principal;
171 }
172 }
173 }
174 return mPrincipals.LastElement();
175}
176
177nsresult ExpandedPrincipal::GetScriptLocation(nsACString& aStr) {
178 aStr.AssignLiteral("[Expanded Principal [");
179 for (size_t i = 0; i < mPrincipals.Length(); ++i) {
180 if (i != 0) {
181 aStr.AppendLiteral(", ");
182 }
183
184 nsAutoCString spec;
185 nsresult rv =
186 nsJSPrincipals::get(mPrincipals.ElementAt(i))->GetScriptLocation(spec);
187 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "./../../caps/ExpandedPrincipal.cpp"
, 187); return rv; } } while (false)
;
188
189 aStr.Append(spec);
190 }
191 aStr.AppendLiteral("]]");
192 return NS_OK;
193}
194
195//////////////////////////////////////////
196// Methods implementing nsISerializable //
197//////////////////////////////////////////
198
199// We've had way too many issues with unversioned serializations, so
200// explicitly version this one.
201static const uint32_t kSerializationVersion = 1;
202
203NS_IMETHODIMPnsresult
204ExpandedPrincipal::Deserializer::Read(nsIObjectInputStream* aStream) {
205 uint32_t version;
206 nsresult rv = aStream->Read32(&version);
Value stored to 'rv' during its initialization is never read
207 if (version != kSerializationVersion) {
208 MOZ_ASSERT(false,do { static_assert( mozilla::detail::AssertionConditionType<
decltype(false)>::isValid, "invalid assertion condition");
if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while (
false); MOZ_ReportAssertionFailure("false" " (" "We really need to add handling of the old(?) version here"
")", "./../../caps/ExpandedPrincipal.cpp", 209); AnnotateMozCrashReason
("MOZ_ASSERT" "(" "false" ") (" "We really need to add handling of the old(?) version here"
")"); do { MOZ_CrashSequence(__null, 209); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
209 "We really need to add handling of the old(?) version here")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(false)>::isValid, "invalid assertion condition");
if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while (
false); MOZ_ReportAssertionFailure("false" " (" "We really need to add handling of the old(?) version here"
")", "./../../caps/ExpandedPrincipal.cpp", 209); AnnotateMozCrashReason
("MOZ_ASSERT" "(" "false" ") (" "We really need to add handling of the old(?) version here"
")"); do { MOZ_CrashSequence(__null, 209); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
210 return NS_ERROR_UNEXPECTED;
211 }
212
213 uint32_t count;
214 rv = aStream->Read32(&count);
215 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
216 return rv;
217 }
218
219 nsTArray<nsCOMPtr<nsIPrincipal>> principals;
220 if (!principals.SetCapacity(count, fallible)) {
221 return NS_ERROR_OUT_OF_MEMORY;
222 }
223
224 for (uint32_t i = 0; i < count; ++i) {
225 nsCOMPtr<nsISupports> read;
226 rv = aStream->ReadObject(true, getter_AddRefs(read));
227 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
228 return rv;
229 }
230
231 nsCOMPtr<nsIPrincipal> principal = do_QueryInterface(read);
232 if (!principal) {
233 return NS_ERROR_UNEXPECTED;
234 }
235
236 principals.AppendElement(std::move(principal));
237 }
238
239 mPrincipal = ExpandedPrincipal::Create(principals, OriginAttributes());
240 return NS_OK;
241}
242
243nsresult ExpandedPrincipal::GetSiteIdentifier(SiteIdentifier& aSite) {
244 // Call GetSiteIdentifier on each of our principals and return a new
245 // ExpandedPrincipal.
246
247 nsTArray<nsCOMPtr<nsIPrincipal>> allowlist;
248 for (const auto& principal : mPrincipals) {
249 SiteIdentifier site;
250 nsresult rv = Cast(principal)->GetSiteIdentifier(site);
251 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "./../../caps/ExpandedPrincipal.cpp"
, 251); return rv; } } while (false)
;
252 allowlist.AppendElement(site.GetPrincipal());
253 }
254
255 RefPtr<ExpandedPrincipal> expandedPrincipal =
256 ExpandedPrincipal::Create(allowlist, OriginAttributesRef());
257 MOZ_ASSERT(expandedPrincipal, "ExpandedPrincipal::Create returned nullptr?")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(expandedPrincipal)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(expandedPrincipal))), 0))) {
do { } while (false); MOZ_ReportAssertionFailure("expandedPrincipal"
" (" "ExpandedPrincipal::Create returned nullptr?" ")", "./../../caps/ExpandedPrincipal.cpp"
, 257); AnnotateMozCrashReason("MOZ_ASSERT" "(" "expandedPrincipal"
") (" "ExpandedPrincipal::Create returned nullptr?" ")"); do
{ MOZ_CrashSequence(__null, 257); __attribute__((nomerge)) ::
abort(); } while (false); } } while (false)
;
258
259 aSite.Init(expandedPrincipal);
260 return NS_OK;
261}
262
263nsresult ExpandedPrincipal::WriteJSONInnerProperties(JSONWriter& aWriter) {
264 aWriter.StartArrayProperty(JSONEnumKeyString<eSpecs>(),
265 JSONWriter::CollectionStyle::SingleLineStyle);
266
267 for (const auto& principal : mPrincipals) {
268 aWriter.StartObjectElement(JSONWriter::CollectionStyle::SingleLineStyle);
269
270 nsresult rv = BasePrincipal::Cast(principal)->WriteJSONProperties(aWriter);
271 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "./../../caps/ExpandedPrincipal.cpp"
, 271); return rv; } } while (false)
;
272
273 aWriter.EndObject();
274 }
275
276 aWriter.EndArray();
277
278 nsAutoCString suffix;
279 OriginAttributesRef().CreateSuffix(suffix);
280 if (suffix.Length() > 0) {
281 WriteJSONProperty<eSuffix>(aWriter, suffix);
282 }
283
284 return NS_OK;
285}
286
287bool ExpandedPrincipalJSONHandler::ProcessSubsumedResult(bool aResult) {
288 if (!aResult) {
289 NS_WARNING("Failed to parse subsumed principal")NS_DebugBreak(NS_DEBUG_WARNING, "Failed to parse subsumed principal"
, nullptr, "./../../caps/ExpandedPrincipal.cpp", 289)
;
290 mState = State::Error;
291 return false;
292 }
293 return true;
294}
295
296bool ExpandedPrincipalJSONHandler::startObject() {
297 if (mSubsumedHandler.isSome()) {
298 return ProcessSubsumedResult(mSubsumedHandler->startObject());
299 }
300
301 switch (mState) {
302 case State::Init:
303 mState = State::StartObject;
304 break;
305 case State::StartArray:
306 mState = State::SubsumedPrincipal;
307 [[fallthrough]];
308 case State::SubsumedPrincipal:
309 mSubsumedHandler.emplace();
310
311 return ProcessSubsumedResult(mSubsumedHandler->startObject());
312 default:
313 NS_WARNING("Unexpected object value")NS_DebugBreak(NS_DEBUG_WARNING, "Unexpected object value", nullptr
, "./../../caps/ExpandedPrincipal.cpp", 313)
;
314 mState = State::Error;
315 return false;
316 }
317
318 return true;
319}
320
321bool ExpandedPrincipalJSONHandler::propertyName(const JS::Latin1Char* name,
322 size_t length) {
323 if (mSubsumedHandler.isSome()) {
324 return ProcessSubsumedResult(mSubsumedHandler->propertyName(name, length));
325 }
326
327 switch (mState) {
328 case State::StartObject:
329 case State::AfterPropertyValue: {
330 if (length != 1) {
331 NS_WARNING(NS_DebugBreak(NS_DEBUG_WARNING, nsPrintfCString("Unexpected property name length: %zu"
, length) .get(), nullptr, "./../../caps/ExpandedPrincipal.cpp"
, 333)
332 nsPrintfCString("Unexpected property name length: %zu", length)NS_DebugBreak(NS_DEBUG_WARNING, nsPrintfCString("Unexpected property name length: %zu"
, length) .get(), nullptr, "./../../caps/ExpandedPrincipal.cpp"
, 333)
333 .get())NS_DebugBreak(NS_DEBUG_WARNING, nsPrintfCString("Unexpected property name length: %zu"
, length) .get(), nullptr, "./../../caps/ExpandedPrincipal.cpp"
, 333)
;
334 mState = State::Error;
335 return false;
336 }
337
338 char key = char(name[0]);
339 switch (key) {
340 case ExpandedPrincipal::SpecsKey:
341 mState = State::SpecsKey;
342 break;
343 case ExpandedPrincipal::SuffixKey:
344 mState = State::SuffixKey;
345 break;
346 default:
347 NS_WARNING(NS_DebugBreak(NS_DEBUG_WARNING, nsPrintfCString("Unexpected property name: '%c'"
, key).get(), nullptr, "./../../caps/ExpandedPrincipal.cpp", 348
)
348 nsPrintfCString("Unexpected property name: '%c'", key).get())NS_DebugBreak(NS_DEBUG_WARNING, nsPrintfCString("Unexpected property name: '%c'"
, key).get(), nullptr, "./../../caps/ExpandedPrincipal.cpp", 348
)
;
349 mState = State::Error;
350 return false;
351 }
352 break;
353 }
354 default:
355 NS_WARNING("Unexpected property name")NS_DebugBreak(NS_DEBUG_WARNING, "Unexpected property name", nullptr
, "./../../caps/ExpandedPrincipal.cpp", 355)
;
356 mState = State::Error;
357 return false;
358 }
359
360 return true;
361}
362
363bool ExpandedPrincipalJSONHandler::endObject() {
364 if (mSubsumedHandler.isSome()) {
365 if (!ProcessSubsumedResult(mSubsumedHandler->endObject())) {
366 return false;
367 }
368 if (mSubsumedHandler->HasAccepted()) {
369 nsCOMPtr<nsIPrincipal> principal = mSubsumedHandler->mPrincipal.forget();
370 mSubsumedHandler.reset();
371 mAllowList.AppendElement(principal);
372 }
373 return true;
374 }
375
376 switch (mState) {
377 case State::AfterPropertyValue:
378 mPrincipal = ExpandedPrincipal::Create(mAllowList, mAttrs);
379 MOZ_ASSERT(mPrincipal)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(mPrincipal)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(mPrincipal))), 0))) { do { }
while (false); MOZ_ReportAssertionFailure("mPrincipal", "./../../caps/ExpandedPrincipal.cpp"
, 379); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mPrincipal" ")"
); do { MOZ_CrashSequence(__null, 379); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
380
381 mState = State::EndObject;
382 break;
383 default:
384 NS_WARNING("Unexpected end of object")NS_DebugBreak(NS_DEBUG_WARNING, "Unexpected end of object", nullptr
, "./../../caps/ExpandedPrincipal.cpp", 384)
;
385 mState = State::Error;
386 return false;
387 }
388
389 return true;
390}
391
392bool ExpandedPrincipalJSONHandler::startArray() {
393 switch (mState) {
394 case State::SpecsKey:
395 mState = State::StartArray;
396 break;
397 default:
398 NS_WARNING("Unexpected array value")NS_DebugBreak(NS_DEBUG_WARNING, "Unexpected array value", nullptr
, "./../../caps/ExpandedPrincipal.cpp", 398)
;
399 mState = State::Error;
400 return false;
401 }
402
403 return true;
404}
405
406bool ExpandedPrincipalJSONHandler::endArray() {
407 switch (mState) {
408 case State::SubsumedPrincipal: {
409 mState = State::AfterPropertyValue;
410 break;
411 }
412 default:
413 NS_WARNING("Unexpected end of array")NS_DebugBreak(NS_DEBUG_WARNING, "Unexpected end of array", nullptr
, "./../../caps/ExpandedPrincipal.cpp", 413)
;
414 mState = State::Error;
415 return false;
416 }
417
418 return true;
419}
420
421bool ExpandedPrincipalJSONHandler::stringValue(const JS::Latin1Char* str,
422 size_t length) {
423 if (mSubsumedHandler.isSome()) {
424 return ProcessSubsumedResult(mSubsumedHandler->stringValue(str, length));
425 }
426
427 switch (mState) {
428 case State::SpecsKey: {
429 nsDependentCSubstring specs(reinterpret_cast<const char*>(str), length);
430
431 for (const nsACString& each : specs.Split(',')) {
432 nsAutoCString result;
433 nsresult rv = Base64Decode(each, result);
434 MOZ_ASSERT(NS_SUCCEEDED(rv), "failed to decode")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1)
)))>::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1)
))))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1)))" " ("
"failed to decode" ")", "./../../caps/ExpandedPrincipal.cpp"
, 434); AnnotateMozCrashReason("MOZ_ASSERT" "(" "((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1)))"
") (" "failed to decode" ")"); do { MOZ_CrashSequence(__null
, 434); __attribute__((nomerge)) ::abort(); } while (false); }
} while (false)
;
435 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
436 mState = State::Error;
437 return false;
438 }
439
440 nsCOMPtr<nsIPrincipal> principal = BasePrincipal::FromJSON(result);
441 if (!principal) {
442 mState = State::Error;
443 return false;
444 }
445 mAllowList.AppendElement(principal);
446 }
447
448 mState = State::AfterPropertyValue;
449 break;
450 }
451 case State::SuffixKey: {
452 nsDependentCSubstring attrs(reinterpret_cast<const char*>(str), length);
453 if (!mAttrs.PopulateFromSuffix(attrs)) {
454 mState = State::Error;
455 return false;
456 }
457
458 mState = State::AfterPropertyValue;
459 break;
460 }
461 default:
462 NS_WARNING("Unexpected string value")NS_DebugBreak(NS_DEBUG_WARNING, "Unexpected string value", nullptr
, "./../../caps/ExpandedPrincipal.cpp", 462)
;
463 mState = State::Error;
464 return false;
465 }
466
467 return true;
468}
469
470NS_IMETHODIMPnsresult
471ExpandedPrincipal::IsThirdPartyURI(nsIURI* aURI, bool* aRes) {
472 // ExpandedPrincipal for extension content scripts consist of two principals,
473 // the document's principal and the extension's principal.
474 // To make sure that the third-party check behaves like the web page on which
475 // the content script is running, ignore the extension's principal.
476
477 for (const auto& principal : mPrincipals) {
478 if (!Cast(principal)->AddonPolicyCore()) {
479 return Cast(principal)->IsThirdPartyURI(aURI, aRes);
480 }
481 }
482
483 if (mPrincipals.IsEmpty()) {
484 *aRes = true;
485 return NS_OK;
486 }
487
488 return Cast(mPrincipals[0])->IsThirdPartyURI(aURI, aRes);
489}