Chromium Code Reviews| 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 <memory> | 5 #include <memory> |
| 6 #include <utility> | 6 #include <utility> |
| 7 | 7 |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| (...skipping 816 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 827 } | 827 } |
| 828 | 828 |
| 829 protected: | 829 protected: |
| 830 TestNetLog net_log_; | 830 TestNetLog net_log_; |
| 831 TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. | 831 TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
| 832 URLRequestJobFactoryImpl* job_factory_impl_; | 832 URLRequestJobFactoryImpl* job_factory_impl_; |
| 833 std::unique_ptr<URLRequestJobFactory> job_factory_; | 833 std::unique_ptr<URLRequestJobFactory> job_factory_; |
| 834 TestURLRequestContext default_context_; | 834 TestURLRequestContext default_context_; |
| 835 }; | 835 }; |
| 836 | 836 |
| 837 // This NetworkDelegate is picky about what files are accessible. Only | |
| 838 // whitelisted files are allowed. | |
| 839 class PickyNetworkDelegate : public TestNetworkDelegate { | |
|
mmenke
2017/05/15 19:21:14
nit: Maybe CookieBlockingNetworkDelegate?
satorux1
2017/05/19 07:02:53
Done.
| |
| 840 public: | |
| 841 // Adds |directory| to the access white list. | |
| 842 void AddToWhitelist(const base::FilePath& directory) { | |
| 843 whitelist_.insert(directory); | |
| 844 } | |
| 845 | |
| 846 // Returns true if |path| matches the white list. | |
| 847 bool OnCanAccessFileInternal(const base::FilePath& path) const { | |
| 848 for (const auto& directory : whitelist_) { | |
| 849 if (directory == path || directory.IsParent(path)) | |
| 850 return true; | |
| 851 } | |
| 852 return false; | |
| 853 } | |
| 854 | |
| 855 // Returns true only if both |original_path| and |absolute_path| match the | |
| 856 // white list. | |
| 857 bool OnCanAccessFile(const URLRequest& request, | |
| 858 const base::FilePath& original_path, | |
| 859 const base::FilePath& absolute_path) const override { | |
| 860 return (OnCanAccessFileInternal(original_path) && | |
| 861 OnCanAccessFileInternal(absolute_path)); | |
| 862 } | |
| 863 | |
| 864 private: | |
| 865 std::set<base::FilePath> whitelist_; | |
|
mmenke
2017/05/15 19:21:14
nit: DISALLOW_COPY_AND_ASSIGN
satorux1
2017/05/19 07:02:53
Done.
| |
| 866 }; | |
| 867 | |
| 837 TEST_F(URLRequestTest, AboutBlankTest) { | 868 TEST_F(URLRequestTest, AboutBlankTest) { |
| 838 TestDelegate d; | 869 TestDelegate d; |
| 839 { | 870 { |
| 840 std::unique_ptr<URLRequest> r( | 871 std::unique_ptr<URLRequest> r( |
| 841 default_context_.CreateRequest(GURL("about:blank"), DEFAULT_PRIORITY, | 872 default_context_.CreateRequest(GURL("about:blank"), DEFAULT_PRIORITY, |
| 842 &d, TRAFFIC_ANNOTATION_FOR_TESTS)); | 873 &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 843 | 874 |
| 844 r->Start(); | 875 r->Start(); |
| 845 EXPECT_TRUE(r->is_pending()); | 876 EXPECT_TRUE(r->is_pending()); |
| 846 | 877 |
| (...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1075 base::RunLoop().Run(); | 1106 base::RunLoop().Run(); |
| 1076 EXPECT_TRUE(d.request_failed()); | 1107 EXPECT_TRUE(d.request_failed()); |
| 1077 } | 1108 } |
| 1078 | 1109 |
| 1079 EXPECT_TRUE(base::DeleteFile(temp_path, false)); | 1110 EXPECT_TRUE(base::DeleteFile(temp_path, false)); |
| 1080 } | 1111 } |
| 1081 | 1112 |
| 1082 TEST_F(URLRequestTest, AllowFileURLs) { | 1113 TEST_F(URLRequestTest, AllowFileURLs) { |
| 1083 base::ScopedTempDir temp_dir; | 1114 base::ScopedTempDir temp_dir; |
| 1084 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 1115 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 1116 // Get an absolute path since the path can contain a symbolic link. | |
| 1117 base::FilePath absolute_temp_dir = | |
| 1118 base::MakeAbsoluteFilePath(temp_dir.GetPath()); | |
| 1085 base::FilePath test_file; | 1119 base::FilePath test_file; |
| 1086 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.GetPath(), &test_file)); | 1120 ASSERT_TRUE(base::CreateTemporaryFileInDir(absolute_temp_dir, &test_file)); |
| 1087 std::string test_data("monkey"); | 1121 std::string test_data("monkey"); |
| 1088 base::WriteFile(test_file, test_data.data(), test_data.size()); | 1122 base::WriteFile(test_file, test_data.data(), test_data.size()); |
| 1089 GURL test_file_url = FilePathToFileURL(test_file); | 1123 GURL test_file_url = FilePathToFileURL(test_file); |
| 1090 | |
| 1091 { | 1124 { |
| 1092 TestDelegate d; | 1125 TestDelegate d; |
| 1093 TestNetworkDelegate network_delegate; | 1126 PickyNetworkDelegate network_delegate; |
| 1094 network_delegate.set_can_access_files(true); | 1127 network_delegate.AddToWhitelist(absolute_temp_dir); |
| 1095 default_context_.set_network_delegate(&network_delegate); | 1128 default_context_.set_network_delegate(&network_delegate); |
| 1096 std::unique_ptr<URLRequest> r(default_context_.CreateRequest( | 1129 std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1097 test_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); | 1130 test_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 1098 r->Start(); | 1131 r->Start(); |
| 1099 base::RunLoop().Run(); | 1132 base::RunLoop().Run(); |
| 1133 // This should be allowed as the file path is white listed. | |
| 1100 EXPECT_FALSE(d.request_failed()); | 1134 EXPECT_FALSE(d.request_failed()); |
| 1101 EXPECT_EQ(test_data, d.data_received()); | 1135 EXPECT_EQ(test_data, d.data_received()); |
| 1102 } | 1136 } |
| 1103 | 1137 |
| 1104 { | 1138 { |
| 1105 TestDelegate d; | 1139 TestDelegate d; |
| 1106 TestNetworkDelegate network_delegate; | 1140 PickyNetworkDelegate network_delegate; |
| 1107 network_delegate.set_can_access_files(false); | |
| 1108 default_context_.set_network_delegate(&network_delegate); | 1141 default_context_.set_network_delegate(&network_delegate); |
| 1109 std::unique_ptr<URLRequest> r(default_context_.CreateRequest( | 1142 std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1110 test_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); | 1143 test_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 1111 r->Start(); | 1144 r->Start(); |
| 1112 base::RunLoop().Run(); | 1145 base::RunLoop().Run(); |
| 1146 // This should be rejected as the file path is not white listed. | |
| 1113 EXPECT_TRUE(d.request_failed()); | 1147 EXPECT_TRUE(d.request_failed()); |
| 1114 EXPECT_EQ("", d.data_received()); | 1148 EXPECT_EQ("", d.data_received()); |
| 1149 EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status()); | |
| 1115 } | 1150 } |
| 1116 } | 1151 } |
| 1117 | 1152 |
| 1153 #if defined(OS_POSIX) // Bacause of symbolic links. | |
| 1154 | |
| 1155 TEST_F(URLRequestTest, SymlinksToFiles) { | |
| 1156 base::ScopedTempDir temp_dir; | |
| 1157 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
| 1158 // Get an absolute path since the path can contain a symbolic link. | |
| 1159 base::FilePath absolute_temp_dir = | |
| 1160 base::MakeAbsoluteFilePath(temp_dir.GetPath()); | |
| 1161 base::FilePath test_file; | |
| 1162 ASSERT_TRUE(base::CreateTemporaryFileInDir(absolute_temp_dir, &test_file)); | |
| 1163 std::string test_data("monkey"); | |
| 1164 base::WriteFile(test_file, test_data.data(), test_data.size()); | |
| 1165 | |
| 1166 // This symlink will point to the test file. Access to the symlink will be | |
| 1167 // allowed as both the symlink and the destination file are in the same | |
| 1168 // directory that'll be white listed. | |
| 1169 base::FilePath good_symlink = absolute_temp_dir.AppendASCII("good_symlink"); | |
| 1170 ASSERT_TRUE(base::CreateSymbolicLink(test_file, good_symlink)); | |
| 1171 GURL good_file_url = FilePathToFileURL(good_symlink); | |
| 1172 // This symlink will point to /dev/null. Access to the symlink will be | |
| 1173 // rejected as /dev/null will not be white listed. | |
| 1174 base::FilePath bad_symlink = absolute_temp_dir.AppendASCII("bad_symlink"); | |
| 1175 ASSERT_TRUE(base::CreateSymbolicLink( | |
| 1176 base::FilePath(FILE_PATH_LITERAL("/dev/null")), bad_symlink)); | |
|
mmenke
2017/05/15 19:21:14
Maybe use something that's windows friendly? Not
satorux1
2017/05/19 07:02:52
Good point. Done
| |
| 1177 GURL bad_file_url = FilePathToFileURL(bad_symlink); | |
| 1178 | |
| 1179 { | |
| 1180 TestDelegate d; | |
| 1181 PickyNetworkDelegate network_delegate; | |
| 1182 network_delegate.AddToWhitelist(absolute_temp_dir); | |
| 1183 default_context_.set_network_delegate(&network_delegate); | |
| 1184 std::unique_ptr<URLRequest> r( | |
| 1185 default_context_.CreateRequest(good_file_url, DEFAULT_PRIORITY, &d)); | |
| 1186 r->Start(); | |
| 1187 base::RunLoop().Run(); | |
| 1188 // good_file_url should be allowed. | |
| 1189 EXPECT_FALSE(d.request_failed()); | |
| 1190 EXPECT_EQ(test_data, d.data_received()); | |
| 1191 } | |
| 1192 | |
| 1193 { | |
| 1194 TestDelegate d; | |
| 1195 PickyNetworkDelegate network_delegate; | |
| 1196 network_delegate.AddToWhitelist(absolute_temp_dir); | |
| 1197 default_context_.set_network_delegate(&network_delegate); | |
| 1198 std::unique_ptr<URLRequest> r( | |
| 1199 default_context_.CreateRequest(bad_file_url, DEFAULT_PRIORITY, &d)); | |
| 1200 r->Start(); | |
| 1201 base::RunLoop().Run(); | |
| 1202 // bad_file_url should be rejected. | |
| 1203 EXPECT_TRUE(d.request_failed()); | |
| 1204 EXPECT_EQ("", d.data_received()); | |
| 1205 EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status()); | |
| 1206 } | |
| 1207 } | |
| 1208 | |
| 1209 TEST_F(URLRequestTest, SymlinksToDirs) { | |
| 1210 // The temporary dir will be added to the whitelist. | |
| 1211 base::ScopedTempDir temp_dir; | |
| 1212 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
| 1213 // Get an absolute path since the path can contain a symbolic link. | |
| 1214 base::FilePath absolute_temp_dir = | |
| 1215 base::MakeAbsoluteFilePath(temp_dir.GetPath()); | |
| 1216 | |
| 1217 // This symlink will point to the temp dir. Access to the symlink will be | |
| 1218 // allowed as the symlink is in the temp dir that'll be white listed. | |
| 1219 base::FilePath good_symlink = absolute_temp_dir.AppendASCII("good_symlink"); | |
| 1220 ASSERT_TRUE(base::CreateSymbolicLink(absolute_temp_dir, good_symlink)); | |
| 1221 GURL good_file_url = FilePathToFileURL(good_symlink); | |
| 1222 // This symlink will point to /dev. Access to the symlink will be rejected | |
| 1223 // since /dev will not be white listed. | |
| 1224 base::FilePath bad_symlink = absolute_temp_dir.AppendASCII("bad_symlink"); | |
| 1225 ASSERT_TRUE(base::CreateSymbolicLink( | |
| 1226 base::FilePath(FILE_PATH_LITERAL("/dev")), bad_symlink)); | |
|
mmenke
2017/05/15 19:21:14
Do we really want to be modifying /dev in a test?
satorux1
2017/05/19 07:02:53
it's not modifying /dev. it's just creating a syml
| |
| 1227 GURL bad_file_url = FilePathToFileURL(bad_symlink); | |
| 1228 | |
| 1229 { | |
| 1230 TestDelegate d; | |
| 1231 PickyNetworkDelegate network_delegate; | |
| 1232 network_delegate.AddToWhitelist(absolute_temp_dir); | |
| 1233 default_context_.set_network_delegate(&network_delegate); | |
| 1234 std::unique_ptr<URLRequest> r( | |
| 1235 default_context_.CreateRequest(good_file_url, DEFAULT_PRIORITY, &d)); | |
| 1236 r->Start(); | |
| 1237 base::RunLoop().Run(); | |
| 1238 // good_file_url should be allowed. | |
| 1239 EXPECT_FALSE(d.request_failed()); | |
| 1240 ASSERT_NE(d.data_received().find("good_symlink"), std::string::npos); | |
| 1241 } | |
| 1242 | |
| 1243 { | |
| 1244 TestDelegate d; | |
| 1245 PickyNetworkDelegate network_delegate; | |
| 1246 network_delegate.AddToWhitelist(absolute_temp_dir); | |
| 1247 default_context_.set_network_delegate(&network_delegate); | |
| 1248 std::unique_ptr<URLRequest> r( | |
| 1249 default_context_.CreateRequest(bad_file_url, DEFAULT_PRIORITY, &d)); | |
| 1250 r->Start(); | |
| 1251 base::RunLoop().Run(); | |
| 1252 // bad_file_url should be rejected. | |
| 1253 EXPECT_TRUE(d.request_failed()); | |
| 1254 EXPECT_EQ("", d.data_received()); | |
| 1255 EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status()); | |
| 1256 } | |
| 1257 } | |
| 1258 | |
| 1259 #endif // defined(OS_POSIX) | |
| 1118 | 1260 |
| 1119 TEST_F(URLRequestTest, FileDirCancelTest) { | 1261 TEST_F(URLRequestTest, FileDirCancelTest) { |
| 1120 // Put in mock resource provider. | 1262 // Put in mock resource provider. |
| 1121 NetModule::SetResourceProvider(TestNetResourceProvider); | 1263 NetModule::SetResourceProvider(TestNetResourceProvider); |
| 1122 | 1264 |
| 1123 TestDelegate d; | 1265 TestDelegate d; |
| 1124 { | 1266 { |
| 1125 base::FilePath file_path; | 1267 base::FilePath file_path; |
| 1126 PathService::Get(base::DIR_SOURCE_ROOT, &file_path); | 1268 PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
| 1127 file_path = file_path.Append(FILE_PATH_LITERAL("net")); | 1269 file_path = file_path.Append(FILE_PATH_LITERAL("net")); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1159 req->Start(); | 1301 req->Start(); |
| 1160 base::RunLoop().Run(); | 1302 base::RunLoop().Run(); |
| 1161 | 1303 |
| 1162 // Generate entry for the sentinel file. | 1304 // Generate entry for the sentinel file. |
| 1163 base::FilePath sentinel_path = path.AppendASCII(sentinel_name); | 1305 base::FilePath sentinel_path = path.AppendASCII(sentinel_name); |
| 1164 base::File::Info info; | 1306 base::File::Info info; |
| 1165 EXPECT_TRUE(base::GetFileInfo(sentinel_path, &info)); | 1307 EXPECT_TRUE(base::GetFileInfo(sentinel_path, &info)); |
| 1166 EXPECT_GT(info.size, 0); | 1308 EXPECT_GT(info.size, 0); |
| 1167 std::string sentinel_output = GetDirectoryListingEntry( | 1309 std::string sentinel_output = GetDirectoryListingEntry( |
| 1168 base::string16(sentinel_name, sentinel_name + strlen(sentinel_name)), | 1310 base::string16(sentinel_name, sentinel_name + strlen(sentinel_name)), |
| 1169 std::string(sentinel_name), | 1311 std::string(sentinel_name), false /* is_dir */, info.size, |
| 1170 false /* is_dir */, | 1312 |
| 1171 info.size, | |
| 1172 info.last_modified); | 1313 info.last_modified); |
| 1173 | 1314 |
| 1174 ASSERT_LT(0, d.bytes_received()); | 1315 ASSERT_LT(0, d.bytes_received()); |
| 1175 ASSERT_FALSE(d.request_failed()); | 1316 ASSERT_FALSE(d.request_failed()); |
| 1176 EXPECT_EQ(OK, d.request_status()); | 1317 EXPECT_EQ(OK, d.request_status()); |
| 1177 // Check for the entry generated for the "sentinel" file. | 1318 // Check for the entry generated for the "sentinel" file. |
| 1178 const std::string& data = d.data_received(); | 1319 const std::string& data = d.data_received(); |
| 1179 ASSERT_NE(data.find(sentinel_output), std::string::npos); | 1320 ASSERT_NE(data.find(sentinel_output), std::string::npos); |
| 1180 } | 1321 } |
| 1181 | 1322 |
| (...skipping 9876 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 11058 AddTestInterceptor()->set_main_intercept_job(std::move(job)); | 11199 AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
| 11059 | 11200 |
| 11060 req->Start(); | 11201 req->Start(); |
| 11061 req->Cancel(); | 11202 req->Cancel(); |
| 11062 base::RunLoop().RunUntilIdle(); | 11203 base::RunLoop().RunUntilIdle(); |
| 11063 EXPECT_EQ(ERR_ABORTED, d.request_status()); | 11204 EXPECT_EQ(ERR_ABORTED, d.request_status()); |
| 11064 EXPECT_EQ(0, d.received_redirect_count()); | 11205 EXPECT_EQ(0, d.received_redirect_count()); |
| 11065 } | 11206 } |
| 11066 | 11207 |
| 11067 } // namespace net | 11208 } // namespace net |
| OLD | NEW |