OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <set> |
5 #include <string> | 6 #include <string> |
6 | 7 |
7 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
8 #include "base/file_path.h" | 9 #include "base/file_path.h" |
9 #include "base/platform_file.h" | 10 #include "base/platform_file.h" |
10 #include "content/browser/child_process_security_policy.h" | 11 #include "content/browser/child_process_security_policy.h" |
| 12 #include "content/browser/mock_content_browser_client.h" |
11 #include "content/common/test_url_constants.h" | 13 #include "content/common/test_url_constants.h" |
12 #include "content/public/common/url_constants.h" | 14 #include "content/public/common/url_constants.h" |
13 #include "net/url_request/url_request.h" | 15 #include "googleurl/src/gurl.h" |
14 #include "net/url_request/url_request_test_job.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
16 | 17 |
17 class ChildProcessSecurityPolicyTest : public testing::Test { | 18 namespace { |
18 protected: | 19 |
19 // testing::Test | 20 const int kRendererID = 42; |
20 virtual void SetUp() { | 21 const int kWorkerRendererID = kRendererID + 1; |
21 // In the real world, "chrome:" is a handled scheme. | 22 |
22 RegisterProtocolFactory(chrome::kChromeUIScheme, | 23 class ChildProcessSecurityPolicyTestBrowserClient : |
23 &net::URLRequestTestJob::Factory); | 24 public content::MockContentBrowserClient { |
24 } | 25 public: |
25 virtual void TearDown() { | 26 ChildProcessSecurityPolicyTestBrowserClient() {} |
26 RegisterProtocolFactory(chrome::kChromeUIScheme, NULL); | 27 |
| 28 virtual bool IsHandledURL(const GURL& url) { |
| 29 return schemes_.find(url.scheme()) != schemes_.end(); |
27 } | 30 } |
28 | 31 |
29 static net::URLRequest::ProtocolFactory* RegisterProtocolFactory( | 32 void ClearSchemes() { |
30 const std::string& scheme, | 33 schemes_.clear(); |
31 net::URLRequest::ProtocolFactory* factory) { | |
32 return net::URLRequest::Deprecated::RegisterProtocolFactory( | |
33 scheme, factory); | |
34 } | 34 } |
| 35 |
| 36 void AddScheme(const std::string& scheme) { |
| 37 schemes_.insert(scheme); |
| 38 } |
| 39 |
| 40 private: |
| 41 std::set<std::string> schemes_; |
35 }; | 42 }; |
36 | 43 |
37 static int kRendererID = 42; | 44 } // namespace |
38 static int kWorkerRendererID = kRendererID + 1; | 45 |
| 46 class ChildProcessSecurityPolicyTest : public testing::Test { |
| 47 public: |
| 48 ChildProcessSecurityPolicyTest() : old_browser_client_(NULL) { |
| 49 } |
| 50 |
| 51 virtual void SetUp() { |
| 52 old_browser_client_ = content::GetContentClient()->browser(); |
| 53 content::GetContentClient()->set_browser(&test_browser_client_); |
| 54 |
| 55 // Claim to always handle chrome:// URLs because the CPSP's notion of |
| 56 // allowing WebUI bindings is hard-wired to this particular scheme. |
| 57 test_browser_client_.AddScheme("chrome"); |
| 58 } |
| 59 |
| 60 virtual void TearDown() { |
| 61 test_browser_client_.ClearSchemes(); |
| 62 content::GetContentClient()->set_browser(old_browser_client_); |
| 63 } |
| 64 |
| 65 protected: |
| 66 void RegisterTestScheme(const std::string& scheme) { |
| 67 test_browser_client_.AddScheme(scheme); |
| 68 } |
| 69 |
| 70 private: |
| 71 ChildProcessSecurityPolicyTestBrowserClient test_browser_client_; |
| 72 content::ContentBrowserClient* old_browser_client_; |
| 73 }; |
39 | 74 |
40 TEST_F(ChildProcessSecurityPolicyTest, IsWebSafeSchemeTest) { | 75 TEST_F(ChildProcessSecurityPolicyTest, IsWebSafeSchemeTest) { |
41 ChildProcessSecurityPolicy* p = ChildProcessSecurityPolicy::GetInstance(); | 76 ChildProcessSecurityPolicy* p = ChildProcessSecurityPolicy::GetInstance(); |
42 | 77 |
43 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kHttpScheme)); | 78 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kHttpScheme)); |
44 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kHttpsScheme)); | 79 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kHttpsScheme)); |
45 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kFtpScheme)); | 80 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kFtpScheme)); |
46 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kDataScheme)); | 81 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kDataScheme)); |
47 EXPECT_TRUE(p->IsWebSafeScheme("feed")); | 82 EXPECT_TRUE(p->IsWebSafeScheme("feed")); |
48 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kBlobScheme)); | 83 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kBlobScheme)); |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 } | 202 } |
168 | 203 |
169 TEST_F(ChildProcessSecurityPolicyTest, RegisterWebSafeSchemeTest) { | 204 TEST_F(ChildProcessSecurityPolicyTest, RegisterWebSafeSchemeTest) { |
170 ChildProcessSecurityPolicy* p = ChildProcessSecurityPolicy::GetInstance(); | 205 ChildProcessSecurityPolicy* p = ChildProcessSecurityPolicy::GetInstance(); |
171 | 206 |
172 p->Add(kRendererID); | 207 p->Add(kRendererID); |
173 | 208 |
174 // Currently, "asdf" is destined for ShellExecute, so it is allowed. | 209 // Currently, "asdf" is destined for ShellExecute, so it is allowed. |
175 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("asdf:rockers"))); | 210 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("asdf:rockers"))); |
176 | 211 |
177 // Once we register a ProtocolFactory for "asdf", we default to deny. | 212 // Once we register "asdf", we default to deny. |
178 RegisterProtocolFactory("asdf", &net::URLRequestTestJob::Factory); | 213 RegisterTestScheme("asdf"); |
179 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("asdf:rockers"))); | 214 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("asdf:rockers"))); |
180 | 215 |
181 // We can allow new schemes by adding them to the whitelist. | 216 // We can allow new schemes by adding them to the whitelist. |
182 p->RegisterWebSafeScheme("asdf"); | 217 p->RegisterWebSafeScheme("asdf"); |
183 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("asdf:rockers"))); | 218 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("asdf:rockers"))); |
184 | 219 |
185 // Cleanup. | 220 // Cleanup. |
186 RegisterProtocolFactory("asdf", NULL); | |
187 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("asdf:rockers"))); | |
188 | |
189 p->Remove(kRendererID); | 221 p->Remove(kRendererID); |
190 } | 222 } |
191 | 223 |
192 TEST_F(ChildProcessSecurityPolicyTest, CanServiceCommandsTest) { | 224 TEST_F(ChildProcessSecurityPolicyTest, CanServiceCommandsTest) { |
193 ChildProcessSecurityPolicy* p = ChildProcessSecurityPolicy::GetInstance(); | 225 ChildProcessSecurityPolicy* p = ChildProcessSecurityPolicy::GetInstance(); |
194 | 226 |
195 p->Add(kRendererID); | 227 p->Add(kRendererID); |
196 | 228 |
197 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd"))); | 229 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd"))); |
198 p->GrantRequestURL(kRendererID, GURL("file:///etc/passwd")); | 230 p->GrantRequestURL(kRendererID, GURL("file:///etc/passwd")); |
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
430 | 462 |
431 // Renderers are added and removed on the UI thread, but the policy can be | 463 // Renderers are added and removed on the UI thread, but the policy can be |
432 // queried on the IO thread. The ChildProcessSecurityPolicy needs to be | 464 // queried on the IO thread. The ChildProcessSecurityPolicy needs to be |
433 // prepared to answer policy questions about renderers who no longer exist. | 465 // prepared to answer policy questions about renderers who no longer exist. |
434 | 466 |
435 // In this case, we default to secure behavior. | 467 // In this case, we default to secure behavior. |
436 EXPECT_FALSE(p->CanRequestURL(kRendererID, url)); | 468 EXPECT_FALSE(p->CanRequestURL(kRendererID, url)); |
437 EXPECT_FALSE(p->CanReadFile(kRendererID, file)); | 469 EXPECT_FALSE(p->CanReadFile(kRendererID, file)); |
438 EXPECT_FALSE(p->HasWebUIBindings(kRendererID)); | 470 EXPECT_FALSE(p->HasWebUIBindings(kRendererID)); |
439 } | 471 } |
OLD | NEW |