File: stateful_ssl_host_state_delegate.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (184 lines) | stat: -rw-r--r-- 7,893 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_SECURITY_INTERSTITIALS_CONTENT_STATEFUL_SSL_HOST_STATE_DELEGATE_H_
#define COMPONENTS_SECURITY_INTERSTITIALS_CONTENT_STATEFUL_SSL_HOST_STATE_DELEGATE_H_

#include <memory>
#include <set>
#include <string>

#include "base/memory/raw_ptr.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/security_interstitials/core/https_only_mode_allowlist.h"
#include "components/security_interstitials/core/https_only_mode_enforcelist.h"
#include "content/public/browser/ssl_host_state_delegate.h"
#include "url/gurl.h"

class HostContentSettingsMap;
class PrefService;

namespace base {
class Clock;
class Value;
class FilePath;
}  //  namespace base

namespace content {
class BrowserContext;
class StoragePartition;
}  // namespace content

// Tracks state related to certificate and SSL errors. This state includes:
// - certificate error exceptions
// - mixed content exceptions
class StatefulSSLHostStateDelegate : public content::SSLHostStateDelegate,
                                     public KeyedService {
 public:
  StatefulSSLHostStateDelegate(
      content::BrowserContext* browser_context,
      HostContentSettingsMap* host_content_settings_map);

  StatefulSSLHostStateDelegate(const StatefulSSLHostStateDelegate&) = delete;
  StatefulSSLHostStateDelegate& operator=(const StatefulSSLHostStateDelegate&) =
      delete;

  ~StatefulSSLHostStateDelegate() override;

  // content::SSLHostStateDelegate overrides:
  void AllowCert(const std::string& host,
                 const net::X509Certificate& cert,
                 int error,
                 content::StoragePartition* storage_partition) override;
  void Clear(
      base::RepeatingCallback<bool(const std::string&)> host_filter) override;
  CertJudgment QueryPolicy(
      const std::string& host,
      const net::X509Certificate& cert,
      int error,
      content::StoragePartition* storage_partition) override;

  void HostRanInsecureContent(const std::string& host,
                              InsecureContentType content_type) override;
  bool DidHostRunInsecureContent(const std::string& host,
                                 InsecureContentType content_type) override;

  void AllowHttpForHost(const std::string& host,
                        content::StoragePartition* storage_partition) override;
  bool IsHttpAllowedForHost(
      const std::string& host,
      content::StoragePartition* storage_partition) override;
  void RevokeUserAllowExceptions(const std::string& host) override;
  bool HasAllowException(const std::string& host,
                         content::StoragePartition* storage_partition) override;
  // Returns true if the user has allowed a certificate error exception or HTTP
  // exception for any host.
  bool HasAllowExceptionForAnyHost(
      content::StoragePartition* storage_partition) override;

  void SetHttpsEnforcementForHost(
      const std::string& host,
      bool enforced,
      content::StoragePartition* storage_partition) override;
  bool IsHttpsEnforcedForUrl(
      const GURL& url,
      content::StoragePartition* storage_partition) override;
  std::set<GURL> GetHttpsEnforcedHosts(
      content::StoragePartition* storage_partition) const;

  // Clears all entries from the HTTP allowlist.
  void ClearHttpsOnlyModeAllowlist();
  // Clear all entries from the HTTPS enforcelist.
  void ClearHttpsEnforcelist();

  // RevokeUserAllowExceptionsHard is the same as RevokeUserAllowExceptions but
  // additionally may close idle connections in the process. This should be used
  // *only* for rare events, such as a user controlled button, as it may be very
  // disruptive to the networking stack.
  virtual void RevokeUserAllowExceptionsHard(const std::string& host);

  bool HttpsFirstBalancedModeSuppressedForTesting();
  void SetHttpsFirstBalancedModeSuppressedForTesting(bool suppressed);

  // SetClockForTesting takes ownership of the passed in clock.
  void SetClockForTesting(std::unique_ptr<base::Clock> clock);

  // Returns whether the user has allowed a certificate error exception for
  // |host|.
  bool HasCertAllowException(const std::string& host,
                             content::StoragePartition* storage_partition);

  // Returns whether the user has allowed an HTTP exception for |host|.
  bool HasHttpAllowException(const std::string& host,
                             content::StoragePartition* storage_partition);

 private:
  // Used to specify whether new content setting entries should be created if
  // they don't already exist when querying the user's settings.
  enum CreateDictionaryEntriesDisposition {
    CREATE_DICTIONARY_ENTRIES,
    DO_NOT_CREATE_DICTIONARY_ENTRIES
  };

  // Returns a dictionary of certificate fingerprints and errors that have been
  // allowed as exceptions by the user.
  //
  // |dict| specifies the user's full exceptions dictionary for a specific site
  // in their content settings. Must be retrieved directly from a website
  // setting in |host_content_settings_map_|.
  //
  // If |create_entries| specifies CreateDictionaryEntries, then
  // GetValidCertDecisionsDict will create a new set of entries within the
  // dictionary if they do not already exist. Otherwise will fail and return if
  // NULL if they do not exist.
  base::Value::Dict* GetValidCertDecisionsDict(
      CreateDictionaryEntriesDisposition create_entries,
      base::Value::Dict& dict);

  bool HasCertAllowExceptionForAnyHost(
      content::StoragePartition* storage_partition);
  bool IsHttpAllowedForAnyHost(content::StoragePartition* storage_partition);

  std::unique_ptr<base::Clock> clock_;
  raw_ptr<content::BrowserContext> browser_context_;
  raw_ptr<PrefService> pref_service_;
  raw_ptr<HostContentSettingsMap> host_content_settings_map_;

  using AllowedCert = std::pair<std::string /* certificate fingerprint */,
                                base::FilePath /* StoragePartition path */>;

  // Typically, cert decisions are stored in ContentSettings and persisted to
  // disk. For non-default StoragePartitions, particularly a <webview> in a
  // Chrome App, the decisions should be isolated from normal browsing and don't
  // need to be persisted to disk. In fact, persisting them is undesirable
  // because they may not have UI exposed to the user when a certificate error
  // is bypassed. So we track these decisions purely in memory. See
  // https://crbug.com/639173.
  std::map<std::string /* host */, std::set<AllowedCert>>
      allowed_certs_for_non_default_storage_partitions_;

  // Hosts which have been contaminated with insecure mixed content. Running
  // insecure content is remembered for a host but not persisted across browser
  // restarts.
  std::set<std::string> ran_mixed_content_hosts_;

  // Hosts which have been contaminated with content with certificate errors.
  std::set<std::string> ran_content_with_cert_errors_hosts_;

  // Tracks sites that are allowed to load over HTTP when HTTPS-First Mode is
  // enabled. Allowed hosts are exact hostname matches -- subdomains of a host
  // on the allowlist must be separately allowlisted.
  security_interstitials::HttpsOnlyModeAllowlist https_only_mode_allowlist_;

  // Tracks sites that are not allowed to load over HTTP when HTTPS-First Mode
  // is enabled. Enforced hosts are exact hostname matches -- subdomains of a
  // host on the enforcelist must be separately added.
  // The allowlist takes precedence over enforcelist. If a site is in both
  // lists, it's allowed to load over HTTP.
  security_interstitials::HttpsOnlyModeEnforcelist https_only_mode_enforcelist_;

  bool https_first_balanced_mode_suppressed_for_testing;
};

#endif  // COMPONENTS_SECURITY_INTERSTITIALS_CONTENT_STATEFUL_SSL_HOST_STATE_DELEGATE_H_