scheme_host_port_matcher_rule.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright 2020 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_BASE_SCHEME_HOST_PORT_MATCHER_RULE_H_
  5. #define NET_BASE_SCHEME_HOST_PORT_MATCHER_RULE_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/strings/string_piece.h"
  9. #include "net/base/ip_address.h"
  10. #include "net/base/ip_endpoint.h"
  11. #include "net/base/net_export.h"
  12. #include "net/base/scheme_host_port_matcher_result.h"
  13. #include "url/gurl.h"
  14. namespace net {
  15. // Interface for an individual SchemeHostPortMatcher rule.
  16. class NET_EXPORT SchemeHostPortMatcherRule {
  17. public:
  18. SchemeHostPortMatcherRule() = default;
  19. SchemeHostPortMatcherRule(const SchemeHostPortMatcherRule&) = delete;
  20. SchemeHostPortMatcherRule& operator=(const SchemeHostPortMatcherRule&) =
  21. delete;
  22. virtual ~SchemeHostPortMatcherRule() = default;
  23. // Creates a SchemeHostPortMatcherRule by best-effort parsing the string. If
  24. // it can't parse, returns a nullptr. It only parses all the rule types in
  25. // this header file. Types with other serializations will need to be handled
  26. // by the caller.
  27. static std::unique_ptr<SchemeHostPortMatcherRule> FromUntrimmedRawString(
  28. base::StringPiece raw_untrimmed);
  29. // Evaluates the rule against |url|.
  30. virtual SchemeHostPortMatcherResult Evaluate(const GURL& url) const = 0;
  31. // Returns a string representation of this rule. The returned string will not
  32. // match any distinguishable rule of any type.
  33. virtual std::string ToString() const = 0;
  34. // Returns true if |this| is an instance of
  35. // SchemeHostPortMatcherHostnamePatternRule.
  36. virtual bool IsHostnamePatternRule() const;
  37. };
  38. // Rule that matches URLs with wildcard hostname patterns, and
  39. // scheme/port restrictions.
  40. //
  41. // For example:
  42. // *.google.com
  43. // https://*.google.com
  44. // google.com:443
  45. class NET_EXPORT SchemeHostPortMatcherHostnamePatternRule
  46. : public SchemeHostPortMatcherRule {
  47. public:
  48. SchemeHostPortMatcherHostnamePatternRule(const std::string& optional_scheme,
  49. const std::string& hostname_pattern,
  50. int optional_port);
  51. SchemeHostPortMatcherHostnamePatternRule(
  52. const SchemeHostPortMatcherHostnamePatternRule&) = delete;
  53. SchemeHostPortMatcherHostnamePatternRule& operator=(
  54. const SchemeHostPortMatcherHostnamePatternRule&) = delete;
  55. // SchemeHostPortMatcherRule implementation:
  56. SchemeHostPortMatcherResult Evaluate(const GURL& url) const override;
  57. std::string ToString() const override;
  58. bool IsHostnamePatternRule() const override;
  59. // Generates a new SchemeHostPortMatcherHostnamePatternRule based on the
  60. // current rule. The new rule will do suffix matching if the current rule
  61. // doesn't. For example, "google.com" would become "*google.com" and match
  62. // "foogoogle.com".
  63. std::unique_ptr<SchemeHostPortMatcherHostnamePatternRule>
  64. GenerateSuffixMatchingRule() const;
  65. private:
  66. const std::string optional_scheme_;
  67. const std::string hostname_pattern_;
  68. const int optional_port_;
  69. };
  70. // Rule that matches URLs with IP address as hostname, and scheme/port
  71. // restrictions. * only works in the host portion. i18n domain names must be
  72. // input in punycode format.
  73. //
  74. // For example:
  75. // 127.0.0.1,
  76. // http://127.0.0.1
  77. // [::1]
  78. // [0:0::1]
  79. // http://[::1]:99
  80. class NET_EXPORT SchemeHostPortMatcherIPHostRule
  81. : public SchemeHostPortMatcherRule {
  82. public:
  83. SchemeHostPortMatcherIPHostRule(const std::string& optional_scheme,
  84. const IPEndPoint& ip_end_point);
  85. SchemeHostPortMatcherIPHostRule(const SchemeHostPortMatcherIPHostRule&) =
  86. delete;
  87. SchemeHostPortMatcherIPHostRule& operator=(
  88. const SchemeHostPortMatcherIPHostRule&) = delete;
  89. // SchemeHostPortMatcherRule implementation:
  90. SchemeHostPortMatcherResult Evaluate(const GURL& url) const override;
  91. std::string ToString() const override;
  92. private:
  93. const std::string optional_scheme_;
  94. const std::string ip_host_;
  95. const int optional_port_;
  96. };
  97. // Rule for matching a URL that is an IP address, if that IP address falls
  98. // within a certain numeric range.
  99. //
  100. // For example:
  101. // 127.0.0.1/8.
  102. // FE80::/10
  103. // but not http://127.0.0.1:7/8 or http://[FE80::]/10 (IPv6 with brackets).
  104. class NET_EXPORT SchemeHostPortMatcherIPBlockRule
  105. : public SchemeHostPortMatcherRule {
  106. public:
  107. // |ip_prefix| + |prefix_length| define the IP block to match.
  108. SchemeHostPortMatcherIPBlockRule(const std::string& description,
  109. const std::string& optional_scheme,
  110. const IPAddress& ip_prefix,
  111. size_t prefix_length_in_bits);
  112. SchemeHostPortMatcherIPBlockRule(const SchemeHostPortMatcherIPBlockRule&) =
  113. delete;
  114. SchemeHostPortMatcherIPBlockRule& operator=(
  115. const SchemeHostPortMatcherIPBlockRule&) = delete;
  116. // SchemeHostPortMatcherRule implementation:
  117. SchemeHostPortMatcherResult Evaluate(const GURL& url) const override;
  118. std::string ToString() const override;
  119. private:
  120. const std::string description_;
  121. const std::string optional_scheme_;
  122. const IPAddress ip_prefix_;
  123. const size_t prefix_length_in_bits_;
  124. };
  125. } // namespace net
  126. #endif // NET_BASE_SCHEME_HOST_PORT_MATCHER_RULE_H_