Bug 1824753 [wpt PR 39216] - [FLEDGE] Add WPT test that FLEDGE is not allowed in...
[gecko.git] / dom / filesystem / FileSystemSecurity.cpp
blobb9468270d45379fb1916f76f181b8a4cff3f58ec
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 {
15 namespace {
17 StaticRefPtr<FileSystemSecurity> gFileSystemSecurity;
19 } // namespace
21 /* static */
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();
30 /* static */
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)) {
61 return;
64 entry.OrInsertWith([] { return MakeUnique<nsTArray<nsString>>(); })
65 ->AppendElement(aDirectoryPath);
66 });
69 void FileSystemSecurity::Forget(ContentParentId aId) {
70 MOZ_ASSERT(NS_IsMainThread());
71 mozilla::ipc::AssertIsInMainProcess();
73 mPaths.Remove(aId);
76 bool FileSystemSecurity::ContentProcessHasAccessTo(ContentParentId aId,
77 const nsAString& aPath) {
78 MOZ_ASSERT(NS_IsMainThread());
79 mozilla::ipc::AssertIsInMainProcess();
81 #if defined(XP_WIN)
82 if (StringBeginsWith(aPath, u"..\\"_ns) ||
83 FindInReadable(u"\\..\\"_ns, aPath)) {
84 return false;
86 #elif defined(XP_UNIX)
87 if (StringBeginsWith(aPath, u"../"_ns) || FindInReadable(u"/../"_ns, aPath)) {
88 return false;
90 #endif
92 nsTArray<nsString>* paths;
93 if (!mPaths.Get(aId, &paths)) {
94 return false;
97 for (uint32_t i = 0, len = paths->Length(); i < len; ++i) {
98 if (FileSystemUtils::IsDescendantPath(paths->ElementAt(i), aPath)) {
99 return true;
103 return false;
106 } // namespace mozilla::dom