blob: 16367d7e28f4eaf39eb231b9460025d06e866ffd [file] [log] [blame]
Dylan Cutler27307332021-08-02 21:00:411// Copyright 2021 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "net/cookies/cookie_partition_key.h"
6
Peter Vargaec193052021-12-01 10:25:057#include <tuple>
8
Dylan Cutler39fa227e2021-08-10 22:40:049#include "base/feature_list.h"
10#include "net/base/features.h"
Dylan Cutler27307332021-08-02 21:00:4111#include "net/cookies/cookie_constants.h"
12
13namespace net {
14
15CookiePartitionKey::CookiePartitionKey() = default;
16
shivanigithub7aefb232021-11-18 19:26:5917CookiePartitionKey::CookiePartitionKey(
18 const SchemefulSite& site,
19 absl::optional<base::UnguessableToken> nonce)
20 : site_(site), nonce_(nonce) {}
Dylan Cutler27307332021-08-02 21:00:4121
22CookiePartitionKey::CookiePartitionKey(const GURL& url)
Dylan Cutler402f6c5d42021-11-10 17:31:3323 : site_(SchemefulSite(url)) {}
Dylan Cutler4a461f1b2021-11-03 01:08:3624
25CookiePartitionKey::CookiePartitionKey(bool from_script)
26 : from_script_(from_script) {}
Dylan Cutler27307332021-08-02 21:00:4127
28CookiePartitionKey::CookiePartitionKey(const CookiePartitionKey& other) =
29 default;
30
31CookiePartitionKey::CookiePartitionKey(CookiePartitionKey&& other) = default;
32
33CookiePartitionKey& CookiePartitionKey::operator=(
34 const CookiePartitionKey& other) = default;
35
36CookiePartitionKey& CookiePartitionKey::operator=(CookiePartitionKey&& other) =
37 default;
38
39CookiePartitionKey::~CookiePartitionKey() = default;
40
41bool CookiePartitionKey::operator==(const CookiePartitionKey& other) const {
shivanigithub7aefb232021-11-18 19:26:5942 return site_ == other.site_ && nonce_ == other.nonce_;
Dylan Cutler27307332021-08-02 21:00:4143}
44
Dylan Cutler7f121542021-09-28 20:41:4845bool CookiePartitionKey::operator!=(const CookiePartitionKey& other) const {
shivanigithub7aefb232021-11-18 19:26:5946 return site_ != other.site_ || nonce_ != other.nonce_;
Dylan Cutler7f121542021-09-28 20:41:4847}
48
Dylan Cutler8b928ad2021-08-11 16:24:0949bool CookiePartitionKey::operator<(const CookiePartitionKey& other) const {
shivanigithub7aefb232021-11-18 19:26:5950 return std::tie(site_, nonce_) < std::tie(other.site_, other.nonce_);
Dylan Cutler8b928ad2021-08-11 16:24:0951}
52
Dylan Cutler27307332021-08-02 21:00:4153// static
54bool CookiePartitionKey::Serialize(const absl::optional<CookiePartitionKey>& in,
55 std::string& out) {
56 if (!in) {
57 out = kEmptyCookiePartitionKey;
58 return true;
59 }
Dylan Cutler46cb8a92021-11-17 23:29:5660 if (!in->IsSerializeable())
Dylan Cutlerc68e593e2021-09-15 22:07:2761 return false;
Dylan Cutler46cb8a92021-11-17 23:29:5662 out = in->site_.GetURL().SchemeIsFile()
63 ? in->site_.SerializeFileSiteWithHost()
64 : in->site_.Serialize();
Dylan Cutler27307332021-08-02 21:00:4165 return true;
66}
67
68// static
69bool CookiePartitionKey::Deserialize(const std::string& in,
70 absl::optional<CookiePartitionKey>& out) {
71 if (in == kEmptyCookiePartitionKey) {
72 out = absl::nullopt;
73 return true;
74 }
Dylan Cutlerc68e593e2021-09-15 22:07:2775 if (!base::FeatureList::IsEnabled(features::kPartitionedCookies))
76 return false;
Dylan Cutler27307332021-08-02 21:00:4177 auto schemeful_site = SchemefulSite::Deserialize(in);
78 // SchemfulSite is opaque if the input is invalid.
79 if (schemeful_site.opaque())
80 return false;
shivanigithub7aefb232021-11-18 19:26:5981 out = absl::make_optional(CookiePartitionKey(schemeful_site, absl::nullopt));
Dylan Cutler27307332021-08-02 21:00:4182 return true;
83}
84
Dylan Cutler39fa227e2021-08-10 22:40:0485absl::optional<CookiePartitionKey> CookiePartitionKey::FromNetworkIsolationKey(
86 const NetworkIsolationKey& network_isolation_key) {
shivanigithub7aefb232021-11-18 19:26:5987 if (!base::FeatureList::IsEnabled(features::kPartitionedCookies)) {
Dylan Cutler39fa227e2021-08-10 22:40:0488 return absl::nullopt;
shivanigithub7aefb232021-11-18 19:26:5989 }
90
Dylan Cutler39fa227e2021-08-10 22:40:0491 // TODO(crbug.com/1225444): Check if the top frame site is in a First-Party
92 // Set or if it is an extension URL.
93 absl::optional<net::SchemefulSite> top_frame_site =
94 network_isolation_key.GetTopFrameSite();
95 if (!top_frame_site)
96 return absl::nullopt;
shivanigithub7aefb232021-11-18 19:26:5997
98 absl::optional<base::UnguessableToken> nonce =
99 network_isolation_key.GetNonce();
100 return absl::make_optional(
101 net::CookiePartitionKey(top_frame_site.value(), nonce));
Dylan Cutler39fa227e2021-08-10 22:40:04102}
103
Dylan Cutler46cb8a92021-11-17 23:29:56104bool CookiePartitionKey::IsSerializeable() const {
105 if (!base::FeatureList::IsEnabled(features::kPartitionedCookies))
106 return false;
107 // We should not try to serialize a partition key created by a renderer.
108 DCHECK(!from_script_);
shivanigithub7aefb232021-11-18 19:26:59109 return !site_.opaque() && !nonce_.has_value();
Dylan Cutler46cb8a92021-11-17 23:29:56110}
111
Dylan Cutler27307332021-08-02 21:00:41112} // namespace net