cookie_partition_key.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // 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. #ifndef NET_COOKIES_COOKIE_PARTITION_KEY_H_
  5. #define NET_COOKIES_COOKIE_PARTITION_KEY_H_
  6. #include <string>
  7. #include "net/base/net_export.h"
  8. #include "net/base/network_isolation_key.h"
  9. #include "net/base/schemeful_site.h"
  10. #include "third_party/abseil-cpp/absl/types/optional.h"
  11. #include "url/gurl.h"
  12. namespace net {
  13. class NET_EXPORT CookiePartitionKey {
  14. public:
  15. CookiePartitionKey();
  16. CookiePartitionKey(const CookiePartitionKey& other);
  17. CookiePartitionKey(CookiePartitionKey&& other);
  18. CookiePartitionKey& operator=(const CookiePartitionKey& other);
  19. CookiePartitionKey& operator=(CookiePartitionKey&& other);
  20. ~CookiePartitionKey();
  21. bool operator==(const CookiePartitionKey& other) const;
  22. bool operator!=(const CookiePartitionKey& other) const;
  23. bool operator<(const CookiePartitionKey& other) const;
  24. // Methods for serializing and deserializing a partition key to/from a string.
  25. // This will be used for Android, storing persistent partitioned cookies, and
  26. // loading partitioned cookies into Java code.
  27. //
  28. // This function returns true if the partition key is not opaque and if nonce_
  29. // is not present. We do not want to serialize cookies with opaque origins or
  30. // nonce in their partition key to disk, because if the browser session ends
  31. // we will not be able to attach the saved cookie to any future requests. This
  32. // is because opaque origins' nonces are only stored in volatile memory.
  33. //
  34. // TODO(crbug.com/1225444) Investigate ways to persist partition keys with
  35. // opaque origins if a browser session is restored.
  36. [[nodiscard]] static bool Serialize(
  37. const absl::optional<CookiePartitionKey>& in,
  38. std::string& out);
  39. // Deserializes the result of the method above.
  40. // If the result is absl::nullopt, the resulting cookie is not partitioned.
  41. //
  42. // Returns if the resulting partition key is valid.
  43. [[nodiscard]] static bool Deserialize(
  44. const std::string& in,
  45. absl::optional<CookiePartitionKey>& out);
  46. static CookiePartitionKey FromURLForTesting(
  47. const GURL& url,
  48. const absl::optional<base::UnguessableToken> nonce = absl::nullopt) {
  49. return nonce ? CookiePartitionKey(SchemefulSite(url), nonce)
  50. : CookiePartitionKey(url);
  51. }
  52. // Create a cookie partition key from a request's NetworkIsolationKey.
  53. //
  54. // `first_party_set_owner_site` should be nullptr if the NetworkIsolationKey's
  55. // top-frame site is not in First-Party Set. Otherwise it should be the owner
  56. // site of the top-frame site's set.
  57. static absl::optional<CookiePartitionKey> FromNetworkIsolationKey(
  58. const NetworkIsolationKey& network_isolation_key,
  59. const SchemefulSite* first_party_set_owner_site = nullptr);
  60. // Create a new CookiePartitionKey from the site of an existing
  61. // CookiePartitionKey. This should only be used for sites of partition keys
  62. // which were already created using Deserialize or FromNetworkIsolationKey.
  63. static CookiePartitionKey FromWire(
  64. const SchemefulSite& site,
  65. absl::optional<base::UnguessableToken> nonce = absl::nullopt) {
  66. return CookiePartitionKey(site, nonce);
  67. }
  68. // Create a new CookiePartitionKey in a script running in a renderer. We do
  69. // not trust the renderer to provide us with a cookie partition key, so we let
  70. // the renderer use this method to indicate the cookie is partitioned but the
  71. // key still needs to be determined.
  72. //
  73. // When the browser is ingesting cookie partition keys from the renderer,
  74. // either the `from_script_` flag should be set or the cookie partition key
  75. // should match the browser's. Otherwise the renderer may be compromised.
  76. //
  77. // TODO(crbug.com/1225444) Consider removing this factory method and
  78. // `from_script_` flag when BlinkStorageKey is available in
  79. // ServiceWorkerGlobalScope.
  80. static absl::optional<CookiePartitionKey> FromScript() {
  81. return absl::make_optional(CookiePartitionKey(true));
  82. }
  83. const SchemefulSite& site() const { return site_; }
  84. bool from_script() const { return from_script_; }
  85. // Returns true if the current partition key can be serialized to a string.
  86. // Cookie partition keys whose internal site is opaque cannot be serialized.
  87. bool IsSerializeable() const;
  88. const absl::optional<base::UnguessableToken>& nonce() const { return nonce_; }
  89. static bool HasNonce(const absl::optional<CookiePartitionKey>& key) {
  90. return key && key->nonce();
  91. }
  92. private:
  93. explicit CookiePartitionKey(const SchemefulSite& site,
  94. absl::optional<base::UnguessableToken> nonce);
  95. explicit CookiePartitionKey(const GURL& url);
  96. explicit CookiePartitionKey(bool from_script);
  97. SchemefulSite site_;
  98. bool from_script_ = false;
  99. // Having a nonce is a way to force a transient opaque `CookiePartitionKey`
  100. // for non-opaque origins.
  101. absl::optional<base::UnguessableToken> nonce_;
  102. };
  103. // Used so that CookiePartitionKeys can be the arguments of DCHECK_EQ.
  104. NET_EXPORT std::ostream& operator<<(std::ostream& os,
  105. const CookiePartitionKey& cpk);
  106. } // namespace net
  107. #endif // NET_COOKIES_COOKIE_PARTITION_KEY_H_