1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/net/OpaqueResponseUtils.h"
#include "mozilla/dom/Document.h"
#include "mozilla/StaticPrefs_browser.h"
#include "ErrorList.h"
#include "nsContentUtils.h"
#include "nsHttpResponseHead.h"
#include "nsISupports.h"
#include "nsMimeTypes.h"
#include "nsThreadUtils.h"
#include "nsStringStream.h"
#include "HttpBaseChannel.h"
#define LOGORB(msg, ...) \
MOZ_LOG(gORBLog, LogLevel::Debug, \
("%s: %p " msg, __func__, this, ##__VA_ARGS__))
namespace mozilla::net {
static bool IsOpaqueSafeListedMIMEType(const nsACString& aContentType) {
if (aContentType.EqualsLiteral(TEXT_CSS) ||
aContentType.EqualsLiteral(IMAGE_SVG_XML)) {
return true;
}
NS_ConvertUTF8toUTF16 typeString(aContentType);
return nsContentUtils::IsJavascriptMIMEType(typeString);
}
static bool IsOpaqueBlockListedMIMEType(const nsACString& aContentType) {
return aContentType.EqualsLiteral(TEXT_HTML) ||
StringEndsWith(aContentType, "+json"_ns) ||
aContentType.EqualsLiteral(APPLICATION_JSON) ||
aContentType.EqualsLiteral(TEXT_JSON) ||
StringEndsWith(aContentType, "+xml"_ns) ||
aContentType.EqualsLiteral(APPLICATION_XML) ||
aContentType.EqualsLiteral(TEXT_XML);
}
static bool IsOpaqueBlockListedNeverSniffedMIMEType(
const nsACString& aContentType) {
return aContentType.EqualsLiteral(APPLICATION_GZIP2) ||
aContentType.EqualsLiteral(APPLICATION_MSEXCEL) ||
aContentType.EqualsLiteral(APPLICATION_MSPPT) ||
aContentType.EqualsLiteral(APPLICATION_MSWORD) ||
aContentType.EqualsLiteral(APPLICATION_MSWORD_TEMPLATE) ||
aContentType.EqualsLiteral(APPLICATION_PDF) ||
aContentType.EqualsLiteral(APPLICATION_MPEGURL) ||
aContentType.EqualsLiteral(APPLICATION_VND_CES_QUICKPOINT) ||
aContentType.EqualsLiteral(APPLICATION_VND_CES_QUICKSHEET) ||
aContentType.EqualsLiteral(APPLICATION_VND_CES_QUICKWORD) ||
aContentType.EqualsLiteral(APPLICATION_VND_MS_EXCEL) ||
aContentType.EqualsLiteral(APPLICATION_VND_MS_EXCEL2) ||
aContentType.EqualsLiteral(APPLICATION_VND_MS_PPT) ||
aContentType.EqualsLiteral(APPLICATION_VND_MS_PPT2) ||
aContentType.EqualsLiteral(APPLICATION_VND_MS_WORD) ||
aContentType.EqualsLiteral(APPLICATION_VND_MS_WORD2) ||
aContentType.EqualsLiteral(APPLICATION_VND_MS_WORD3) ||
aContentType.EqualsLiteral(APPLICATION_VND_MSWORD) ||
aContentType.EqualsLiteral(
APPLICATION_VND_PRESENTATIONML_PRESENTATION) ||
aContentType.EqualsLiteral(APPLICATION_VND_PRESENTATIONML_TEMPLATE) ||
aContentType.EqualsLiteral(APPLICATION_VND_SPREADSHEETML_SHEET) ||
aContentType.EqualsLiteral(APPLICATION_VND_SPREADSHEETML_TEMPLATE) ||
aContentType.EqualsLiteral(
APPLICATION_VND_WORDPROCESSINGML_DOCUMENT) ||
aContentType.EqualsLiteral(
APPLICATION_VND_WORDPROCESSINGML_TEMPLATE) ||
aContentType.EqualsLiteral(APPLICATION_VND_PRESENTATION_OPENXML) ||
aContentType.EqualsLiteral(APPLICATION_VND_PRESENTATION_OPENXMLM) ||
aContentType.EqualsLiteral(APPLICATION_VND_SPREADSHEET_OPENXML) ||
aContentType.EqualsLiteral(APPLICATION_VND_WORDPROSSING_OPENXML) ||
aContentType.EqualsLiteral(APPLICATION_GZIP) ||
aContentType.EqualsLiteral(APPLICATION_XPROTOBUF) ||
aContentType.EqualsLiteral(APPLICATION_XPROTOBUFFER) ||
aContentType.EqualsLiteral(APPLICATION_ZIP) ||
aContentType.EqualsLiteral(AUDIO_MPEG_URL) ||
aContentType.EqualsLiteral(MULTIPART_BYTERANGES) ||
aContentType.EqualsLiteral(MULTIPART_SIGNED) ||
aContentType.EqualsLiteral(TEXT_EVENT_STREAM) ||
aContentType.EqualsLiteral(TEXT_CSV) ||
aContentType.EqualsLiteral(TEXT_VTT);
}
OpaqueResponseBlockedReason GetOpaqueResponseBlockedReason(
const nsACString& aContentType, uint16_t aStatus, bool aNoSniff) {
if (aContentType.IsEmpty()) {
return OpaqueResponseBlockedReason::BLOCKED_SHOULD_SNIFF;
}
if (IsOpaqueSafeListedMIMEType(aContentType)) {
return OpaqueResponseBlockedReason::ALLOWED_SAFE_LISTED;
}
if (IsOpaqueBlockListedNeverSniffedMIMEType(aContentType)) {
return OpaqueResponseBlockedReason::BLOCKED_BLOCKLISTED_NEVER_SNIFFED;
}
if (aStatus == 206 && IsOpaqueBlockListedMIMEType(aContentType)) {
return OpaqueResponseBlockedReason::BLOCKED_206_AND_BLOCKLISTED;
}
nsAutoCString contentTypeOptionsHeader;
if (aNoSniff && (IsOpaqueBlockListedMIMEType(aContentType) ||
aContentType.EqualsLiteral(TEXT_PLAIN))) {
return OpaqueResponseBlockedReason::
BLOCKED_NOSNIFF_AND_EITHER_BLOCKLISTED_OR_TEXTPLAIN;
}
return OpaqueResponseBlockedReason::BLOCKED_SHOULD_SNIFF;
}
OpaqueResponseBlockedReason GetOpaqueResponseBlockedReason(
const nsHttpResponseHead& aResponseHead) {
nsAutoCString contentType;
aResponseHead.ContentType(contentType);
nsAutoCString contentTypeOptionsHeader;
bool nosniff =
aResponseHead.GetContentTypeOptionsHeader(contentTypeOptionsHeader) &&
contentTypeOptionsHeader.EqualsIgnoreCase("nosniff");
return GetOpaqueResponseBlockedReason(contentType, aResponseHead.Status(),
nosniff);
}
Result<std::tuple<int64_t, int64_t, int64_t>, nsresult>
ParseContentRangeHeaderString(const nsAutoCString& aRangeStr) {
// Parse the range header: e.g. Content-Range: bytes 7000-7999/8000.
const int32_t spacePos = aRangeStr.Find(" "_ns);
const int32_t dashPos = aRangeStr.Find("-"_ns, spacePos);
const int32_t slashPos = aRangeStr.Find("/"_ns, dashPos);
nsAutoCString rangeStartText;
aRangeStr.Mid(rangeStartText, spacePos + 1, dashPos - (spacePos + 1));
nsresult rv;
const int64_t rangeStart = rangeStartText.ToInteger64(&rv);
if (NS_FAILED(rv)) {
return Err(rv);
}
if (0 > rangeStart) {
return Err(NS_ERROR_ILLEGAL_VALUE);
}
nsAutoCString rangeEndText;
aRangeStr.Mid(rangeEndText, dashPos + 1, slashPos - (dashPos + 1));
const int64_t rangeEnd = rangeEndText.ToInteger64(&rv);
if (NS_FAILED(rv)) {
return Err(rv);
}
if (rangeStart > rangeEnd) {
return Err(NS_ERROR_ILLEGAL_VALUE);
}
nsAutoCString rangeTotalText;
aRangeStr.Right(rangeTotalText, aRangeStr.Length() - (slashPos + 1));
if (rangeTotalText[0] == '*') {
return std::make_tuple(rangeStart, rangeEnd, (int64_t)-1);
}
const int64_t rangeTotal = rangeTotalText.ToInteger64(&rv);
if (NS_FAILED(rv)) {
return Err(rv);
}
if (rangeEnd >= rangeTotal) {
return Err(NS_ERROR_ILLEGAL_VALUE);
}
return std::make_tuple(rangeStart, rangeEnd, rangeTotal);
}
bool IsFirstPartialResponse(nsHttpResponseHead& aResponseHead) {
MOZ_ASSERT(aResponseHead.Status() == 206);
nsAutoCString contentRange;
Unused << aResponseHead.GetHeader(nsHttp::Content_Range, contentRange);
auto rangeOrErr = ParseContentRangeHeaderString(contentRange);
if (rangeOrErr.isErr()) {
return false;
}
const int64_t responseFirstBytePos = std::get<0>(rangeOrErr.unwrap());
return responseFirstBytePos == 0;
}
void LogORBError(nsILoadInfo* aLoadInfo, nsIURI* aURI) {
RefPtr<dom::Document> doc;
aLoadInfo->GetLoadingDocument(getter_AddRefs(doc));
nsAutoCString uri;
nsresult rv = nsContentUtils::AnonymizeURI(aURI, uri);
if (NS_WARN_IF(NS_FAILED(rv))) {
return;
}
AutoTArray<nsString, 1> params;
CopyUTF8toUTF16(uri, *params.AppendElement());
MOZ_LOG(gORBLog, LogLevel::Debug,
("%s: Resource blocked: %s ", __func__, uri.get()));
nsContentUtils::ReportToConsole(nsIScriptError::warningFlag, "ORB"_ns, doc,
nsContentUtils::eNECKO_PROPERTIES,
"ResourceBlockedCORS", params);
}
OpaqueResponseBlocker::OpaqueResponseBlocker(nsIStreamListener* aNext,
HttpBaseChannel* aChannel,
const nsCString& aContentType,
bool aNoSniff)
: mNext(aNext), mContentType(aContentType), mNoSniff(aNoSniff) {
// Storing aChannel as a member is tricky as aChannel owns us and it's
// hard to ensure aChannel is alive when we about to use it without
// creating a cycle. This is all doable but need some extra efforts.
//
// So we are just passing aChannel from the caller when we need to use it.
MOZ_ASSERT(aChannel);
if (MOZ_UNLIKELY(MOZ_LOG_TEST(gORBLog, LogLevel::Debug))) {
nsCOMPtr<nsIURI> uri;
aChannel->GetURI(getter_AddRefs(uri));
if (uri) {
LOGORB(" channel=%p, uri=%s", aChannel, uri->GetSpecOrDefault().get());
}
}
MOZ_DIAGNOSTIC_ASSERT(XRE_IsParentProcess());
MOZ_DIAGNOSTIC_ASSERT(aChannel->CachedOpaqueResponseBlockingPref());
}
NS_IMETHODIMP
OpaqueResponseBlocker::OnStartRequest(nsIRequest* aRequest) {
LOGORB();
if (mState == State::Sniffing) {
Unused << EnsureOpaqueResponseIsAllowedAfterSniff(aRequest);
}
// mState will remain State::Sniffing if we need to wait
// for JS validator to make a decision.
//
// When the state is Sniffing, we can't call mNext->OnStartRequest
// because fetch requests need the cancellation to be done
// before its FetchDriver::OnStartRequest is called, otherwise it'll
// resolve the promise regardless the decision of JS validator.
if (mState != State::Sniffing) {
nsresult rv = mNext->OnStartRequest(aRequest);
return NS_SUCCEEDED(mStatus) ? rv : mStatus;
}
return NS_OK;
}
NS_IMETHODIMP
OpaqueResponseBlocker::OnStopRequest(nsIRequest* aRequest,
nsresult aStatusCode) {
LOGORB();
nsresult statusForStop = aStatusCode;
if (mState == State::Blocked && NS_FAILED(mStatus)) {
statusForStop = mStatus;
}
if (mState == State::Sniffing) {
MOZ_ASSERT(mJSValidator);
mPendingOnStopRequestStatus = Some(aStatusCode);
mJSValidator->OnStopRequest(aStatusCode);
return NS_OK;
}
return mNext->OnStopRequest(aRequest, statusForStop);
}
NS_IMETHODIMP
OpaqueResponseBlocker::OnDataAvailable(nsIRequest* aRequest,
nsIInputStream* aInputStream,
uint64_t aOffset, uint32_t aCount) {
LOGORB();
if (mState == State::Allowed) {
return mNext->OnDataAvailable(aRequest, aInputStream, aOffset, aCount);
}
if (mState == State::Blocked) {
return NS_ERROR_FAILURE;
}
MOZ_ASSERT(mState == State::Sniffing);
nsCString data;
if (!data.SetLength(aCount, fallible)) {
return NS_ERROR_OUT_OF_MEMORY;
}
uint32_t read;
nsresult rv = aInputStream->Read(data.BeginWriting(), aCount, &read);
if (NS_FAILED(rv)) {
return rv;
}
MOZ_ASSERT(mJSValidator);
mJSValidator->OnDataAvailable(data);
return NS_OK;
}
nsresult OpaqueResponseBlocker::EnsureOpaqueResponseIsAllowedAfterSniff(
nsIRequest* aRequest) {
nsCOMPtr<HttpBaseChannel> httpBaseChannel = do_QueryInterface(aRequest);
MOZ_ASSERT(httpBaseChannel);
// The `AfterSniff` check shouldn't be run when
// 1. We have made a decision already
// 2. The JS validator is running, so we should wait
// for its result.
if (mState != State::Sniffing || mJSValidator) {
return NS_OK;
}
nsCOMPtr<nsILoadInfo> loadInfo;
nsresult rv =
httpBaseChannel->GetLoadInfo(getter_AddRefs<nsILoadInfo>(loadInfo));
if (NS_FAILED(rv)) {
LOGORB("Failed to get LoadInfo");
BlockResponse(httpBaseChannel, rv);
return rv;
}
nsCOMPtr<nsIURI> uri;
rv = httpBaseChannel->GetURI(getter_AddRefs<nsIURI>(uri));
if (NS_FAILED(rv)) {
LOGORB("Failed to get uri");
BlockResponse(httpBaseChannel, rv);
return rv;
}
switch (httpBaseChannel->PerformOpaqueResponseSafelistCheckAfterSniff(
mContentType, mNoSniff)) {
case OpaqueResponse::Block:
BlockResponse(httpBaseChannel, NS_ERROR_FAILURE);
return NS_ERROR_FAILURE;
case OpaqueResponse::Alllow:
AllowResponse();
return NS_OK;
case OpaqueResponse::Sniff:
case OpaqueResponse::SniffCompressed:
break;
}
MOZ_ASSERT(mState == State::Sniffing);
return ValidateJavaScript(httpBaseChannel, uri, loadInfo);
}
// The specification for ORB is currently being written:
// https://whatpr.org/fetch/1442.html#orb-algorithm
// The `opaque-response-safelist check` is implemented in:
// * `HttpBaseChannel::OpaqueResponseSafelistCheckBeforeSniff`
// * `nsHttpChannel::DisableIsOpaqueResponseAllowedAfterSniffCheck`
// * `HttpBaseChannel::OpaqueResponseSafelistCheckAfterSniff`
// * `OpaqueResponseBlocker::ValidateJavaScript`
nsresult OpaqueResponseBlocker::ValidateJavaScript(HttpBaseChannel* aChannel,
nsIURI* aURI,
nsILoadInfo* aLoadInfo) {
MOZ_DIAGNOSTIC_ASSERT(aChannel);
MOZ_ASSERT(aURI && aLoadInfo);
if (!StaticPrefs::browser_opaqueResponseBlocking_javascriptValidator()) {
LOGORB("Allowed: JS Validator is disabled");
AllowResponse();
return NS_OK;
}
int64_t contentLength;
nsresult rv = aChannel->GetContentLength(&contentLength);
if (NS_FAILED(rv)) {
LOGORB("Blocked: No Content Length");
BlockResponse(aChannel, rv);
return rv;
}
LOGORB("Send %s to the validator", aURI->GetSpecOrDefault().get());
// https://whatpr.org/fetch/1442.html#orb-algorithm, step 15
mJSValidator = dom::JSValidatorParent::Create();
mJSValidator->IsOpaqueResponseAllowed(
[self = RefPtr{this}, channel = nsCOMPtr{aChannel}, uri = nsCOMPtr{aURI},
loadInfo = nsCOMPtr{aLoadInfo}](bool aAllowed,
Maybe<ipc::Shmem> aSharedData) {
MOZ_LOG(gORBLog, LogLevel::Debug,
("JSValidator resolved for %s with %s",
uri->GetSpecOrDefault().get(),
aSharedData.isSome() ? "true" : "false"));
if (aAllowed) {
self->AllowResponse();
} else {
self->BlockResponse(channel, NS_ERROR_FAILURE);
LogORBError(loadInfo, uri);
}
self->ResolveAndProcessData(channel, aAllowed, aSharedData);
if (aSharedData.isSome()) {
self->mJSValidator->DeallocShmem(aSharedData.ref());
}
Unused << dom::PJSValidatorParent::Send__delete__(self->mJSValidator);
self->mJSValidator = nullptr;
});
return NS_OK;
}
bool OpaqueResponseBlocker::IsSniffing() const {
return mState == State::Sniffing;
}
void OpaqueResponseBlocker::AllowResponse() {
LOGORB("Sniffer is done, allow response, this=%p", this);
MOZ_ASSERT(mState == State::Sniffing);
mState = State::Allowed;
}
void OpaqueResponseBlocker::BlockResponse(HttpBaseChannel* aChannel,
nsresult aReason) {
LOGORB("Sniffer is done, block response, this=%p", this);
MOZ_ASSERT(mState == State::Sniffing);
mState = State::Blocked;
mStatus = aReason;
aChannel->SetChannelBlockedByOpaqueResponse();
aChannel->CancelWithReason(mStatus,
"OpaqueResponseBlocker::BlockResponse"_ns);
}
void OpaqueResponseBlocker::ResolveAndProcessData(
HttpBaseChannel* aChannel, bool aAllowed, Maybe<ipc::Shmem>& aSharedData) {
nsresult rv = OnStartRequest(aChannel);
if (!aAllowed || NS_FAILED(rv)) {
MOZ_ASSERT_IF(!aAllowed, mState == State::Blocked);
MaybeRunOnStopRequest(aChannel);
return;
}
MOZ_ASSERT(mState == State::Allowed);
if (aSharedData.isNothing()) {
MaybeRunOnStopRequest(aChannel);
return;
}
const ipc::Shmem& mem = aSharedData.ref();
nsCOMPtr<nsIInputStream> input;
rv = NS_NewByteInputStream(getter_AddRefs(input),
Span(mem.get<char>(), mem.Size<char>()),
NS_ASSIGNMENT_DEPEND);
if (NS_WARN_IF(NS_FAILED(rv))) {
BlockResponse(aChannel, rv);
MaybeRunOnStopRequest(aChannel);
return;
}
// When this line reaches, the state is either State::Allowed or
// State::Blocked. The OnDataAvailable call will either call
// the next listener or reject the request.
OnDataAvailable(aChannel, input, 0, mem.Size<char>());
MaybeRunOnStopRequest(aChannel);
}
void OpaqueResponseBlocker::MaybeRunOnStopRequest(HttpBaseChannel* aChannel) {
MOZ_ASSERT(mState != State::Sniffing);
if (mPendingOnStopRequestStatus.isSome()) {
OnStopRequest(aChannel, mPendingOnStopRequestStatus.value());
}
}
NS_IMPL_ISUPPORTS(OpaqueResponseBlocker, nsIStreamListener, nsIRequestObserver)
} // namespace mozilla::net
|