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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/browser/api/storage/storage_api.h"
#include <stddef.h>
#include <string>
#include <utility>
#include <vector>
#include "base/functional/bind.h"
#include "base/strings/stringprintf.h"
#include "base/task/sequenced_task_runner.h"
#include "base/trace_event/trace_event.h"
#include "base/values.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "extensions/browser/api/storage/session_storage_manager.h"
#include "extensions/browser/api/storage/storage_frontend.h"
#include "extensions/browser/api/storage/storage_utils.h"
#include "extensions/browser/quota_service.h"
#include "extensions/common/api/storage.h"
#include "extensions/common/features/feature.h"
#include "extensions/common/features/feature_channel.h"
#include "extensions/common/mojom/context_type.mojom.h"
using value_store::ValueStore;
namespace extensions {
// Concrete settings functions
namespace {
// Returns a vector of any strings within the given list.
std::vector<std::string> GetKeysFromList(const base::Value::List& list) {
std::vector<std::string> keys;
keys.reserve(list.size());
for (const auto& value : list) {
auto* as_string = value.GetIfString();
if (as_string) {
keys.push_back(*as_string);
}
}
return keys;
}
// Returns a vector of keys within the given dict.
std::vector<std::string> GetKeysFromDict(const base::Value::Dict& dict) {
std::vector<std::string> keys;
keys.reserve(dict.size());
for (auto value : dict) {
keys.push_back(value.first);
}
return keys;
}
// Creates quota heuristics for settings modification.
void GetModificationQuotaLimitHeuristics(QuotaLimitHeuristics* heuristics) {
// See storage.json for the current value of these limits.
QuotaLimitHeuristic::Config short_limit_config = {
api::storage::sync::MAX_WRITE_OPERATIONS_PER_MINUTE, base::Minutes(1)};
QuotaLimitHeuristic::Config long_limit_config = {
api::storage::sync::MAX_WRITE_OPERATIONS_PER_HOUR, base::Hours(1)};
heuristics->push_back(std::make_unique<QuotaService::TimedLimit>(
short_limit_config,
std::make_unique<QuotaLimitHeuristic::SingletonBucketMapper>(),
"MAX_WRITE_OPERATIONS_PER_MINUTE"));
heuristics->push_back(std::make_unique<QuotaService::TimedLimit>(
long_limit_config,
std::make_unique<QuotaLimitHeuristic::SingletonBucketMapper>(),
"MAX_WRITE_OPERATIONS_PER_HOUR"));
}
} // namespace
// SettingsFunction
SettingsFunction::SettingsFunction() = default;
SettingsFunction::~SettingsFunction() = default;
bool SettingsFunction::ShouldSkipQuotaLimiting() const {
// Only apply quota if this is for sync storage.
if (args().empty() || !args()[0].is_string()) {
// This should be EXTENSION_FUNCTION_VALIDATE(false) but there is no way
// to signify that from this function. It will be caught in Run().
return false;
}
const std::string& storage_area_string = args()[0].GetString();
return StorageAreaFromString(storage_area_string) !=
StorageAreaNamespace::kSync;
}
bool SettingsFunction::PreRunValidation(std::string* error) {
if (!ExtensionFunction::PreRunValidation(error)) {
return false;
}
EXTENSION_FUNCTION_PRERUN_VALIDATE(args().size() >= 1);
EXTENSION_FUNCTION_PRERUN_VALIDATE(args()[0].is_string());
base::ListValue& mutable_args = GetMutableArgs();
// Not a ref since we remove the underlying value after.
const std::string storage_area_string(std::move(mutable_args[0].GetString()));
mutable_args.erase(mutable_args.begin());
storage_area_ = StorageAreaFromString(storage_area_string);
EXTENSION_FUNCTION_PRERUN_VALIDATE(storage_area_ !=
StorageAreaNamespace::kInvalid);
// Session is the only storage area that does not use ValueStore, and will
// return synchronously.
if (storage_area_ == StorageAreaNamespace::kSession) {
// Currently only `session` can restrict the storage access. This call will
// be moved after the other storage areas allow it.
if (!IsAccessToStorageAllowed()) {
*error = "Access to storage is not allowed from this context.";
return false;
}
return true;
}
// All other StorageAreas use ValueStore with settings_namespace, and will
// return asynchronously if successful.
settings_namespace_ = StorageAreaToSettingsNamespace(storage_area_);
EXTENSION_FUNCTION_PRERUN_VALIDATE(settings_namespace_ !=
settings_namespace::INVALID);
if (extension()->is_login_screen_extension() &&
storage_area_ != StorageAreaNamespace::kManaged) {
// Login screen extensions are not allowed to use local/sync storage for
// security reasons (see crbug.com/978443).
*error = base::StringPrintf(
"\"%s\" is not available for login screen extensions",
storage_area_string.c_str());
return false;
}
StorageFrontend* frontend = StorageFrontend::Get(browser_context());
if (!frontend->IsStorageEnabled(settings_namespace_)) {
*error =
base::StringPrintf("\"%s\" is not available in this instance of Chrome",
storage_area_string.c_str());
return false;
}
return true;
}
bool SettingsFunction::IsAccessToStorageAllowed() {
api::storage::AccessLevel access_level = storage_utils::GetSessionAccessLevel(
extension()->id(), *browser_context());
// Only a privileged extension context is considered trusted.
if (access_level == api::storage::AccessLevel::kTrustedContexts) {
return source_context_type() == mojom::ContextType::kPrivilegedExtension;
}
// All contexts are allowed.
DCHECK_EQ(api::storage::AccessLevel::kTrustedAndUntrustedContexts,
access_level);
return true;
}
void SettingsFunction::OnWriteOperationFinished(
StorageFrontend::ResultStatus status) {
// Since the storage access happens asynchronously, the browser context can
// be torn down in the interim. If this happens, early-out.
if (!browser_context()) {
return;
}
if (!status.success) {
CHECK(status.error.has_value());
Respond(Error(*status.error));
return;
}
Respond(NoArguments());
}
ExtensionFunction::ResponseAction StorageStorageAreaGetFunction::Run() {
if (args().empty()) {
return RespondNow(BadMessage());
}
base::ListValue& mutable_args = GetMutableArgs();
base::Value input = std::move(mutable_args[0]);
mutable_args.erase(args().begin());
std::optional<std::vector<std::string>> keys;
std::optional<base::Value::Dict> defaults;
switch (input.type()) {
case base::Value::Type::NONE:
keys = std::nullopt;
break;
case base::Value::Type::STRING:
keys = std::optional(std::vector<std::string>(1, input.GetString()));
break;
case base::Value::Type::LIST:
keys = std::optional(GetKeysFromList(input.GetList()));
break;
case base::Value::Type::DICT: {
keys = std::optional(GetKeysFromDict(input.GetDict()));
// When the input holds a dictionary, the values are default values for
// any keys not present in storage. This is only the case for this
// parameter type.
defaults = std::move(input).TakeDict();
break;
}
default:
return RespondNow(BadMessage());
}
StorageFrontend* frontend = StorageFrontend::Get(browser_context());
frontend->GetValues(
extension(), storage_area(), std::move(keys),
base::BindOnce(&StorageStorageAreaGetFunction::OnGetOperationFinished,
this, std::move(defaults)));
return RespondLater();
}
void StorageStorageAreaGetFunction::OnGetOperationFinished(
std::optional<base::Value::Dict> defaults,
StorageFrontend::GetResult result) {
// Since the storage access happens asynchronously, the browser context can
// be torn down in the interim. If this happens, early-out.
if (!browser_context()) {
return;
}
StorageFrontend::ResultStatus status = result.status;
if (!status.success) {
CHECK(status.error.has_value());
Respond(Error(*status.error));
return;
}
CHECK(result.data.has_value());
base::Value::Dict values =
defaults ? std::move(*defaults) : base::Value::Dict();
// It's important that we merge the values into the defaults, and not the
// other way around, to avoid the defaults overwriting any existing values.
values.Merge(std::move(*result.data));
Respond(WithArguments(std::move(values)));
}
ExtensionFunction::ResponseAction StorageStorageAreaGetKeysFunction::Run() {
StorageFrontend* frontend = StorageFrontend::Get(browser_context());
frontend->GetKeys(
extension(), storage_area(),
base::BindOnce(
&StorageStorageAreaGetKeysFunction::OnGetKeysOperationFinished,
this));
return RespondLater();
}
void StorageStorageAreaGetKeysFunction::OnGetKeysOperationFinished(
StorageFrontend::GetKeysResult result) {
// Since the storage access happens asynchronously, the browser context can
// be torn down in the interim. If this happens, early-out.
if (!browser_context()) {
return;
}
StorageFrontend::ResultStatus status = result.status;
if (!status.success) {
CHECK(status.error.has_value());
Respond(Error(*status.error));
return;
}
CHECK(result.data.has_value());
Respond(WithArguments(std::move(*result.data)));
}
ExtensionFunction::ResponseAction
StorageStorageAreaGetBytesInUseFunction::Run() {
if (args().empty()) {
return RespondNow(BadMessage());
}
const base::Value& input = args()[0];
std::optional<std::vector<std::string>> keys;
switch (input.type()) {
case base::Value::Type::NONE:
keys = std::nullopt;
break;
case base::Value::Type::STRING:
keys = std::optional(std::vector<std::string>(1, input.GetString()));
break;
case base::Value::Type::LIST:
keys = std::optional(GetKeysFromList(input.GetList()));
break;
default:
return RespondNow(BadMessage());
}
StorageFrontend* frontend = StorageFrontend::Get(browser_context());
frontend->GetBytesInUse(
extension(), storage_area(), keys,
base::BindOnce(&StorageStorageAreaGetBytesInUseFunction::
OnGetBytesInUseOperationFinished,
this));
return RespondLater();
}
void StorageStorageAreaGetBytesInUseFunction::OnGetBytesInUseOperationFinished(
size_t bytes_in_use) {
// Since the storage access happens asynchronously, the browser context can
// be torn down in the interim. If this happens, early-out.
if (!browser_context()) {
return;
}
// Checked cast should not overflow since a double can represent up to 2*53
// bytes before a loss of precision.
Respond(WithArguments(base::checked_cast<double>(bytes_in_use)));
}
ExtensionFunction::ResponseAction StorageStorageAreaSetFunction::Run() {
if (args().empty() || !args()[0].is_dict()) {
return RespondNow(BadMessage());
}
base::ListValue& mutable_args = GetMutableArgs();
// Retrieve and delete input from `args_` since they will be moved to storage.
base::Value input = std::move(mutable_args[0]);
mutable_args.erase(args().begin());
StorageFrontend* frontend = StorageFrontend::Get(browser_context());
frontend->Set(
extension(), storage_area(), std::move(input).TakeDict(),
base::BindOnce(&StorageStorageAreaSetFunction::OnWriteOperationFinished,
this));
return RespondLater();
}
void StorageStorageAreaSetFunction::GetQuotaLimitHeuristics(
QuotaLimitHeuristics* heuristics) const {
GetModificationQuotaLimitHeuristics(heuristics);
}
ExtensionFunction::ResponseAction StorageStorageAreaRemoveFunction::Run() {
if (args().empty()) {
return RespondNow(BadMessage());
}
const base::Value& input = args()[0];
std::vector<std::string> keys;
switch (input.type()) {
case base::Value::Type::STRING:
keys = std::vector<std::string>(1, input.GetString());
break;
case base::Value::Type::LIST:
keys = GetKeysFromList(input.GetList());
break;
default:
return RespondNow(BadMessage());
}
StorageFrontend* frontend = StorageFrontend::Get(browser_context());
frontend->Remove(
extension(), storage_area(), keys,
base::BindOnce(
&StorageStorageAreaRemoveFunction::OnWriteOperationFinished, this));
return RespondLater();
}
void StorageStorageAreaRemoveFunction::GetQuotaLimitHeuristics(
QuotaLimitHeuristics* heuristics) const {
GetModificationQuotaLimitHeuristics(heuristics);
}
ExtensionFunction::ResponseAction StorageStorageAreaClearFunction::Run() {
StorageFrontend* frontend = StorageFrontend::Get(browser_context());
frontend->Clear(
extension(), storage_area(),
base::BindOnce(&StorageStorageAreaClearFunction::OnWriteOperationFinished,
this));
return RespondLater();
}
void StorageStorageAreaClearFunction::GetQuotaLimitHeuristics(
QuotaLimitHeuristics* heuristics) const {
GetModificationQuotaLimitHeuristics(heuristics);
}
ExtensionFunction::ResponseAction
StorageStorageAreaSetAccessLevelFunction::Run() {
if (storage_area() != StorageAreaNamespace::kSession) {
// TODO(crbug.com/40949182). Support storage areas other than kSession. For
// now, we return an error.
return RespondNow(
Error("This StorageArea is not available for setting access level"));
}
if (source_context_type() != mojom::ContextType::kPrivilegedExtension) {
return RespondNow(Error("Context cannot set the storage access level"));
}
std::optional<api::storage::StorageArea::SetAccessLevel::Params> params =
api::storage::StorageArea::SetAccessLevel::Params::Create(args());
if (!params) {
return RespondNow(BadMessage());
}
// The parsing code ensures `access_level` is sane.
DCHECK(params->access_options.access_level ==
api::storage::AccessLevel::kTrustedContexts ||
params->access_options.access_level ==
api::storage::AccessLevel::kTrustedAndUntrustedContexts);
storage_utils::SetSessionAccessLevel(extension_id(), *browser_context(),
params->access_options.access_level);
return RespondNow(NoArguments());
}
} // namespace extensions
|