[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 1 | // Copyright (c) 2013 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 | |
| 5 | #include "base/command_line.h" |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 6 | #include "base/containers/hash_tables.h" |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 7 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 8 | #include "content/browser/dom_storage/dom_storage_context_wrapper.h" |
| 9 | #include "content/browser/dom_storage/session_storage_namespace_impl.h" |
[email protected] | 65920f33 | 2014-03-04 21:14:18 | [diff] [blame] | 10 | #include "content/browser/frame_host/navigator.h" |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 11 | #include "content/browser/frame_host/render_frame_host_impl.h" |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 12 | #include "content/browser/renderer_host/render_view_host_factory.h" |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 13 | #include "content/browser/renderer_host/render_view_host_impl.h" |
| 14 | #include "content/browser/web_contents/web_contents_impl.h" |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 15 | #include "content/common/frame_messages.h" |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 16 | #include "content/common/resource_messages.h" |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 17 | #include "content/common/view_messages.h" |
| 18 | #include "content/public/browser/browser_context.h" |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 19 | #include "content/public/browser/content_browser_client.h" |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 20 | #include "content/public/browser/interstitial_page.h" |
| 21 | #include "content/public/browser/interstitial_page_delegate.h" |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 22 | #include "content/public/browser/storage_partition.h" |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 23 | #include "content/public/common/appcache_info.h" |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 24 | #include "content/public/common/content_switches.h" |
wfh | 815c487 | 2015-02-25 21:01:31 | [diff] [blame] | 25 | #include "content/public/common/file_chooser_params.h" |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 26 | #include "content/public/test/browser_test_utils.h" |
[email protected] | 6e9def1 | 2014-03-27 20:23:28 | [diff] [blame] | 27 | #include "content/public/test/content_browser_test.h" |
| 28 | #include "content/public/test/content_browser_test_utils.h" |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 29 | #include "content/public/test/test_utils.h" |
[email protected] | de7d61ff | 2013-08-20 11:30:41 | [diff] [blame] | 30 | #include "content/shell/browser/shell.h" |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 31 | #include "content/test/test_content_browser_client.h" |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 32 | #include "ipc/ipc_security_test_util.h" |
| 33 | #include "net/dns/mock_host_resolver.h" |
| 34 | #include "net/test/embedded_test_server/embedded_test_server.h" |
| 35 | |
| 36 | using IPC::IpcSecurityTestUtil; |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 37 | |
| 38 | namespace content { |
| 39 | |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 40 | namespace { |
| 41 | |
| 42 | // This is a helper function for the tests which attempt to create a |
| 43 | // duplicate RenderViewHost or RenderWidgetHost. It tries to create two objects |
| 44 | // with the same process and routing ids, which causes a collision. |
| 45 | // It creates a couple of windows in process 1, which causes a few routing ids |
| 46 | // to be allocated. Then a cross-process navigation is initiated, which causes a |
| 47 | // new process 2 to be created and have a pending RenderViewHost for it. The |
| 48 | // routing id of the RenderViewHost which is target for a duplicate is set |
| 49 | // into |target_routing_id| and the pending RenderViewHost which is used for |
| 50 | // the attempt is the return value. |
| 51 | RenderViewHostImpl* PrepareToDuplicateHosts(Shell* shell, |
| 52 | int* target_routing_id) { |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 53 | GURL foo("http://foo.com/simple_page.html"); |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 54 | |
| 55 | // Start off with initial navigation, so we get the first process allocated. |
| 56 | NavigateToURL(shell, foo); |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 57 | EXPECT_EQ(base::ASCIIToUTF16("OK"), shell->web_contents()->GetTitle()); |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 58 | |
| 59 | // Open another window, so we generate some more routing ids. |
| 60 | ShellAddedObserver shell2_observer; |
| 61 | EXPECT_TRUE(ExecuteScript( |
| 62 | shell->web_contents(), "window.open(document.URL + '#2');")); |
| 63 | Shell* shell2 = shell2_observer.GetShell(); |
| 64 | |
| 65 | // The new window must be in the same process, but have a new routing id. |
| 66 | EXPECT_EQ(shell->web_contents()->GetRenderViewHost()->GetProcess()->GetID(), |
| 67 | shell2->web_contents()->GetRenderViewHost()->GetProcess()->GetID()); |
| 68 | *target_routing_id = |
| 69 | shell2->web_contents()->GetRenderViewHost()->GetRoutingID(); |
| 70 | EXPECT_NE(*target_routing_id, |
| 71 | shell->web_contents()->GetRenderViewHost()->GetRoutingID()); |
| 72 | |
| 73 | // Now, simulate a link click coming from the renderer. |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 74 | GURL extension_url("https://bar.com/simple_page.html"); |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 75 | WebContentsImpl* wc = static_cast<WebContentsImpl*>(shell->web_contents()); |
[email protected] | 65920f33 | 2014-03-04 21:14:18 | [diff] [blame] | 76 | wc->GetFrameTree()->root()->navigator()->RequestOpenURL( |
lfg | 9ef7d2d | 2014-12-15 22:32:30 | [diff] [blame] | 77 | wc->GetFrameTree()->root()->current_frame_host(), extension_url, nullptr, |
nick | 94144d4 | 2015-04-27 19:21:40 | [diff] [blame] | 78 | Referrer(), CURRENT_TAB, false, true); |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 79 | |
| 80 | // Since the navigation above requires a cross-process swap, there will be a |
carlosk | c49005eb | 2015-06-16 11:25:07 | [diff] [blame] | 81 | // speculative/pending RenderFrameHost. Ensure it exists and is in a different |
| 82 | // process than the initial page. |
| 83 | RenderFrameHostImpl* next_rfh; |
| 84 | if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 85 | switches::kEnableBrowserSideNavigation)) { |
| 86 | next_rfh = |
clamy | 11e1151 | 2015-07-07 16:42:17 | [diff] [blame] | 87 | wc->GetRenderManagerForTesting()->speculative_frame_host(); |
carlosk | c49005eb | 2015-06-16 11:25:07 | [diff] [blame] | 88 | } else { |
| 89 | next_rfh = wc->GetRenderManagerForTesting()->pending_frame_host(); |
| 90 | } |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 91 | |
carlosk | c49005eb | 2015-06-16 11:25:07 | [diff] [blame] | 92 | EXPECT_TRUE(next_rfh); |
| 93 | EXPECT_NE(shell->web_contents()->GetRenderProcessHost()->GetID(), |
| 94 | next_rfh->GetProcess()->GetID()); |
| 95 | |
| 96 | return next_rfh->render_view_host(); |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 97 | } |
| 98 | |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 99 | ResourceHostMsg_Request CreateXHRRequestWithOrigin(const char* origin) { |
| 100 | ResourceHostMsg_Request request; |
| 101 | request.method = "GET"; |
| 102 | request.url = GURL("http://bar.com/simple_page.html"); |
| 103 | request.first_party_for_cookies = GURL(origin); |
| 104 | request.referrer_policy = blink::WebReferrerPolicyDefault; |
| 105 | request.headers = base::StringPrintf("Origin: %s\r\n", origin); |
| 106 | request.load_flags = 0; |
| 107 | request.origin_pid = 0; |
| 108 | request.resource_type = RESOURCE_TYPE_XHR; |
| 109 | request.request_context = 0; |
| 110 | request.appcache_host_id = kAppCacheNoHostId; |
| 111 | request.download_to_file = false; |
| 112 | request.should_reset_appcache = false; |
| 113 | request.is_main_frame = true; |
| 114 | request.parent_is_main_frame = false; |
| 115 | request.parent_render_frame_id = -1; |
| 116 | request.transition_type = ui::PAGE_TRANSITION_LINK; |
| 117 | request.allow_download = true; |
| 118 | return request; |
| 119 | } |
| 120 | |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 121 | } // namespace |
| 122 | |
| 123 | |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 124 | // The goal of these tests will be to "simulate" exploited renderer processes, |
| 125 | // which can send arbitrary IPC messages and confuse browser process internal |
| 126 | // state, leading to security bugs. We are trying to verify that the browser |
| 127 | // doesn't perform any dangerous operations in such cases. |
| 128 | class SecurityExploitBrowserTest : public ContentBrowserTest { |
| 129 | public: |
| 130 | SecurityExploitBrowserTest() {} |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 131 | |
avi | 83883c8 | 2014-12-23 00:08:49 | [diff] [blame] | 132 | void SetUpCommandLine(base::CommandLine* command_line) override { |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 133 | ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 134 | |
| 135 | // Add a host resolver rule to map all outgoing requests to the test server. |
| 136 | // This allows us to use "real" hostnames in URLs, which we can use to |
| 137 | // create arbitrary SiteInstances. |
| 138 | command_line->AppendSwitchASCII( |
| 139 | switches::kHostResolverRules, |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 140 | "MAP * " + |
| 141 | net::HostPortPair::FromURL(embedded_test_server()->base_url()) |
| 142 | .ToString() + |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 143 | ",EXCLUDE localhost"); |
| 144 | } |
wfh | 815c487 | 2015-02-25 21:01:31 | [diff] [blame] | 145 | |
| 146 | protected: |
| 147 | // Tests that a given file path sent in a ViewHostMsg_RunFileChooser will |
| 148 | // cause renderer to be killed. |
| 149 | void TestFileChooserWithPath(const base::FilePath& path); |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 150 | }; |
| 151 | |
wfh | 815c487 | 2015-02-25 21:01:31 | [diff] [blame] | 152 | void SecurityExploitBrowserTest::TestFileChooserWithPath( |
| 153 | const base::FilePath& path) { |
| 154 | GURL foo("http://foo.com/simple_page.html"); |
| 155 | NavigateToURL(shell(), foo); |
| 156 | EXPECT_EQ(base::ASCIIToUTF16("OK"), shell()->web_contents()->GetTitle()); |
| 157 | |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 158 | RenderViewHost* compromised_renderer = |
wfh | 815c487 | 2015-02-25 21:01:31 | [diff] [blame] | 159 | shell()->web_contents()->GetRenderViewHost(); |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 160 | RenderProcessHostWatcher terminated( |
wfh | 815c487 | 2015-02-25 21:01:31 | [diff] [blame] | 161 | shell()->web_contents(), |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 162 | RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT); |
wfh | 815c487 | 2015-02-25 21:01:31 | [diff] [blame] | 163 | |
| 164 | FileChooserParams params; |
| 165 | params.default_file_name = path; |
| 166 | |
| 167 | ViewHostMsg_RunFileChooser evil(compromised_renderer->GetRoutingID(), params); |
| 168 | |
| 169 | IpcSecurityTestUtil::PwnMessageReceived( |
| 170 | compromised_renderer->GetProcess()->GetChannel(), evil); |
| 171 | terminated.Wait(); |
| 172 | } |
| 173 | |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 174 | // Ensure that we kill the renderer process if we try to give it WebUI |
| 175 | // properties and it doesn't have enabled WebUI bindings. |
jaekyun | 37e572a3 | 2014-12-04 23:33:35 | [diff] [blame] | 176 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, SetWebUIProperty) { |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 177 | GURL foo("http://foo.com/simple_page.html"); |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 178 | |
| 179 | NavigateToURL(shell(), foo); |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 180 | EXPECT_EQ(base::ASCIIToUTF16("OK"), shell()->web_contents()->GetTitle()); |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 181 | EXPECT_EQ(0, |
| 182 | shell()->web_contents()->GetRenderViewHost()->GetEnabledBindings()); |
| 183 | |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 184 | RenderProcessHostWatcher terminated( |
[email protected] | 8ffad4e | 2014-01-02 23:18:26 | [diff] [blame] | 185 | shell()->web_contents(), |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 186 | RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT); |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 187 | shell()->web_contents()->GetRenderViewHost()->SetWebUIProperty( |
| 188 | "toolkit", "views"); |
| 189 | terminated.Wait(); |
| 190 | } |
| 191 | |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 192 | // This is a test for crbug.com/312016 attempting to create duplicate |
| 193 | // RenderViewHosts. SetupForDuplicateHosts sets up this test case and leaves |
| 194 | // it in a state with pending RenderViewHost. Before the commit of the new |
| 195 | // pending RenderViewHost, this test case creates a new window through the new |
| 196 | // process. |
| 197 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, |
| 198 | AttemptDuplicateRenderViewHost) { |
dcheng | 3ce04b6 | 2015-10-26 23:30:55 | [diff] [blame^] | 199 | int32_t duplicate_routing_id = MSG_ROUTING_NONE; |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 200 | RenderViewHostImpl* pending_rvh = |
| 201 | PrepareToDuplicateHosts(shell(), &duplicate_routing_id); |
| 202 | EXPECT_NE(MSG_ROUTING_NONE, duplicate_routing_id); |
| 203 | |
| 204 | // Since this test executes on the UI thread and hopping threads might cause |
| 205 | // different timing in the test, let's simulate a CreateNewWindow call coming |
| 206 | // from the IO thread. |
| 207 | ViewHostMsg_CreateWindow_Params params; |
| 208 | DOMStorageContextWrapper* dom_storage_context = |
| 209 | static_cast<DOMStorageContextWrapper*>( |
| 210 | BrowserContext::GetStoragePartition( |
| 211 | shell()->web_contents()->GetBrowserContext(), |
| 212 | pending_rvh->GetSiteInstance())->GetDOMStorageContext()); |
[email protected] | 4af62451 | 2013-12-13 14:58:43 | [diff] [blame] | 213 | scoped_refptr<SessionStorageNamespaceImpl> session_storage( |
| 214 | new SessionStorageNamespaceImpl(dom_storage_context)); |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 215 | // Cause a deliberate collision in routing ids. |
dcheng | 3ce04b6 | 2015-10-26 23:30:55 | [diff] [blame^] | 216 | int32_t main_frame_routing_id = duplicate_routing_id + 1; |
| 217 | // TODO(avi): This should be made unique from the view routing ID once |
| 218 | // RenderViewHostImpl has-a RenderWidgetHostImpl. https://crbug.com/545684 |
| 219 | int32_t main_frame_widget_routing_id = duplicate_routing_id; |
| 220 | pending_rvh->CreateNewWindow(duplicate_routing_id, main_frame_routing_id, |
| 221 | main_frame_widget_routing_id, params, |
dcheng | 54c3719d | 2014-08-26 21:52:56 | [diff] [blame] | 222 | session_storage.get()); |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 223 | |
| 224 | // If the above operation doesn't cause a crash, the test has succeeded! |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 225 | } |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 226 | |
[email protected] | a850402 | 2013-12-04 20:23:51 | [diff] [blame] | 227 | // This is a test for crbug.com/312016. It tries to create two RenderWidgetHosts |
| 228 | // with the same process and routing ids, which causes a collision. It is almost |
| 229 | // identical to the AttemptDuplicateRenderViewHost test case. |
| 230 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, |
| 231 | AttemptDuplicateRenderWidgetHost) { |
| 232 | int duplicate_routing_id = MSG_ROUTING_NONE; |
| 233 | RenderViewHostImpl* pending_rvh = |
| 234 | PrepareToDuplicateHosts(shell(), &duplicate_routing_id); |
| 235 | EXPECT_NE(MSG_ROUTING_NONE, duplicate_routing_id); |
| 236 | |
| 237 | // Since this test executes on the UI thread and hopping threads might cause |
| 238 | // different timing in the test, let's simulate a CreateNewWidget call coming |
| 239 | // from the IO thread. Use the existing window routing id to cause a |
| 240 | // deliberate collision. |
piman | 5d36dae | 2015-09-24 22:47:05 | [diff] [blame] | 241 | pending_rvh->CreateNewWidget(duplicate_routing_id, blink::WebPopupTypePage); |
[email protected] | a850402 | 2013-12-04 20:23:51 | [diff] [blame] | 242 | |
| 243 | // If the above operation doesn't crash, the test has succeeded! |
| 244 | } |
| 245 | |
wfh | 815c487 | 2015-02-25 21:01:31 | [diff] [blame] | 246 | // This is a test for crbug.com/444198. It tries to send a |
| 247 | // ViewHostMsg_RunFileChooser containing an invalid path. The browser should |
| 248 | // correctly terminate the renderer in these cases. |
| 249 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, AttemptRunFileChoosers) { |
| 250 | TestFileChooserWithPath(base::FilePath(FILE_PATH_LITERAL("../../*.txt"))); |
| 251 | TestFileChooserWithPath(base::FilePath(FILE_PATH_LITERAL("/etc/*.conf"))); |
| 252 | #if defined(OS_WIN) |
| 253 | TestFileChooserWithPath( |
| 254 | base::FilePath(FILE_PATH_LITERAL("\\\\evilserver\\evilshare\\*.txt"))); |
| 255 | TestFileChooserWithPath(base::FilePath(FILE_PATH_LITERAL("c:\\*.txt"))); |
| 256 | TestFileChooserWithPath(base::FilePath(FILE_PATH_LITERAL("..\\..\\*.txt"))); |
| 257 | #endif |
| 258 | } |
| 259 | |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 260 | class SecurityExploitTestInterstitialPage : public InterstitialPageDelegate { |
| 261 | public: |
| 262 | explicit SecurityExploitTestInterstitialPage(WebContents* contents) { |
| 263 | InterstitialPage* interstitial = InterstitialPage::Create( |
| 264 | contents, true, contents->GetLastCommittedURL(), this); |
| 265 | interstitial->Show(); |
| 266 | } |
| 267 | |
| 268 | // InterstitialPageDelegate implementation. |
| 269 | void CommandReceived(const std::string& command) override { |
| 270 | last_command_ = command; |
| 271 | } |
| 272 | |
| 273 | std::string GetHTMLContents() override { |
| 274 | return "<html><head><script>" |
| 275 | "window.domAutomationController.setAutomationId(1);" |
| 276 | "window.domAutomationController.send(\"okay\");" |
| 277 | "</script></head>" |
| 278 | "<body>this page is an interstitial</body></html>"; |
| 279 | } |
| 280 | |
| 281 | std::string last_command() { return last_command_; } |
| 282 | |
| 283 | private: |
| 284 | std::string last_command_; |
| 285 | DISALLOW_COPY_AND_ASSIGN(SecurityExploitTestInterstitialPage); |
| 286 | }; |
| 287 | |
| 288 | // Fails due to InterstitialPage's reliance on PostNonNestableTask |
| 289 | // http://crbug.com/432737 |
| 290 | #if defined(OS_ANDROID) |
| 291 | #define MAYBE_InterstitialCommandFromUnderlyingContent \ |
| 292 | DISABLED_InterstitialCommandFromUnderlyingContent |
| 293 | #else |
| 294 | #define MAYBE_InterstitialCommandFromUnderlyingContent \ |
| 295 | InterstitialCommandFromUnderlyingContent |
| 296 | #endif |
| 297 | |
| 298 | // The interstitial should not be controllable by the underlying content. |
| 299 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, |
| 300 | MAYBE_InterstitialCommandFromUnderlyingContent) { |
| 301 | // Start off with initial navigation, to allocate the process. |
| 302 | GURL foo("http://foo.com/simple_page.html"); |
| 303 | NavigateToURL(shell(), foo); |
| 304 | EXPECT_EQ(base::ASCIIToUTF16("OK"), shell()->web_contents()->GetTitle()); |
| 305 | |
| 306 | DOMMessageQueue message_queue; |
| 307 | |
| 308 | // Install and show an interstitial page. |
| 309 | SecurityExploitTestInterstitialPage* interstitial = |
| 310 | new SecurityExploitTestInterstitialPage(shell()->web_contents()); |
| 311 | |
| 312 | ASSERT_EQ("", interstitial->last_command()); |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 313 | WaitForInterstitialAttach(shell()->web_contents()); |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 314 | |
| 315 | InterstitialPage* interstitial_page = |
| 316 | shell()->web_contents()->GetInterstitialPage(); |
| 317 | ASSERT_TRUE(interstitial_page != NULL); |
| 318 | ASSERT_TRUE(shell()->web_contents()->ShowingInterstitialPage()); |
| 319 | ASSERT_TRUE(interstitial_page->GetDelegateForTesting() == interstitial); |
| 320 | |
| 321 | // The interstitial page ought to be able to send a message. |
| 322 | std::string message; |
| 323 | ASSERT_TRUE(message_queue.WaitForMessage(&message)); |
| 324 | ASSERT_EQ("\"okay\"", message); |
| 325 | ASSERT_EQ("\"okay\"", interstitial->last_command()); |
| 326 | |
| 327 | // Send an automation message from the underlying content and wait for it to |
| 328 | // be dispatched on this thread. This message should not be received by the |
| 329 | // interstitial. |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 330 | RenderFrameHost* compromised_renderer = |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 331 | shell()->web_contents()->GetMainFrame(); |
| 332 | FrameHostMsg_DomOperationResponse evil(compromised_renderer->GetRoutingID(), |
avi | 60bd490 | 2015-09-23 20:39:24 | [diff] [blame] | 333 | "evil"); |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 334 | IpcSecurityTestUtil::PwnMessageReceived( |
| 335 | compromised_renderer->GetProcess()->GetChannel(), evil); |
| 336 | |
| 337 | ASSERT_TRUE(message_queue.WaitForMessage(&message)); |
| 338 | ASSERT_EQ("evil", message) |
| 339 | << "Automation message should be received by WebContents."; |
| 340 | ASSERT_EQ("\"okay\"", interstitial->last_command()) |
| 341 | << "Interstitial should not be affected."; |
| 342 | |
| 343 | // Send a second message from the interstitial page, and make sure that the |
| 344 | // "evil" message doesn't arrive in the intervening period. |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 345 | ASSERT_TRUE(ExecuteScript(interstitial_page->GetMainFrame(), |
| 346 | "window.domAutomationController.send(\"okay2\");")); |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 347 | ASSERT_TRUE(message_queue.WaitForMessage(&message)); |
| 348 | ASSERT_EQ("\"okay2\"", message); |
| 349 | ASSERT_EQ("\"okay2\"", interstitial->last_command()); |
| 350 | } |
| 351 | |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 352 | class IsolatedAppContentBrowserClient : public TestContentBrowserClient { |
| 353 | public: |
| 354 | bool IsIllegalOrigin(content::ResourceContext* resource_context, |
| 355 | int child_process_id, |
| 356 | const GURL& origin) override { |
| 357 | // Simulate a case where an app origin is not in an app process. |
| 358 | return true; |
| 359 | } |
| 360 | }; |
| 361 | |
| 362 | // Renderer processes should not be able to spoof Origin HTTP headers. |
| 363 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, InvalidOriginHeaders) { |
| 364 | // Create a set of IPC messages with various Origin headers. |
| 365 | ResourceHostMsg_Request chrome_origin_msg( |
| 366 | CreateXHRRequestWithOrigin("chrome://settings")); |
| 367 | ResourceHostMsg_Request embedder_isolated_origin_msg( |
| 368 | CreateXHRRequestWithOrigin("https://isolated.bar.com")); |
| 369 | ResourceHostMsg_Request invalid_origin_msg( |
| 370 | CreateXHRRequestWithOrigin("invalidurl")); |
| 371 | ResourceHostMsg_Request invalid_scheme_origin_msg( |
| 372 | CreateXHRRequestWithOrigin("fake-scheme://foo")); |
| 373 | |
| 374 | GURL web_url("http://foo.com/simple_page.html"); |
| 375 | NavigateToURL(shell(), web_url); |
| 376 | RenderFrameHost* web_rfh = shell()->web_contents()->GetMainFrame(); |
| 377 | |
| 378 | // Web processes cannot make XHRs with chrome:// Origin headers. |
| 379 | { |
| 380 | RenderProcessHostWatcher web_process_killed( |
| 381 | web_rfh->GetProcess(), |
| 382 | RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT); |
| 383 | IPC::IpcSecurityTestUtil::PwnMessageReceived( |
| 384 | web_rfh->GetProcess()->GetChannel(), |
| 385 | ResourceHostMsg_RequestResource(web_rfh->GetRoutingID(), 1, |
| 386 | chrome_origin_msg)); |
| 387 | web_process_killed.Wait(); |
| 388 | } |
| 389 | |
| 390 | // Web processes cannot make XHRs with URLs that the content embedder expects |
| 391 | // to have process isolation. Ideally this would test chrome-extension:// |
| 392 | // URLs for Chrome Apps, but those can't be tested inside content/ and the |
| 393 | // ResourceHostMsg_Request IPC can't be created in a test outside content/. |
| 394 | NavigateToURL(shell(), web_url); |
| 395 | { |
| 396 | // Set up a ContentBrowserClient that simulates an app URL in a non-app |
| 397 | // process. |
| 398 | IsolatedAppContentBrowserClient app_client; |
| 399 | ContentBrowserClient* old_client = SetBrowserClientForTesting(&app_client); |
| 400 | RenderProcessHostWatcher web_process_killed( |
| 401 | web_rfh->GetProcess(), |
| 402 | RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT); |
| 403 | IPC::IpcSecurityTestUtil::PwnMessageReceived( |
| 404 | web_rfh->GetProcess()->GetChannel(), |
| 405 | ResourceHostMsg_RequestResource(web_rfh->GetRoutingID(), 1, |
| 406 | embedder_isolated_origin_msg)); |
| 407 | web_process_killed.Wait(); |
| 408 | SetBrowserClientForTesting(old_client); |
| 409 | } |
| 410 | |
| 411 | // Web processes cannot make XHRs with invalid Origin headers. |
| 412 | NavigateToURL(shell(), web_url); |
| 413 | { |
| 414 | RenderProcessHostWatcher web_process_killed( |
| 415 | web_rfh->GetProcess(), |
| 416 | RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT); |
| 417 | IPC::IpcSecurityTestUtil::PwnMessageReceived( |
| 418 | web_rfh->GetProcess()->GetChannel(), |
| 419 | ResourceHostMsg_RequestResource(web_rfh->GetRoutingID(), 1, |
| 420 | invalid_origin_msg)); |
| 421 | web_process_killed.Wait(); |
| 422 | } |
| 423 | |
| 424 | // Web processes cannot make XHRs with invalid scheme Origin headers. |
| 425 | NavigateToURL(shell(), web_url); |
| 426 | { |
| 427 | RenderProcessHostWatcher web_process_killed( |
| 428 | web_rfh->GetProcess(), |
| 429 | RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT); |
| 430 | IPC::IpcSecurityTestUtil::PwnMessageReceived( |
| 431 | web_rfh->GetProcess()->GetChannel(), |
| 432 | ResourceHostMsg_RequestResource(web_rfh->GetRoutingID(), 1, |
| 433 | invalid_scheme_origin_msg)); |
| 434 | web_process_killed.Wait(); |
| 435 | } |
| 436 | } |
| 437 | |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 438 | } // namespace content |