File: url_request_rewrite_rules_validation.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 (169 lines) | stat: -rw-r--r-- 5,111 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2021 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/url_rewrite/browser/url_request_rewrite_rules_validation.h"

#include <algorithm>
#include <string_view>

#include "base/strings/strcat.h"
#include "base/strings/string_util.h"
#include "net/http/http_util.h"
#include "url/url_constants.h"

namespace url_rewrite {
namespace {

bool IsValidUrlHost(std::string_view host) {
  return GURL(base::StrCat({url::kHttpScheme, "://", host})).is_valid();
}

bool ValidateAddHeaders(
    const mojom::UrlRequestRewriteAddHeadersPtr& add_headers) {
  if (!add_headers || add_headers->headers.empty()) {
    LOG(ERROR) << "Add headers is missing";
    return false;
  }
  return std::ranges::all_of(
      add_headers->headers, [](const mojom::UrlHeaderPtr& header) {
        if (!net::HttpUtil::IsValidHeaderName(header->name)) {
          LOG(ERROR) << "Invalid header name: " << header->name;
          return false;
        }
        if (!net::HttpUtil::IsValidHeaderValue(header->value)) {
          LOG(ERROR) << "Invalid header value: " << header->value;
          return false;
        }
        return true;
      });
}

bool ValidateRemoveHeader(
    const mojom::UrlRequestRewriteRemoveHeaderPtr& remove_header) {
  if (!remove_header) {
    LOG(ERROR) << "Remove headers is missing";
    return false;
  }
  if (!net::HttpUtil::IsValidHeaderName(remove_header->header_name)) {
    LOG(ERROR) << "Invalid header name: " << remove_header->header_name;
    return false;
  }
  return true;
}

bool ValidateSubstituteQueryPattern(
    const mojom::UrlRequestRewriteSubstituteQueryPatternPtr&
        substitute_query_pattern) {
  if (!substitute_query_pattern) {
    LOG(ERROR) << "Substitute query pattern is missing";
    return false;
  }
  if (substitute_query_pattern->pattern.empty()) {
    LOG(ERROR) << "Substitute query pattern is empty";
    return false;
  }
  return true;
}

bool ValidateReplaceUrl(
    const mojom::UrlRequestRewriteReplaceUrlPtr& replace_url) {
  if (!replace_url) {
    LOG(ERROR) << "Replace url is missing";
    return false;
  }
  if (!GURL("http://site.com/" + replace_url->url_ends_with).is_valid()) {
    LOG(ERROR) << "url_ends_with is not valid: " << replace_url->url_ends_with;
    return false;
  }
  if (!replace_url->new_url.is_valid()) {
    LOG(ERROR) << "new_url is not valid: " << replace_url->new_url;
    return false;
  }
  return true;
}

bool ValidateAppendToQuery(
    const mojom::UrlRequestRewriteAppendToQueryPtr& append_to_query) {
  if (!append_to_query) {
    LOG(ERROR) << "Append to query is missing";
    return false;
  }
  if (append_to_query->query.empty()) {
    LOG(ERROR) << "Append to query is empty";
    return false;
  }
  return true;
}

bool ValidateRewrite(const mojom::UrlRequestActionPtr& action) {
  switch (action->which()) {
    case mojom::UrlRequestAction::Tag::kAddHeaders:
      return ValidateAddHeaders(action->get_add_headers());
    case mojom::UrlRequestAction::Tag::kRemoveHeader:
      return ValidateRemoveHeader(action->get_remove_header());
    case mojom::UrlRequestAction::Tag::kSubstituteQueryPattern:
      return ValidateSubstituteQueryPattern(
          action->get_substitute_query_pattern());
    case mojom::UrlRequestAction::Tag::kReplaceUrl:
      return ValidateReplaceUrl(action->get_replace_url());
    case mojom::UrlRequestAction::Tag::kAppendToQuery:
      return ValidateAppendToQuery(action->get_append_to_query());
    case mojom::UrlRequestAction::Tag::kPolicy:
      return true;
  }
}

}  // namespace

bool ValidateRules(const mojom::UrlRequestRewriteRules* rules) {
  static constexpr std::string_view kWildcard("*.");
  if (!rules)
    return false;
  for (const auto& rule : rules->rules) {
    if (!rule)
      return false;
    if (rule->hosts_filter) {
      if (rule->hosts_filter->empty()) {
        LOG(ERROR) << "Hosts filter is empty";
        return false;
      }
      for (const std::string_view host : *rule->hosts_filter) {
        if (base::StartsWith(host, kWildcard, base::CompareCase::SENSITIVE)) {
          if (!IsValidUrlHost(host.substr(2))) {
            LOG(ERROR) << "Host filter is not valid: " << host;
            return false;
          }
        } else {
          if (!IsValidUrlHost(host)) {
            LOG(ERROR) << "Host filter is not valid: " << host;
            return false;
          }
        }
      }
    }
    if (rule->schemes_filter && rule->schemes_filter->empty()) {
      LOG(ERROR) << "Schemes filter is empty";
      return false;
    }

    if (rule->actions.empty()) {
      // No rewrites, no action = no point!
      LOG(ERROR) << "Actions are empty";
      return false;
    }

    for (const auto& action : rule->actions) {
      if (!action)
        return false;
      if (action->is_policy() && rule->actions.size() > 1)
        // |policy| cannot be combined with other rewrites.
        return false;
      if (!ValidateRewrite(action))
        return false;
    }
  }
  return true;
}

}  // namespace url_rewrite