OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <set> | 5 #include <set> |
6 #include <string> | 6 #include <string> |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "content/browser/child_process_security_policy_impl.h" | 10 #include "content/browser/child_process_security_policy_impl.h" |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
148 | 148 |
149 EXPECT_FALSE(p->IsPseudoScheme(kChromeUIScheme)); | 149 EXPECT_FALSE(p->IsPseudoScheme(kChromeUIScheme)); |
150 } | 150 } |
151 | 151 |
152 TEST_F(ChildProcessSecurityPolicyTest, StandardSchemesTest) { | 152 TEST_F(ChildProcessSecurityPolicyTest, StandardSchemesTest) { |
153 ChildProcessSecurityPolicyImpl* p = | 153 ChildProcessSecurityPolicyImpl* p = |
154 ChildProcessSecurityPolicyImpl::GetInstance(); | 154 ChildProcessSecurityPolicyImpl::GetInstance(); |
155 | 155 |
156 p->Add(kRendererID); | 156 p->Add(kRendererID); |
157 | 157 |
158 // Safe | 158 // Safe to request or commit. |
159 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("http://www.google.com/"))); | 159 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("http://www.google.com/"))); |
160 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("https://www.paypal.com/"))); | 160 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("https://www.paypal.com/"))); |
161 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("ftp://ftp.gnu.org/"))); | 161 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("ftp://ftp.gnu.org/"))); |
162 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("data:text/html,<b>Hi</b>"))); | 162 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("data:text/html,<b>Hi</b>"))); |
163 EXPECT_TRUE(p->CanRequestURL( | |
164 kRendererID, GURL("filesystem:http://localhost/temporary/a.gif"))); | |
165 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"))); | |
171 | |
172 // Safe to request but not commit. | |
163 EXPECT_TRUE(p->CanRequestURL(kRendererID, | 173 EXPECT_TRUE(p->CanRequestURL(kRendererID, |
164 GURL("view-source:http://www.google.com/"))); | 174 GURL("view-source:http://www.google.com/"))); |
165 EXPECT_TRUE(p->CanRequestURL( | 175 EXPECT_FALSE(p->CanCommitURL(kRendererID, |
166 kRendererID, GURL("filesystem:http://localhost/temporary/a.gif"))); | 176 GURL("view-source:http://www.google.com/"))); |
167 | 177 |
168 // Dangerous | 178 // Dangerous to request or commit. |
169 EXPECT_FALSE(p->CanRequestURL(kRendererID, | 179 EXPECT_FALSE(p->CanRequestURL(kRendererID, |
170 GURL("file:///etc/passwd"))); | 180 GURL("file:///etc/passwd"))); |
171 EXPECT_FALSE(p->CanRequestURL(kRendererID, | 181 EXPECT_FALSE(p->CanRequestURL(kRendererID, |
172 GURL("chrome://foo/bar"))); | 182 GURL("chrome://foo/bar"))); |
183 EXPECT_FALSE(p->CanCommitURL(kRendererID, | |
184 GURL("file:///etc/passwd"))); | |
185 EXPECT_FALSE(p->CanCommitURL(kRendererID, | |
186 GURL("chrome://foo/bar"))); | |
173 | 187 |
174 p->Remove(kRendererID); | 188 p->Remove(kRendererID); |
175 } | 189 } |
176 | 190 |
177 TEST_F(ChildProcessSecurityPolicyTest, AboutTest) { | 191 TEST_F(ChildProcessSecurityPolicyTest, AboutTest) { |
178 ChildProcessSecurityPolicyImpl* p = | 192 ChildProcessSecurityPolicyImpl* p = |
179 ChildProcessSecurityPolicyImpl::GetInstance(); | 193 ChildProcessSecurityPolicyImpl::GetInstance(); |
180 | 194 |
181 p->Add(kRendererID); | 195 p->Add(kRendererID); |
182 | 196 |
183 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("about:blank"))); | 197 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("about:blank"))); |
184 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("about:BlAnK"))); | 198 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("about:BlAnK"))); |
185 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("aBouT:BlAnK"))); | 199 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("aBouT:BlAnK"))); |
186 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("aBouT:blank"))); | 200 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("aBouT:blank"))); |
201 EXPECT_TRUE(p->CanCommitURL(kRendererID, GURL("about:blank"))); | |
202 EXPECT_TRUE(p->CanCommitURL(kRendererID, GURL("about:BlAnK"))); | |
nasko
2015/08/14 22:14:43
Why not add a test case with capitalized letters i
Charlie Reis
2015/08/14 23:23:32
Done.
| |
187 | 203 |
188 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:memory"))); | 204 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:memory"))); |
189 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:crash"))); | 205 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:crash"))); |
190 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:cache"))); | 206 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:cache"))); |
191 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:hang"))); | 207 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:hang"))); |
208 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("about:memory"))); | |
209 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("about:crash"))); | |
210 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("about:cache"))); | |
211 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("about:hang"))); | |
192 | 212 |
193 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("aBoUt:memory"))); | 213 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("aBoUt:memory"))); |
194 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:CrASh"))); | 214 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:CrASh"))); |
195 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("abOuT:cAChe"))); | 215 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("abOuT:cAChe"))); |
216 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("aBoUt:memory"))); | |
217 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("about:CrASh"))); | |
218 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("abOuT:cAChe"))); | |
196 | 219 |
197 // Requests for about: pages should be denied. | 220 // Requests for about: pages should be denied. |
198 p->GrantRequestURL(kRendererID, GURL("about:crash")); | 221 p->GrantRequestURL(kRendererID, GURL("about:crash")); |
199 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:crash"))); | 222 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:crash"))); |
223 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("about:crash"))); | |
200 | 224 |
201 // These requests for chrome:// pages should be granted. | 225 // These requests for chrome:// pages should be granted. |
202 GURL chrome_url("chrome://foo"); | 226 GURL chrome_url("chrome://foo"); |
203 p->GrantRequestURL(kRendererID, chrome_url); | 227 p->GrantRequestURL(kRendererID, chrome_url); |
204 EXPECT_TRUE(p->CanRequestURL(kRendererID, chrome_url)); | 228 EXPECT_TRUE(p->CanRequestURL(kRendererID, chrome_url)); |
229 EXPECT_TRUE(p->CanCommitURL(kRendererID, chrome_url)); | |
205 | 230 |
206 p->Remove(kRendererID); | 231 p->Remove(kRendererID); |
207 } | 232 } |
208 | 233 |
209 TEST_F(ChildProcessSecurityPolicyTest, JavaScriptTest) { | 234 TEST_F(ChildProcessSecurityPolicyTest, JavaScriptTest) { |
210 ChildProcessSecurityPolicyImpl* p = | 235 ChildProcessSecurityPolicyImpl* p = |
211 ChildProcessSecurityPolicyImpl::GetInstance(); | 236 ChildProcessSecurityPolicyImpl::GetInstance(); |
212 | 237 |
213 p->Add(kRendererID); | 238 p->Add(kRendererID); |
214 | 239 |
215 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("javascript:alert('xss')"))); | 240 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("javascript:alert('xss')"))); |
241 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("javascript:alert('xss')"))); | |
216 p->GrantRequestURL(kRendererID, GURL("javascript:alert('xss')")); | 242 p->GrantRequestURL(kRendererID, GURL("javascript:alert('xss')")); |
217 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("javascript:alert('xss')"))); | 243 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("javascript:alert('xss')"))); |
244 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("javascript:alert('xss')"))); | |
218 | 245 |
219 p->Remove(kRendererID); | 246 p->Remove(kRendererID); |
220 } | 247 } |
221 | 248 |
222 TEST_F(ChildProcessSecurityPolicyTest, RegisterWebSafeSchemeTest) { | 249 TEST_F(ChildProcessSecurityPolicyTest, RegisterWebSafeSchemeTest) { |
223 ChildProcessSecurityPolicyImpl* p = | 250 ChildProcessSecurityPolicyImpl* p = |
224 ChildProcessSecurityPolicyImpl::GetInstance(); | 251 ChildProcessSecurityPolicyImpl::GetInstance(); |
225 | 252 |
226 p->Add(kRendererID); | 253 p->Add(kRendererID); |
227 | 254 |
228 // Currently, "asdf" is destined for ShellExecute, so it is allowed. | 255 // Currently, "asdf" is destined for ShellExecute, so it is allowed to be |
256 // requested but not committed. | |
229 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("asdf:rockers"))); | 257 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("asdf:rockers"))); |
258 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("asdf:rockers"))); | |
230 | 259 |
231 // Once we register "asdf", we default to deny. | 260 // Once we register "asdf", we default to deny. |
232 RegisterTestScheme("asdf"); | 261 RegisterTestScheme("asdf"); |
233 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("asdf:rockers"))); | 262 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("asdf:rockers"))); |
263 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("asdf:rockers"))); | |
234 | 264 |
235 // We can allow new schemes by adding them to the whitelist. | 265 // We can allow new schemes by adding them to the whitelist. |
236 p->RegisterWebSafeScheme("asdf"); | 266 p->RegisterWebSafeScheme("asdf"); |
237 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("asdf:rockers"))); | 267 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("asdf:rockers"))); |
268 EXPECT_TRUE(p->CanCommitURL(kRendererID, GURL("asdf:rockers"))); | |
238 | 269 |
239 // Cleanup. | 270 // Cleanup. |
240 p->Remove(kRendererID); | 271 p->Remove(kRendererID); |
241 } | 272 } |
242 | 273 |
243 TEST_F(ChildProcessSecurityPolicyTest, CanServiceCommandsTest) { | 274 TEST_F(ChildProcessSecurityPolicyTest, CanServiceCommandsTest) { |
244 ChildProcessSecurityPolicyImpl* p = | 275 ChildProcessSecurityPolicyImpl* p = |
245 ChildProcessSecurityPolicyImpl::GetInstance(); | 276 ChildProcessSecurityPolicyImpl::GetInstance(); |
246 | 277 |
247 p->Add(kRendererID); | 278 p->Add(kRendererID); |
248 | 279 |
249 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd"))); | 280 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd"))); |
281 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("file:///etc/passwd"))); | |
250 p->GrantRequestURL(kRendererID, GURL("file:///etc/passwd")); | 282 p->GrantRequestURL(kRendererID, GURL("file:///etc/passwd")); |
251 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd"))); | 283 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd"))); |
284 EXPECT_TRUE(p->CanCommitURL(kRendererID, GURL("file:///etc/passwd"))); | |
252 | 285 |
253 // We should forget our state if we repeat a renderer id. | 286 // We should forget our state if we repeat a renderer id. |
254 p->Remove(kRendererID); | 287 p->Remove(kRendererID); |
255 p->Add(kRendererID); | 288 p->Add(kRendererID); |
256 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd"))); | 289 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd"))); |
290 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("file:///etc/passwd"))); | |
257 p->Remove(kRendererID); | 291 p->Remove(kRendererID); |
258 } | 292 } |
259 | 293 |
260 TEST_F(ChildProcessSecurityPolicyTest, ViewSource) { | 294 TEST_F(ChildProcessSecurityPolicyTest, ViewSource) { |
261 ChildProcessSecurityPolicyImpl* p = | 295 ChildProcessSecurityPolicyImpl* p = |
262 ChildProcessSecurityPolicyImpl::GetInstance(); | 296 ChildProcessSecurityPolicyImpl::GetInstance(); |
263 | 297 |
264 p->Add(kRendererID); | 298 p->Add(kRendererID); |
265 | 299 |
266 // View source is determined by the embedded scheme. | 300 // View source is determined by the embedded scheme. |
267 EXPECT_TRUE(p->CanRequestURL(kRendererID, | 301 EXPECT_TRUE(p->CanRequestURL(kRendererID, |
268 GURL("view-source:http://www.google.com/"))); | 302 GURL("view-source:http://www.google.com/"))); |
269 EXPECT_FALSE(p->CanRequestURL(kRendererID, | 303 EXPECT_FALSE(p->CanRequestURL(kRendererID, |
270 GURL("view-source:file:///etc/passwd"))); | 304 GURL("view-source:file:///etc/passwd"))); |
271 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd"))); | 305 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd"))); |
272 EXPECT_FALSE(p->CanRequestURL( | 306 EXPECT_FALSE(p->CanRequestURL( |
273 kRendererID, GURL("view-source:view-source:http://www.google.com/"))); | 307 kRendererID, GURL("view-source:view-source:http://www.google.com/"))); |
274 | 308 |
309 // View source URLs don't actually commit; the renderer is put into view | |
310 // source mode, and the inner URL commits. | |
311 EXPECT_FALSE(p->CanCommitURL(kRendererID, | |
312 GURL("view-source:http://www.google.com/"))); | |
313 EXPECT_FALSE(p->CanCommitURL(kRendererID, | |
314 GURL("view-source:file:///etc/passwd"))); | |
315 EXPECT_FALSE(p->CanCommitURL(kRendererID, GURL("file:///etc/passwd"))); | |
316 EXPECT_FALSE(p->CanCommitURL( | |
317 kRendererID, GURL("view-source:view-source:http://www.google.com/"))); | |
318 | |
319 | |
275 p->GrantRequestURL(kRendererID, GURL("view-source:file:///etc/passwd")); | 320 p->GrantRequestURL(kRendererID, GURL("view-source:file:///etc/passwd")); |
276 // View source needs to be able to request the embedded scheme. | 321 // View source needs to be able to request the embedded scheme. |
322 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd"))); | |
323 EXPECT_TRUE(p->CanCommitURL(kRendererID, GURL("file:///etc/passwd"))); | |
277 EXPECT_TRUE(p->CanRequestURL(kRendererID, | 324 EXPECT_TRUE(p->CanRequestURL(kRendererID, |
278 GURL("view-source:file:///etc/passwd"))); | 325 GURL("view-source:file:///etc/passwd"))); |
279 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd"))); | 326 EXPECT_FALSE(p->CanCommitURL(kRendererID, |
327 GURL("view-source:file:///etc/passwd"))); | |
280 | 328 |
281 p->Remove(kRendererID); | 329 p->Remove(kRendererID); |
282 } | 330 } |
283 | 331 |
284 TEST_F(ChildProcessSecurityPolicyTest, SpecificFile) { | 332 TEST_F(ChildProcessSecurityPolicyTest, SpecificFile) { |
285 ChildProcessSecurityPolicyImpl* p = | 333 ChildProcessSecurityPolicyImpl* p = |
286 ChildProcessSecurityPolicyImpl::GetInstance(); | 334 ChildProcessSecurityPolicyImpl::GetInstance(); |
287 | 335 |
288 p->Add(kRendererID); | 336 p->Add(kRendererID); |
289 | 337 |
290 GURL icon_url("file:///tmp/foo.png"); | 338 GURL icon_url("file:///tmp/foo.png"); |
291 GURL sensitive_url("file:///etc/passwd"); | 339 GURL sensitive_url("file:///etc/passwd"); |
292 EXPECT_FALSE(p->CanRequestURL(kRendererID, icon_url)); | 340 EXPECT_FALSE(p->CanRequestURL(kRendererID, icon_url)); |
293 EXPECT_FALSE(p->CanRequestURL(kRendererID, sensitive_url)); | 341 EXPECT_FALSE(p->CanRequestURL(kRendererID, sensitive_url)); |
342 EXPECT_FALSE(p->CanCommitURL(kRendererID, icon_url)); | |
343 EXPECT_FALSE(p->CanCommitURL(kRendererID, sensitive_url)); | |
294 | 344 |
295 p->GrantRequestSpecificFileURL(kRendererID, icon_url); | 345 p->GrantRequestSpecificFileURL(kRendererID, icon_url); |
296 EXPECT_TRUE(p->CanRequestURL(kRendererID, icon_url)); | 346 EXPECT_TRUE(p->CanRequestURL(kRendererID, icon_url)); |
297 EXPECT_FALSE(p->CanRequestURL(kRendererID, sensitive_url)); | 347 EXPECT_FALSE(p->CanRequestURL(kRendererID, sensitive_url)); |
348 EXPECT_TRUE(p->CanCommitURL(kRendererID, icon_url)); | |
349 EXPECT_FALSE(p->CanCommitURL(kRendererID, sensitive_url)); | |
298 | 350 |
299 p->GrantRequestURL(kRendererID, icon_url); | 351 p->GrantRequestURL(kRendererID, icon_url); |
300 EXPECT_TRUE(p->CanRequestURL(kRendererID, icon_url)); | 352 EXPECT_TRUE(p->CanRequestURL(kRendererID, icon_url)); |
301 EXPECT_TRUE(p->CanRequestURL(kRendererID, sensitive_url)); | 353 EXPECT_TRUE(p->CanRequestURL(kRendererID, sensitive_url)); |
354 EXPECT_TRUE(p->CanCommitURL(kRendererID, icon_url)); | |
355 EXPECT_TRUE(p->CanCommitURL(kRendererID, sensitive_url)); | |
302 | 356 |
303 p->Remove(kRendererID); | 357 p->Remove(kRendererID); |
304 } | 358 } |
305 | 359 |
306 TEST_F(ChildProcessSecurityPolicyTest, FileSystemGrantsTest) { | 360 TEST_F(ChildProcessSecurityPolicyTest, FileSystemGrantsTest) { |
307 ChildProcessSecurityPolicyImpl* p = | 361 ChildProcessSecurityPolicyImpl* p = |
308 ChildProcessSecurityPolicyImpl::GetInstance(); | 362 ChildProcessSecurityPolicyImpl::GetInstance(); |
309 | 363 |
310 p->Add(kRendererID); | 364 p->Add(kRendererID); |
311 std::string read_id = | 365 std::string read_id = |
(...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
635 // queried on the IO thread. The ChildProcessSecurityPolicy needs to be | 689 // queried on the IO thread. The ChildProcessSecurityPolicy needs to be |
636 // prepared to answer policy questions about renderers who no longer exist. | 690 // prepared to answer policy questions about renderers who no longer exist. |
637 | 691 |
638 // In this case, we default to secure behavior. | 692 // In this case, we default to secure behavior. |
639 EXPECT_FALSE(p->CanRequestURL(kRendererID, url)); | 693 EXPECT_FALSE(p->CanRequestURL(kRendererID, url)); |
640 EXPECT_FALSE(p->CanReadFile(kRendererID, file)); | 694 EXPECT_FALSE(p->CanReadFile(kRendererID, file)); |
641 EXPECT_FALSE(p->HasWebUIBindings(kRendererID)); | 695 EXPECT_FALSE(p->HasWebUIBindings(kRendererID)); |
642 } | 696 } |
643 | 697 |
644 } // namespace content | 698 } // namespace content |
OLD | NEW |