diff --git a/netwerk/base/nsILoadInfo.idl b/netwerk/base/nsILoadInfo.idl index de4fc00d765b..7fc57124ecfe 100644 --- a/netwerk/base/nsILoadInfo.idl +++ b/netwerk/base/nsILoadInfo.idl @@ -1411,7 +1411,7 @@ interface nsILoadInfo : nsISupports // which is way too generic to distinguish an exact reason. const uint32_t BLOCKING_REASON_CONTENT_POLICY_GENERAL = 4000; const uint32_t BLOCKING_REASON_CONTENT_POLICY_NO_DATA_PROTOCOL = 4001; - const uint32_t BLOCKING_REASON_CONTENT_POLICY_WEBEXT = 4002; + // removed 4002 const uint32_t BLOCKING_REASON_CONTENT_POLICY_CONTENT_BLOCKED = 4003; const uint32_t BLOCKING_REASON_CONTENT_POLICY_DATA_DOCUMENT = 4004; const uint32_t BLOCKING_REASON_CONTENT_POLICY_WEB_BROWSER = 4005; diff --git a/toolkit/components/build/components.conf b/toolkit/components/build/components.conf index 184621359fdf..4f042e8b1979 100644 --- a/toolkit/components/build/components.conf +++ b/toolkit/components/build/components.conf @@ -16,7 +16,6 @@ Classes = [ 'contract_ids': ['@mozilla.org/addons/content-policy;1'], 'type': 'AddonContentPolicy', 'headers': ['mozilla/AddonContentPolicy.h'], - 'categories': {'content-policy': '@mozilla.org/addons/content-policy;1'}, }, { 'cid': '{17a59a6b-92b8-42e5-bce0-ab434c7a7135}', diff --git a/toolkit/mozapps/extensions/AddonContentPolicy.cpp b/toolkit/mozapps/extensions/AddonContentPolicy.cpp index bffe78a7baf2..983935f7c5d2 100644 --- a/toolkit/mozapps/extensions/AddonContentPolicy.cpp +++ b/toolkit/mozapps/extensions/AddonContentPolicy.cpp @@ -9,18 +9,12 @@ #include "mozilla/dom/nsCSPContext.h" #include "nsCOMPtr.h" #include "nsComponentManagerUtils.h" -#include "nsContentPolicyUtils.h" -#include "nsContentTypeParser.h" -#include "nsContentUtils.h" -#include "nsIConsoleService.h" -#include "nsIContentSecurityPolicy.h" #include "nsIContent.h" +#include "mozilla/BasePrincipal.h" #include "mozilla/Components.h" #include "mozilla/dom/Document.h" #include "mozilla/intl/Localization.h" #include "nsIEffectiveTLDService.h" -#include "nsIScriptError.h" -#include "nsIStringBundle.h" #include "nsIUUIDGenerator.h" #include "nsIURI.h" #include "nsNetCID.h" @@ -31,124 +25,17 @@ using namespace mozilla::intl; /* Enforces content policies for WebExtension scopes. Currently: * - * - Prevents loading scripts with a non-default JavaScript version. * - Checks custom content security policies for sufficiently stringent * script-src and other script-related directives. * - We also used to validate object-src similarly to script-src, but that was * dropped because NPAPI plugins are no longer supported (see bug 1766881). */ -#define VERSIONED_JS_BLOCKED_MESSAGE \ - u"Versioned JavaScript is a non-standard, deprecated extension, and is " \ - u"not supported in WebExtension code. For alternatives, please see: " \ - u"https://developer.mozilla.org/Add-ons/WebExtensions/Tips" - AddonContentPolicy::AddonContentPolicy() = default; AddonContentPolicy::~AddonContentPolicy() = default; -NS_IMPL_ISUPPORTS(AddonContentPolicy, nsIContentPolicy, nsIAddonContentPolicy) - -static nsresult GetWindowIDFromContext(nsISupports* aContext, - uint64_t* aResult) { - NS_ENSURE_TRUE(aContext, NS_ERROR_FAILURE); - - nsCOMPtr content = do_QueryInterface(aContext); - NS_ENSURE_TRUE(content, NS_ERROR_FAILURE); - - nsCOMPtr window = content->OwnerDoc()->GetInnerWindow(); - NS_ENSURE_TRUE(window, NS_ERROR_FAILURE); - - *aResult = window->WindowID(); - return NS_OK; -} - -static nsresult LogMessage(const nsAString& aMessage, - const nsAString& aSourceName, - const nsAString& aSourceSample, - nsISupports* aContext) { - nsCOMPtr error = do_CreateInstance(NS_SCRIPTERROR_CONTRACTID); - NS_ENSURE_TRUE(error, NS_ERROR_OUT_OF_MEMORY); - - uint64_t windowID = 0; - GetWindowIDFromContext(aContext, &windowID); - - nsresult rv = error->InitWithSanitizedSource( - aMessage, aSourceName, aSourceSample, 0, 0, nsIScriptError::errorFlag, - "JavaScript", windowID); - NS_ENSURE_SUCCESS(rv, rv); - - nsCOMPtr console = - do_GetService(NS_CONSOLESERVICE_CONTRACTID); - NS_ENSURE_TRUE(console, NS_ERROR_OUT_OF_MEMORY); - - console->LogMessage(error); - return NS_OK; -} - -// Content policy enforcement: - -NS_IMETHODIMP -AddonContentPolicy::ShouldLoad(nsIURI* aContentLocation, nsILoadInfo* aLoadInfo, - const nsACString& aMimeTypeGuess, - int16_t* aShouldLoad) { - if (!aContentLocation || !aLoadInfo) { - NS_SetRequestBlockingReason( - aLoadInfo, nsILoadInfo::BLOCKING_REASON_CONTENT_POLICY_WEBEXT); - *aShouldLoad = REJECT_REQUEST; - return NS_ERROR_FAILURE; - } - - ExtContentPolicyType contentType = aLoadInfo->GetExternalContentPolicyType(); - - *aShouldLoad = nsIContentPolicy::ACCEPT; - nsCOMPtr loadingPrincipal = aLoadInfo->GetLoadingPrincipal(); - if (!loadingPrincipal) { - return NS_OK; - } - - // Only apply this policy to requests from documents loaded from - // moz-extension URLs, or to resources being loaded from moz-extension URLs. - if (!(aContentLocation->SchemeIs("moz-extension") || - loadingPrincipal->SchemeIs("moz-extension"))) { - return NS_OK; - } - - if (contentType == ExtContentPolicy::TYPE_SCRIPT) { - NS_ConvertUTF8toUTF16 typeString(aMimeTypeGuess); - nsContentTypeParser mimeParser(typeString); - - // Reject attempts to load JavaScript scripts with a non-default version. - nsAutoString mimeType, version; - if (NS_SUCCEEDED(mimeParser.GetType(mimeType)) && - nsContentUtils::IsJavascriptMIMEType(mimeType) && - NS_SUCCEEDED(mimeParser.GetParameter("version", version))) { - NS_SetRequestBlockingReason( - aLoadInfo, nsILoadInfo::BLOCKING_REASON_CONTENT_POLICY_WEBEXT); - *aShouldLoad = nsIContentPolicy::REJECT_REQUEST; - - nsCString sourceName; - loadingPrincipal->GetExposableSpec(sourceName); - NS_ConvertUTF8toUTF16 nameString(sourceName); - - nsCOMPtr context = aLoadInfo->GetLoadingContext(); - LogMessage(nsLiteralString(VERSIONED_JS_BLOCKED_MESSAGE), nameString, - typeString, context); - return NS_OK; - } - } - - return NS_OK; -} - -NS_IMETHODIMP -AddonContentPolicy::ShouldProcess(nsIURI* aContentLocation, - nsILoadInfo* aLoadInfo, - const nsACString& aMimeTypeGuess, - int16_t* aShouldProcess) { - *aShouldProcess = nsIContentPolicy::ACCEPT; - return NS_OK; -} +NS_IMPL_ISUPPORTS(AddonContentPolicy, nsIAddonContentPolicy) // CSP Validation: diff --git a/toolkit/mozapps/extensions/AddonContentPolicy.h b/toolkit/mozapps/extensions/AddonContentPolicy.h index db4c29db05e5..d88949037992 100644 --- a/toolkit/mozapps/extensions/AddonContentPolicy.h +++ b/toolkit/mozapps/extensions/AddonContentPolicy.h @@ -4,11 +4,9 @@ * 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 "nsIContentPolicy.h" #include "nsIAddonPolicyService.h" -class AddonContentPolicy : public nsIContentPolicy, - public nsIAddonContentPolicy { +class AddonContentPolicy : public nsIAddonContentPolicy { protected: virtual ~AddonContentPolicy(); @@ -16,6 +14,5 @@ class AddonContentPolicy : public nsIContentPolicy, AddonContentPolicy(); NS_DECL_ISUPPORTS - NS_DECL_NSICONTENTPOLICY NS_DECL_NSIADDONCONTENTPOLICY };