File: origin_matcher.cc

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 (148 lines) | stat: -rw-r--r-- 4,085 bytes parent folder | download | duplicates (3)
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
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/origin_matcher/origin_matcher.h"

#include "base/containers/adapters.h"
#include "base/strings/string_util.h"
#include "components/origin_matcher/origin_matcher_internal.h"
#include "net/base/ip_address.h"
#include "net/base/ip_endpoint.h"
#include "net/base/parse_number.h"
#include "net/base/url_util.h"
#include "url/gurl.h"
#include "url/origin.h"
#include "url/url_constants.h"
#include "url/url_util.h"

namespace origin_matcher {

namespace {

inline int GetDefaultPortForSchemeIfNoPortInfo(const std::string& scheme,
                                               int port) {
  // The input has explicit port information, so don't modify it.
  if (port != -1) {
    return port;
  }

  // Hard code the port for http and https.
  if (scheme == url::kHttpScheme) {
    return 80;
  }
  if (scheme == url::kHttpsScheme) {
    return 443;
  }

  return port;
}

}  // namespace

OriginMatcher::OriginMatcher() = default;
// Allow copy and assign.
OriginMatcher::OriginMatcher(OriginMatcher&&) = default;
OriginMatcher& OriginMatcher::operator=(OriginMatcher&&) = default;

OriginMatcher::~OriginMatcher() = default;

OriginMatcher::OriginMatcher(const OriginMatcher& rhs) {
  *this = rhs;
}

OriginMatcher& OriginMatcher::operator=(const OriginMatcher& rhs) {
  rules_.clear();
  for (const auto& rule : rhs.Serialize()) {
    AddRuleFromString(rule);
  }
  return *this;
}

void OriginMatcher::SetRules(RuleList rules) {
  rules_.swap(rules);
}

void OriginMatcher::MoveRules(OriginMatcher& matcher) {
  rules_ = std::move(matcher.rules_);
}

bool OriginMatcher::AddRuleFromString(const std::string& raw_untrimmed) {
  std::string raw;
  base::TrimWhitespaceASCII(raw_untrimmed, base::TRIM_ALL, &raw);

  if (raw == "*") {
    rules_.push_back(std::make_unique<MatchAllOriginsRule>());
    return true;
  }

  // Extract scheme-restriction.
  std::string::size_type scheme_pos = raw.find("://");
  if (scheme_pos == std::string::npos) {
    return false;
  }

  const std::string scheme = raw.substr(0, scheme_pos);
  if (!SubdomainMatchingRule::IsValidScheme(scheme)) {
    return false;
  }

  std::string host_and_port = raw.substr(scheme_pos + 3);
  if (host_and_port.empty()) {
    if (!SubdomainMatchingRule::IsValidSchemeAndHost(scheme, std::string())) {
      return false;
    }
    rules_.push_back(
        std::make_unique<SubdomainMatchingRule>(scheme, std::string(), -1));
    return true;
  }

  std::string host;
  int port;
  if (!net::ParseHostAndPort(host_and_port, &host, &port) ||
      !SubdomainMatchingRule::IsValidSchemeAndHost(scheme, host)) {
    return false;
  }

  // Check if we have an <ip-address>[:port] input and try to canonicalize the
  // IP literal.
  net::IPAddress ip_address;
  if (ip_address.AssignFromIPLiteral(host)) {
    port = GetDefaultPortForSchemeIfNoPortInfo(scheme, port);
    host = ip_address.ToString();
    if (ip_address.IsIPv6()) {
      host = '[' + host + ']';
    }
    rules_.push_back(
        std::make_unique<SubdomainMatchingRule>(scheme, host, port));
    return true;
  }

  port = GetDefaultPortForSchemeIfNoPortInfo(scheme, port);
  rules_.push_back(std::make_unique<SubdomainMatchingRule>(scheme, host, port));
  return true;
}

bool OriginMatcher::Matches(const url::Origin& origin) const {
  GURL origin_url = origin.GetURL();
  // Since we only do kInclude vs kNoMatch, the order doesn't actually matter.
  for (const std::unique_ptr<OriginMatcherRule>& rule :
       base::Reversed(rules_)) {
    net::SchemeHostPortMatcherResult result = rule->Evaluate(origin_url);
    if (result == net::SchemeHostPortMatcherResult::kInclude) {
      return true;
    }
  }
  return false;
}

std::vector<std::string> OriginMatcher::Serialize() const {
  std::vector<std::string> result;
  result.reserve(rules_.size());
  for (const auto& rule : rules_) {
    result.push_back(rule->ToString());
  }
  return result;
}

}  // namespace origin_matcher