Commit cf002f92 authored by Antonio Sartori's avatar Antonio Sartori Committed by Commit Bot

Simplify AncestorThrottleNavigationTest

This CL simplifies the test
AncestorThrottleNavigationTest.WillStartRequestAddsSecRequiredCSPHeader
relying upon fixes / new functionalities added to the
NavigationSimulatorImpl in https://crrev.com/c/2294801 and
https://crrev.com/c/2310416.

Bug: 1094909
Change-Id: I155fc027748cb2de7255199fb07184b5375136b4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2320831
Commit-Queue: Antonio Sartori <antoniosartori@chromium.org>
Reviewed-by: default avatarArthur Sonzogni <arthursonzogni@chromium.org>
Cr-Commit-Position: refs/heads/master@{#804753}
parent 3d146fed
...@@ -264,17 +264,13 @@ TEST_F(AncestorThrottleTest, AllowsBlanketEnforcementOfRequiredCSP) { ...@@ -264,17 +264,13 @@ TEST_F(AncestorThrottleTest, AllowsBlanketEnforcementOfRequiredCSP) {
} }
} }
class AncestorThrottleNavigationTest using AncestorThrottleNavigationTest = RenderViewHostTestHarness;
: public content::RenderViewHostTestHarness {};
TEST_F(AncestorThrottleNavigationTest, TEST_F(AncestorThrottleNavigationTest,
WillStartRequestAddsSecRequiredCSPHeader) { WillStartRequestAddsSecRequiredCSPHeader) {
base::test::ScopedFeatureList feature_list; base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(network::features::kOutOfBlinkCSPEE); feature_list.InitAndEnableFeature(network::features::kOutOfBlinkCSPEE);
// Perform an initial navigation to set up everything.
NavigateAndCommit(GURL("https://test.com"));
// Create a frame tree with different 'csp' attributes according to the // Create a frame tree with different 'csp' attributes according to the
// following graph: // following graph:
// //
...@@ -292,123 +288,80 @@ TEST_F(AncestorThrottleNavigationTest, ...@@ -292,123 +288,80 @@ TEST_F(AncestorThrottleNavigationTest,
// Test that the required CSP of every frame is computed/inherited correctly // Test that the required CSP of every frame is computed/inherited correctly
// and that the Sec-Required-CSP header is set. // and that the Sec-Required-CSP header is set.
auto test = [](TestRenderFrameHost* frame, std::string csp_attr,
std::string expect_csp) {
SCOPED_TRACE(frame->GetFrameName());
if (!csp_attr.empty())
frame->frame_tree_node()->set_csp_attribute(ParsePolicy(csp_attr));
std::unique_ptr<NavigationSimulator> simulator =
content::NavigationSimulator::CreateRendererInitiated(
// Chrome blocks a frame navigating to a URL if more than one of its
// ancestors have the same URL. Use a different URL every time, to
// avoid blocking navigation of the grandchild frame.
GURL("https://www.example.com/" + frame->GetFrameName()), frame);
simulator->Start();
NavigationRequest* request =
NavigationRequest::From(simulator->GetNavigationHandle());
std::string header_value;
bool found = request->GetRequestHeaders().GetHeader("sec-required-csp",
&header_value);
if (!expect_csp.empty()) {
EXPECT_TRUE(found);
EXPECT_EQ(expect_csp, header_value);
} else {
EXPECT_FALSE(found);
}
// Complete the navigation so that the required csp is stored in the
// RenderFrameHost, so that when we will add children to this frame they
// will be able to get the parent's required csp (and hence also test that
// the whole logic works).
auto response_headers =
base::MakeRefCounted<net::HttpResponseHeaders>("HTTP/1.1 200 OK");
response_headers->SetHeader("Allow-CSP-From", "*");
simulator->SetResponseHeaders(response_headers);
simulator->Commit();
};
auto* main_frame = static_cast<TestRenderFrameHost*>(main_rfh()); auto* main_frame = static_cast<TestRenderFrameHost*>(main_rfh());
test(main_frame, "", "");
auto* child_with_csp = static_cast<TestRenderFrameHost*>( auto* child_with_csp = static_cast<TestRenderFrameHost*>(
content::RenderFrameHostTester::For(main_frame) content::RenderFrameHostTester::For(main_frame)
->AppendChild("child_frame")); ->AppendChild("child_with_csp"));
child_with_csp->frame_tree_node()->set_csp_attribute( test(child_with_csp, "script-src 'none'", "script-src 'none'");
ParsePolicy("script-src 'none'"));
auto* grandchild_same_csp = static_cast<TestRenderFrameHost*>( auto* grandchild_same_csp = static_cast<TestRenderFrameHost*>(
content::RenderFrameHostTester::For(child_with_csp) content::RenderFrameHostTester::For(child_with_csp)
->AppendChild("grandchild_frame")); ->AppendChild("grandchild_same_csp"));
grandchild_same_csp->frame_tree_node()->set_csp_attribute( test(grandchild_same_csp, "script-src 'none'", "script-src 'none'");
ParsePolicy("script-src 'none'"));
auto* grandchild_no_csp = static_cast<TestRenderFrameHost*>( auto* grandchild_no_csp = static_cast<TestRenderFrameHost*>(
content::RenderFrameHostTester::For(child_with_csp) content::RenderFrameHostTester::For(child_with_csp)
->AppendChild("grandchild_frame")); ->AppendChild("grandchild_no_csp"));
test(grandchild_no_csp, "", "script-src 'none'");
auto* grandgrandchild = static_cast<TestRenderFrameHost*>( auto* grandgrandchild = static_cast<TestRenderFrameHost*>(
content::RenderFrameHostTester::For(grandchild_no_csp) content::RenderFrameHostTester::For(grandchild_no_csp)
->AppendChild("grandgrandchild_frame")); ->AppendChild("grandgrandchild"));
test(grandgrandchild, "", "script-src 'none'");
auto* grandchild_invalid_csp = static_cast<TestRenderFrameHost*>( auto* grandchild_invalid_csp = static_cast<TestRenderFrameHost*>(
content::RenderFrameHostTester::For(child_with_csp) content::RenderFrameHostTester::For(child_with_csp)
->AppendChild("grandchild_frame")); ->AppendChild("grandchild_invalid_csp"));
grandchild_invalid_csp->frame_tree_node()->set_csp_attribute( test(grandchild_invalid_csp, "report-to group", "script-src 'none'");
ParsePolicy("report-to group"));
auto* grandchild_invalid_csp2 = static_cast<TestRenderFrameHost*>( auto* grandchild_invalid_csp2 = static_cast<TestRenderFrameHost*>(
content::RenderFrameHostTester::For(child_with_csp) content::RenderFrameHostTester::For(child_with_csp)
->AppendChild("grandchild_frame")); ->AppendChild("grandchild_invalid_csp2"));
grandchild_invalid_csp2->frame_tree_node()->set_csp_attribute( test(grandchild_invalid_csp2, "script-src 'none'; invalid-directive",
ParsePolicy("script-src 'none'; invalid-directive")); "script-src 'none'");
auto* sibling = static_cast<TestRenderFrameHost*>( auto* sibling = static_cast<TestRenderFrameHost*>(
content::RenderFrameHostTester::For(main_frame) content::RenderFrameHostTester::For(main_frame)->AppendChild("sibling"));
->AppendChild("sibling_frame")); test(sibling, "", "");
struct TestCase {
const char* name;
TestRenderFrameHost* frame;
const char* expected_header;
} cases[] = {
{
"Main frame does not set header",
main_frame,
nullptr,
},
{
"Frame with 'csp' attribute sets correct header",
child_with_csp,
"script-src 'none'",
},
{
"Child with same 'csp' attribute as parent frame sets correct header",
grandchild_same_csp,
"script-src 'none'",
},
{
"Child without 'csp' attribute inherits from parent",
grandchild_no_csp,
"script-src 'none'",
},
{
"Grandchild without 'csp' attribute inherits from grandparent"
"header",
grandgrandchild,
"script-src 'none'",
},
{
"Child with invalid 'csp' attribute inherits from parent",
grandchild_invalid_csp,
"script-src 'none'",
},
{
"Child with invalid 'csp' attribute inherits from parent 2",
grandchild_invalid_csp2,
"script-src 'none'",
},
{
"Frame without 'csp' attribute does not set header",
sibling,
nullptr,
},
};
for (auto test : cases) {
SCOPED_TRACE(test.name);
std::unique_ptr<NavigationSimulator> simulator =
content::NavigationSimulator::CreateRendererInitiated(
GURL("https://www.foo.com/"), test.frame);
simulator->Start();
NavigationRequest* request =
NavigationRequest::From(simulator->GetNavigationHandle());
std::string header_value;
bool found = request->GetRequestHeaders().GetHeader("sec-required-csp",
&header_value);
if (test.expected_header) {
EXPECT_TRUE(found);
EXPECT_EQ(test.expected_header, header_value);
} else {
EXPECT_FALSE(found);
}
// Complete the navigation and store the required CSP in the
// RenderFrameHostImpl so that the next tests can rely on this.
// TODO(antoniosartori): Update the NavigationSimulatorImpl so that this is
// done automatically on commit.
auto response_headers =
base::MakeRefCounted<net::HttpResponseHeaders>("HTTP/1.1 200 OK");
response_headers->SetHeader("Allow-CSP-From", "*");
simulator->SetResponseHeaders(response_headers);
simulator->ReadyToCommit();
auto* new_required_csp = request->required_csp();
if (new_required_csp)
test.frame->required_csp_ = new_required_csp->Clone();
}
} }
TEST_F(AncestorThrottleNavigationTest, EvaluateCSPEmbeddedEnforcement) { TEST_F(AncestorThrottleNavigationTest, EvaluateCSPEmbeddedEnforcement) {
......
...@@ -1948,8 +1948,6 @@ class CONTENT_EXPORT RenderFrameHostImpl ...@@ -1948,8 +1948,6 @@ class CONTENT_EXPORT RenderFrameHostImpl
AttemptDuplicateRenderWidgetHost); AttemptDuplicateRenderWidgetHost);
FRIEND_TEST_ALL_PREFIXES(RenderDocumentHostUserDataTest, FRIEND_TEST_ALL_PREFIXES(RenderDocumentHostUserDataTest,
CheckInPendingDeletionState); CheckInPendingDeletionState);
FRIEND_TEST_ALL_PREFIXES(AncestorThrottleNavigationTest,
WillStartRequestAddsSecRequiredCSPHeader);
class DroppedInterfaceRequestLogger; class DroppedInterfaceRequestLogger;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment