2018-01-24 20:47:16 +03:00
|
|
|
/* -*- 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 "ServiceWorkerUtils.h"
|
|
|
|
|
|
|
|
#include "mozilla/Preferences.h"
|
2018-07-10 02:02:39 +03:00
|
|
|
#include "mozilla/dom/ClientInfo.h"
|
|
|
|
#include "mozilla/dom/ServiceWorkerRegistrarTypes.h"
|
2020-05-12 22:02:05 +03:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIPrincipal.h"
|
2018-07-10 02:02:39 +03:00
|
|
|
#include "nsIURL.h"
|
2020-09-25 11:57:23 +03:00
|
|
|
#include "nsPrintfCString.h"
|
2018-01-24 20:47:16 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
bool ServiceWorkerParentInterceptEnabled() {
|
2018-06-04 19:10:49 +03:00
|
|
|
static Atomic<bool> sEnabled;
|
2018-06-19 18:26:25 +03:00
|
|
|
static Atomic<bool> sInitialized;
|
|
|
|
if (!sInitialized) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
sInitialized = true;
|
|
|
|
sEnabled =
|
|
|
|
Preferences::GetBool("dom.serviceWorkers.parent_intercept", false);
|
2018-01-24 20:47:16 +03:00
|
|
|
}
|
|
|
|
return sEnabled;
|
|
|
|
}
|
|
|
|
|
2018-04-09 20:54:23 +03:00
|
|
|
bool ServiceWorkerRegistrationDataIsValid(
|
|
|
|
const ServiceWorkerRegistrationData& aData) {
|
|
|
|
return !aData.scope().IsEmpty() && !aData.currentWorkerURL().IsEmpty() &&
|
|
|
|
!aData.cacheName().IsEmpty();
|
|
|
|
}
|
|
|
|
|
2018-04-23 19:46:55 +03:00
|
|
|
namespace {
|
|
|
|
|
2020-02-06 22:56:31 +03:00
|
|
|
void CheckForSlashEscapedCharsInPath(nsIURI* aURI, const char* aURLDescription,
|
|
|
|
ErrorResult& aRv) {
|
2018-04-23 19:46:55 +03:00
|
|
|
MOZ_ASSERT(aURI);
|
|
|
|
|
|
|
|
// A URL that can't be downcast to a standard URL is an invalid URL and should
|
|
|
|
// be treated as such and fail with SecurityError.
|
|
|
|
nsCOMPtr<nsIURL> url(do_QueryInterface(aURI));
|
|
|
|
if (NS_WARN_IF(!url)) {
|
2020-02-06 22:56:31 +03:00
|
|
|
// This really should not happen, since the caller checks that we
|
|
|
|
// have an http: or https: URL!
|
|
|
|
aRv.ThrowInvalidStateError("http: or https: URL without a concept of path");
|
|
|
|
return;
|
2018-04-23 19:46:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString path;
|
|
|
|
nsresult rv = url->GetFilePath(path);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2020-02-06 22:56:31 +03:00
|
|
|
// Again, should not happen.
|
|
|
|
aRv.ThrowInvalidStateError("http: or https: URL without a concept of path");
|
|
|
|
return;
|
2018-04-23 19:46:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ToLowerCase(path);
|
|
|
|
if (path.Find("%2f") != kNotFound || path.Find("%5c") != kNotFound) {
|
2020-02-06 22:56:31 +03:00
|
|
|
nsPrintfCString err("%s contains %%2f or %%5c", aURLDescription);
|
2020-03-07 00:08:02 +03:00
|
|
|
aRv.ThrowTypeError(err);
|
2018-04-23 19:46:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2020-02-06 22:56:31 +03:00
|
|
|
void ServiceWorkerScopeAndScriptAreValid(const ClientInfo& aClientInfo,
|
|
|
|
nsIURI* aScopeURI, nsIURI* aScriptURI,
|
|
|
|
ErrorResult& aRv) {
|
2018-04-23 19:46:55 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aScopeURI);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aScriptURI);
|
|
|
|
|
2020-05-12 22:02:05 +03:00
|
|
|
auto principalOrErr = aClientInfo.GetPrincipal();
|
|
|
|
if (NS_WARN_IF(principalOrErr.isErr())) {
|
2020-02-06 22:56:31 +03:00
|
|
|
aRv.ThrowInvalidStateError("Can't make security decisions about Client");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-03 13:14:24 +03:00
|
|
|
auto hasHTTPScheme = [](nsIURI* aURI) -> bool {
|
|
|
|
return aURI->SchemeIs("http") || aURI->SchemeIs("https");
|
|
|
|
};
|
|
|
|
auto hasMozExtScheme = [](nsIURI* aURI) -> bool {
|
|
|
|
return aURI->SchemeIs("moz-extension");
|
|
|
|
};
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> principal = principalOrErr.unwrap();
|
|
|
|
|
|
|
|
auto isExtension = !!BasePrincipal::Cast(principal)->AddonPolicy();
|
|
|
|
auto hasValidURISchemes = !isExtension ? hasHTTPScheme : hasMozExtScheme;
|
|
|
|
|
2020-02-06 22:56:31 +03:00
|
|
|
// https://w3c.github.io/ServiceWorker/#start-register-algorithm step 3.
|
2020-07-03 13:14:24 +03:00
|
|
|
if (!hasValidURISchemes(aScriptURI)) {
|
|
|
|
auto message = !isExtension
|
|
|
|
? "Script URL's scheme is not 'http' or 'https'"_ns
|
|
|
|
: "Script URL's scheme is not 'moz-extension'"_ns;
|
|
|
|
aRv.ThrowTypeError(message);
|
2020-02-06 22:56:31 +03:00
|
|
|
return;
|
|
|
|
}
|
2018-04-23 19:46:55 +03:00
|
|
|
|
2020-02-06 22:56:31 +03:00
|
|
|
// https://w3c.github.io/ServiceWorker/#start-register-algorithm step 4.
|
|
|
|
CheckForSlashEscapedCharsInPath(aScriptURI, "script URL", aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
2018-04-23 19:46:55 +03:00
|
|
|
|
2020-02-06 22:56:31 +03:00
|
|
|
// https://w3c.github.io/ServiceWorker/#start-register-algorithm step 8.
|
2020-07-03 13:14:24 +03:00
|
|
|
if (!hasValidURISchemes(aScopeURI)) {
|
|
|
|
auto message = !isExtension
|
|
|
|
? "Scope URL's scheme is not 'http' or 'https'"_ns
|
|
|
|
: "Scope URL's scheme is not 'moz-extension'"_ns;
|
|
|
|
aRv.ThrowTypeError(message);
|
2020-02-06 22:56:31 +03:00
|
|
|
return;
|
|
|
|
}
|
2018-04-23 19:46:55 +03:00
|
|
|
|
2020-02-06 22:56:31 +03:00
|
|
|
// https://w3c.github.io/ServiceWorker/#start-register-algorithm step 9.
|
|
|
|
CheckForSlashEscapedCharsInPath(aScopeURI, "scope URL", aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
2018-04-23 19:46:55 +03:00
|
|
|
|
2020-02-06 22:56:31 +03:00
|
|
|
// The refs should really be empty coming in here, but if someone
|
|
|
|
// injects bad data into IPC, who knows. So let's revalidate that.
|
2018-04-23 19:46:55 +03:00
|
|
|
nsAutoCString ref;
|
|
|
|
Unused << aScopeURI->GetRef(ref);
|
2020-02-06 22:56:31 +03:00
|
|
|
if (NS_WARN_IF(!ref.IsEmpty())) {
|
|
|
|
aRv.ThrowSecurityError("Non-empty fragment on scope URL");
|
|
|
|
return;
|
|
|
|
}
|
2018-04-23 19:46:55 +03:00
|
|
|
|
|
|
|
Unused << aScriptURI->GetRef(ref);
|
2020-02-06 22:56:31 +03:00
|
|
|
if (NS_WARN_IF(!ref.IsEmpty())) {
|
|
|
|
aRv.ThrowSecurityError("Non-empty fragment on script URL");
|
|
|
|
return;
|
|
|
|
}
|
2018-04-23 19:46:55 +03:00
|
|
|
|
2019-12-13 09:24:12 +03:00
|
|
|
// Unfortunately we don't seem to have an obvious window id here; in
|
|
|
|
// particular ClientInfo does not have one.
|
2020-02-06 22:56:31 +03:00
|
|
|
nsresult rv = principal->CheckMayLoadWithReporting(
|
2019-12-13 09:24:12 +03:00
|
|
|
aScopeURI, false /* allowIfInheritsPrincipal */, 0 /* innerWindowID */);
|
2020-02-06 22:56:31 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
aRv.ThrowSecurityError("Scope URL is not same-origin with Client");
|
|
|
|
return;
|
|
|
|
}
|
2018-04-23 19:46:55 +03:00
|
|
|
|
2019-12-13 09:24:12 +03:00
|
|
|
rv = principal->CheckMayLoadWithReporting(
|
|
|
|
aScriptURI, false /* allowIfInheritsPrincipal */, 0 /* innerWindowID */);
|
2020-02-06 22:56:31 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
aRv.ThrowSecurityError("Script URL is not same-origin with Client");
|
|
|
|
return;
|
|
|
|
}
|
2018-04-23 19:46:55 +03:00
|
|
|
}
|
|
|
|
|
2018-01-24 20:47:16 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|