Commit aa9d555e authored by Shuran Huang's avatar Shuran Huang Committed by Chromium LUCI CQ

Add enum class SamePartyCookieContextType into CookieOptions.

Add enum class object SamePartyCookieContextType into CookieOptions,
as well as one field to store the enum and one field to store the size
of the full party_context set.

Bug: 1136102
Change-Id: I2e72d0d973097ad6f993f8ecc0d979bd4167f328
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2570123
Commit-Queue: Shuran Huang <shuuran@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarLily Chen <chlily@chromium.org>
Reviewed-by: default avatarMatt Menke <mmenke@chromium.org>
Cr-Commit-Position: refs/heads/master@{#835678}
parent c3cbad38
......@@ -65,6 +65,8 @@ CookieOptions CookieOptions::MakeAllInclusive() {
options.set_same_site_cookie_context(SameSiteCookieContext::MakeInclusive());
options.set_do_not_update_access_time();
options.set_full_party_context(std::set<SchemefulSite>());
options.set_same_party_cookie_context_type(
SamePartyCookieContextType::kSameParty);
return options;
}
......
......@@ -86,6 +86,16 @@ class NET_EXPORT CookieOptions {
ContextType schemeful_context_;
};
// Computed in URLRequestHttpJob for every cookie access attempt but is only
// relevant for SameParty cookies.
enum class SamePartyCookieContextType {
// The opposite to kSameParty. Should be the default value.
kCrossParty = 0,
// If the request URL is in the same First-Party Sets as the top-frame site
// and each member of the isolation_info.party_context.
kSameParty = 1,
};
// Creates a CookieOptions object which:
//
// * Excludes HttpOnly cookies
......@@ -100,6 +110,7 @@ class NET_EXPORT CookieOptions {
// * |set_same_site_cookie_context()|
// * |set_do_not_update_access_time()|
// * |set_full_party_context()|
// * |set_same_party_cookie_context_type()|
CookieOptions();
CookieOptions(const CookieOptions& other);
CookieOptions(CookieOptions&& other);
......@@ -138,6 +149,22 @@ class NET_EXPORT CookieOptions {
return full_party_context_;
}
// How trusted is the current browser environment when it comes to accessing
// SameParty cookies. Default is not trusted, e.g. kCrossParty.
void set_same_party_cookie_context_type(
SamePartyCookieContextType context_type) {
same_party_cookie_context_type_ = context_type;
}
SamePartyCookieContextType same_party_cookie_context_type() const {
return same_party_cookie_context_type_;
}
// Getter/setter of |full_party_context_size_| for logging purposes.
void set_full_party_context_size(uint32_t len) {
full_party_context_size_ = len;
}
uint32_t full_party_context_size() const { return full_party_context_size_; }
// Convenience method for where you need a CookieOptions that will
// work for getting/setting all types of cookies, including HttpOnly and
// SameSite cookies. Also specifies not to update the access time, because
......@@ -152,6 +179,12 @@ class NET_EXPORT CookieOptions {
bool update_access_time_;
bool return_excluded_cookies_ = false;
base::Optional<std::set<SchemefulSite>> full_party_context_;
SamePartyCookieContextType same_party_cookie_context_type_ =
SamePartyCookieContextType::kCrossParty;
// The size of the isolation_info.party_context plus the top-frame site.
// Stored for logging purposes.
uint32_t full_party_context_size_ = 0;
};
} // namespace net
......
......@@ -723,7 +723,7 @@ void URLRequestHttpJob::SaveCookiesAndNotifyHeadersComplete(int result) {
continue;
}
request_->context()->cookie_store()->SetCanonicalCookieAsync(
cookie_store->SetCanonicalCookieAsync(
std::move(cookie), request_->url(), options,
base::BindOnce(&URLRequestHttpJob::OnSetCookieResult,
weak_factory_.GetWeakPtr(), options, cookie_to_return,
......
......@@ -320,6 +320,35 @@ bool StructTraits<network::mojom::CookieSameSiteContextDataView,
return true;
}
bool EnumTraits<network::mojom::SamePartyCookieContextType,
net::CookieOptions::SamePartyCookieContextType>::
FromMojom(network::mojom::SamePartyCookieContextType context_type,
net::CookieOptions::SamePartyCookieContextType* out) {
switch (context_type) {
case network::mojom::SamePartyCookieContextType::kCrossParty:
*out = net::CookieOptions::SamePartyCookieContextType::kCrossParty;
return true;
case network::mojom::SamePartyCookieContextType::kSameParty:
*out = net::CookieOptions::SamePartyCookieContextType::kSameParty;
return true;
}
return false;
}
network::mojom::SamePartyCookieContextType
EnumTraits<network::mojom::SamePartyCookieContextType,
net::CookieOptions::SamePartyCookieContextType>::
ToMojom(net::CookieOptions::SamePartyCookieContextType context_type) {
switch (context_type) {
case net::CookieOptions::SamePartyCookieContextType::kCrossParty:
return network::mojom::SamePartyCookieContextType::kCrossParty;
case net::CookieOptions::SamePartyCookieContextType::kSameParty:
return network::mojom::SamePartyCookieContextType::kSameParty;
}
NOTREACHED();
return network::mojom::SamePartyCookieContextType::kCrossParty;
}
bool StructTraits<network::mojom::CookieOptionsDataView, net::CookieOptions>::
Read(network::mojom::CookieOptionsDataView mojo_options,
net::CookieOptions* cookie_options) {
......@@ -355,6 +384,16 @@ bool StructTraits<network::mojom::CookieOptionsDataView, net::CookieOptions>::
}
cookie_options->set_full_party_context(full_party_context);
net::CookieOptions::SamePartyCookieContextType same_party_cookie_context_type;
if (!mojo_options.ReadSamePartyCookieContextType(
&same_party_cookie_context_type))
return false;
cookie_options->set_same_party_cookie_context_type(
same_party_cookie_context_type);
cookie_options->set_full_party_context_size(
mojo_options.full_party_context_size());
return true;
}
......
......@@ -99,6 +99,16 @@ struct StructTraits<network::mojom::CookieSameSiteContextDataView,
net::CookieOptions::SameSiteCookieContext* context);
};
template <>
struct EnumTraits<network::mojom::SamePartyCookieContextType,
net::CookieOptions::SamePartyCookieContextType> {
static network::mojom::SamePartyCookieContextType ToMojom(
net::CookieOptions::SamePartyCookieContextType context_type);
static bool FromMojom(network::mojom::SamePartyCookieContextType context_type,
net::CookieOptions::SamePartyCookieContextType* out);
};
template <>
struct StructTraits<network::mojom::CookieOptionsDataView, net::CookieOptions> {
static bool exclude_httponly(const net::CookieOptions& o) {
......@@ -120,6 +130,15 @@ struct StructTraits<network::mojom::CookieOptionsDataView, net::CookieOptions> {
return o.full_party_context();
}
static net::CookieOptions::SamePartyCookieContextType
same_party_cookie_context_type(const net::CookieOptions& o) {
return o.same_party_cookie_context_type();
}
static uint32_t full_party_context_size(const net::CookieOptions& o) {
return o.full_party_context_size();
}
static bool Read(network::mojom::CookieOptionsDataView mojo_options,
net::CookieOptions* cookie_options);
};
......
......@@ -307,12 +307,25 @@ TEST(CookieManagerTraitsTest, Roundtrips_CookieSameSiteContext) {
}
}
TEST(CookieManagerTraitsTest, Roundtrips_SamePartyCookieContextType) {
using ContextType = net::CookieOptions::SamePartyCookieContextType;
for (ContextType context_type :
{ContextType::kCrossParty, ContextType::kSameParty}) {
ContextType roundtrip;
ASSERT_TRUE(
mojo::test::SerializeAndDeserialize<mojom::SamePartyCookieContextType>(
context_type, roundtrip));
EXPECT_EQ(context_type, roundtrip);
}
}
TEST(CookieManagerTraitsTest, Roundtrips_CookieOptions) {
{
net::CookieOptions least_trusted, copy;
EXPECT_FALSE(least_trusted.return_excluded_cookies());
least_trusted.set_return_excluded_cookies(); // differ from default.
least_trusted.set_full_party_context_size(10u);
EXPECT_TRUE(mojo::test::SerializeAndDeserialize<mojom::CookieOptions>(
least_trusted, copy));
......@@ -322,6 +335,9 @@ TEST(CookieManagerTraitsTest, Roundtrips_CookieOptions) {
net::CookieOptions::SameSiteCookieContext::ContextType::CROSS_SITE),
copy.same_site_cookie_context());
EXPECT_TRUE(copy.return_excluded_cookies());
EXPECT_EQ(net::CookieOptions::SamePartyCookieContextType::kCrossParty,
copy.same_party_cookie_context_type());
EXPECT_EQ(10u, copy.full_party_context_size());
}
{
......@@ -332,6 +348,9 @@ TEST(CookieManagerTraitsTest, Roundtrips_CookieOptions) {
very_trusted.set_same_site_cookie_context(
net::CookieOptions::SameSiteCookieContext::MakeInclusive());
very_trusted.set_full_party_context(kPartyContext);
very_trusted.set_same_party_cookie_context_type(
net::CookieOptions::SamePartyCookieContextType::kSameParty);
very_trusted.set_full_party_context_size(kPartyContext.size());
EXPECT_TRUE(mojo::test::SerializeAndDeserialize<mojom::CookieOptions>(
very_trusted, copy));
......@@ -340,6 +359,9 @@ TEST(CookieManagerTraitsTest, Roundtrips_CookieOptions) {
copy.same_site_cookie_context());
EXPECT_FALSE(copy.return_excluded_cookies());
EXPECT_EQ(kPartyContext, copy.full_party_context());
EXPECT_EQ(net::CookieOptions::SamePartyCookieContextType::kSameParty,
copy.same_party_cookie_context_type());
EXPECT_EQ(1u, copy.full_party_context_size());
}
}
......
......@@ -102,6 +102,16 @@ struct CookieSameSiteContext {
ContextType schemeful_context = CROSS_SITE;
};
// Computed for every cookie access attempt but is only relevant for SameParty
// cookies.
enum SamePartyCookieContextType {
// The opposite to kSameParty. Should be the default value.
kCrossParty,
// If the request URL is in the same First-Party Sets as the top-frame site
// and each member of the isolation_info.party_context.
kSameParty,
};
// What rules to apply when determining whether access to a particular cookie is
// allowed.
// Keep in sync with net/cookies/cookie_constants.h.
......@@ -118,6 +128,10 @@ struct CookieOptions {
bool update_access_time = true;
bool return_excluded_cookies = false;
array<SchemefulSite>? full_party_context;
SamePartyCookieContextType same_party_cookie_context_type = kCrossParty;
// The size of the isolation_info.party_context plus the top-frame site for
// logging purposes.
uint32 full_party_context_size = 0;
};
// See net/cookies/canonical_cookie.{h,cc} for documentation.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment