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 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/modules/cookie_store/cookie_store.h"
#include <optional>
#include <utility>
#include "base/containers/contains.h"
#include "net/base/features.h"
#include "net/cookies/canonical_cookie.h"
#include "services/network/public/cpp/is_potentially_trustworthy.h"
#include "services/network/public/mojom/restricted_cookie_manager.mojom-blink.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_core.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_throw_dom_exception.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_cookie_init.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_cookie_list_item.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_cookie_store_delete_options.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_cookie_store_get_options.h"
#include "third_party/blink/renderer/core/core_probes_inl.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/dom/dom_exception.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/modules/cookie_store/cookie_change_event.h"
#include "third_party/blink/renderer/modules/event_modules.h"
#include "third_party/blink/renderer/modules/event_target_modules.h"
#include "third_party/blink/renderer/modules/service_worker/service_worker_global_scope.h"
#include "third_party/blink/renderer/modules/service_worker/service_worker_registration.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/bindings/script_state.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/instrumentation/use_counter.h"
#include "third_party/blink/renderer/platform/weborigin/kurl.h"
#include "third_party/blink/renderer/platform/weborigin/security_origin.h"
#include "third_party/blink/renderer/platform/wtf/functional.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
namespace blink {
namespace {
// Returns null if and only if an exception is thrown.
network::mojom::blink::CookieManagerGetOptionsPtr ToBackendOptions(
const CookieStoreGetOptions* options,
ExceptionState& exception_state) {
auto backend_options = network::mojom::blink::CookieManagerGetOptions::New();
// TODO(crbug.com/1124499): Cleanup matchType after evaluation.
backend_options->match_type = network::mojom::blink::CookieMatchType::EQUALS;
if (options->hasName()) {
backend_options->name = options->name();
} else {
// No name provided. Use a filter that matches all cookies. This overrides
// a user-provided matchType.
backend_options->match_type =
network::mojom::blink::CookieMatchType::STARTS_WITH;
backend_options->name = g_empty_string;
}
return backend_options;
}
// Returns no value if and only if an exception is thrown.
std::unique_ptr<net::CanonicalCookie> ToCanonicalCookie(
const KURL& cookie_url,
const CookieInit* options,
ExceptionState& exception_state,
net::CookieInclusionStatus& status_out) {
const String& name = options->name();
const String& value = options->value();
if (name.empty() && value.Contains('=')) {
exception_state.ThrowTypeError(
"Cookie value cannot contain '=' if the name is empty");
return nullptr;
}
if (name.empty() && value.empty()) {
exception_state.ThrowTypeError(
"Cookie name and value both cannot be empty");
return nullptr;
}
base::Time expires = options->hasExpiresNonNull()
? base::Time::FromMillisecondsSinceUnixEpoch(
options->expiresNonNull())
: base::Time();
String cookie_url_host = cookie_url.Host().ToString();
String domain;
if (!options->domain().IsNull()) {
if (name.StartsWith("__Host-")) {
exception_state.ThrowTypeError(
"Cookies with \"__Host-\" prefix cannot have a domain");
return nullptr;
}
// The leading dot (".") from the domain attribute is stripped in the
// Set-Cookie header, for compatibility. This API doesn't have compatibility
// constraints, so reject the edge case outright.
if (options->domain().StartsWith(".")) {
exception_state.ThrowTypeError("Cookie domain cannot start with \".\"");
return nullptr;
}
domain = String(".") + options->domain();
if (!cookie_url_host.EndsWith(domain) &&
cookie_url_host != options->domain()) {
exception_state.ThrowTypeError(
"Cookie domain must domain-match current host");
return nullptr;
}
}
String path = options->path();
if (!path.empty()) {
if (name.StartsWith("__Host-") && path != "/") {
exception_state.ThrowTypeError(
"Cookies with \"__Host-\" prefix cannot have a non-\"/\" path");
return nullptr;
}
if (!path.StartsWith("/")) {
exception_state.ThrowTypeError("Cookie path must start with \"/\"");
return nullptr;
}
if (!path.EndsWith("/")) {
path = path + String("/");
}
}
// The Cookie Store API will only write secure cookies but will read insecure
// cookies. As a result,
// cookieStore.get("name", "value") can get an insecure cookie, but when
// modifying a retrieved insecure cookie via the Cookie Store API, it will
// automatically turn it into a secure cookie without any warning.
//
// The Cookie Store API can only set secure cookies, so it is unusable on
// insecure origins. file:// are excluded too for consistency with
// document.cookie.
if (!network::IsUrlPotentiallyTrustworthy(GURL(cookie_url)) ||
base::Contains(url::GetLocalSchemes(), cookie_url.Protocol().Ascii())) {
exception_state.ThrowTypeError(
"Cannot modify a secure cookie on insecure origin");
return nullptr;
}
net::CookieSameSite same_site;
if (options->sameSite() == "strict") {
same_site = net::CookieSameSite::STRICT_MODE;
} else if (options->sameSite() == "lax") {
same_site = net::CookieSameSite::LAX_MODE;
} else {
DCHECK_EQ(options->sameSite(), "none");
same_site = net::CookieSameSite::NO_RESTRICTION;
}
std::optional<net::CookiePartitionKey> cookie_partition_key = std::nullopt;
if (options->partitioned()) {
// We don't trust the renderer to determine the cookie partition key, so we
// use this factory to indicate we are using a temporary value here.
cookie_partition_key = net::CookiePartitionKey::FromScript();
}
std::unique_ptr<net::CanonicalCookie> cookie =
net::CanonicalCookie::CreateSanitizedCookie(
GURL(cookie_url), name.Utf8(), value.Utf8(), domain.Utf8(),
path.Utf8(), base::Time() /*creation*/, expires,
base::Time() /*last_access*/, true /*secure*/, false /*http_only*/,
same_site, net::CookiePriority::COOKIE_PRIORITY_DEFAULT,
cookie_partition_key, &status_out);
// TODO(crbug.com/1310444): Improve serialization validation comments and
// associate them with ExceptionState codes.
if (!status_out.IsInclude()) {
exception_state.ThrowTypeError(
"Cookie was malformed and could not be stored, due to problem(s) while "
"parsing.");
}
return cookie;
}
const KURL DefaultCookieURL(ExecutionContext* execution_context) {
DCHECK(execution_context);
if (auto* window = DynamicTo<LocalDOMWindow>(execution_context))
return window->document()->CookieURL();
return KURL(To<ServiceWorkerGlobalScope>(execution_context)
->serviceWorker()
->scriptURL());
}
// Return empty KURL if and only if an exception is thrown.
KURL CookieUrlForRead(const CookieStoreGetOptions* options,
const KURL& default_cookie_url,
ScriptState* script_state,
ExceptionState& exception_state) {
ExecutionContext* context = ExecutionContext::From(script_state);
if (!options->hasUrl())
return default_cookie_url;
KURL cookie_url = KURL(default_cookie_url, options->url());
if (auto* window = DynamicTo<LocalDOMWindow>(context)) {
DCHECK_EQ(default_cookie_url, window->document()->CookieURL());
if (cookie_url.GetString() != default_cookie_url.GetString()) {
exception_state.ThrowTypeError("URL must match the document URL");
return KURL();
}
} else {
DCHECK(context->IsServiceWorkerGlobalScope());
DCHECK_EQ(
default_cookie_url.GetString(),
To<ServiceWorkerGlobalScope>(context)->serviceWorker()->scriptURL());
if (!cookie_url.GetString().StartsWith(default_cookie_url.GetString())) {
exception_state.ThrowTypeError("URL must be within Service Worker scope");
return KURL();
}
}
return cookie_url;
}
net::SiteForCookies DefaultSiteForCookies(ExecutionContext* execution_context) {
DCHECK(execution_context);
if (auto* window = DynamicTo<LocalDOMWindow>(execution_context))
return window->document()->SiteForCookies();
auto* scope = To<ServiceWorkerGlobalScope>(execution_context);
const blink::BlinkStorageKey& key = scope->storage_key();
if (key.IsFirstPartyContext()) {
return net::SiteForCookies::FromUrl(GURL(scope->Url()));
}
return net::SiteForCookies();
}
const scoped_refptr<const SecurityOrigin> DefaultTopFrameOrigin(
ExecutionContext* execution_context) {
DCHECK(execution_context);
if (auto* window = DynamicTo<LocalDOMWindow>(execution_context)) {
// Can we avoid the copy? TopFrameOrigin is returned as const& but we need
// a scoped_refptr.
return window->document()->TopFrameOrigin()->IsolatedCopy();
}
const BlinkStorageKey& key =
To<ServiceWorkerGlobalScope>(execution_context)->storage_key();
if (key.IsFirstPartyContext()) {
return key.GetSecurityOrigin();
}
return SecurityOrigin::CreateFromUrlOrigin(
url::Origin::Create(net::SchemefulSite(key.GetTopLevelSite()).GetURL()));
}
bool IsAdTagged(ExecutionContext* context) {
if (auto* window = DynamicTo<LocalDOMWindow>(context)) {
if (auto* local_frame = window->GetFrame()) {
return local_frame->IsAdFrame();
}
}
return false;
}
} // namespace
CookieStore::CookieStore(
ExecutionContext* execution_context,
HeapMojoRemote<network::mojom::blink::RestrictedCookieManager> backend)
: ExecutionContextClient(execution_context),
backend_(std::move(backend)),
change_listener_receiver_(this, execution_context),
default_cookie_url_(DefaultCookieURL(execution_context)),
default_site_for_cookies_(DefaultSiteForCookies(execution_context)),
default_top_frame_origin_(DefaultTopFrameOrigin(execution_context)) {
DCHECK(backend_);
}
CookieStore::~CookieStore() = default;
ScriptPromise<IDLSequence<CookieListItem>> CookieStore::getAll(
ScriptState* script_state,
const String& name,
ExceptionState& exception_state) {
CookieStoreGetOptions* options = CookieStoreGetOptions::Create();
options->setName(name);
return getAll(script_state, options, exception_state);
}
ScriptPromise<IDLSequence<CookieListItem>> CookieStore::getAll(
ScriptState* script_state,
const CookieStoreGetOptions* options,
ExceptionState& exception_state) {
UseCounter::Count(CurrentExecutionContext(script_state->GetIsolate()),
WebFeature::kCookieStoreAPI);
auto* resolver =
MakeGarbageCollected<ScriptPromiseResolver<IDLSequence<CookieListItem>>>(
script_state, exception_state.GetContext());
auto promise = resolver->Promise();
DoRead(script_state, options,
WTF::BindOnce(&CookieStore::GetAllForUrlToGetAllResult,
WrapPersistent(resolver)),
exception_state);
if (exception_state.HadException()) {
resolver->Detach();
return EmptyPromise();
}
return promise;
}
ScriptPromise<IDLNullable<CookieListItem>> CookieStore::get(
ScriptState* script_state,
const String& name,
ExceptionState& exception_state) {
CookieStoreGetOptions* options = CookieStoreGetOptions::Create();
options->setName(name);
return get(script_state, options, exception_state);
}
ScriptPromise<IDLNullable<CookieListItem>> CookieStore::get(
ScriptState* script_state,
const CookieStoreGetOptions* options,
ExceptionState& exception_state) {
UseCounter::Count(CurrentExecutionContext(script_state->GetIsolate()),
WebFeature::kCookieStoreAPI);
if (!options->hasName() && !options->hasUrl()) {
exception_state.ThrowTypeError("CookieStoreGetOptions must not be empty");
return ScriptPromise<IDLNullable<CookieListItem>>();
}
auto* resolver =
MakeGarbageCollected<ScriptPromiseResolver<IDLNullable<CookieListItem>>>(
script_state, exception_state.GetContext());
auto promise = resolver->Promise();
DoRead(script_state, options,
WTF::BindOnce(&CookieStore::GetAllForUrlToGetResult,
WrapPersistent(resolver)),
exception_state);
if (exception_state.HadException()) {
resolver->Detach();
return EmptyPromise();
}
return promise;
}
ScriptPromise<IDLUndefined> CookieStore::set(ScriptState* script_state,
const String& name,
const String& value,
ExceptionState& exception_state) {
CookieInit* set_options = CookieInit::Create();
set_options->setName(name);
set_options->setValue(value);
return set(script_state, set_options, exception_state);
}
ScriptPromise<IDLUndefined> CookieStore::set(ScriptState* script_state,
const CookieInit* options,
ExceptionState& exception_state) {
UseCounter::Count(CurrentExecutionContext(script_state->GetIsolate()),
WebFeature::kCookieStoreAPI);
return DoWrite(script_state, options, exception_state);
}
ScriptPromise<IDLUndefined> CookieStore::Delete(
ScriptState* script_state,
const String& name,
ExceptionState& exception_state) {
UseCounter::Count(CurrentExecutionContext(script_state->GetIsolate()),
WebFeature::kCookieStoreAPI);
CookieInit* set_options = CookieInit::Create();
set_options->setName(name);
set_options->setValue("deleted");
set_options->setExpires(0);
return DoWrite(script_state, set_options, exception_state);
}
ScriptPromise<IDLUndefined> CookieStore::Delete(
ScriptState* script_state,
const CookieStoreDeleteOptions* options,
ExceptionState& exception_state) {
CookieInit* set_options = CookieInit::Create();
set_options->setName(options->name());
set_options->setValue("deleted");
set_options->setExpires(0);
set_options->setDomain(options->domain());
set_options->setPath(options->path());
set_options->setSameSite("strict");
set_options->setPartitioned(options->partitioned());
return DoWrite(script_state, set_options, exception_state);
}
void CookieStore::Trace(Visitor* visitor) const {
visitor->Trace(change_listener_receiver_);
visitor->Trace(backend_);
EventTarget::Trace(visitor);
ExecutionContextClient::Trace(visitor);
}
const AtomicString& CookieStore::InterfaceName() const {
return event_target_names::kCookieStore;
}
ExecutionContext* CookieStore::GetExecutionContext() const {
return ExecutionContextClient::GetExecutionContext();
}
void CookieStore::RemoveAllEventListeners() {
EventTarget::RemoveAllEventListeners();
DCHECK(!HasEventListeners());
StopObserving();
}
void CookieStore::OnCookieChange(
network::mojom::blink::CookieChangeInfoPtr change) {
HeapVector<Member<CookieListItem>> changed, deleted;
CookieChangeEvent::ToEventInfo(change, changed, deleted);
if (changed.empty() && deleted.empty()) {
// The backend only reported OVERWRITE events, which are dropped.
return;
}
DispatchEvent(*CookieChangeEvent::Create(
event_type_names::kChange, std::move(changed), std::move(deleted)));
}
void CookieStore::AddedEventListener(
const AtomicString& event_type,
RegisteredEventListener& registered_listener) {
EventTarget::AddedEventListener(event_type, registered_listener);
StartObserving();
}
void CookieStore::RemovedEventListener(
const AtomicString& event_type,
const RegisteredEventListener& registered_listener) {
EventTarget::RemovedEventListener(event_type, registered_listener);
if (!HasEventListeners())
StopObserving();
}
void CookieStore::DoRead(ScriptState* script_state,
const CookieStoreGetOptions* options,
GetAllForUrlCallback backend_result_converter,
ExceptionState& exception_state) {
ExecutionContext* context = ExecutionContext::From(script_state);
if (!context->GetSecurityOrigin()->CanAccessCookies()) {
exception_state.ThrowSecurityError(
"Access to the CookieStore API is denied in this context.");
return;
}
network::mojom::blink::CookieManagerGetOptionsPtr backend_options =
ToBackendOptions(options, exception_state);
KURL cookie_url = CookieUrlForRead(options, default_cookie_url_, script_state,
exception_state);
if (backend_options.is_null() || cookie_url.IsNull()) {
DCHECK(exception_state.HadException());
return;
}
if (!backend_) {
exception_state.ThrowDOMException(DOMExceptionCode::kInvalidStateError,
"CookieStore backend went away");
return;
}
bool is_ad_tagged = IsAdTagged(context);
bool should_apply_devtools_overrides = false;
probe::ShouldApplyDevtoolsCookieSettingOverrides(
GetExecutionContext(), &should_apply_devtools_overrides);
backend_->GetAllForUrl(
cookie_url, default_site_for_cookies_, default_top_frame_origin_,
context->GetStorageAccessApiStatus(), std::move(backend_options),
is_ad_tagged, should_apply_devtools_overrides,
/*force_disable_third_party_cookies=*/false,
std::move(backend_result_converter));
}
// static
void CookieStore::GetAllForUrlToGetAllResult(
ScriptPromiseResolver<IDLSequence<CookieListItem>>* resolver,
const Vector<network::mojom::blink::CookieWithAccessResultPtr>
backend_cookies) {
ScriptState* script_state = resolver->GetScriptState();
if (!script_state->ContextIsValid())
return;
ScriptState::Scope scope(script_state);
HeapVector<Member<CookieListItem>> cookies;
cookies.ReserveInitialCapacity(backend_cookies.size());
for (const auto& backend_cookie : backend_cookies) {
cookies.push_back(CookieChangeEvent::ToCookieListItem(
backend_cookie->cookie,
backend_cookie->access_result->effective_same_site,
false /* is_deleted */));
}
resolver->Resolve(std::move(cookies));
}
// static
void CookieStore::GetAllForUrlToGetResult(
ScriptPromiseResolver<IDLNullable<CookieListItem>>* resolver,
const Vector<network::mojom::blink::CookieWithAccessResultPtr>
backend_cookies) {
ScriptState* script_state = resolver->GetScriptState();
if (!script_state->ContextIsValid())
return;
ScriptState::Scope scope(script_state);
if (backend_cookies.empty()) {
resolver->Resolve(nullptr);
return;
}
const auto& backend_cookie = backend_cookies.front();
CookieListItem* cookie = CookieChangeEvent::ToCookieListItem(
backend_cookie->cookie,
backend_cookie->access_result->effective_same_site,
false /* is_deleted */);
resolver->Resolve(cookie);
}
ScriptPromise<IDLUndefined> CookieStore::DoWrite(
ScriptState* script_state,
const CookieInit* options,
ExceptionState& exception_state) {
ExecutionContext* context = ExecutionContext::From(script_state);
if (!context->GetSecurityOrigin()->CanAccessCookies()) {
exception_state.ThrowSecurityError(
"Access to the CookieStore API is denied in this context.");
return EmptyPromise();
}
net::CookieInclusionStatus status;
std::unique_ptr<net::CanonicalCookie> canonical_cookie =
ToCanonicalCookie(default_cookie_url_, options, exception_state, status);
if (!canonical_cookie) {
DCHECK(exception_state.HadException());
return EmptyPromise();
}
// Since a canonical cookie exists, the status should have no exclusion
// reasons associated with it.
DCHECK(status.IsInclude());
if (!backend_) {
exception_state.ThrowDOMException(DOMExceptionCode::kInvalidStateError,
"CookieStore backend went away");
return EmptyPromise();
}
bool is_ad_tagged = IsAdTagged(context);
bool should_apply_devtools_overrides = false;
probe::ShouldApplyDevtoolsCookieSettingOverrides(
GetExecutionContext(), &should_apply_devtools_overrides);
auto* resolver = MakeGarbageCollected<ScriptPromiseResolver<IDLUndefined>>(
script_state, exception_state.GetContext());
backend_->SetCanonicalCookie(
*std::move(canonical_cookie), default_cookie_url_,
default_site_for_cookies_, default_top_frame_origin_,
context->GetStorageAccessApiStatus(), status, is_ad_tagged,
should_apply_devtools_overrides,
WTF::BindOnce(&CookieStore::OnSetCanonicalCookieResult,
WrapPersistent(resolver)));
return resolver->Promise();
}
// static
void CookieStore::OnSetCanonicalCookieResult(
ScriptPromiseResolver<IDLUndefined>* resolver,
bool backend_success) {
if (!backend_success) {
resolver->RejectWithDOMException(
DOMExceptionCode::kUnknownError,
"An unknown error occurred while writing the cookie.");
return;
}
resolver->Resolve();
}
void CookieStore::StartObserving() {
if (change_listener_receiver_.is_bound() || !backend_)
return;
// See https://bit.ly/2S0zRAS for task types.
auto task_runner =
GetExecutionContext()->GetTaskRunner(TaskType::kDOMManipulation);
backend_->AddChangeListener(
default_cookie_url_, default_site_for_cookies_, default_top_frame_origin_,
GetExecutionContext()->GetStorageAccessApiStatus(),
change_listener_receiver_.BindNewPipeAndPassRemote(task_runner), {});
}
void CookieStore::StopObserving() {
if (!change_listener_receiver_.is_bound())
return;
change_listener_receiver_.reset();
}
} // namespace blink
|