1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219

android_webview / browser / file_system_access / aw_file_system_access_permission_context.cc [blame]

// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "android_webview/browser/file_system_access/aw_file_system_access_permission_context.h"

#include <vector>

#include "base/android/build_info.h"
#include "base/base_paths_android.h"
#include "base/base_paths_posix.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/memory/scoped_refptr.h"
#include "base/path_service.h"
#include "base/strings/strcat.h"
#include "base/strings/string_util.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "content/public/browser/file_system_access_permission_grant.h"

namespace android_webview {
namespace {

class FixedFileSystemAccessPermissionGrant
    : public content::FileSystemAccessPermissionGrant {
 public:
  FixedFileSystemAccessPermissionGrant(PermissionStatus status,
                                       content::PathInfo path_info)
      : status_(status), path_info_(std::move(path_info)) {}

  FixedFileSystemAccessPermissionGrant(
      const FixedFileSystemAccessPermissionGrant&) = delete;

  FixedFileSystemAccessPermissionGrant& operator=(
      const FixedFileSystemAccessPermissionGrant&) = delete;

  // FileSystemAccessPermissionGrant:
  PermissionStatus GetStatus() override { return status_; }

  base::FilePath GetPath() override { return path_info_.path; }

  std::string GetDisplayName() override { return path_info_.display_name; }

  void RequestPermission(
      content::GlobalRenderFrameHostId frame_id,
      UserActivationState user_activation_state,
      base::OnceCallback<void(PermissionRequestOutcome)> callback) override {
    std::move(callback).Run(PermissionRequestOutcome::kRequestAborted);
  }

 private:
  ~FixedFileSystemAccessPermissionGrant() override = default;

  const PermissionStatus status_;
  const content::PathInfo path_info_;
};

bool ShouldBlockAccessToPath(const base::FilePath& path) {
  CHECK(!path.empty());

  // We do not block any Content-URIs since no internal chrome paths are
  // exposed. We rely on the hosting app to filter any disallowed paths when
  // they implement WebChromeClient#onFileChooser().
  if (path.IsContentUri()) {
    return false;
  }

  // Block any empty or non-absolute paths.
  if (!path.IsAbsolute()) {
    return true;
  }

  constexpr int kBlockedPaths[] = {
      base::DIR_ANDROID_APP_DATA,
      base::DIR_CACHE,
  };

  // Check internal WebView paths.
  for (auto path_key : kBlockedPaths) {
    base::FilePath blocked_path;
    if (!base::PathService::Get(path_key, &blocked_path)) {
      continue;
    }
    if (path == blocked_path || blocked_path.IsParent(path)) {
      return true;
    }
  }

  return false;
}

}  //  namespace

AwFileSystemAccessPermissionContext::AwFileSystemAccessPermissionContext() =
    default;

AwFileSystemAccessPermissionContext::~AwFileSystemAccessPermissionContext() =
    default;

scoped_refptr<content::FileSystemAccessPermissionGrant>
AwFileSystemAccessPermissionContext::GetReadPermissionGrant(
    const url::Origin& origin,
    const content::PathInfo& path_info,
    HandleType handle_type,
    UserAction user_action) {
  return base::MakeRefCounted<FixedFileSystemAccessPermissionGrant>(
      content::FileSystemAccessPermissionGrant::PermissionStatus::GRANTED,
      path_info);
}

scoped_refptr<content::FileSystemAccessPermissionGrant>
AwFileSystemAccessPermissionContext::GetWritePermissionGrant(
    const url::Origin& origin,
    const content::PathInfo& path_info,
    HandleType handle_type,
    UserAction user_action) {
  return base::MakeRefCounted<FixedFileSystemAccessPermissionGrant>(
      content::FileSystemAccessPermissionGrant::PermissionStatus::GRANTED,
      path_info);
}

void AwFileSystemAccessPermissionContext::ConfirmSensitiveEntryAccess(
    const url::Origin& origin,
    const content::PathInfo& path_info,
    HandleType handle_type,
    UserAction user_action,
    content::GlobalRenderFrameHostId frame_id,
    base::OnceCallback<void(SensitiveEntryResult)> callback) {
  CheckPathAgainstBlocklist(
      path_info.path,
      base::BindOnce(
          &AwFileSystemAccessPermissionContext::DidCheckPathAgainstBlocklist,
          weak_factory_.GetWeakPtr(), path_info.path, std::move(callback)));
}

void AwFileSystemAccessPermissionContext::PerformAfterWriteChecks(
    std::unique_ptr<content::FileSystemAccessWriteItem> item,
    content::GlobalRenderFrameHostId frame_id,
    base::OnceCallback<void(AfterWriteCheckResult)> callback) {
  std::move(callback).Run(AfterWriteCheckResult::kAllow);
}

base::expected<void, std::string>
AwFileSystemAccessPermissionContext::CanShowFilePicker(
    content::RenderFrameHost* rfh) {
  return base::ok();
}

bool AwFileSystemAccessPermissionContext::CanObtainReadPermission(
    const url::Origin& origin) {
  return true;
}
bool AwFileSystemAccessPermissionContext::CanObtainWritePermission(
    const url::Origin& origin) {
  return true;
}

bool AwFileSystemAccessPermissionContext::IsFileTypeDangerous(
    const base::FilePath& path,
    const url::Origin& origin) {
  return false;
}

void AwFileSystemAccessPermissionContext::SetLastPickedDirectory(
    const url::Origin& origin,
    const std::string& id,
    const content::PathInfo& path_info) {}

content::PathInfo AwFileSystemAccessPermissionContext::GetLastPickedDirectory(
    const url::Origin& origin,
    const std::string& id) {
  return content::PathInfo();
}

base::FilePath AwFileSystemAccessPermissionContext::GetWellKnownDirectoryPath(
    blink::mojom::WellKnownDirectory directory,
    const url::Origin& origin) {
  return base::FilePath();
}

std::u16string AwFileSystemAccessPermissionContext::GetPickerTitle(
    const blink::mojom::FilePickerOptionsPtr& options) {
  return std::u16string();
}

void AwFileSystemAccessPermissionContext::NotifyEntryMoved(
    const url::Origin& origin,
    const content::PathInfo& old_path,
    const content::PathInfo& new_path) {}

void AwFileSystemAccessPermissionContext::OnFileCreatedFromShowSaveFilePicker(
    const GURL& file_picker_binding_context,
    const storage::FileSystemURL& url) {}

void AwFileSystemAccessPermissionContext::CheckPathsAgainstEnterprisePolicy(
    std::vector<content::PathInfo> entries,
    content::GlobalRenderFrameHostId frame_id,
    EntriesAllowedByEnterprisePolicyCallback callback) {
  std::move(callback).Run(std::move(entries));
}

void AwFileSystemAccessPermissionContext::CheckPathAgainstBlocklist(
    const base::FilePath& path,
    base::OnceCallback<void(bool)> callback) {
  base::ThreadPool::PostTaskAndReplyWithResult(
      FROM_HERE, {base::MayBlock(), base::TaskPriority::USER_VISIBLE},
      base::BindOnce(&ShouldBlockAccessToPath, path), std::move(callback));
}

void AwFileSystemAccessPermissionContext::DidCheckPathAgainstBlocklist(
    const base::FilePath& path,
    base::OnceCallback<void(SensitiveEntryResult)> callback,
    bool should_block) {
  std::move(callback).Run(should_block ? SensitiveEntryResult::kAbort
                                       : SensitiveEntryResult::kAllowed);
}

}  // namespace android_webview