Line data Source code
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 "ServiceWorkerUtils.h"
8 :
9 : #include "mozilla/Preferences.h"
10 :
11 : namespace mozilla {
12 : namespace dom {
13 :
14 : bool
15 0 : ServiceWorkerParentInterceptEnabled()
16 : {
17 : static bool sInit = false;
18 : static Atomic<bool> sEnabled;
19 :
20 8 : if (!sInit) {
21 1 : MOZ_ASSERT(NS_IsMainThread());
22 : Preferences::AddAtomicBoolVarCache(&sEnabled,
23 : "dom.serviceWorkers.parent_intercept",
24 0 : false);
25 0 : sInit = true;
26 : }
27 :
28 0 : return sEnabled;
29 : }
30 :
31 : bool
32 0 : ServiceWorkerRegistrationDataIsValid(const ServiceWorkerRegistrationData& aData)
33 : {
34 0 : return !aData.scope().IsEmpty() &&
35 0 : !aData.currentWorkerURL().IsEmpty() &&
36 0 : !aData.cacheName().IsEmpty();
37 : }
38 :
39 : namespace {
40 :
41 : nsresult
42 0 : CheckForSlashEscapedCharsInPath(nsIURI* aURI)
43 : {
44 0 : MOZ_ASSERT(aURI);
45 :
46 : // A URL that can't be downcast to a standard URL is an invalid URL and should
47 : // be treated as such and fail with SecurityError.
48 0 : nsCOMPtr<nsIURL> url(do_QueryInterface(aURI));
49 0 : if (NS_WARN_IF(!url)) {
50 : return NS_ERROR_DOM_SECURITY_ERR;
51 : }
52 :
53 0 : nsAutoCString path;
54 0 : nsresult rv = url->GetFilePath(path);
55 0 : if (NS_WARN_IF(NS_FAILED(rv))) {
56 : return rv;
57 : }
58 :
59 0 : ToLowerCase(path);
60 0 : if (path.Find("%2f") != kNotFound ||
61 0 : path.Find("%5c") != kNotFound) {
62 : return NS_ERROR_DOM_TYPE_ERR;
63 : }
64 :
65 0 : return NS_OK;
66 : }
67 :
68 : } // anonymous namespace
69 :
70 : nsresult
71 0 : ServiceWorkerScopeAndScriptAreValid(const ClientInfo& aClientInfo,
72 : nsIURI* aScopeURI,
73 : nsIURI* aScriptURI)
74 : {
75 0 : MOZ_DIAGNOSTIC_ASSERT(aScopeURI);
76 0 : MOZ_DIAGNOSTIC_ASSERT(aScriptURI);
77 :
78 0 : nsCOMPtr<nsIPrincipal> principal = aClientInfo.GetPrincipal();
79 0 : NS_ENSURE_TRUE(principal, NS_ERROR_DOM_INVALID_STATE_ERR);
80 :
81 0 : bool isHttp = false;
82 0 : bool isHttps = false;
83 0 : Unused << aScriptURI->SchemeIs("http", &isHttp);
84 0 : Unused << aScriptURI->SchemeIs("https", &isHttps);
85 0 : NS_ENSURE_TRUE(isHttp || isHttps, NS_ERROR_DOM_SECURITY_ERR);
86 :
87 0 : nsresult rv = CheckForSlashEscapedCharsInPath(aScopeURI);
88 0 : NS_ENSURE_SUCCESS(rv, rv);
89 :
90 0 : rv = CheckForSlashEscapedCharsInPath(aScriptURI);
91 0 : NS_ENSURE_SUCCESS(rv, rv);
92 :
93 0 : nsAutoCString ref;
94 0 : Unused << aScopeURI->GetRef(ref);
95 0 : NS_ENSURE_TRUE(ref.IsEmpty(), NS_ERROR_DOM_SECURITY_ERR);
96 :
97 0 : Unused << aScriptURI->GetRef(ref);
98 0 : NS_ENSURE_TRUE(ref.IsEmpty(), NS_ERROR_DOM_SECURITY_ERR);
99 :
100 0 : rv = principal->CheckMayLoad(aScopeURI, true /* report */,
101 0 : false /* allowIfInheritsPrincipal */);
102 0 : NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SECURITY_ERR);
103 :
104 0 : rv = principal->CheckMayLoad(aScriptURI, true /* report */,
105 0 : false /* allowIfInheritsPrincipal */);
106 0 : NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SECURITY_ERR);
107 :
108 : return NS_OK;
109 : }
110 :
111 : } // namespace dom
112 2 : } // namespace mozilla
|