network_error_logging_service.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. // Copyright 2017 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_NETWORK_ERROR_LOGGING_NETWORK_ERROR_LOGGING_SERVICE_H_
  5. #define NET_NETWORK_ERROR_LOGGING_NETWORK_ERROR_LOGGING_SERVICE_H_
  6. #include <map>
  7. #include <memory>
  8. #include <set>
  9. #include <string>
  10. #include <vector>
  11. #include "base/feature_list.h"
  12. #include "base/memory/raw_ptr.h"
  13. #include "base/time/clock.h"
  14. #include "base/time/time.h"
  15. #include "net/base/ip_address.h"
  16. #include "net/base/net_errors.h"
  17. #include "net/base/net_export.h"
  18. #include "net/base/network_isolation_key.h"
  19. #include "url/gurl.h"
  20. #include "url/origin.h"
  21. namespace base {
  22. class Value;
  23. } // namespace base
  24. namespace net {
  25. class ReportingService;
  26. } // namespace net
  27. namespace url {
  28. class Origin;
  29. } // namespace url
  30. namespace features {
  31. extern const base::Feature NET_EXPORT kNetworkErrorLogging;
  32. } // namespace features
  33. namespace net {
  34. class NET_EXPORT NetworkErrorLoggingService {
  35. public:
  36. class PersistentNelStore;
  37. // Every (NIK, origin) pair can have at most one policy.
  38. struct NET_EXPORT NelPolicyKey {
  39. NelPolicyKey();
  40. NelPolicyKey(const NetworkIsolationKey& network_isolation_key,
  41. const url::Origin& origin);
  42. NelPolicyKey(const NelPolicyKey& other);
  43. ~NelPolicyKey();
  44. bool operator<(const NelPolicyKey& other) const;
  45. bool operator==(const NelPolicyKey& other) const;
  46. bool operator!=(const NelPolicyKey& other) const;
  47. // The NIK of the request this policy was received from. This will be used
  48. // for any requests uploading reports according to this policy. (Not
  49. // included in the report itself.)
  50. NetworkIsolationKey network_isolation_key;
  51. url::Origin origin;
  52. };
  53. // Used for wildcard policies that are applicable to |domain| and its
  54. // subdomains.
  55. struct WildcardNelPolicyKey {
  56. WildcardNelPolicyKey();
  57. WildcardNelPolicyKey(const NetworkIsolationKey& network_isolation_key,
  58. const std::string& domain);
  59. explicit WildcardNelPolicyKey(const NelPolicyKey& origin_key);
  60. WildcardNelPolicyKey(const WildcardNelPolicyKey& other);
  61. ~WildcardNelPolicyKey();
  62. bool operator<(const WildcardNelPolicyKey& other) const;
  63. // The NIK of the request this policy was received from. This will be used
  64. // for any requests uploading reports according to this policy. (Not
  65. // included in the report itself.)
  66. NetworkIsolationKey network_isolation_key;
  67. std::string domain;
  68. };
  69. // NEL policy set by an origin.
  70. struct NET_EXPORT NelPolicy {
  71. NelPolicy();
  72. NelPolicy(const NelPolicy& other);
  73. ~NelPolicy();
  74. NelPolicyKey key;
  75. IPAddress received_ip_address = IPAddress();
  76. // Reporting API endpoint group to which reports should be sent.
  77. std::string report_to;
  78. base::Time expires;
  79. double success_fraction = 0.0;
  80. double failure_fraction = 1.0;
  81. bool include_subdomains = false;
  82. // Last time the policy was accessed to create a report, even if no report
  83. // ends up being queued. Also updated when the policy is first set.
  84. mutable base::Time last_used;
  85. };
  86. // The details of a network error that are included in an NEL report.
  87. //
  88. // See http://wicg.github.io/network-error-logging/#dfn-network-error-object
  89. // for details on the semantics of each field.
  90. struct NET_EXPORT RequestDetails {
  91. RequestDetails();
  92. RequestDetails(const RequestDetails& other);
  93. ~RequestDetails();
  94. // NetworkIsolationKey of the request triggering the error. Not included
  95. // in the uploaded report.
  96. NetworkIsolationKey network_isolation_key;
  97. GURL uri;
  98. GURL referrer;
  99. std::string user_agent;
  100. IPAddress server_ip;
  101. std::string protocol;
  102. std::string method;
  103. int status_code;
  104. base::TimeDelta elapsed_time;
  105. Error type;
  106. // Upload nesting depth of this request.
  107. //
  108. // If the request is not a Reporting upload, the depth is 0.
  109. //
  110. // If the request is a Reporting upload, the depth is the max of the depth
  111. // of the requests reported within it plus 1. (Non-NEL reports are
  112. // considered to have depth 0.)
  113. int reporting_upload_depth;
  114. };
  115. // The details of a signed exchange report.
  116. struct NET_EXPORT SignedExchangeReportDetails {
  117. SignedExchangeReportDetails();
  118. SignedExchangeReportDetails(const SignedExchangeReportDetails& other);
  119. ~SignedExchangeReportDetails();
  120. // NetworkIsolationKey of the request triggering the error. Not included
  121. // in the uploaded report.
  122. NetworkIsolationKey network_isolation_key;
  123. bool success;
  124. std::string type;
  125. GURL outer_url;
  126. GURL inner_url;
  127. GURL cert_url;
  128. std::string referrer;
  129. IPAddress server_ip_address;
  130. std::string protocol;
  131. std::string method;
  132. int32_t status_code;
  133. base::TimeDelta elapsed_time;
  134. std::string user_agent;
  135. };
  136. static const char kHeaderName[];
  137. static const char kReportType[];
  138. static const int kMaxNestedReportDepth;
  139. // Keys for data included in report bodies. Exposed for tests.
  140. static const char kReferrerKey[];
  141. static const char kSamplingFractionKey[];
  142. static const char kServerIpKey[];
  143. static const char kProtocolKey[];
  144. static const char kMethodKey[];
  145. static const char kStatusCodeKey[];
  146. static const char kElapsedTimeKey[];
  147. static const char kPhaseKey[];
  148. static const char kTypeKey[];
  149. static const char kSignedExchangePhaseValue[];
  150. static const char kSignedExchangeBodyKey[];
  151. static const char kOuterUrlKey[];
  152. static const char kInnerUrlKey[];
  153. static const char kCertUrlKey[];
  154. // Maximum number of NEL policies to store before evicting.
  155. static const size_t kMaxPolicies;
  156. static const char kSignedExchangeRequestOutcomeHistogram[];
  157. // Used for histogramming Signed Exchange request outcomes only. Previously,
  158. // the outcome of all requests would be histogrammed, but this was removed in
  159. // crbug.com/1007122 because the histogram was very large and not very useful.
  160. enum class RequestOutcome {
  161. kDiscardedNoNetworkErrorLoggingService = 0,
  162. kDiscardedNoReportingService = 1,
  163. kDiscardedInsecureOrigin = 2,
  164. kDiscardedNoOriginPolicy = 3,
  165. kDiscardedUnmappedError = 4,
  166. kDiscardedReportingUpload = 5,
  167. kDiscardedUnsampledSuccess = 6,
  168. kDiscardedUnsampledFailure = 7,
  169. kQueued = 8,
  170. kDiscardedNonDNSSubdomainReport = 9,
  171. kDiscardedIPAddressMismatch = 10,
  172. kMaxValue = kDiscardedIPAddressMismatch
  173. };
  174. // NEL policies are persisted to disk if |store| is not null.
  175. // The store, if given, should outlive |*this|.
  176. static std::unique_ptr<NetworkErrorLoggingService> Create(
  177. PersistentNelStore* store);
  178. NetworkErrorLoggingService(const NetworkErrorLoggingService&) = delete;
  179. NetworkErrorLoggingService& operator=(const NetworkErrorLoggingService&) =
  180. delete;
  181. virtual ~NetworkErrorLoggingService();
  182. // Ingests a "NEL:" header received for |network_isolation_key| and |origin|
  183. // from |received_ip_address| with normalized value |value|. May or may not
  184. // actually set a policy for that origin.
  185. virtual void OnHeader(const NetworkIsolationKey& network_isolation_key,
  186. const url::Origin& origin,
  187. const IPAddress& received_ip_address,
  188. const std::string& value) = 0;
  189. // Considers queueing a network error report for the request described in
  190. // |details|. The contents of |details| might be changed, depending on the
  191. // NEL policy associated with the request's origin. Note that |details| is
  192. // passed by value, so that it doesn't need to be copied in this function if
  193. // it needs to be changed. Consider using std::move to pass this parameter if
  194. // the caller doesn't need to access it after this method call.
  195. //
  196. // Note that Network Error Logging can report a fraction of successful
  197. // requests as well (to calculate error rates), so this should be called on
  198. // *all* secure requests. NEL is only available to secure origins, so this is
  199. // not called on any insecure requests.
  200. virtual void OnRequest(RequestDetails details) = 0;
  201. // Queues a Signed Exchange report.
  202. virtual void QueueSignedExchangeReport(
  203. SignedExchangeReportDetails details) = 0;
  204. // Removes browsing data (origin policies) associated with any origin for
  205. // which |origin_filter| returns true.
  206. virtual void RemoveBrowsingData(
  207. const base::RepeatingCallback<bool(const url::Origin&)>&
  208. origin_filter) = 0;
  209. // Removes browsing data (origin policies) for all origins. Allows slight
  210. // optimization over passing an always-true filter to RemoveBrowsingData.
  211. virtual void RemoveAllBrowsingData() = 0;
  212. // Sets the ReportingService that will be used to queue network error reports.
  213. // If |nullptr| is passed, reports will be queued locally or discarded.
  214. // |reporting_service| must outlive the NetworkErrorLoggingService.
  215. // Should not be called again if previously called with a non-null pointer.
  216. void SetReportingService(ReportingService* reporting_service);
  217. // Shuts down the NEL service, so that no more requests or headers can be
  218. // processed, no more reports are queued, and browsing data can no longer be
  219. // cleared.
  220. void OnShutdown();
  221. // Sets a base::Clock (used to track policy expiration) for tests.
  222. // |clock| must outlive the NetworkErrorLoggingService, and cannot be
  223. // nullptr.
  224. void SetClockForTesting(const base::Clock* clock);
  225. // Dumps info about all the currently stored policies, including expired ones.
  226. // Used to display information about NEL policies on the NetLog Reporting tab.
  227. virtual base::Value StatusAsValue() const;
  228. // Gets the (NIK, origin) keys of all currently stored policies, including
  229. // expired ones.
  230. virtual std::set<NelPolicyKey> GetPolicyKeysForTesting();
  231. virtual PersistentNelStore* GetPersistentNelStoreForTesting();
  232. virtual ReportingService* GetReportingServiceForTesting();
  233. protected:
  234. NetworkErrorLoggingService();
  235. // Unowned:
  236. raw_ptr<const base::Clock> clock_;
  237. raw_ptr<ReportingService> reporting_service_ = nullptr;
  238. bool shut_down_ = false;
  239. };
  240. // Persistent storage for NEL policies.
  241. class NET_EXPORT NetworkErrorLoggingService::PersistentNelStore {
  242. public:
  243. using NelPoliciesLoadedCallback =
  244. base::OnceCallback<void(std::vector<NelPolicy>)>;
  245. PersistentNelStore() = default;
  246. PersistentNelStore(const PersistentNelStore&) = delete;
  247. PersistentNelStore& operator=(const PersistentNelStore&) = delete;
  248. virtual ~PersistentNelStore() = default;
  249. // Initializes the store and retrieves stored NEL policies. This will be
  250. // called only once at startup.
  251. virtual void LoadNelPolicies(NelPoliciesLoadedCallback loaded_callback) = 0;
  252. // Adds a NEL policy to the store.
  253. virtual void AddNelPolicy(const NelPolicy& policy) = 0;
  254. // Updates the access time of the NEL policy in the store.
  255. virtual void UpdateNelPolicyAccessTime(const NelPolicy& policy) = 0;
  256. // Deletes a NEL policy from the store.
  257. virtual void DeleteNelPolicy(const NelPolicy& policy) = 0;
  258. // Flushes the store.
  259. virtual void Flush() = 0;
  260. };
  261. } // namespace net
  262. #endif // NET_NETWORK_ERROR_LOGGING_NETWORK_ERROR_LOGGING_SERVICE_H_