transport_security_persister.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright (c) 2012 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. // TransportSecurityState maintains an in memory database containing the
  5. // list of hosts that currently have transport security enabled. This
  6. // singleton object deals with writing that data out to disk as needed and
  7. // loading it at startup.
  8. // At startup we need to load the transport security state from the
  9. // disk. For the moment, we don't want to delay startup for this load, so we
  10. // let the TransportSecurityState run for a while without being loaded.
  11. // This means that it's possible for pages opened very quickly not to get the
  12. // correct transport security information.
  13. //
  14. // To load the state, we schedule a Task on background_runner, which
  15. // deserializes and configures the TransportSecurityState.
  16. //
  17. // The TransportSecurityState object supports running a callback function
  18. // when it changes. This object registers the callback, pointing at itself.
  19. //
  20. // TransportSecurityState calls...
  21. // TransportSecurityPersister::StateIsDirty
  22. // since the callback isn't allowed to block or reenter, we schedule a Task
  23. // on the file task runner after some small amount of time
  24. //
  25. // ...
  26. //
  27. // TransportSecurityPersister::SerializeState
  28. // copies the current state of the TransportSecurityState, serializes
  29. // and writes to disk.
  30. #ifndef NET_HTTP_TRANSPORT_SECURITY_PERSISTER_H_
  31. #define NET_HTTP_TRANSPORT_SECURITY_PERSISTER_H_
  32. #include <string>
  33. #include "base/files/file_path.h"
  34. #include "base/files/important_file_writer.h"
  35. #include "base/memory/raw_ptr.h"
  36. #include "base/memory/ref_counted.h"
  37. #include "base/memory/weak_ptr.h"
  38. #include "net/base/net_export.h"
  39. #include "net/http/transport_security_state.h"
  40. namespace base {
  41. class SequencedTaskRunner;
  42. }
  43. namespace net {
  44. // Reads and updates on-disk TransportSecurity state. Clients of this class
  45. // should create, destroy, and call into it from one thread.
  46. //
  47. // background_runner is the task runner this class should use internally to
  48. // perform file IO, and can optionally be associated with a different thread.
  49. class NET_EXPORT TransportSecurityPersister
  50. : public TransportSecurityState::Delegate,
  51. public base::ImportantFileWriter::DataSerializer {
  52. public:
  53. // Create a TransportSecurityPersister with state |state| on background runner
  54. // |background_runner|. |data_path| points to the file to hold the transport
  55. // security state data on disk.
  56. TransportSecurityPersister(
  57. TransportSecurityState* state,
  58. const scoped_refptr<base::SequencedTaskRunner>& background_runner,
  59. const base::FilePath& data_path);
  60. TransportSecurityPersister(const TransportSecurityPersister&) = delete;
  61. TransportSecurityPersister& operator=(const TransportSecurityPersister&) =
  62. delete;
  63. ~TransportSecurityPersister() override;
  64. // Called by the TransportSecurityState when it changes its state.
  65. void StateIsDirty(TransportSecurityState*) override;
  66. // Called when the TransportSecurityState should be written immediately.
  67. // |callback| is called after data is persisted.
  68. void WriteNow(TransportSecurityState* state,
  69. base::OnceClosure callback) override;
  70. // ImportantFileWriter::DataSerializer:
  71. //
  72. // Serializes |transport_security_state_| into |*output|. Returns true if
  73. // all STS and Expect_CT states were serialized correctly.
  74. //
  75. // The serialization format is JSON; the JSON represents a dictionary of
  76. // host:DomainState pairs (host is a string). The DomainState contains
  77. // the STS and Expect-CT states and is represented as a dictionary containing
  78. // the following keys and value types (not all keys will always be
  79. // present):
  80. //
  81. // "sts_include_subdomains": true|false
  82. // "created": double
  83. // "expiry": double
  84. // "mode": "default"|"force-https"
  85. // legacy value synonyms "strict" = "force-https"
  86. // "pinning-only" = "default"
  87. // legacy value "spdy-only" is unused and ignored
  88. // "report-uri": string
  89. // "sts_observed": double
  90. // "expect_ct": dictionary with keys:
  91. // "expect_ct_expiry": double
  92. // "expect_ct_observed": double
  93. // "expect_ct_enforce": true|false
  94. // "expect_ct_report_uri": string
  95. //
  96. // The JSON dictionary keys are strings containing
  97. // Base64(SHA256(TransportSecurityState::CanonicalizeHost(domain))).
  98. // The reason for hashing them is so that the stored state does not
  99. // trivially reveal a user's browsing history to an attacker reading the
  100. // serialized state on disk.
  101. bool SerializeData(std::string* data) override;
  102. // Clears any existing non-static entries, and then re-populates
  103. // |transport_security_state_|.
  104. void LoadEntries(const std::string& serialized);
  105. private:
  106. // Populates |state| from the JSON string |serialized|.
  107. static void Deserialize(const std::string& serialized,
  108. TransportSecurityState* state);
  109. void CompleteLoad(const std::string& state);
  110. void OnWriteFinished(base::OnceClosure callback);
  111. raw_ptr<TransportSecurityState> transport_security_state_;
  112. // Helper for safely writing the data.
  113. base::ImportantFileWriter writer_;
  114. scoped_refptr<base::SequencedTaskRunner> foreground_runner_;
  115. scoped_refptr<base::SequencedTaskRunner> background_runner_;
  116. base::WeakPtrFactory<TransportSecurityPersister> weak_ptr_factory_{this};
  117. };
  118. } // namespace net
  119. #endif // NET_HTTP_TRANSPORT_SECURITY_PERSISTER_H_