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 "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 3 : StaticRefPtr<FileSystemSecurity> gFileSystemSecurity;
18 :
19 : } // anonymous
20 :
21 : /* static */ already_AddRefed<FileSystemSecurity>
22 0 : FileSystemSecurity::Get()
23 : {
24 0 : MOZ_ASSERT(NS_IsMainThread());
25 0 : AssertIsInMainProcess();
26 :
27 0 : RefPtr<FileSystemSecurity> service = gFileSystemSecurity.get();
28 0 : return service.forget();
29 : }
30 :
31 : /* static */ already_AddRefed<FileSystemSecurity>
32 0 : FileSystemSecurity::GetOrCreate()
33 : {
34 0 : MOZ_ASSERT(NS_IsMainThread());
35 0 : AssertIsInMainProcess();
36 :
37 0 : if (!gFileSystemSecurity) {
38 0 : gFileSystemSecurity = new FileSystemSecurity();
39 0 : ClearOnShutdown(&gFileSystemSecurity);
40 : }
41 :
42 0 : RefPtr<FileSystemSecurity> service = gFileSystemSecurity.get();
43 0 : return service.forget();
44 : }
45 :
46 0 : FileSystemSecurity::FileSystemSecurity()
47 : {
48 0 : MOZ_ASSERT(NS_IsMainThread());
49 0 : AssertIsInMainProcess();
50 0 : }
51 :
52 0 : FileSystemSecurity::~FileSystemSecurity()
53 : {
54 0 : MOZ_ASSERT(NS_IsMainThread());
55 0 : AssertIsInMainProcess();
56 0 : }
57 :
58 : void
59 0 : FileSystemSecurity::GrantAccessToContentProcess(ContentParentId aId,
60 : const nsAString& aDirectoryPath)
61 : {
62 0 : MOZ_ASSERT(NS_IsMainThread());
63 0 : AssertIsInMainProcess();
64 :
65 : 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 : }
72 :
73 0 : paths->AppendElement(aDirectoryPath);
74 : }
75 :
76 : void
77 0 : FileSystemSecurity::Forget(ContentParentId aId)
78 : {
79 0 : MOZ_ASSERT(NS_IsMainThread());
80 0 : AssertIsInMainProcess();
81 :
82 0 : mPaths.Remove(aId);
83 0 : }
84 :
85 : bool
86 0 : FileSystemSecurity::ContentProcessHasAccessTo(ContentParentId aId,
87 : const nsAString& aPath)
88 : {
89 0 : MOZ_ASSERT(NS_IsMainThread());
90 0 : AssertIsInMainProcess();
91 :
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 : }
102 : #endif
103 :
104 : nsTArray<nsString>* paths;
105 0 : if (!mPaths.Get(aId, &paths)) {
106 0 : return false;
107 : }
108 :
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 : }
113 : }
114 :
115 0 : return false;
116 : }
117 :
118 : } // dom namespace
119 : } // mozilla namespace
|