tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

ScriptResponseHeaderProcessor.cpp (3173B)


      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 "ScriptResponseHeaderProcessor.h"
      8 
      9 #include "mozilla/StaticPrefs_javascript.h"
     10 #include "mozilla/Try.h"
     11 #include "mozilla/dom/WorkerRef.h"
     12 #include "mozilla/dom/WorkerScope.h"
     13 
     14 namespace mozilla {
     15 namespace dom {
     16 
     17 namespace workerinternals {
     18 
     19 namespace loader {
     20 
     21 NS_IMPL_ISUPPORTS(ScriptResponseHeaderProcessor, nsIRequestObserver);
     22 
     23 nsresult ScriptResponseHeaderProcessor::ProcessCrossOriginEmbedderPolicyHeader(
     24    WorkerPrivate* aWorkerPrivate,
     25    nsILoadInfo::CrossOriginEmbedderPolicy aPolicy, bool aIsMainScript) {
     26  MOZ_ASSERT(aWorkerPrivate);
     27 
     28  if (aIsMainScript) {
     29    MOZ_TRY(aWorkerPrivate->SetEmbedderPolicy(aPolicy));
     30  } else {
     31    // NOTE: Spec doesn't mention non-main scripts must match COEP header with
     32    // the main script, but it must pass CORP checking.
     33    // see: wpt window-simple-success.https.html, the worker import script
     34    // test-incrementer.js without coep header.
     35    (void)NS_WARN_IF(!aWorkerPrivate->MatchEmbedderPolicy(aPolicy));
     36  }
     37 
     38  return NS_OK;
     39 }
     40 
     41 // Enforce strict MIME type checks for worker-imported scripts
     42 // https://github.com/whatwg/html/pull/4001
     43 nsresult ScriptResponseHeaderProcessor::EnsureExpectedModuleType(
     44    nsIRequest* aRequest) {
     45  nsCOMPtr<nsIChannel> channel = do_QueryInterface(aRequest);
     46  MOZ_ASSERT(channel);
     47  nsAutoCString mimeType;
     48  channel->GetContentType(mimeType);
     49  NS_ConvertUTF8toUTF16 typeString(mimeType);
     50 
     51  if (mModuleType == JS::ModuleType::JavaScriptOrWasm) {
     52    if (nsContentUtils::IsJavascriptMIMEType(typeString)) {
     53      return NS_OK;
     54    }
     55 #ifdef NIGHTLY_BUILD
     56    if (StaticPrefs::javascript_options_experimental_wasm_esm_integration()) {
     57      if (nsContentUtils::HasWasmMimeTypeEssence(typeString)) {
     58        return NS_OK;
     59      }
     60    }
     61 #endif
     62  }
     63 
     64  if (mModuleType == JS::ModuleType::JSON &&
     65      nsContentUtils::IsJsonMimeType(typeString)) {
     66    return NS_OK;
     67  }
     68 
     69  return NS_ERROR_DOM_NETWORK_ERR;
     70 }
     71 
     72 nsresult ScriptResponseHeaderProcessor::ProcessCrossOriginEmbedderPolicyHeader(
     73    nsIRequest* aRequest) {
     74  nsCOMPtr<nsIHttpChannelInternal> httpChannel = do_QueryInterface(aRequest);
     75 
     76  MOZ_ASSERT(mWorkerRef);
     77  // NOTE: the spec doesn't say what to do with non-HTTP workers.
     78  // See: https://github.com/whatwg/html/issues/4916
     79  if (!httpChannel) {
     80    if (mIsMainScript) {
     81      mWorkerRef->Private()->InheritOwnerEmbedderPolicyOrNull(aRequest);
     82    }
     83 
     84    return NS_OK;
     85  }
     86 
     87  nsILoadInfo::CrossOriginEmbedderPolicy coep;
     88  MOZ_TRY(httpChannel->GetResponseEmbedderPolicy(
     89      mWorkerRef->Private()->Trials().IsEnabled(
     90          OriginTrial::CoepCredentialless),
     91      &coep));
     92 
     93  return ProcessCrossOriginEmbedderPolicyHeader(mWorkerRef->Private(), coep,
     94                                                mIsMainScript);
     95 }
     96 
     97 }  // namespace loader
     98 }  // namespace workerinternals
     99 
    100 }  // namespace dom
    101 }  // namespace mozilla