Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/netwerk/protocol/res/ExtensionProtocolHandler.cpp
Line
Count
Source (jump to first uncovered line)
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 file,
5
 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "ExtensionProtocolHandler.h"
8
9
#include "mozilla/ClearOnShutdown.h"
10
#include "mozilla/dom/ContentChild.h"
11
#include "mozilla/ExtensionPolicyService.h"
12
#include "mozilla/FileUtils.h"
13
#include "mozilla/ipc/IPCStreamUtils.h"
14
#include "mozilla/ipc/URIParams.h"
15
#include "mozilla/ipc/URIUtils.h"
16
#include "mozilla/net/NeckoChild.h"
17
#include "mozilla/RefPtr.h"
18
#include "mozilla/ResultExtensions.h"
19
20
#include "FileDescriptor.h"
21
#include "FileDescriptorFile.h"
22
#include "LoadInfo.h"
23
#include "nsContentUtils.h"
24
#include "nsServiceManagerUtils.h"
25
#include "nsDirectoryServiceDefs.h"
26
#include "nsIFile.h"
27
#include "nsIFileChannel.h"
28
#include "nsIFileStreams.h"
29
#include "nsIFileURL.h"
30
#include "nsIJARChannel.h"
31
#include "nsIMIMEService.h"
32
#include "nsIURL.h"
33
#include "nsIChannel.h"
34
#include "nsIInputStreamPump.h"
35
#include "nsIJARURI.h"
36
#include "nsIStreamListener.h"
37
#include "nsIThread.h"
38
#include "nsIInputStream.h"
39
#include "nsIOutputStream.h"
40
#include "nsIStreamConverterService.h"
41
#include "nsNetUtil.h"
42
#include "prio.h"
43
#include "SimpleChannel.h"
44
45
#if defined(XP_WIN)
46
#include "nsILocalFileWin.h"
47
#include "WinUtils.h"
48
#endif
49
50
#define EXTENSION_SCHEME "moz-extension"
51
using mozilla::ipc::FileDescriptor;
52
using OptionalIPCStream = mozilla::ipc::OptionalIPCStream;
53
54
namespace mozilla {
55
56
namespace net {
57
58
using extensions::URLInfo;
59
60
LazyLogModule gExtProtocolLog("ExtProtocol");
61
#undef LOG
62
0
#define LOG(...) MOZ_LOG(gExtProtocolLog, LogLevel::Debug, (__VA_ARGS__))
63
64
StaticRefPtr<ExtensionProtocolHandler> ExtensionProtocolHandler::sSingleton;
65
66
/**
67
 * Helper class used with SimpleChannel to asynchronously obtain an input
68
 * stream or file descriptor from the parent for a remote moz-extension load
69
 * from the child.
70
 */
71
class ExtensionStreamGetter : public RefCounted<ExtensionStreamGetter>
72
{
73
  public:
74
    // To use when getting a remote input stream for a resource
75
    // in an unpacked extension.
76
    ExtensionStreamGetter(nsIURI* aURI, nsILoadInfo* aLoadInfo)
77
      : mURI(aURI)
78
      , mLoadInfo(aLoadInfo)
79
      , mIsJarChannel(false)
80
0
    {
81
0
      MOZ_ASSERT(aURI);
82
0
      MOZ_ASSERT(aLoadInfo);
83
0
84
0
      SetupEventTarget();
85
0
    }
86
87
    // To use when getting an FD for a packed extension JAR file
88
    // in order to load a resource.
89
    ExtensionStreamGetter(nsIURI* aURI, nsILoadInfo* aLoadInfo,
90
                          already_AddRefed<nsIJARChannel>&& aJarChannel,
91
                          nsIFile* aJarFile)
92
      : mURI(aURI)
93
      , mLoadInfo(aLoadInfo)
94
      , mJarChannel(std::move(aJarChannel))
95
      , mJarFile(aJarFile)
96
      , mIsJarChannel(true)
97
0
    {
98
0
      MOZ_ASSERT(aURI);
99
0
      MOZ_ASSERT(aLoadInfo);
100
0
      MOZ_ASSERT(mJarChannel);
101
0
      MOZ_ASSERT(aJarFile);
102
0
103
0
      SetupEventTarget();
104
0
    }
105
106
0
    ~ExtensionStreamGetter() = default;
107
108
    void SetupEventTarget()
109
0
    {
110
0
      mMainThreadEventTarget =
111
0
        nsContentUtils::GetEventTargetByLoadInfo(mLoadInfo, TaskCategory::Other);
112
0
      if (!mMainThreadEventTarget) {
113
0
        mMainThreadEventTarget = GetMainThreadSerialEventTarget();
114
0
      }
115
0
    }
116
117
    // Get an input stream or file descriptor from the parent asynchronously.
118
    Result<Ok, nsresult> GetAsync(nsIStreamListener* aListener,
119
                                  nsIChannel* aChannel);
120
121
    // Handle an input stream being returned from the parent
122
    void OnStream(already_AddRefed<nsIInputStream> aStream);
123
124
    // Handle file descriptor being returned from the parent
125
    void OnFD(const FileDescriptor& aFD);
126
127
    MOZ_DECLARE_REFCOUNTED_TYPENAME(ExtensionStreamGetter)
128
129
  private:
130
    nsCOMPtr<nsIURI> mURI;
131
    nsCOMPtr<nsILoadInfo> mLoadInfo;
132
    nsCOMPtr<nsIJARChannel> mJarChannel;
133
    nsCOMPtr<nsIFile> mJarFile;
134
    nsCOMPtr<nsIStreamListener> mListener;
135
    nsCOMPtr<nsIChannel> mChannel;
136
    nsCOMPtr<nsISerialEventTarget> mMainThreadEventTarget;
137
    bool mIsJarChannel;
138
};
139
140
class ExtensionJARFileOpener final : public nsISupports
141
{
142
public:
143
  ExtensionJARFileOpener(nsIFile* aFile,
144
                         NeckoParent::GetExtensionFDResolver& aResolve) :
145
    mFile(aFile),
146
    mResolve(aResolve)
147
0
  {
148
0
    MOZ_ASSERT(aFile);
149
0
    MOZ_ASSERT(aResolve);
150
0
  }
151
152
  NS_IMETHOD OpenFile()
153
0
  {
154
0
    MOZ_ASSERT(!NS_IsMainThread());
155
0
    AutoFDClose prFileDesc;
156
0
157
#if defined(XP_WIN)
158
    nsresult rv;
159
    nsCOMPtr<nsILocalFileWin> winFile = do_QueryInterface(mFile, &rv);
160
    MOZ_ASSERT(winFile);
161
    if (NS_SUCCEEDED(rv)) {
162
      rv = winFile->OpenNSPRFileDescShareDelete(PR_RDONLY, 0,
163
                                                &prFileDesc.rwget());
164
    }
165
#else
166
    nsresult rv = mFile->OpenNSPRFileDesc(PR_RDONLY, 0, &prFileDesc.rwget());
167
0
#endif /* XP_WIN */
168
0
169
0
    if (NS_SUCCEEDED(rv)) {
170
0
      mFD = FileDescriptor(FileDescriptor::PlatformHandleType(
171
0
                           PR_FileDesc2NativeHandle(prFileDesc)));
172
0
    }
173
0
174
0
    nsCOMPtr<nsIRunnable> event =
175
0
      mozilla::NewRunnableMethod("ExtensionJarFileFDResolver",
176
0
        this, &ExtensionJARFileOpener::SendBackFD);
177
0
178
0
    rv = NS_DispatchToMainThread(event, nsIEventTarget::DISPATCH_NORMAL);
179
0
    NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "NS_DispatchToMainThread");
180
0
    return NS_OK;
181
0
  }
182
183
  NS_IMETHOD SendBackFD()
184
0
  {
185
0
    MOZ_ASSERT(NS_IsMainThread());
186
0
    mResolve(mFD);
187
0
    mResolve = nullptr;
188
0
    return NS_OK;
189
0
  }
190
191
  NS_DECL_THREADSAFE_ISUPPORTS
192
193
private:
194
0
  virtual ~ExtensionJARFileOpener() = default;
195
196
  nsCOMPtr<nsIFile> mFile;
197
  NeckoParent::GetExtensionFDResolver mResolve;
198
  FileDescriptor mFD;
199
};
200
201
NS_IMPL_ISUPPORTS(ExtensionJARFileOpener, nsISupports)
202
203
// The amount of time, in milliseconds, that the file opener thread will remain
204
// allocated after it is used. This value chosen because to match other uses
205
// of LazyIdleThread.
206
0
#define DEFAULT_THREAD_TIMEOUT_MS 30000
207
208
// Request an FD or input stream from the parent.
209
Result<Ok, nsresult>
210
ExtensionStreamGetter::GetAsync(nsIStreamListener* aListener,
211
                                nsIChannel* aChannel)
212
0
{
213
0
  MOZ_ASSERT(IsNeckoChild());
214
0
  MOZ_ASSERT(mMainThreadEventTarget);
215
0
216
0
  mListener = aListener;
217
0
  mChannel = aChannel;
218
0
219
0
  // Serialize the URI to send to parent
220
0
  mozilla::ipc::URIParams uri;
221
0
  SerializeURI(mURI, uri);
222
0
223
0
  RefPtr<ExtensionStreamGetter> self = this;
224
0
  if (mIsJarChannel) {
225
0
    // Request an FD for this moz-extension URI
226
0
    gNeckoChild->SendGetExtensionFD(uri)->Then(
227
0
      mMainThreadEventTarget,
228
0
      __func__,
229
0
      [self] (const FileDescriptor& fd) {
230
0
        self->OnFD(fd);
231
0
      },
232
0
      [self] (const mozilla::ipc::ResponseRejectReason) {
233
0
        self->OnFD(FileDescriptor());
234
0
      }
235
0
    );
236
0
    return Ok();
237
0
  }
238
0
239
0
  // Request an input stream for this moz-extension URI
240
0
  gNeckoChild->SendGetExtensionStream(uri)->Then(
241
0
    mMainThreadEventTarget,
242
0
    __func__,
243
0
    [self] (const RefPtr<nsIInputStream>& stream) {
244
0
      self->OnStream(do_AddRef(stream));
245
0
    },
246
0
    [self] (const mozilla::ipc::ResponseRejectReason) {
247
0
      self->OnStream(nullptr);
248
0
    }
249
0
  );
250
0
  return Ok();
251
0
}
252
253
static void
254
CancelRequest(nsIStreamListener* aListener,
255
              nsIChannel* aChannel,
256
              nsresult aResult)
257
0
{
258
0
  MOZ_ASSERT(aListener);
259
0
  MOZ_ASSERT(aChannel);
260
0
261
0
  aListener->OnStartRequest(aChannel, nullptr);
262
0
  aListener->OnStopRequest(aChannel, nullptr, aResult);
263
0
  aChannel->Cancel(NS_BINDING_ABORTED);
264
0
}
265
266
// Handle an input stream sent from the parent.
267
void
268
ExtensionStreamGetter::OnStream(already_AddRefed<nsIInputStream> aStream)
269
0
{
270
0
  MOZ_ASSERT(IsNeckoChild());
271
0
  MOZ_ASSERT(mListener);
272
0
  MOZ_ASSERT(mMainThreadEventTarget);
273
0
274
0
  nsCOMPtr<nsIInputStream> stream = std::move(aStream);
275
0
276
0
  // We must keep an owning reference to the listener
277
0
  // until we pass it on to AsyncRead.
278
0
  nsCOMPtr<nsIStreamListener> listener = mListener.forget();
279
0
280
0
  MOZ_ASSERT(mChannel);
281
0
282
0
  if (!stream) {
283
0
    // The parent didn't send us back a stream.
284
0
    CancelRequest(listener, mChannel, NS_ERROR_FILE_ACCESS_DENIED);
285
0
    return;
286
0
  }
287
0
288
0
  nsCOMPtr<nsIInputStreamPump> pump;
289
0
  nsresult rv = NS_NewInputStreamPump(getter_AddRefs(pump), stream.forget(),
290
0
                                      0, 0, false, mMainThreadEventTarget);
291
0
  if (NS_FAILED(rv)) {
292
0
    CancelRequest(listener, mChannel, rv);
293
0
    return;
294
0
  }
295
0
296
0
  rv = pump->AsyncRead(listener, nullptr);
297
0
  if (NS_FAILED(rv)) {
298
0
    CancelRequest(listener, mChannel, rv);
299
0
  }
300
0
}
301
302
// Handle an FD sent from the parent.
303
void
304
ExtensionStreamGetter::OnFD(const FileDescriptor& aFD)
305
0
{
306
0
  MOZ_ASSERT(IsNeckoChild());
307
0
  MOZ_ASSERT(mListener);
308
0
  MOZ_ASSERT(mChannel);
309
0
310
0
  if (!aFD.IsValid()) {
311
0
    OnStream(nullptr);
312
0
    return;
313
0
  }
314
0
315
0
  // We must keep an owning reference to the listener
316
0
  // until we pass it on to AsyncOpen2.
317
0
  nsCOMPtr<nsIStreamListener> listener = mListener.forget();
318
0
319
0
  RefPtr<FileDescriptorFile> fdFile = new FileDescriptorFile(aFD, mJarFile);
320
0
  mJarChannel->SetJarFile(fdFile);
321
0
  nsresult rv = mJarChannel->AsyncOpen2(listener);
322
0
  if (NS_FAILED(rv)) {
323
0
    CancelRequest(listener, mChannel, rv);
324
0
  }
325
0
}
326
327
NS_IMPL_QUERY_INTERFACE(ExtensionProtocolHandler, nsISubstitutingProtocolHandler,
328
                        nsIProtocolHandler, nsIProtocolHandlerWithDynamicFlags,
329
                        nsISupportsWeakReference)
330
NS_IMPL_ADDREF_INHERITED(ExtensionProtocolHandler, SubstitutingProtocolHandler)
331
NS_IMPL_RELEASE_INHERITED(ExtensionProtocolHandler, SubstitutingProtocolHandler)
332
333
already_AddRefed<ExtensionProtocolHandler>
334
ExtensionProtocolHandler::GetSingleton()
335
0
{
336
0
  if (!sSingleton) {
337
0
    sSingleton = new ExtensionProtocolHandler();
338
0
    ClearOnShutdown(&sSingleton);
339
0
  }
340
0
  return do_AddRef(sSingleton);
341
0
}
342
343
ExtensionProtocolHandler::ExtensionProtocolHandler()
344
  : SubstitutingProtocolHandler(EXTENSION_SCHEME)
345
#if !defined(XP_WIN)
346
#if defined(XP_MACOSX)
347
  , mAlreadyCheckedDevRepo(false)
348
#endif /* XP_MACOSX */
349
  , mAlreadyCheckedAppDir(false)
350
#endif /* ! XP_WIN */
351
0
{
352
0
  // Note, extensions.webextensions.protocol.remote=false is for
353
0
  // debugging purposes only. With process-level sandboxing, child
354
0
  // processes (specifically content and extension processes), will
355
0
  // not be able to load most moz-extension URI's when the pref is
356
0
  // set to false.
357
0
  mUseRemoteFileChannels = IsNeckoChild() &&
358
0
    Preferences::GetBool("extensions.webextensions.protocol.remote");
359
0
}
360
361
static inline ExtensionPolicyService&
362
EPS()
363
0
{
364
0
  return ExtensionPolicyService::GetSingleton();
365
0
}
366
367
nsresult
368
ExtensionProtocolHandler::GetFlagsForURI(nsIURI* aURI, uint32_t* aFlags)
369
0
{
370
0
  // In general a moz-extension URI is only loadable by chrome, but a whitelisted
371
0
  // subset are web-accessible (and cross-origin fetchable). Check that whitelist.
372
0
  bool loadableByAnyone = false;
373
0
374
0
  URLInfo url(aURI);
375
0
  if (auto* policy = EPS().GetByURL(url)) {
376
0
    loadableByAnyone = policy->IsPathWebAccessible(url.FilePath());
377
0
  }
378
0
379
0
  *aFlags = URI_STD | URI_IS_LOCAL_RESOURCE | URI_IS_POTENTIALLY_TRUSTWORTHY |
380
0
    (loadableByAnyone ? (URI_LOADABLE_BY_ANYONE |
381
0
                         URI_FETCHABLE_BY_ANYONE) : URI_DANGEROUS_TO_LOAD);
382
0
  return NS_OK;
383
0
}
384
385
bool
386
ExtensionProtocolHandler::ResolveSpecialCases(const nsACString& aHost,
387
                                              const nsACString& aPath,
388
                                              const nsACString& aPathname,
389
                                              nsACString& aResult)
390
0
{
391
0
  // Create special moz-extension:-pages such as moz-extension://foo/_blank.html
392
0
  // for all registered extensions. We can't just do this as a substitution
393
0
  // because substitutions can only match on host.
394
0
  if (!SubstitutingProtocolHandler::HasSubstitution(aHost)) {
395
0
    return false;
396
0
  }
397
0
398
0
  if (aPathname.EqualsLiteral("/_generated_background_page.html")) {
399
0
    Unused << EPS().GetGeneratedBackgroundPageUrl(aHost, aResult);
400
0
    return !aResult.IsEmpty();
401
0
  }
402
0
403
0
  return false;
404
0
}
405
406
// For file or JAR URI's, substitute in a remote channel.
407
Result<Ok, nsresult>
408
ExtensionProtocolHandler::SubstituteRemoteChannel(nsIURI* aURI,
409
                                                  nsILoadInfo* aLoadInfo,
410
                                                  nsIChannel** aRetVal)
411
0
{
412
0
  MOZ_ASSERT(IsNeckoChild());
413
0
  MOZ_TRY(aURI ? NS_OK : NS_ERROR_INVALID_ARG);
414
0
  MOZ_TRY(aLoadInfo ? NS_OK : NS_ERROR_INVALID_ARG);
415
0
416
0
  nsAutoCString unResolvedSpec;
417
0
  MOZ_TRY(aURI->GetSpec(unResolvedSpec));
418
0
419
0
  nsAutoCString resolvedSpec;
420
0
  MOZ_TRY(ResolveURI(aURI, resolvedSpec));
421
0
422
0
  // Use the target URI scheme to determine if this is a packed or unpacked
423
0
  // extension URI. For unpacked extensions, we'll request an input stream
424
0
  // from the parent. For a packed extension, we'll request a file descriptor
425
0
  // for the JAR file.
426
0
  nsAutoCString scheme;
427
0
  MOZ_TRY(net_ExtractURLScheme(resolvedSpec, scheme));
428
0
429
0
  if (scheme.EqualsLiteral("file")) {
430
0
    // Unpacked extension
431
0
    SubstituteRemoteFileChannel(aURI, aLoadInfo, resolvedSpec, aRetVal);
432
0
    return Ok();
433
0
  }
434
0
435
0
  if (scheme.EqualsLiteral("jar")) {
436
0
    // Packed extension
437
0
    return SubstituteRemoteJarChannel(aURI, aLoadInfo, resolvedSpec, aRetVal);
438
0
  }
439
0
440
0
  // Only unpacked resource files and JAR files are remoted.
441
0
  // No other moz-extension loads should be reading from the filesystem.
442
0
  return Ok();
443
0
}
444
445
nsresult
446
ExtensionProtocolHandler::SubstituteChannel(nsIURI* aURI,
447
                                            nsILoadInfo* aLoadInfo,
448
                                            nsIChannel** result)
449
0
{
450
0
  nsresult rv;
451
0
  nsCOMPtr<nsIURL> url = do_QueryInterface(aURI, &rv);
452
0
  NS_ENSURE_SUCCESS(rv, rv);
453
0
454
0
  if (mUseRemoteFileChannels) {
455
0
    MOZ_TRY(SubstituteRemoteChannel(aURI, aLoadInfo, result));
456
0
  }
457
0
458
0
  nsAutoCString ext;
459
0
  rv = url->GetFileExtension(ext);
460
0
  NS_ENSURE_SUCCESS(rv, rv);
461
0
462
0
  if (!ext.LowerCaseEqualsLiteral("css")) {
463
0
    return NS_OK;
464
0
  }
465
0
466
0
  // Filter CSS files to replace locale message tokens with localized strings.
467
0
468
0
  bool haveLoadInfo = aLoadInfo;
469
0
  nsCOMPtr<nsIChannel> channel = NS_NewSimpleChannel(
470
0
    aURI, aLoadInfo, *result,
471
0
    [haveLoadInfo] (nsIStreamListener* listener, nsIChannel* channel, nsIChannel* origChannel) -> RequestOrReason {
472
0
      nsresult rv;
473
0
      nsCOMPtr<nsIStreamConverterService> convService =
474
0
        do_GetService(NS_STREAMCONVERTERSERVICE_CONTRACTID, &rv);
475
0
      MOZ_TRY(rv);
476
0
477
0
      nsCOMPtr<nsIURI> uri;
478
0
      MOZ_TRY(channel->GetURI(getter_AddRefs(uri)));
479
0
480
0
      const char* kFromType = "application/vnd.mozilla.webext.unlocalized";
481
0
      const char* kToType = "text/css";
482
0
483
0
      nsCOMPtr<nsIStreamListener> converter;
484
0
      MOZ_TRY(convService->AsyncConvertData(kFromType, kToType, listener,
485
0
                                        uri, getter_AddRefs(converter)));
486
0
      if (haveLoadInfo) {
487
0
        MOZ_TRY(origChannel->AsyncOpen2(converter));
488
0
      } else {
489
0
        MOZ_TRY(origChannel->AsyncOpen(converter, nullptr));
490
0
      }
491
0
492
0
      return RequestOrReason(origChannel);
493
0
    });
494
0
  NS_ENSURE_TRUE(channel, NS_ERROR_OUT_OF_MEMORY);
495
0
496
0
  if (aLoadInfo) {
497
0
    nsCOMPtr<nsILoadInfo> loadInfo =
498
0
        static_cast<LoadInfo*>(aLoadInfo)->CloneForNewRequest();
499
0
    (*result)->SetLoadInfo(loadInfo);
500
0
  }
501
0
502
0
  channel.swap(*result);
503
0
504
0
  return NS_OK;
505
0
}
506
507
Result<Ok, nsresult>
508
ExtensionProtocolHandler::AllowExternalResource(nsIFile* aExtensionDir,
509
                                                nsIFile* aRequestedFile,
510
                                                bool* aResult)
511
0
{
512
0
  MOZ_ASSERT(!IsNeckoChild());
513
0
  MOZ_ASSERT(aResult);
514
0
  *aResult = false;
515
0
516
#if defined(XP_WIN)
517
  // On Windows, dev builds don't use symlinks so we never need to
518
  // allow a resource from outside of the extension dir.
519
  return Ok();
520
#else
521
0
  if (!mozilla::IsDevelopmentBuild()) {
522
0
    return Ok();
523
0
  }
524
0
525
0
  // On Mac and Linux unpackaged dev builds, system extensions use
526
0
  // symlinks to point to resources in the repo dir which we have to
527
0
  // allow loading. Before we allow an unpacked extension to load a
528
0
  // resource outside of the extension dir, we make sure the extension
529
0
  // dir is within the app directory.
530
0
  MOZ_TRY(AppDirContains(aExtensionDir, aResult));
531
0
  if (!*aResult) {
532
0
    return Ok();
533
0
  }
534
0
535
#if defined(XP_MACOSX)
536
  // Additionally, on Mac dev builds, we make sure that the requested
537
  // resource is within the repo dir. We don't perform this check on Linux
538
  // because we don't have a reliable path to the repo dir on Linux.
539
  MOZ_TRY(DevRepoContains(aRequestedFile, aResult));
540
#endif /* XP_MACOSX */
541
542
0
  return Ok();
543
0
#endif /* defined(XP_WIN) */
544
0
}
545
546
#if defined(XP_MACOSX)
547
// The |aRequestedFile| argument must already be Normalize()'d
548
Result<Ok, nsresult>
549
ExtensionProtocolHandler::DevRepoContains(nsIFile* aRequestedFile,
550
                                          bool* aResult)
551
{
552
  MOZ_ASSERT(mozilla::IsDevelopmentBuild());
553
  MOZ_ASSERT(!IsNeckoChild());
554
  MOZ_ASSERT(aResult);
555
  *aResult = false;
556
557
  // On the first invocation, set mDevRepo
558
  if (!mAlreadyCheckedDevRepo) {
559
    mAlreadyCheckedDevRepo = true;
560
    MOZ_TRY(mozilla::GetRepoDir(getter_AddRefs(mDevRepo)));
561
    if (MOZ_LOG_TEST(gExtProtocolLog, LogLevel::Debug)) {
562
      nsAutoCString repoPath;
563
      Unused << mDevRepo->GetNativePath(repoPath);
564
      LOG("Repo path: %s", repoPath.get());
565
    }
566
  }
567
568
  if (mDevRepo) {
569
    MOZ_TRY(mDevRepo->Contains(aRequestedFile, aResult));
570
  }
571
572
  return Ok();
573
}
574
#endif /* XP_MACOSX */
575
576
#if !defined(XP_WIN)
577
Result<Ok, nsresult>
578
ExtensionProtocolHandler::AppDirContains(nsIFile* aExtensionDir,
579
                                         bool* aResult)
580
0
{
581
0
  MOZ_ASSERT(mozilla::IsDevelopmentBuild());
582
0
  MOZ_ASSERT(!IsNeckoChild());
583
0
  MOZ_ASSERT(aResult);
584
0
  *aResult = false;
585
0
586
0
  // On the first invocation, set mAppDir
587
0
  if (!mAlreadyCheckedAppDir) {
588
0
    mAlreadyCheckedAppDir = true;
589
0
    MOZ_TRY(NS_GetSpecialDirectory(NS_GRE_DIR, getter_AddRefs(mAppDir)));
590
0
    if (MOZ_LOG_TEST(gExtProtocolLog, LogLevel::Debug)) {
591
0
      nsAutoCString appDirPath;
592
0
      Unused << mAppDir->GetNativePath(appDirPath);
593
0
      LOG("AppDir path: %s", appDirPath.get());
594
0
    }
595
0
  }
596
0
597
0
  if (mAppDir) {
598
0
    MOZ_TRY(mAppDir->Contains(aExtensionDir, aResult));
599
0
  }
600
0
601
0
  return Ok();
602
0
}
603
#endif /* !defined(XP_WIN) */
604
605
static void
606
LogExternalResourceError(nsIFile* aExtensionDir, nsIFile* aRequestedFile)
607
0
{
608
0
  MOZ_ASSERT(aExtensionDir);
609
0
  MOZ_ASSERT(aRequestedFile);
610
0
611
0
  LOG("Rejecting external unpacked extension resource [%s] from "
612
0
      "extension directory [%s]", aRequestedFile->HumanReadablePath().get(),
613
0
      aExtensionDir->HumanReadablePath().get());
614
0
}
615
616
Result<nsCOMPtr<nsIInputStream>, nsresult>
617
ExtensionProtocolHandler::NewStream(nsIURI* aChildURI, bool* aTerminateSender)
618
0
{
619
0
  MOZ_ASSERT(!IsNeckoChild());
620
0
  MOZ_TRY(aChildURI ? NS_OK : NS_ERROR_INVALID_ARG);
621
0
  MOZ_TRY(aTerminateSender ? NS_OK : NS_ERROR_INVALID_ARG);
622
0
623
0
  *aTerminateSender = true;
624
0
  nsresult rv;
625
0
626
0
  // We should never receive a URI that isn't for a moz-extension because
627
0
  // these requests ordinarily come from the child's ExtensionProtocolHandler.
628
0
  // Ensure this request is for a moz-extension URI. A rogue child process
629
0
  // could send us any URI.
630
0
  bool isExtScheme = false;
631
0
  if (NS_FAILED(aChildURI->SchemeIs(EXTENSION_SCHEME, &isExtScheme)) ||
632
0
      !isExtScheme) {
633
0
    return Err(NS_ERROR_UNKNOWN_PROTOCOL);
634
0
  }
635
0
636
0
  // For errors after this point, we want to propagate the error to
637
0
  // the child, but we don't force the child to be terminated because
638
0
  // the error is likely to be due to a bug in the extension.
639
0
  *aTerminateSender = false;
640
0
641
0
  /*
642
0
   * Make sure there is a substitution installed for the host found
643
0
   * in the child's request URI and make sure the host resolves to
644
0
   * a directory.
645
0
   */
646
0
647
0
  nsAutoCString host;
648
0
  MOZ_TRY(aChildURI->GetAsciiHost(host));
649
0
650
0
  // Lookup the directory this host string resolves to
651
0
  nsCOMPtr<nsIURI> baseURI;
652
0
  MOZ_TRY(GetSubstitution(host, getter_AddRefs(baseURI)));
653
0
654
0
  // The result should be a file URL for the extension base dir
655
0
  nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(baseURI, &rv);
656
0
  MOZ_TRY(rv);
657
0
658
0
  nsCOMPtr<nsIFile> extensionDir;
659
0
  MOZ_TRY(fileURL->GetFile(getter_AddRefs(extensionDir)));
660
0
661
0
  bool isDirectory = false;
662
0
  MOZ_TRY(extensionDir->IsDirectory(&isDirectory));
663
0
  if (!isDirectory) {
664
0
    // The host should map to a directory for unpacked extensions
665
0
    return Err(NS_ERROR_FILE_NOT_DIRECTORY);
666
0
  }
667
0
668
0
  // Make sure the child URI resolves to a file URI then get a file
669
0
  // channel for the request. The resultant channel should be a
670
0
  // file channel because we only request remote streams for unpacked
671
0
  // extension resource loads where the URI resolves to a file.
672
0
  nsAutoCString resolvedSpec;
673
0
  MOZ_TRY(ResolveURI(aChildURI, resolvedSpec));
674
0
675
0
  nsAutoCString resolvedScheme;
676
0
  MOZ_TRY(net_ExtractURLScheme(resolvedSpec, resolvedScheme));
677
0
  if (!resolvedScheme.EqualsLiteral("file")) {
678
0
    return Err(NS_ERROR_UNEXPECTED);
679
0
  }
680
0
681
0
  nsCOMPtr<nsIIOService> ioService = do_GetIOService(&rv);
682
0
  MOZ_TRY(rv);
683
0
684
0
  nsCOMPtr<nsIURI> resolvedURI;
685
0
  MOZ_TRY(ioService->NewURI(resolvedSpec,
686
0
                            nullptr,
687
0
                            nullptr,
688
0
                            getter_AddRefs(resolvedURI)));
689
0
690
0
  // We use the system principal to get a file channel for the request,
691
0
  // but only after we've checked (above) that the child URI is of
692
0
  // moz-extension scheme and that the URI host maps to a directory.
693
0
  nsCOMPtr<nsIChannel> channel;
694
0
  MOZ_TRY(NS_NewChannel(getter_AddRefs(channel),
695
0
                        resolvedURI,
696
0
                        nsContentUtils::GetSystemPrincipal(),
697
0
                        nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL,
698
0
                        nsIContentPolicy::TYPE_OTHER));
699
0
700
0
  nsCOMPtr<nsIFileChannel> fileChannel = do_QueryInterface(channel, &rv);
701
0
  MOZ_TRY(rv);
702
0
703
0
  nsCOMPtr<nsIFile> requestedFile;
704
0
  MOZ_TRY(fileChannel->GetFile(getter_AddRefs(requestedFile)));
705
0
706
0
  /*
707
0
   * Make sure the file we resolved to is within the extension directory.
708
0
   */
709
0
710
0
  // Normalize paths for sane comparisons. nsIFile::Contains depends on
711
0
  // it for reliable subpath checks.
712
0
  MOZ_TRY(extensionDir->Normalize());
713
0
  MOZ_TRY(requestedFile->Normalize());
714
#if defined(XP_WIN)
715
  if (!widget::WinUtils::ResolveJunctionPointsAndSymLinks(extensionDir) ||
716
      !widget::WinUtils::ResolveJunctionPointsAndSymLinks(requestedFile)) {
717
    return Err(NS_ERROR_FILE_ACCESS_DENIED);
718
  }
719
#endif
720
721
0
  bool isResourceFromExtensionDir = false;
722
0
  MOZ_TRY(extensionDir->Contains(requestedFile, &isResourceFromExtensionDir));
723
0
  if (!isResourceFromExtensionDir) {
724
0
    bool isAllowed = false;
725
0
    MOZ_TRY(AllowExternalResource(extensionDir, requestedFile, &isAllowed));
726
0
    if (!isAllowed) {
727
0
      LogExternalResourceError(extensionDir, requestedFile);
728
0
      return Err(NS_ERROR_FILE_ACCESS_DENIED);
729
0
    }
730
0
  }
731
0
732
0
  nsCOMPtr<nsIInputStream> inputStream;
733
0
  MOZ_TRY(NS_NewLocalFileInputStream(getter_AddRefs(inputStream),
734
0
                                     requestedFile,
735
0
                                     PR_RDONLY,
736
0
                                     -1,
737
0
                                     nsIFileInputStream::DEFER_OPEN));
738
0
739
0
  return inputStream;
740
0
}
741
742
Result<Ok, nsresult>
743
ExtensionProtocolHandler::NewFD(nsIURI* aChildURI,
744
                                bool* aTerminateSender,
745
                                NeckoParent::GetExtensionFDResolver& aResolve)
746
0
{
747
0
  MOZ_ASSERT(!IsNeckoChild());
748
0
  MOZ_TRY(aChildURI ? NS_OK : NS_ERROR_INVALID_ARG);
749
0
  MOZ_TRY(aTerminateSender ? NS_OK : NS_ERROR_INVALID_ARG);
750
0
751
0
  *aTerminateSender = true;
752
0
  nsresult rv;
753
0
754
0
  // Ensure this is a moz-extension URI
755
0
  bool isExtScheme = false;
756
0
  if (NS_FAILED(aChildURI->SchemeIs(EXTENSION_SCHEME, &isExtScheme)) ||
757
0
      !isExtScheme) {
758
0
    return Err(NS_ERROR_UNKNOWN_PROTOCOL);
759
0
  }
760
0
761
0
  // For errors after this point, we want to propagate the error to
762
0
  // the child, but we don't force the child to be terminated.
763
0
  *aTerminateSender = false;
764
0
765
0
  nsAutoCString host;
766
0
  MOZ_TRY(aChildURI->GetAsciiHost(host));
767
0
768
0
  // We expect the host string to map to a JAR file because the URI
769
0
  // should refer to a web accessible resource for an enabled extension.
770
0
  nsCOMPtr<nsIURI> subURI;
771
0
  MOZ_TRY(GetSubstitution(host, getter_AddRefs(subURI)));
772
0
773
0
  nsCOMPtr<nsIJARURI> jarURI = do_QueryInterface(subURI, &rv);
774
0
  MOZ_TRY(rv);
775
0
776
0
  nsCOMPtr<nsIURI> innerFileURI;
777
0
  MOZ_TRY(jarURI->GetJARFile(getter_AddRefs(innerFileURI)));
778
0
779
0
  nsCOMPtr<nsIFileURL> innerFileURL = do_QueryInterface(innerFileURI, &rv);
780
0
  MOZ_TRY(rv);
781
0
782
0
  nsCOMPtr<nsIFile> jarFile;
783
0
  MOZ_TRY(innerFileURL->GetFile(getter_AddRefs(jarFile)));
784
0
785
0
  if (!mFileOpenerThread) {
786
0
    mFileOpenerThread =
787
0
      new LazyIdleThread(DEFAULT_THREAD_TIMEOUT_MS,
788
0
                         NS_LITERAL_CSTRING("ExtensionProtocolHandler"));
789
0
  }
790
0
791
0
  RefPtr<ExtensionJARFileOpener> fileOpener =
792
0
    new ExtensionJARFileOpener(jarFile, aResolve);
793
0
794
0
  nsCOMPtr<nsIRunnable> event =
795
0
    mozilla::NewRunnableMethod("ExtensionJarFileOpener",
796
0
        fileOpener, &ExtensionJARFileOpener::OpenFile);
797
0
798
0
  MOZ_TRY(mFileOpenerThread->Dispatch(event, nsIEventTarget::DISPATCH_NORMAL));
799
0
800
0
  return Ok();
801
0
}
802
803
// Set the channel's content type using the provided URI's type
804
void
805
SetContentType(nsIURI* aURI, nsIChannel* aChannel)
806
0
{
807
0
  nsresult rv;
808
0
  nsCOMPtr<nsIMIMEService> mime = do_GetService("@mozilla.org/mime;1", &rv);
809
0
  if (NS_SUCCEEDED(rv)) {
810
0
    nsAutoCString contentType;
811
0
    rv = mime->GetTypeFromURI(aURI, contentType);
812
0
    if (NS_SUCCEEDED(rv)) {
813
0
      Unused << aChannel->SetContentType(contentType);
814
0
    }
815
0
  }
816
0
}
817
818
// Gets a SimpleChannel that wraps the provided ExtensionStreamGetter
819
static void
820
NewSimpleChannel(nsIURI* aURI,
821
                 nsILoadInfo* aLoadinfo,
822
                 ExtensionStreamGetter* aStreamGetter,
823
                 nsIChannel** aRetVal)
824
0
{
825
0
  nsCOMPtr<nsIChannel> channel = NS_NewSimpleChannel(
826
0
    aURI, aLoadinfo, aStreamGetter,
827
0
    [] (nsIStreamListener* listener, nsIChannel* simpleChannel,
828
0
        ExtensionStreamGetter* getter) -> RequestOrReason {
829
0
      MOZ_TRY(getter->GetAsync(listener, simpleChannel));
830
0
      return RequestOrReason(nullptr);
831
0
    });
832
0
833
0
  SetContentType(aURI, channel);
834
0
  channel.swap(*aRetVal);
835
0
}
836
837
// Gets a SimpleChannel that wraps the provided channel
838
static void
839
NewSimpleChannel(nsIURI* aURI,
840
                 nsILoadInfo* aLoadinfo,
841
                 nsIChannel* aChannel,
842
                 nsIChannel** aRetVal)
843
0
{
844
0
  nsCOMPtr<nsIChannel> channel = NS_NewSimpleChannel(aURI, aLoadinfo, aChannel,
845
0
    [] (nsIStreamListener* listener, nsIChannel* simpleChannel,
846
0
        nsIChannel* origChannel) -> RequestOrReason {
847
0
      nsresult rv = origChannel->AsyncOpen2(listener);
848
0
      if (NS_FAILED(rv)) {
849
0
        simpleChannel->Cancel(NS_BINDING_ABORTED);
850
0
        return RequestOrReason(rv);
851
0
      }
852
0
      return RequestOrReason(origChannel);
853
0
    });
854
0
855
0
  SetContentType(aURI, channel);
856
0
  channel.swap(*aRetVal);
857
0
}
858
859
void
860
ExtensionProtocolHandler::SubstituteRemoteFileChannel(nsIURI* aURI,
861
                                                      nsILoadInfo* aLoadinfo,
862
                                                      nsACString& aResolvedFileSpec,
863
                                                      nsIChannel** aRetVal)
864
0
{
865
0
  MOZ_ASSERT(IsNeckoChild());
866
0
867
0
  RefPtr<ExtensionStreamGetter> streamGetter =
868
0
    new ExtensionStreamGetter(aURI, aLoadinfo);
869
0
870
0
  NewSimpleChannel(aURI, aLoadinfo, streamGetter, aRetVal);
871
0
}
872
873
static Result<Ok, nsresult>
874
LogCacheCheck(const nsIJARChannel* aJarChannel,
875
              nsIJARURI* aJarURI,
876
              bool aIsCached)
877
0
{
878
0
  nsresult rv;
879
0
880
0
  nsCOMPtr<nsIURI> innerFileURI;
881
0
  MOZ_TRY(aJarURI->GetJARFile(getter_AddRefs(innerFileURI)));
882
0
883
0
  nsCOMPtr<nsIFileURL> innerFileURL = do_QueryInterface(innerFileURI, &rv);
884
0
  MOZ_TRY(rv);
885
0
886
0
  nsCOMPtr<nsIFile> jarFile;
887
0
  MOZ_TRY(innerFileURL->GetFile(getter_AddRefs(jarFile)));
888
0
889
0
  nsAutoCString uriSpec, jarSpec;
890
0
  Unused << aJarURI->GetSpec(uriSpec);
891
0
  Unused << innerFileURI->GetSpec(jarSpec);
892
0
  LOG("[JARChannel %p] Cache %s: %s (%s)",
893
0
      aJarChannel, aIsCached ? "hit" : "miss", uriSpec.get(), jarSpec.get());
894
0
895
0
  return Ok();
896
0
}
897
898
Result<Ok, nsresult>
899
ExtensionProtocolHandler::SubstituteRemoteJarChannel(nsIURI* aURI,
900
                                                     nsILoadInfo* aLoadinfo,
901
                                                     nsACString& aResolvedSpec,
902
                                                     nsIChannel** aRetVal)
903
0
{
904
0
  MOZ_ASSERT(IsNeckoChild());
905
0
  nsresult rv;
906
0
907
0
  // Build a JAR URI for this jar:file:// URI and use it to extract the
908
0
  // inner file URI.
909
0
  nsCOMPtr<nsIURI> uri;
910
0
  MOZ_TRY(NS_NewURI(getter_AddRefs(uri), aResolvedSpec));
911
0
912
0
  nsCOMPtr<nsIJARURI> jarURI = do_QueryInterface(uri, &rv);
913
0
  MOZ_TRY(rv);
914
0
915
0
  nsCOMPtr<nsIJARChannel> jarChannel = do_QueryInterface(*aRetVal, &rv);
916
0
  MOZ_TRY(rv);
917
0
918
0
  bool isCached = false;
919
0
  MOZ_TRY(jarChannel->EnsureCached(&isCached));
920
0
  if (MOZ_LOG_TEST(gExtProtocolLog, LogLevel::Debug)) {
921
0
    Unused << LogCacheCheck(jarChannel, jarURI, isCached);
922
0
  }
923
0
924
0
  if (isCached) {
925
0
    // Using a SimpleChannel with an ExtensionStreamGetter here (like the
926
0
    // non-cached JAR case) isn't needed to load the extension resource
927
0
    // because we don't need to ask the parent for an FD for the JAR, but
928
0
    // wrapping the JARChannel in a SimpleChannel allows HTTP forwarding to
929
0
    // moz-extension URI's to work because HTTP forwarding requires the
930
0
    // target channel implement nsIChildChannel.
931
0
    NewSimpleChannel(aURI, aLoadinfo, jarChannel.get(), aRetVal);
932
0
    return Ok();
933
0
  }
934
0
935
0
  nsCOMPtr<nsIURI> innerFileURI;
936
0
  MOZ_TRY(jarURI->GetJARFile(getter_AddRefs(innerFileURI)));
937
0
938
0
  nsCOMPtr<nsIFileURL> innerFileURL = do_QueryInterface(innerFileURI, &rv);
939
0
  MOZ_TRY(rv);
940
0
941
0
  nsCOMPtr<nsIFile> jarFile;
942
0
  MOZ_TRY(innerFileURL->GetFile(getter_AddRefs(jarFile)));
943
0
944
0
  RefPtr<ExtensionStreamGetter> streamGetter =
945
0
    new ExtensionStreamGetter(aURI,
946
0
                              aLoadinfo,
947
0
                              jarChannel.forget(),
948
0
                              jarFile);
949
0
950
0
  NewSimpleChannel(aURI, aLoadinfo, streamGetter, aRetVal);
951
0
  return Ok();
952
0
}
953
954
} // namespace net
955
} // namespace mozilla