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

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: Replace MarkAsUnauthorized with constructor argument. Created 4 years, 4 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 | « no previous file | chrome/test/data/save_page/frames-objects.htm » ('j') | 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 <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 773 matching lines...) Expand 10 before | Expand all | Expand 10 after
804 base::FilePath full_file_name, dir; 805 base::FilePath full_file_name, dir;
805 SaveCurrentTab(url, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML, 806 SaveCurrentTab(url, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML,
806 "iframe-src-is-a-download", 3, &dir, &full_file_name); 807 "iframe-src-is-a-download", 3, &dir, &full_file_name);
807 ASSERT_FALSE(HasFailure()); 808 ASSERT_FALSE(HasFailure());
808 809
809 EXPECT_TRUE(base::PathExists(full_file_name)); 810 EXPECT_TRUE(base::PathExists(full_file_name));
810 EXPECT_TRUE(base::PathExists(dir.AppendASCII("thisdayinhistory.html"))); 811 EXPECT_TRUE(base::PathExists(dir.AppendASCII("thisdayinhistory.html")));
811 EXPECT_TRUE(base::PathExists(dir.AppendASCII("no-such-file.html"))); 812 EXPECT_TRUE(base::PathExists(dir.AppendASCII("no-such-file.html")));
812 } 813 }
813 814
815 // Test that file: URI won't be saved when referred to from an HTTP page.
816 // See also https://crbug.com/616429.
817 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveUnauthorizedResource) {
818 GURL url = NavigateToMockURL("unauthorized-access");
819
820 // Create a test file (that the web page should not have access to).
821 base::ScopedTempDir temp_dir2;
822 ASSERT_TRUE(temp_dir2.CreateUniqueTempDir());
823 base::FilePath file_path =
824 temp_dir2.path().Append(FILE_PATH_LITERAL("should-not-save.jpg"));
825 std::string file_content("fake-jpg");
826 ASSERT_LT(
827 0, base::WriteFile(file_path, file_content.data(), file_content.size()));
828
829 // Refer to the test file from the test page.
830 GURL file_url = net::FilePathToFileURL(file_path);
831 ASSERT_TRUE(ExecuteScript(
832 browser()->tab_strip_model()->GetWebContentsAt(0),
833 base::StringPrintf("document.getElementById('resource1').src = '%s';",
834 file_url.spec().data())));
835
836 // Save the current page.
837 base::FilePath full_file_name, dir;
838 SaveCurrentTab(url, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML,
839 "unauthorized-access", 2, &dir, &full_file_name);
840
841 // We should not save resource that the web page didn't have access to.
842 // (because executing a resource request can have side effects - for example
843 // after https://crbug.com/590714 a website from the internet should not be
844 // able to issue a resource request to an intranet website and trigger
845 // server-side actions in the internet; this test uses a file: URI as a
846 // canary for detecting whether a website can access restricted resources).
847 EXPECT_FALSE(base::PathExists(dir.AppendASCII("should-not-save.jpg")));
848 }
849
814 // Test suite that allows testing --site-per-process against cross-site frames. 850 // Test suite that allows testing --site-per-process against cross-site frames.
815 // See http://dev.chromium.org/developers/design-documents/site-isolation. 851 // See http://dev.chromium.org/developers/design-documents/site-isolation.
816 class SavePageSitePerProcessBrowserTest : public SavePageBrowserTest { 852 class SavePageSitePerProcessBrowserTest : public SavePageBrowserTest {
817 public: 853 public:
818 SavePageSitePerProcessBrowserTest() {} 854 SavePageSitePerProcessBrowserTest() {}
819 855
820 protected: 856 protected:
821 void SetUpCommandLine(base::CommandLine* command_line) override { 857 void SetUpCommandLine(base::CommandLine* command_line) override {
822 SavePageBrowserTest::SetUpCommandLine(command_line); 858 SavePageBrowserTest::SetUpCommandLine(command_line);
823 859
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
1014 // [saved version of the page] as for the "before" 1050 // [saved version of the page] as for the "before"
1015 // [the original version of the page]. 1051 // [the original version of the page].
1016 ui_test_utils::NavigateToURL(browser(), 1052 ui_test_utils::NavigateToURL(browser(),
1017 GURL(net::FilePathToFileURL(full_file_name))); 1053 GURL(net::FilePathToFileURL(full_file_name)));
1018 DLOG(INFO) << "Verifying test expectations for saved page... : " 1054 DLOG(INFO) << "Verifying test expectations for saved page... : "
1019 << GetCurrentTab(browser())->GetLastCommittedURL(); 1055 << GetCurrentTab(browser())->GetLastCommittedURL();
1020 AssertExpectationsAboutCurrentTab(expected_number_of_frames, 1056 AssertExpectationsAboutCurrentTab(expected_number_of_frames,
1021 expected_substrings); 1057 expected_substrings);
1022 } 1058 }
1023 1059
1060 // Helper method to deduplicate some code across 2 tests.
1061 void RunObjectElementsTest(GURL url) {
1062 content::SavePageType save_page_type = GetParam();
1063
1064 // 7 comes from:
1065 // - main frame (frames-objects.htm)
1066 // - object with frame-nested.htm + 2 subframes (frames-nested2.htm + b.htm)
1067 // - iframe with a.htm
1068 // - object with svg.svg
1069 // - object with text.txt
1070 // (pdf and png objects do not get a separate frame)
1071 int expected_number_of_frames = 7;
1072
1073 std::string arr[] = {
1074 "frames-objects.htm: 8da13db4-a512-4d9b-b1c5-dc1c134234b9",
1075 "a.htm: 1b8aae2b-e164-462f-bd5b-98aa366205f2",
1076 "b.htm: 3a35f7fa-96a9-4487-9f18-4470263907fa",
1077 "frames-nested.htm: 4388232f-8d45-4d2e-9807-721b381be153",
1078 "frames-nested2.htm: 6d23dc47-f283-4977-96ec-66bcf72301a4",
1079 "text-object.txt: ae52dd09-9746-4b7e-86a6-6ada5e2680c2",
1080 };
1081 std::vector<std::string> expected_substrings(std::begin(arr),
1082 std::end(arr));
1083
1084 // TODO(lukasza): crbug.com/553478: Enable <object> testing of MHTML.
1085 if (save_page_type == content::SAVE_PAGE_TYPE_AS_MHTML)
1086 return;
1087
1088 TestOriginalVsSavedPage(save_page_type, url, expected_number_of_frames,
1089 expected_substrings);
1090 }
1091
1024 private: 1092 private:
1025 void AssertExpectationsAboutCurrentTab( 1093 void AssertExpectationsAboutCurrentTab(
1026 int expected_number_of_frames, 1094 int expected_number_of_frames,
1027 const std::vector<std::string>& expected_substrings) { 1095 const std::vector<std::string>& expected_substrings) {
1028 int actual_number_of_frames = 0; 1096 int actual_number_of_frames = 0;
1029 GetCurrentTab(browser())->ForEachFrame(base::Bind( 1097 GetCurrentTab(browser())->ForEachFrame(base::Bind(
1030 &IncrementInteger, base::Unretained(&actual_number_of_frames))); 1098 &IncrementInteger, base::Unretained(&actual_number_of_frames)));
1031 EXPECT_EQ(expected_number_of_frames, actual_number_of_frames); 1099 EXPECT_EQ(expected_number_of_frames, actual_number_of_frames);
1032 1100
1033 for (const auto& expected_substring : expected_substrings) { 1101 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)); 1151 std::vector<std::string> expected_substrings(std::begin(arr), std::end(arr));
1084 1152
1085 GURL url( 1153 GURL url(
1086 embedded_test_server()->GetURL("a.com", "/save_page/frames-xsite.htm")); 1154 embedded_test_server()->GetURL("a.com", "/save_page/frames-xsite.htm"));
1087 1155
1088 TestOriginalVsSavedPage(save_page_type, url, 3, expected_substrings); 1156 TestOriginalVsSavedPage(save_page_type, url, 3, expected_substrings);
1089 } 1157 }
1090 1158
1091 // Test compares original-vs-saved for a page with <object> elements. 1159 // Test compares original-vs-saved for a page with <object> elements.
1092 // (see crbug.com/553478). 1160 // (see crbug.com/553478).
1093 IN_PROC_BROWSER_TEST_P(SavePageOriginalVsSavedComparisonTest, ObjectElements) { 1161 IN_PROC_BROWSER_TEST_P(SavePageOriginalVsSavedComparisonTest,
1094 content::SavePageType save_page_type = GetParam(); 1162 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( 1163 GURL url(
1110 embedded_test_server()->GetURL("a.com", "/save_page/frames-objects.htm")); 1164 embedded_test_server()->GetURL("a.com", "/save_page/frames-objects.htm"));
1111 1165
1112 // TODO(lukasza): crbug.com/553478: Enable <object> testing of MHTML. 1166 RunObjectElementsTest(url);
1113 if (save_page_type == content::SAVE_PAGE_TYPE_AS_MHTML) 1167 }
1114 return;
1115 1168
1116 TestOriginalVsSavedPage(save_page_type, url, expected_number_of_frames, 1169 // Tests that saving a page from file: URI works.
1117 expected_substrings); 1170 IN_PROC_BROWSER_TEST_P(SavePageOriginalVsSavedComparisonTest,
1171 ObjectElementsViaFile) {
1172 base::FilePath test_data_dir;
1173 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir));
1174 GURL url(net::FilePathToFileURL(
1175 test_data_dir.Append(FILE_PATH_LITERAL("save_page/frames-objects.htm"))));
1176 EXPECT_TRUE(url.SchemeIsFile());
1177
1178 RunObjectElementsTest(url);
1118 } 1179 }
1119 1180
1120 // Test compares original-vs-saved for a page with frames at about:blank uri. 1181 // 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 1182 // This tests handling of iframe elements without src attribute (only with
1122 // srcdoc attribute) and how they get saved / cross-referenced. 1183 // srcdoc attribute) and how they get saved / cross-referenced.
1123 IN_PROC_BROWSER_TEST_P(SavePageOriginalVsSavedComparisonTest, AboutBlank) { 1184 IN_PROC_BROWSER_TEST_P(SavePageOriginalVsSavedComparisonTest, AboutBlank) {
1124 content::SavePageType save_page_type = GetParam(); 1185 content::SavePageType save_page_type = GetParam();
1125 1186
1126 std::string arr[] = { 1187 std::string arr[] = {
1127 "main: acb0609d-eb10-4c26-83e2-ad8afb7b0ff3", 1188 "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); 1316 TestOriginalVsSavedPage(save_page_type, url, 1, expected_substrings);
1256 } 1317 }
1257 1318
1258 INSTANTIATE_TEST_CASE_P( 1319 INSTANTIATE_TEST_CASE_P(
1259 SaveType, 1320 SaveType,
1260 SavePageOriginalVsSavedComparisonTest, 1321 SavePageOriginalVsSavedComparisonTest,
1261 ::testing::Values(content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML, 1322 ::testing::Values(content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML,
1262 content::SAVE_PAGE_TYPE_AS_MHTML)); 1323 content::SAVE_PAGE_TYPE_AS_MHTML));
1263 1324
1264 } // namespace 1325 } // namespace
OLDNEW
« no previous file with comments | « no previous file | chrome/test/data/save_page/frames-objects.htm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698