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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/common/url_pattern_set.h"
#include <iterator>
#include <ostream>
#include "base/containers/contains.h"
#include "base/logging.h"
#include "base/stl_util.h"
#include "extensions/common/error_utils.h"
#include "extensions/common/url_pattern.h"
#include "url/gurl.h"
#include "url/origin.h"
#include "url/url_constants.h"
namespace extensions {
namespace {
const char kInvalidURLPatternError[] = "Invalid url pattern '*'";
} // namespace
// static
URLPatternSet URLPatternSet::CreateDifference(const URLPatternSet& set1,
const URLPatternSet& set2) {
return URLPatternSet(base::STLSetDifference<std::set<URLPattern>>(
set1.patterns_, set2.patterns_));
}
// static
URLPatternSet URLPatternSet::CreateIntersection(
const URLPatternSet& set1,
const URLPatternSet& set2,
IntersectionBehavior intersection_behavior) {
// Note: leverage return value optimization; always return the same object.
URLPatternSet result;
if (intersection_behavior == IntersectionBehavior::kStringComparison) {
// String comparison just relies on STL set behavior, which looks at the
// string representation.
result = URLPatternSet(base::STLSetIntersection<std::set<URLPattern>>(
set1.patterns_, set2.patterns_));
return result;
}
// Look for a semantic intersection.
// Step 1: Iterate over each set. Find any patterns that are completely
// contained by the other (thus being necessarily present in any intersection)
// and add them, collecting the others in a set of unique items.
// Note: Use a collection of pointers for the uniques to avoid excessive
// copies. Since these are owned by the URLPatternSet passed in, which is
// const, this should be safe.
std::vector<const URLPattern*> unique_set1;
for (const URLPattern& pattern : set1) {
if (set2.ContainsPattern(pattern)) {
result.patterns_.insert(pattern);
} else {
unique_set1.push_back(&pattern);
}
}
std::vector<const URLPattern*> unique_set2;
for (const URLPattern& pattern : set2) {
if (set1.ContainsPattern(pattern)) {
result.patterns_.insert(pattern);
} else {
unique_set2.push_back(&pattern);
}
}
// If we're just looking for patterns contained by both, we're done.
if (intersection_behavior == IntersectionBehavior::kPatternsContainedByBoth) {
return result;
}
DCHECK_EQ(IntersectionBehavior::kDetailed, intersection_behavior);
// Step 2: Iterate over all the unique patterns and find the intersections
// they have with the other patterns.
for (const auto* pattern : unique_set1) {
for (const auto* pattern2 : unique_set2) {
std::optional<URLPattern> intersection =
pattern->CreateIntersection(*pattern2);
if (intersection) {
result.patterns_.insert(std::move(*intersection));
}
}
}
return result;
}
// static
URLPatternSet URLPatternSet::CreateUnion(const URLPatternSet& set1,
const URLPatternSet& set2) {
return URLPatternSet(
base::STLSetUnion<std::set<URLPattern>>(set1.patterns_, set2.patterns_));
}
URLPatternSet::URLPatternSet() = default;
URLPatternSet::URLPatternSet(URLPatternSet&& rhs) = default;
URLPatternSet::URLPatternSet(const std::set<URLPattern>& patterns)
: patterns_(patterns) {}
URLPatternSet::~URLPatternSet() = default;
URLPatternSet& URLPatternSet::operator=(URLPatternSet&& rhs) = default;
bool URLPatternSet::operator==(const URLPatternSet& other) const {
return patterns_ == other.patterns_;
}
std::ostream& operator<<(std::ostream& out,
const URLPatternSet& url_pattern_set) {
out << "{ ";
auto iter = url_pattern_set.patterns().cbegin();
if (!url_pattern_set.patterns().empty()) {
out << *iter;
++iter;
}
for (; iter != url_pattern_set.patterns().end(); ++iter) {
out << ", " << *iter;
}
if (!url_pattern_set.patterns().empty()) {
out << " ";
}
out << "}";
return out;
}
URLPatternSet URLPatternSet::Clone() const {
return URLPatternSet(patterns_);
}
bool URLPatternSet::is_empty() const {
return patterns_.empty();
}
size_t URLPatternSet::size() const {
return patterns_.size();
}
bool URLPatternSet::AddPattern(const URLPattern& pattern) {
return patterns_.insert(pattern).second;
}
void URLPatternSet::AddPatterns(const URLPatternSet& set) {
patterns_.insert(set.patterns().begin(),
set.patterns().end());
}
void URLPatternSet::ClearPatterns() {
patterns_.clear();
}
bool URLPatternSet::AddOrigin(int valid_schemes, const GURL& origin) {
if (origin.is_empty()) {
return false;
}
const url::Origin real_origin = url::Origin::Create(origin);
DCHECK(real_origin.IsSameOriginWith(
url::Origin::Create(origin.DeprecatedGetOriginAsURL())));
// TODO(devlin): Implement this in terms of the `AddOrigin()` call that takes
// an url::Origin? It's interesting because this doesn't currently supply an
// extra path, so if the GURL has not path ("https://example.com"), it would
// fail to add - which is probably a bug.
URLPattern origin_pattern(valid_schemes);
// Origin adding could fail if |origin| does not match |valid_schemes|.
if (origin_pattern.Parse(origin.spec()) !=
URLPattern::ParseResult::kSuccess) {
return false;
}
origin_pattern.SetPath("/*");
return AddPattern(origin_pattern);
}
bool URLPatternSet::AddOrigin(int valid_schemes, const url::Origin& origin) {
DCHECK(!origin.opaque());
URLPattern origin_pattern(valid_schemes);
// Origin adding could fail if |origin| does not match |valid_schemes|.
std::string string_pattern = origin.Serialize() + "/*";
if (origin_pattern.Parse(string_pattern) !=
URLPattern::ParseResult::kSuccess) {
return false;
}
return AddPattern(origin_pattern);
}
bool URLPatternSet::Contains(const URLPatternSet& other) const {
for (const auto& it : other) {
if (!ContainsPattern(it)) {
return false;
}
}
return true;
}
bool URLPatternSet::ContainsPattern(const URLPattern& pattern) const {
for (const auto& it : *this) {
if (it.Contains(pattern)) {
return true;
}
}
return false;
}
bool URLPatternSet::MatchesURL(const GURL& url) const {
for (const auto& pattern : patterns_) {
if (pattern.MatchesURL(url)) {
return true;
}
}
return false;
}
bool URLPatternSet::MatchesAllURLs() const {
for (const auto& host : *this) {
if (host.match_all_urls() ||
(host.match_subdomains() && host.host().empty())) {
return true;
}
}
return false;
}
bool URLPatternSet::MatchesHost(const GURL& test,
bool require_match_subdomains) const {
if (!test.is_valid()) {
return false;
}
return std::any_of(
patterns_.begin(), patterns_.end(),
[&test, require_match_subdomains](const URLPattern& pattern) {
return pattern.MatchesHost(test) &&
(!require_match_subdomains || pattern.match_subdomains());
});
}
bool URLPatternSet::MatchesSecurityOrigin(const GURL& origin) const {
for (const auto& pattern : patterns_) {
if (pattern.MatchesSecurityOrigin(origin)) {
return true;
}
}
return false;
}
bool URLPatternSet::OverlapsWith(const URLPatternSet& other) const {
// Two extension extents overlap if there is any one URL that would match at
// least one pattern in each of the extents.
for (const auto& pattern : patterns_) {
for (const auto& j : other.patterns()) {
if (pattern.OverlapsWith(j)) {
return true;
}
}
}
return false;
}
base::Value::List URLPatternSet::ToValue() const {
base::Value::List result;
for (const auto& pattern : patterns_) {
base::Value pattern_str_value(pattern.GetAsString());
if (!base::Contains(result, pattern_str_value)) {
result.Append(std::move(pattern_str_value));
}
}
return result;
}
bool URLPatternSet::Populate(const std::vector<std::string>& patterns,
int valid_schemes,
bool allow_file_access,
std::string* error) {
ClearPatterns();
for (const auto& i : patterns) {
URLPattern pattern(valid_schemes);
if (pattern.Parse(i) != URLPattern::ParseResult::kSuccess) {
if (error) {
*error = ErrorUtils::FormatErrorMessage(kInvalidURLPatternError, i);
} else {
LOG(ERROR) << "Invalid url pattern: " << i;
}
return false;
}
if (!allow_file_access && pattern.MatchesScheme(url::kFileScheme)) {
pattern.SetValidSchemes(
pattern.valid_schemes() & ~URLPattern::SCHEME_FILE);
}
AddPattern(pattern);
}
return true;
}
std::vector<std::string> URLPatternSet::ToStringVector() const {
std::vector<std::string> result;
for (const auto& pattern : patterns_) {
result.push_back(pattern.GetAsString());
}
return result;
}
bool URLPatternSet::Populate(const base::Value::List& value,
int valid_schemes,
bool allow_file_access,
std::string* error) {
std::vector<std::string> patterns;
for (const base::Value& pattern : value) {
const std::string* item = pattern.GetIfString();
if (!item) {
return false;
}
patterns.push_back(*item);
}
return Populate(patterns, valid_schemes, allow_file_access, error);
}
} // namespace extensions
|