Source code

Revision control

Other Tools

1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 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/. */
6
7
#include "nsDocShellLoadState.h"
8
#include "nsIDocShell.h"
9
#include "SHEntryParent.h"
10
#include "SHEntryChild.h"
11
#include "nsISHEntry.h"
12
#include "nsIWebNavigation.h"
13
#include "nsIChannel.h"
14
#include "ReferrerInfo.h"
15
#include "mozilla/BasePrincipal.h"
16
#include "mozilla/dom/BrowsingContext.h"
17
#include "mozilla/dom/LoadURIOptionsBinding.h"
18
#include "mozilla/StaticPrefs_fission.h"
19
20
#include "mozilla/OriginAttributes.h"
21
#include "mozilla/NullPrincipal.h"
22
23
#include "mozilla/dom/PContent.h"
24
25
nsDocShellLoadState::nsDocShellLoadState(nsIURI* aURI)
26
: mURI(aURI),
27
mResultPrincipalURIIsSome(false),
28
mKeepResultPrincipalURIIfSet(false),
29
mLoadReplace(false),
30
mInheritPrincipal(false),
31
mPrincipalIsExplicit(false),
32
mForceAllowDataURI(false),
33
mOriginalFrameSrc(false),
34
mIsFormSubmission(false),
35
mLoadType(LOAD_NORMAL),
36
mTarget(),
37
mSrcdocData(VoidString()),
38
mLoadFlags(0),
39
mFirstParty(false),
40
mTypeHint(VoidCString()),
41
mFileName(VoidString()),
42
mIsFromProcessingFrameAttributes(false) {
43
MOZ_ASSERT(aURI, "Cannot create a LoadState with a null URI!");
44
}
45
46
nsDocShellLoadState::nsDocShellLoadState(
47
const DocShellLoadStateInit& aLoadState) {
48
MOZ_ASSERT(aLoadState.URI(), "Cannot create a LoadState with a null URI!");
49
mResultPrincipalURI = aLoadState.ResultPrincipalURI();
50
mResultPrincipalURIIsSome = aLoadState.ResultPrincipalURIIsSome();
51
mKeepResultPrincipalURIIfSet = aLoadState.KeepResultPrincipalURIIfSet();
52
mLoadReplace = aLoadState.LoadReplace();
53
mInheritPrincipal = aLoadState.InheritPrincipal();
54
mPrincipalIsExplicit = aLoadState.PrincipalIsExplicit();
55
mForceAllowDataURI = aLoadState.ForceAllowDataURI();
56
mOriginalFrameSrc = aLoadState.OriginalFrameSrc();
57
mIsFormSubmission = aLoadState.IsFormSubmission();
58
mLoadType = aLoadState.LoadType();
59
mTarget = aLoadState.Target();
60
mLoadFlags = aLoadState.LoadFlags();
61
mFirstParty = aLoadState.FirstParty();
62
mTypeHint = aLoadState.TypeHint();
63
mFileName = aLoadState.FileName();
64
mIsFromProcessingFrameAttributes =
65
aLoadState.IsFromProcessingFrameAttributes();
66
mReferrerInfo = aLoadState.ReferrerInfo();
67
mURI = aLoadState.URI();
68
mOriginalURI = aLoadState.OriginalURI();
69
mBaseURI = aLoadState.BaseURI();
70
mTriggeringPrincipal = aLoadState.TriggeringPrincipal();
71
mPrincipalToInherit = aLoadState.PrincipalToInherit();
72
mStoragePrincipalToInherit = aLoadState.StoragePrincipalToInherit();
73
mCsp = aLoadState.Csp();
74
mOriginalURIString = aLoadState.OriginalURIString();
75
mCancelContentJSEpoch = aLoadState.CancelContentJSEpoch();
76
mPostDataStream = aLoadState.PostDataStream();
77
mHeadersStream = aLoadState.HeadersStream();
78
mSrcdocData = aLoadState.SrcdocData();
79
mResultPrincipalURI = aLoadState.ResultPrincipalURI();
80
if (!aLoadState.SHEntry() || !StaticPrefs::fission_sessionHistoryInParent()) {
81
return;
82
}
83
if (XRE_IsParentProcess()) {
84
mSHEntry = static_cast<LegacySHEntry*>(aLoadState.SHEntry());
85
} else {
86
mSHEntry = static_cast<SHEntryChild*>(aLoadState.SHEntry());
87
}
88
}
89
90
nsDocShellLoadState::~nsDocShellLoadState() {}
91
92
nsresult nsDocShellLoadState::CreateFromPendingChannel(
93
nsIChannel* aPendingChannel, nsDocShellLoadState** aResult) {
94
// Create the nsDocShellLoadState object with default state pulled from the
95
// passed-in channel.
96
nsCOMPtr<nsIURI> uri;
97
nsresult rv = aPendingChannel->GetURI(getter_AddRefs(uri));
98
if (NS_WARN_IF(NS_FAILED(rv))) {
99
return rv;
100
}
101
102
RefPtr<nsDocShellLoadState> loadState = new nsDocShellLoadState(uri);
103
loadState->mPendingRedirectedChannel = aPendingChannel;
104
105
// Pull relevant state from the channel, and store it on the
106
// nsDocShellLoadState.
107
nsCOMPtr<nsIURI> originalUri;
108
rv = aPendingChannel->GetOriginalURI(getter_AddRefs(originalUri));
109
if (NS_WARN_IF(NS_FAILED(rv))) {
110
return rv;
111
}
112
loadState->SetOriginalURI(originalUri);
113
114
nsCOMPtr<nsILoadInfo> loadInfo = aPendingChannel->LoadInfo();
115
loadState->SetTriggeringPrincipal(loadInfo->TriggeringPrincipal());
116
117
// Return the newly created loadState.
118
loadState.forget(aResult);
119
return NS_OK;
120
}
121
122
nsresult nsDocShellLoadState::CreateFromLoadURIOptions(
123
nsISupports* aConsumer, nsIURIFixup* aURIFixup, const nsAString& aURI,
124
const LoadURIOptions& aLoadURIOptions, nsDocShellLoadState** aResult) {
125
uint32_t loadFlags = aLoadURIOptions.mLoadFlags;
126
127
NS_ASSERTION(
128
(loadFlags & nsDocShell::INTERNAL_LOAD_FLAGS_LOADURI_SETUP_FLAGS) == 0,
129
"Unexpected flags");
130
131
nsCOMPtr<nsIURI> uri;
132
nsCOMPtr<nsIInputStream> postData(aLoadURIOptions.mPostData);
133
nsresult rv = NS_OK;
134
135
NS_ConvertUTF16toUTF8 uriString(aURI);
136
// Cleanup the empty spaces that might be on each end.
137
uriString.Trim(" ");
138
// Eliminate embedded newlines, which single-line text fields now allow:
139
uriString.StripCRLF();
140
NS_ENSURE_TRUE(!uriString.IsEmpty(), NS_ERROR_FAILURE);
141
142
nsCOMPtr<nsIURIFixupInfo> fixupInfo;
143
if (aURIFixup) {
144
uint32_t fixupFlags;
145
rv = aURIFixup->WebNavigationFlagsToFixupFlags(uriString, loadFlags,
146
&fixupFlags);
147
NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE);
148
149
// If we don't allow keyword lookups for this URL string, make sure to
150
// update loadFlags to indicate this as well.
151
if (!(fixupFlags & nsIURIFixup::FIXUP_FLAG_ALLOW_KEYWORD_LOOKUP)) {
152
loadFlags &= ~nsIWebNavigation::LOAD_FLAGS_ALLOW_THIRD_PARTY_FIXUP;
153
}
154
// The consumer is either a DocShell or an Element.
155
nsCOMPtr<nsILoadContext> loadContext = do_QueryInterface(aConsumer);
156
if (!loadContext) {
157
if (RefPtr<Element> element = do_QueryObject(aConsumer)) {
158
loadContext = do_QueryInterface(element->OwnerDoc()->GetDocShell());
159
}
160
}
161
// Ensure URIFixup will use the right search engine in Private Browsing.
162
MOZ_ASSERT(loadContext, "We should always have a LoadContext here.");
163
if (loadContext && loadContext->UsePrivateBrowsing()) {
164
fixupFlags |= nsIURIFixup::FIXUP_FLAG_PRIVATE_CONTEXT;
165
}
166
nsCOMPtr<nsIInputStream> fixupStream;
167
rv = aURIFixup->GetFixupURIInfo(uriString, fixupFlags,
168
getter_AddRefs(fixupStream),
169
getter_AddRefs(fixupInfo));
170
171
if (NS_SUCCEEDED(rv)) {
172
fixupInfo->GetPreferredURI(getter_AddRefs(uri));
173
fixupInfo->SetConsumer(aConsumer);
174
}
175
176
if (fixupStream) {
177
// GetFixupURIInfo only returns a post data stream if it succeeded
178
// and changed the URI, in which case we should override the
179
// passed-in post data.
180
postData = fixupStream;
181
}
182
183
if (loadFlags & nsIWebNavigation::LOAD_FLAGS_ALLOW_THIRD_PARTY_FIXUP) {
184
nsCOMPtr<nsIObserverService> serv = services::GetObserverService();
185
if (serv) {
186
serv->NotifyObservers(fixupInfo, "keyword-uri-fixup",
187
PromiseFlatString(aURI).get());
188
}
189
}
190
} else {
191
// No fixup service so just create a URI and see what happens...
192
rv = NS_NewURI(getter_AddRefs(uri), uriString);
193
loadFlags &= ~nsIWebNavigation::LOAD_FLAGS_ALLOW_THIRD_PARTY_FIXUP;
194
}
195
196
if (rv == NS_ERROR_MALFORMED_URI) {
197
MOZ_ASSERT(!uri);
198
return rv;
199
}
200
201
if (NS_FAILED(rv) || !uri) {
202
return NS_ERROR_FAILURE;
203
}
204
205
uint64_t available;
206
if (postData) {
207
rv = postData->Available(&available);
208
NS_ENSURE_SUCCESS(rv, rv);
209
if (available == 0) {
210
return NS_ERROR_INVALID_ARG;
211
}
212
}
213
214
if (aLoadURIOptions.mHeaders) {
215
rv = aLoadURIOptions.mHeaders->Available(&available);
216
NS_ENSURE_SUCCESS(rv, rv);
217
if (available == 0) {
218
return NS_ERROR_INVALID_ARG;
219
}
220
}
221
222
bool forceAllowDataURI =
223
loadFlags & nsIWebNavigation::LOAD_FLAGS_FORCE_ALLOW_DATA_URI;
224
225
// Don't pass certain flags that aren't needed and end up confusing
226
// ConvertLoadTypeToDocShellInfoLoadType. We do need to ensure that they are
227
// passed to LoadURI though, since it uses them.
228
uint32_t extraFlags = (loadFlags & EXTRA_LOAD_FLAGS);
229
loadFlags &= ~EXTRA_LOAD_FLAGS;
230
231
RefPtr<nsDocShellLoadState> loadState = new nsDocShellLoadState(uri);
232
loadState->SetReferrerInfo(aLoadURIOptions.mReferrerInfo);
233
234
/*
235
* If the user "Disables Protection on This Page", we have to make sure to
236
* remember the users decision when opening links in child tabs [Bug 906190]
237
*/
238
if (loadFlags & nsIWebNavigation::LOAD_FLAGS_ALLOW_MIXED_CONTENT) {
239
loadState->SetLoadType(
240
MAKE_LOAD_TYPE(LOAD_NORMAL_ALLOW_MIXED_CONTENT, loadFlags));
241
} else {
242
loadState->SetLoadType(MAKE_LOAD_TYPE(LOAD_NORMAL, loadFlags));
243
}
244
245
loadState->SetLoadFlags(extraFlags);
246
loadState->SetFirstParty(true);
247
loadState->SetPostDataStream(postData);
248
loadState->SetHeadersStream(aLoadURIOptions.mHeaders);
249
loadState->SetBaseURI(aLoadURIOptions.mBaseURI);
250
loadState->SetTriggeringPrincipal(aLoadURIOptions.mTriggeringPrincipal);
251
loadState->SetCsp(aLoadURIOptions.mCsp);
252
loadState->SetForceAllowDataURI(forceAllowDataURI);
253
loadState->SetOriginalURIString(uriString);
254
if (aLoadURIOptions.mCancelContentJSEpoch) {
255
loadState->SetCancelContentJSEpoch(aLoadURIOptions.mCancelContentJSEpoch);
256
}
257
258
if (fixupInfo) {
259
nsAutoString searchProvider, keyword;
260
fixupInfo->GetKeywordProviderName(searchProvider);
261
fixupInfo->GetKeywordAsSent(keyword);
262
nsDocShell::MaybeNotifyKeywordSearchLoading(searchProvider, keyword);
263
}
264
265
loadState.forget(aResult);
266
return NS_OK;
267
}
268
269
nsIReferrerInfo* nsDocShellLoadState::GetReferrerInfo() const {
270
return mReferrerInfo;
271
}
272
273
void nsDocShellLoadState::SetReferrerInfo(nsIReferrerInfo* aReferrerInfo) {
274
mReferrerInfo = aReferrerInfo;
275
}
276
277
nsIURI* nsDocShellLoadState::URI() const { return mURI; }
278
279
void nsDocShellLoadState::SetURI(nsIURI* aURI) { mURI = aURI; }
280
281
nsIURI* nsDocShellLoadState::OriginalURI() const { return mOriginalURI; }
282
283
void nsDocShellLoadState::SetOriginalURI(nsIURI* aOriginalURI) {
284
mOriginalURI = aOriginalURI;
285
}
286
287
nsIURI* nsDocShellLoadState::ResultPrincipalURI() const {
288
return mResultPrincipalURI;
289
}
290
291
void nsDocShellLoadState::SetResultPrincipalURI(nsIURI* aResultPrincipalURI) {
292
mResultPrincipalURI = aResultPrincipalURI;
293
}
294
295
bool nsDocShellLoadState::ResultPrincipalURIIsSome() const {
296
return mResultPrincipalURIIsSome;
297
}
298
299
void nsDocShellLoadState::SetResultPrincipalURIIsSome(bool aIsSome) {
300
mResultPrincipalURIIsSome = aIsSome;
301
}
302
303
bool nsDocShellLoadState::KeepResultPrincipalURIIfSet() const {
304
return mKeepResultPrincipalURIIfSet;
305
}
306
307
void nsDocShellLoadState::SetKeepResultPrincipalURIIfSet(bool aKeep) {
308
mKeepResultPrincipalURIIfSet = aKeep;
309
}
310
311
bool nsDocShellLoadState::LoadReplace() const { return mLoadReplace; }
312
313
void nsDocShellLoadState::SetLoadReplace(bool aLoadReplace) {
314
mLoadReplace = aLoadReplace;
315
}
316
317
nsIPrincipal* nsDocShellLoadState::TriggeringPrincipal() const {
318
return mTriggeringPrincipal;
319
}
320
321
void nsDocShellLoadState::SetTriggeringPrincipal(
322
nsIPrincipal* aTriggeringPrincipal) {
323
mTriggeringPrincipal = aTriggeringPrincipal;
324
}
325
326
nsIPrincipal* nsDocShellLoadState::PrincipalToInherit() const {
327
return mPrincipalToInherit;
328
}
329
330
void nsDocShellLoadState::SetPrincipalToInherit(
331
nsIPrincipal* aPrincipalToInherit) {
332
mPrincipalToInherit = aPrincipalToInherit;
333
}
334
335
nsIPrincipal* nsDocShellLoadState::StoragePrincipalToInherit() const {
336
return mStoragePrincipalToInherit;
337
}
338
339
void nsDocShellLoadState::SetStoragePrincipalToInherit(
340
nsIPrincipal* aStoragePrincipalToInherit) {
341
mStoragePrincipalToInherit = aStoragePrincipalToInherit;
342
}
343
344
void nsDocShellLoadState::SetCsp(nsIContentSecurityPolicy* aCsp) {
345
mCsp = aCsp;
346
}
347
348
nsIContentSecurityPolicy* nsDocShellLoadState::Csp() const { return mCsp; }
349
350
bool nsDocShellLoadState::InheritPrincipal() const { return mInheritPrincipal; }
351
352
void nsDocShellLoadState::SetInheritPrincipal(bool aInheritPrincipal) {
353
mInheritPrincipal = aInheritPrincipal;
354
}
355
356
bool nsDocShellLoadState::PrincipalIsExplicit() const {
357
return mPrincipalIsExplicit;
358
}
359
360
void nsDocShellLoadState::SetPrincipalIsExplicit(bool aPrincipalIsExplicit) {
361
mPrincipalIsExplicit = aPrincipalIsExplicit;
362
}
363
364
bool nsDocShellLoadState::ForceAllowDataURI() const {
365
return mForceAllowDataURI;
366
}
367
368
void nsDocShellLoadState::SetForceAllowDataURI(bool aForceAllowDataURI) {
369
mForceAllowDataURI = aForceAllowDataURI;
370
}
371
372
bool nsDocShellLoadState::OriginalFrameSrc() const { return mOriginalFrameSrc; }
373
374
void nsDocShellLoadState::SetOriginalFrameSrc(bool aOriginalFrameSrc) {
375
mOriginalFrameSrc = aOriginalFrameSrc;
376
}
377
378
bool nsDocShellLoadState::IsFormSubmission() const { return mIsFormSubmission; }
379
380
void nsDocShellLoadState::SetIsFormSubmission(bool aIsFormSubmission) {
381
mIsFormSubmission = aIsFormSubmission;
382
}
383
384
uint32_t nsDocShellLoadState::LoadType() const { return mLoadType; }
385
386
void nsDocShellLoadState::SetLoadType(uint32_t aLoadType) {
387
mLoadType = aLoadType;
388
}
389
390
nsISHEntry* nsDocShellLoadState::SHEntry() const { return mSHEntry; }
391
392
void nsDocShellLoadState::SetSHEntry(nsISHEntry* aSHEntry) {
393
mSHEntry = aSHEntry;
394
}
395
396
const nsString& nsDocShellLoadState::Target() const { return mTarget; }
397
398
void nsDocShellLoadState::SetTarget(const nsAString& aTarget) {
399
mTarget = aTarget;
400
}
401
402
nsIInputStream* nsDocShellLoadState::PostDataStream() const {
403
return mPostDataStream;
404
}
405
406
void nsDocShellLoadState::SetPostDataStream(nsIInputStream* aStream) {
407
mPostDataStream = aStream;
408
}
409
410
nsIInputStream* nsDocShellLoadState::HeadersStream() const {
411
return mHeadersStream;
412
}
413
414
void nsDocShellLoadState::SetHeadersStream(nsIInputStream* aHeadersStream) {
415
mHeadersStream = aHeadersStream;
416
}
417
418
const nsString& nsDocShellLoadState::SrcdocData() const { return mSrcdocData; }
419
420
void nsDocShellLoadState::SetSrcdocData(const nsAString& aSrcdocData) {
421
mSrcdocData = aSrcdocData;
422
}
423
424
nsIDocShell* nsDocShellLoadState::SourceDocShell() const {
425
return mSourceDocShell;
426
}
427
428
void nsDocShellLoadState::SetSourceDocShell(nsIDocShell* aSourceDocShell) {
429
mSourceDocShell = aSourceDocShell;
430
}
431
432
nsIURI* nsDocShellLoadState::BaseURI() const { return mBaseURI; }
433
434
void nsDocShellLoadState::SetBaseURI(nsIURI* aBaseURI) { mBaseURI = aBaseURI; }
435
436
void nsDocShellLoadState::GetMaybeResultPrincipalURI(
437
mozilla::Maybe<nsCOMPtr<nsIURI>>& aRPURI) const {
438
bool isSome = ResultPrincipalURIIsSome();
439
aRPURI.reset();
440
441
if (!isSome) {
442
return;
443
}
444
445
nsCOMPtr<nsIURI> uri = ResultPrincipalURI();
446
aRPURI.emplace(std::move(uri));
447
}
448
449
void nsDocShellLoadState::SetMaybeResultPrincipalURI(
450
mozilla::Maybe<nsCOMPtr<nsIURI>> const& aRPURI) {
451
SetResultPrincipalURI(aRPURI.refOr(nullptr));
452
SetResultPrincipalURIIsSome(aRPURI.isSome());
453
}
454
455
uint32_t nsDocShellLoadState::LoadFlags() const { return mLoadFlags; }
456
457
void nsDocShellLoadState::SetLoadFlags(uint32_t aLoadFlags) {
458
mLoadFlags = aLoadFlags;
459
}
460
461
void nsDocShellLoadState::SetLoadFlag(uint32_t aFlag) { mLoadFlags |= aFlag; }
462
463
void nsDocShellLoadState::UnsetLoadFlag(uint32_t aFlag) {
464
mLoadFlags &= ~aFlag;
465
}
466
467
bool nsDocShellLoadState::HasLoadFlags(uint32_t aFlags) {
468
return (mLoadFlags & aFlags) == aFlags;
469
}
470
471
bool nsDocShellLoadState::FirstParty() const { return mFirstParty; }
472
473
void nsDocShellLoadState::SetFirstParty(bool aFirstParty) {
474
mFirstParty = aFirstParty;
475
}
476
477
const nsCString& nsDocShellLoadState::TypeHint() const { return mTypeHint; }
478
479
void nsDocShellLoadState::SetTypeHint(const nsCString& aTypeHint) {
480
mTypeHint = aTypeHint;
481
}
482
483
const nsString& nsDocShellLoadState::FileName() const { return mFileName; }
484
485
void nsDocShellLoadState::SetFileName(const nsAString& aFileName) {
486
mFileName = aFileName;
487
}
488
489
nsresult nsDocShellLoadState::SetupInheritingPrincipal(
490
BrowsingContext::Type aType,
491
const mozilla::OriginAttributes& aOriginAttributes) {
492
// We need a principalToInherit.
493
//
494
// If principalIsExplicit is not set there are 4 possibilities:
495
// (1) If the system principal or an expanded principal was passed
496
// in and we're a typeContent docshell, inherit the principal
497
// from the current document instead.
498
// (2) In all other cases when the principal passed in is not null,
499
// use that principal.
500
// (3) If the caller has allowed inheriting from the current document,
501
// or if we're being called from system code (eg chrome JS or pure
502
// C++) then inheritPrincipal should be true and InternalLoad will get
503
// a principal from the current document. If none of these things are
504
// true, then
505
// (4) we don't pass a principal into the channel, and a principal will be
506
// created later from the channel's internal data.
507
//
508
// If principalIsExplicit *is* set, there are 4 possibilities
509
// (1) If the system principal or an expanded principal was passed in
510
// and we're a typeContent docshell, return an error.
511
// (2) In all other cases when the principal passed in is not null,
512
// use that principal.
513
// (3) If the caller has allowed inheriting from the current document,
514
// then inheritPrincipal should be true and InternalLoad will get
515
// a principal from the current document. If none of these things are
516
// true, then
517
// (4) we dont' pass a principal into the channel, and a principal will be
518
// created later from the channel's internal data.
519
mPrincipalToInherit = mTriggeringPrincipal;
520
if (mPrincipalToInherit && aType != BrowsingContext::Type::Chrome) {
521
if (mPrincipalToInherit->IsSystemPrincipal()) {
522
if (mPrincipalIsExplicit) {
523
return NS_ERROR_DOM_SECURITY_ERR;
524
}
525
mPrincipalToInherit = nullptr;
526
mInheritPrincipal = true;
527
} else if (nsContentUtils::IsExpandedPrincipal(mPrincipalToInherit)) {
528
if (mPrincipalIsExplicit) {
529
return NS_ERROR_DOM_SECURITY_ERR;
530
}
531
// Don't inherit from the current page. Just do the safe thing
532
// and pretend that we were loaded by a nullprincipal.
533
//
534
// We didn't inherit OriginAttributes here as ExpandedPrincipal doesn't
535
// have origin attributes.
536
mPrincipalToInherit = NullPrincipal::CreateWithInheritedAttributes(
537
aOriginAttributes, false);
538
mInheritPrincipal = false;
539
}
540
}
541
542
if (!mPrincipalToInherit && !mInheritPrincipal && !mPrincipalIsExplicit) {
543
// See if there's system or chrome JS code running
544
mInheritPrincipal = nsContentUtils::LegacyIsCallerChromeOrNativeCode();
545
}
546
547
if (mLoadFlags & nsIWebNavigation::LOAD_FLAGS_DISALLOW_INHERIT_PRINCIPAL) {
548
mInheritPrincipal = false;
549
// If mFirstParty is true and the pref 'privacy.firstparty.isolate' is
550
// enabled, we will set firstPartyDomain on the origin attributes.
551
mPrincipalToInherit = NullPrincipal::CreateWithInheritedAttributes(
552
aOriginAttributes, mFirstParty);
553
}
554
555
return NS_OK;
556
}
557
558
nsresult nsDocShellLoadState::SetupTriggeringPrincipal(
559
const mozilla::OriginAttributes& aOriginAttributes) {
560
// If the triggeringPrincipal is not set, we first try to create a principal
561
// from the referrer, since the referrer URI reflects the web origin that
562
// triggered the load. If there is no referrer URI, we fall back to using the
563
// SystemPrincipal. It's safe to assume that no provided triggeringPrincipal
564
// and no referrer simulate a load that was triggered by the system. It's
565
// important to note that this block of code needs to appear *after* the block
566
// where we munge the principalToInherit, because otherwise we would never
567
// enter code blocks checking if the principalToInherit is null and we will
568
// end up with a wrong inheritPrincipal flag.
569
if (!mTriggeringPrincipal) {
570
if (mReferrerInfo) {
571
nsCOMPtr<nsIURI> referrer = mReferrerInfo->GetOriginalReferrer();
572
mTriggeringPrincipal =
573
BasePrincipal::CreateContentPrincipal(referrer, aOriginAttributes);
574
575
if (!mTriggeringPrincipal) {
576
return NS_ERROR_FAILURE;
577
}
578
} else {
579
mTriggeringPrincipal = nsContentUtils::GetSystemPrincipal();
580
}
581
}
582
return NS_OK;
583
}
584
585
void nsDocShellLoadState::CalculateLoadURIFlags() {
586
uint32_t oldLoadFlags = mLoadFlags;
587
mLoadFlags = 0;
588
589
if (mInheritPrincipal) {
590
MOZ_ASSERT(
591
!mPrincipalToInherit || !mPrincipalToInherit->IsSystemPrincipal(),
592
"Should not inherit SystemPrincipal");
593
mLoadFlags |= nsDocShell::INTERNAL_LOAD_FLAGS_INHERIT_PRINCIPAL;
594
}
595
596
if (mReferrerInfo && !mReferrerInfo->GetSendReferrer()) {
597
mLoadFlags |= nsDocShell::INTERNAL_LOAD_FLAGS_DONT_SEND_REFERRER;
598
}
599
if (oldLoadFlags & nsIWebNavigation::LOAD_FLAGS_ALLOW_THIRD_PARTY_FIXUP) {
600
mLoadFlags |= nsDocShell::INTERNAL_LOAD_FLAGS_ALLOW_THIRD_PARTY_FIXUP;
601
}
602
603
if (oldLoadFlags & nsIWebNavigation::LOAD_FLAGS_FIRST_LOAD) {
604
mLoadFlags |= nsDocShell::INTERNAL_LOAD_FLAGS_FIRST_LOAD;
605
}
606
607
if (oldLoadFlags & nsIWebNavigation::LOAD_FLAGS_BYPASS_CLASSIFIER) {
608
mLoadFlags |= nsDocShell::INTERNAL_LOAD_FLAGS_BYPASS_CLASSIFIER;
609
}
610
611
if (oldLoadFlags & nsIWebNavigation::LOAD_FLAGS_FORCE_ALLOW_COOKIES) {
612
mLoadFlags |= nsDocShell::INTERNAL_LOAD_FLAGS_FORCE_ALLOW_COOKIES;
613
}
614
615
if (oldLoadFlags & nsIWebNavigation::LOAD_FLAGS_BYPASS_LOAD_URI_DELEGATE) {
616
mLoadFlags |= nsDocShell::INTERNAL_LOAD_FLAGS_BYPASS_LOAD_URI_DELEGATE;
617
}
618
619
if (!mSrcdocData.IsVoid()) {
620
mLoadFlags |= nsDocShell::INTERNAL_LOAD_FLAGS_IS_SRCDOC;
621
}
622
623
if (mForceAllowDataURI) {
624
mLoadFlags |= nsDocShell::INTERNAL_LOAD_FLAGS_FORCE_ALLOW_DATA_URI;
625
}
626
627
if (mOriginalFrameSrc) {
628
mLoadFlags |= nsDocShell::INTERNAL_LOAD_FLAGS_ORIGINAL_FRAME_SRC;
629
}
630
}
631
632
DocShellLoadStateInit nsDocShellLoadState::Serialize() {
633
DocShellLoadStateInit loadState;
634
loadState.ResultPrincipalURI() = mResultPrincipalURI;
635
loadState.ResultPrincipalURIIsSome() = mResultPrincipalURIIsSome;
636
loadState.KeepResultPrincipalURIIfSet() = mKeepResultPrincipalURIIfSet;
637
loadState.LoadReplace() = mLoadReplace;
638
loadState.InheritPrincipal() = mInheritPrincipal;
639
loadState.PrincipalIsExplicit() = mPrincipalIsExplicit;
640
loadState.ForceAllowDataURI() = mForceAllowDataURI;
641
loadState.OriginalFrameSrc() = mOriginalFrameSrc;
642
loadState.IsFormSubmission() = mIsFormSubmission;
643
loadState.LoadType() = mLoadType;
644
loadState.Target() = mTarget;
645
loadState.LoadFlags() = mLoadFlags;
646
loadState.FirstParty() = mFirstParty;
647
loadState.TypeHint() = mTypeHint;
648
loadState.FileName() = mFileName;
649
loadState.IsFromProcessingFrameAttributes() =
650
mIsFromProcessingFrameAttributes;
651
loadState.URI() = mURI;
652
loadState.OriginalURI() = mOriginalURI;
653
loadState.BaseURI() = mBaseURI;
654
loadState.TriggeringPrincipal() = mTriggeringPrincipal;
655
loadState.PrincipalToInherit() = mPrincipalToInherit;
656
loadState.StoragePrincipalToInherit() = mStoragePrincipalToInherit;
657
loadState.Csp() = mCsp;
658
loadState.OriginalURIString() = mOriginalURIString;
659
loadState.CancelContentJSEpoch() = mCancelContentJSEpoch;
660
loadState.ReferrerInfo() = mReferrerInfo;
661
loadState.PostDataStream() = mPostDataStream;
662
loadState.HeadersStream() = mHeadersStream;
663
loadState.SrcdocData() = mSrcdocData;
664
loadState.ResultPrincipalURI() = mResultPrincipalURI;
665
if (!mSHEntry || !StaticPrefs::fission_sessionHistoryInParent()) {
666
// Without the pref, we don't have an actor for shentry and thus
667
// we can't serialize it. We could write custom (de)serializers,
668
// but a session history rewrite is on the way anyway.
669
return loadState;
670
}
671
if (XRE_IsParentProcess()) {
672
loadState.SHEntry() = static_cast<CrossProcessSHEntry*>(
673
static_cast<LegacySHEntry*>(mSHEntry.get()));
674
} else {
675
loadState.SHEntry() = static_cast<CrossProcessSHEntry*>(
676
static_cast<SHEntryChild*>(mSHEntry.get()));
677
}
678
return loadState;
679
}