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