Skip to content

Commit 7c81dcc

Browse files
committed
Bug 1911759 - Add triggeringPrincipal override to RequestInit r=necko-reviewers,webidl,smaug,kershaw
This allows code with Chrome priviledges to use the fetch API to specify a triggering principal instead of needing to use XMLHttpRequest. The triggeringPrincipal is only used when the fetch principal is already the systemPrincipal. Differential Revision: https://phabricator.services.mozilla.com/D213418
1 parent b8bb90e commit 7c81dcc

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

dom/fetch/FetchDriver.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,8 +641,14 @@ nsresult FetchDriver::HttpFetch(
641641
nullptr, /* aCallbacks */
642642
loadFlags, ios);
643643
} else {
644+
nsCOMPtr<nsIPrincipal> principal = mPrincipal;
645+
if (principal->IsSystemPrincipal() &&
646+
mRequest->GetTriggeringPrincipalOverride()) {
647+
principal = mRequest->GetTriggeringPrincipalOverride();
648+
}
649+
644650
rv =
645-
NS_NewChannel(getter_AddRefs(chan), uri, mPrincipal, secFlags,
651+
NS_NewChannel(getter_AddRefs(chan), uri, principal, secFlags,
646652
mRequest->ContentPolicyType(), mCookieJarSettings,
647653
mPerformanceStorage, mLoadGroup, nullptr, /* aCallbacks */
648654
loadFlags, ios);

dom/fetch/InternalRequest.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,14 @@ class InternalRequest final : public AtomicSafeRefCounted<InternalRequest> {
262262

263263
void SetMozErrors() { mMozErrors = true; }
264264

265+
void SetTriggeringPrincipal(nsIPrincipal* aPrincipal) {
266+
mTriggeringPrincipalOverride = aPrincipal;
267+
}
268+
269+
nsIPrincipal* GetTriggeringPrincipalOverride() {
270+
return mTriggeringPrincipalOverride;
271+
}
272+
265273
const nsCString& GetFragment() const { return mFragment; }
266274

267275
nsContentPolicyType ContentPolicyType() const { return mContentPolicyType; }
@@ -437,6 +445,8 @@ class InternalRequest final : public AtomicSafeRefCounted<InternalRequest> {
437445
nsCString mBodyBlobURISpec;
438446
nsString mBodyLocalPath;
439447
nsCOMPtr<nsIInputStream> mBodyStream;
448+
449+
nsCOMPtr<nsIPrincipal> mTriggeringPrincipalOverride;
440450
int64_t mBodyLength{InternalResponse::UNKNOWN_BODY_SIZE};
441451

442452
nsCString mPreferredAlternativeDataType;

dom/fetch/Request.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,11 @@ SafeRefPtr<Request> Request::Constructor(
352352
request->SetMozErrors();
353353
}
354354

355+
if (aInit.mTriggeringPrincipal.WasPassed() &&
356+
aInit.mTriggeringPrincipal.Value()) {
357+
request->SetTriggeringPrincipal(aInit.mTriggeringPrincipal.Value());
358+
}
359+
355360
// Request constructor step 14.
356361
if (aInit.mMethod.WasPassed()) {
357362
nsAutoCString method(aInit.mMethod.Value());

dom/webidl/Request.webidl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
* https://fetch.spec.whatwg.org/#request-class
88
*/
99

10+
11+
interface Principal;
12+
1013
typedef (Request or UTF8String) RequestInfo;
1114
typedef unsigned long nsContentPolicyType;
1215

@@ -77,6 +80,9 @@ dictionary RequestInit {
7780
[ChromeOnly]
7881
boolean mozErrors;
7982

83+
[ChromeOnly]
84+
Principal triggeringPrincipal;
85+
8086
AbortSignal? signal;
8187

8288
[Pref="network.fetchpriority.enabled"]

0 commit comments

Comments
 (0)