Chromium Code Reviews| Index: net/url_request/url_request_unittest.cc |
| diff --git a/net/url_request/url_request_unittest.cc b/net/url_request/url_request_unittest.cc |
| index 25b603b268beb6504e9f5974a0a0d170824bbfd3..072a47e5dc841411fa6e259a7075daaa82bb732e 100644 |
| --- a/net/url_request/url_request_unittest.cc |
| +++ b/net/url_request/url_request_unittest.cc |
| @@ -840,6 +840,25 @@ class URLRequestTest : public PlatformTest { |
| TestURLRequestContext default_context_; |
| }; |
| +// This NetworkDelegate is picky about what files are accessible. Only |
| +// whitelisted files are allowed. |
| +class PickyNetworkDelegate : public TestNetworkDelegate { |
| + public: |
| + // Adds |path| to the access white list. |
| + void AddToWhitelist(const base::FilePath& path) { |
| + whitelist_.insert(base::MakeAbsoluteFilePath(path)); |
| + } |
| + |
| + bool OnCanAccessFile(const URLRequest& request, |
| + const base::FilePath& original_path, |
| + const base::FilePath& absolute_path) const override { |
| + return whitelist_.count(absolute_path) > 0; |
| + } |
| + |
| + private: |
| + std::set<base::FilePath> whitelist_; |
| +}; |
| + |
| TEST_F(URLRequestTest, AboutBlankTest) { |
| TestDelegate d; |
| { |
| @@ -1093,34 +1112,129 @@ TEST_F(URLRequestTest, AllowFileURLs) { |
| std::string test_data("monkey"); |
| base::WriteFile(test_file, test_data.data(), test_data.size()); |
| GURL test_file_url = FilePathToFileURL(test_file); |
| - |
| { |
| TestDelegate d; |
| - TestNetworkDelegate network_delegate; |
| - network_delegate.set_can_access_files(true); |
| + PickyNetworkDelegate network_delegate; |
| + network_delegate.AddToWhitelist(test_file); |
| default_context_.set_network_delegate(&network_delegate); |
| std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| test_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| r->Start(); |
| base::RunLoop().Run(); |
| + // This should be allowed as the file path is white listed. |
| EXPECT_FALSE(d.request_failed()); |
| EXPECT_EQ(test_data, d.data_received()); |
| } |
| { |
| TestDelegate d; |
| - TestNetworkDelegate network_delegate; |
| - network_delegate.set_can_access_files(false); |
| + PickyNetworkDelegate network_delegate; |
| default_context_.set_network_delegate(&network_delegate); |
| std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| test_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| r->Start(); |
| base::RunLoop().Run(); |
| + // This should be rejected as the file path is not white listed. |
| + EXPECT_TRUE(d.request_failed()); |
| + EXPECT_EQ("", d.data_received()); |
| + } |
| +} |
| + |
| +#if defined(OS_POSIX) // Bacause of symbolic links. |
| + |
| +TEST_F(URLRequestTest, SymlinksToFiles) { |
| + base::ScopedTempDir temp_dir; |
| + ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| + // The test file be added to the whitelist. |
| + base::FilePath test_file; |
| + ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.GetPath(), &test_file)); |
| + std::string test_data("monkey"); |
| + base::WriteFile(test_file, test_data.data(), test_data.size()); |
| + |
| + // This_symlink will point to the test file. |
| + base::FilePath good_symlink = temp_dir.GetPath().AppendASCII("good_symlink"); |
| + ASSERT_TRUE(base::CreateSymbolicLink(test_file, good_symlink)); |
| + GURL good_file_url = FilePathToFileURL(good_symlink); |
| + // This_symlink will point to /dev/null. |
| + base::FilePath bad_symlink = temp_dir.GetPath().AppendASCII("bad_symlink"); |
| + ASSERT_TRUE(base::CreateSymbolicLink( |
| + base::FilePath(FILE_PATH_LITERAL("/dev/null")), bad_symlink)); |
| + GURL bad_file_url = FilePathToFileURL(bad_symlink); |
| + |
| + { |
| + TestDelegate d; |
| + PickyNetworkDelegate network_delegate; |
| + network_delegate.AddToWhitelist(test_file); |
| + default_context_.set_network_delegate(&network_delegate); |
| + std::unique_ptr<URLRequest> r( |
| + default_context_.CreateRequest(good_file_url, DEFAULT_PRIORITY, &d)); |
| + r->Start(); |
| + base::RunLoop().Run(); |
| + // good_file_url should be allowed. |
| + EXPECT_FALSE(d.request_failed()); |
| + EXPECT_EQ(test_data, d.data_received()); |
| + } |
| + |
| + { |
| + TestDelegate d; |
| + PickyNetworkDelegate network_delegate; |
| + network_delegate.AddToWhitelist(test_file); |
| + default_context_.set_network_delegate(&network_delegate); |
| + std::unique_ptr<URLRequest> r( |
| + default_context_.CreateRequest(bad_file_url, DEFAULT_PRIORITY, &d)); |
| + r->Start(); |
| + base::RunLoop().Run(); |
| + // bad_file_url should be rejected. |
| EXPECT_TRUE(d.request_failed()); |
| EXPECT_EQ("", d.data_received()); |
|
mmenke
2017/05/10 18:03:31
For the failure cases, can you check that d.reques
satorux1
2017/05/12 13:24:11
Good idea! Done.
|
| } |
| } |
| +TEST_F(URLRequestTest, SymlinksToDirs) { |
| + // The temporary dir will be added to the whitelist. |
| + base::ScopedTempDir temp_dir; |
| + ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| + |
| + // This_symlink will point to the temp dir. |
| + base::FilePath good_symlink = temp_dir.GetPath().AppendASCII("good_symlink"); |
| + ASSERT_TRUE(base::CreateSymbolicLink(temp_dir.GetPath(), good_symlink)); |
| + GURL good_file_url = FilePathToFileURL(good_symlink); |
| + // This_symlink will point to /dev. |
| + base::FilePath bad_symlink = temp_dir.GetPath().AppendASCII("bad_symlink"); |
| + ASSERT_TRUE(base::CreateSymbolicLink( |
| + base::FilePath(FILE_PATH_LITERAL("/dev")), bad_symlink)); |
| + GURL bad_file_url = FilePathToFileURL(bad_symlink); |
| + |
| + { |
| + TestDelegate d; |
| + PickyNetworkDelegate network_delegate; |
| + network_delegate.AddToWhitelist(temp_dir.GetPath()); |
| + default_context_.set_network_delegate(&network_delegate); |
| + std::unique_ptr<URLRequest> r( |
| + default_context_.CreateRequest(good_file_url, DEFAULT_PRIORITY, &d)); |
| + r->Start(); |
| + base::RunLoop().Run(); |
| + // good_file_url should be allowed. |
| + EXPECT_FALSE(d.request_failed()); |
| + ASSERT_NE(d.data_received().find("good_symlink"), std::string::npos); |
| + } |
| + |
| + { |
| + TestDelegate d; |
| + PickyNetworkDelegate network_delegate; |
| + network_delegate.AddToWhitelist(temp_dir.GetPath()); |
| + default_context_.set_network_delegate(&network_delegate); |
| + std::unique_ptr<URLRequest> r( |
| + default_context_.CreateRequest(bad_file_url, DEFAULT_PRIORITY, &d)); |
| + r->Start(); |
| + base::RunLoop().Run(); |
| + // bad_file_url should be rejected. |
| + EXPECT_TRUE(d.request_failed()); |
| + EXPECT_EQ("", d.data_received()); |
| + } |
| +} |
| + |
| +#endif // defined(OS_POSIX) |
| TEST_F(URLRequestTest, FileDirCancelTest) { |
| // Put in mock resource provider. |
| @@ -1172,9 +1286,8 @@ TEST_F(URLRequestTest, FileDirOutputSanity) { |
| EXPECT_GT(info.size, 0); |
| std::string sentinel_output = GetDirectoryListingEntry( |
| base::string16(sentinel_name, sentinel_name + strlen(sentinel_name)), |
| - std::string(sentinel_name), |
| - false /* is_dir */, |
| - info.size, |
| + std::string(sentinel_name), false /* is_dir */, info.size, |
| + |
| info.last_modified); |
| ASSERT_LT(0, d.bytes_received()); |