File: url_matcher.cc

package info (click to toggle)
chromium 139.0.7258.127-1~deb13u1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,096 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 (63 lines) | stat: -rw-r--r-- 2,441 bytes parent folder | download | duplicates (8)
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