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

Side by Side Diff: chrome/browser/download/save_page_browsertest.cc

Issue 2075273002: Resource requests from Save-Page-As should go through CanRequestURL checks. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Updated docs. Created 4 years, 5 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
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 <stddef.h> 5 #include <stddef.h>
6 #include <stdint.h> 6 #include <stdint.h>
7 #include <string> 7 #include <string>
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/bind_helpers.h" 12 #include "base/bind_helpers.h"
13 #include "base/command_line.h" 13 #include "base/command_line.h"
14 #include "base/files/file_path.h" 14 #include "base/files/file_path.h"
15 #include "base/files/file_util.h" 15 #include "base/files/file_util.h"
16 #include "base/files/scoped_temp_dir.h" 16 #include "base/files/scoped_temp_dir.h"
17 #include "base/macros.h" 17 #include "base/macros.h"
18 #include "base/path_service.h" 18 #include "base/path_service.h"
19 #include "base/run_loop.h" 19 #include "base/run_loop.h"
20 #include "base/strings/string_util.h" 20 #include "base/strings/string_util.h"
21 #include "base/strings/stringprintf.h"
21 #include "base/strings/utf_string_conversions.h" 22 #include "base/strings/utf_string_conversions.h"
22 #include "base/test/test_file_util.h" 23 #include "base/test/test_file_util.h"
23 #include "build/build_config.h" 24 #include "build/build_config.h"
24 #include "chrome/app/chrome_command_ids.h" 25 #include "chrome/app/chrome_command_ids.h"
25 #include "chrome/browser/download/chrome_download_manager_delegate.h" 26 #include "chrome/browser/download/chrome_download_manager_delegate.h"
26 #include "chrome/browser/download/download_history.h" 27 #include "chrome/browser/download/download_history.h"
27 #include "chrome/browser/download/download_prefs.h" 28 #include "chrome/browser/download/download_prefs.h"
28 #include "chrome/browser/download/download_service.h" 29 #include "chrome/browser/download/download_service.h"
29 #include "chrome/browser/download/download_service_factory.h" 30 #include "chrome/browser/download/download_service_factory.h"
30 #include "chrome/browser/download/save_package_file_picker.h" 31 #include "chrome/browser/download/save_package_file_picker.h"
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 using content::BrowserContext; 66 using content::BrowserContext;
66 using content::BrowserThread; 67 using content::BrowserThread;
67 using content::DownloadItem; 68 using content::DownloadItem;
68 using content::DownloadManager; 69 using content::DownloadManager;
69 using content::RenderFrameHost; 70 using content::RenderFrameHost;
70 using content::RenderProcessHost; 71 using content::RenderProcessHost;
71 using content::WebContents; 72 using content::WebContents;
72 using net::URLRequestMockHTTPJob; 73 using net::URLRequestMockHTTPJob;
73 using testing::ContainsRegex; 74 using testing::ContainsRegex;
74 using testing::HasSubstr; 75 using testing::HasSubstr;
76 using testing::Not;
75 77
76 namespace { 78 namespace {
77 79
78 // Returns file contents with each continuous run of whitespace replaced by a 80 // Returns file contents with each continuous run of whitespace replaced by a
79 // single space. 81 // single space.
80 std::string ReadFileAndCollapseWhitespace(const base::FilePath& file_path) { 82 std::string ReadFileAndCollapseWhitespace(const base::FilePath& file_path) {
81 std::string file_contents; 83 std::string file_contents;
82 if (!base::ReadFileToString(file_path, &file_contents)) { 84 if (!base::ReadFileToString(file_path, &file_contents)) {
83 ADD_FAILURE() << "Failed to read \"" << file_path.value() << "\" file."; 85 ADD_FAILURE() << "Failed to read \"" << file_path.value() << "\" file.";
84 return std::string(); 86 return std::string();
(...skipping 719 matching lines...) Expand 10 before | Expand all | Expand 10 after
804 base::FilePath full_file_name, dir; 806 base::FilePath full_file_name, dir;
805 SaveCurrentTab(url, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML, 807 SaveCurrentTab(url, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML,
806 "iframe-src-is-a-download", 3, &dir, &full_file_name); 808 "iframe-src-is-a-download", 3, &dir, &full_file_name);
807 ASSERT_FALSE(HasFailure()); 809 ASSERT_FALSE(HasFailure());
808 810
809 EXPECT_TRUE(base::PathExists(full_file_name)); 811 EXPECT_TRUE(base::PathExists(full_file_name));
810 EXPECT_TRUE(base::PathExists(dir.AppendASCII("thisdayinhistory.html"))); 812 EXPECT_TRUE(base::PathExists(dir.AppendASCII("thisdayinhistory.html")));
811 EXPECT_TRUE(base::PathExists(dir.AppendASCII("no-such-file.html"))); 813 EXPECT_TRUE(base::PathExists(dir.AppendASCII("no-such-file.html")));
812 } 814 }
813 815
816 // Test that file: URI won't be saved when referred to from an HTTP page.
817 // See also https://crbug.com/616429.
818 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveUnauthorizedResource) {
819 GURL url = NavigateToMockURL("unauthorized-access");
820
821 // Create a test file (that the web page should not have access to).
822 base::ScopedTempDir temp_dir2;
823 ASSERT_TRUE(temp_dir2.CreateUniqueTempDir());
824 base::FilePath file_path =
825 temp_dir2.path().Append(FILE_PATH_LITERAL("should-not-save.jpg"));
826 std::string file_content("fake-jpg");
827 ASSERT_LT(
828 0, base::WriteFile(file_path, file_content.data(), file_content.size()));
829
830 // Refer to the test file from the test page.
831 GURL file_url = net::FilePathToFileURL(file_path);
832 ASSERT_TRUE(ExecuteScript(
833 browser()->tab_strip_model()->GetWebContentsAt(0),
834 base::StringPrintf("document.getElementById('resource1').src = '%s';",
835 file_url.spec().data())));
836
837 // Save the current page.
838 base::FilePath full_file_name, dir;
839 SaveCurrentTab(url, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML,
840 "unauthorized-access", 2, &dir, &full_file_name);
841
842 // We should not save resource that the web page didn't have access to.
843 // (because executing a resource request can have side effects - for example
844 // after https://crbug.com/590714 a website from the internet should not be
845 // able to issue a resource request to an intranet website and trigger
846 // server-side actions in the internet; this test uses a file: URI as a
847 // canary for detecting whether a website can access restricted resources).
848 EXPECT_FALSE(base::PathExists(dir.AppendASCII("should-not-save.jpg")));
849 }
850
814 // Test suite that allows testing --site-per-process against cross-site frames. 851 // Test suite that allows testing --site-per-process against cross-site frames.
815 // See http://dev.chromium.org/developers/design-documents/site-isolation. 852 // See http://dev.chromium.org/developers/design-documents/site-isolation.
816 class SavePageSitePerProcessBrowserTest : public SavePageBrowserTest { 853 class SavePageSitePerProcessBrowserTest : public SavePageBrowserTest {
817 public: 854 public:
818 SavePageSitePerProcessBrowserTest() {} 855 SavePageSitePerProcessBrowserTest() {}
819 856
820 protected: 857 protected:
821 void SetUpCommandLine(base::CommandLine* command_line) override { 858 void SetUpCommandLine(base::CommandLine* command_line) override {
822 SavePageBrowserTest::SetUpCommandLine(command_line); 859 SavePageBrowserTest::SetUpCommandLine(command_line);
823 860
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
1014 // [saved version of the page] as for the "before" 1051 // [saved version of the page] as for the "before"
1015 // [the original version of the page]. 1052 // [the original version of the page].
1016 ui_test_utils::NavigateToURL(browser(), 1053 ui_test_utils::NavigateToURL(browser(),
1017 GURL(net::FilePathToFileURL(full_file_name))); 1054 GURL(net::FilePathToFileURL(full_file_name)));
1018 DLOG(INFO) << "Verifying test expectations for saved page... : " 1055 DLOG(INFO) << "Verifying test expectations for saved page... : "
1019 << GetCurrentTab(browser())->GetLastCommittedURL(); 1056 << GetCurrentTab(browser())->GetLastCommittedURL();
1020 AssertExpectationsAboutCurrentTab(expected_number_of_frames, 1057 AssertExpectationsAboutCurrentTab(expected_number_of_frames,
1021 expected_substrings); 1058 expected_substrings);
1022 } 1059 }
1023 1060
1061 // Helper method to deduplicate some code across 2 tests.
1062 void RunObjectElementsTest(GURL url) {
1063 content::SavePageType save_page_type = GetParam();
1064
1065 // 4 = main frame + iframe + object w/ html doc + object w/ pdf doc + plain
1066 // text (svg and png objects do not get a separate frame)
Randy Smith (Not in Mondays) 2016/07/21 19:29:53 I do not understand the relationship between the c
Łukasz Anforowicz 2016/07/21 23:44:30 Ooops - done. I screwed that up in https://chromi
1067 int expected_number_of_frames = 7;
1068
1069 std::string arr[] = {
1070 "frames-objects.htm: 8da13db4-a512-4d9b-b1c5-dc1c134234b9",
1071 "a.htm: 1b8aae2b-e164-462f-bd5b-98aa366205f2",
1072 "b.htm: 3a35f7fa-96a9-4487-9f18-4470263907fa",
1073 "frames-nested.htm: 4388232f-8d45-4d2e-9807-721b381be153",
1074 "frames-nested2.htm: 6d23dc47-f283-4977-96ec-66bcf72301a4",
1075 "text-object.txt: ae52dd09-9746-4b7e-86a6-6ada5e2680c2",
1076 };
1077 std::vector<std::string> expected_substrings(std::begin(arr),
1078 std::end(arr));
1079
1080 // TODO(lukasza): crbug.com/553478: Enable <object> testing of MHTML.
1081 if (save_page_type == content::SAVE_PAGE_TYPE_AS_MHTML)
1082 return;
1083
1084 TestOriginalVsSavedPage(save_page_type, url, expected_number_of_frames,
1085 expected_substrings);
1086 }
1087
1024 private: 1088 private:
1025 void AssertExpectationsAboutCurrentTab( 1089 void AssertExpectationsAboutCurrentTab(
1026 int expected_number_of_frames, 1090 int expected_number_of_frames,
1027 const std::vector<std::string>& expected_substrings) { 1091 const std::vector<std::string>& expected_substrings) {
1028 int actual_number_of_frames = 0; 1092 int actual_number_of_frames = 0;
1029 GetCurrentTab(browser())->ForEachFrame(base::Bind( 1093 GetCurrentTab(browser())->ForEachFrame(base::Bind(
1030 &IncrementInteger, base::Unretained(&actual_number_of_frames))); 1094 &IncrementInteger, base::Unretained(&actual_number_of_frames)));
1031 EXPECT_EQ(expected_number_of_frames, actual_number_of_frames); 1095 EXPECT_EQ(expected_number_of_frames, actual_number_of_frames);
1032 1096
1033 for (const auto& expected_substring : expected_substrings) { 1097 for (const auto& expected_substring : expected_substrings) {
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
1083 std::vector<std::string> expected_substrings(std::begin(arr), std::end(arr)); 1147 std::vector<std::string> expected_substrings(std::begin(arr), std::end(arr));
1084 1148
1085 GURL url( 1149 GURL url(
1086 embedded_test_server()->GetURL("a.com", "/save_page/frames-xsite.htm")); 1150 embedded_test_server()->GetURL("a.com", "/save_page/frames-xsite.htm"));
1087 1151
1088 TestOriginalVsSavedPage(save_page_type, url, 3, expected_substrings); 1152 TestOriginalVsSavedPage(save_page_type, url, 3, expected_substrings);
1089 } 1153 }
1090 1154
1091 // Test compares original-vs-saved for a page with <object> elements. 1155 // Test compares original-vs-saved for a page with <object> elements.
1092 // (see crbug.com/553478). 1156 // (see crbug.com/553478).
1093 IN_PROC_BROWSER_TEST_P(SavePageOriginalVsSavedComparisonTest, ObjectElements) { 1157 IN_PROC_BROWSER_TEST_P(SavePageOriginalVsSavedComparisonTest,
1094 content::SavePageType save_page_type = GetParam(); 1158 ObjectElementsViaHttp) {
1095
1096 // 4 = main frame + iframe + object w/ html doc + object w/ pdf doc
1097 // (svg and png objects do not get a separate frame)
1098 int expected_number_of_frames = 6;
1099
1100 std::string arr[] = {
1101 "frames-objects.htm: 8da13db4-a512-4d9b-b1c5-dc1c134234b9",
1102 "a.htm: 1b8aae2b-e164-462f-bd5b-98aa366205f2",
1103 "b.htm: 3a35f7fa-96a9-4487-9f18-4470263907fa",
1104 "frames-nested.htm: 4388232f-8d45-4d2e-9807-721b381be153",
1105 "frames-nested2.htm: 6d23dc47-f283-4977-96ec-66bcf72301a4",
1106 };
1107 std::vector<std::string> expected_substrings(std::begin(arr), std::end(arr));
1108
1109 GURL url( 1159 GURL url(
1110 embedded_test_server()->GetURL("a.com", "/save_page/frames-objects.htm")); 1160 embedded_test_server()->GetURL("a.com", "/save_page/frames-objects.htm"));
1111 1161
1112 // TODO(lukasza): crbug.com/553478: Enable <object> testing of MHTML. 1162 RunObjectElementsTest(url);
1113 if (save_page_type == content::SAVE_PAGE_TYPE_AS_MHTML) 1163 }
1114 return;
1115 1164
1116 TestOriginalVsSavedPage(save_page_type, url, expected_number_of_frames, 1165 // Tests that saving a page from file: URI works.
1117 expected_substrings); 1166 IN_PROC_BROWSER_TEST_P(SavePageOriginalVsSavedComparisonTest,
1167 ObjectElementsViaFile) {
1168 base::FilePath test_data_dir;
1169 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir));
1170 GURL url(net::FilePathToFileURL(
1171 test_data_dir.Append(FILE_PATH_LITERAL("save_page/frames-objects.htm"))));
1172 EXPECT_TRUE(url.SchemeIsFile());
1173
1174 RunObjectElementsTest(url);
1118 } 1175 }
1119 1176
1120 // Test compares original-vs-saved for a page with frames at about:blank uri. 1177 // Test compares original-vs-saved for a page with frames at about:blank uri.
1121 // This tests handling of iframe elements without src attribute (only with 1178 // This tests handling of iframe elements without src attribute (only with
1122 // srcdoc attribute) and how they get saved / cross-referenced. 1179 // srcdoc attribute) and how they get saved / cross-referenced.
1123 IN_PROC_BROWSER_TEST_P(SavePageOriginalVsSavedComparisonTest, AboutBlank) { 1180 IN_PROC_BROWSER_TEST_P(SavePageOriginalVsSavedComparisonTest, AboutBlank) {
1124 content::SavePageType save_page_type = GetParam(); 1181 content::SavePageType save_page_type = GetParam();
1125 1182
1126 std::string arr[] = { 1183 std::string arr[] = {
1127 "main: acb0609d-eb10-4c26-83e2-ad8afb7b0ff3", 1184 "main: acb0609d-eb10-4c26-83e2-ad8afb7b0ff3",
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
1255 TestOriginalVsSavedPage(save_page_type, url, 1, expected_substrings); 1312 TestOriginalVsSavedPage(save_page_type, url, 1, expected_substrings);
1256 } 1313 }
1257 1314
1258 INSTANTIATE_TEST_CASE_P( 1315 INSTANTIATE_TEST_CASE_P(
1259 SaveType, 1316 SaveType,
1260 SavePageOriginalVsSavedComparisonTest, 1317 SavePageOriginalVsSavedComparisonTest,
1261 ::testing::Values(content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML, 1318 ::testing::Values(content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML,
1262 content::SAVE_PAGE_TYPE_AS_MHTML)); 1319 content::SAVE_PAGE_TYPE_AS_MHTML));
1263 1320
1264 } // namespace 1321 } // namespace
OLDNEW
« no previous file with comments | « no previous file | chrome/test/data/save_page/frames-objects.htm » ('j') | content/browser/download/docs/save-page-as.md » ('J')

Powered by Google App Engine
This is Rietveld 408576698