Adam Langley | 573d3ac | 2018-04-28 00:32:13 | [diff] [blame] | 1 | // Copyright (c) 2018 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Sebastien Marchand | f1349f5 | 2019-01-25 03:16:41 | [diff] [blame] | 5 | #include "base/bind.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 6 | #include "base/memory/raw_ptr.h" |
Guido Urdaneta | ef4e9194 | 2020-11-09 15:06:24 | [diff] [blame] | 7 | #include "base/test/bind.h" |
Adam Langley | 573d3ac | 2018-04-28 00:32:13 | [diff] [blame] | 8 | #include "build/build_config.h" |
| 9 | #include "chrome/browser/devtools/devtools_window_testing.h" |
Adam Langley | 573d3ac | 2018-04-28 00:32:13 | [diff] [blame] | 10 | #include "chrome/browser/ui/browser.h" |
| 11 | #include "chrome/browser/ui/browser_commands.h" |
Evan Stade | 9ce4260 | 2019-07-29 23:56:14 | [diff] [blame] | 12 | #include "chrome/browser/ui/browser_list.h" |
Nina Satragno | 8db5a0d | 2019-04-08 14:37:32 | [diff] [blame] | 13 | #include "chrome/browser/webauthn/authenticator_request_scheduler.h" |
| 14 | #include "chrome/browser/webauthn/chrome_authenticator_request_delegate.h" |
Adam Langley | 573d3ac | 2018-04-28 00:32:13 | [diff] [blame] | 15 | #include "chrome/test/base/in_process_browser_test.h" |
| 16 | #include "chrome/test/base/interactive_test_utils.h" |
Evan Stade | 4487c45 | 2019-07-11 00:58:06 | [diff] [blame] | 17 | #include "chrome/test/base/ui_test_utils.h" |
Adam Langley | 573d3ac | 2018-04-28 00:32:13 | [diff] [blame] | 18 | #include "components/network_session_configurator/common/network_switches.h" |
Nina Satragno | acf403f9 | 2019-05-23 17:16:52 | [diff] [blame] | 19 | #include "content/public/browser/authenticator_environment.h" |
Peter Kasting | 919ce65 | 2020-05-07 10:22:36 | [diff] [blame] | 20 | #include "content/public/test/browser_test.h" |
Adam Langley | 573d3ac | 2018-04-28 00:32:13 | [diff] [blame] | 21 | #include "content/public/test/browser_test_utils.h" |
Nina Satragno | acf403f9 | 2019-05-23 17:16:52 | [diff] [blame] | 22 | #include "device/fido/virtual_fido_device_factory.h" |
Adam Langley | 573d3ac | 2018-04-28 00:32:13 | [diff] [blame] | 23 | #include "net/dns/mock_host_resolver.h" |
| 24 | #include "net/test/embedded_test_server/embedded_test_server.h" |
| 25 | #include "testing/gmock/include/gmock/gmock.h" |
| 26 | |
| 27 | namespace { |
| 28 | |
| 29 | class WebAuthFocusTest : public InProcessBrowserTest, |
Nina Satragno | 8db5a0d | 2019-04-08 14:37:32 | [diff] [blame] | 30 | public AuthenticatorRequestDialogModel::Observer { |
Adam Langley | 573d3ac | 2018-04-28 00:32:13 | [diff] [blame] | 31 | protected: |
Nina Satragno | 8db5a0d | 2019-04-08 14:37:32 | [diff] [blame] | 32 | WebAuthFocusTest() |
| 33 | : https_server_(net::EmbeddedTestServer::TYPE_HTTPS), |
| 34 | permission_requested_(false) {} |
Adam Langley | 573d3ac | 2018-04-28 00:32:13 | [diff] [blame] | 35 | |
Peter Boström | fadb175 | 2021-09-30 19:17:01 | [diff] [blame] | 36 | WebAuthFocusTest(const WebAuthFocusTest&) = delete; |
| 37 | WebAuthFocusTest& operator=(const WebAuthFocusTest&) = delete; |
| 38 | |
Adam Langley | 573d3ac | 2018-04-28 00:32:13 | [diff] [blame] | 39 | void SetUpOnMainThread() override { |
| 40 | host_resolver()->AddRule("*", "127.0.0.1"); |
| 41 | https_server_.ServeFilesFromSourceDirectory("content/test/data"); |
| 42 | ASSERT_TRUE(https_server_.Start()); |
| 43 | } |
| 44 | |
| 45 | GURL GetHttpsURL(const std::string& hostname, |
| 46 | const std::string& relative_url) { |
| 47 | return https_server_.GetURL(hostname, relative_url); |
| 48 | } |
| 49 | |
Nina Satragno | 8db5a0d | 2019-04-08 14:37:32 | [diff] [blame] | 50 | bool permission_requested() { return permission_requested_; } |
| 51 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 52 | raw_ptr<AuthenticatorRequestDialogModel> dialog_model_; |
Adam Langley | 573d3ac | 2018-04-28 00:32:13 | [diff] [blame] | 53 | |
| 54 | private: |
| 55 | void SetUpCommandLine(base::CommandLine* command_line) override { |
| 56 | command_line->AppendSwitch(switches::kIgnoreCertificateErrors); |
| 57 | } |
| 58 | |
Nina Satragno | 8db5a0d | 2019-04-08 14:37:32 | [diff] [blame] | 59 | // AuthenticatorRequestDialogModel::Observer: |
| 60 | void OnStepTransition() override { |
| 61 | if (dialog_model_->current_step() != |
| 62 | AuthenticatorRequestDialogModel::Step::kAttestationPermissionRequest) |
| 63 | return; |
| 64 | |
| 65 | // Simulate accepting the permission request. |
| 66 | dialog_model_->OnAttestationPermissionResponse(true); |
| 67 | permission_requested_ = true; |
| 68 | } |
| 69 | |
Nina Satragno | db5b129 | 2021-02-19 20:33:37 | [diff] [blame] | 70 | void OnModelDestroyed(AuthenticatorRequestDialogModel* model) override {} |
Nina Satragno | 8db5a0d | 2019-04-08 14:37:32 | [diff] [blame] | 71 | |
Adam Langley | 573d3ac | 2018-04-28 00:32:13 | [diff] [blame] | 72 | net::EmbeddedTestServer https_server_; |
| 73 | |
Nina Satragno | 8db5a0d | 2019-04-08 14:37:32 | [diff] [blame] | 74 | // Set to true when the permission sheet is triggered. |
| 75 | bool permission_requested_; |
Adam Langley | 573d3ac | 2018-04-28 00:32:13 | [diff] [blame] | 76 | }; |
| 77 | |
Takumi Fujimoto | 5a46d691 | 2021-06-22 20:55:47 | [diff] [blame] | 78 | // TODO(crbug.com/1222768): Disabled for being flaky. |
| 79 | IN_PROC_BROWSER_TEST_F(WebAuthFocusTest, DISABLED_Focus) { |
Adam Langley | 573d3ac | 2018-04-28 00:32:13 | [diff] [blame] | 80 | // Web Authentication requests will often trigger machine-wide indications, |
| 81 | // such as a Security Key flashing for a touch. If background tabs were able |
| 82 | // to trigger this, there would be a risk of user confusion since the user |
| 83 | // would not know which tab they would be interacting with if they touched a |
| 84 | // Security Key. Because of that, some Web Authentication APIs require that |
| 85 | // the frame be in the foreground in a focused window. |
| 86 | |
| 87 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
Lukasz Anforowicz | b78290c | 2021-09-08 04:31:38 | [diff] [blame] | 88 | ASSERT_TRUE(ui_test_utils::NavigateToURL( |
| 89 | browser(), GetHttpsURL("www.example.com", "/title1.html"))); |
Adam Langley | 573d3ac | 2018-04-28 00:32:13 | [diff] [blame] | 90 | |
Nina Satragno | acf403f9 | 2019-05-23 17:16:52 | [diff] [blame] | 91 | auto owned_virtual_device_factory = |
| 92 | std::make_unique<device::test::VirtualFidoDeviceFactory>(); |
| 93 | auto* virtual_device_factory = owned_virtual_device_factory.get(); |
| 94 | content::AuthenticatorEnvironment::GetInstance() |
| 95 | ->ReplaceDefaultDiscoveryFactoryForTesting( |
| 96 | std::move(owned_virtual_device_factory)); |
Adam Langley | 573d3ac | 2018-04-28 00:32:13 | [diff] [blame] | 97 | |
| 98 | constexpr char kRegisterTemplate[] = |
| 99 | "navigator.credentials.create({publicKey: {" |
| 100 | " rp: {name: 't'}," |
| 101 | " user: {id: new Uint8Array([1]), name: 't', displayName: 't'}," |
| 102 | " challenge: new Uint8Array([1,2,3,4])," |
| 103 | " timeout: 10000," |
| 104 | " attestation: '$1'," |
| 105 | " pubKeyCredParams: [{type: 'public-key', alg: -7}]" |
| 106 | "}}).then(c => window.domAutomationController.send('OK')," |
| 107 | " e => window.domAutomationController.send(e.toString()));"; |
| 108 | const std::string register_script = base::ReplaceStringPlaceholders( |
| 109 | kRegisterTemplate, std::vector<std::string>{"none"}, nullptr); |
| 110 | |
| 111 | content::WebContents* const initial_web_contents = |
| 112 | browser()->tab_strip_model()->GetActiveWebContents(); |
| 113 | |
| 114 | std::string result; |
| 115 | // When operating in the foreground, the operation should succeed. |
| 116 | ASSERT_TRUE(content::ExecuteScriptAndExtractString(initial_web_contents, |
| 117 | register_script, &result)); |
| 118 | EXPECT_EQ(result, "OK"); |
| 119 | |
| 120 | // Open a new tab to put the previous page in the background. |
| 121 | chrome::NewTab(browser()); |
| 122 | |
| 123 | // When in the background, the same request should result in a focus error. |
| 124 | ASSERT_TRUE(content::ExecuteScriptAndExtractString(initial_web_contents, |
| 125 | register_script, &result)); |
| 126 | constexpr char kFocusErrorSubstring[] = "the page does not have focus"; |
| 127 | EXPECT_THAT(result, ::testing::HasSubstr(kFocusErrorSubstring)); |
| 128 | |
| 129 | // Close the tab and the action should succeed again. |
| 130 | chrome::CloseTab(browser()); |
| 131 | ASSERT_TRUE(content::ExecuteScriptAndExtractString(initial_web_contents, |
| 132 | register_script, &result)); |
| 133 | EXPECT_EQ(result, "OK"); |
| 134 | |
| 135 | // Start the request in the foreground and open a new tab between starting and |
| 136 | // finishing the request. This should fail because we don't want foreground |
| 137 | // pages to be able to start a request, open a trusted site in a new |
| 138 | // tab/window, and have the user believe that they are interacting with that |
| 139 | // trusted site. |
Nina Satragno | acf403f9 | 2019-05-23 17:16:52 | [diff] [blame] | 140 | virtual_device_factory->mutable_state()->simulate_press_callback = |
Nina Satragno | 9d6389e | 2019-06-14 21:21:35 | [diff] [blame] | 141 | base::BindRepeating( |
| 142 | [](Browser* browser, device::VirtualFidoDevice* device) { |
| 143 | chrome::NewTab(browser); |
| 144 | return true; |
| 145 | }, |
| 146 | browser()); |
Adam Langley | 573d3ac | 2018-04-28 00:32:13 | [diff] [blame] | 147 | ASSERT_TRUE(content::ExecuteScriptAndExtractString(initial_web_contents, |
| 148 | register_script, &result)); |
| 149 | EXPECT_THAT(result, ::testing::HasSubstr(kFocusErrorSubstring)); |
| 150 | |
| 151 | // Close the tab and the action should succeed again. |
| 152 | chrome::CloseTab(browser()); |
Nina Satragno | acf403f9 | 2019-05-23 17:16:52 | [diff] [blame] | 153 | virtual_device_factory->mutable_state()->simulate_press_callback.Reset(); |
Adam Langley | 573d3ac | 2018-04-28 00:32:13 | [diff] [blame] | 154 | ASSERT_TRUE(content::ExecuteScriptAndExtractString(initial_web_contents, |
| 155 | register_script, &result)); |
| 156 | EXPECT_EQ(result, "OK"); |
| 157 | |
| 158 | // Open dev tools and check that operations still succeed. |
| 159 | DevToolsWindow* dev_tools_window = |
| 160 | DevToolsWindowTesting::OpenDevToolsWindowSync( |
| 161 | initial_web_contents, true /* docked, not a separate window */); |
| 162 | ASSERT_TRUE(content::ExecuteScriptAndExtractString(initial_web_contents, |
| 163 | register_script, &result)); |
| 164 | EXPECT_EQ(result, "OK"); |
| 165 | DevToolsWindowTesting::CloseDevToolsWindowSync(dev_tools_window); |
| 166 | |
| 167 | // Open a second browser window. |
Adam Langley | 573d3ac | 2018-04-28 00:32:13 | [diff] [blame] | 168 | chrome::NewWindow(browser()); |
Evan Stade | 9ce4260 | 2019-07-29 23:56:14 | [diff] [blame] | 169 | Browser* new_window = BrowserList::GetInstance()->GetLastActive(); |
Adam Langley | 573d3ac | 2018-04-28 00:32:13 | [diff] [blame] | 170 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(new_window)); |
| 171 | |
Balazs Engedy | 9311dc3 | 2018-06-14 13:56:06 | [diff] [blame] | 172 | // Operations in the (now unfocused) window should still succeed, as the |
| 173 | // calling tab is still the active tab in that window. |
Adam Langley | 573d3ac | 2018-04-28 00:32:13 | [diff] [blame] | 174 | ASSERT_TRUE(content::ExecuteScriptAndExtractString(initial_web_contents, |
| 175 | register_script, &result)); |
Balazs Engedy | 9311dc3 | 2018-06-14 13:56:06 | [diff] [blame] | 176 | EXPECT_THAT(result, "OK"); |
Adam Langley | 573d3ac | 2018-04-28 00:32:13 | [diff] [blame] | 177 | |
| 178 | // Check that closing the window brings things back to a focused state. |
| 179 | chrome::CloseWindow(new_window); |
| 180 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
| 181 | ASSERT_TRUE(content::ExecuteScriptAndExtractString(initial_web_contents, |
| 182 | register_script, &result)); |
| 183 | EXPECT_EQ(result, "OK"); |
| 184 | |
| 185 | // Requesting "direct" attestation will trigger a permissions prompt. |
Nina Satragno | acf403f9 | 2019-05-23 17:16:52 | [diff] [blame] | 186 | virtual_device_factory->mutable_state()->simulate_press_callback = |
Nina Satragno | 9d6389e | 2019-06-14 21:21:35 | [diff] [blame] | 187 | base::BindLambdaForTesting([&](device::VirtualFidoDevice* device) { |
Nina Satragno | db5b129 | 2021-02-19 20:33:37 | [diff] [blame] | 188 | dialog_model_ = AuthenticatorRequestScheduler::GetRequestDelegate( |
| 189 | initial_web_contents) |
| 190 | ->dialog_model(); |
Nina Satragno | 8db5a0d | 2019-04-08 14:37:32 | [diff] [blame] | 191 | dialog_model_->AddObserver(this); |
Nina Satragno | 9d6389e | 2019-06-14 21:21:35 | [diff] [blame] | 192 | return true; |
Nina Satragno | 8db5a0d | 2019-04-08 14:37:32 | [diff] [blame] | 193 | }); |
| 194 | |
Adam Langley | 573d3ac | 2018-04-28 00:32:13 | [diff] [blame] | 195 | const std::string get_assertion_with_attestation_script = |
| 196 | base::ReplaceStringPlaceholders( |
| 197 | kRegisterTemplate, std::vector<std::string>{"direct"}, nullptr); |
Adam Langley | 573d3ac | 2018-04-28 00:32:13 | [diff] [blame] | 198 | ASSERT_TRUE(content::ExecuteScriptAndExtractString( |
| 199 | initial_web_contents, get_assertion_with_attestation_script, &result)); |
Nina Satragno | 8db5a0d | 2019-04-08 14:37:32 | [diff] [blame] | 200 | |
| 201 | EXPECT_TRUE(permission_requested()); |
Adam Langley | 573d3ac | 2018-04-28 00:32:13 | [diff] [blame] | 202 | EXPECT_EQ(result, "OK"); |
Adam Langley | 573d3ac | 2018-04-28 00:32:13 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | } // anonymous namespace |