Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/caps/NullPrincipal.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set sw=2 sts=2 ts=2 et 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
/**
8
 * This is the principal that has no rights and can't be accessed by
9
 * anything other than itself and chrome; null principals are not
10
 * same-origin with anything but themselves.
11
 */
12
13
#include "mozilla/ArrayUtils.h"
14
15
#include "nsDocShell.h"
16
#include "NullPrincipal.h"
17
#include "NullPrincipalURI.h"
18
#include "nsMemory.h"
19
#include "nsIClassInfoImpl.h"
20
#include "nsNetCID.h"
21
#include "nsError.h"
22
#include "nsIScriptSecurityManager.h"
23
#include "ContentPrincipal.h"
24
#include "nsScriptSecurityManager.h"
25
#include "pratom.h"
26
27
using namespace mozilla;
28
29
NS_IMPL_CLASSINFO(NullPrincipal, nullptr, nsIClassInfo::MAIN_THREAD_ONLY,
30
                  NS_NULLPRINCIPAL_CID)
31
NS_IMPL_QUERY_INTERFACE_CI(NullPrincipal,
32
                           nsIPrincipal,
33
                           nsISerializable)
34
NS_IMPL_CI_INTERFACE_GETTER(NullPrincipal,
35
                            nsIPrincipal,
36
                            nsISerializable)
37
38
/* static */ already_AddRefed<NullPrincipal>
39
NullPrincipal::CreateWithInheritedAttributes(nsIPrincipal* aInheritFrom)
40
0
{
41
0
  RefPtr<NullPrincipal> nullPrin = new NullPrincipal();
42
0
  nsresult rv = nullPrin->Init(Cast(aInheritFrom)->OriginAttributesRef());
43
0
  MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv));
44
0
  return nullPrin.forget();
45
0
}
46
47
/* static */ already_AddRefed<NullPrincipal>
48
NullPrincipal::CreateWithInheritedAttributes(nsIDocShell* aDocShell, bool aIsFirstParty)
49
0
{
50
0
  OriginAttributes attrs = nsDocShell::Cast(aDocShell)->GetOriginAttributes();
51
0
52
0
  RefPtr<NullPrincipal> nullPrin = new NullPrincipal();
53
0
  nsresult rv = nullPrin->Init(attrs, aIsFirstParty);
54
0
  MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv));
55
0
  return nullPrin.forget();
56
0
}
57
58
/* static */ already_AddRefed<NullPrincipal>
59
NullPrincipal::Create(const OriginAttributes& aOriginAttributes, nsIURI* aURI)
60
9
{
61
9
  RefPtr<NullPrincipal> nullPrin = new NullPrincipal();
62
9
  nsresult rv = nullPrin->Init(aOriginAttributes, aURI);
63
9
  MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv));
64
9
65
9
  return nullPrin.forget();
66
9
}
67
68
/* static */ already_AddRefed<NullPrincipal>
69
NullPrincipal::CreateWithoutOriginAttributes()
70
9
{
71
9
  return NullPrincipal::Create(OriginAttributes(), nullptr);
72
9
}
73
74
nsresult
75
NullPrincipal::Init(const OriginAttributes& aOriginAttributes, nsIURI* aURI)
76
9
{
77
9
  if (aURI) {
78
0
    nsAutoCString scheme;
79
0
    nsresult rv = aURI->GetScheme(scheme);
80
0
    NS_ENSURE_SUCCESS(rv, rv);
81
0
82
0
    NS_ENSURE_TRUE(scheme.EqualsLiteral(NS_NULLPRINCIPAL_SCHEME),
83
0
                   NS_ERROR_NOT_AVAILABLE);
84
0
85
0
    mURI = aURI;
86
9
  } else {
87
9
    mURI = NullPrincipalURI::Create();
88
9
    NS_ENSURE_TRUE(mURI, NS_ERROR_NOT_AVAILABLE);
89
9
  }
90
9
91
9
  nsAutoCString originNoSuffix;
92
9
  DebugOnly<nsresult> rv = mURI->GetSpec(originNoSuffix);
93
9
  MOZ_ASSERT(NS_SUCCEEDED(rv));
94
9
95
9
  FinishInit(originNoSuffix, aOriginAttributes);
96
9
97
9
  return NS_OK;
98
9
}
99
100
nsresult
101
NullPrincipal::Init(const OriginAttributes& aOriginAttributes, bool aIsFirstParty)
102
0
{
103
0
  mURI = NullPrincipalURI::Create();
104
0
  NS_ENSURE_TRUE(mURI, NS_ERROR_NOT_AVAILABLE);
105
0
106
0
  nsAutoCString originNoSuffix;
107
0
  DebugOnly<nsresult> rv = mURI->GetSpec(originNoSuffix);
108
0
  MOZ_ASSERT(NS_SUCCEEDED(rv));
109
0
110
0
  nsAutoCString path;
111
0
  rv = mURI->GetPathQueryRef(path);
112
0
  MOZ_ASSERT(NS_SUCCEEDED(rv));
113
0
114
0
  OriginAttributes attrs(aOriginAttributes);
115
0
  if (aIsFirstParty) {
116
0
    // remove the '{}' characters from both ends.
117
0
    path.Mid(path, 1, path.Length() - 2);
118
0
    path.AppendLiteral(".mozilla");
119
0
    attrs.SetFirstPartyDomain(true, path);
120
0
  }
121
0
122
0
  FinishInit(originNoSuffix, attrs);
123
0
124
0
  return NS_OK;
125
0
}
126
127
nsresult
128
NullPrincipal::GetScriptLocation(nsACString &aStr)
129
0
{
130
0
  return mURI->GetSpec(aStr);
131
0
}
132
133
/**
134
 * nsIPrincipal implementation
135
 */
136
137
NS_IMETHODIMP
138
NullPrincipal::GetHashValue(uint32_t *aResult)
139
0
{
140
0
  *aResult = (NS_PTR_TO_INT32(this) >> 2);
141
0
  return NS_OK;
142
0
}
143
144
NS_IMETHODIMP
145
NullPrincipal::SetCsp(nsIContentSecurityPolicy* aCsp)
146
0
{
147
0
  // Never destroy an existing CSP on the principal.
148
0
  // This method should only be called in rare cases.
149
0
150
0
  MOZ_ASSERT(!mCSP, "do not destroy an existing CSP");
151
0
  if (mCSP) {
152
0
    return NS_ERROR_ALREADY_INITIALIZED;
153
0
  }
154
0
155
0
  mCSP = aCsp;
156
0
  return NS_OK;
157
0
}
158
159
NS_IMETHODIMP
160
NullPrincipal::GetURI(nsIURI** aURI)
161
6
{
162
6
  nsCOMPtr<nsIURI> uri = mURI;
163
6
  uri.forget(aURI);
164
6
  return NS_OK;
165
6
}
166
167
NS_IMETHODIMP
168
NullPrincipal::GetDomain(nsIURI** aDomain)
169
0
{
170
0
  nsCOMPtr<nsIURI> uri = mURI;
171
0
  uri.forget(aDomain);
172
0
  return NS_OK;
173
0
}
174
175
NS_IMETHODIMP
176
NullPrincipal::SetDomain(nsIURI* aDomain)
177
0
{
178
0
  // I think the right thing to do here is to just throw...  Silently failing
179
0
  // seems counterproductive.
180
0
  return NS_ERROR_NOT_AVAILABLE;
181
0
}
182
183
bool
184
NullPrincipal::MayLoadInternal(nsIURI* aURI)
185
0
{
186
0
  // Also allow the load if we are the principal of the URI being checked.
187
0
  nsCOMPtr<nsIPrincipal> blobPrincipal;
188
0
  if (dom::BlobURLProtocolHandler::GetBlobURLPrincipal(aURI,
189
0
                                                       getter_AddRefs(blobPrincipal))) {
190
0
    return blobPrincipal == this;
191
0
  }
192
0
193
0
  return false;
194
0
}
195
196
NS_IMETHODIMP
197
NullPrincipal::GetBaseDomain(nsACString& aBaseDomain)
198
0
{
199
0
  // For a null principal, we use our unique uuid as the base domain.
200
0
  return mURI->GetPathQueryRef(aBaseDomain);
201
0
}
202
203
NS_IMETHODIMP
204
NullPrincipal::GetAddonId(nsAString& aAddonId)
205
0
{
206
0
  aAddonId.Truncate();
207
0
  return NS_OK;
208
0
};
209
210
/**
211
 * nsISerializable implementation
212
 */
213
NS_IMETHODIMP
214
NullPrincipal::Read(nsIObjectInputStream* aStream)
215
0
{
216
0
  // Note - NullPrincipal use NS_GENERIC_FACTORY_CONSTRUCTOR_INIT, which means
217
0
  // that the Init() method has already been invoked by the time we deserialize.
218
0
  // This is in contrast to ContentPrincipal, which uses NS_GENERIC_FACTORY_CONSTRUCTOR,
219
0
  // in which case ::Read needs to invoke Init().
220
0
221
0
  nsAutoCString spec;
222
0
  nsresult rv = aStream->ReadCString(spec);
223
0
  NS_ENSURE_SUCCESS(rv, rv);
224
0
225
0
  nsCOMPtr<nsIURI> uri;
226
0
  rv = NS_NewURI(getter_AddRefs(uri), spec);
227
0
  NS_ENSURE_SUCCESS(rv, rv);
228
0
229
0
  nsAutoCString suffix;
230
0
  rv = aStream->ReadCString(suffix);
231
0
  NS_ENSURE_SUCCESS(rv, rv);
232
0
233
0
  OriginAttributes attrs;
234
0
  bool ok = attrs.PopulateFromSuffix(suffix);
235
0
  NS_ENSURE_TRUE(ok, NS_ERROR_FAILURE);
236
0
237
0
  return Init(attrs, uri);
238
0
}
239
240
NS_IMETHODIMP
241
NullPrincipal::Write(nsIObjectOutputStream* aStream)
242
0
{
243
0
  NS_ENSURE_STATE(mURI);
244
0
245
0
  nsAutoCString spec;
246
0
  nsresult rv = mURI->GetSpec(spec);
247
0
  NS_ENSURE_SUCCESS(rv, rv);
248
0
249
0
  rv = aStream->WriteStringZ(spec.get());
250
0
  NS_ENSURE_SUCCESS(rv, rv);
251
0
252
0
  nsAutoCString suffix;
253
0
  OriginAttributesRef().CreateSuffix(suffix);
254
0
255
0
  rv = aStream->WriteStringZ(suffix.get());
256
0
  NS_ENSURE_SUCCESS(rv, rv);
257
0
258
0
  return NS_OK;
259
0
}