blob: 55750bf0843016bf1f5d10746b35722ba90601ea [file] [log] [blame]
[email protected]b9535422012-02-09 01:47:591// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]46fb9442011-12-09 17:57:475#include <set>
initial.commit09911bf2008-07-26 23:55:296#include <string>
7
8#include "base/basictypes.h"
[email protected]57999812013-02-24 05:40:529#include "base/files/file_path.h"
[email protected]e54edc32010-09-28 01:09:1910#include "base/platform_file.h"
[email protected]b9535422012-02-09 01:47:5911#include "content/browser/child_process_security_policy_impl.h"
[email protected]a1d29162011-10-14 17:14:0312#include "content/public/common/url_constants.h"
[email protected]c6681f32012-06-05 14:43:0113#include "content/test/test_content_browser_client.h"
initial.commit09911bf2008-07-26 23:55:2914#include "testing/gtest/include/gtest/gtest.h"
[email protected]707e1c42013-07-09 21:18:5815#include "url/gurl.h"
initial.commit09911bf2008-07-26 23:55:2916
[email protected]46488322012-10-30 03:22:2017namespace content {
[email protected]46fb9442011-12-09 17:57:4718namespace {
19
20const int kRendererID = 42;
21const int kWorkerRendererID = kRendererID + 1;
22
[email protected]f0ecca4522013-01-07 21:50:5623#if defined(FILE_PATH_USES_DRIVE_LETTERS)
24#define TEST_PATH(x) FILE_PATH_LITERAL("c:") FILE_PATH_LITERAL(x)
25#else
26#define TEST_PATH(x) FILE_PATH_LITERAL(x)
27#endif
28
[email protected]46fb9442011-12-09 17:57:4729class ChildProcessSecurityPolicyTestBrowserClient
[email protected]46488322012-10-30 03:22:2030 : public TestContentBrowserClient {
[email protected]46fb9442011-12-09 17:57:4731 public:
32 ChildProcessSecurityPolicyTestBrowserClient() {}
33
[email protected]c3e35892013-02-12 02:08:0134 virtual bool IsHandledURL(const GURL& url) OVERRIDE {
[email protected]46fb9442011-12-09 17:57:4735 return schemes_.find(url.scheme()) != schemes_.end();
[email protected]e3539402011-07-19 09:31:0836 }
37
[email protected]46fb9442011-12-09 17:57:4738 void ClearSchemes() {
39 schemes_.clear();
initial.commit09911bf2008-07-26 23:55:2940 }
[email protected]46fb9442011-12-09 17:57:4741
42 void AddScheme(const std::string& scheme) {
43 schemes_.insert(scheme);
44 }
45
46 private:
47 std::set<std::string> schemes_;
initial.commit09911bf2008-07-26 23:55:2948};
49
[email protected]46fb9442011-12-09 17:57:4750} // namespace
51
52class ChildProcessSecurityPolicyTest : public testing::Test {
53 public:
54 ChildProcessSecurityPolicyTest() : old_browser_client_(NULL) {
55 }
56
57 virtual void SetUp() {
[email protected]eabbfb12013-04-05 23:28:3558 old_browser_client_ = SetBrowserClientForTesting(&test_browser_client_);
[email protected]46fb9442011-12-09 17:57:4759
60 // Claim to always handle chrome:// URLs because the CPSP's notion of
61 // allowing WebUI bindings is hard-wired to this particular scheme.
[email protected]e0f35c92013-05-08 16:04:3462 test_browser_client_.AddScheme(chrome::kChromeUIScheme);
63
64 // Claim to always handle file:// URLs like the browser would.
65 // net::URLRequest::IsHandledURL() no longer claims support for default
66 // protocols as this is the responsibility of the browser (which is
67 // responsible for adding the appropriate ProtocolHandler).
68 test_browser_client_.AddScheme(chrome::kFileScheme);
[email protected]46fb9442011-12-09 17:57:4769 }
70
71 virtual void TearDown() {
72 test_browser_client_.ClearSchemes();
[email protected]eabbfb12013-04-05 23:28:3573 SetBrowserClientForTesting(old_browser_client_);
[email protected]46fb9442011-12-09 17:57:4774 }
75
76 protected:
77 void RegisterTestScheme(const std::string& scheme) {
78 test_browser_client_.AddScheme(scheme);
79 }
80
81 private:
82 ChildProcessSecurityPolicyTestBrowserClient test_browser_client_;
[email protected]46488322012-10-30 03:22:2083 ContentBrowserClient* old_browser_client_;
[email protected]46fb9442011-12-09 17:57:4784};
initial.commit09911bf2008-07-26 23:55:2985
[email protected]f58ddcf2009-05-18 22:22:0686TEST_F(ChildProcessSecurityPolicyTest, IsWebSafeSchemeTest) {
[email protected]b9535422012-02-09 01:47:5987 ChildProcessSecurityPolicyImpl* p =
88 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:2989
[email protected]e0d481582009-09-15 21:06:2590 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kHttpScheme));
91 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kHttpsScheme));
92 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kFtpScheme));
93 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kDataScheme));
initial.commit09911bf2008-07-26 23:55:2994 EXPECT_TRUE(p->IsWebSafeScheme("feed"));
[email protected]039c7b0b22011-03-04 23:15:4295 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kBlobScheme));
96 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kFileSystemScheme));
initial.commit09911bf2008-07-26 23:55:2997
98 EXPECT_FALSE(p->IsWebSafeScheme("registered-web-safe-scheme"));
99 p->RegisterWebSafeScheme("registered-web-safe-scheme");
100 EXPECT_TRUE(p->IsWebSafeScheme("registered-web-safe-scheme"));
[email protected]89f550b2011-06-08 18:34:03101
102 EXPECT_FALSE(p->IsWebSafeScheme(chrome::kChromeUIScheme));
initial.commit09911bf2008-07-26 23:55:29103}
104
[email protected]f58ddcf2009-05-18 22:22:06105TEST_F(ChildProcessSecurityPolicyTest, IsPseudoSchemeTest) {
[email protected]b9535422012-02-09 01:47:59106 ChildProcessSecurityPolicyImpl* p =
107 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29108
[email protected]e0d481582009-09-15 21:06:25109 EXPECT_TRUE(p->IsPseudoScheme(chrome::kAboutScheme));
110 EXPECT_TRUE(p->IsPseudoScheme(chrome::kJavaScriptScheme));
[email protected]dbdda5402013-05-30 22:13:48111 EXPECT_TRUE(p->IsPseudoScheme(kViewSourceScheme));
initial.commit09911bf2008-07-26 23:55:29112
[email protected]419a0572011-04-18 22:21:46113 EXPECT_FALSE(p->IsPseudoScheme("registered-pseudo-scheme"));
114 p->RegisterPseudoScheme("registered-pseudo-scheme");
115 EXPECT_TRUE(p->IsPseudoScheme("registered-pseudo-scheme"));
[email protected]89f550b2011-06-08 18:34:03116
117 EXPECT_FALSE(p->IsPseudoScheme(chrome::kChromeUIScheme));
[email protected]419a0572011-04-18 22:21:46118}
119
[email protected]f58ddcf2009-05-18 22:22:06120TEST_F(ChildProcessSecurityPolicyTest, StandardSchemesTest) {
[email protected]b9535422012-02-09 01:47:59121 ChildProcessSecurityPolicyImpl* p =
122 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29123
124 p->Add(kRendererID);
125
126 // Safe
127 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("http://www.google.com/")));
128 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("https://www.paypal.com/")));
129 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("ftp://ftp.gnu.org/")));
130 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("data:text/html,<b>Hi</b>")));
131 EXPECT_TRUE(p->CanRequestURL(kRendererID,
132 GURL("view-source:http://www.google.com/")));
[email protected]039c7b0b22011-03-04 23:15:42133 EXPECT_TRUE(p->CanRequestURL(
134 kRendererID, GURL("filesystem:http://localhost/temporary/a.gif")));
initial.commit09911bf2008-07-26 23:55:29135
136 // Dangerous
137 EXPECT_FALSE(p->CanRequestURL(kRendererID,
138 GURL("file:///etc/passwd")));
139 EXPECT_FALSE(p->CanRequestURL(kRendererID,
[email protected]60e448982009-05-06 04:21:16140 GURL("chrome://foo/bar")));
initial.commit09911bf2008-07-26 23:55:29141
142 p->Remove(kRendererID);
143}
144
[email protected]f58ddcf2009-05-18 22:22:06145TEST_F(ChildProcessSecurityPolicyTest, AboutTest) {
[email protected]b9535422012-02-09 01:47:59146 ChildProcessSecurityPolicyImpl* p =
147 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29148
149 p->Add(kRendererID);
150
151 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("about:blank")));
152 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("about:BlAnK")));
153 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("aBouT:BlAnK")));
154 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("aBouT:blank")));
155
[email protected]ed3456f2009-02-26 20:24:48156 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:memory")));
157 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:crash")));
158 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:cache")));
159 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:hang")));
initial.commit09911bf2008-07-26 23:55:29160
161 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("aBoUt:memory")));
162 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:CrASh")));
163 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("abOuT:cAChe")));
164
[email protected]8bf1048012012-02-08 01:22:18165 // Requests for about: pages should be denied.
166 p->GrantRequestURL(kRendererID, GURL("about:crash"));
167 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:crash")));
initial.commit09911bf2008-07-26 23:55:29168
[email protected]89f550b2011-06-08 18:34:03169 // These requests for chrome:// pages should be granted.
[email protected]e068c2d2012-10-23 16:45:18170 GURL chrome_url("chrome://foo");
171 p->GrantRequestURL(kRendererID, chrome_url);
172 EXPECT_TRUE(p->CanRequestURL(kRendererID, chrome_url));
[email protected]89f550b2011-06-08 18:34:03173
initial.commit09911bf2008-07-26 23:55:29174 p->Remove(kRendererID);
175}
176
[email protected]f58ddcf2009-05-18 22:22:06177TEST_F(ChildProcessSecurityPolicyTest, JavaScriptTest) {
[email protected]b9535422012-02-09 01:47:59178 ChildProcessSecurityPolicyImpl* p =
179 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29180
181 p->Add(kRendererID);
182
183 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("javascript:alert('xss')")));
184 p->GrantRequestURL(kRendererID, GURL("javascript:alert('xss')"));
185 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("javascript:alert('xss')")));
186
187 p->Remove(kRendererID);
188}
189
[email protected]f58ddcf2009-05-18 22:22:06190TEST_F(ChildProcessSecurityPolicyTest, RegisterWebSafeSchemeTest) {
[email protected]b9535422012-02-09 01:47:59191 ChildProcessSecurityPolicyImpl* p =
192 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29193
194 p->Add(kRendererID);
195
196 // Currently, "asdf" is destined for ShellExecute, so it is allowed.
197 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("asdf:rockers")));
198
[email protected]46fb9442011-12-09 17:57:47199 // Once we register "asdf", we default to deny.
200 RegisterTestScheme("asdf");
initial.commit09911bf2008-07-26 23:55:29201 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("asdf:rockers")));
202
203 // We can allow new schemes by adding them to the whitelist.
204 p->RegisterWebSafeScheme("asdf");
205 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("asdf:rockers")));
206
207 // Cleanup.
initial.commit09911bf2008-07-26 23:55:29208 p->Remove(kRendererID);
209}
210
[email protected]f58ddcf2009-05-18 22:22:06211TEST_F(ChildProcessSecurityPolicyTest, CanServiceCommandsTest) {
[email protected]b9535422012-02-09 01:47:59212 ChildProcessSecurityPolicyImpl* p =
213 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29214
215 p->Add(kRendererID);
216
217 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd")));
218 p->GrantRequestURL(kRendererID, GURL("file:///etc/passwd"));
219 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd")));
220
initial.commit09911bf2008-07-26 23:55:29221 // We should forget our state if we repeat a renderer id.
222 p->Remove(kRendererID);
223 p->Add(kRendererID);
224 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd")));
225 p->Remove(kRendererID);
226}
227
[email protected]f58ddcf2009-05-18 22:22:06228TEST_F(ChildProcessSecurityPolicyTest, ViewSource) {
[email protected]b9535422012-02-09 01:47:59229 ChildProcessSecurityPolicyImpl* p =
230 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29231
232 p->Add(kRendererID);
233
234 // View source is determined by the embedded scheme.
235 EXPECT_TRUE(p->CanRequestURL(kRendererID,
236 GURL("view-source:http://www.google.com/")));
237 EXPECT_FALSE(p->CanRequestURL(kRendererID,
238 GURL("view-source:file:///etc/passwd")));
239 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd")));
[email protected]690d0a9172010-01-06 00:19:36240 EXPECT_FALSE(p->CanRequestURL(
241 kRendererID, GURL("view-source:view-source:http://www.google.com/")));
initial.commit09911bf2008-07-26 23:55:29242
243 p->GrantRequestURL(kRendererID, GURL("view-source:file:///etc/passwd"));
244 // View source needs to be able to request the embedded scheme.
245 EXPECT_TRUE(p->CanRequestURL(kRendererID,
246 GURL("view-source:file:///etc/passwd")));
247 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd")));
248
249 p->Remove(kRendererID);
250}
251
[email protected]dc67e1c32012-06-08 00:10:40252TEST_F(ChildProcessSecurityPolicyTest, SpecificFile) {
253 ChildProcessSecurityPolicyImpl* p =
254 ChildProcessSecurityPolicyImpl::GetInstance();
255
256 p->Add(kRendererID);
257
258 GURL icon_url("file:///tmp/foo.png");
259 GURL sensitive_url("file:///etc/passwd");
260 EXPECT_FALSE(p->CanRequestURL(kRendererID, icon_url));
261 EXPECT_FALSE(p->CanRequestURL(kRendererID, sensitive_url));
262
263 p->GrantRequestSpecificFileURL(kRendererID, icon_url);
264 EXPECT_TRUE(p->CanRequestURL(kRendererID, icon_url));
265 EXPECT_FALSE(p->CanRequestURL(kRendererID, sensitive_url));
266
267 p->GrantRequestURL(kRendererID, icon_url);
268 EXPECT_TRUE(p->CanRequestURL(kRendererID, icon_url));
269 EXPECT_TRUE(p->CanRequestURL(kRendererID, sensitive_url));
270
271 p->Remove(kRendererID);
272}
273
[email protected]e54edc32010-09-28 01:09:19274TEST_F(ChildProcessSecurityPolicyTest, CanReadFiles) {
[email protected]b9535422012-02-09 01:47:59275 ChildProcessSecurityPolicyImpl* p =
276 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29277
278 p->Add(kRendererID);
279
[email protected]2dec8ec2013-02-07 19:20:34280 EXPECT_FALSE(p->CanReadFile(kRendererID,
281 base::FilePath(TEST_PATH("/etc/passwd"))));
282 p->GrantReadFile(kRendererID, base::FilePath(TEST_PATH("/etc/passwd")));
283 EXPECT_TRUE(p->CanReadFile(kRendererID,
284 base::FilePath(TEST_PATH("/etc/passwd"))));
285 EXPECT_FALSE(p->CanReadFile(kRendererID,
286 base::FilePath(TEST_PATH("/etc/shadow"))));
initial.commit09911bf2008-07-26 23:55:29287
288 p->Remove(kRendererID);
289 p->Add(kRendererID);
290
[email protected]2dec8ec2013-02-07 19:20:34291 EXPECT_FALSE(p->CanReadFile(kRendererID,
292 base::FilePath(TEST_PATH("/etc/passwd"))));
293 EXPECT_FALSE(p->CanReadFile(kRendererID,
294 base::FilePath(TEST_PATH("/etc/shadow"))));
initial.commit09911bf2008-07-26 23:55:29295
296 p->Remove(kRendererID);
297}
298
[email protected]600ea402011-04-12 00:01:51299TEST_F(ChildProcessSecurityPolicyTest, CanReadDirectories) {
[email protected]b9535422012-02-09 01:47:59300 ChildProcessSecurityPolicyImpl* p =
301 ChildProcessSecurityPolicyImpl::GetInstance();
[email protected]600ea402011-04-12 00:01:51302
303 p->Add(kRendererID);
304
[email protected]2dec8ec2013-02-07 19:20:34305 EXPECT_FALSE(p->CanReadDirectory(kRendererID,
306 base::FilePath(TEST_PATH("/etc/"))));
307 p->GrantReadDirectory(kRendererID,
308 base::FilePath(TEST_PATH("/etc/")));
309 EXPECT_TRUE(p->CanReadDirectory(kRendererID,
310 base::FilePath(TEST_PATH("/etc/"))));
311 EXPECT_TRUE(p->CanReadFile(kRendererID,
312 base::FilePath(TEST_PATH("/etc/passwd"))));
[email protected]600ea402011-04-12 00:01:51313
314 p->Remove(kRendererID);
315 p->Add(kRendererID);
316
[email protected]2dec8ec2013-02-07 19:20:34317 EXPECT_FALSE(p->CanReadDirectory(kRendererID,
318 base::FilePath(TEST_PATH("/etc/"))));
319 EXPECT_FALSE(p->CanReadFile(kRendererID,
320 base::FilePath(TEST_PATH("/etc/passwd"))));
[email protected]600ea402011-04-12 00:01:51321
322 // Just granting read permission as a file doesn't imply reading as a
323 // directory.
[email protected]2dec8ec2013-02-07 19:20:34324 p->GrantReadFile(kRendererID, base::FilePath(TEST_PATH("/etc/")));
325 EXPECT_TRUE(p->CanReadFile(kRendererID,
326 base::FilePath(TEST_PATH("/etc/passwd"))));
327 EXPECT_FALSE(p->CanReadDirectory(kRendererID,
328 base::FilePath(TEST_PATH("/etc/"))));
[email protected]600ea402011-04-12 00:01:51329
330 p->Remove(kRendererID);
331}
332
[email protected]e54edc32010-09-28 01:09:19333TEST_F(ChildProcessSecurityPolicyTest, FilePermissions) {
[email protected]c42de732013-02-16 06:26:31334 base::FilePath granted_file = base::FilePath(TEST_PATH("/home/joe"));
335 base::FilePath sibling_file = base::FilePath(TEST_PATH("/home/bob"));
336 base::FilePath child_file = base::FilePath(TEST_PATH("/home/joe/file"));
337 base::FilePath parent_file = base::FilePath(TEST_PATH("/home"));
338 base::FilePath parent_slash_file = base::FilePath(TEST_PATH("/home/"));
339 base::FilePath child_traversal1 =
340 base::FilePath(TEST_PATH("/home/joe/././file"));
341 base::FilePath child_traversal2 = base::FilePath(
[email protected]f0ecca4522013-01-07 21:50:56342 TEST_PATH("/home/joe/file/../otherfile"));
[email protected]2dec8ec2013-02-07 19:20:34343 base::FilePath evil_traversal1 =
[email protected]023ad6ab2013-02-17 05:07:23344 base::FilePath(TEST_PATH("/home/joe/../../etc/passwd"));
[email protected]c42de732013-02-16 06:26:31345 base::FilePath evil_traversal2 = base::FilePath(
[email protected]f0ecca4522013-01-07 21:50:56346 TEST_PATH("/home/joe/./.././../etc/passwd"));
[email protected]c42de732013-02-16 06:26:31347 base::FilePath self_traversal =
348 base::FilePath(TEST_PATH("/home/joe/../joe/file"));
349 base::FilePath relative_file = base::FilePath(FILE_PATH_LITERAL("home/joe"));
[email protected]80838412012-11-20 01:53:59350
[email protected]b9535422012-02-09 01:47:59351 ChildProcessSecurityPolicyImpl* p =
352 ChildProcessSecurityPolicyImpl::GetInstance();
[email protected]e54edc32010-09-28 01:09:19353
354 // Grant permissions for a file.
355 p->Add(kRendererID);
[email protected]80838412012-11-20 01:53:59356 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]e54edc32010-09-28 01:09:19357 base::PLATFORM_FILE_OPEN));
358
[email protected]80838412012-11-20 01:53:59359 p->GrantPermissionsForFile(kRendererID, granted_file,
[email protected]e54edc32010-09-28 01:09:19360 base::PLATFORM_FILE_OPEN |
[email protected]b2f2308d2011-05-23 22:00:04361 base::PLATFORM_FILE_OPEN_TRUNCATED |
[email protected]e54edc32010-09-28 01:09:19362 base::PLATFORM_FILE_READ |
[email protected]b2f2308d2011-05-23 22:00:04363 base::PLATFORM_FILE_WRITE);
[email protected]80838412012-11-20 01:53:59364 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]e54edc32010-09-28 01:09:19365 base::PLATFORM_FILE_OPEN |
[email protected]b2f2308d2011-05-23 22:00:04366 base::PLATFORM_FILE_OPEN_TRUNCATED |
[email protected]e54edc32010-09-28 01:09:19367 base::PLATFORM_FILE_READ |
[email protected]b2f2308d2011-05-23 22:00:04368 base::PLATFORM_FILE_WRITE));
[email protected]80838412012-11-20 01:53:59369 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]e54edc32010-09-28 01:09:19370 base::PLATFORM_FILE_OPEN |
371 base::PLATFORM_FILE_READ));
[email protected]80838412012-11-20 01:53:59372 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]e54edc32010-09-28 01:09:19373 base::PLATFORM_FILE_CREATE));
[email protected]f0ecca4522013-01-07 21:50:56374 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file, 0));
[email protected]80838412012-11-20 01:53:59375 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]e54edc32010-09-28 01:09:19376 base::PLATFORM_FILE_CREATE |
[email protected]b2f2308d2011-05-23 22:00:04377 base::PLATFORM_FILE_OPEN_TRUNCATED |
[email protected]e54edc32010-09-28 01:09:19378 base::PLATFORM_FILE_READ |
[email protected]b2f2308d2011-05-23 22:00:04379 base::PLATFORM_FILE_WRITE));
[email protected]80838412012-11-20 01:53:59380 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, sibling_file,
381 base::PLATFORM_FILE_OPEN |
382 base::PLATFORM_FILE_READ));
383 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, parent_file,
384 base::PLATFORM_FILE_OPEN |
385 base::PLATFORM_FILE_READ));
386 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, child_file,
387 base::PLATFORM_FILE_OPEN |
388 base::PLATFORM_FILE_READ));
389 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, child_traversal1,
390 base::PLATFORM_FILE_OPEN |
391 base::PLATFORM_FILE_READ));
392 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, child_traversal2,
393 base::PLATFORM_FILE_OPEN |
394 base::PLATFORM_FILE_READ));
395 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, evil_traversal1,
396 base::PLATFORM_FILE_OPEN |
397 base::PLATFORM_FILE_READ));
398 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, evil_traversal2,
399 base::PLATFORM_FILE_OPEN |
400 base::PLATFORM_FILE_READ));
401 // CPSP doesn't allow this case for the sake of simplicity.
402 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, self_traversal,
403 base::PLATFORM_FILE_OPEN |
404 base::PLATFORM_FILE_READ));
[email protected]e54edc32010-09-28 01:09:19405 p->Remove(kRendererID);
406
407 // Grant permissions for the directory the file is in.
408 p->Add(kRendererID);
[email protected]80838412012-11-20 01:53:59409 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]e54edc32010-09-28 01:09:19410 base::PLATFORM_FILE_OPEN));
[email protected]80838412012-11-20 01:53:59411 p->GrantPermissionsForFile(kRendererID, parent_file,
[email protected]e54edc32010-09-28 01:09:19412 base::PLATFORM_FILE_OPEN |
413 base::PLATFORM_FILE_READ);
[email protected]80838412012-11-20 01:53:59414 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]e54edc32010-09-28 01:09:19415 base::PLATFORM_FILE_OPEN));
[email protected]80838412012-11-20 01:53:59416 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]e54edc32010-09-28 01:09:19417 base::PLATFORM_FILE_READ |
418 base::PLATFORM_FILE_WRITE));
419 p->Remove(kRendererID);
420
421 // Grant permissions for the directory the file is in (with trailing '/').
422 p->Add(kRendererID);
[email protected]80838412012-11-20 01:53:59423 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]e54edc32010-09-28 01:09:19424 base::PLATFORM_FILE_OPEN));
[email protected]80838412012-11-20 01:53:59425 p->GrantPermissionsForFile(kRendererID, parent_slash_file,
[email protected]e54edc32010-09-28 01:09:19426 base::PLATFORM_FILE_OPEN |
427 base::PLATFORM_FILE_READ);
[email protected]80838412012-11-20 01:53:59428 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]e54edc32010-09-28 01:09:19429 base::PLATFORM_FILE_OPEN));
[email protected]80838412012-11-20 01:53:59430 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]e54edc32010-09-28 01:09:19431 base::PLATFORM_FILE_READ |
432 base::PLATFORM_FILE_WRITE));
433
434 // Grant permissions for the file (should overwrite the permissions granted
435 // for the directory).
[email protected]80838412012-11-20 01:53:59436 p->GrantPermissionsForFile(kRendererID, granted_file,
437 base::PLATFORM_FILE_TEMPORARY);
438 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]e54edc32010-09-28 01:09:19439 base::PLATFORM_FILE_OPEN));
[email protected]80838412012-11-20 01:53:59440 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]e54edc32010-09-28 01:09:19441 base::PLATFORM_FILE_TEMPORARY));
[email protected]77930fe2010-10-01 22:45:34442
443 // Revoke all permissions for the file (it should inherit its permissions
444 // from the directory again).
[email protected]80838412012-11-20 01:53:59445 p->RevokeAllPermissionsForFile(kRendererID, granted_file);
446 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]77930fe2010-10-01 22:45:34447 base::PLATFORM_FILE_OPEN |
448 base::PLATFORM_FILE_READ));
[email protected]80838412012-11-20 01:53:59449 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]77930fe2010-10-01 22:45:34450 base::PLATFORM_FILE_TEMPORARY));
[email protected]e54edc32010-09-28 01:09:19451 p->Remove(kRendererID);
[email protected]cee64fd32011-05-02 18:59:07452
453 // Grant file permissions for the file to main thread renderer process,
454 // make sure its worker thread renderer process inherits those.
455 p->Add(kRendererID);
[email protected]80838412012-11-20 01:53:59456 p->GrantPermissionsForFile(kRendererID, granted_file,
457 base::PLATFORM_FILE_OPEN |
458 base::PLATFORM_FILE_READ);
459 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]cee64fd32011-05-02 18:59:07460 base::PLATFORM_FILE_OPEN |
461 base::PLATFORM_FILE_READ));
[email protected]80838412012-11-20 01:53:59462 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]cee64fd32011-05-02 18:59:07463 base::PLATFORM_FILE_WRITE));
464 p->AddWorker(kWorkerRendererID, kRendererID);
[email protected]80838412012-11-20 01:53:59465 EXPECT_TRUE(p->HasPermissionsForFile(kWorkerRendererID, granted_file,
[email protected]cee64fd32011-05-02 18:59:07466 base::PLATFORM_FILE_OPEN |
467 base::PLATFORM_FILE_READ));
[email protected]80838412012-11-20 01:53:59468 EXPECT_FALSE(p->HasPermissionsForFile(kWorkerRendererID, granted_file,
[email protected]cee64fd32011-05-02 18:59:07469 base::PLATFORM_FILE_WRITE));
470 p->Remove(kRendererID);
[email protected]80838412012-11-20 01:53:59471 EXPECT_FALSE(p->HasPermissionsForFile(kWorkerRendererID, granted_file,
[email protected]cee64fd32011-05-02 18:59:07472 base::PLATFORM_FILE_OPEN |
473 base::PLATFORM_FILE_READ));
474 p->Remove(kWorkerRendererID);
[email protected]f0ecca4522013-01-07 21:50:56475
476 p->Add(kRendererID);
477 p->GrantPermissionsForFile(kRendererID, relative_file,
478 base::PLATFORM_FILE_OPEN);
479 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, relative_file,
480 base::PLATFORM_FILE_OPEN));
481 p->Remove(kRendererID);
[email protected]e54edc32010-09-28 01:09:19482}
483
[email protected]c50008512011-02-03 01:17:27484TEST_F(ChildProcessSecurityPolicyTest, CanServiceWebUIBindings) {
[email protected]b9535422012-02-09 01:47:59485 ChildProcessSecurityPolicyImpl* p =
486 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29487
[email protected]60e448982009-05-06 04:21:16488 GURL url("chrome://thumb/http://www.google.com/");
initial.commit09911bf2008-07-26 23:55:29489
490 p->Add(kRendererID);
491
[email protected]c50008512011-02-03 01:17:27492 EXPECT_FALSE(p->HasWebUIBindings(kRendererID));
initial.commit09911bf2008-07-26 23:55:29493 EXPECT_FALSE(p->CanRequestURL(kRendererID, url));
[email protected]c50008512011-02-03 01:17:27494 p->GrantWebUIBindings(kRendererID);
495 EXPECT_TRUE(p->HasWebUIBindings(kRendererID));
initial.commit09911bf2008-07-26 23:55:29496 EXPECT_TRUE(p->CanRequestURL(kRendererID, url));
497
498 p->Remove(kRendererID);
499}
500
[email protected]f58ddcf2009-05-18 22:22:06501TEST_F(ChildProcessSecurityPolicyTest, RemoveRace) {
[email protected]b9535422012-02-09 01:47:59502 ChildProcessSecurityPolicyImpl* p =
503 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29504
505 GURL url("file:///etc/passwd");
[email protected]2dec8ec2013-02-07 19:20:34506 base::FilePath file(TEST_PATH("/etc/passwd"));
initial.commit09911bf2008-07-26 23:55:29507
508 p->Add(kRendererID);
509
510 p->GrantRequestURL(kRendererID, url);
[email protected]e54edc32010-09-28 01:09:19511 p->GrantReadFile(kRendererID, file);
[email protected]c50008512011-02-03 01:17:27512 p->GrantWebUIBindings(kRendererID);
initial.commit09911bf2008-07-26 23:55:29513
514 EXPECT_TRUE(p->CanRequestURL(kRendererID, url));
[email protected]e54edc32010-09-28 01:09:19515 EXPECT_TRUE(p->CanReadFile(kRendererID, file));
[email protected]c50008512011-02-03 01:17:27516 EXPECT_TRUE(p->HasWebUIBindings(kRendererID));
initial.commit09911bf2008-07-26 23:55:29517
518 p->Remove(kRendererID);
519
520 // Renderers are added and removed on the UI thread, but the policy can be
[email protected]580522632009-08-17 21:55:55521 // queried on the IO thread. The ChildProcessSecurityPolicy needs to be
522 // prepared to answer policy questions about renderers who no longer exist.
initial.commit09911bf2008-07-26 23:55:29523
524 // In this case, we default to secure behavior.
525 EXPECT_FALSE(p->CanRequestURL(kRendererID, url));
[email protected]e54edc32010-09-28 01:09:19526 EXPECT_FALSE(p->CanReadFile(kRendererID, file));
[email protected]c50008512011-02-03 01:17:27527 EXPECT_FALSE(p->HasWebUIBindings(kRendererID));
initial.commit09911bf2008-07-26 23:55:29528}
[email protected]46488322012-10-30 03:22:20529
530} // namespace content