Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/filesystem/FileSystemSecurity.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
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "FileSystemSecurity.h"
8
#include "FileSystemUtils.h"
9
#include "mozilla/ClearOnShutdown.h"
10
#include "mozilla/StaticPtr.h"
11
12
namespace mozilla {
13
namespace dom {
14
15
namespace {
16
17
StaticRefPtr<FileSystemSecurity> gFileSystemSecurity;
18
19
} // anonymous
20
21
/* static */ already_AddRefed<FileSystemSecurity>
22
FileSystemSecurity::Get()
23
0
{
24
0
  MOZ_ASSERT(NS_IsMainThread());
25
0
  AssertIsInMainProcess();
26
0
27
0
  RefPtr<FileSystemSecurity> service = gFileSystemSecurity.get();
28
0
  return service.forget();
29
0
}
30
31
/* static */ already_AddRefed<FileSystemSecurity>
32
FileSystemSecurity::GetOrCreate()
33
0
{
34
0
  MOZ_ASSERT(NS_IsMainThread());
35
0
  AssertIsInMainProcess();
36
0
37
0
  if (!gFileSystemSecurity) {
38
0
    gFileSystemSecurity = new FileSystemSecurity();
39
0
    ClearOnShutdown(&gFileSystemSecurity);
40
0
  }
41
0
42
0
  RefPtr<FileSystemSecurity> service = gFileSystemSecurity.get();
43
0
  return service.forget();
44
0
}
45
46
FileSystemSecurity::FileSystemSecurity()
47
0
{
48
0
  MOZ_ASSERT(NS_IsMainThread());
49
0
  AssertIsInMainProcess();
50
0
}
51
52
FileSystemSecurity::~FileSystemSecurity()
53
0
{
54
0
  MOZ_ASSERT(NS_IsMainThread());
55
0
  AssertIsInMainProcess();
56
0
}
57
58
void
59
FileSystemSecurity::GrantAccessToContentProcess(ContentParentId aId,
60
                                                const nsAString& aDirectoryPath)
61
0
{
62
0
  MOZ_ASSERT(NS_IsMainThread());
63
0
  AssertIsInMainProcess();
64
0
65
0
  nsTArray<nsString>* paths;
66
0
  if (!mPaths.Get(aId, &paths)) {
67
0
    paths = new nsTArray<nsString>();
68
0
    mPaths.Put(aId, paths);
69
0
  } else if (paths->Contains(aDirectoryPath)) {
70
0
    return;
71
0
  }
72
0
73
0
  paths->AppendElement(aDirectoryPath);
74
0
}
75
76
void
77
FileSystemSecurity::Forget(ContentParentId aId)
78
0
{
79
0
  MOZ_ASSERT(NS_IsMainThread());
80
0
  AssertIsInMainProcess();
81
0
82
0
  mPaths.Remove(aId);
83
0
}
84
85
bool
86
FileSystemSecurity::ContentProcessHasAccessTo(ContentParentId aId,
87
                                              const nsAString& aPath)
88
0
{
89
0
  MOZ_ASSERT(NS_IsMainThread());
90
0
  AssertIsInMainProcess();
91
0
92
#if defined(XP_WIN)
93
  if (StringBeginsWith(aPath, NS_LITERAL_STRING("..\\")) ||
94
      FindInReadable(NS_LITERAL_STRING("\\..\\"), aPath)) {
95
    return false;
96
  }
97
#elif defined(XP_UNIX)
98
0
  if (StringBeginsWith(aPath, NS_LITERAL_STRING("../")) ||
99
0
      FindInReadable(NS_LITERAL_STRING("/../"), aPath)) {
100
0
    return false;
101
0
  }
102
0
#endif
103
0
104
0
  nsTArray<nsString>* paths;
105
0
  if (!mPaths.Get(aId, &paths)) {
106
0
    return false;
107
0
  }
108
0
109
0
  for (uint32_t i = 0, len = paths->Length(); i < len; ++i) {
110
0
    if (FileSystemUtils::IsDescendantPath(paths->ElementAt(i), aPath)) {
111
0
      return true;
112
0
    }
113
0
  }
114
0
115
0
  return false;
116
0
}
117
118
} // dom namespace
119
} // mozilla namespace