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 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
|
// Copyright 2017 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/payments/content/manifest_verifier.h"
#include <stdint.h>
#include <utility>
#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "base/test/metrics/histogram_tester.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/payments/content/payment_manifest_web_data_service.h"
#include "components/payments/content/utility/payment_manifest_parser.h"
#include "components/payments/core/const_csp_checker.h"
#include "components/payments/core/test_payment_manifest_downloader.h"
#include "components/webdata_services/web_data_service_wrapper_factory.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/test/browser_test.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/re2/src/re2/re2.h"
#include "url/origin.h"
namespace payments {
namespace {
// Tests for the manifest verifier.
class ManifestVerifierBrowserTest : public InProcessBrowserTest {
public:
ManifestVerifierBrowserTest() = default;
ManifestVerifierBrowserTest(const ManifestVerifierBrowserTest&) = delete;
ManifestVerifierBrowserTest& operator=(const ManifestVerifierBrowserTest&) =
delete;
~ManifestVerifierBrowserTest() override = default;
// Starts the HTTPS test server on localhost.
void SetUpOnMainThread() override {
https_server_ = std::make_unique<net::EmbeddedTestServer>(
net::EmbeddedTestServer::TYPE_HTTPS);
ASSERT_TRUE(https_server_->InitializeAndListen());
https_server_->ServeFilesFromSourceDirectory(
"components/test/data/payments");
https_server_->StartAcceptingConnections();
const_csp_checker_ = std::make_unique<ConstCSPChecker>(/*allow=*/true);
content::BrowserContext* context = browser()->profile();
test_downloader_ = std::make_unique<TestDownloader>(
const_csp_checker_->GetWeakPtr(),
context->GetDefaultStoragePartition()
->GetURLLoaderFactoryForBrowserProcess());
test_downloader_->AddTestServerURL("https://", https_server_->GetURL("/"));
}
// Runs the verifier on the |apps| and blocks until the verifier has finished
// using all resources.
void Verify(content::InstalledPaymentAppsFinder::PaymentApps apps) {
content::WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
content::BrowserContext* context = browser()->profile();
auto parser = std::make_unique<payments::PaymentManifestParser>(
std::make_unique<ErrorLogger>());
auto cache = webdata_services::WebDataServiceWrapperFactory::
GetPaymentManifestWebDataServiceForBrowserContext(
context, ServiceAccessType::EXPLICIT_ACCESS);
ManifestVerifier verifier(url::Origin::Create(GURL("https://chromium.org")),
web_contents, test_downloader_.get(),
parser.get(), cache.get());
base::RunLoop run_loop;
verifier.Verify(
std::move(apps),
base::BindOnce(&ManifestVerifierBrowserTest::OnPaymentAppsVerified,
base::Unretained(this)),
run_loop.QuitClosure());
run_loop.Run();
}
// Returns the apps that have been verified by the Verify() method.
const content::InstalledPaymentAppsFinder::PaymentApps& verified_apps()
const {
return verified_apps_;
}
const std::string& error_message() const { return error_message_; }
// Expects that the verified payment app with |id| has the |expected_scope|
// and the |expected_methods| and the
// |expect_has_explicitly_verified_methods|.
void ExpectApp(int64_t id,
const std::string& expected_scope,
const std::set<std::string>& expected_methods,
bool expect_has_explicitly_verified_methods) {
const auto& it = verified_apps().find(id);
ASSERT_NE(verified_apps().end(), it);
EXPECT_EQ(GURL(expected_scope), it->second->scope);
std::set<std::string> actual_methods(it->second->enabled_methods.begin(),
it->second->enabled_methods.end());
EXPECT_EQ(expected_methods, actual_methods);
EXPECT_EQ(expect_has_explicitly_verified_methods,
it->second->has_explicitly_verified_methods);
EXPECT_TRUE(error_message().empty()) << error_message();
}
protected:
std::unique_ptr<TestDownloader> test_downloader_;
private:
// Called by the verifier upon completed verification. These |apps| have only
// valid payment methods.
void OnPaymentAppsVerified(
content::InstalledPaymentAppsFinder::PaymentApps apps,
const std::string& error_message) {
verified_apps_ = std::move(apps);
error_message_ = error_message;
}
// Serves the payment method manifest files.
std::unique_ptr<net::EmbeddedTestServer> https_server_;
std::unique_ptr<ConstCSPChecker> const_csp_checker_;
// The apps that have been verified by the Verify() method.
content::InstalledPaymentAppsFinder::PaymentApps verified_apps_;
std::string error_message_;
};
// Absence of payment handlers should result in absence of verified payment
// handlers.
IN_PROC_BROWSER_TEST_F(ManifestVerifierBrowserTest, NoApps) {
{
Verify(content::InstalledPaymentAppsFinder::PaymentApps());
EXPECT_TRUE(verified_apps().empty());
EXPECT_TRUE(error_message().empty()) << error_message();
}
// Repeat verifications should have identical results.
{
Verify(content::InstalledPaymentAppsFinder::PaymentApps());
EXPECT_TRUE(verified_apps().empty());
EXPECT_TRUE(error_message().empty()) << error_message();
}
}
// A payment handler without any payment method names is not valid.
IN_PROC_BROWSER_TEST_F(ManifestVerifierBrowserTest, NoMethods) {
{
content::InstalledPaymentAppsFinder::PaymentApps apps;
apps[0] = std::make_unique<content::StoredPaymentApp>();
apps[0]->scope = GURL("https://bobpay.test/webpay");
Verify(std::move(apps));
EXPECT_TRUE(verified_apps().empty());
EXPECT_TRUE(error_message().empty()) << error_message();
}
// Repeat verifications should have identical results.
{
content::InstalledPaymentAppsFinder::PaymentApps apps;
apps[0] = std::make_unique<content::StoredPaymentApp>();
apps[0]->scope = GURL("https://bobpay.test/webpay");
Verify(std::move(apps));
EXPECT_TRUE(verified_apps().empty());
EXPECT_TRUE(error_message().empty()) << error_message();
}
}
// A payment handler with an unknown non-URL payment method name is not valid.
IN_PROC_BROWSER_TEST_F(ManifestVerifierBrowserTest,
UnknownPaymentMethodNameIsRemoved) {
{
content::InstalledPaymentAppsFinder::PaymentApps apps;
apps[0] = std::make_unique<content::StoredPaymentApp>();
apps[0]->scope = GURL("https://bobpay.test/webpay");
apps[0]->enabled_methods.push_back("unknown");
Verify(std::move(apps));
EXPECT_TRUE(verified_apps().empty());
EXPECT_TRUE(error_message().empty()) << error_message();
}
// Repeat verifications should have identical results.
{
content::InstalledPaymentAppsFinder::PaymentApps apps;
apps[0] = std::make_unique<content::StoredPaymentApp>();
apps[0]->scope = GURL("https://bobpay.test/webpay");
apps[0]->enabled_methods.push_back("unknown");
Verify(std::move(apps));
EXPECT_TRUE(verified_apps().empty());
EXPECT_TRUE(error_message().empty()) << error_message();
}
}
// Verify that a payment handler from https://bobpay.test/webpay can not use the
// payment method name https://frankpay.test/webpay, because
// https://frankpay.test/payment-manifest.json does not explicitly authorize
// any payment app.
IN_PROC_BROWSER_TEST_F(ManifestVerifierBrowserTest,
BobPayHandlerCanNotUseMethodWithOriginWildcard) {
{
content::InstalledPaymentAppsFinder::PaymentApps apps;
apps[0] = std::make_unique<content::StoredPaymentApp>();
apps[0]->scope = GURL("https://bobpay.test/webpay");
apps[0]->enabled_methods.push_back("https://frankpay.test/webpay");
Verify(std::move(apps));
EXPECT_TRUE(verified_apps().empty());
EXPECT_TRUE(error_message().empty()) << error_message();
}
// Repeat verifications should have identical results.
{
content::InstalledPaymentAppsFinder::PaymentApps apps;
apps[0] = std::make_unique<content::StoredPaymentApp>();
apps[0]->scope = GURL("https://bobpay.test/webpay");
apps[0]->enabled_methods.push_back("https://frankpay.test/webpay");
Verify(std::move(apps));
EXPECT_TRUE(verified_apps().empty());
EXPECT_TRUE(error_message().empty()) << error_message();
}
}
// Verify that a payment handler from an unreachable website can not use the
// payment method name https://frankpay.test/webpay, because
// https://frankpay.test/payment-manifest.json does not explicitly authorize
// any payment app.
IN_PROC_BROWSER_TEST_F(ManifestVerifierBrowserTest,
Handler404CanNotUseMethodWithOriginWildcard) {
{
content::InstalledPaymentAppsFinder::PaymentApps apps;
apps[0] = std::make_unique<content::StoredPaymentApp>();
apps[0]->scope = GURL("https://404.com/webpay");
apps[0]->enabled_methods.push_back("https://frankpay.test/webpay");
Verify(std::move(apps));
EXPECT_TRUE(verified_apps().empty());
EXPECT_TRUE(error_message().empty()) << error_message();
}
// Repeat verifications should have identical results.
{
content::InstalledPaymentAppsFinder::PaymentApps apps;
apps[0] = std::make_unique<content::StoredPaymentApp>();
apps[0]->scope = GURL("https://404.com/webpay");
apps[0]->enabled_methods.push_back("https://frankpay.test/webpay");
Verify(std::move(apps));
EXPECT_TRUE(verified_apps().empty());
EXPECT_TRUE(error_message().empty()) << error_message();
}
}
// Verify that a payment handler from anywhere on https://bobpay.test can use
// the payment method name from anywhere else on https://bobpay.test, because of
// the origin match.
IN_PROC_BROWSER_TEST_F(ManifestVerifierBrowserTest,
BobPayCanUseAnyMethodOnOwnOrigin) {
{
content::InstalledPaymentAppsFinder::PaymentApps apps;
apps[0] = std::make_unique<content::StoredPaymentApp>();
apps[0]->scope = GURL("https://bobpay.test/anything/here");
apps[0]->enabled_methods.push_back(
"https://bobpay.test/does/not/matter/whats/here");
Verify(std::move(apps));
EXPECT_EQ(1U, verified_apps().size());
ExpectApp(0, "https://bobpay.test/anything/here",
{"https://bobpay.test/does/not/matter/whats/here"}, true);
EXPECT_TRUE(error_message().empty()) << error_message();
}
// Repeat verifications should have identical results.
{
content::InstalledPaymentAppsFinder::PaymentApps apps;
apps[0] = std::make_unique<content::StoredPaymentApp>();
apps[0]->scope = GURL("https://bobpay.test/anything/here");
apps[0]->enabled_methods = {
"https://bobpay.test/does/not/matter/whats/here"};
Verify(std::move(apps));
EXPECT_EQ(1U, verified_apps().size());
ExpectApp(0, "https://bobpay.test/anything/here",
{"https://bobpay.test/does/not/matter/whats/here"}, true);
EXPECT_TRUE(error_message().empty()) << error_message();
}
}
// Verify that a payment handler from anywhere on an unreachable website can use
// the payment method name from anywhere else on the same unreachable website,
// because they have identical origin.
IN_PROC_BROWSER_TEST_F(ManifestVerifierBrowserTest,
Handler404CanUseAnyMethodOnOwnOrigin) {
{
content::InstalledPaymentAppsFinder::PaymentApps apps;
apps[0] = std::make_unique<content::StoredPaymentApp>();
apps[0]->scope = GURL("https://404.com/anything/here");
apps[0]->enabled_methods = {"https://404.com/does/not/matter/whats/here"};
Verify(std::move(apps));
EXPECT_EQ(1U, verified_apps().size());
ExpectApp(0, "https://404.com/anything/here",
{"https://404.com/does/not/matter/whats/here"}, true);
EXPECT_TRUE(error_message().empty()) << error_message();
}
// Repeat verifications should have identical results.
{
content::InstalledPaymentAppsFinder::PaymentApps apps;
apps[0] = std::make_unique<content::StoredPaymentApp>();
apps[0]->scope = GURL("https://404.com/anything/here");
apps[0]->enabled_methods = {"https://404.com/does/not/matter/whats/here"};
Verify(std::move(apps));
EXPECT_EQ(1U, verified_apps().size());
ExpectApp(0, "https://404.com/anything/here",
{"https://404.com/does/not/matter/whats/here"}, true);
EXPECT_TRUE(error_message().empty()) << error_message();
}
}
// Verify that only the payment handler from https://alicepay.test/webpay can
// use payment methods https://georgepay.test/webpay and
// https://ikepay.test/webpay, because both
// https://georgepay.test/payment-manifest.json and
// https://ikepay.test/payment-manifest.json contain "supported_origins":
// ["https://alicepay.test"]. The payment handler from
// https://bobpay.test/webpay cannot use these payment methods, however.
IN_PROC_BROWSER_TEST_F(ManifestVerifierBrowserTest, OneSupportedOrigin) {
{
content::InstalledPaymentAppsFinder::PaymentApps apps;
apps[0] = std::make_unique<content::StoredPaymentApp>();
apps[0]->scope = GURL("https://alicepay.test/webpay");
apps[0]->enabled_methods = {"https://georgepay.test/webpay",
"https://ikepay.test/webpay"};
apps[1] = std::make_unique<content::StoredPaymentApp>();
apps[1]->scope = GURL("https://bobpay.test/webpay");
apps[1]->enabled_methods = {"https://georgepay.test/webpay",
"https://ikepay.test/webpay"};
Verify(std::move(apps));
EXPECT_EQ(1U, verified_apps().size());
ExpectApp(0, "https://alicepay.test/webpay",
{"https://georgepay.test/webpay", "https://ikepay.test/webpay"},
true);
EXPECT_TRUE(error_message().empty()) << error_message();
}
// Repeat verifications should have identical results.
{
content::InstalledPaymentAppsFinder::PaymentApps apps;
apps[0] = std::make_unique<content::StoredPaymentApp>();
apps[0]->scope = GURL("https://alicepay.test/webpay");
apps[0]->enabled_methods = {"https://georgepay.test/webpay",
"https://ikepay.test/webpay"};
apps[1] = std::make_unique<content::StoredPaymentApp>();
apps[1]->scope = GURL("https://bobpay.test/webpay");
apps[1]->enabled_methods = {"https://georgepay.test/webpay",
"https://ikepay.test/webpay"};
Verify(std::move(apps));
EXPECT_EQ(1U, verified_apps().size());
ExpectApp(0, "https://alicepay.test/webpay",
{"https://georgepay.test/webpay", "https://ikepay.test/webpay"},
true);
EXPECT_TRUE(error_message().empty()) << error_message();
}
}
// Verify that a payment handler from https://alicepay.test/webpay can use both
// same-origin URL payment method name and different-origin URL payment method
// name.
IN_PROC_BROWSER_TEST_F(ManifestVerifierBrowserTest, ThreeTypesOfMethods) {
{
content::InstalledPaymentAppsFinder::PaymentApps apps;
apps[0] = std::make_unique<content::StoredPaymentApp>();
apps[0]->scope = GURL("https://alicepay.test/webpay");
apps[0]->enabled_methods = {"basic-card", "https://alicepay.test/webpay2",
"https://ikepay.test/webpay"};
Verify(std::move(apps));
EXPECT_EQ(1U, verified_apps().size());
ExpectApp(0, "https://alicepay.test/webpay",
{"https://alicepay.test/webpay2", "https://ikepay.test/webpay"},
true);
EXPECT_TRUE(error_message().empty()) << error_message();
}
// Repeat verifications should have identical results.
{
content::InstalledPaymentAppsFinder::PaymentApps apps;
apps[0] = std::make_unique<content::StoredPaymentApp>();
apps[0]->scope = GURL("https://alicepay.test/webpay");
apps[0]->enabled_methods = {"basic-card", "https://alicepay.test/webpay2",
"https://ikepay.test/webpay"};
Verify(std::move(apps));
EXPECT_EQ(1U, verified_apps().size());
ExpectApp(0, "https://alicepay.test/webpay",
{"https://alicepay.test/webpay2", "https://ikepay.test/webpay"},
true);
EXPECT_TRUE(error_message().empty()) << error_message();
}
}
// Verify that a payment handler from https://bobpay.test/webpay cannot use
// payment method names that are unreachable websites, the origin of which does
// not match that of the payment handler.
IN_PROC_BROWSER_TEST_F(ManifestVerifierBrowserTest,
SinglePaymentMethodName404) {
std::string expected_pattern =
"Unable to download payment manifest "
"\"https://127.0.0.1:\\d+/404.test/webpay\". HTTP 404 Not Found.";
{
content::InstalledPaymentAppsFinder::PaymentApps apps;
apps[0] = std::make_unique<content::StoredPaymentApp>();
apps[0]->scope = GURL("https://bobpay.test/webpay");
apps[0]->enabled_methods = {"https://404.test/webpay"};
Verify(std::move(apps));
EXPECT_TRUE(verified_apps().empty());
EXPECT_TRUE(RE2::FullMatch(error_message(), expected_pattern))
<< "Actual error message \"" << error_message()
<< "\" did not match expected pattern \"" << expected_pattern << "\".";
}
// Repeat verifications should have identical results.
{
content::InstalledPaymentAppsFinder::PaymentApps apps;
apps[0] = std::make_unique<content::StoredPaymentApp>();
apps[0]->scope = GURL("https://bobpay.test/webpay");
apps[0]->enabled_methods = {"https://404.test/webpay"};
Verify(std::move(apps));
EXPECT_TRUE(verified_apps().empty());
EXPECT_TRUE(RE2::FullMatch(error_message(), expected_pattern))
<< "Actual error message \"" << error_message()
<< "\" did not match expected pattern \"" << expected_pattern << "\".";
}
}
// Verify that a payment handler from https://bobpay.test/webpay cannot use
// payment method names that are unreachable websites, the origin of which does
// not match that of the payment handler. Since multiple downloads fail, the
// error message will describe the first failure.
IN_PROC_BROWSER_TEST_F(ManifestVerifierBrowserTest,
MultiplePaymentMethodName404) {
std::string expected_pattern =
"Unable to download payment manifest "
"\"https://127.0.0.1:\\d+/404(aswell)?.test/webpay\". HTTP 404 Not "
"Found.";
{
content::InstalledPaymentAppsFinder::PaymentApps apps;
apps[0] = std::make_unique<content::StoredPaymentApp>();
apps[0]->scope = GURL("https://bobpay.test/webpay");
apps[0]->enabled_methods = {"https://404.test/webpay",
"https://404aswell.test/webpay"};
Verify(std::move(apps));
EXPECT_TRUE(verified_apps().empty());
EXPECT_TRUE(RE2::FullMatch(error_message(), expected_pattern))
<< "Actual error message \"" << error_message()
<< "\" did not match expected pattern \"" << expected_pattern << "\".";
}
// Repeat verifications should have identical results.
{
content::InstalledPaymentAppsFinder::PaymentApps apps;
apps[0] = std::make_unique<content::StoredPaymentApp>();
apps[0]->scope = GURL("https://bobpay.test/webpay");
apps[0]->enabled_methods = {"https://404.test/webpay",
"https://404aswell.test/webpay"};
Verify(std::move(apps));
EXPECT_TRUE(verified_apps().empty());
EXPECT_TRUE(RE2::FullMatch(error_message(), expected_pattern))
<< "Actual error message \"" << error_message()
<< "\" did not match expected pattern \"" << expected_pattern << "\".";
}
}
// Non-URL payment method names are not valid.
IN_PROC_BROWSER_TEST_F(ManifestVerifierBrowserTest,
NonUrlPaymentMethodNamesAreNotValid) {
{
content::InstalledPaymentAppsFinder::PaymentApps apps;
apps[0] = std::make_unique<content::StoredPaymentApp>();
apps[0]->scope = GURL("https://bobpay.test/webpay");
apps[0]->enabled_methods = {"basic-card",
"interledger",
"payee-credit-transfer",
"payer-credit-transfer",
"tokenized-card",
"not-supported"};
Verify(std::move(apps));
EXPECT_TRUE(verified_apps().empty());
EXPECT_TRUE(error_message().empty()) << error_message();
}
// Repeat verifications should have identical results.
{
content::InstalledPaymentAppsFinder::PaymentApps apps;
apps[0] = std::make_unique<content::StoredPaymentApp>();
apps[0]->scope = GURL("https://bobpay.test/webpay");
apps[0]->enabled_methods = {"basic-card",
"interledger",
"payee-credit-transfer",
"payer-credit-transfer",
"tokenized-card",
"not-supported"};
Verify(std::move(apps));
EXPECT_TRUE(verified_apps().empty());
EXPECT_TRUE(error_message().empty()) << error_message();
}
}
} // namespace
} // namespace payments
|