Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1893)

Side by Side Diff: net/url_request/url_request_unittest.cc

Issue 2786583002: chromeos: Check both original and absolute paths for file: scheme (Closed)
Patch Set: Fix for Mac and Android bots Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/url_request/url_request_test_util.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 817 matching lines...) Expand 10 before | Expand all | Expand 10 after
828 } 828 }
829 829
830 protected: 830 protected:
831 TestNetLog net_log_; 831 TestNetLog net_log_;
832 TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. 832 TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest.
833 URLRequestJobFactoryImpl* job_factory_impl_; 833 URLRequestJobFactoryImpl* job_factory_impl_;
834 std::unique_ptr<URLRequestJobFactory> job_factory_; 834 std::unique_ptr<URLRequestJobFactory> job_factory_;
835 TestURLRequestContext default_context_; 835 TestURLRequestContext default_context_;
836 }; 836 };
837 837
838 // This NetworkDelegate is picky about what files are accessible. Only
839 // whitelisted files are allowed.
840 class CookieBlockingNetworkDelegate : public TestNetworkDelegate {
841 public:
842 CookieBlockingNetworkDelegate(){};
843
844 // Adds |directory| to the access white list.
845 void AddToWhitelist(const base::FilePath& directory) {
846 // TODO(satorux): Remove this. This is for debugging test failures on
847 // Android bots.
848 LOG(ERROR) << "@@ AddToWhitelist: " << directory.value();
mmenke 2017/05/24 19:25:51 nit: Remove logging.
satorux1 2017/05/25 01:51:07 Done.
849 whitelist_.insert(directory);
850 }
851
852 // Returns true if |path| matches the white list.
853 bool OnCanAccessFileInternal(const base::FilePath& path) const {
mmenke 2017/05/24 19:25:50 nit: Private? Doesn't really matter, except that
satorux1 2017/05/25 01:51:07 Done.
854 for (const auto& directory : whitelist_) {
855 if (directory == path || directory.IsParent(path))
856 return true;
857 }
858 return false;
859 }
860
861 // Returns true only if both |original_path| and |absolute_path| match the
862 // white list.
863 bool OnCanAccessFile(const URLRequest& request,
864 const base::FilePath& original_path,
865 const base::FilePath& absolute_path) const override {
866 LOG(ERROR) << "@@ original_path: " << original_path.value();
867 LOG(ERROR) << "@@ absolute_path: " << absolute_path.value();
mmenke 2017/05/24 19:25:50 nit: Remove logging.
satorux1 2017/05/25 01:51:07 Done.
868 return (OnCanAccessFileInternal(original_path) &&
869 OnCanAccessFileInternal(absolute_path));
870 }
871
872 private:
873 std::set<base::FilePath> whitelist_;
874
875 DISALLOW_COPY_AND_ASSIGN(CookieBlockingNetworkDelegate);
876 };
877
838 TEST_F(URLRequestTest, AboutBlankTest) { 878 TEST_F(URLRequestTest, AboutBlankTest) {
839 TestDelegate d; 879 TestDelegate d;
840 { 880 {
841 std::unique_ptr<URLRequest> r( 881 std::unique_ptr<URLRequest> r(
842 default_context_.CreateRequest(GURL("about:blank"), DEFAULT_PRIORITY, 882 default_context_.CreateRequest(GURL("about:blank"), DEFAULT_PRIORITY,
843 &d, TRAFFIC_ANNOTATION_FOR_TESTS)); 883 &d, TRAFFIC_ANNOTATION_FOR_TESTS));
844 884
845 r->Start(); 885 r->Start();
846 EXPECT_TRUE(r->is_pending()); 886 EXPECT_TRUE(r->is_pending());
847 887
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after
1076 base::RunLoop().Run(); 1116 base::RunLoop().Run();
1077 EXPECT_TRUE(d.request_failed()); 1117 EXPECT_TRUE(d.request_failed());
1078 } 1118 }
1079 1119
1080 EXPECT_TRUE(base::DeleteFile(temp_path, false)); 1120 EXPECT_TRUE(base::DeleteFile(temp_path, false));
1081 } 1121 }
1082 1122
1083 TEST_F(URLRequestTest, AllowFileURLs) { 1123 TEST_F(URLRequestTest, AllowFileURLs) {
1084 base::ScopedTempDir temp_dir; 1124 base::ScopedTempDir temp_dir;
1085 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 1125 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1126 LOG(ERROR) << "@@ temp_dir: " << temp_dir.GetPath().value();
mmenke 2017/05/24 19:25:50 nit: Remove logging (Not going to call out the ot
satorux1 2017/05/25 01:51:07 Done.
1127 // Get an absolute path since temp_dir can contain a symbolic link. As of
1128 // now, Mac and Android bots return a path with a symbolic link.
1129 base::FilePath absolute_temp_dir =
1130 base::MakeAbsoluteFilePath(temp_dir.GetPath());
1131
1086 base::FilePath test_file; 1132 base::FilePath test_file;
1087 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.GetPath(), &test_file)); 1133 ASSERT_TRUE(base::CreateTemporaryFileInDir(absolute_temp_dir, &test_file));
1134 LOG(ERROR) << "@@ test_file: " << test_file.value();
1135 // The directory part of the path returned from CreateTemporaryFileInDir()
1136 // can be slightly different from temp_dir on Windows.
mmenke 2017/05/24 19:25:50 nit: |absolute_temp_dir|? (|| around local names
satorux1 2017/05/25 01:51:07 Done.
1137 // Example: C:\\Users\\CHROME~2 -> C:\\Users\\chrome-bot
1138 // Hence the test should use the directory name of test_file, rather than
1139 // temp_dir, for whitelisting.
1140 base::FilePath real_temp_dir = test_file.DirName();
1141 LOG(ERROR) << "@@ real_temp_dir: " << real_temp_dir.value();
1088 std::string test_data("monkey"); 1142 std::string test_data("monkey");
1089 base::WriteFile(test_file, test_data.data(), test_data.size()); 1143 base::WriteFile(test_file, test_data.data(), test_data.size());
1090 GURL test_file_url = FilePathToFileURL(test_file); 1144 GURL test_file_url = FilePathToFileURL(test_file);
1091
1092 { 1145 {
1093 TestDelegate d; 1146 TestDelegate d;
1094 TestNetworkDelegate network_delegate; 1147 CookieBlockingNetworkDelegate network_delegate;
1095 network_delegate.set_can_access_files(true); 1148 network_delegate.AddToWhitelist(real_temp_dir);
1096 default_context_.set_network_delegate(&network_delegate); 1149 default_context_.set_network_delegate(&network_delegate);
1097 std::unique_ptr<URLRequest> r(default_context_.CreateRequest( 1150 std::unique_ptr<URLRequest> r(default_context_.CreateRequest(
1098 test_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); 1151 test_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS));
1099 r->Start(); 1152 r->Start();
1100 base::RunLoop().Run(); 1153 base::RunLoop().Run();
1154 // This should be allowed as the file path is white listed.
mmenke 2017/05/24 19:25:51 nit: You're using whitelisting above, white liste
satorux1 2017/05/25 01:51:07 Done.
1101 EXPECT_FALSE(d.request_failed()); 1155 EXPECT_FALSE(d.request_failed());
1102 EXPECT_EQ(test_data, d.data_received()); 1156 EXPECT_EQ(test_data, d.data_received());
1103 } 1157 }
1104 1158
1105 { 1159 {
1106 TestDelegate d; 1160 TestDelegate d;
1107 TestNetworkDelegate network_delegate; 1161 CookieBlockingNetworkDelegate network_delegate;
1108 network_delegate.set_can_access_files(false);
1109 default_context_.set_network_delegate(&network_delegate); 1162 default_context_.set_network_delegate(&network_delegate);
1110 std::unique_ptr<URLRequest> r(default_context_.CreateRequest( 1163 std::unique_ptr<URLRequest> r(default_context_.CreateRequest(
1111 test_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); 1164 test_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS));
1112 r->Start(); 1165 r->Start();
1113 base::RunLoop().Run(); 1166 base::RunLoop().Run();
1167 // This should be rejected as the file path is not white listed.
mmenke 2017/05/24 19:25:50 whitelisted
satorux1 2017/05/25 01:51:07 Done.
1114 EXPECT_TRUE(d.request_failed()); 1168 EXPECT_TRUE(d.request_failed());
1115 EXPECT_EQ("", d.data_received()); 1169 EXPECT_EQ("", d.data_received());
1170 EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status());
1116 } 1171 }
1117 } 1172 }
1118 1173
1174 #if defined(OS_POSIX) // Bacause of symbolic links.
1175
1176 TEST_F(URLRequestTest, SymlinksToFiles) {
1177 base::ScopedTempDir temp_dir;
1178 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1179 // Get an absolute path since temp_dir can contain a symbolic link.
1180 base::FilePath absolute_temp_dir =
1181 base::MakeAbsoluteFilePath(temp_dir.GetPath());
1182
1183 // Create a good directory (will be white listed) and a good file.
mmenke 2017/05/24 19:25:51 whitelisted
satorux1 2017/05/25 01:51:07 Done.
1184 base::FilePath good_dir = absolute_temp_dir.AppendASCII("good");
1185 ASSERT_TRUE(base::CreateDirectory(good_dir));
1186 base::FilePath good_file;
1187 ASSERT_TRUE(base::CreateTemporaryFileInDir(good_dir, &good_file));
1188 std::string good_data("good");
1189 base::WriteFile(good_file, good_data.data(), good_data.size());
1190 // See the comment in AllowFileURLs() for why doing this.
mmenke 2017/05/24 19:25:50 why doing this -> why this is done (Or why this is
satorux1 2017/05/25 01:51:07 Done.
1191 base::FilePath real_good_dir = good_file.DirName();
1192
1193 // Create a bad directory (will not be white listed) and a bad file.
mmenke 2017/05/24 19:25:50 whitelisted
satorux1 2017/05/25 01:51:06 Done.
1194 base::FilePath bad_dir = absolute_temp_dir.AppendASCII("bad");
1195 ASSERT_TRUE(base::CreateDirectory(bad_dir));
1196 base::FilePath bad_file;
1197 ASSERT_TRUE(base::CreateTemporaryFileInDir(bad_dir, &bad_file));
1198 std::string bad_data("bad");
1199 base::WriteFile(bad_file, bad_data.data(), bad_data.size());
1200
1201 // This symlink will point to the good file. Access to the symlink will be
1202 // allowed as both the symlink and the destination file are in the same
1203 // good directory.
1204 base::FilePath good_symlink = good_dir.AppendASCII("good_symlink");
1205 ASSERT_TRUE(base::CreateSymbolicLink(good_file, good_symlink));
1206 GURL good_file_url = FilePathToFileURL(good_symlink);
1207 // This symlink will point to the bad file. Even though the symlink is in
1208 // the good directory, access to the symlink will be rejected since it
1209 // points to the bad file.
1210 base::FilePath bad_symlink = good_dir.AppendASCII("bad_symlink");
1211 ASSERT_TRUE(base::CreateSymbolicLink(bad_file, bad_symlink));
1212 GURL bad_file_url = FilePathToFileURL(bad_symlink);
1213
1214 {
1215 TestDelegate d;
1216 CookieBlockingNetworkDelegate network_delegate;
1217 network_delegate.AddToWhitelist(real_good_dir);
1218 default_context_.set_network_delegate(&network_delegate);
mmenke 2017/05/24 19:25:50 Optional: Can move this network delegate outside
satorux1 2017/05/25 01:51:06 Good idea. Done!
1219 std::unique_ptr<URLRequest> r(
1220 default_context_.CreateRequest(good_file_url, DEFAULT_PRIORITY, &d));
1221 r->Start();
1222 base::RunLoop().Run();
1223 // good_file_url should be allowed.
1224 EXPECT_FALSE(d.request_failed());
1225 EXPECT_EQ(good_data, d.data_received());
1226 }
1227
1228 {
1229 TestDelegate d;
1230 CookieBlockingNetworkDelegate network_delegate;
1231 network_delegate.AddToWhitelist(real_good_dir);
1232 default_context_.set_network_delegate(&network_delegate);
1233 std::unique_ptr<URLRequest> r(
1234 default_context_.CreateRequest(bad_file_url, DEFAULT_PRIORITY, &d));
1235 r->Start();
1236 base::RunLoop().Run();
1237 // bad_file_url should be rejected.
1238 EXPECT_TRUE(d.request_failed());
1239 EXPECT_EQ("", d.data_received());
1240 EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status());
1241 }
1242 }
1243
1244 TEST_F(URLRequestTest, SymlinksToDirs) {
1245 base::ScopedTempDir temp_dir;
1246 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1247 // Get an absolute path since temp_dir can contain a symbolic link.
1248 base::FilePath absolute_temp_dir =
1249 base::MakeAbsoluteFilePath(temp_dir.GetPath());
1250
1251 // Create a good directory (will be white listed).
mmenke 2017/05/24 19:25:50 whitelisted
satorux1 2017/05/25 01:51:06 Done.
1252 base::FilePath good_dir = absolute_temp_dir.AppendASCII("good");
1253 ASSERT_TRUE(base::CreateDirectory(good_dir));
1254
1255 // Create a bad directory (will not be white listed).
mmenke 2017/05/24 19:25:50 whitelisted
satorux1 2017/05/25 01:51:07 Done.
1256 base::FilePath bad_dir = absolute_temp_dir.AppendASCII("bad");
1257 ASSERT_TRUE(base::CreateDirectory(bad_dir));
1258
1259 // This symlink will point to the good directory. Access to the symlink
1260 // will be allowed as the symlink is in the good dir that'll be white
1261 // listed.
1262 base::FilePath good_symlink = good_dir.AppendASCII("good_symlink");
1263 ASSERT_TRUE(base::CreateSymbolicLink(good_dir, good_symlink));
1264 GURL good_file_url = FilePathToFileURL(good_symlink);
1265 // This symlink will point to the bad directory. Even though the symlink is
1266 // in the good directory, access to the symlink will be rejected since it
1267 // points to the bad directory.
1268 base::FilePath bad_symlink = good_dir.AppendASCII("bad_symlink");
1269 ASSERT_TRUE(base::CreateSymbolicLink(bad_dir, bad_symlink));
1270 GURL bad_file_url = FilePathToFileURL(bad_symlink);
1271
1272 {
1273 TestDelegate d;
1274 CookieBlockingNetworkDelegate network_delegate;
1275 network_delegate.AddToWhitelist(good_dir);
1276 default_context_.set_network_delegate(&network_delegate);
mmenke 2017/05/24 19:25:50 Again, can share these
satorux1 2017/05/25 01:51:07 Done.
1277 std::unique_ptr<URLRequest> r(
1278 default_context_.CreateRequest(good_file_url, DEFAULT_PRIORITY, &d));
1279 r->Start();
1280 base::RunLoop().Run();
1281 // good_file_url should be allowed.
1282 EXPECT_FALSE(d.request_failed());
1283 ASSERT_NE(d.data_received().find("good_symlink"), std::string::npos);
1284 }
1285
1286 {
1287 TestDelegate d;
1288 CookieBlockingNetworkDelegate network_delegate;
1289 network_delegate.AddToWhitelist(good_dir);
1290 default_context_.set_network_delegate(&network_delegate);
1291 std::unique_ptr<URLRequest> r(
1292 default_context_.CreateRequest(bad_file_url, DEFAULT_PRIORITY, &d));
1293 r->Start();
1294 base::RunLoop().Run();
1295 // bad_file_url should be rejected.
1296 EXPECT_TRUE(d.request_failed());
1297 EXPECT_EQ("", d.data_received());
1298 EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status());
1299 }
1300 }
1301
1302 #endif // defined(OS_POSIX)
1119 1303
1120 TEST_F(URLRequestTest, FileDirCancelTest) { 1304 TEST_F(URLRequestTest, FileDirCancelTest) {
1121 // Put in mock resource provider. 1305 // Put in mock resource provider.
1122 NetModule::SetResourceProvider(TestNetResourceProvider); 1306 NetModule::SetResourceProvider(TestNetResourceProvider);
1123 1307
1124 TestDelegate d; 1308 TestDelegate d;
1125 { 1309 {
1126 base::FilePath file_path; 1310 base::FilePath file_path;
1127 PathService::Get(base::DIR_SOURCE_ROOT, &file_path); 1311 PathService::Get(base::DIR_SOURCE_ROOT, &file_path);
1128 file_path = file_path.Append(FILE_PATH_LITERAL("net")); 1312 file_path = file_path.Append(FILE_PATH_LITERAL("net"));
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1160 req->Start(); 1344 req->Start();
1161 base::RunLoop().Run(); 1345 base::RunLoop().Run();
1162 1346
1163 // Generate entry for the sentinel file. 1347 // Generate entry for the sentinel file.
1164 base::FilePath sentinel_path = path.AppendASCII(sentinel_name); 1348 base::FilePath sentinel_path = path.AppendASCII(sentinel_name);
1165 base::File::Info info; 1349 base::File::Info info;
1166 EXPECT_TRUE(base::GetFileInfo(sentinel_path, &info)); 1350 EXPECT_TRUE(base::GetFileInfo(sentinel_path, &info));
1167 EXPECT_GT(info.size, 0); 1351 EXPECT_GT(info.size, 0);
1168 std::string sentinel_output = GetDirectoryListingEntry( 1352 std::string sentinel_output = GetDirectoryListingEntry(
1169 base::string16(sentinel_name, sentinel_name + strlen(sentinel_name)), 1353 base::string16(sentinel_name, sentinel_name + strlen(sentinel_name)),
1170 std::string(sentinel_name), 1354 std::string(sentinel_name), false /* is_dir */, info.size,
1171 false /* is_dir */, 1355
1172 info.size,
1173 info.last_modified); 1356 info.last_modified);
1174 1357
1175 ASSERT_LT(0, d.bytes_received()); 1358 ASSERT_LT(0, d.bytes_received());
1176 ASSERT_FALSE(d.request_failed()); 1359 ASSERT_FALSE(d.request_failed());
1177 EXPECT_EQ(OK, d.request_status()); 1360 EXPECT_EQ(OK, d.request_status());
1178 // Check for the entry generated for the "sentinel" file. 1361 // Check for the entry generated for the "sentinel" file.
1179 const std::string& data = d.data_received(); 1362 const std::string& data = d.data_received();
1180 ASSERT_NE(data.find(sentinel_output), std::string::npos); 1363 ASSERT_NE(data.find(sentinel_output), std::string::npos);
1181 } 1364 }
1182 1365
(...skipping 9876 matching lines...) Expand 10 before | Expand all | Expand 10 after
11059 AddTestInterceptor()->set_main_intercept_job(std::move(job)); 11242 AddTestInterceptor()->set_main_intercept_job(std::move(job));
11060 11243
11061 req->Start(); 11244 req->Start();
11062 req->Cancel(); 11245 req->Cancel();
11063 base::RunLoop().RunUntilIdle(); 11246 base::RunLoop().RunUntilIdle();
11064 EXPECT_EQ(ERR_ABORTED, d.request_status()); 11247 EXPECT_EQ(ERR_ABORTED, d.request_status());
11065 EXPECT_EQ(0, d.received_redirect_count()); 11248 EXPECT_EQ(0, d.received_redirect_count());
11066 } 11249 }
11067 11250
11068 } // namespace net 11251 } // namespace net
OLDNEW
« no previous file with comments | « net/url_request/url_request_test_util.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698