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

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

Issue 438283002: Sanitize referrer in context menus. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Change SerializedNavigationEntry::Sanitize and add extra check. Created 6 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 | Annotate | Revision Log
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 <sstream> 5 #include <sstream>
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/file_util.h" 10 #include "base/file_util.h"
11 #include "base/files/file.h" 11 #include "base/files/file.h"
12 #include "base/files/file_path.h" 12 #include "base/files/file_path.h"
13 #include "base/files/scoped_temp_dir.h" 13 #include "base/files/scoped_temp_dir.h"
14 #include "base/memory/ref_counted.h" 14 #include "base/memory/ref_counted.h"
15 #include "base/path_service.h" 15 #include "base/path_service.h"
16 #include "base/prefs/pref_service.h" 16 #include "base/prefs/pref_service.h"
17 #include "base/stl_util.h" 17 #include "base/stl_util.h"
18 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/string_split.h" 19 #include "base/strings/string_split.h"
19 #include "base/strings/string_util.h" 20 #include "base/strings/string_util.h"
20 #include "base/strings/stringprintf.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/sys_info.h" 23 #include "base/sys_info.h"
23 #include "base/test/test_file_util.h" 24 #include "base/test/test_file_util.h"
24 #include "chrome/app/chrome_command_ids.h" 25 #include "chrome/app/chrome_command_ids.h"
25 #include "chrome/browser/browser_process.h" 26 #include "chrome/browser/browser_process.h"
26 #include "chrome/browser/chrome_notification_types.h" 27 #include "chrome/browser/chrome_notification_types.h"
27 #include "chrome/browser/download/chrome_download_manager_delegate.h" 28 #include "chrome/browser/download/chrome_download_manager_delegate.h"
28 #include "chrome/browser/download/download_browsertest.h" 29 #include "chrome/browser/download/download_browsertest.h"
29 #include "chrome/browser/download/download_crx_util.h" 30 #include "chrome/browser/download/download_crx_util.h"
30 #include "chrome/browser/download/download_history.h" 31 #include "chrome/browser/download/download_history.h"
31 #include "chrome/browser/download/download_item_model.h" 32 #include "chrome/browser/download/download_item_model.h"
32 #include "chrome/browser/download/download_prefs.h" 33 #include "chrome/browser/download/download_prefs.h"
33 #include "chrome/browser/download/download_request_limiter.h" 34 #include "chrome/browser/download/download_request_limiter.h"
34 #include "chrome/browser/download/download_service.h" 35 #include "chrome/browser/download/download_service.h"
35 #include "chrome/browser/download/download_service_factory.h" 36 #include "chrome/browser/download/download_service_factory.h"
36 #include "chrome/browser/download/download_shelf.h" 37 #include "chrome/browser/download/download_shelf.h"
37 #include "chrome/browser/download/download_target_determiner.h" 38 #include "chrome/browser/download/download_target_determiner.h"
38 #include "chrome/browser/download/download_test_file_activity_observer.h" 39 #include "chrome/browser/download/download_test_file_activity_observer.h"
39 #include "chrome/browser/extensions/extension_install_prompt.h" 40 #include "chrome/browser/extensions/extension_install_prompt.h"
40 #include "chrome/browser/extensions/extension_service.h" 41 #include "chrome/browser/extensions/extension_service.h"
41 #include "chrome/browser/history/download_row.h" 42 #include "chrome/browser/history/download_row.h"
42 #include "chrome/browser/history/history_service.h" 43 #include "chrome/browser/history/history_service.h"
43 #include "chrome/browser/history/history_service_factory.h" 44 #include "chrome/browser/history/history_service_factory.h"
44 #include "chrome/browser/infobars/infobar_service.h" 45 #include "chrome/browser/infobars/infobar_service.h"
45 #include "chrome/browser/net/url_request_mock_util.h" 46 #include "chrome/browser/net/url_request_mock_util.h"
46 #include "chrome/browser/profiles/profile.h" 47 #include "chrome/browser/profiles/profile.h"
48 #include "chrome/browser/renderer_context_menu/render_view_context_menu_browsert est_util.h"
47 #include "chrome/browser/renderer_context_menu/render_view_context_menu_test_uti l.h" 49 #include "chrome/browser/renderer_context_menu/render_view_context_menu_test_uti l.h"
48 #include "chrome/browser/safe_browsing/download_feedback_service.h" 50 #include "chrome/browser/safe_browsing/download_feedback_service.h"
49 #include "chrome/browser/safe_browsing/download_protection_service.h" 51 #include "chrome/browser/safe_browsing/download_protection_service.h"
50 #include "chrome/browser/safe_browsing/safe_browsing_service.h" 52 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
51 #include "chrome/browser/ui/browser.h" 53 #include "chrome/browser/ui/browser.h"
52 #include "chrome/browser/ui/browser_commands.h" 54 #include "chrome/browser/ui/browser_commands.h"
53 #include "chrome/browser/ui/browser_finder.h" 55 #include "chrome/browser/ui/browser_finder.h"
54 #include "chrome/browser/ui/browser_list.h" 56 #include "chrome/browser/ui/browser_list.h"
55 #include "chrome/browser/ui/browser_tabstrip.h" 57 #include "chrome/browser/ui/browser_tabstrip.h"
56 #include "chrome/browser/ui/browser_window.h" 58 #include "chrome/browser/ui/browser_window.h"
(...skipping 2686 matching lines...) Expand 10 before | Expand all | Expand 10 after
2743 ASSERT_EQ(1u, download_items.size()); 2745 ASSERT_EQ(1u, download_items.size());
2744 ASSERT_EQ(test_server()->GetURL("echoheader?Referer"), 2746 ASSERT_EQ(test_server()->GetURL("echoheader?Referer"),
2745 download_items[0]->GetOriginalUrl()); 2747 download_items[0]->GetOriginalUrl());
2746 2748
2747 // Check that the file contains the expected referrer. 2749 // Check that the file contains the expected referrer.
2748 base::FilePath file(download_items[0]->GetTargetFilePath()); 2750 base::FilePath file(download_items[0]->GetTargetFilePath());
2749 std::string expected_contents = test_server()->GetURL(std::string()).spec(); 2751 std::string expected_contents = test_server()->GetURL(std::string()).spec();
2750 ASSERT_TRUE(VerifyFile(file, expected_contents, expected_contents.length())); 2752 ASSERT_TRUE(VerifyFile(file, expected_contents, expected_contents.length()));
2751 } 2753 }
2752 2754
2755 // This test ensures that the Referer header is properly sanitized when
2756 // Save Link As is chosen from the context menu.
2757 IN_PROC_BROWSER_TEST_F(DownloadTest, SaveLinkAsReferrerPolicyOrigin) {
2758 // Do initial setup.
2759 ASSERT_TRUE(test_server()->Start());
2760 net::SpawnedTestServer ssl_test_server(
2761 net::SpawnedTestServer::TYPE_HTTPS,
2762 net::SpawnedTestServer::kLocalhost,
2763 base::FilePath(FILE_PATH_LITERAL("chrome/test/data/referrer_policy")));
2764 ASSERT_TRUE(ssl_test_server.Start());
2765 EnableFileChooser(true);
2766 std::vector<DownloadItem*> download_items;
2767 GetDownloads(browser(), &download_items);
2768 ASSERT_TRUE(download_items.empty());
2769
2770 // Navigate to the initial page, where Save Link As will be executed.
2771 GURL url = ssl_test_server.GetURL(
2772 std::string("files/referrer-policy-start.html?policy=origin") +
2773 "&port=" + base::IntToString(test_server()->host_port_pair().port()) +
2774 "&ssl_port=" +
2775 base::IntToString(ssl_test_server.host_port_pair().port()) +
2776 "&redirect=echoheader&link=true&target=");
2777 ASSERT_TRUE(url.is_valid());
2778 ui_test_utils::NavigateToURL(browser(), url);
2779
2780 scoped_ptr<content::DownloadTestObserver> waiter(
2781 new content::DownloadTestObserverTerminal(
2782 DownloadManagerForBrowser(browser()), 1,
2783 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL));
2784
2785 // Right-click on the link and choose Save Link As. This will download the
2786 // link target.
2787 ContextMenuNotificationObserver context_menu_observer(
2788 IDC_CONTENT_CONTEXT_SAVELINKAS);
2789
2790 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
2791 blink::WebMouseEvent mouse_event;
2792 mouse_event.type = blink::WebInputEvent::MouseDown;
2793 mouse_event.button = blink::WebMouseEvent::ButtonRight;
2794 mouse_event.x = 15;
2795 mouse_event.y = 15;
2796 mouse_event.clickCount = 1;
2797 tab->GetRenderViewHost()->ForwardMouseEvent(mouse_event);
2798 mouse_event.type = blink::WebInputEvent::MouseUp;
2799 tab->GetRenderViewHost()->ForwardMouseEvent(mouse_event);
2800
2801 waiter->WaitForFinished();
2802 EXPECT_EQ(1u, waiter->NumDownloadsSeenInState(DownloadItem::COMPLETE));
2803 CheckDownloadStates(1, DownloadItem::COMPLETE);
2804
2805 // Validate that the correct file was downloaded.
2806 GetDownloads(browser(), &download_items);
2807 EXPECT_EQ(1u, download_items.size());
2808 EXPECT_EQ(test_server()->GetURL("echoheader?Referer"),
2809 download_items[0]->GetOriginalUrl());
2810
2811 // Check that the file contains the expected referrer.
2812 base::FilePath file(download_items[0]->GetTargetFilePath());
2813 std::string expected_contents = ssl_test_server.GetURL(std::string()).spec();
2814 EXPECT_TRUE(VerifyFile(file, expected_contents, expected_contents.length()));
2815 }
2816
2817 // This test ensures that the Referer header is properly sanitized when
2818 // Save Image As is chosen from the context menu. The test succeeds if
2819 // it doesn't crash.
2820 IN_PROC_BROWSER_TEST_F(DownloadTest, SaveImageAsReferrerPolicyDefault) {
2821 // Do initial setup.
2822 ASSERT_TRUE(test_server()->Start());
2823 net::SpawnedTestServer ssl_test_server(
2824 net::SpawnedTestServer::TYPE_HTTPS,
2825 net::SpawnedTestServer::kLocalhost,
2826 base::FilePath(FILE_PATH_LITERAL("chrome/test/data/")));
2827 ASSERT_TRUE(ssl_test_server.Start());
2828 EnableFileChooser(true);
2829 std::vector<DownloadItem*> download_items;
2830 GetDownloads(browser(), &download_items);
2831 ASSERT_TRUE(download_items.empty());
2832
2833 GURL url = ssl_test_server.GetURL("files/title1.html");
2834 GURL img_url = test_server()->GetURL("files/downloads/image.jpg");
2835 ASSERT_TRUE(url.is_valid());
2836 ui_test_utils::NavigateToURL(browser(), url);
2837
2838 // Try to download an image via a context menu.
2839 scoped_ptr<content::DownloadTestObserver> waiter_context_menu(
2840 new content::DownloadTestObserverTerminal(
2841 DownloadManagerForBrowser(browser()), 1,
2842 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL));
2843 content::ContextMenuParams context_menu_params;
2844 context_menu_params.media_type = blink::WebContextMenuData::MediaTypeImage;
2845 context_menu_params.page_url = url;
2846 context_menu_params.src_url = img_url;
2847 TestRenderViewContextMenu menu(
2848 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(),
2849 context_menu_params);
2850 menu.Init();
2851 menu.ExecuteCommand(IDC_CONTENT_CONTEXT_SAVEIMAGEAS, 0);
2852 waiter_context_menu->WaitForFinished();
2853 EXPECT_EQ(
2854 1u, waiter_context_menu->NumDownloadsSeenInState(DownloadItem::COMPLETE));
2855 CheckDownloadStates(1, DownloadItem::COMPLETE);
2856
2857 // Validate that the correct file was downloaded via the context menu.
2858 download_items.clear();
2859 GetDownloads(browser(), &download_items);
2860 EXPECT_TRUE(DidShowFileChooser());
2861 ASSERT_EQ(1u, download_items.size());
2862 ASSERT_EQ(img_url, download_items[0]->GetOriginalUrl());
2863 }
2864
2753 IN_PROC_BROWSER_TEST_F(DownloadTest, HiddenDownload) { 2865 IN_PROC_BROWSER_TEST_F(DownloadTest, HiddenDownload) {
2754 base::FilePath file(FILE_PATH_LITERAL("download-test1.lib")); 2866 base::FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
2755 GURL url(URLRequestMockHTTPJob::GetMockUrl(file)); 2867 GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
2756 2868
2757 DownloadManager* download_manager = DownloadManagerForBrowser(browser()); 2869 DownloadManager* download_manager = DownloadManagerForBrowser(browser());
2758 scoped_ptr<content::DownloadTestObserver> observer( 2870 scoped_ptr<content::DownloadTestObserver> observer(
2759 new content::DownloadTestObserverTerminal( 2871 new content::DownloadTestObserverTerminal(
2760 download_manager, 2872 download_manager,
2761 1, 2873 1,
2762 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL)); 2874 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL));
(...skipping 516 matching lines...) Expand 10 before | Expand all | Expand 10 after
3279 *(downloads[0]))); 3391 *(downloads[0])));
3280 3392
3281 // Begin feedback and check that the file is "stolen". 3393 // Begin feedback and check that the file is "stolen".
3282 download_protection_service->feedback_service()->BeginFeedbackForDownload( 3394 download_protection_service->feedback_service()->BeginFeedbackForDownload(
3283 downloads[0]); 3395 downloads[0]);
3284 std::vector<DownloadItem*> updated_downloads; 3396 std::vector<DownloadItem*> updated_downloads;
3285 GetDownloads(browser(), &updated_downloads); 3397 GetDownloads(browser(), &updated_downloads);
3286 ASSERT_TRUE(updated_downloads.empty()); 3398 ASSERT_TRUE(updated_downloads.empty());
3287 } 3399 }
3288 #endif 3400 #endif
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/referrer_policy_browsertest.cc » ('j') | components/sessions/serialized_navigation_entry.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698