File: content_settings_pattern.h

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 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 (331 lines) | stat: -rw-r--r-- 12,019 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Patterns used in content setting rules.

#ifndef COMPONENTS_CONTENT_SETTINGS_CORE_COMMON_CONTENT_SETTINGS_PATTERN_H_
#define COMPONENTS_CONTENT_SETTINGS_CORE_COMMON_CONTENT_SETTINGS_PATTERN_H_

#include <memory>
#include <string>
#include <string_view>

#include "base/gtest_prod_util.h"
#include "mojo/public/cpp/bindings/struct_traits.h"

class GURL;

namespace content_settings {
class PatternParser;

namespace mojom {
class ContentSettingsPatternDataView;
}
}  // namespace content_settings

// A pattern used in content setting rules. See |IsValid| for a description of
// possible patterns.
class ContentSettingsPattern {
 public:
  // Each content settings pattern describes a set of origins. Patterns, and the
  // sets they describe, have specific relations. |Relation| describes the
  // relation of two patterns A and B. When pattern A is compared with pattern B
  // (A compare B) interesting relations are:
  // - IDENTITY:
  //   Pattern A and B are identical. The patterns are equal.
  //
  // - DISJOINT_ORDER_PRE:
  //   Pattern A and B have no intersection. A and B never match the origin of
  //   a URL at the same time. But pattern A has a higher precedence than
  //   pattern B when patterns are sorted.
  //
  // - DISJOINT_ORDER_POST:
  //   Pattern A and B have no intersection. A and B never match the origin of
  //   a URL at the same time. But pattern A has a lower precedence than
  //   pattern B when patterns are sorted.
  //
  // - SUCCESSOR:
  //   Pattern A and B have an intersection. But pattern B has a higher
  //   precedence than pattern A for URLs that are matched by both pattern.
  //
  // - PREDECESSOR:
  //   Pattern A and B have an intersection. But pattern A has a higher
  //   precedence than pattern B for URLs that are matched by both pattern.
  //
  //  See the url below for more details about pattern precedence.
  //  https://developer.chrome.com/docs/extensions/reference/api/contentSettings#content_setting_patterns
  //
  enum Relation {
    DISJOINT_ORDER_POST = -2,
    SUCCESSOR = -1,
    IDENTITY = 0,
    PREDECESSOR = 1,
    DISJOINT_ORDER_PRE = 2,
  };

  // This enum is used to back an UMA histogram, the order of existing values
  // should not be changed.
  // New values should only be appended before SCHEME_MAX.
  // Also keep it consistent with kSchemeNames in content_settings_pattern.cc,
  // and the ContentSettingScheme enum in histograms/enums.xml.
  enum SchemeType {
    SCHEME_WILDCARD,
    SCHEME_OTHER,
    SCHEME_HTTP,
    SCHEME_HTTPS,
    SCHEME_FILE,
    SCHEME_CHROMEEXTENSION,
    SCHEME_CHROMESEARCH,
    SCHEME_CHROME,
    SCHEME_CHROMEUNTRUSTED,
    SCHEME_DEVTOOLS,
    SCHEME_ISOLATEDAPP,
    SCHEME_MAX,
  };

  // These values are persisted to logs. Entries should not be renumbered and
  // numeric values should never be reused.
  //
  // LINT.IfChange(Scope)
  enum class Scope {
    kOriginScoped = 0,                        // https://example.com:443
    kWithDomainWildcard = 1,                  // https://[*.]example.com:443
    kWithPortWildcard = 2,                    // https://example.com
    kWithSchemeWildcard = 3,                  // example.com:443
    kWithSchemeAndPortWildcard = 4,           // example.com
    kWithDomainAndPortWildcard = 5,           // https://[*.]example.com
    kWithDomainAndSchemeWildcard = 6,         // [*.]example.com:443
    kWithDomainAndSchemeAndPortWildcard = 7,  // [*.]example.com
    kFullWildcard = 8,                        // * (default values)
    kFilePath = 9,                            // file:///tmp/index.html
    kCustomScope = 10,                        // everything else: https://*,
                                              // *:443 etc.
    kMaxValue = kCustomScope
  };
  // LINT.ThenChange(//tools/metrics/histograms/metadata/privacy/enums.xml:ContentSettingPatternScope)

  struct PatternParts {
    PatternParts();
    PatternParts(const PatternParts& other);
    PatternParts(PatternParts&& other);
    ~PatternParts();

    PatternParts& operator=(const PatternParts& other);
    PatternParts& operator=(PatternParts&& other);

    bool operator==(const PatternParts& other) const;

    // Lowercase string of the URL scheme to match. This string is empty if the
    // |is_scheme_wildcard| flag is set.
    std::string scheme;

    // True if the scheme wildcard is set.
    bool is_scheme_wildcard;

    // Normalized string that is either of the following:
    // - IPv4 or IPv6
    // - hostname
    // - domain
    // - empty string if the |is_host_wildcard| flag is set.
    std::string host;

    // True if the domain wildcard is set.
    bool has_domain_wildcard;

    // String with the port to match. This string is empty if the
    // |is_port_wildcard| flag is set.
    std::string port;

    // True if the port wildcard is set.
    bool is_port_wildcard;

    // TODO(markusheintz): Needed for legacy reasons. Remove. Path
    // specification. Only used for content settings pattern with a "file"
    // scheme part.
    std::string path;

    // True if the path wildcard is set.
    bool is_path_wildcard;
  };

  class BuilderInterface {
   public:
    virtual ~BuilderInterface() = default;

    virtual BuilderInterface* WithPort(const std::string& port) = 0;

    virtual BuilderInterface* WithPortWildcard() = 0;

    virtual BuilderInterface* WithHost(const std::string& host) = 0;

    virtual BuilderInterface* WithDomainWildcard() = 0;

    virtual BuilderInterface* WithScheme(const std::string& scheme) = 0;

    virtual BuilderInterface* WithSchemeWildcard() = 0;

    virtual BuilderInterface* WithPath(const std::string& path) = 0;

    virtual BuilderInterface* WithPathWildcard() = 0;

    virtual BuilderInterface* Invalid() = 0;

    // Returns a content settings pattern according to the current configuration
    // of the builder.
    virtual ContentSettingsPattern Build() = 0;
  };

  static std::unique_ptr<BuilderInterface> CreateBuilder();

  // The version of the pattern format implemented.
  static const int kContentSettingsPatternVersion;

  // Returns a wildcard content settings pattern that matches all possible valid
  // origins.
  static ContentSettingsPattern Wildcard();

  // Returns a pattern that matches the scheme and host of this URL, as well as
  // all subdomains and ports.
  static ContentSettingsPattern FromURL(const GURL& url);

  // Returns a pattern that matches exactly this URL. (Paths are ignored for
  // non-"file://" URLs.)
  static ContentSettingsPattern FromURLNoWildcard(const GURL& url);

  // Converts a given url to a ContentSettingsPattern that represents a site,
  // i.e. with domain, path, and port wildcards.
  static ContentSettingsPattern FromURLToSchemefulSitePattern(const GURL& url);

  // Returns a pattern that matches the given pattern specification.
  // Valid patterns specifications are:
  //   - [*.]domain.tld (matches domain.tld and all sub-domains)
  //   - host (matches an exact hostname)
  //   - scheme://host:port (supported schemes: http,https)
  //   - scheme://[*.]domain.tld:port (supported schemes: http,https)
  //   - file://path (The path has to be an absolute path and start with a '/')
  //   - a.b.c.d (matches an exact IPv4 ip)
  //   - [a:b:c:d:e:f:g:h] (matches an exact IPv6 ip)
  static ContentSettingsPattern FromString(std::string_view pattern_spec);

  // Sets schemes that do not support domain wildcards and ports.
  // Needs to be called by the embedder before using ContentSettingsPattern.
  // |schemes| can't be NULL, and the pointed to strings must remain alive
  // until the app terminates.
  // The method should only be called once. If called again, the parameters
  // must have values equal to the parameter values of the first call.
  // The |count| parameter represents the number of strings that
  // |schemes| points to.
  static void SetNonWildcardDomainNonPortSchemes(const char* const* schemes,
                                                 size_t count);

  // Compares |scheme| against the schemes set by the embedder.
  static bool IsNonWildcardDomainNonPortScheme(std::string_view scheme);

  // Convert pattern to domain wildcard pattern. If fail to extract domain from
  // the pattern, return an invalid pattern.
  static ContentSettingsPattern ToDomainWildcardPattern(
      const ContentSettingsPattern& pattern);

  // Convert pattern to host only pattern.
  static ContentSettingsPattern ToHostOnlyPattern(
      const ContentSettingsPattern& pattern);

  // Expose a comparator to sort domains by precedence. Highest precedence
  // first. Returns true if |domain_a| has a higher precedence than |domain_b|.
  // If there is no difference in precedence, then the domains are compared
  // alphabetically.
  struct CompareDomains {
    using is_transparent = void;
    bool operator()(std::string_view domain_a, std::string_view domain_b) const;
  };

  // Constructs an empty pattern. Empty patterns are invalid patterns. Invalid
  // patterns match nothing.
  ContentSettingsPattern();

  // True if this is a valid pattern.
  bool IsValid() const { return is_valid_; }

  // True if |url| matches this pattern.
  bool Matches(const GURL& url) const;

  // True if this pattern matches all hosts (i.e. it has a host wildcard).
  bool MatchesAllHosts() const;

  // True if this pattern matches a single origin (i.e. it's the narrowest kind
  // of a pattern, with no wildcards).
  bool MatchesSingleOrigin() const;

  // True if this pattern has domain wildcard.
  bool HasDomainWildcard() const;

  // Returns a std::string representation of this pattern.
  std::string ToString() const;

  // Returns a valid URL that matches the pattern if a host part is specified.
  // If the pattern matches a file:// scheme, the path needs to be specified.
  // Returns GURL() otherwise.
  GURL ToRepresentativeUrl() const;

  // Returns scheme type of pattern.
  ContentSettingsPattern::SchemeType GetScheme() const;

  // Returns the host of a pattern.
  const std::string& GetHost() const;

  // Returns the scope of the pattern (based on the wildcards in the pattern).
  Scope GetScope() const;

  // Compares the pattern with a given |other| pattern and returns the
  // |Relation| of the two patterns.
  Relation Compare(const ContentSettingsPattern& other) const;

  friend bool operator==(const ContentSettingsPattern& a,
                         const ContentSettingsPattern& b) {
    return a.Compare(b) == IDENTITY;
  }

  friend auto operator<=>(const ContentSettingsPattern& a,
                          const ContentSettingsPattern& b) {
    return a.Compare(b) <=> IDENTITY;
  }

  // Formatter method for Google Test
  friend void PrintTo(const ContentSettingsPattern& pattern, std::ostream* os) {
    *os << pattern.ToString();
  }

 private:
  friend class content_settings::PatternParser;
  friend struct mojo::StructTraits<
      content_settings::mojom::ContentSettingsPatternDataView,
      ContentSettingsPattern>;
  FRIEND_TEST_ALL_PREFIXES(ContentSettingsPatternParserTest, SerializePatterns);

  class Builder;

  static Relation CompareScheme(
      const ContentSettingsPattern::PatternParts& parts,
      const ContentSettingsPattern::PatternParts& other_parts);

  static Relation CompareHost(
      const ContentSettingsPattern::PatternParts& parts,
      const ContentSettingsPattern::PatternParts& other_parts);

  static Relation ComparePort(
      const ContentSettingsPattern::PatternParts& parts,
      const ContentSettingsPattern::PatternParts& other_parts);

  static Relation ComparePath(
      const ContentSettingsPattern::PatternParts& parts,
      const ContentSettingsPattern::PatternParts& other_parts);

  ContentSettingsPattern(PatternParts parts, bool valid);

  PatternParts parts_;

  bool is_valid_;
};

#endif  // COMPONENTS_CONTENT_SETTINGS_CORE_COMMON_CONTENT_SETTINGS_PATTERN_H_