OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "core/frame/csp/CSPSource.h" | 5 #include "core/frame/csp/CSPSource.h" |
6 | 6 |
7 #include "core/dom/Document.h" | 7 #include "core/dom/Document.h" |
8 #include "core/frame/csp/ContentSecurityPolicy.h" | 8 #include "core/frame/csp/ContentSecurityPolicy.h" |
9 #include "platform/network/ResourceRequest.h" | 9 #include "platform/network/ResourceRequest.h" |
10 #include "platform/weborigin/KURL.h" | 10 #include "platform/weborigin/KURL.h" |
11 #include "platform/weborigin/SecurityOrigin.h" | 11 #include "platform/weborigin/SecurityOrigin.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 namespace blink { | 14 namespace blink { |
15 | 15 |
16 class CSPSourceTest : public ::testing::Test { | 16 class CSPSourceTest : public ::testing::Test { |
17 public: | 17 public: |
18 CSPSourceTest() : csp(ContentSecurityPolicy::create()) {} | 18 CSPSourceTest() : csp(ContentSecurityPolicy::create()) {} |
19 | 19 |
20 protected: | 20 protected: |
21 Persistent<ContentSecurityPolicy> csp; | 21 Persistent<ContentSecurityPolicy> csp; |
| 22 struct Source { |
| 23 String scheme; |
| 24 String host; |
| 25 String path; |
| 26 const int port; |
| 27 CSPSource::WildcardDisposition hostWildcard; |
| 28 CSPSource::WildcardDisposition portWildcard; |
| 29 }; |
| 30 |
| 31 bool equalSources(const Source& a, const Source& b) { |
| 32 return a.scheme == b.scheme && a.host == b.host && a.port == b.port && |
| 33 a.path == b.path && a.hostWildcard == b.hostWildcard && |
| 34 a.portWildcard == b.portWildcard; |
| 35 } |
22 }; | 36 }; |
23 | 37 |
24 TEST_F(CSPSourceTest, BasicMatching) { | 38 TEST_F(CSPSourceTest, BasicMatching) { |
25 KURL base; | 39 KURL base; |
26 CSPSource source(csp.get(), "http", "example.com", 8000, "/foo/", | 40 CSPSource source(csp.get(), "http", "example.com", 8000, "/foo/", |
27 CSPSource::NoWildcard, CSPSource::NoWildcard); | 41 CSPSource::NoWildcard, CSPSource::NoWildcard); |
28 | 42 |
29 EXPECT_TRUE(source.matches(KURL(base, "http://example.com:8000/foo/"))); | 43 EXPECT_TRUE(source.matches(KURL(base, "http://example.com:8000/foo/"))); |
30 EXPECT_TRUE(source.matches(KURL(base, "http://example.com:8000/foo/bar"))); | 44 EXPECT_TRUE(source.matches(KURL(base, "http://example.com:8000/foo/bar"))); |
31 EXPECT_TRUE(source.matches(KURL(base, "HTTP://EXAMPLE.com:8000/foo/BAR"))); | 45 EXPECT_TRUE(source.matches(KURL(base, "HTTP://EXAMPLE.com:8000/foo/BAR"))); |
(...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
438 CSPSource* required = new CSPSource( | 452 CSPSource* required = new CSPSource( |
439 csp.get(), test.b.scheme, test.b.host, test.b.port, test.b.path, | 453 csp.get(), test.b.scheme, test.b.host, test.b.port, test.b.path, |
440 CSPSource::NoWildcard, CSPSource::NoWildcard); | 454 CSPSource::NoWildcard, CSPSource::NoWildcard); |
441 | 455 |
442 EXPECT_EQ(returned->isSimilar(required), test.isSimilar); | 456 EXPECT_EQ(returned->isSimilar(required), test.isSimilar); |
443 // Verify the same test with a and b swapped. | 457 // Verify the same test with a and b swapped. |
444 EXPECT_EQ(required->isSimilar(returned), test.isSimilar); | 458 EXPECT_EQ(required->isSimilar(returned), test.isSimilar); |
445 } | 459 } |
446 } | 460 } |
447 | 461 |
| 462 TEST_F(CSPSourceTest, Intersect) { |
| 463 struct TestCase { |
| 464 const Source a; |
| 465 const Source b; |
| 466 const Source normalized; |
| 467 } cases[] = { |
| 468 {{"http", "example.com", "/", 0, CSPSource::NoWildcard, |
| 469 CSPSource::NoWildcard}, |
| 470 {"http", "example.com", "/", 0, CSPSource::NoWildcard, |
| 471 CSPSource::NoWildcard}, |
| 472 {"http", "example.com", "/", 0, CSPSource::NoWildcard, |
| 473 CSPSource::NoWildcard}}, |
| 474 {{"ws", "example.com", "/", 0, CSPSource::NoWildcard, |
| 475 CSPSource::NoWildcard}, |
| 476 {"wss", "example.com", "/", 0, CSPSource::NoWildcard, |
| 477 CSPSource::NoWildcard}, |
| 478 {"wss", "example.com", "/", 0, CSPSource::NoWildcard, |
| 479 CSPSource::NoWildcard}}, |
| 480 // Wildcards |
| 481 {{"http", "example.com", "/", 0, CSPSource::HasWildcard, |
| 482 CSPSource::NoWildcard}, |
| 483 {"http", "example.com", "/", 0, CSPSource::NoWildcard, |
| 484 CSPSource::NoWildcard}, |
| 485 {"http", "example.com", "/", 0, CSPSource::NoWildcard, |
| 486 CSPSource::NoWildcard}}, |
| 487 {{"http", "example.com", "/", 0, CSPSource::HasWildcard, |
| 488 CSPSource::HasWildcard}, |
| 489 {"http", "example.com", "/", 0, CSPSource::NoWildcard, |
| 490 CSPSource::NoWildcard}, |
| 491 {"http", "example.com", "/", 0, CSPSource::NoWildcard, |
| 492 CSPSource::NoWildcard}}, |
| 493 {{"http", "example.com", "/", 0, CSPSource::HasWildcard, |
| 494 CSPSource::NoWildcard}, |
| 495 {"http", "example.com", "/", 0, CSPSource::NoWildcard, |
| 496 CSPSource::HasWildcard}, |
| 497 {"http", "example.com", "/", 0, CSPSource::NoWildcard, |
| 498 CSPSource::NoWildcard}}, |
| 499 // Ports |
| 500 {{"http", "example.com", "/", 80, CSPSource::NoWildcard, |
| 501 CSPSource::NoWildcard}, |
| 502 {"http", "example.com", "/", 0, CSPSource::NoWildcard, |
| 503 CSPSource::NoWildcard}, |
| 504 {"http", "example.com", "/", 80, CSPSource::NoWildcard, |
| 505 CSPSource::NoWildcard}}, |
| 506 // Paths |
| 507 {{"http", "example.com", "/", 0, CSPSource::NoWildcard, |
| 508 CSPSource::NoWildcard}, |
| 509 {"http", "example.com", "/1.html", 0, CSPSource::NoWildcard, |
| 510 CSPSource::NoWildcard}, |
| 511 {"http", "example.com", "/1.html", 0, CSPSource::NoWildcard, |
| 512 CSPSource::NoWildcard}}, |
| 513 {{"http", "example.com", "/", 0, CSPSource::NoWildcard, |
| 514 CSPSource::NoWildcard}, |
| 515 {"http", "example.com", "", 0, CSPSource::NoWildcard, |
| 516 CSPSource::NoWildcard}, |
| 517 {"http", "example.com", "/", 0, CSPSource::NoWildcard, |
| 518 CSPSource::NoWildcard}}, |
| 519 {{"http", "example.com", "/", 0, CSPSource::NoWildcard, |
| 520 CSPSource::NoWildcard}, |
| 521 {"http", "example.com", "/a/b/", 0, CSPSource::NoWildcard, |
| 522 CSPSource::NoWildcard}, |
| 523 {"http", "example.com", "/a/b/", 0, CSPSource::NoWildcard, |
| 524 CSPSource::NoWildcard}}, |
| 525 {{"http", "example.com", "/a/", 0, CSPSource::NoWildcard, |
| 526 CSPSource::NoWildcard}, |
| 527 {"http", "example.com", "/a/b/", 0, CSPSource::NoWildcard, |
| 528 CSPSource::NoWildcard}, |
| 529 {"http", "example.com", "/a/b/", 0, CSPSource::NoWildcard, |
| 530 CSPSource::NoWildcard}}, |
| 531 {{"http", "example.com", "/a/", 0, CSPSource::NoWildcard, |
| 532 CSPSource::NoWildcard}, |
| 533 {"http", "example.com", "/a/b/1.html", 0, CSPSource::NoWildcard, |
| 534 CSPSource::NoWildcard}, |
| 535 {"http", "example.com", "/a/b/1.html", 0, CSPSource::NoWildcard, |
| 536 CSPSource::NoWildcard}}, |
| 537 // Mixed |
| 538 {{"http", "example.com", "/1.html", 0, CSPSource::NoWildcard, |
| 539 CSPSource::NoWildcard}, |
| 540 {"http", "example.com", "/", 80, CSPSource::NoWildcard, |
| 541 CSPSource::NoWildcard}, |
| 542 {"http", "example.com", "/1.html", 80, CSPSource::NoWildcard, |
| 543 CSPSource::NoWildcard}}, |
| 544 }; |
| 545 |
| 546 for (const auto& test : cases) { |
| 547 CSPSource* A = |
| 548 new CSPSource(csp.get(), test.a.scheme, test.a.host, test.a.port, |
| 549 test.a.path, test.a.hostWildcard, test.a.portWildcard); |
| 550 CSPSource* B = |
| 551 new CSPSource(csp.get(), test.b.scheme, test.b.host, test.b.port, |
| 552 test.b.path, test.b.hostWildcard, test.b.portWildcard); |
| 553 |
| 554 CSPSource* normalized = A->intersect(B); |
| 555 Source intersectAB = { |
| 556 normalized->m_scheme, normalized->m_host, |
| 557 normalized->m_path, normalized->m_port, |
| 558 normalized->m_hostWildcard, normalized->m_portWildcard}; |
| 559 EXPECT_TRUE(equalSources(intersectAB, test.normalized)); |
| 560 |
| 561 // Verify the same test with A and B swapped. The result should be |
| 562 // identical. |
| 563 normalized = B->intersect(A); |
| 564 Source intersectBA = { |
| 565 normalized->m_scheme, normalized->m_host, |
| 566 normalized->m_path, normalized->m_port, |
| 567 normalized->m_hostWildcard, normalized->m_portWildcard}; |
| 568 EXPECT_TRUE(equalSources(intersectBA, test.normalized)); |
| 569 } |
| 570 } |
| 571 |
| 572 TEST_F(CSPSourceTest, IntersectSchemesOnly) { |
| 573 struct TestCase { |
| 574 const Source a; |
| 575 const Source b; |
| 576 const Source normalized; |
| 577 } cases[] = { |
| 578 // Both sources are schemes only. |
| 579 {{"http", "", "", 0, CSPSource::NoWildcard, CSPSource::NoWildcard}, |
| 580 {"http", "", "", 0, CSPSource::NoWildcard, CSPSource::NoWildcard}, |
| 581 {"http", "", "", 0, CSPSource::NoWildcard, CSPSource::NoWildcard}}, |
| 582 {{"http", "", "", 0, CSPSource::NoWildcard, CSPSource::NoWildcard}, |
| 583 {"https", "", "", 0, CSPSource::NoWildcard, CSPSource::NoWildcard}, |
| 584 {"https", "", "", 0, CSPSource::NoWildcard, CSPSource::NoWildcard}}, |
| 585 {{"ws", "", "", 0, CSPSource::NoWildcard, CSPSource::NoWildcard}, |
| 586 {"wss", "", "", 0, CSPSource::NoWildcard, CSPSource::NoWildcard}, |
| 587 {"wss", "", "", 0, CSPSource::NoWildcard, CSPSource::NoWildcard}}, |
| 588 // One source is a scheme only and the other one has no wildcards. |
| 589 {{"http", "", "", 0, CSPSource::NoWildcard, CSPSource::NoWildcard}, |
| 590 {"http", "example.com", "/", 0, CSPSource::NoWildcard, |
| 591 CSPSource::NoWildcard}, |
| 592 {"http", "example.com", "/", 0, CSPSource::NoWildcard, |
| 593 CSPSource::NoWildcard}}, |
| 594 {{"http", "", "", 0, CSPSource::NoWildcard, CSPSource::NoWildcard}, |
| 595 {"https", "example.com", "/", 80, CSPSource::NoWildcard, |
| 596 CSPSource::NoWildcard}, |
| 597 {"https", "example.com", "/", 80, CSPSource::NoWildcard, |
| 598 CSPSource::NoWildcard}}, |
| 599 {{"https", "", "", 0, CSPSource::NoWildcard, CSPSource::NoWildcard}, |
| 600 {"http", "example.com", "/page.html", 80, CSPSource::NoWildcard, |
| 601 CSPSource::NoWildcard}, |
| 602 {"https", "example.com", "/page.html", 80, CSPSource::NoWildcard, |
| 603 CSPSource::NoWildcard}}, |
| 604 // One source is a scheme only and the other has one or two wildcards. |
| 605 {{"https", "", "", 0, CSPSource::NoWildcard, CSPSource::NoWildcard}, |
| 606 {"http", "example.com", "/page.html", 80, CSPSource::HasWildcard, |
| 607 CSPSource::NoWildcard}, |
| 608 {"https", "example.com", "/page.html", 80, CSPSource::HasWildcard, |
| 609 CSPSource::NoWildcard}}, |
| 610 {{"https", "", "", 0, CSPSource::NoWildcard, CSPSource::NoWildcard}, |
| 611 {"http", "example.com", "/page.html", 80, CSPSource::NoWildcard, |
| 612 CSPSource::HasWildcard}, |
| 613 {"https", "example.com", "/page.html", 80, CSPSource::NoWildcard, |
| 614 CSPSource::HasWildcard}}, |
| 615 {{"https", "", "", 0, CSPSource::NoWildcard, CSPSource::NoWildcard}, |
| 616 {"http", "example.com", "/page.html", 80, CSPSource::HasWildcard, |
| 617 CSPSource::HasWildcard}, |
| 618 {"https", "example.com", "/page.html", 80, CSPSource::HasWildcard, |
| 619 CSPSource::HasWildcard}}, |
| 620 }; |
| 621 |
| 622 for (const auto& test : cases) { |
| 623 CSPSource* A = |
| 624 new CSPSource(csp.get(), test.a.scheme, test.a.host, test.a.port, |
| 625 test.a.path, test.a.hostWildcard, test.a.portWildcard); |
| 626 |
| 627 CSPSource* B = |
| 628 new CSPSource(csp.get(), test.b.scheme, test.b.host, test.b.port, |
| 629 test.b.path, test.b.hostWildcard, test.b.portWildcard); |
| 630 |
| 631 CSPSource* normalized = A->intersect(B); |
| 632 Source intersectAB = { |
| 633 normalized->m_scheme, normalized->m_host, |
| 634 normalized->m_path, normalized->m_port, |
| 635 normalized->m_hostWildcard, normalized->m_portWildcard}; |
| 636 EXPECT_TRUE(equalSources(intersectAB, test.normalized)); |
| 637 |
| 638 // Verify the same test with A and B swapped. The result should be |
| 639 // identical. |
| 640 normalized = B->intersect(A); |
| 641 Source intersectBA = { |
| 642 normalized->m_scheme, normalized->m_host, |
| 643 normalized->m_path, normalized->m_port, |
| 644 normalized->m_hostWildcard, normalized->m_portWildcard}; |
| 645 EXPECT_TRUE(equalSources(intersectBA, test.normalized)); |
| 646 } |
| 647 } |
| 648 |
448 } // namespace blink | 649 } // namespace blink |
OLD | NEW |