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
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/shell/browser/shell.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "services/network/public/cpp/features.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/page_transition_types.h"
#include "url/gurl.h"
namespace content {
// Test suite covering the interaction between browser bookmarks and
// `Sec-Fetch-*` headers that can't be covered by Web Platform Tests (yet).
// See https://mikewest.github.io/sec-metadata/#directly-user-initiated and
// https://github.com/web-platform-tests/wpt/issues/16019.
class SecFetchBrowserTest : public ContentBrowserTest {
public:
SecFetchBrowserTest()
: https_test_server_(net::EmbeddedTestServer::TYPE_HTTPS) {}
SecFetchBrowserTest(const SecFetchBrowserTest&) = delete;
SecFetchBrowserTest& operator=(const SecFetchBrowserTest&) = delete;
void SetUpOnMainThread() override {
host_resolver()->AddRule("*", "127.0.0.1");
https_test_server_.AddDefaultHandlers(GetTestDataFilePath());
https_test_server_.SetSSLConfig(net::EmbeddedTestServer::CERT_OK);
ASSERT_TRUE(https_test_server_.Start());
}
WebContents* web_contents() { return shell()->web_contents(); }
void NavigateForHeader(const std::string& header) {
std::string url = "/echoheader?";
ASSERT_TRUE(
NavigateToURL(shell(), https_test_server_.GetURL(url + header)));
NavigationEntry* entry =
web_contents()->GetController().GetLastCommittedEntry();
ASSERT_TRUE(PageTransitionCoreTypeIs(entry->GetTransitionType(),
ui::PAGE_TRANSITION_TYPED));
}
GURL GetUrl(const std::string& path_and_query) {
return https_test_server_.GetURL(path_and_query);
}
GURL GetSecFetchUrl() { return GetUrl("/echoheader?sec-fetch-site"); }
std::string GetContent() {
return EvalJs(shell(), "document.body.innerText").ExtractString();
}
private:
net::EmbeddedTestServer https_test_server_;
base::test::ScopedFeatureList feature_list_;
};
IN_PROC_BROWSER_TEST_F(SecFetchBrowserTest, TypedNavigation) {
{
// Sec-Fetch-Dest: document
NavigateForHeader("Sec-Fetch-Dest");
EXPECT_EQ("document", GetContent());
}
{
// Sec-Fetch-Mode: navigate
NavigateForHeader("Sec-Fetch-Mode");
EXPECT_EQ("navigate", GetContent());
}
{
// Sec-Fetch-Site: none
NavigateForHeader("Sec-Fetch-Site");
EXPECT_EQ("none", GetContent());
}
{
// Sec-Fetch-User: ?1
NavigateForHeader("Sec-Fetch-User");
EXPECT_EQ("?1", GetContent());
}
}
// Verify that cross-port navigations are treated as same-site by
// Sec-Fetch-Site.
IN_PROC_BROWSER_TEST_F(SecFetchBrowserTest, CrossPortNavigation) {
net::EmbeddedTestServer server2(net::EmbeddedTestServer::TYPE_HTTPS);
server2.AddDefaultHandlers(GetTestDataFilePath());
server2.SetSSLConfig(net::EmbeddedTestServer::CERT_OK);
ASSERT_TRUE(server2.Start());
GURL initial_url = server2.GetURL("/title1.html");
GURL final_url = GetSecFetchUrl();
EXPECT_EQ(initial_url.scheme(), final_url.scheme());
EXPECT_EQ(initial_url.host(), final_url.host());
EXPECT_NE(initial_url.port(), final_url.port());
// Navigate to (paraphrasing): https://foo.com:port1/...
ASSERT_TRUE(NavigateToURL(shell(), initial_url));
// Navigate to (paraphrasing): https://foo.com:port2/... when the navigation
// is initiated from (paraphrasing): https://foo.com:port1/...
{
TestNavigationObserver nav_observer(shell()->web_contents());
ASSERT_TRUE(ExecJs(shell(), JsReplace("location = $1", final_url)));
nav_observer.Wait();
}
// Verify that https://foo.com:port1 is treated as same-site wrt
// https://foo.com:port2.
EXPECT_EQ("same-site", GetContent());
}
// This test verifies presence of a correct ("replayed") Sec-Fetch-Site HTTP
// request header in a history/back navigation.
//
// This is a regression test for https://crbug.com/946503.
//
// This test is slightly redundant with
// wpt/fetch/metadata/history.tentative.https.sub.html
// but it tests history navigations that are browser-initiated
// (e.g. as-if they were initiated by Chrome UI, not by javascript).
IN_PROC_BROWSER_TEST_F(SecFetchBrowserTest, BackNavigation) {
// Start the test at |initial_url|.
ASSERT_TRUE(embedded_test_server()->Start());
GURL initial_url(GetUrl("/title1.html"));
ASSERT_TRUE(NavigateToURL(shell(), initial_url));
// Renderer-initiated navigation to same-origin |main_url|.
GURL main_url(GetSecFetchUrl());
EXPECT_EQ(url::Origin::Create(initial_url), url::Origin::Create(main_url));
{
TestNavigationObserver nav_observer(shell()->web_contents(), 1);
ASSERT_TRUE(ExecJs(shell(), JsReplace("window.location = $1", main_url)));
nav_observer.Wait();
EXPECT_EQ("same-origin", GetContent());
}
// Renderer-initiated navigation to |cross_origin_url|.
{
GURL cross_origin_url(embedded_test_server()->GetURL("/title1.html"));
EXPECT_NE(url::Origin::Create(main_url),
url::Origin::Create(cross_origin_url));
TestNavigationObserver nav_observer(shell()->web_contents(), 1);
ASSERT_TRUE(
ExecJs(shell(), JsReplace("window.location = $1", cross_origin_url)));
nav_observer.Wait();
}
// Go back and verify that `Sec-Fetch-Site: same-origin` is again sent to the
// server.
{
TestNavigationObserver nav_observer(shell()->web_contents(), 1);
shell()->web_contents()->GetController().GoBack();
nav_observer.Wait();
EXPECT_EQ(main_url, shell()->web_contents()->GetLastCommittedURL());
EXPECT_EQ("same-origin", GetContent());
}
}
// This test verifies presence of a correct ("replayed") Sec-Fetch-Site HTTP
// request header in a history/reload navigation.
//
// This is a regression test for https://crbug.com/946503.
IN_PROC_BROWSER_TEST_F(SecFetchBrowserTest, ReloadNavigation) {
// Start the test at |initial_url|.
ASSERT_TRUE(embedded_test_server()->Start());
GURL initial_url(GetUrl("/title1.html"));
ASSERT_TRUE(NavigateToURL(shell(), initial_url));
// Renderer-initiated navigation to same-origin |main_url|.
GURL main_url(GetSecFetchUrl());
EXPECT_EQ(url::Origin::Create(initial_url), url::Origin::Create(main_url));
{
TestNavigationObserver nav_observer(shell()->web_contents(), 1);
ASSERT_TRUE(ExecJs(shell(), JsReplace("window.location = $1", main_url)));
nav_observer.Wait();
EXPECT_EQ("same-origin", GetContent());
}
// Reload and verify that `Sec-Fetch-Site: same-origin` is again sent to the
// server.
{
TestNavigationObserver nav_observer(shell()->web_contents(), 1);
shell()->web_contents()->GetController().Reload(ReloadType::BYPASSING_CACHE,
true);
nav_observer.Wait();
EXPECT_EQ(main_url, shell()->web_contents()->GetLastCommittedURL());
EXPECT_EQ("same-origin", GetContent());
}
}
} // namespace content
|