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 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
|
// 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 "net/device_bound_sessions/cookie_craving.h"
#include "base/strings/string_util.h"
#include "base/unguessable_token.h"
#include "net/cookies/canonical_cookie.h"
#include "net/cookies/cookie_constants.h"
#include "net/cookies/cookie_partition_key.h"
#include "net/device_bound_sessions/proto/storage.pb.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net::device_bound_sessions {
// Default values for tests.
constexpr char kUrlString[] = "https://www.example.test/foo";
constexpr char kName[] = "name";
const base::Time kCreationTime = base::Time::Now();
// Helper to Create() and unwrap a CookieCraving, expecting it to be valid.
CookieCraving CreateValidCookieCraving(
const GURL& url,
const std::string& name,
const std::string& attributes,
base::Time creation_time = kCreationTime,
std::optional<CookiePartitionKey> cookie_partition_key = std::nullopt) {
std::optional<CookieCraving> maybe_cc = CookieCraving::Create(
url, name, attributes, creation_time, cookie_partition_key);
EXPECT_TRUE(maybe_cc);
EXPECT_TRUE(maybe_cc->IsValid());
return std::move(*maybe_cc);
}
// Helper to create and unwrap a CanonicalCookie.
CanonicalCookie CreateCanonicalCookie(
const GURL& url,
const std::string& cookie_line,
base::Time creation_time = kCreationTime,
std::optional<CookiePartitionKey> cookie_partition_key = std::nullopt) {
std::unique_ptr<CanonicalCookie> canonical_cookie =
CanonicalCookie::CreateForTesting(url, cookie_line, creation_time,
/*server_time=*/std::nullopt,
cookie_partition_key);
EXPECT_TRUE(canonical_cookie);
EXPECT_TRUE(canonical_cookie->IsCanonical());
return *canonical_cookie;
}
TEST(CookieCravingTest, CreateBasic) {
// Default cookie.
CookieCraving cc = CreateValidCookieCraving(GURL(kUrlString), kName, "");
EXPECT_EQ(cc.Name(), kName);
EXPECT_EQ(cc.Domain(), "www.example.test");
EXPECT_EQ(cc.Path(), "/");
EXPECT_EQ(cc.CreationDate(), kCreationTime);
EXPECT_FALSE(cc.SecureAttribute());
EXPECT_FALSE(cc.IsHttpOnly());
EXPECT_EQ(cc.SameSite(), CookieSameSite::UNSPECIFIED);
EXPECT_EQ(cc.PartitionKey(), std::nullopt);
EXPECT_EQ(cc.SourceScheme(), CookieSourceScheme::kSecure);
EXPECT_EQ(cc.SourcePort(), 443);
// Non-default attributes.
cc = CreateValidCookieCraving(
GURL(kUrlString), kName,
"Secure; HttpOnly; Path=/foo; Domain=example.test; SameSite=Lax");
EXPECT_EQ(cc.Name(), kName);
EXPECT_EQ(cc.Domain(), ".example.test");
EXPECT_EQ(cc.Path(), "/foo");
EXPECT_EQ(cc.CreationDate(), kCreationTime);
EXPECT_TRUE(cc.SecureAttribute());
EXPECT_TRUE(cc.IsHttpOnly());
EXPECT_EQ(cc.SameSite(), CookieSameSite::LAX_MODE);
EXPECT_EQ(cc.PartitionKey(), std::nullopt);
EXPECT_EQ(cc.SourceScheme(), CookieSourceScheme::kSecure);
EXPECT_EQ(cc.SourcePort(), 443);
// Normalize whitespace.
cc = CreateValidCookieCraving(
GURL(kUrlString), " name ",
" Secure;HttpOnly;Path = /foo; Domain= example.test; SameSite =Lax ");
EXPECT_EQ(cc.Name(), "name");
EXPECT_EQ(cc.Domain(), ".example.test");
EXPECT_EQ(cc.Path(), "/foo");
EXPECT_EQ(cc.CreationDate(), kCreationTime);
EXPECT_TRUE(cc.SecureAttribute());
EXPECT_TRUE(cc.IsHttpOnly());
EXPECT_EQ(cc.SameSite(), CookieSameSite::LAX_MODE);
EXPECT_EQ(cc.PartitionKey(), std::nullopt);
EXPECT_EQ(cc.SourceScheme(), CookieSourceScheme::kSecure);
EXPECT_EQ(cc.SourcePort(), 443);
}
TEST(CookieCravingTest, CreateWithPartitionKey) {
// The site of the partition key is not checked in Create(), so these two
// should behave the same.
const CookiePartitionKey kSameSitePartitionKey =
CookiePartitionKey::FromURLForTesting(GURL("https://auth.example.test"));
const CookiePartitionKey kCrossSitePartitionKey =
CookiePartitionKey::FromURLForTesting(GURL("https://www.other.test"));
// A key with a nonce might be used for a fenced frame or anonymous iframe.
const CookiePartitionKey kNoncedPartitionKey =
CookiePartitionKey::FromURLForTesting(
GURL("https://www.anonymous-iframe.test"),
CookiePartitionKey::AncestorChainBit::kCrossSite,
base::UnguessableToken::Create());
for (const CookiePartitionKey& partition_key :
{kSameSitePartitionKey, kCrossSitePartitionKey, kNoncedPartitionKey}) {
// Partitioned cookies must be set with Secure. The __Host- prefix is not
// required.
CookieCraving cc =
CreateValidCookieCraving(GURL(kUrlString), kName, "Secure; Partitioned",
kCreationTime, partition_key);
EXPECT_TRUE(cc.SecureAttribute());
EXPECT_TRUE(cc.IsPartitioned());
EXPECT_EQ(cc.PartitionKey(), partition_key);
}
// If a cookie is not set with a Partitioned attribute, the partition key
// should be ignored and cleared (if it's a normal partition key).
for (const CookiePartitionKey& partition_key :
{kSameSitePartitionKey, kCrossSitePartitionKey}) {
CookieCraving cc = CreateValidCookieCraving(
GURL(kUrlString), kName, "Secure", kCreationTime, partition_key);
EXPECT_TRUE(cc.SecureAttribute());
EXPECT_FALSE(cc.IsPartitioned());
EXPECT_EQ(cc.PartitionKey(), std::nullopt);
}
// For nonced partition keys, the Partitioned attribute is not explicitly
// required in order for the cookie to be considered partitioned.
CookieCraving cc = CreateValidCookieCraving(
GURL(kUrlString), kName, "Secure", kCreationTime, kNoncedPartitionKey);
EXPECT_TRUE(cc.SecureAttribute());
EXPECT_TRUE(cc.IsPartitioned());
EXPECT_EQ(cc.PartitionKey(), kNoncedPartitionKey);
// The Secure attribute is also not required for a nonced partition key.
cc = CreateValidCookieCraving(GURL(kUrlString), kName, "", kCreationTime,
kNoncedPartitionKey);
EXPECT_FALSE(cc.SecureAttribute());
EXPECT_TRUE(cc.IsPartitioned());
EXPECT_EQ(cc.PartitionKey(), kNoncedPartitionKey);
}
TEST(CookieCravingTest, CreateWithPrefix) {
// Valid __Host- cookie.
CookieCraving cc = CreateValidCookieCraving(GURL(kUrlString), "__Host-blah",
"Secure; Path=/");
EXPECT_EQ(cc.Domain(), "www.example.test");
EXPECT_EQ(cc.Path(), "/");
EXPECT_TRUE(cc.SecureAttribute());
// Valid __Secure- cookie.
cc = CreateValidCookieCraving(GURL(kUrlString), "__Secure-blah",
"Secure; Path=/foo; Domain=example.test");
EXPECT_TRUE(cc.SecureAttribute());
}
// Test various strange inputs that should still be valid.
TEST(CookieCravingTest, CreateStrange) {
const char* kStrangeNames[] = {
// Empty name is permitted.
"",
// Leading and trailing whitespace should get trimmed.
" name ",
// Internal whitespace is allowed.
"n a m e",
// Trim leading and trailing whitespace while preserving internal
// whitespace.
" n a m e ",
};
for (const char* name : kStrangeNames) {
CookieCraving cc = CreateValidCookieCraving(GURL(kUrlString), name, "");
EXPECT_EQ(cc.Name(), base::TrimWhitespaceASCII(name, base::TRIM_ALL));
}
const char* kStrangeAttributesLines[] = {
// Capitalization.
"SECURE; PATH=/; SAMESITE=LAX",
// Leading semicolon.
"; Secure; Path=/; SameSite=Lax",
// Empty except for semicolons.
";;;",
// Extra whitespace.
" Secure; Path=/; SameSite=Lax ",
// No whitespace.
"Secure;Path=/;SameSite=Lax",
// Domain attribute with leading dot.
"Domain=.example.test",
// Different path from the URL is allowed.
"Path=/different",
// Path not beginning with '/' is allowed. (It's just ignored.)
"Path=noslash",
// Attributes with extraneous values.
"Secure=true; HttpOnly=yes; Partitioned=absolutely",
// Unknown attributes or attribute values.
"Fake=totally; SameSite=SuperStrict",
};
for (const char* attributes : kStrangeAttributesLines) {
CreateValidCookieCraving(GURL(kUrlString), kName, attributes);
}
}
// Another strange/maybe unexpected case is that Create() does not check the
// secureness of the URL against the cookie's Secure attribute. (This is
// documented in the method comment.)
TEST(CookieCravingTest, CreateSecureFromInsecureUrl) {
CookieCraving cc =
CreateValidCookieCraving(GURL("http://insecure.test"), kName, "Secure");
EXPECT_TRUE(cc.SecureAttribute());
EXPECT_EQ(cc.SourceScheme(), CookieSourceScheme::kNonSecure);
}
// Test inputs that should result in a failure to parse the cookie line.
TEST(CookieCravingTest, CreateFailParse) {
const struct {
const char* name;
const char* attributes;
} kParseFailInputs[] = {
// Invalid characters in name.
{"blah\nsomething", "Secure; Path=/"},
{"blah=something", "Secure; Path=/"},
{"blah;something", "Secure; Path=/"},
// Truncated lines are blocked.
{"name", "Secure;\n Path=/"},
};
for (const auto& input : kParseFailInputs) {
std::optional<CookieCraving> cc =
CookieCraving::Create(GURL(kUrlString), input.name, input.attributes,
kCreationTime, std::nullopt);
EXPECT_FALSE(cc);
}
}
// Test cases where the Create() params are not valid.
TEST(CookieCravingTest, CreateFailInvalidParams) {
// Invalid URL.
std::optional<CookieCraving> cc =
CookieCraving::Create(GURL(), kName, "", kCreationTime, std::nullopt);
EXPECT_FALSE(cc);
// Null creation time.
cc = CookieCraving::Create(GURL(kUrlString), kName, "", base::Time(),
std::nullopt);
EXPECT_FALSE(cc);
}
TEST(CookieCravingTest, CreateFailBadDomain) {
// URL does not match domain.
std::optional<CookieCraving> cc =
CookieCraving::Create(GURL(kUrlString), kName, "Domain=other.test",
kCreationTime, std::nullopt);
EXPECT_FALSE(cc);
// Public suffix is not allowed to be Domain attribute.
cc = CookieCraving::Create(GURL(kUrlString), kName, "Domain=test",
kCreationTime, std::nullopt);
EXPECT_FALSE(cc);
// IP addresses cannot set suffixes as the Domain attribute.
cc = CookieCraving::Create(GURL("http://1.2.3.4"), kName, "Domain=2.3.4",
kCreationTime, std::nullopt);
EXPECT_FALSE(cc);
}
TEST(CookieCravingTest, CreateFailBadPartitioned) {
const CookiePartitionKey kPartitionKey =
CookiePartitionKey::FromURLForTesting(GURL("https://example.test"));
// Not Secure.
std::optional<CookieCraving> cc = CookieCraving::Create(
GURL(kUrlString), kName, "Partitioned", kCreationTime, kPartitionKey);
EXPECT_FALSE(cc);
// The URL scheme is not cryptographic.
cc = CookieCraving::Create(GURL("http://example.test"), kName,
"Secure; Partitioned", kCreationTime,
kPartitionKey);
EXPECT_FALSE(cc);
}
TEST(CookieCravingTest, CreateFailInvalidPrefix) {
// __Host- with insecure URL.
std::optional<CookieCraving> cc =
CookieCraving::Create(GURL("http://insecure.test"), "__Host-blah",
"Secure; Path=/", kCreationTime, std::nullopt);
EXPECT_FALSE(cc);
// __Host- with non-Secure cookie.
cc = CookieCraving::Create(GURL(kUrlString), "__Host-blah", "Path=/",
kCreationTime, std::nullopt);
EXPECT_FALSE(cc);
// __Host- with Domain attribute value.
cc = CookieCraving::Create(GURL(kUrlString), "__Host-blah",
"Secure; Path=/; Domain=example.test",
kCreationTime, std::nullopt);
EXPECT_FALSE(cc);
// __Host- with non-root path.
cc = CookieCraving::Create(GURL(kUrlString), "__Host-blah",
"Secure; Path=/foo", kCreationTime, std::nullopt);
EXPECT_FALSE(cc);
// __Secure- with non-Secure cookie.
cc = CookieCraving::Create(GURL(kUrlString), "__Secure-blah", "",
kCreationTime, std::nullopt);
EXPECT_FALSE(cc);
// Prefixes are checked case-insensitively, so these CookieCravings are also
// invalid for not satisfying the prefix requirements.
// Missing Secure.
cc = CookieCraving::Create(GURL(kUrlString), "__host-blah", "Path=/",
kCreationTime, std::nullopt);
EXPECT_FALSE(cc);
// Specifies Domain.
cc = CookieCraving::Create(GURL(kUrlString), "__HOST-blah",
"Secure; Path=/; Domain=example.test",
kCreationTime, std::nullopt);
EXPECT_FALSE(cc);
// Missing Secure.
cc = CookieCraving::Create(GURL(kUrlString), "__SeCuRe-blah", "",
kCreationTime, std::nullopt);
EXPECT_FALSE(cc);
}
// Valid cases were tested as part of the successful Create() tests above, so
// this only tests the invalid cases.
TEST(CookieCravingTest, IsNotValid) {
const struct {
const char* name;
const char* domain;
const char* path;
bool secure;
base::Time creation = kCreationTime;
} kTestCases[] = {
// Invalid name.
{" name", "www.example.test", "/", true},
{";", "www.example.test", "/", true},
{"=", "www.example.test", "/", true},
{"na\nme", "www.example.test", "/", true},
// Empty domain.
{"name", "", "/", true},
// Non-canonical domain.
{"name", "ExAmPlE.test", "/", true},
// Empty path.
{"name", "www.example.test", "", true},
// Path not beginning with slash.
{"name", "www.example.test", "noslash", true},
// Invalid __Host- prefix.
{"__Host-name", ".example.test", "/", true},
{"__Host-name", "www.example.test", "/", false},
{"__Host-name", "www.example.test", "/foo", false},
// Invalid __Secure- prefix.
{"__Secure-name", "www.example.test", "/", false},
// Invalid __Host- prefix (case insensitive).
{"__HOST-name", ".example.test", "/", true},
{"__HoSt-name", "www.example.test", "/", false},
{"__host-name", "www.example.test", "/foo", false},
// Invalid __Secure- prefix (case insensitive).
{"__secure-name", "www.example.test", "/", false},
// Null creation date.
{"name", "www.example.test", "/", true, base::Time()},
};
for (const auto& test_case : kTestCases) {
CookieCraving cc = CookieCraving::CreateUnsafeForTesting(
test_case.name, test_case.domain, test_case.path, test_case.creation,
test_case.secure,
/*httponly=*/false, CookieSameSite::LAX_MODE,
/*partition_key=*/std::nullopt, CookieSourceScheme::kSecure, 443);
SCOPED_TRACE(cc.DebugString());
EXPECT_FALSE(cc.IsValid());
}
// Additionally, Partitioned requires the Secure attribute.
CookieCraving cc = CookieCraving::CreateUnsafeForTesting(
"name", "www.example.test", "/", kCreationTime, /*secure=*/false,
/*httponly=*/false, CookieSameSite::LAX_MODE,
CookiePartitionKey::FromURLForTesting(GURL("https://example.test")),
CookieSourceScheme::kSecure, 443);
EXPECT_FALSE(cc.IsValid());
}
TEST(CookieCravingTest, IsSatisfiedBy) {
// Default case with no attributes.
CanonicalCookie canonical_cookie =
CreateCanonicalCookie(GURL(kUrlString), "name=somevalue");
CookieCraving cookie_craving =
CreateValidCookieCraving(GURL(kUrlString), "name", "");
EXPECT_TRUE(cookie_craving.IsSatisfiedBy(canonical_cookie));
// With attributes.
canonical_cookie =
CreateCanonicalCookie(GURL(kUrlString),
"name=somevalue; Domain=example.test; Path=/; "
"Secure; HttpOnly; SameSite=Lax");
cookie_craving = CreateValidCookieCraving(
GURL(kUrlString), "name",
"Domain=example.test; Path=/; Secure; HttpOnly; SameSite=Lax");
EXPECT_TRUE(cookie_craving.IsSatisfiedBy(canonical_cookie));
// The URL may differ as long as the cookie attributes match.
canonical_cookie = CreateCanonicalCookie(
GURL(kUrlString), "name=somevalue; Domain=example.test");
cookie_craving = CreateValidCookieCraving(
GURL("https://subdomain.example.test"), "name", "Domain=example.test");
EXPECT_TRUE(cookie_craving.IsSatisfiedBy(canonical_cookie));
// Creation time is not required to match.
canonical_cookie = CreateCanonicalCookie(
GURL(kUrlString), "name=somevalue; Domain=example.test", kCreationTime);
cookie_craving =
CreateValidCookieCraving(GURL(kUrlString), "name", "Domain=example.test",
kCreationTime + base::Hours(1));
EXPECT_TRUE(cookie_craving.IsSatisfiedBy(canonical_cookie));
// Source scheme and port (and indeed source host) are not required to match.
canonical_cookie = CreateCanonicalCookie(
GURL(kUrlString), "name=somevalue; Domain=example.test");
cookie_craving =
CreateValidCookieCraving(GURL("http://subdomain.example.test:8080"),
"name", "Domain=example.test");
EXPECT_TRUE(cookie_craving.IsSatisfiedBy(canonical_cookie));
}
TEST(CookieCravingTest, IsNotSatisfiedBy) {
// Name does not match.
CanonicalCookie canonical_cookie =
CreateCanonicalCookie(GURL(kUrlString), "realname=somevalue");
CookieCraving cookie_craving =
CreateValidCookieCraving(GURL(kUrlString), "fakename", "");
EXPECT_FALSE(cookie_craving.IsSatisfiedBy(canonical_cookie));
// Domain does not match.
canonical_cookie = CreateCanonicalCookie(
GURL(kUrlString), "name=somevalue; Domain=example.test");
cookie_craving = CreateValidCookieCraving(GURL(kUrlString), "name",
"Domain=www.example.test");
EXPECT_FALSE(cookie_craving.IsSatisfiedBy(canonical_cookie));
// Host cookie vs domain cookie.
canonical_cookie = CreateCanonicalCookie(GURL(kUrlString), "name=somevalue");
cookie_craving = CreateValidCookieCraving(GURL(kUrlString), "name",
"Domain=www.example.test");
EXPECT_FALSE(cookie_craving.IsSatisfiedBy(canonical_cookie));
// Domain cookie vs host cookie.
canonical_cookie = CreateCanonicalCookie(
GURL(kUrlString), "name=somevalue; Domain=www.example.test");
cookie_craving = CreateValidCookieCraving(GURL(kUrlString), "name", "");
EXPECT_FALSE(cookie_craving.IsSatisfiedBy(canonical_cookie));
// Path does not match.
canonical_cookie = CreateCanonicalCookie(
GURL(kUrlString), "name=somevalue; Domain=example.test; Path=/");
cookie_craving = CreateValidCookieCraving(GURL(kUrlString), "name",
"Domain=example.test; Path=/foo");
EXPECT_FALSE(cookie_craving.IsSatisfiedBy(canonical_cookie));
// Secure vs non-Secure.
canonical_cookie = CreateCanonicalCookie(
GURL(kUrlString), "name=somevalue; Secure; Domain=example.test; Path=/");
cookie_craving = CreateValidCookieCraving(GURL(kUrlString), "name",
"Domain=example.test; Path=/foo");
EXPECT_FALSE(cookie_craving.IsSatisfiedBy(canonical_cookie));
// Non-Secure vs Secure.
canonical_cookie = CreateCanonicalCookie(
GURL(kUrlString), "name=somevalue; Domain=example.test; Path=/");
cookie_craving = CreateValidCookieCraving(
GURL(kUrlString), "name", "Secure; Domain=example.test; Path=/foo");
EXPECT_FALSE(cookie_craving.IsSatisfiedBy(canonical_cookie));
// HttpOnly vs non-HttpOnly.
canonical_cookie = CreateCanonicalCookie(
GURL(kUrlString),
"name=somevalue; HttpOnly; Domain=example.test; Path=/");
cookie_craving = CreateValidCookieCraving(GURL(kUrlString), "name",
"Domain=example.test; Path=/foo");
EXPECT_FALSE(cookie_craving.IsSatisfiedBy(canonical_cookie));
// Non-HttpOnly vs HttpOnly.
canonical_cookie = CreateCanonicalCookie(
GURL(kUrlString), "name=somevalue; Domain=example.test; Path=/");
cookie_craving = CreateValidCookieCraving(
GURL(kUrlString), "name", "HttpOnly; Domain=example.test; Path=/foo");
EXPECT_FALSE(cookie_craving.IsSatisfiedBy(canonical_cookie));
// SameSite does not match.
canonical_cookie =
CreateCanonicalCookie(GURL(kUrlString), "name=somevalue; SameSite=Lax");
cookie_craving =
CreateValidCookieCraving(GURL(kUrlString), "name", "SameSite=Strict");
EXPECT_FALSE(cookie_craving.IsSatisfiedBy(canonical_cookie));
// SameSite vs unspecified SameSite. (Note that the SameSite attribute value
// is compared, not the effective SameSite enforcement mode.)
canonical_cookie =
CreateCanonicalCookie(GURL(kUrlString), "name=somevalue; SameSite=Lax");
cookie_craving = CreateValidCookieCraving(GURL(kUrlString), "name", "");
EXPECT_FALSE(cookie_craving.IsSatisfiedBy(canonical_cookie));
}
TEST(CookieCravingTest, IsSatisfiedByWithPartitionKey) {
const CookiePartitionKey kPartitionKey =
CookiePartitionKey::FromURLForTesting(GURL("https://example.test"));
const CookiePartitionKey kOtherPartitionKey =
CookiePartitionKey::FromURLForTesting(GURL("https://other.test"));
const base::UnguessableToken kNonce = base::UnguessableToken::Create();
const CookiePartitionKey kNoncedPartitionKey =
CookiePartitionKey::FromURLForTesting(
GURL("https://example.test"),
CookiePartitionKey::AncestorChainBit::kCrossSite, kNonce);
// Partition keys match.
CanonicalCookie canonical_cookie = CreateCanonicalCookie(
GURL(kUrlString), "name=somevalue; Secure; Partitioned", kCreationTime,
kPartitionKey);
CookieCraving cookie_craving =
CreateValidCookieCraving(GURL(kUrlString), "name", "Secure; Partitioned",
kCreationTime, kPartitionKey);
EXPECT_TRUE(cookie_craving.IsSatisfiedBy(canonical_cookie));
// Cookie line doesn't specified Partitioned so key gets cleared for both.
canonical_cookie = CreateCanonicalCookie(
GURL(kUrlString), "name=somevalue; Secure", kCreationTime, kPartitionKey);
cookie_craving = CreateValidCookieCraving(GURL(kUrlString), "name", "Secure",
kCreationTime, kOtherPartitionKey);
EXPECT_TRUE(cookie_craving.IsSatisfiedBy(canonical_cookie));
// Without partition key for the CookieCraving, but cookie line doesn't
// specify Partitioned so they are equivalent.
canonical_cookie = CreateCanonicalCookie(
GURL(kUrlString), "name=somevalue; Secure", kCreationTime, kPartitionKey);
cookie_craving = CreateValidCookieCraving(GURL(kUrlString), "name", "Secure");
EXPECT_TRUE(cookie_craving.IsSatisfiedBy(canonical_cookie));
// Without partition key for the CanonicalCookie, but cookie line doesn't
// specify Partitioned so they are equivalent.
canonical_cookie =
CreateCanonicalCookie(GURL(kUrlString), "name=somevalue; Secure");
cookie_craving = CreateValidCookieCraving(GURL(kUrlString), "name", "Secure",
kCreationTime, kPartitionKey);
EXPECT_TRUE(cookie_craving.IsSatisfiedBy(canonical_cookie));
// Identical nonced partition keys.
canonical_cookie =
CreateCanonicalCookie(GURL(kUrlString), "name=somevalue; Secure",
kCreationTime, kNoncedPartitionKey);
cookie_craving = CreateValidCookieCraving(GURL(kUrlString), "name", "Secure",
kCreationTime, kNoncedPartitionKey);
EXPECT_TRUE(cookie_craving.IsSatisfiedBy(canonical_cookie));
}
TEST(CookieCravingTest, IsNotSatisfiedByWithPartitionKey) {
const CookiePartitionKey kPartitionKey =
CookiePartitionKey::FromURLForTesting(GURL("https://example.test"));
const CookiePartitionKey kOtherPartitionKey =
CookiePartitionKey::FromURLForTesting(GURL("https://other.test"));
const base::UnguessableToken kNonce = base::UnguessableToken::Create();
const base::UnguessableToken kOtherNonce = base::UnguessableToken::Create();
const CookiePartitionKey kNoncedPartitionKey =
CookiePartitionKey::FromURLForTesting(
GURL("https://example.test"),
CookiePartitionKey::AncestorChainBit::kCrossSite, kNonce);
const CookiePartitionKey kOtherNoncedPartitionKey =
CookiePartitionKey::FromURLForTesting(
GURL("https://example.test"),
CookiePartitionKey::AncestorChainBit::kCrossSite, kOtherNonce);
// Partition keys do not match.
CanonicalCookie canonical_cookie = CreateCanonicalCookie(
GURL(kUrlString), "name=somevalue; Secure; Partitioned", kCreationTime,
kPartitionKey);
CookieCraving cookie_craving =
CreateValidCookieCraving(GURL(kUrlString), "name", "Secure; Partitioned",
kCreationTime, kOtherPartitionKey);
EXPECT_FALSE(cookie_craving.IsSatisfiedBy(canonical_cookie));
// Nonced partition keys do not match.
canonical_cookie = CreateCanonicalCookie(
GURL(kUrlString), "name=somevalue; Secure; Partitioned", kCreationTime,
kNoncedPartitionKey);
cookie_craving =
CreateValidCookieCraving(GURL(kUrlString), "name", "Secure; Partitioned",
kCreationTime, kOtherNoncedPartitionKey);
EXPECT_FALSE(cookie_craving.IsSatisfiedBy(canonical_cookie));
// Nonced partition key vs regular partition key.
canonical_cookie = CreateCanonicalCookie(
GURL(kUrlString), "name=somevalue; Secure; Partitioned", kCreationTime,
kNoncedPartitionKey);
cookie_craving =
CreateValidCookieCraving(GURL(kUrlString), "name", "Secure; Partitioned",
kCreationTime, kPartitionKey);
EXPECT_FALSE(cookie_craving.IsSatisfiedBy(canonical_cookie));
// Regular partition key vs nonced partition key.
canonical_cookie = CreateCanonicalCookie(
GURL(kUrlString), "name=somevalue; Secure; Partitioned", kCreationTime,
kPartitionKey);
cookie_craving =
CreateValidCookieCraving(GURL(kUrlString), "name", "Secure; Partitioned",
kCreationTime, kNoncedPartitionKey);
EXPECT_FALSE(cookie_craving.IsSatisfiedBy(canonical_cookie));
}
TEST(CookieCravingTest, BasicCookieToFromProto) {
// Default cookie.
CookieCraving cc = CreateValidCookieCraving(GURL(kUrlString), kName, "");
proto::CookieCraving proto = cc.ToProto();
EXPECT_EQ(proto.name(), kName);
EXPECT_EQ(proto.domain(), "www.example.test");
EXPECT_EQ(proto.path(), "/");
EXPECT_EQ(proto.creation_time(),
kCreationTime.ToDeltaSinceWindowsEpoch().InMicroseconds());
EXPECT_FALSE(proto.secure());
EXPECT_FALSE(proto.httponly());
EXPECT_EQ(proto.same_site(),
proto::CookieSameSite::COOKIE_SAME_SITE_UNSPECIFIED);
EXPECT_FALSE(proto.has_serialized_partition_key());
EXPECT_EQ(proto.source_scheme(), proto::CookieSourceScheme::SECURE);
EXPECT_EQ(proto.source_port(), 443);
std::optional<CookieCraving> restored_cc =
CookieCraving::CreateFromProto(proto);
ASSERT_TRUE(restored_cc.has_value());
EXPECT_TRUE(restored_cc->IsEqualForTesting(cc));
// Non-default attributes.
cc = CreateValidCookieCraving(
GURL(kUrlString), kName,
"Secure; HttpOnly; Path=/foo; Domain=example.test; SameSite=Lax");
proto = cc.ToProto();
EXPECT_EQ(proto.name(), kName);
EXPECT_EQ(proto.domain(), ".example.test");
EXPECT_EQ(proto.path(), "/foo");
EXPECT_EQ(proto.creation_time(),
kCreationTime.ToDeltaSinceWindowsEpoch().InMicroseconds());
EXPECT_TRUE(proto.secure());
EXPECT_TRUE(proto.httponly());
EXPECT_EQ(proto.same_site(), proto::CookieSameSite::LAX_MODE);
EXPECT_FALSE(proto.has_serialized_partition_key());
EXPECT_EQ(proto.source_scheme(), proto::CookieSourceScheme::SECURE);
EXPECT_EQ(proto.source_port(), 443);
restored_cc = CookieCraving::CreateFromProto(proto);
ASSERT_TRUE(restored_cc.has_value());
EXPECT_TRUE(restored_cc->IsEqualForTesting(cc));
}
TEST(CookieCravingTest, PartitionedCookieToFromProto) {
const CookiePartitionKey kSameSitePartitionKey =
CookiePartitionKey::FromURLForTesting(GURL("https://auth.example.test"));
const CookiePartitionKey kCrossSitePartitionKey =
CookiePartitionKey::FromURLForTesting(GURL("https://www.other.test"));
for (const CookiePartitionKey& partition_key :
{kSameSitePartitionKey, kCrossSitePartitionKey}) {
// Partitioned cookies must be set with Secure. The __Host- prefix is not
// required.
CookieCraving cc =
CreateValidCookieCraving(GURL(kUrlString), kName, "Secure; Partitioned",
kCreationTime, partition_key);
EXPECT_EQ(cc.PartitionKey(), partition_key);
base::expected<net::CookiePartitionKey::SerializedCookiePartitionKey,
std::string>
serialized_partition_key =
net::CookiePartitionKey::Serialize(partition_key);
CHECK(serialized_partition_key.has_value());
proto::CookieCraving proto = cc.ToProto();
EXPECT_TRUE(proto.secure());
ASSERT_TRUE(proto.has_serialized_partition_key());
EXPECT_EQ(proto.serialized_partition_key().top_level_site(),
serialized_partition_key->TopLevelSite());
EXPECT_EQ(proto.serialized_partition_key().has_cross_site_ancestor(),
serialized_partition_key->has_cross_site_ancestor());
std::optional<CookieCraving> restored_cc =
CookieCraving::CreateFromProto(proto);
ASSERT_TRUE(restored_cc.has_value());
EXPECT_TRUE(restored_cc->IsEqualForTesting(cc));
}
}
TEST(CookieCravingTest, FailCreateFromInvalidProto) {
// Empty proto.
proto::CookieCraving proto;
std::optional<CookieCraving> cc = CookieCraving::CreateFromProto(proto);
EXPECT_FALSE(cc.has_value());
cc = CreateValidCookieCraving(
GURL(kUrlString), kName,
"Secure; HttpOnly; Path=/foo; Domain=example.test; SameSite=Lax");
proto = cc->ToProto();
// Missing parameters.
{
proto::CookieCraving p(proto);
p.clear_name();
std::optional<CookieCraving> c = CookieCraving::CreateFromProto(p);
EXPECT_FALSE(c.has_value());
}
{
proto::CookieCraving p(proto);
p.clear_domain();
std::optional<CookieCraving> c = CookieCraving::CreateFromProto(p);
EXPECT_FALSE(c.has_value());
}
{
proto::CookieCraving p(proto);
p.clear_path();
std::optional<CookieCraving> c = CookieCraving::CreateFromProto(p);
EXPECT_FALSE(c.has_value());
}
{
proto::CookieCraving p(proto);
p.clear_secure();
std::optional<CookieCraving> c = CookieCraving::CreateFromProto(p);
EXPECT_FALSE(c.has_value());
}
{
proto::CookieCraving p(proto);
p.clear_httponly();
std::optional<CookieCraving> c = CookieCraving::CreateFromProto(p);
EXPECT_FALSE(c.has_value());
}
{
proto::CookieCraving p(proto);
p.clear_source_port();
std::optional<CookieCraving> c = CookieCraving::CreateFromProto(p);
EXPECT_FALSE(c.has_value());
}
{
proto::CookieCraving p(proto);
p.clear_creation_time();
std::optional<CookieCraving> c = CookieCraving::CreateFromProto(p);
EXPECT_FALSE(c.has_value());
}
{
proto::CookieCraving p(proto);
p.clear_same_site();
std::optional<CookieCraving> c = CookieCraving::CreateFromProto(p);
EXPECT_FALSE(c.has_value());
}
{
proto::CookieCraving p(proto);
p.clear_source_scheme();
std::optional<CookieCraving> c = CookieCraving::CreateFromProto(p);
EXPECT_FALSE(c.has_value());
}
// Malformed serialized partition key.
{
proto::CookieCraving p(proto);
p.mutable_serialized_partition_key()->set_top_level_site("");
p.mutable_serialized_partition_key()->set_has_cross_site_ancestor(false);
std::optional<CookieCraving> c = CookieCraving::CreateFromProto(p);
EXPECT_FALSE(c.has_value());
}
}
} // namespace net::device_bound_sessions
|