blob: 6b69dd7fe38c692ed5e16b0aca2748c6d9c4e8ab [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));
jww04480402016-10-25 02:50:33144 EXPECT_TRUE(p->IsPseudoScheme(url::kHttpSuboriginScheme));
145 EXPECT_TRUE(p->IsPseudoScheme(url::kHttpsSuboriginScheme));
initial.commit09911bf2008-07-26 23:55:29146
[email protected]419a0572011-04-18 22:21:46147 EXPECT_FALSE(p->IsPseudoScheme("registered-pseudo-scheme"));
148 p->RegisterPseudoScheme("registered-pseudo-scheme");
149 EXPECT_TRUE(p->IsPseudoScheme("registered-pseudo-scheme"));
[email protected]89f550b2011-06-08 18:34:03150
[email protected]2d9748b22014-02-11 00:17:29151 EXPECT_FALSE(p->IsPseudoScheme(kChromeUIScheme));
[email protected]419a0572011-04-18 22:21:46152}
153
[email protected]f58ddcf2009-05-18 22:22:06154TEST_F(ChildProcessSecurityPolicyTest, StandardSchemesTest) {
[email protected]b9535422012-02-09 01:47:59155 ChildProcessSecurityPolicyImpl* p =
156 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29157
158 p->Add(kRendererID);
159
arthursonzogni98e5a232017-07-13 15:18:16160 // Safe to request, redirect or commit.
initial.commit09911bf2008-07-26 23:55:29161 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("http://www.google.com/")));
162 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("https://www.paypal.com/")));
163 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("ftp://ftp.gnu.org/")));
164 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("data:text/html,<b>Hi</b>")));
[email protected]039c7b0b22011-03-04 23:15:42165 EXPECT_TRUE(p->CanRequestURL(
166 kRendererID, GURL("filesystem:http://localhost/temporary/a.gif")));
arthursonzogni98e5a232017-07-13 15:18:16167 EXPECT_TRUE(p->CanRedirectToURL(GURL("http://www.google.com/")));
168 EXPECT_TRUE(p->CanRedirectToURL(GURL("https://www.paypal.com/")));
169 EXPECT_TRUE(p->CanRedirectToURL(GURL("ftp://ftp.gnu.org/")));
170 EXPECT_TRUE(p->CanRedirectToURL(GURL("data:text/html,<b>Hi</b>")));
171 EXPECT_TRUE(
172 p->CanRedirectToURL(GURL("filesystem:http://localhost/temporary/a.gif")));
creis3710b2382015-08-18 00:12:15173 EXPECT_TRUE(p->CanCommitURL(kRendererID, GURL("http://www.google.com/")));
174 EXPECT_TRUE(p->CanCommitURL(kRendererID, GURL("https://www.paypal.com/")));
175 EXPECT_TRUE(p->CanCommitURL(kRendererID, GURL("ftp://ftp.gnu.org/")));
176 EXPECT_TRUE(p->CanCommitURL(kRendererID, GURL("data:text/html,<b>Hi</b>")));
177 EXPECT_TRUE(p->CanCommitURL(
178 kRendererID, GURL("filesystem:http://localhost/temporary/a.gif")));
jww2cdad9e2016-09-24 05:42:02179 EXPECT_TRUE(
180 p->CanSetAsOriginHeader(kRendererID, GURL("http://www.google.com/")));
181 EXPECT_TRUE(
182 p->CanSetAsOriginHeader(kRendererID, GURL("https://www.paypal.com/")));
183 EXPECT_TRUE(p->CanSetAsOriginHeader(kRendererID, GURL("ftp://ftp.gnu.org/")));
184 EXPECT_TRUE(
185 p->CanSetAsOriginHeader(kRendererID, GURL("data:text/html,<b>Hi</b>")));
186 EXPECT_TRUE(p->CanSetAsOriginHeader(
187 kRendererID, GURL("filesystem:http://localhost/temporary/a.gif")));
initial.commit09911bf2008-07-26 23:55:29188
jww2cdad9e2016-09-24 05:42:02189 // Dangerous to request, commit, or set as origin header.
initial.commit09911bf2008-07-26 23:55:29190 EXPECT_FALSE(p->CanRequestURL(kRendererID,
191 GURL("file:///etc/passwd")));
192 EXPECT_FALSE(p->CanRequestURL(kRendererID,
[email protected]60e448982009-05-06 04:21:16193 GURL("chrome://foo/bar")));
meacerce6b66032016-06-02 20:56:05194 EXPECT_FALSE(p->CanRequestURL(kRendererID,
195 GURL("view-source:http://www.google.com/")));
arthursonzogni98e5a232017-07-13 15:18:16196 EXPECT_TRUE(p->CanRedirectToURL(GURL("file:///etc/passwd")));
197 EXPECT_TRUE(p->CanRedirectToURL(GURL("chrome://foo/bar")));
198 EXPECT_FALSE(p->CanRedirectToURL(GURL("view-source:http://www.google.com/")));
creis3710b2382015-08-18 00:12:15199 EXPECT_FALSE(p->CanCommitURL(kRendererID,
200 GURL("file:///etc/passwd")));
201 EXPECT_FALSE(p->CanCommitURL(kRendererID,
202 GURL("chrome://foo/bar")));
meacerce6b66032016-06-02 20:56:05203 EXPECT_FALSE(
204 p->CanCommitURL(kRendererID, GURL("view-source:http://www.google.com/")));
jww2cdad9e2016-09-24 05:42:02205 EXPECT_FALSE(
206 p->CanSetAsOriginHeader(kRendererID, GURL("file:///etc/passwd")));
207 EXPECT_FALSE(p->CanSetAsOriginHeader(kRendererID, GURL("chrome://foo/bar")));
208 EXPECT_FALSE(p->CanSetAsOriginHeader(
209 kRendererID, GURL("view-source:http://www.google.com/")));
Alex Moshchuk71f485592017-08-16 16:20:00210 EXPECT_FALSE(p->CanRedirectToURL(GURL(kUnreachableWebDataURL)));
211 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL(kUnreachableWebDataURL)));
initial.commit09911bf2008-07-26 23:55:29212
213 p->Remove(kRendererID);
214}
215
nicka76cc402016-09-22 20:02:59216TEST_F(ChildProcessSecurityPolicyTest, BlobSchemeTest) {
217 ChildProcessSecurityPolicyImpl* p =
218 ChildProcessSecurityPolicyImpl::GetInstance();
219
220 p->Add(kRendererID);
221
222 EXPECT_TRUE(
223 p->CanRequestURL(kRendererID, GURL("blob:http://localhost/some-guid")));
224 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("blob:null/some-guid")));
225 EXPECT_TRUE(
226 p->CanRequestURL(kRendererID, GURL("blob:http://localhost/some-guid")));
227 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("blob:NulL/some-guid")));
228 EXPECT_TRUE(
229 p->CanRequestURL(kRendererID, GURL("blob:NulL/some-guid#fragment")));
230 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("blob:NulL/some-guid?query")));
231 EXPECT_TRUE(
232 p->CanRequestURL(kRendererID, GURL("blob:blobinternal://some-guid")));
233 EXPECT_FALSE(p->CanRequestURL(
234 kRendererID, GURL("blob:http://username@localhost/some-guid")));
235 EXPECT_FALSE(p->CanRequestURL(
236 kRendererID, GURL("blob:http://username @localhost/some-guid")));
237 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("blob:blob:some-guid")));
238 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("blob:some-guid")));
239 EXPECT_FALSE(p->CanRequestURL(kRendererID,
240 GURL("blob:filesystem:http://localhost/path")));
241 EXPECT_FALSE(p->CanRequestURL(kRendererID,
242 GURL("filesystem:blob:http://localhost/guid")));
243
arthursonzogni98e5a232017-07-13 15:18:16244 EXPECT_TRUE(p->CanRedirectToURL(GURL("blob:http://localhost/some-guid")));
245 EXPECT_TRUE(p->CanRedirectToURL(GURL("blob:null/some-guid")));
246 EXPECT_TRUE(p->CanRedirectToURL(GURL("blob:http://localhost/some-guid")));
247 EXPECT_TRUE(p->CanRedirectToURL(GURL("blob:NulL/some-guid")));
248 EXPECT_TRUE(p->CanRedirectToURL(GURL("blob:NulL/some-guid#fragment")));
249 EXPECT_TRUE(p->CanRedirectToURL(GURL("blob:NulL/some-guid?query")));
250 EXPECT_TRUE(p->CanRedirectToURL(GURL("blob:blobinternal://some-guid")));
251 EXPECT_TRUE(
252 p->CanRedirectToURL(GURL("blob:http://username@localhost/some-guid")));
253 EXPECT_TRUE(p->CanRedirectToURL(
254 GURL("blob:http://username @localhost/some-guid")));
255 EXPECT_TRUE(p->CanRedirectToURL(GURL("blob:blob:some-guid")));
256 EXPECT_TRUE(p->CanRedirectToURL(GURL("blob:some-guid")));
257 EXPECT_TRUE(
258 p->CanRedirectToURL(GURL("blob:filesystem:http://localhost/path")));
259 EXPECT_FALSE(
260 p->CanRedirectToURL(GURL("filesystem:blob:http://localhost/guid")));
261
nicka76cc402016-09-22 20:02:59262 EXPECT_TRUE(
263 p->CanCommitURL(kRendererID, GURL("blob:http://localhost/some-guid")));
264 EXPECT_TRUE(p->CanCommitURL(kRendererID, GURL("blob:null/some-guid")));
265 EXPECT_TRUE(
266 p->CanCommitURL(kRendererID, GURL("blob:http://localhost/some-guid")));
267 EXPECT_TRUE(p->CanCommitURL(kRendererID, GURL("blob:NulL/some-guid")));
268 EXPECT_TRUE(
269 p->CanCommitURL(kRendererID, GURL("blob:NulL/some-guid#fragment")));
270 EXPECT_TRUE(
271 p->CanCommitURL(kRendererID, GURL("blob:blobinternal://some-guid")));
272 EXPECT_FALSE(p->CanCommitURL(
273 kRendererID, GURL("blob:http://username@localhost/some-guid")));
274 EXPECT_FALSE(p->CanCommitURL(
275 kRendererID, GURL("blob:http://username @localhost/some-guid")));
276 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("blob:blob:some-guid")));
277 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("blob:some-guid")));
278 EXPECT_FALSE(p->CanCommitURL(kRendererID,
279 GURL("blob:filesystem:http://localhost/path")));
280 EXPECT_FALSE(p->CanCommitURL(kRendererID,
281 GURL("filesystem:blob:http://localhost/guid")));
282
283 p->Remove(kRendererID);
284}
285
[email protected]f58ddcf2009-05-18 22:22:06286TEST_F(ChildProcessSecurityPolicyTest, AboutTest) {
[email protected]b9535422012-02-09 01:47:59287 ChildProcessSecurityPolicyImpl* p =
288 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29289
290 p->Add(kRendererID);
291
292 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("about:blank")));
arthursonzogniee7f43b2016-12-06 10:52:29293 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:BlAnK")));
294 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("aBouT:BlAnK")));
initial.commit09911bf2008-07-26 23:55:29295 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("aBouT:blank")));
arthursonzogni98e5a232017-07-13 15:18:16296 EXPECT_TRUE(p->CanRedirectToURL(GURL("about:blank")));
297 EXPECT_FALSE(p->CanRedirectToURL(GURL("about:BlAnK")));
298 EXPECT_FALSE(p->CanRedirectToURL(GURL("aBouT:BlAnK")));
299 EXPECT_TRUE(p->CanRedirectToURL(GURL("aBouT:blank")));
creis3710b2382015-08-18 00:12:15300 EXPECT_TRUE(p->CanCommitURL(kRendererID, GURL("about:blank")));
arthursonzogniee7f43b2016-12-06 10:52:29301 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("about:BlAnK")));
302 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("aBouT:BlAnK")));
creis3710b2382015-08-18 00:12:15303 EXPECT_TRUE(p->CanCommitURL(kRendererID, GURL("aBouT:blank")));
jww2cdad9e2016-09-24 05:42:02304 EXPECT_TRUE(p->CanSetAsOriginHeader(kRendererID, GURL("about:blank")));
arthursonzogniee7f43b2016-12-06 10:52:29305 EXPECT_FALSE(p->CanSetAsOriginHeader(kRendererID, GURL("about:BlAnK")));
306 EXPECT_FALSE(p->CanSetAsOriginHeader(kRendererID, GURL("aBouT:BlAnK")));
jww2cdad9e2016-09-24 05:42:02307 EXPECT_TRUE(p->CanSetAsOriginHeader(kRendererID, GURL("aBouT:blank")));
initial.commit09911bf2008-07-26 23:55:29308
arthursonzogniee7f43b2016-12-06 10:52:29309 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("about:srcdoc")));
arthursonzogni98e5a232017-07-13 15:18:16310 EXPECT_FALSE(p->CanRedirectToURL(GURL("about:srcdoc")));
arthursonzogniee7f43b2016-12-06 10:52:29311 EXPECT_TRUE(p->CanCommitURL(kRendererID, GURL("about:srcdoc")));
312 EXPECT_FALSE(p->CanSetAsOriginHeader(kRendererID, GURL("about:srcdoc")));
313 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:SRCDOC")));
314 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("about:SRCDOC")));
315 EXPECT_FALSE(p->CanSetAsOriginHeader(kRendererID, GURL("about:SRCDOC")));
316
[email protected]ed3456f2009-02-26 20:24:48317 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:crash")));
318 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:cache")));
319 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:hang")));
asvitkine2c4b4d1a2016-03-19 14:18:07320 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:version")));
arthursonzogni98e5a232017-07-13 15:18:16321 EXPECT_FALSE(p->CanRedirectToURL(GURL("about:crash")));
322 EXPECT_FALSE(p->CanRedirectToURL(GURL("about:cache")));
323 EXPECT_FALSE(p->CanRedirectToURL(GURL("about:hang")));
324 EXPECT_FALSE(p->CanRedirectToURL(GURL("about:version")));
creis3710b2382015-08-18 00:12:15325 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("about:crash")));
326 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("about:cache")));
327 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("about:hang")));
asvitkine2c4b4d1a2016-03-19 14:18:07328 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("about:version")));
jww2cdad9e2016-09-24 05:42:02329 EXPECT_FALSE(p->CanSetAsOriginHeader(kRendererID, GURL("about:crash")));
330 EXPECT_FALSE(p->CanSetAsOriginHeader(kRendererID, GURL("about:cache")));
331 EXPECT_FALSE(p->CanSetAsOriginHeader(kRendererID, GURL("about:hang")));
332 EXPECT_FALSE(p->CanSetAsOriginHeader(kRendererID, GURL("about:version")));
initial.commit09911bf2008-07-26 23:55:29333
asvitkine2c4b4d1a2016-03-19 14:18:07334 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("aBoUt:version")));
initial.commit09911bf2008-07-26 23:55:29335 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:CrASh")));
336 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("abOuT:cAChe")));
arthursonzogni98e5a232017-07-13 15:18:16337 EXPECT_FALSE(p->CanRedirectToURL(GURL("aBoUt:version")));
338 EXPECT_FALSE(p->CanRedirectToURL(GURL("about:CrASh")));
339 EXPECT_FALSE(p->CanRedirectToURL(GURL("abOuT:cAChe")));
asvitkine2c4b4d1a2016-03-19 14:18:07340 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("aBoUt:version")));
creis3710b2382015-08-18 00:12:15341 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("about:CrASh")));
342 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("abOuT:cAChe")));
asvitkine2c4b4d1a2016-03-19 14:18:07343 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("aBoUt:version")));
jww2cdad9e2016-09-24 05:42:02344 EXPECT_FALSE(p->CanSetAsOriginHeader(kRendererID, GURL("aBoUt:version")));
345 EXPECT_FALSE(p->CanSetAsOriginHeader(kRendererID, GURL("about:CrASh")));
346 EXPECT_FALSE(p->CanSetAsOriginHeader(kRendererID, GURL("abOuT:cAChe")));
347 EXPECT_FALSE(p->CanSetAsOriginHeader(kRendererID, GURL("aBoUt:version")));
initial.commit09911bf2008-07-26 23:55:29348
[email protected]8bf1048012012-02-08 01:22:18349 // Requests for about: pages should be denied.
350 p->GrantRequestURL(kRendererID, GURL("about:crash"));
351 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:crash")));
arthursonzogni98e5a232017-07-13 15:18:16352 EXPECT_FALSE(p->CanRedirectToURL(GURL("about:crash")));
creis3710b2382015-08-18 00:12:15353 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("about:crash")));
jww2cdad9e2016-09-24 05:42:02354 EXPECT_FALSE(p->CanSetAsOriginHeader(kRendererID, GURL("about:crash")));
initial.commit09911bf2008-07-26 23:55:29355
[email protected]89f550b2011-06-08 18:34:03356 // These requests for chrome:// pages should be granted.
[email protected]e068c2d2012-10-23 16:45:18357 GURL chrome_url("chrome://foo");
358 p->GrantRequestURL(kRendererID, chrome_url);
359 EXPECT_TRUE(p->CanRequestURL(kRendererID, chrome_url));
arthursonzogni98e5a232017-07-13 15:18:16360 EXPECT_TRUE(p->CanRedirectToURL(GURL(chrome_url)));
creis3710b2382015-08-18 00:12:15361 EXPECT_TRUE(p->CanCommitURL(kRendererID, chrome_url));
jww2cdad9e2016-09-24 05:42:02362 EXPECT_TRUE(p->CanSetAsOriginHeader(kRendererID, chrome_url));
[email protected]89f550b2011-06-08 18:34:03363
initial.commit09911bf2008-07-26 23:55:29364 p->Remove(kRendererID);
365}
366
[email protected]f58ddcf2009-05-18 22:22:06367TEST_F(ChildProcessSecurityPolicyTest, JavaScriptTest) {
[email protected]b9535422012-02-09 01:47:59368 ChildProcessSecurityPolicyImpl* p =
369 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29370
371 p->Add(kRendererID);
372
373 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("javascript:alert('xss')")));
arthursonzogni98e5a232017-07-13 15:18:16374 EXPECT_FALSE(p->CanRedirectToURL(GURL("javascript:alert('xss')")));
creis3710b2382015-08-18 00:12:15375 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("javascript:alert('xss')")));
jww2cdad9e2016-09-24 05:42:02376 EXPECT_FALSE(
377 p->CanSetAsOriginHeader(kRendererID, GURL("javascript:alert('xss')")));
initial.commit09911bf2008-07-26 23:55:29378 p->GrantRequestURL(kRendererID, GURL("javascript:alert('xss')"));
379 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("javascript:alert('xss')")));
arthursonzogni98e5a232017-07-13 15:18:16380 EXPECT_FALSE(p->CanRedirectToURL(GURL("javascript:alert('xss')")));
creis3710b2382015-08-18 00:12:15381 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("javascript:alert('xss')")));
jww2cdad9e2016-09-24 05:42:02382 EXPECT_FALSE(
383 p->CanSetAsOriginHeader(kRendererID, GURL("javascript:alert('xss')")));
384
385 p->Remove(kRendererID);
386}
387
388TEST_F(ChildProcessSecurityPolicyTest, SuboriginTest) {
389 ChildProcessSecurityPolicyImpl* p =
390 ChildProcessSecurityPolicyImpl::GetInstance();
391
392 p->Add(kRendererID);
393
394 // Suborigin URLs are not requestable or committable.
395 EXPECT_FALSE(
396 p->CanRequestURL(kRendererID, GURL("http-so://foobar.example.com")));
397 EXPECT_FALSE(
398 p->CanRequestURL(kRendererID, GURL("https-so://foobar.example.com")));
arthursonzogni98e5a232017-07-13 15:18:16399 EXPECT_FALSE(p->CanRedirectToURL(GURL("http-so://foobar.example.com")));
400 EXPECT_FALSE(p->CanRedirectToURL(GURL("https-so://foobar.example.com")));
jww2cdad9e2016-09-24 05:42:02401 EXPECT_FALSE(
402 p->CanCommitURL(kRendererID, GURL("http-so://foobar.example.com")));
403 EXPECT_FALSE(
404 p->CanCommitURL(kRendererID, GURL("https-so://foobar.example.com")));
405
406 // It's not possible to grant suborigins requestable status.
407 p->GrantRequestURL(kRendererID, GURL("https-so://foobar.example.com"));
408 EXPECT_FALSE(
409 p->CanCommitURL(kRendererID, GURL("https-so://foobar.example.com")));
410
411 // Suborigin URLs are valid origin headers.
412 EXPECT_TRUE(p->CanSetAsOriginHeader(kRendererID,
413 GURL("http-so://foobar.example.com")));
414 EXPECT_TRUE(p->CanSetAsOriginHeader(kRendererID,
415 GURL("https-so://foobar.example.com")));
initial.commit09911bf2008-07-26 23:55:29416
417 p->Remove(kRendererID);
418}
419
[email protected]f58ddcf2009-05-18 22:22:06420TEST_F(ChildProcessSecurityPolicyTest, RegisterWebSafeSchemeTest) {
[email protected]b9535422012-02-09 01:47:59421 ChildProcessSecurityPolicyImpl* p =
422 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29423
424 p->Add(kRendererID);
425
creis3710b2382015-08-18 00:12:15426 // Currently, "asdf" is destined for ShellExecute, so it is allowed to be
427 // requested but not committed.
initial.commit09911bf2008-07-26 23:55:29428 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("asdf:rockers")));
arthursonzogni98e5a232017-07-13 15:18:16429 EXPECT_TRUE(p->CanRedirectToURL(GURL("asdf:rockers")));
creis3710b2382015-08-18 00:12:15430 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("asdf:rockers")));
jww2cdad9e2016-09-24 05:42:02431 EXPECT_FALSE(p->CanSetAsOriginHeader(kRendererID, GURL("asdf:rockers")));
initial.commit09911bf2008-07-26 23:55:29432
[email protected]46fb9442011-12-09 17:57:47433 // Once we register "asdf", we default to deny.
434 RegisterTestScheme("asdf");
initial.commit09911bf2008-07-26 23:55:29435 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("asdf:rockers")));
arthursonzogni98e5a232017-07-13 15:18:16436 EXPECT_TRUE(p->CanRedirectToURL(GURL("asdf:rockers")));
creis3710b2382015-08-18 00:12:15437 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("asdf:rockers")));
jww2cdad9e2016-09-24 05:42:02438 EXPECT_FALSE(p->CanSetAsOriginHeader(kRendererID, GURL("asdf:rockers")));
initial.commit09911bf2008-07-26 23:55:29439
440 // We can allow new schemes by adding them to the whitelist.
441 p->RegisterWebSafeScheme("asdf");
442 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("asdf:rockers")));
arthursonzogni98e5a232017-07-13 15:18:16443 EXPECT_TRUE(p->CanRedirectToURL(GURL("asdf:rockers")));
creis3710b2382015-08-18 00:12:15444 EXPECT_TRUE(p->CanCommitURL(kRendererID, GURL("asdf:rockers")));
jww2cdad9e2016-09-24 05:42:02445 EXPECT_TRUE(p->CanSetAsOriginHeader(kRendererID, GURL("asdf:rockers")));
initial.commit09911bf2008-07-26 23:55:29446
447 // Cleanup.
initial.commit09911bf2008-07-26 23:55:29448 p->Remove(kRendererID);
449}
450
[email protected]f58ddcf2009-05-18 22:22:06451TEST_F(ChildProcessSecurityPolicyTest, CanServiceCommandsTest) {
[email protected]b9535422012-02-09 01:47:59452 ChildProcessSecurityPolicyImpl* p =
453 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29454
455 p->Add(kRendererID);
456
457 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd")));
arthursonzogni98e5a232017-07-13 15:18:16458 EXPECT_TRUE(p->CanRedirectToURL(GURL("file:///etc/passwd")));
creis3710b2382015-08-18 00:12:15459 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("file:///etc/passwd")));
jww2cdad9e2016-09-24 05:42:02460 EXPECT_FALSE(
461 p->CanSetAsOriginHeader(kRendererID, GURL("file:///etc/passwd")));
initial.commit09911bf2008-07-26 23:55:29462 p->GrantRequestURL(kRendererID, GURL("file:///etc/passwd"));
463 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd")));
arthursonzogni98e5a232017-07-13 15:18:16464 EXPECT_TRUE(p->CanRedirectToURL(GURL("file:///etc/passwd")));
creis3710b2382015-08-18 00:12:15465 EXPECT_TRUE(p->CanCommitURL(kRendererID, GURL("file:///etc/passwd")));
jww2cdad9e2016-09-24 05:42:02466 EXPECT_TRUE(p->CanSetAsOriginHeader(kRendererID, GURL("file:///etc/passwd")));
initial.commit09911bf2008-07-26 23:55:29467
468 // We should forget our state if we repeat a renderer id.
469 p->Remove(kRendererID);
470 p->Add(kRendererID);
471 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd")));
arthursonzogni98e5a232017-07-13 15:18:16472 EXPECT_TRUE(p->CanRedirectToURL(GURL("file:///etc/passwd")));
creis3710b2382015-08-18 00:12:15473 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("file:///etc/passwd")));
jww2cdad9e2016-09-24 05:42:02474 EXPECT_FALSE(
475 p->CanSetAsOriginHeader(kRendererID, GURL("file:///etc/passwd")));
initial.commit09911bf2008-07-26 23:55:29476 p->Remove(kRendererID);
477}
478
[email protected]f58ddcf2009-05-18 22:22:06479TEST_F(ChildProcessSecurityPolicyTest, ViewSource) {
[email protected]b9535422012-02-09 01:47:59480 ChildProcessSecurityPolicyImpl* p =
481 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29482
483 p->Add(kRendererID);
484
meacerce6b66032016-06-02 20:56:05485 // Child processes cannot request view source URLs.
486 EXPECT_FALSE(p->CanRequestURL(kRendererID,
487 GURL("view-source:http://www.google.com/")));
initial.commit09911bf2008-07-26 23:55:29488 EXPECT_FALSE(p->CanRequestURL(kRendererID,
489 GURL("view-source:file:///etc/passwd")));
490 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd")));
[email protected]690d0a9172010-01-06 00:19:36491 EXPECT_FALSE(p->CanRequestURL(
492 kRendererID, GURL("view-source:view-source:http://www.google.com/")));
initial.commit09911bf2008-07-26 23:55:29493
arthursonzogni98e5a232017-07-13 15:18:16494 // Child processes cannot be redirected to view source URLs.
495 EXPECT_FALSE(p->CanRedirectToURL(GURL("view-source:http://www.google.com/")));
496 EXPECT_FALSE(p->CanRedirectToURL(GURL("view-source:file:///etc/passwd")));
497 EXPECT_TRUE(p->CanRedirectToURL(GURL("file:///etc/passwd")));
498 EXPECT_FALSE(p->CanRedirectToURL(
499 GURL("view-source:view-source:http://www.google.com/")));
500
creis3710b2382015-08-18 00:12:15501 // View source URLs don't actually commit; the renderer is put into view
502 // source mode, and the inner URL commits.
503 EXPECT_FALSE(p->CanCommitURL(kRendererID,
504 GURL("view-source:http://www.google.com/")));
505 EXPECT_FALSE(p->CanCommitURL(kRendererID,
506 GURL("view-source:file:///etc/passwd")));
507 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("file:///etc/passwd")));
508 EXPECT_FALSE(p->CanCommitURL(
509 kRendererID, GURL("view-source:view-source:http://www.google.com/")));
510
jww2cdad9e2016-09-24 05:42:02511 // View source URLs should not be setable as origin headers
512 EXPECT_FALSE(p->CanSetAsOriginHeader(
513 kRendererID, GURL("view-source:http://www.google.com/")));
514 EXPECT_FALSE(p->CanSetAsOriginHeader(kRendererID,
515 GURL("view-source:file:///etc/passwd")));
516 EXPECT_FALSE(
517 p->CanSetAsOriginHeader(kRendererID, GURL("file:///etc/passwd")));
518 EXPECT_FALSE(p->CanSetAsOriginHeader(
519 kRendererID, GURL("view-source:view-source:http://www.google.com/")));
520
initial.commit09911bf2008-07-26 23:55:29521 p->GrantRequestURL(kRendererID, GURL("view-source:file:///etc/passwd"));
meacerce6b66032016-06-02 20:56:05522 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd")));
arthursonzogni98e5a232017-07-13 15:18:16523 EXPECT_TRUE(p->CanRedirectToURL(GURL("file:///etc/passwd")));
meacerce6b66032016-06-02 20:56:05524 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("file:///etc/passwd")));
525 EXPECT_FALSE(
jww2cdad9e2016-09-24 05:42:02526 p->CanSetAsOriginHeader(kRendererID, GURL("file:///etc/passwd")));
527 EXPECT_FALSE(
meacerce6b66032016-06-02 20:56:05528 p->CanRequestURL(kRendererID, GURL("view-source:file:///etc/passwd")));
arthursonzogni98e5a232017-07-13 15:18:16529 EXPECT_FALSE(p->CanRedirectToURL(GURL("view-source:file:///etc/passwd")));
creis3710b2382015-08-18 00:12:15530 EXPECT_FALSE(p->CanCommitURL(kRendererID,
531 GURL("view-source:file:///etc/passwd")));
jww2cdad9e2016-09-24 05:42:02532 EXPECT_FALSE(p->CanSetAsOriginHeader(kRendererID,
533 GURL("view-source:file:///etc/passwd")));
initial.commit09911bf2008-07-26 23:55:29534 p->Remove(kRendererID);
535}
536
[email protected]dc67e1c32012-06-08 00:10:40537TEST_F(ChildProcessSecurityPolicyTest, SpecificFile) {
538 ChildProcessSecurityPolicyImpl* p =
539 ChildProcessSecurityPolicyImpl::GetInstance();
540
541 p->Add(kRendererID);
542
543 GURL icon_url("file:///tmp/foo.png");
544 GURL sensitive_url("file:///etc/passwd");
545 EXPECT_FALSE(p->CanRequestURL(kRendererID, icon_url));
546 EXPECT_FALSE(p->CanRequestURL(kRendererID, sensitive_url));
arthursonzogni98e5a232017-07-13 15:18:16547 EXPECT_TRUE(p->CanRedirectToURL(icon_url));
548 EXPECT_TRUE(p->CanRedirectToURL(sensitive_url));
creis3710b2382015-08-18 00:12:15549 EXPECT_FALSE(p->CanCommitURL(kRendererID, icon_url));
550 EXPECT_FALSE(p->CanCommitURL(kRendererID, sensitive_url));
jww2cdad9e2016-09-24 05:42:02551 EXPECT_FALSE(p->CanSetAsOriginHeader(kRendererID, icon_url));
552 EXPECT_FALSE(p->CanSetAsOriginHeader(kRendererID, sensitive_url));
[email protected]dc67e1c32012-06-08 00:10:40553
554 p->GrantRequestSpecificFileURL(kRendererID, icon_url);
555 EXPECT_TRUE(p->CanRequestURL(kRendererID, icon_url));
556 EXPECT_FALSE(p->CanRequestURL(kRendererID, sensitive_url));
arthursonzogni98e5a232017-07-13 15:18:16557 EXPECT_TRUE(p->CanRedirectToURL(icon_url));
558 EXPECT_TRUE(p->CanRedirectToURL(sensitive_url));
creis3710b2382015-08-18 00:12:15559 EXPECT_TRUE(p->CanCommitURL(kRendererID, icon_url));
560 EXPECT_FALSE(p->CanCommitURL(kRendererID, sensitive_url));
jww2cdad9e2016-09-24 05:42:02561 EXPECT_TRUE(p->CanSetAsOriginHeader(kRendererID, icon_url));
562 EXPECT_FALSE(p->CanSetAsOriginHeader(kRendererID, sensitive_url));
[email protected]dc67e1c32012-06-08 00:10:40563
564 p->GrantRequestURL(kRendererID, icon_url);
565 EXPECT_TRUE(p->CanRequestURL(kRendererID, icon_url));
566 EXPECT_TRUE(p->CanRequestURL(kRendererID, sensitive_url));
arthursonzogni98e5a232017-07-13 15:18:16567 EXPECT_TRUE(p->CanRedirectToURL(icon_url));
568 EXPECT_TRUE(p->CanRedirectToURL(sensitive_url));
creis3710b2382015-08-18 00:12:15569 EXPECT_TRUE(p->CanCommitURL(kRendererID, icon_url));
570 EXPECT_TRUE(p->CanCommitURL(kRendererID, sensitive_url));
jww2cdad9e2016-09-24 05:42:02571 EXPECT_TRUE(p->CanSetAsOriginHeader(kRendererID, icon_url));
572 EXPECT_TRUE(p->CanSetAsOriginHeader(kRendererID, sensitive_url));
[email protected]dc67e1c32012-06-08 00:10:40573
574 p->Remove(kRendererID);
575}
576
[email protected]b78c188fa62013-07-23 18:04:45577TEST_F(ChildProcessSecurityPolicyTest, FileSystemGrantsTest) {
578 ChildProcessSecurityPolicyImpl* p =
579 ChildProcessSecurityPolicyImpl::GetInstance();
580
581 p->Add(kRendererID);
[email protected]cd501a72014-08-22 19:58:31582 std::string read_id =
583 storage::IsolatedContext::GetInstance()->RegisterFileSystemForVirtualPath(
584 storage::kFileSystemTypeTest, "read_filesystem", base::FilePath());
585 std::string read_write_id =
586 storage::IsolatedContext::GetInstance()->RegisterFileSystemForVirtualPath(
587 storage::kFileSystemTypeTest,
588 "read_write_filesystem",
589 base::FilePath());
590 std::string copy_into_id =
591 storage::IsolatedContext::GetInstance()->RegisterFileSystemForVirtualPath(
592 storage::kFileSystemTypeTest,
593 "copy_into_filesystem",
594 base::FilePath());
595 std::string delete_from_id =
596 storage::IsolatedContext::GetInstance()->RegisterFileSystemForVirtualPath(
597 storage::kFileSystemTypeTest,
598 "delete_from_filesystem",
599 base::FilePath());
[email protected]b78c188fa62013-07-23 18:04:45600
601 // Test initially having no permissions.
[email protected]5a65fde32013-10-22 05:15:34602 CheckHasNoFileSystemPermission(p, read_id);
603 CheckHasNoFileSystemPermission(p, read_write_id);
604 CheckHasNoFileSystemPermission(p, copy_into_id);
605 CheckHasNoFileSystemPermission(p, delete_from_id);
[email protected]b78c188fa62013-07-23 18:04:45606
607 // Testing varying combinations of grants and checks.
608 p->GrantReadFileSystem(kRendererID, read_id);
609 EXPECT_TRUE(p->CanReadFileSystem(kRendererID, read_id));
610 EXPECT_FALSE(p->CanReadWriteFileSystem(kRendererID, read_id));
611 EXPECT_FALSE(p->CanCopyIntoFileSystem(kRendererID, read_id));
[email protected]5a65fde32013-10-22 05:15:34612 EXPECT_FALSE(p->CanDeleteFromFileSystem(kRendererID, read_id));
[email protected]b78c188fa62013-07-23 18:04:45613
614 p->GrantReadFileSystem(kRendererID, read_write_id);
615 p->GrantWriteFileSystem(kRendererID, read_write_id);
616 EXPECT_TRUE(p->CanReadFileSystem(kRendererID, read_write_id));
617 EXPECT_TRUE(p->CanReadWriteFileSystem(kRendererID, read_write_id));
618 EXPECT_FALSE(p->CanCopyIntoFileSystem(kRendererID, read_write_id));
[email protected]5a65fde32013-10-22 05:15:34619 EXPECT_FALSE(p->CanDeleteFromFileSystem(kRendererID, read_write_id));
[email protected]b78c188fa62013-07-23 18:04:45620
621 p->GrantCopyIntoFileSystem(kRendererID, copy_into_id);
622 EXPECT_FALSE(p->CanReadFileSystem(kRendererID, copy_into_id));
623 EXPECT_FALSE(p->CanReadWriteFileSystem(kRendererID, copy_into_id));
624 EXPECT_TRUE(p->CanCopyIntoFileSystem(kRendererID, copy_into_id));
[email protected]5a65fde32013-10-22 05:15:34625 EXPECT_FALSE(p->CanDeleteFromFileSystem(kRendererID, copy_into_id));
626
627 p->GrantDeleteFromFileSystem(kRendererID, delete_from_id);
628 EXPECT_FALSE(p->CanReadFileSystem(kRendererID, delete_from_id));
629 EXPECT_FALSE(p->CanReadWriteFileSystem(kRendererID, delete_from_id));
630 EXPECT_FALSE(p->CanCopyIntoFileSystem(kRendererID, delete_from_id));
631 EXPECT_TRUE(p->CanDeleteFromFileSystem(kRendererID, delete_from_id));
[email protected]b78c188fa62013-07-23 18:04:45632
633 // Test revoke permissions on renderer ID removal.
634 p->Remove(kRendererID);
[email protected]5a65fde32013-10-22 05:15:34635 CheckHasNoFileSystemPermission(p, read_id);
636 CheckHasNoFileSystemPermission(p, read_write_id);
637 CheckHasNoFileSystemPermission(p, copy_into_id);
638 CheckHasNoFileSystemPermission(p, delete_from_id);
[email protected]b78c188fa62013-07-23 18:04:45639
640 // Test having no permissions upon re-adding same renderer ID.
641 p->Add(kRendererID);
[email protected]5a65fde32013-10-22 05:15:34642 CheckHasNoFileSystemPermission(p, read_id);
643 CheckHasNoFileSystemPermission(p, read_write_id);
644 CheckHasNoFileSystemPermission(p, copy_into_id);
645 CheckHasNoFileSystemPermission(p, delete_from_id);
[email protected]b78c188fa62013-07-23 18:04:45646
647 // Cleanup.
648 p->Remove(kRendererID);
[email protected]cd501a72014-08-22 19:58:31649 storage::IsolatedContext::GetInstance()->RevokeFileSystem(read_id);
650 storage::IsolatedContext::GetInstance()->RevokeFileSystem(read_write_id);
651 storage::IsolatedContext::GetInstance()->RevokeFileSystem(copy_into_id);
652 storage::IsolatedContext::GetInstance()->RevokeFileSystem(delete_from_id);
[email protected]b78c188fa62013-07-23 18:04:45653}
654
[email protected]9f104312013-07-23 23:18:19655TEST_F(ChildProcessSecurityPolicyTest, FilePermissionGrantingAndRevoking) {
[email protected]b9535422012-02-09 01:47:59656 ChildProcessSecurityPolicyImpl* p =
657 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29658
[email protected]9f104312013-07-23 23:18:19659 p->RegisterFileSystemPermissionPolicy(
[email protected]cd501a72014-08-22 19:58:31660 storage::kFileSystemTypeTest,
661 storage::FILE_PERMISSION_USE_FILE_PERMISSION);
[email protected]9f104312013-07-23 23:18:19662
initial.commit09911bf2008-07-26 23:55:29663 p->Add(kRendererID);
[email protected]9f104312013-07-23 23:18:19664 base::FilePath file(TEST_PATH("/dir/testfile"));
665 file = file.NormalizePathSeparators();
[email protected]cd501a72014-08-22 19:58:31666 storage::FileSystemURL url = storage::FileSystemURL::CreateForTest(
667 GURL("http://foo/"), storage::kFileSystemTypeTest, file);
initial.commit09911bf2008-07-26 23:55:29668
[email protected]9f104312013-07-23 23:18:19669 // Test initially having no permissions.
[email protected]5a65fde32013-10-22 05:15:34670 CheckHasNoFileSystemFilePermission(p, file, url);
initial.commit09911bf2008-07-26 23:55:29671
[email protected]9f104312013-07-23 23:18:19672 // Testing every combination of permissions granting and revoking.
673 p->GrantReadFile(kRendererID, file);
674 EXPECT_TRUE(p->CanReadFile(kRendererID, file));
[email protected]d4c797f2013-09-26 08:18:53675 EXPECT_FALSE(p->CanCreateReadWriteFile(kRendererID, file));
[email protected]9f104312013-07-23 23:18:19676 EXPECT_TRUE(p->CanReadFileSystemFile(kRendererID, url));
677 EXPECT_FALSE(p->CanWriteFileSystemFile(kRendererID, url));
678 EXPECT_FALSE(p->CanCreateFileSystemFile(kRendererID, url));
[email protected]d4c797f2013-09-26 08:18:53679 EXPECT_FALSE(p->CanCreateReadWriteFileSystemFile(kRendererID, url));
[email protected]5a65fde32013-10-22 05:15:34680 EXPECT_FALSE(p->CanCopyIntoFileSystemFile(kRendererID, url));
681 EXPECT_FALSE(p->CanDeleteFileSystemFile(kRendererID, url));
[email protected]9f104312013-07-23 23:18:19682 p->RevokeAllPermissionsForFile(kRendererID, file);
[email protected]5a65fde32013-10-22 05:15:34683 CheckHasNoFileSystemFilePermission(p, file, url);
[email protected]9f104312013-07-23 23:18:19684
685 p->GrantCreateReadWriteFile(kRendererID, file);
686 EXPECT_TRUE(p->CanReadFile(kRendererID, file));
[email protected]d4c797f2013-09-26 08:18:53687 EXPECT_TRUE(p->CanCreateReadWriteFile(kRendererID, file));
[email protected]9f104312013-07-23 23:18:19688 EXPECT_TRUE(p->CanReadFileSystemFile(kRendererID, url));
689 EXPECT_TRUE(p->CanWriteFileSystemFile(kRendererID, url));
690 EXPECT_TRUE(p->CanCreateFileSystemFile(kRendererID, url));
[email protected]d4c797f2013-09-26 08:18:53691 EXPECT_TRUE(p->CanCreateReadWriteFileSystemFile(kRendererID, url));
[email protected]5a65fde32013-10-22 05:15:34692 EXPECT_TRUE(p->CanCopyIntoFileSystemFile(kRendererID, url));
693 EXPECT_TRUE(p->CanDeleteFileSystemFile(kRendererID, url));
[email protected]9f104312013-07-23 23:18:19694 p->RevokeAllPermissionsForFile(kRendererID, file);
[email protected]5a65fde32013-10-22 05:15:34695 CheckHasNoFileSystemFilePermission(p, file, url);
[email protected]9f104312013-07-23 23:18:19696
697 // Test revoke permissions on renderer ID removal.
698 p->GrantCreateReadWriteFile(kRendererID, file);
699 EXPECT_TRUE(p->CanReadFile(kRendererID, file));
[email protected]d4c797f2013-09-26 08:18:53700 EXPECT_TRUE(p->CanCreateReadWriteFile(kRendererID, file));
[email protected]9f104312013-07-23 23:18:19701 EXPECT_TRUE(p->CanReadFileSystemFile(kRendererID, url));
702 EXPECT_TRUE(p->CanWriteFileSystemFile(kRendererID, url));
703 EXPECT_TRUE(p->CanCreateFileSystemFile(kRendererID, url));
[email protected]d4c797f2013-09-26 08:18:53704 EXPECT_TRUE(p->CanCreateReadWriteFileSystemFile(kRendererID, url));
[email protected]5a65fde32013-10-22 05:15:34705 EXPECT_TRUE(p->CanCopyIntoFileSystemFile(kRendererID, url));
706 EXPECT_TRUE(p->CanDeleteFileSystemFile(kRendererID, url));
initial.commit09911bf2008-07-26 23:55:29707 p->Remove(kRendererID);
[email protected]5a65fde32013-10-22 05:15:34708 CheckHasNoFileSystemFilePermission(p, file, url);
[email protected]9f104312013-07-23 23:18:19709
710 // Test having no permissions upon re-adding same renderer ID.
initial.commit09911bf2008-07-26 23:55:29711 p->Add(kRendererID);
[email protected]5a65fde32013-10-22 05:15:34712 CheckHasNoFileSystemFilePermission(p, file, url);
initial.commit09911bf2008-07-26 23:55:29713
[email protected]9f104312013-07-23 23:18:19714 // Cleanup.
initial.commit09911bf2008-07-26 23:55:29715 p->Remove(kRendererID);
716}
717
[email protected]e54edc32010-09-28 01:09:19718TEST_F(ChildProcessSecurityPolicyTest, FilePermissions) {
[email protected]c42de732013-02-16 06:26:31719 base::FilePath granted_file = base::FilePath(TEST_PATH("/home/joe"));
720 base::FilePath sibling_file = base::FilePath(TEST_PATH("/home/bob"));
721 base::FilePath child_file = base::FilePath(TEST_PATH("/home/joe/file"));
722 base::FilePath parent_file = base::FilePath(TEST_PATH("/home"));
723 base::FilePath parent_slash_file = base::FilePath(TEST_PATH("/home/"));
724 base::FilePath child_traversal1 =
725 base::FilePath(TEST_PATH("/home/joe/././file"));
726 base::FilePath child_traversal2 = base::FilePath(
[email protected]f0ecca4522013-01-07 21:50:56727 TEST_PATH("/home/joe/file/../otherfile"));
[email protected]2dec8ec2013-02-07 19:20:34728 base::FilePath evil_traversal1 =
[email protected]023ad6ab2013-02-17 05:07:23729 base::FilePath(TEST_PATH("/home/joe/../../etc/passwd"));
[email protected]c42de732013-02-16 06:26:31730 base::FilePath evil_traversal2 = base::FilePath(
[email protected]f0ecca4522013-01-07 21:50:56731 TEST_PATH("/home/joe/./.././../etc/passwd"));
[email protected]c42de732013-02-16 06:26:31732 base::FilePath self_traversal =
733 base::FilePath(TEST_PATH("/home/joe/../joe/file"));
734 base::FilePath relative_file = base::FilePath(FILE_PATH_LITERAL("home/joe"));
[email protected]80838412012-11-20 01:53:59735
[email protected]b9535422012-02-09 01:47:59736 ChildProcessSecurityPolicyImpl* p =
737 ChildProcessSecurityPolicyImpl::GetInstance();
[email protected]e54edc32010-09-28 01:09:19738
739 // Grant permissions for a file.
740 p->Add(kRendererID);
[email protected]80838412012-11-20 01:53:59741 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41742 base::File::FLAG_OPEN));
[email protected]e54edc32010-09-28 01:09:19743
[email protected]bfcf1e92013-07-11 04:37:25744 GrantPermissionsForFile(p, kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41745 base::File::FLAG_OPEN |
746 base::File::FLAG_OPEN_TRUNCATED |
747 base::File::FLAG_READ |
748 base::File::FLAG_WRITE);
[email protected]80838412012-11-20 01:53:59749 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41750 base::File::FLAG_OPEN |
751 base::File::FLAG_OPEN_TRUNCATED |
752 base::File::FLAG_READ |
753 base::File::FLAG_WRITE));
[email protected]80838412012-11-20 01:53:59754 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41755 base::File::FLAG_OPEN |
756 base::File::FLAG_READ));
[email protected]80838412012-11-20 01:53:59757 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41758 base::File::FLAG_CREATE));
[email protected]f0ecca4522013-01-07 21:50:56759 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file, 0));
[email protected]80838412012-11-20 01:53:59760 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41761 base::File::FLAG_CREATE |
762 base::File::FLAG_OPEN_TRUNCATED |
763 base::File::FLAG_READ |
764 base::File::FLAG_WRITE));
[email protected]80838412012-11-20 01:53:59765 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, sibling_file,
[email protected]2c288ed2014-06-05 22:07:41766 base::File::FLAG_OPEN |
767 base::File::FLAG_READ));
[email protected]80838412012-11-20 01:53:59768 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, parent_file,
[email protected]2c288ed2014-06-05 22:07:41769 base::File::FLAG_OPEN |
770 base::File::FLAG_READ));
[email protected]80838412012-11-20 01:53:59771 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, child_file,
[email protected]2c288ed2014-06-05 22:07:41772 base::File::FLAG_OPEN |
773 base::File::FLAG_READ));
[email protected]80838412012-11-20 01:53:59774 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, child_traversal1,
[email protected]2c288ed2014-06-05 22:07:41775 base::File::FLAG_OPEN |
776 base::File::FLAG_READ));
[email protected]80838412012-11-20 01:53:59777 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, child_traversal2,
[email protected]2c288ed2014-06-05 22:07:41778 base::File::FLAG_OPEN |
779 base::File::FLAG_READ));
[email protected]80838412012-11-20 01:53:59780 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, evil_traversal1,
[email protected]2c288ed2014-06-05 22:07:41781 base::File::FLAG_OPEN |
782 base::File::FLAG_READ));
[email protected]80838412012-11-20 01:53:59783 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, evil_traversal2,
[email protected]2c288ed2014-06-05 22:07:41784 base::File::FLAG_OPEN |
785 base::File::FLAG_READ));
[email protected]80838412012-11-20 01:53:59786 // CPSP doesn't allow this case for the sake of simplicity.
787 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, self_traversal,
[email protected]2c288ed2014-06-05 22:07:41788 base::File::FLAG_OPEN |
789 base::File::FLAG_READ));
[email protected]e54edc32010-09-28 01:09:19790 p->Remove(kRendererID);
791
792 // Grant permissions for the directory the file is in.
793 p->Add(kRendererID);
[email protected]80838412012-11-20 01:53:59794 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41795 base::File::FLAG_OPEN));
[email protected]bfcf1e92013-07-11 04:37:25796 GrantPermissionsForFile(p, kRendererID, parent_file,
[email protected]2c288ed2014-06-05 22:07:41797 base::File::FLAG_OPEN |
798 base::File::FLAG_READ);
[email protected]80838412012-11-20 01:53:59799 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41800 base::File::FLAG_OPEN));
[email protected]80838412012-11-20 01:53:59801 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41802 base::File::FLAG_READ |
803 base::File::FLAG_WRITE));
[email protected]e54edc32010-09-28 01:09:19804 p->Remove(kRendererID);
805
806 // Grant permissions for the directory the file is in (with trailing '/').
807 p->Add(kRendererID);
[email protected]80838412012-11-20 01:53:59808 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41809 base::File::FLAG_OPEN));
[email protected]bfcf1e92013-07-11 04:37:25810 GrantPermissionsForFile(p, kRendererID, parent_slash_file,
[email protected]2c288ed2014-06-05 22:07:41811 base::File::FLAG_OPEN |
812 base::File::FLAG_READ);
[email protected]80838412012-11-20 01:53:59813 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41814 base::File::FLAG_OPEN));
[email protected]80838412012-11-20 01:53:59815 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41816 base::File::FLAG_READ |
817 base::File::FLAG_WRITE));
[email protected]e54edc32010-09-28 01:09:19818
819 // Grant permissions for the file (should overwrite the permissions granted
820 // for the directory).
[email protected]bfcf1e92013-07-11 04:37:25821 GrantPermissionsForFile(p, kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41822 base::File::FLAG_TEMPORARY);
[email protected]80838412012-11-20 01:53:59823 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41824 base::File::FLAG_OPEN));
[email protected]80838412012-11-20 01:53:59825 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41826 base::File::FLAG_TEMPORARY));
[email protected]77930fe2010-10-01 22:45:34827
828 // Revoke all permissions for the file (it should inherit its permissions
829 // from the directory again).
[email protected]80838412012-11-20 01:53:59830 p->RevokeAllPermissionsForFile(kRendererID, granted_file);
831 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41832 base::File::FLAG_OPEN |
833 base::File::FLAG_READ));
[email protected]80838412012-11-20 01:53:59834 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41835 base::File::FLAG_TEMPORARY));
[email protected]e54edc32010-09-28 01:09:19836 p->Remove(kRendererID);
[email protected]cee64fd32011-05-02 18:59:07837
838 // Grant file permissions for the file to main thread renderer process,
839 // make sure its worker thread renderer process inherits those.
840 p->Add(kRendererID);
[email protected]bfcf1e92013-07-11 04:37:25841 GrantPermissionsForFile(p, kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41842 base::File::FLAG_OPEN |
843 base::File::FLAG_READ);
[email protected]80838412012-11-20 01:53:59844 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41845 base::File::FLAG_OPEN |
846 base::File::FLAG_READ));
[email protected]80838412012-11-20 01:53:59847 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41848 base::File::FLAG_WRITE));
[email protected]cee64fd32011-05-02 18:59:07849 p->AddWorker(kWorkerRendererID, kRendererID);
[email protected]80838412012-11-20 01:53:59850 EXPECT_TRUE(p->HasPermissionsForFile(kWorkerRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41851 base::File::FLAG_OPEN |
852 base::File::FLAG_READ));
[email protected]80838412012-11-20 01:53:59853 EXPECT_FALSE(p->HasPermissionsForFile(kWorkerRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41854 base::File::FLAG_WRITE));
[email protected]cee64fd32011-05-02 18:59:07855 p->Remove(kRendererID);
[email protected]80838412012-11-20 01:53:59856 EXPECT_FALSE(p->HasPermissionsForFile(kWorkerRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41857 base::File::FLAG_OPEN |
858 base::File::FLAG_READ));
[email protected]cee64fd32011-05-02 18:59:07859 p->Remove(kWorkerRendererID);
[email protected]f0ecca4522013-01-07 21:50:56860
861 p->Add(kRendererID);
[email protected]bfcf1e92013-07-11 04:37:25862 GrantPermissionsForFile(p, kRendererID, relative_file,
[email protected]2c288ed2014-06-05 22:07:41863 base::File::FLAG_OPEN);
[email protected]f0ecca4522013-01-07 21:50:56864 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, relative_file,
[email protected]2c288ed2014-06-05 22:07:41865 base::File::FLAG_OPEN));
[email protected]f0ecca4522013-01-07 21:50:56866 p->Remove(kRendererID);
[email protected]e54edc32010-09-28 01:09:19867}
868
[email protected]c50008512011-02-03 01:17:27869TEST_F(ChildProcessSecurityPolicyTest, CanServiceWebUIBindings) {
[email protected]b9535422012-02-09 01:47:59870 ChildProcessSecurityPolicyImpl* p =
871 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29872
[email protected]60e448982009-05-06 04:21:16873 GURL url("chrome://thumb/http://www.google.com/");
initial.commit09911bf2008-07-26 23:55:29874
875 p->Add(kRendererID);
876
[email protected]c50008512011-02-03 01:17:27877 EXPECT_FALSE(p->HasWebUIBindings(kRendererID));
initial.commit09911bf2008-07-26 23:55:29878 EXPECT_FALSE(p->CanRequestURL(kRendererID, url));
arthursonzogni98e5a232017-07-13 15:18:16879 EXPECT_TRUE(p->CanRedirectToURL(url));
[email protected]c50008512011-02-03 01:17:27880 p->GrantWebUIBindings(kRendererID);
881 EXPECT_TRUE(p->HasWebUIBindings(kRendererID));
initial.commit09911bf2008-07-26 23:55:29882 EXPECT_TRUE(p->CanRequestURL(kRendererID, url));
arthursonzogni98e5a232017-07-13 15:18:16883 EXPECT_TRUE(p->CanRedirectToURL(url));
initial.commit09911bf2008-07-26 23:55:29884
885 p->Remove(kRendererID);
886}
887
[email protected]f58ddcf2009-05-18 22:22:06888TEST_F(ChildProcessSecurityPolicyTest, RemoveRace) {
[email protected]b9535422012-02-09 01:47:59889 ChildProcessSecurityPolicyImpl* p =
890 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29891
892 GURL url("file:///etc/passwd");
[email protected]2dec8ec2013-02-07 19:20:34893 base::FilePath file(TEST_PATH("/etc/passwd"));
initial.commit09911bf2008-07-26 23:55:29894
895 p->Add(kRendererID);
896
897 p->GrantRequestURL(kRendererID, url);
[email protected]e54edc32010-09-28 01:09:19898 p->GrantReadFile(kRendererID, file);
[email protected]c50008512011-02-03 01:17:27899 p->GrantWebUIBindings(kRendererID);
initial.commit09911bf2008-07-26 23:55:29900
901 EXPECT_TRUE(p->CanRequestURL(kRendererID, url));
arthursonzogni98e5a232017-07-13 15:18:16902 EXPECT_TRUE(p->CanRedirectToURL(url));
[email protected]e54edc32010-09-28 01:09:19903 EXPECT_TRUE(p->CanReadFile(kRendererID, file));
[email protected]c50008512011-02-03 01:17:27904 EXPECT_TRUE(p->HasWebUIBindings(kRendererID));
initial.commit09911bf2008-07-26 23:55:29905
906 p->Remove(kRendererID);
907
908 // Renderers are added and removed on the UI thread, but the policy can be
[email protected]580522632009-08-17 21:55:55909 // queried on the IO thread. The ChildProcessSecurityPolicy needs to be
910 // prepared to answer policy questions about renderers who no longer exist.
initial.commit09911bf2008-07-26 23:55:29911
912 // In this case, we default to secure behavior.
913 EXPECT_FALSE(p->CanRequestURL(kRendererID, url));
arthursonzogni98e5a232017-07-13 15:18:16914 EXPECT_TRUE(p->CanRedirectToURL(url));
[email protected]e54edc32010-09-28 01:09:19915 EXPECT_FALSE(p->CanReadFile(kRendererID, file));
[email protected]c50008512011-02-03 01:17:27916 EXPECT_FALSE(p->HasWebUIBindings(kRendererID));
initial.commit09911bf2008-07-26 23:55:29917}
[email protected]46488322012-10-30 03:22:20918
paulmeyer1eefa26e2015-10-01 02:11:13919// Test the granting of origin permissions, and their interactions with
920// granting scheme permissions.
921TEST_F(ChildProcessSecurityPolicyTest, OriginGranting) {
922 ChildProcessSecurityPolicyImpl* p =
923 ChildProcessSecurityPolicyImpl::GetInstance();
924
925 p->Add(kRendererID);
926
927 GURL url_foo1("chrome://foo/resource1");
928 GURL url_foo2("chrome://foo/resource2");
929 GURL url_bar("chrome://bar/resource3");
930
931 EXPECT_FALSE(p->CanRequestURL(kRendererID, url_foo1));
932 EXPECT_FALSE(p->CanRequestURL(kRendererID, url_foo2));
933 EXPECT_FALSE(p->CanRequestURL(kRendererID, url_bar));
arthursonzogni98e5a232017-07-13 15:18:16934 EXPECT_TRUE(p->CanRedirectToURL(url_foo1));
935 EXPECT_TRUE(p->CanRedirectToURL(url_foo2));
936 EXPECT_TRUE(p->CanRedirectToURL(url_bar));
paulmeyer1eefa26e2015-10-01 02:11:13937 EXPECT_FALSE(p->CanCommitURL(kRendererID, url_foo1));
938 EXPECT_FALSE(p->CanCommitURL(kRendererID, url_foo2));
939 EXPECT_FALSE(p->CanCommitURL(kRendererID, url_bar));
jww2cdad9e2016-09-24 05:42:02940 EXPECT_FALSE(p->CanSetAsOriginHeader(kRendererID, url_foo1));
941 EXPECT_FALSE(p->CanSetAsOriginHeader(kRendererID, url_foo2));
942 EXPECT_FALSE(p->CanSetAsOriginHeader(kRendererID, url_bar));
paulmeyer1eefa26e2015-10-01 02:11:13943
944 p->GrantOrigin(kRendererID, url::Origin(url_foo1));
945
946 EXPECT_TRUE(p->CanRequestURL(kRendererID, url_foo1));
947 EXPECT_TRUE(p->CanRequestURL(kRendererID, url_foo2));
948 EXPECT_FALSE(p->CanRequestURL(kRendererID, url_bar));
arthursonzogni98e5a232017-07-13 15:18:16949 EXPECT_TRUE(p->CanRedirectToURL(url_foo1));
950 EXPECT_TRUE(p->CanRedirectToURL(url_foo2));
951 EXPECT_TRUE(p->CanRedirectToURL(url_bar));
paulmeyer1eefa26e2015-10-01 02:11:13952 EXPECT_TRUE(p->CanCommitURL(kRendererID, url_foo1));
953 EXPECT_TRUE(p->CanCommitURL(kRendererID, url_foo2));
954 EXPECT_FALSE(p->CanCommitURL(kRendererID, url_bar));
jww2cdad9e2016-09-24 05:42:02955 EXPECT_TRUE(p->CanSetAsOriginHeader(kRendererID, url_foo1));
956 EXPECT_TRUE(p->CanSetAsOriginHeader(kRendererID, url_foo2));
957 EXPECT_FALSE(p->CanSetAsOriginHeader(kRendererID, url_bar));
paulmeyer1eefa26e2015-10-01 02:11:13958
959 p->GrantScheme(kRendererID, kChromeUIScheme);
960
961 EXPECT_TRUE(p->CanRequestURL(kRendererID, url_foo1));
962 EXPECT_TRUE(p->CanRequestURL(kRendererID, url_foo2));
963 EXPECT_TRUE(p->CanRequestURL(kRendererID, url_bar));
arthursonzogni98e5a232017-07-13 15:18:16964 EXPECT_TRUE(p->CanRedirectToURL(url_foo1));
965 EXPECT_TRUE(p->CanRedirectToURL(url_foo2));
966 EXPECT_TRUE(p->CanRedirectToURL(url_bar));
paulmeyer1eefa26e2015-10-01 02:11:13967 EXPECT_TRUE(p->CanCommitURL(kRendererID, url_foo1));
968 EXPECT_TRUE(p->CanCommitURL(kRendererID, url_foo2));
969 EXPECT_TRUE(p->CanCommitURL(kRendererID, url_bar));
jww2cdad9e2016-09-24 05:42:02970 EXPECT_TRUE(p->CanSetAsOriginHeader(kRendererID, url_foo1));
971 EXPECT_TRUE(p->CanSetAsOriginHeader(kRendererID, url_foo2));
972 EXPECT_TRUE(p->CanSetAsOriginHeader(kRendererID, url_bar));
paulmeyer1eefa26e2015-10-01 02:11:13973
974 p->Remove(kRendererID);
975}
976
alexmos3b9ad102017-05-26 23:41:08977// Verifies parsing logic that extracts origins from --isolate-origins.
978TEST_F(ChildProcessSecurityPolicyTest, IsolateOriginsFromCommandLine) {
979 // Invalid and unique origins are not permitted.
980 auto* policy = ChildProcessSecurityPolicyImpl::GetInstance();
981 policy->AddIsolatedOriginsFromCommandLine("foo");
982 policy->AddIsolatedOriginsFromCommandLine("");
983 policy->AddIsolatedOriginsFromCommandLine("about:blank");
984 EXPECT_EQ(0U, policy->isolated_origins_.size());
985
986 policy->AddIsolatedOriginsFromCommandLine("http://isolated.foo.com");
987 EXPECT_EQ(1U, policy->isolated_origins_.size());
988 EXPECT_TRUE(
989 policy->IsIsolatedOrigin(url::Origin(GURL("http://isolated.foo.com"))));
990
991 policy->AddIsolatedOriginsFromCommandLine(
992 "http://a.com,https://b.com,,https://c.com:8000");
993 EXPECT_EQ(4U, policy->isolated_origins_.size());
994 EXPECT_TRUE(
995 policy->IsIsolatedOrigin(url::Origin(GURL("http://isolated.foo.com"))));
996 EXPECT_TRUE(policy->IsIsolatedOrigin(url::Origin(GURL("http://a.com"))));
997 EXPECT_TRUE(policy->IsIsolatedOrigin(url::Origin(GURL("https://b.com"))));
998 EXPECT_TRUE(
999 policy->IsIsolatedOrigin(url::Origin(GURL("https://c.com:8000"))));
1000}
1001
[email protected]46488322012-10-30 03:22:201002} // namespace content