blob: 24aa45bc21f050f00bea4642e7f5fe50674589a6 [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
[email protected]57999812013-02-24 05:40:528#include "base/files/file_path.h"
[email protected]b9535422012-02-09 01:47:599#include "content/browser/child_process_security_policy_impl.h"
[email protected]a1d29162011-10-14 17:14:0310#include "content/public/common/url_constants.h"
[email protected]c6681f32012-06-05 14:43:0111#include "content/test/test_content_browser_client.h"
pilgrime92c5fcd2014-09-10 23:31:2312#include "storage/browser/fileapi/file_permission_policy.h"
13#include "storage/browser/fileapi/file_system_url.h"
14#include "storage/browser/fileapi/isolated_context.h"
pilgrim16330552014-09-10 01:32:2215#include "storage/common/fileapi/file_system_types.h"
initial.commit09911bf2008-07-26 23:55:2916#include "testing/gtest/include/gtest/gtest.h"
[email protected]707e1c42013-07-09 21:18:5817#include "url/gurl.h"
paulmeyer1eefa26e2015-10-01 02:11:1318#include "url/origin.h"
initial.commit09911bf2008-07-26 23:55:2919
[email protected]46488322012-10-30 03:22:2020namespace content {
[email protected]46fb9442011-12-09 17:57:4721namespace {
22
23const int kRendererID = 42;
24const int kWorkerRendererID = kRendererID + 1;
25
[email protected]f0ecca4522013-01-07 21:50:5626#if defined(FILE_PATH_USES_DRIVE_LETTERS)
27#define TEST_PATH(x) FILE_PATH_LITERAL("c:") FILE_PATH_LITERAL(x)
28#else
29#define TEST_PATH(x) FILE_PATH_LITERAL(x)
30#endif
31
[email protected]46fb9442011-12-09 17:57:4732class ChildProcessSecurityPolicyTestBrowserClient
[email protected]46488322012-10-30 03:22:2033 : public TestContentBrowserClient {
[email protected]46fb9442011-12-09 17:57:4734 public:
35 ChildProcessSecurityPolicyTestBrowserClient() {}
36
dchengc2282aa2014-10-21 12:07:5837 bool IsHandledURL(const GURL& url) override {
[email protected]46fb9442011-12-09 17:57:4738 return schemes_.find(url.scheme()) != schemes_.end();
[email protected]e3539402011-07-19 09:31:0839 }
40
[email protected]46fb9442011-12-09 17:57:4741 void ClearSchemes() {
42 schemes_.clear();
initial.commit09911bf2008-07-26 23:55:2943 }
[email protected]46fb9442011-12-09 17:57:4744
45 void AddScheme(const std::string& scheme) {
46 schemes_.insert(scheme);
47 }
48
49 private:
50 std::set<std::string> schemes_;
initial.commit09911bf2008-07-26 23:55:2951};
52
[email protected]46fb9442011-12-09 17:57:4753} // namespace
54
55class ChildProcessSecurityPolicyTest : public testing::Test {
56 public:
57 ChildProcessSecurityPolicyTest() : old_browser_client_(NULL) {
58 }
59
dchengfa85b152014-10-28 01:13:4260 void SetUp() override {
[email protected]eabbfb12013-04-05 23:28:3561 old_browser_client_ = SetBrowserClientForTesting(&test_browser_client_);
[email protected]46fb9442011-12-09 17:57:4762
63 // Claim to always handle chrome:// URLs because the CPSP's notion of
64 // allowing WebUI bindings is hard-wired to this particular scheme.
[email protected]2d9748b22014-02-11 00:17:2965 test_browser_client_.AddScheme(kChromeUIScheme);
[email protected]e0f35c92013-05-08 16:04:3466
67 // Claim to always handle file:// URLs like the browser would.
68 // net::URLRequest::IsHandledURL() no longer claims support for default
69 // protocols as this is the responsibility of the browser (which is
70 // responsible for adding the appropriate ProtocolHandler).
[email protected]cca6f392014-05-28 21:32:2671 test_browser_client_.AddScheme(url::kFileScheme);
[email protected]46fb9442011-12-09 17:57:4772 }
73
dchengfa85b152014-10-28 01:13:4274 void TearDown() override {
[email protected]46fb9442011-12-09 17:57:4775 test_browser_client_.ClearSchemes();
[email protected]eabbfb12013-04-05 23:28:3576 SetBrowserClientForTesting(old_browser_client_);
[email protected]46fb9442011-12-09 17:57:4777 }
78
79 protected:
80 void RegisterTestScheme(const std::string& scheme) {
81 test_browser_client_.AddScheme(scheme);
82 }
83
[email protected]bfcf1e92013-07-11 04:37:2584 void GrantPermissionsForFile(ChildProcessSecurityPolicyImpl* p,
85 int child_id,
86 const base::FilePath& file,
87 int permissions) {
88 p->GrantPermissionsForFile(child_id, file, permissions);
89 }
90
[email protected]5a65fde32013-10-22 05:15:3491 void CheckHasNoFileSystemPermission(ChildProcessSecurityPolicyImpl* p,
92 const std::string& child_id) {
93 EXPECT_FALSE(p->CanReadFileSystem(kRendererID, child_id));
94 EXPECT_FALSE(p->CanReadWriteFileSystem(kRendererID, child_id));
95 EXPECT_FALSE(p->CanCopyIntoFileSystem(kRendererID, child_id));
96 EXPECT_FALSE(p->CanDeleteFromFileSystem(kRendererID, child_id));
97 }
98
99 void CheckHasNoFileSystemFilePermission(ChildProcessSecurityPolicyImpl* p,
100 const base::FilePath& file,
[email protected]cd501a72014-08-22 19:58:31101 const storage::FileSystemURL& url) {
[email protected]5a65fde32013-10-22 05:15:34102 EXPECT_FALSE(p->CanReadFile(kRendererID, file));
103 EXPECT_FALSE(p->CanCreateReadWriteFile(kRendererID, file));
104 EXPECT_FALSE(p->CanReadFileSystemFile(kRendererID, url));
105 EXPECT_FALSE(p->CanWriteFileSystemFile(kRendererID, url));
106 EXPECT_FALSE(p->CanCreateFileSystemFile(kRendererID, url));
107 EXPECT_FALSE(p->CanCreateReadWriteFileSystemFile(kRendererID, url));
108 EXPECT_FALSE(p->CanCopyIntoFileSystemFile(kRendererID, url));
109 EXPECT_FALSE(p->CanDeleteFileSystemFile(kRendererID, url));
110 }
111
[email protected]46fb9442011-12-09 17:57:47112 private:
113 ChildProcessSecurityPolicyTestBrowserClient test_browser_client_;
[email protected]46488322012-10-30 03:22:20114 ContentBrowserClient* old_browser_client_;
[email protected]46fb9442011-12-09 17:57:47115};
initial.commit09911bf2008-07-26 23:55:29116
[email protected]9f104312013-07-23 23:18:19117
[email protected]f58ddcf2009-05-18 22:22:06118TEST_F(ChildProcessSecurityPolicyTest, IsWebSafeSchemeTest) {
[email protected]b9535422012-02-09 01:47:59119 ChildProcessSecurityPolicyImpl* p =
120 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29121
[email protected]e8ca69c2014-05-07 15:31:19122 EXPECT_TRUE(p->IsWebSafeScheme(url::kHttpScheme));
123 EXPECT_TRUE(p->IsWebSafeScheme(url::kHttpsScheme));
[email protected]cca6f392014-05-28 21:32:26124 EXPECT_TRUE(p->IsWebSafeScheme(url::kFtpScheme));
125 EXPECT_TRUE(p->IsWebSafeScheme(url::kDataScheme));
initial.commit09911bf2008-07-26 23:55:29126 EXPECT_TRUE(p->IsWebSafeScheme("feed"));
[email protected]cca6f392014-05-28 21:32:26127 EXPECT_TRUE(p->IsWebSafeScheme(url::kBlobScheme));
128 EXPECT_TRUE(p->IsWebSafeScheme(url::kFileSystemScheme));
initial.commit09911bf2008-07-26 23:55:29129
130 EXPECT_FALSE(p->IsWebSafeScheme("registered-web-safe-scheme"));
131 p->RegisterWebSafeScheme("registered-web-safe-scheme");
132 EXPECT_TRUE(p->IsWebSafeScheme("registered-web-safe-scheme"));
[email protected]89f550b2011-06-08 18:34:03133
[email protected]2d9748b22014-02-11 00:17:29134 EXPECT_FALSE(p->IsWebSafeScheme(kChromeUIScheme));
initial.commit09911bf2008-07-26 23:55:29135}
136
[email protected]f58ddcf2009-05-18 22:22:06137TEST_F(ChildProcessSecurityPolicyTest, IsPseudoSchemeTest) {
[email protected]b9535422012-02-09 01:47:59138 ChildProcessSecurityPolicyImpl* p =
139 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29140
[email protected]8e09c7af2014-06-10 11:46:17141 EXPECT_TRUE(p->IsPseudoScheme(url::kAboutScheme));
[email protected]cca6f392014-05-28 21:32:26142 EXPECT_TRUE(p->IsPseudoScheme(url::kJavaScriptScheme));
[email protected]dbdda5402013-05-30 22:13:48143 EXPECT_TRUE(p->IsPseudoScheme(kViewSourceScheme));
initial.commit09911bf2008-07-26 23:55:29144
[email protected]419a0572011-04-18 22:21:46145 EXPECT_FALSE(p->IsPseudoScheme("registered-pseudo-scheme"));
146 p->RegisterPseudoScheme("registered-pseudo-scheme");
147 EXPECT_TRUE(p->IsPseudoScheme("registered-pseudo-scheme"));
[email protected]89f550b2011-06-08 18:34:03148
[email protected]2d9748b22014-02-11 00:17:29149 EXPECT_FALSE(p->IsPseudoScheme(kChromeUIScheme));
[email protected]419a0572011-04-18 22:21:46150}
151
[email protected]f58ddcf2009-05-18 22:22:06152TEST_F(ChildProcessSecurityPolicyTest, StandardSchemesTest) {
[email protected]b9535422012-02-09 01:47:59153 ChildProcessSecurityPolicyImpl* p =
154 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29155
156 p->Add(kRendererID);
157
creis3710b2382015-08-18 00:12:15158 // Safe to request or commit.
initial.commit09911bf2008-07-26 23:55:29159 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("http://www.google.com/")));
160 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("https://www.paypal.com/")));
161 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("ftp://ftp.gnu.org/")));
162 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("data:text/html,<b>Hi</b>")));
[email protected]039c7b0b22011-03-04 23:15:42163 EXPECT_TRUE(p->CanRequestURL(
164 kRendererID, GURL("filesystem:http://localhost/temporary/a.gif")));
creis3710b2382015-08-18 00:12:15165 EXPECT_TRUE(p->CanCommitURL(kRendererID, GURL("http://www.google.com/")));
166 EXPECT_TRUE(p->CanCommitURL(kRendererID, GURL("https://www.paypal.com/")));
167 EXPECT_TRUE(p->CanCommitURL(kRendererID, GURL("ftp://ftp.gnu.org/")));
168 EXPECT_TRUE(p->CanCommitURL(kRendererID, GURL("data:text/html,<b>Hi</b>")));
169 EXPECT_TRUE(p->CanCommitURL(
170 kRendererID, GURL("filesystem:http://localhost/temporary/a.gif")));
initial.commit09911bf2008-07-26 23:55:29171
creis3710b2382015-08-18 00:12:15172 // Dangerous to request or commit.
initial.commit09911bf2008-07-26 23:55:29173 EXPECT_FALSE(p->CanRequestURL(kRendererID,
174 GURL("file:///etc/passwd")));
175 EXPECT_FALSE(p->CanRequestURL(kRendererID,
[email protected]60e448982009-05-06 04:21:16176 GURL("chrome://foo/bar")));
meacerce6b66032016-06-02 20:56:05177 EXPECT_FALSE(p->CanRequestURL(kRendererID,
178 GURL("view-source:http://www.google.com/")));
creis3710b2382015-08-18 00:12:15179 EXPECT_FALSE(p->CanCommitURL(kRendererID,
180 GURL("file:///etc/passwd")));
181 EXPECT_FALSE(p->CanCommitURL(kRendererID,
182 GURL("chrome://foo/bar")));
meacerce6b66032016-06-02 20:56:05183 EXPECT_FALSE(
184 p->CanCommitURL(kRendererID, GURL("view-source:http://www.google.com/")));
initial.commit09911bf2008-07-26 23:55:29185
186 p->Remove(kRendererID);
187}
188
[email protected]f58ddcf2009-05-18 22:22:06189TEST_F(ChildProcessSecurityPolicyTest, AboutTest) {
[email protected]b9535422012-02-09 01:47:59190 ChildProcessSecurityPolicyImpl* p =
191 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29192
193 p->Add(kRendererID);
194
195 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("about:blank")));
196 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("about:BlAnK")));
197 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("aBouT:BlAnK")));
198 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("aBouT:blank")));
creis3710b2382015-08-18 00:12:15199 EXPECT_TRUE(p->CanCommitURL(kRendererID, GURL("about:blank")));
200 EXPECT_TRUE(p->CanCommitURL(kRendererID, GURL("about:BlAnK")));
201 EXPECT_TRUE(p->CanCommitURL(kRendererID, GURL("aBouT:BlAnK")));
202 EXPECT_TRUE(p->CanCommitURL(kRendererID, GURL("aBouT:blank")));
initial.commit09911bf2008-07-26 23:55:29203
[email protected]ed3456f2009-02-26 20:24:48204 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:crash")));
205 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:cache")));
206 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:hang")));
asvitkine2c4b4d1a2016-03-19 14:18:07207 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:version")));
creis3710b2382015-08-18 00:12:15208 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("about:crash")));
209 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("about:cache")));
210 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("about:hang")));
asvitkine2c4b4d1a2016-03-19 14:18:07211 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("about:version")));
initial.commit09911bf2008-07-26 23:55:29212
asvitkine2c4b4d1a2016-03-19 14:18:07213 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("aBoUt:version")));
initial.commit09911bf2008-07-26 23:55:29214 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:CrASh")));
215 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("abOuT:cAChe")));
asvitkine2c4b4d1a2016-03-19 14:18:07216 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("aBoUt:version")));
creis3710b2382015-08-18 00:12:15217 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("about:CrASh")));
218 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("abOuT:cAChe")));
asvitkine2c4b4d1a2016-03-19 14:18:07219 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("aBoUt:version")));
initial.commit09911bf2008-07-26 23:55:29220
[email protected]8bf1048012012-02-08 01:22:18221 // Requests for about: pages should be denied.
222 p->GrantRequestURL(kRendererID, GURL("about:crash"));
223 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:crash")));
creis3710b2382015-08-18 00:12:15224 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("about:crash")));
initial.commit09911bf2008-07-26 23:55:29225
[email protected]89f550b2011-06-08 18:34:03226 // These requests for chrome:// pages should be granted.
[email protected]e068c2d2012-10-23 16:45:18227 GURL chrome_url("chrome://foo");
228 p->GrantRequestURL(kRendererID, chrome_url);
229 EXPECT_TRUE(p->CanRequestURL(kRendererID, chrome_url));
creis3710b2382015-08-18 00:12:15230 EXPECT_TRUE(p->CanCommitURL(kRendererID, chrome_url));
[email protected]89f550b2011-06-08 18:34:03231
initial.commit09911bf2008-07-26 23:55:29232 p->Remove(kRendererID);
233}
234
[email protected]f58ddcf2009-05-18 22:22:06235TEST_F(ChildProcessSecurityPolicyTest, JavaScriptTest) {
[email protected]b9535422012-02-09 01:47:59236 ChildProcessSecurityPolicyImpl* p =
237 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29238
239 p->Add(kRendererID);
240
241 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("javascript:alert('xss')")));
creis3710b2382015-08-18 00:12:15242 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("javascript:alert('xss')")));
initial.commit09911bf2008-07-26 23:55:29243 p->GrantRequestURL(kRendererID, GURL("javascript:alert('xss')"));
244 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("javascript:alert('xss')")));
creis3710b2382015-08-18 00:12:15245 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("javascript:alert('xss')")));
initial.commit09911bf2008-07-26 23:55:29246
247 p->Remove(kRendererID);
248}
249
[email protected]f58ddcf2009-05-18 22:22:06250TEST_F(ChildProcessSecurityPolicyTest, RegisterWebSafeSchemeTest) {
[email protected]b9535422012-02-09 01:47:59251 ChildProcessSecurityPolicyImpl* p =
252 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29253
254 p->Add(kRendererID);
255
creis3710b2382015-08-18 00:12:15256 // Currently, "asdf" is destined for ShellExecute, so it is allowed to be
257 // requested but not committed.
initial.commit09911bf2008-07-26 23:55:29258 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("asdf:rockers")));
creis3710b2382015-08-18 00:12:15259 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("asdf:rockers")));
initial.commit09911bf2008-07-26 23:55:29260
[email protected]46fb9442011-12-09 17:57:47261 // Once we register "asdf", we default to deny.
262 RegisterTestScheme("asdf");
initial.commit09911bf2008-07-26 23:55:29263 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("asdf:rockers")));
creis3710b2382015-08-18 00:12:15264 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("asdf:rockers")));
initial.commit09911bf2008-07-26 23:55:29265
266 // We can allow new schemes by adding them to the whitelist.
267 p->RegisterWebSafeScheme("asdf");
268 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("asdf:rockers")));
creis3710b2382015-08-18 00:12:15269 EXPECT_TRUE(p->CanCommitURL(kRendererID, GURL("asdf:rockers")));
initial.commit09911bf2008-07-26 23:55:29270
271 // Cleanup.
initial.commit09911bf2008-07-26 23:55:29272 p->Remove(kRendererID);
273}
274
[email protected]f58ddcf2009-05-18 22:22:06275TEST_F(ChildProcessSecurityPolicyTest, CanServiceCommandsTest) {
[email protected]b9535422012-02-09 01:47:59276 ChildProcessSecurityPolicyImpl* p =
277 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29278
279 p->Add(kRendererID);
280
281 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd")));
creis3710b2382015-08-18 00:12:15282 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("file:///etc/passwd")));
initial.commit09911bf2008-07-26 23:55:29283 p->GrantRequestURL(kRendererID, GURL("file:///etc/passwd"));
284 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd")));
creis3710b2382015-08-18 00:12:15285 EXPECT_TRUE(p->CanCommitURL(kRendererID, GURL("file:///etc/passwd")));
initial.commit09911bf2008-07-26 23:55:29286
287 // We should forget our state if we repeat a renderer id.
288 p->Remove(kRendererID);
289 p->Add(kRendererID);
290 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd")));
creis3710b2382015-08-18 00:12:15291 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("file:///etc/passwd")));
initial.commit09911bf2008-07-26 23:55:29292 p->Remove(kRendererID);
293}
294
[email protected]f58ddcf2009-05-18 22:22:06295TEST_F(ChildProcessSecurityPolicyTest, ViewSource) {
[email protected]b9535422012-02-09 01:47:59296 ChildProcessSecurityPolicyImpl* p =
297 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29298
299 p->Add(kRendererID);
300
meacerce6b66032016-06-02 20:56:05301 // Child processes cannot request view source URLs.
302 EXPECT_FALSE(p->CanRequestURL(kRendererID,
303 GURL("view-source:http://www.google.com/")));
initial.commit09911bf2008-07-26 23:55:29304 EXPECT_FALSE(p->CanRequestURL(kRendererID,
305 GURL("view-source:file:///etc/passwd")));
306 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd")));
[email protected]690d0a9172010-01-06 00:19:36307 EXPECT_FALSE(p->CanRequestURL(
308 kRendererID, GURL("view-source:view-source:http://www.google.com/")));
initial.commit09911bf2008-07-26 23:55:29309
creis3710b2382015-08-18 00:12:15310 // View source URLs don't actually commit; the renderer is put into view
311 // source mode, and the inner URL commits.
312 EXPECT_FALSE(p->CanCommitURL(kRendererID,
313 GURL("view-source:http://www.google.com/")));
314 EXPECT_FALSE(p->CanCommitURL(kRendererID,
315 GURL("view-source:file:///etc/passwd")));
316 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("file:///etc/passwd")));
317 EXPECT_FALSE(p->CanCommitURL(
318 kRendererID, GURL("view-source:view-source:http://www.google.com/")));
319
initial.commit09911bf2008-07-26 23:55:29320 p->GrantRequestURL(kRendererID, GURL("view-source:file:///etc/passwd"));
meacerce6b66032016-06-02 20:56:05321 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd")));
322 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("file:///etc/passwd")));
323 EXPECT_FALSE(
324 p->CanRequestURL(kRendererID, GURL("view-source:file:///etc/passwd")));
creis3710b2382015-08-18 00:12:15325 EXPECT_FALSE(p->CanCommitURL(kRendererID,
326 GURL("view-source:file:///etc/passwd")));
initial.commit09911bf2008-07-26 23:55:29327 p->Remove(kRendererID);
328}
329
[email protected]dc67e1c32012-06-08 00:10:40330TEST_F(ChildProcessSecurityPolicyTest, SpecificFile) {
331 ChildProcessSecurityPolicyImpl* p =
332 ChildProcessSecurityPolicyImpl::GetInstance();
333
334 p->Add(kRendererID);
335
336 GURL icon_url("file:///tmp/foo.png");
337 GURL sensitive_url("file:///etc/passwd");
338 EXPECT_FALSE(p->CanRequestURL(kRendererID, icon_url));
339 EXPECT_FALSE(p->CanRequestURL(kRendererID, sensitive_url));
creis3710b2382015-08-18 00:12:15340 EXPECT_FALSE(p->CanCommitURL(kRendererID, icon_url));
341 EXPECT_FALSE(p->CanCommitURL(kRendererID, sensitive_url));
[email protected]dc67e1c32012-06-08 00:10:40342
343 p->GrantRequestSpecificFileURL(kRendererID, icon_url);
344 EXPECT_TRUE(p->CanRequestURL(kRendererID, icon_url));
345 EXPECT_FALSE(p->CanRequestURL(kRendererID, sensitive_url));
creis3710b2382015-08-18 00:12:15346 EXPECT_TRUE(p->CanCommitURL(kRendererID, icon_url));
347 EXPECT_FALSE(p->CanCommitURL(kRendererID, sensitive_url));
[email protected]dc67e1c32012-06-08 00:10:40348
349 p->GrantRequestURL(kRendererID, icon_url);
350 EXPECT_TRUE(p->CanRequestURL(kRendererID, icon_url));
351 EXPECT_TRUE(p->CanRequestURL(kRendererID, sensitive_url));
creis3710b2382015-08-18 00:12:15352 EXPECT_TRUE(p->CanCommitURL(kRendererID, icon_url));
353 EXPECT_TRUE(p->CanCommitURL(kRendererID, sensitive_url));
[email protected]dc67e1c32012-06-08 00:10:40354
355 p->Remove(kRendererID);
356}
357
[email protected]b78c188fa62013-07-23 18:04:45358TEST_F(ChildProcessSecurityPolicyTest, FileSystemGrantsTest) {
359 ChildProcessSecurityPolicyImpl* p =
360 ChildProcessSecurityPolicyImpl::GetInstance();
361
362 p->Add(kRendererID);
[email protected]cd501a72014-08-22 19:58:31363 std::string read_id =
364 storage::IsolatedContext::GetInstance()->RegisterFileSystemForVirtualPath(
365 storage::kFileSystemTypeTest, "read_filesystem", base::FilePath());
366 std::string read_write_id =
367 storage::IsolatedContext::GetInstance()->RegisterFileSystemForVirtualPath(
368 storage::kFileSystemTypeTest,
369 "read_write_filesystem",
370 base::FilePath());
371 std::string copy_into_id =
372 storage::IsolatedContext::GetInstance()->RegisterFileSystemForVirtualPath(
373 storage::kFileSystemTypeTest,
374 "copy_into_filesystem",
375 base::FilePath());
376 std::string delete_from_id =
377 storage::IsolatedContext::GetInstance()->RegisterFileSystemForVirtualPath(
378 storage::kFileSystemTypeTest,
379 "delete_from_filesystem",
380 base::FilePath());
[email protected]b78c188fa62013-07-23 18:04:45381
382 // Test initially having no permissions.
[email protected]5a65fde32013-10-22 05:15:34383 CheckHasNoFileSystemPermission(p, read_id);
384 CheckHasNoFileSystemPermission(p, read_write_id);
385 CheckHasNoFileSystemPermission(p, copy_into_id);
386 CheckHasNoFileSystemPermission(p, delete_from_id);
[email protected]b78c188fa62013-07-23 18:04:45387
388 // Testing varying combinations of grants and checks.
389 p->GrantReadFileSystem(kRendererID, read_id);
390 EXPECT_TRUE(p->CanReadFileSystem(kRendererID, read_id));
391 EXPECT_FALSE(p->CanReadWriteFileSystem(kRendererID, read_id));
392 EXPECT_FALSE(p->CanCopyIntoFileSystem(kRendererID, read_id));
[email protected]5a65fde32013-10-22 05:15:34393 EXPECT_FALSE(p->CanDeleteFromFileSystem(kRendererID, read_id));
[email protected]b78c188fa62013-07-23 18:04:45394
395 p->GrantReadFileSystem(kRendererID, read_write_id);
396 p->GrantWriteFileSystem(kRendererID, read_write_id);
397 EXPECT_TRUE(p->CanReadFileSystem(kRendererID, read_write_id));
398 EXPECT_TRUE(p->CanReadWriteFileSystem(kRendererID, read_write_id));
399 EXPECT_FALSE(p->CanCopyIntoFileSystem(kRendererID, read_write_id));
[email protected]5a65fde32013-10-22 05:15:34400 EXPECT_FALSE(p->CanDeleteFromFileSystem(kRendererID, read_write_id));
[email protected]b78c188fa62013-07-23 18:04:45401
402 p->GrantCopyIntoFileSystem(kRendererID, copy_into_id);
403 EXPECT_FALSE(p->CanReadFileSystem(kRendererID, copy_into_id));
404 EXPECT_FALSE(p->CanReadWriteFileSystem(kRendererID, copy_into_id));
405 EXPECT_TRUE(p->CanCopyIntoFileSystem(kRendererID, copy_into_id));
[email protected]5a65fde32013-10-22 05:15:34406 EXPECT_FALSE(p->CanDeleteFromFileSystem(kRendererID, copy_into_id));
407
408 p->GrantDeleteFromFileSystem(kRendererID, delete_from_id);
409 EXPECT_FALSE(p->CanReadFileSystem(kRendererID, delete_from_id));
410 EXPECT_FALSE(p->CanReadWriteFileSystem(kRendererID, delete_from_id));
411 EXPECT_FALSE(p->CanCopyIntoFileSystem(kRendererID, delete_from_id));
412 EXPECT_TRUE(p->CanDeleteFromFileSystem(kRendererID, delete_from_id));
[email protected]b78c188fa62013-07-23 18:04:45413
414 // Test revoke permissions on renderer ID removal.
415 p->Remove(kRendererID);
[email protected]5a65fde32013-10-22 05:15:34416 CheckHasNoFileSystemPermission(p, read_id);
417 CheckHasNoFileSystemPermission(p, read_write_id);
418 CheckHasNoFileSystemPermission(p, copy_into_id);
419 CheckHasNoFileSystemPermission(p, delete_from_id);
[email protected]b78c188fa62013-07-23 18:04:45420
421 // Test having no permissions upon re-adding same renderer ID.
422 p->Add(kRendererID);
[email protected]5a65fde32013-10-22 05:15:34423 CheckHasNoFileSystemPermission(p, read_id);
424 CheckHasNoFileSystemPermission(p, read_write_id);
425 CheckHasNoFileSystemPermission(p, copy_into_id);
426 CheckHasNoFileSystemPermission(p, delete_from_id);
[email protected]b78c188fa62013-07-23 18:04:45427
428 // Cleanup.
429 p->Remove(kRendererID);
[email protected]cd501a72014-08-22 19:58:31430 storage::IsolatedContext::GetInstance()->RevokeFileSystem(read_id);
431 storage::IsolatedContext::GetInstance()->RevokeFileSystem(read_write_id);
432 storage::IsolatedContext::GetInstance()->RevokeFileSystem(copy_into_id);
433 storage::IsolatedContext::GetInstance()->RevokeFileSystem(delete_from_id);
[email protected]b78c188fa62013-07-23 18:04:45434}
435
[email protected]9f104312013-07-23 23:18:19436TEST_F(ChildProcessSecurityPolicyTest, FilePermissionGrantingAndRevoking) {
[email protected]b9535422012-02-09 01:47:59437 ChildProcessSecurityPolicyImpl* p =
438 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29439
[email protected]9f104312013-07-23 23:18:19440 p->RegisterFileSystemPermissionPolicy(
[email protected]cd501a72014-08-22 19:58:31441 storage::kFileSystemTypeTest,
442 storage::FILE_PERMISSION_USE_FILE_PERMISSION);
[email protected]9f104312013-07-23 23:18:19443
initial.commit09911bf2008-07-26 23:55:29444 p->Add(kRendererID);
[email protected]9f104312013-07-23 23:18:19445 base::FilePath file(TEST_PATH("/dir/testfile"));
446 file = file.NormalizePathSeparators();
[email protected]cd501a72014-08-22 19:58:31447 storage::FileSystemURL url = storage::FileSystemURL::CreateForTest(
448 GURL("http://foo/"), storage::kFileSystemTypeTest, file);
initial.commit09911bf2008-07-26 23:55:29449
[email protected]9f104312013-07-23 23:18:19450 // Test initially having no permissions.
[email protected]5a65fde32013-10-22 05:15:34451 CheckHasNoFileSystemFilePermission(p, file, url);
initial.commit09911bf2008-07-26 23:55:29452
[email protected]9f104312013-07-23 23:18:19453 // Testing every combination of permissions granting and revoking.
454 p->GrantReadFile(kRendererID, file);
455 EXPECT_TRUE(p->CanReadFile(kRendererID, file));
[email protected]d4c797f2013-09-26 08:18:53456 EXPECT_FALSE(p->CanCreateReadWriteFile(kRendererID, file));
[email protected]9f104312013-07-23 23:18:19457 EXPECT_TRUE(p->CanReadFileSystemFile(kRendererID, url));
458 EXPECT_FALSE(p->CanWriteFileSystemFile(kRendererID, url));
459 EXPECT_FALSE(p->CanCreateFileSystemFile(kRendererID, url));
[email protected]d4c797f2013-09-26 08:18:53460 EXPECT_FALSE(p->CanCreateReadWriteFileSystemFile(kRendererID, url));
[email protected]5a65fde32013-10-22 05:15:34461 EXPECT_FALSE(p->CanCopyIntoFileSystemFile(kRendererID, url));
462 EXPECT_FALSE(p->CanDeleteFileSystemFile(kRendererID, url));
[email protected]9f104312013-07-23 23:18:19463 p->RevokeAllPermissionsForFile(kRendererID, file);
[email protected]5a65fde32013-10-22 05:15:34464 CheckHasNoFileSystemFilePermission(p, file, url);
[email protected]9f104312013-07-23 23:18:19465
466 p->GrantCreateReadWriteFile(kRendererID, file);
467 EXPECT_TRUE(p->CanReadFile(kRendererID, file));
[email protected]d4c797f2013-09-26 08:18:53468 EXPECT_TRUE(p->CanCreateReadWriteFile(kRendererID, file));
[email protected]9f104312013-07-23 23:18:19469 EXPECT_TRUE(p->CanReadFileSystemFile(kRendererID, url));
470 EXPECT_TRUE(p->CanWriteFileSystemFile(kRendererID, url));
471 EXPECT_TRUE(p->CanCreateFileSystemFile(kRendererID, url));
[email protected]d4c797f2013-09-26 08:18:53472 EXPECT_TRUE(p->CanCreateReadWriteFileSystemFile(kRendererID, url));
[email protected]5a65fde32013-10-22 05:15:34473 EXPECT_TRUE(p->CanCopyIntoFileSystemFile(kRendererID, url));
474 EXPECT_TRUE(p->CanDeleteFileSystemFile(kRendererID, url));
[email protected]9f104312013-07-23 23:18:19475 p->RevokeAllPermissionsForFile(kRendererID, file);
[email protected]5a65fde32013-10-22 05:15:34476 CheckHasNoFileSystemFilePermission(p, file, url);
[email protected]9f104312013-07-23 23:18:19477
478 // Test revoke permissions on renderer ID removal.
479 p->GrantCreateReadWriteFile(kRendererID, file);
480 EXPECT_TRUE(p->CanReadFile(kRendererID, file));
[email protected]d4c797f2013-09-26 08:18:53481 EXPECT_TRUE(p->CanCreateReadWriteFile(kRendererID, file));
[email protected]9f104312013-07-23 23:18:19482 EXPECT_TRUE(p->CanReadFileSystemFile(kRendererID, url));
483 EXPECT_TRUE(p->CanWriteFileSystemFile(kRendererID, url));
484 EXPECT_TRUE(p->CanCreateFileSystemFile(kRendererID, url));
[email protected]d4c797f2013-09-26 08:18:53485 EXPECT_TRUE(p->CanCreateReadWriteFileSystemFile(kRendererID, url));
[email protected]5a65fde32013-10-22 05:15:34486 EXPECT_TRUE(p->CanCopyIntoFileSystemFile(kRendererID, url));
487 EXPECT_TRUE(p->CanDeleteFileSystemFile(kRendererID, url));
initial.commit09911bf2008-07-26 23:55:29488 p->Remove(kRendererID);
[email protected]5a65fde32013-10-22 05:15:34489 CheckHasNoFileSystemFilePermission(p, file, url);
[email protected]9f104312013-07-23 23:18:19490
491 // Test having no permissions upon re-adding same renderer ID.
initial.commit09911bf2008-07-26 23:55:29492 p->Add(kRendererID);
[email protected]5a65fde32013-10-22 05:15:34493 CheckHasNoFileSystemFilePermission(p, file, url);
initial.commit09911bf2008-07-26 23:55:29494
[email protected]9f104312013-07-23 23:18:19495 // Cleanup.
initial.commit09911bf2008-07-26 23:55:29496 p->Remove(kRendererID);
497}
498
[email protected]e54edc32010-09-28 01:09:19499TEST_F(ChildProcessSecurityPolicyTest, FilePermissions) {
[email protected]c42de732013-02-16 06:26:31500 base::FilePath granted_file = base::FilePath(TEST_PATH("/home/joe"));
501 base::FilePath sibling_file = base::FilePath(TEST_PATH("/home/bob"));
502 base::FilePath child_file = base::FilePath(TEST_PATH("/home/joe/file"));
503 base::FilePath parent_file = base::FilePath(TEST_PATH("/home"));
504 base::FilePath parent_slash_file = base::FilePath(TEST_PATH("/home/"));
505 base::FilePath child_traversal1 =
506 base::FilePath(TEST_PATH("/home/joe/././file"));
507 base::FilePath child_traversal2 = base::FilePath(
[email protected]f0ecca4522013-01-07 21:50:56508 TEST_PATH("/home/joe/file/../otherfile"));
[email protected]2dec8ec2013-02-07 19:20:34509 base::FilePath evil_traversal1 =
[email protected]023ad6ab2013-02-17 05:07:23510 base::FilePath(TEST_PATH("/home/joe/../../etc/passwd"));
[email protected]c42de732013-02-16 06:26:31511 base::FilePath evil_traversal2 = base::FilePath(
[email protected]f0ecca4522013-01-07 21:50:56512 TEST_PATH("/home/joe/./.././../etc/passwd"));
[email protected]c42de732013-02-16 06:26:31513 base::FilePath self_traversal =
514 base::FilePath(TEST_PATH("/home/joe/../joe/file"));
515 base::FilePath relative_file = base::FilePath(FILE_PATH_LITERAL("home/joe"));
[email protected]80838412012-11-20 01:53:59516
[email protected]b9535422012-02-09 01:47:59517 ChildProcessSecurityPolicyImpl* p =
518 ChildProcessSecurityPolicyImpl::GetInstance();
[email protected]e54edc32010-09-28 01:09:19519
520 // Grant permissions for a file.
521 p->Add(kRendererID);
[email protected]80838412012-11-20 01:53:59522 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41523 base::File::FLAG_OPEN));
[email protected]e54edc32010-09-28 01:09:19524
[email protected]bfcf1e92013-07-11 04:37:25525 GrantPermissionsForFile(p, kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41526 base::File::FLAG_OPEN |
527 base::File::FLAG_OPEN_TRUNCATED |
528 base::File::FLAG_READ |
529 base::File::FLAG_WRITE);
[email protected]80838412012-11-20 01:53:59530 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41531 base::File::FLAG_OPEN |
532 base::File::FLAG_OPEN_TRUNCATED |
533 base::File::FLAG_READ |
534 base::File::FLAG_WRITE));
[email protected]80838412012-11-20 01:53:59535 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41536 base::File::FLAG_OPEN |
537 base::File::FLAG_READ));
[email protected]80838412012-11-20 01:53:59538 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41539 base::File::FLAG_CREATE));
[email protected]f0ecca4522013-01-07 21:50:56540 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file, 0));
[email protected]80838412012-11-20 01:53:59541 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41542 base::File::FLAG_CREATE |
543 base::File::FLAG_OPEN_TRUNCATED |
544 base::File::FLAG_READ |
545 base::File::FLAG_WRITE));
[email protected]80838412012-11-20 01:53:59546 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, sibling_file,
[email protected]2c288ed2014-06-05 22:07:41547 base::File::FLAG_OPEN |
548 base::File::FLAG_READ));
[email protected]80838412012-11-20 01:53:59549 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, parent_file,
[email protected]2c288ed2014-06-05 22:07:41550 base::File::FLAG_OPEN |
551 base::File::FLAG_READ));
[email protected]80838412012-11-20 01:53:59552 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, child_file,
[email protected]2c288ed2014-06-05 22:07:41553 base::File::FLAG_OPEN |
554 base::File::FLAG_READ));
[email protected]80838412012-11-20 01:53:59555 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, child_traversal1,
[email protected]2c288ed2014-06-05 22:07:41556 base::File::FLAG_OPEN |
557 base::File::FLAG_READ));
[email protected]80838412012-11-20 01:53:59558 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, child_traversal2,
[email protected]2c288ed2014-06-05 22:07:41559 base::File::FLAG_OPEN |
560 base::File::FLAG_READ));
[email protected]80838412012-11-20 01:53:59561 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, evil_traversal1,
[email protected]2c288ed2014-06-05 22:07:41562 base::File::FLAG_OPEN |
563 base::File::FLAG_READ));
[email protected]80838412012-11-20 01:53:59564 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, evil_traversal2,
[email protected]2c288ed2014-06-05 22:07:41565 base::File::FLAG_OPEN |
566 base::File::FLAG_READ));
[email protected]80838412012-11-20 01:53:59567 // CPSP doesn't allow this case for the sake of simplicity.
568 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, self_traversal,
[email protected]2c288ed2014-06-05 22:07:41569 base::File::FLAG_OPEN |
570 base::File::FLAG_READ));
[email protected]e54edc32010-09-28 01:09:19571 p->Remove(kRendererID);
572
573 // Grant permissions for the directory the file is in.
574 p->Add(kRendererID);
[email protected]80838412012-11-20 01:53:59575 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41576 base::File::FLAG_OPEN));
[email protected]bfcf1e92013-07-11 04:37:25577 GrantPermissionsForFile(p, kRendererID, parent_file,
[email protected]2c288ed2014-06-05 22:07:41578 base::File::FLAG_OPEN |
579 base::File::FLAG_READ);
[email protected]80838412012-11-20 01:53:59580 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41581 base::File::FLAG_OPEN));
[email protected]80838412012-11-20 01:53:59582 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41583 base::File::FLAG_READ |
584 base::File::FLAG_WRITE));
[email protected]e54edc32010-09-28 01:09:19585 p->Remove(kRendererID);
586
587 // Grant permissions for the directory the file is in (with trailing '/').
588 p->Add(kRendererID);
[email protected]80838412012-11-20 01:53:59589 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41590 base::File::FLAG_OPEN));
[email protected]bfcf1e92013-07-11 04:37:25591 GrantPermissionsForFile(p, kRendererID, parent_slash_file,
[email protected]2c288ed2014-06-05 22:07:41592 base::File::FLAG_OPEN |
593 base::File::FLAG_READ);
[email protected]80838412012-11-20 01:53:59594 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41595 base::File::FLAG_OPEN));
[email protected]80838412012-11-20 01:53:59596 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41597 base::File::FLAG_READ |
598 base::File::FLAG_WRITE));
[email protected]e54edc32010-09-28 01:09:19599
600 // Grant permissions for the file (should overwrite the permissions granted
601 // for the directory).
[email protected]bfcf1e92013-07-11 04:37:25602 GrantPermissionsForFile(p, kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41603 base::File::FLAG_TEMPORARY);
[email protected]80838412012-11-20 01:53:59604 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41605 base::File::FLAG_OPEN));
[email protected]80838412012-11-20 01:53:59606 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41607 base::File::FLAG_TEMPORARY));
[email protected]77930fe2010-10-01 22:45:34608
609 // Revoke all permissions for the file (it should inherit its permissions
610 // from the directory again).
[email protected]80838412012-11-20 01:53:59611 p->RevokeAllPermissionsForFile(kRendererID, granted_file);
612 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41613 base::File::FLAG_OPEN |
614 base::File::FLAG_READ));
[email protected]80838412012-11-20 01:53:59615 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41616 base::File::FLAG_TEMPORARY));
[email protected]e54edc32010-09-28 01:09:19617 p->Remove(kRendererID);
[email protected]cee64fd32011-05-02 18:59:07618
619 // Grant file permissions for the file to main thread renderer process,
620 // make sure its worker thread renderer process inherits those.
621 p->Add(kRendererID);
[email protected]bfcf1e92013-07-11 04:37:25622 GrantPermissionsForFile(p, kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41623 base::File::FLAG_OPEN |
624 base::File::FLAG_READ);
[email protected]80838412012-11-20 01:53:59625 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41626 base::File::FLAG_OPEN |
627 base::File::FLAG_READ));
[email protected]80838412012-11-20 01:53:59628 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41629 base::File::FLAG_WRITE));
[email protected]cee64fd32011-05-02 18:59:07630 p->AddWorker(kWorkerRendererID, kRendererID);
[email protected]80838412012-11-20 01:53:59631 EXPECT_TRUE(p->HasPermissionsForFile(kWorkerRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41632 base::File::FLAG_OPEN |
633 base::File::FLAG_READ));
[email protected]80838412012-11-20 01:53:59634 EXPECT_FALSE(p->HasPermissionsForFile(kWorkerRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41635 base::File::FLAG_WRITE));
[email protected]cee64fd32011-05-02 18:59:07636 p->Remove(kRendererID);
[email protected]80838412012-11-20 01:53:59637 EXPECT_FALSE(p->HasPermissionsForFile(kWorkerRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41638 base::File::FLAG_OPEN |
639 base::File::FLAG_READ));
[email protected]cee64fd32011-05-02 18:59:07640 p->Remove(kWorkerRendererID);
[email protected]f0ecca4522013-01-07 21:50:56641
642 p->Add(kRendererID);
[email protected]bfcf1e92013-07-11 04:37:25643 GrantPermissionsForFile(p, kRendererID, relative_file,
[email protected]2c288ed2014-06-05 22:07:41644 base::File::FLAG_OPEN);
[email protected]f0ecca4522013-01-07 21:50:56645 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, relative_file,
[email protected]2c288ed2014-06-05 22:07:41646 base::File::FLAG_OPEN));
[email protected]f0ecca4522013-01-07 21:50:56647 p->Remove(kRendererID);
[email protected]e54edc32010-09-28 01:09:19648}
649
[email protected]c50008512011-02-03 01:17:27650TEST_F(ChildProcessSecurityPolicyTest, CanServiceWebUIBindings) {
[email protected]b9535422012-02-09 01:47:59651 ChildProcessSecurityPolicyImpl* p =
652 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29653
[email protected]60e448982009-05-06 04:21:16654 GURL url("chrome://thumb/http://www.google.com/");
initial.commit09911bf2008-07-26 23:55:29655
656 p->Add(kRendererID);
657
[email protected]c50008512011-02-03 01:17:27658 EXPECT_FALSE(p->HasWebUIBindings(kRendererID));
initial.commit09911bf2008-07-26 23:55:29659 EXPECT_FALSE(p->CanRequestURL(kRendererID, url));
[email protected]c50008512011-02-03 01:17:27660 p->GrantWebUIBindings(kRendererID);
661 EXPECT_TRUE(p->HasWebUIBindings(kRendererID));
initial.commit09911bf2008-07-26 23:55:29662 EXPECT_TRUE(p->CanRequestURL(kRendererID, url));
663
664 p->Remove(kRendererID);
665}
666
[email protected]f58ddcf2009-05-18 22:22:06667TEST_F(ChildProcessSecurityPolicyTest, RemoveRace) {
[email protected]b9535422012-02-09 01:47:59668 ChildProcessSecurityPolicyImpl* p =
669 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29670
671 GURL url("file:///etc/passwd");
[email protected]2dec8ec2013-02-07 19:20:34672 base::FilePath file(TEST_PATH("/etc/passwd"));
initial.commit09911bf2008-07-26 23:55:29673
674 p->Add(kRendererID);
675
676 p->GrantRequestURL(kRendererID, url);
[email protected]e54edc32010-09-28 01:09:19677 p->GrantReadFile(kRendererID, file);
[email protected]c50008512011-02-03 01:17:27678 p->GrantWebUIBindings(kRendererID);
initial.commit09911bf2008-07-26 23:55:29679
680 EXPECT_TRUE(p->CanRequestURL(kRendererID, url));
[email protected]e54edc32010-09-28 01:09:19681 EXPECT_TRUE(p->CanReadFile(kRendererID, file));
[email protected]c50008512011-02-03 01:17:27682 EXPECT_TRUE(p->HasWebUIBindings(kRendererID));
initial.commit09911bf2008-07-26 23:55:29683
684 p->Remove(kRendererID);
685
686 // Renderers are added and removed on the UI thread, but the policy can be
[email protected]580522632009-08-17 21:55:55687 // queried on the IO thread. The ChildProcessSecurityPolicy needs to be
688 // prepared to answer policy questions about renderers who no longer exist.
initial.commit09911bf2008-07-26 23:55:29689
690 // In this case, we default to secure behavior.
691 EXPECT_FALSE(p->CanRequestURL(kRendererID, url));
[email protected]e54edc32010-09-28 01:09:19692 EXPECT_FALSE(p->CanReadFile(kRendererID, file));
[email protected]c50008512011-02-03 01:17:27693 EXPECT_FALSE(p->HasWebUIBindings(kRendererID));
initial.commit09911bf2008-07-26 23:55:29694}
[email protected]46488322012-10-30 03:22:20695
paulmeyer1eefa26e2015-10-01 02:11:13696// Test the granting of origin permissions, and their interactions with
697// granting scheme permissions.
698TEST_F(ChildProcessSecurityPolicyTest, OriginGranting) {
699 ChildProcessSecurityPolicyImpl* p =
700 ChildProcessSecurityPolicyImpl::GetInstance();
701
702 p->Add(kRendererID);
703
704 GURL url_foo1("chrome://foo/resource1");
705 GURL url_foo2("chrome://foo/resource2");
706 GURL url_bar("chrome://bar/resource3");
707
708 EXPECT_FALSE(p->CanRequestURL(kRendererID, url_foo1));
709 EXPECT_FALSE(p->CanRequestURL(kRendererID, url_foo2));
710 EXPECT_FALSE(p->CanRequestURL(kRendererID, url_bar));
711 EXPECT_FALSE(p->CanCommitURL(kRendererID, url_foo1));
712 EXPECT_FALSE(p->CanCommitURL(kRendererID, url_foo2));
713 EXPECT_FALSE(p->CanCommitURL(kRendererID, url_bar));
714
715 p->GrantOrigin(kRendererID, url::Origin(url_foo1));
716
717 EXPECT_TRUE(p->CanRequestURL(kRendererID, url_foo1));
718 EXPECT_TRUE(p->CanRequestURL(kRendererID, url_foo2));
719 EXPECT_FALSE(p->CanRequestURL(kRendererID, url_bar));
720 EXPECT_TRUE(p->CanCommitURL(kRendererID, url_foo1));
721 EXPECT_TRUE(p->CanCommitURL(kRendererID, url_foo2));
722 EXPECT_FALSE(p->CanCommitURL(kRendererID, url_bar));
723
724 p->GrantScheme(kRendererID, kChromeUIScheme);
725
726 EXPECT_TRUE(p->CanRequestURL(kRendererID, url_foo1));
727 EXPECT_TRUE(p->CanRequestURL(kRendererID, url_foo2));
728 EXPECT_TRUE(p->CanRequestURL(kRendererID, url_bar));
729 EXPECT_TRUE(p->CanCommitURL(kRendererID, url_foo1));
730 EXPECT_TRUE(p->CanCommitURL(kRendererID, url_foo2));
731 EXPECT_TRUE(p->CanCommitURL(kRendererID, url_bar));
732
733 p->Remove(kRendererID);
734}
735
[email protected]46488322012-10-30 03:22:20736} // namespace content