File: url_matcher.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (63 lines) | stat: -rw-r--r-- 2,441 bytes parent folder | download | duplicates (6)
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
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/core/loader/url_matcher.h"

#include <string_view>

#include "third_party/blink/renderer/platform/weborigin/security_origin.h"

namespace blink {

UrlMatcher::UrlMatcher(const std::string_view& encoded_url_list_string) {
  ParseFieldTrialParam(encoded_url_list_string);
}

UrlMatcher::~UrlMatcher() = default;

bool UrlMatcher::Match(const KURL& url) const {
  scoped_refptr<const SecurityOrigin> origin = SecurityOrigin::Create(url);
  for (const auto& it : url_list_) {
    // TODO(sisidovski): IsSameOriginWith is more strict but we skip the port
    // number check in order to avoid hardcoding port numbers to corresponding
    // WPT test suites. To check port numbers, we need to set them to the
    // allowlist which is passed by Chrome launch flag or Finch params. But,
    // WPT server could have multiple ports, and it's difficult to expect which
    // ports are available and set to the feature params before starting the
    // test. That will affect the test reliability.
    if ((origin.get()->Protocol() == it.first->Protocol() &&
         origin.get()->Host() == it.first->Host())) {
      // AllowList could only have domain info. In that case the matcher neither
      // cares path nor query strings.
      if (!it.second.has_value())
        return true;
      // Otherwise check if the path or query contains the string.
      if (url.GetPath().ToString().Contains(it.second.value()) ||
          url.Query().ToString().Contains(it.second.value())) {
        return true;
      }
    }
  }

  return false;
}

void UrlMatcher::ParseFieldTrialParam(
    const std::string_view& encoded_url_list_string) {
  Vector<String> parsed_strings;
  String::FromUTF8(encoded_url_list_string)
      .Split(",", /*allow_empty_entries=*/false, parsed_strings);
  Vector<String> site_info;
  for (const auto& it : parsed_strings) {
    it.Split("|", /*allow_empty_entries=*/false, site_info);
    DCHECK_LE(site_info.size(), 2u)
        << "Got unexpected format that UrlMatcher cannot handle: " << it;
    std::optional<String> match_string;
    if (site_info.size() == 2u)
      match_string = site_info[1];
    url_list_.push_back(std::make_pair(
        SecurityOrigin::CreateFromString(site_info[0]), match_string));
  }
}
}  // namespace blink