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/. */
7 #include "FileSystemSecurity.h"
8 #include "FileSystemUtils.h"
9 #include "mozilla/ClearOnShutdown.h"
10 #include "mozilla/ipc/BackgroundParent.h"
11 #include "mozilla/StaticPtr.h"
13 namespace mozilla::dom
{
17 StaticRefPtr
<FileSystemSecurity
> gFileSystemSecurity
;
22 already_AddRefed
<FileSystemSecurity
> FileSystemSecurity::Get() {
23 MOZ_ASSERT(NS_IsMainThread());
24 mozilla::ipc::AssertIsInMainProcess();
26 RefPtr
<FileSystemSecurity
> service
= gFileSystemSecurity
.get();
27 return service
.forget();
31 already_AddRefed
<FileSystemSecurity
> FileSystemSecurity::GetOrCreate() {
32 MOZ_ASSERT(NS_IsMainThread());
33 mozilla::ipc::AssertIsInMainProcess();
35 if (!gFileSystemSecurity
) {
36 gFileSystemSecurity
= new FileSystemSecurity();
37 ClearOnShutdown(&gFileSystemSecurity
);
40 RefPtr
<FileSystemSecurity
> service
= gFileSystemSecurity
.get();
41 return service
.forget();
44 FileSystemSecurity::FileSystemSecurity() {
45 MOZ_ASSERT(NS_IsMainThread());
46 mozilla::ipc::AssertIsInMainProcess();
49 FileSystemSecurity::~FileSystemSecurity() {
50 MOZ_ASSERT(NS_IsMainThread());
51 mozilla::ipc::AssertIsInMainProcess();
54 void FileSystemSecurity::GrantAccessToContentProcess(
55 ContentParentId aId
, const nsAString
& aDirectoryPath
) {
56 MOZ_ASSERT(NS_IsMainThread());
57 mozilla::ipc::AssertIsInMainProcess();
59 mPaths
.WithEntryHandle(aId
, [&](auto&& entry
) {
60 if (entry
&& entry
.Data()->Contains(aDirectoryPath
)) {
64 entry
.OrInsertWith([] { return MakeUnique
<nsTArray
<nsString
>>(); })
65 ->AppendElement(aDirectoryPath
);
69 void FileSystemSecurity::Forget(ContentParentId aId
) {
70 MOZ_ASSERT(NS_IsMainThread());
71 mozilla::ipc::AssertIsInMainProcess();
76 bool FileSystemSecurity::ContentProcessHasAccessTo(ContentParentId aId
,
77 const nsAString
& aPath
) {
78 MOZ_ASSERT(NS_IsMainThread());
79 mozilla::ipc::AssertIsInMainProcess();
82 if (StringBeginsWith(aPath
, u
"..\\"_ns
) ||
83 FindInReadable(u
"\\..\\"_ns
, aPath
)) {
86 #elif defined(XP_UNIX)
87 if (StringBeginsWith(aPath
, u
"../"_ns
) || FindInReadable(u
"/../"_ns
, aPath
)) {
92 nsTArray
<nsString
>* paths
;
93 if (!mPaths
.Get(aId
, &paths
)) {
97 for (uint32_t i
= 0, len
= paths
->Length(); i
< len
; ++i
) {
98 if (FileSystemUtils::IsDescendantPath(paths
->ElementAt(i
), aPath
)) {
106 } // namespace mozilla::dom