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

Side by Side Diff: chrome/browser/file_select_helper.cc

Issue 1409003002: [SafeBrowsing] Block dangerous unchecked downloads based on a Finch trial. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 2 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 "chrome/browser/file_select_helper.h" 5 #include "chrome/browser/file_select_helper.h"
6 6
7 #include <string> 7 #include <string>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 14 matching lines...) Expand all
25 #include "content/public/browser/notification_details.h" 25 #include "content/public/browser/notification_details.h"
26 #include "content/public/browser/notification_source.h" 26 #include "content/public/browser/notification_source.h"
27 #include "content/public/browser/notification_types.h" 27 #include "content/public/browser/notification_types.h"
28 #include "content/public/browser/render_view_host.h" 28 #include "content/public/browser/render_view_host.h"
29 #include "content/public/browser/render_widget_host.h" 29 #include "content/public/browser/render_widget_host.h"
30 #include "content/public/browser/render_widget_host_view.h" 30 #include "content/public/browser/render_widget_host_view.h"
31 #include "content/public/browser/storage_partition.h" 31 #include "content/public/browser/storage_partition.h"
32 #include "content/public/browser/web_contents.h" 32 #include "content/public/browser/web_contents.h"
33 #include "content/public/common/file_chooser_file_info.h" 33 #include "content/public/common/file_chooser_file_info.h"
34 #include "content/public/common/file_chooser_params.h" 34 #include "content/public/common/file_chooser_params.h"
35 #include "net/base/filename_util.h"
35 #include "net/base/mime_util.h" 36 #include "net/base/mime_util.h"
36 #include "ui/base/l10n/l10n_util.h" 37 #include "ui/base/l10n/l10n_util.h"
37 #include "ui/shell_dialogs/selected_file_info.h" 38 #include "ui/shell_dialogs/selected_file_info.h"
38 39
39 #if defined(OS_CHROMEOS) 40 #if defined(OS_CHROMEOS)
40 #include "chrome/browser/chromeos/file_manager/fileapi_util.h" 41 #include "chrome/browser/chromeos/file_manager/fileapi_util.h"
41 #include "content/public/browser/site_instance.h" 42 #include "content/public/browser/site_instance.h"
42 #endif 43 #endif
43 44
45 #if defined(FULL_SAFE_BROWSING)
46 #include "chrome/browser/safe_browsing/unverified_download_policy.h"
47 #endif
48
44 using content::BrowserThread; 49 using content::BrowserThread;
45 using content::FileChooserParams; 50 using content::FileChooserParams;
46 using content::RenderViewHost; 51 using content::RenderViewHost;
47 using content::RenderWidgetHost; 52 using content::RenderWidgetHost;
48 using content::WebContents; 53 using content::WebContents;
49 54
50 namespace { 55 namespace {
51 56
52 // There is only one file-selection happening at any given time, 57 // There is only one file-selection happening at any given time,
53 // so we allocate an enumeration ID for that purpose. All IDs from 58 // so we allocate an enumeration ID for that purpose. All IDs from
(...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after
427 select_file_types_ = GetFileTypesFromAcceptType(params.accept_types); 432 select_file_types_ = GetFileTypesFromAcceptType(params.accept_types);
428 select_file_types_->support_drive = !params.need_local_path; 433 select_file_types_->support_drive = !params.need_local_path;
429 434
430 BrowserThread::PostTask( 435 BrowserThread::PostTask(
431 BrowserThread::UI, FROM_HERE, 436 BrowserThread::UI, FROM_HERE,
432 base::Bind(&FileSelectHelper::RunFileChooserOnUIThread, this, params)); 437 base::Bind(&FileSelectHelper::RunFileChooserOnUIThread, this, params));
433 } 438 }
434 439
435 void FileSelectHelper::RunFileChooserOnUIThread( 440 void FileSelectHelper::RunFileChooserOnUIThread(
436 const FileChooserParams& params) { 441 const FileChooserParams& params) {
442 DCHECK_IMPLIES(!params.default_file_name.empty(),
sky 2015/10/21 22:46:19 Please don't use DCHECK_IMPLIES. Yes, it is in bas
asanka 2015/10/22 21:55:12 Done :), and point taken.
443 params.mode == FileChooserParams::Save)
444 << "The default_file_name parameter should only be specified for Save "
445 "file choosers";
446 DCHECK(params.default_file_name == params.default_file_name.BaseName())
447 << "The default_file_name parameter should not contain path separators";
448
437 if (!render_view_host_ || !web_contents_ || !IsValidProfile(profile_)) { 449 if (!render_view_host_ || !web_contents_ || !IsValidProfile(profile_)) {
438 // If the renderer was destroyed before we started, just cancel the 450 // If the renderer was destroyed before we started, just cancel the
439 // operation. 451 // operation.
440 RunFileChooserEnd(); 452 RunFileChooserEnd();
441 return; 453 return;
442 } 454 }
443 455
456 // The parameters contain potentially unsafe filenames provided by the
sky 2015/10/21 22:46:20 I think you should move this to a standalone funct
asanka 2015/10/22 21:55:12 Done and added a test to FileSelectHelpTest unit t
457 // renderer. They should be sanitized before being used in a file selection
458 // dialog. Also, the subsequent file type blocking checks rely on the file
459 // extension being valid and significant (e.g. no trailing periods on OS_WIN
460 // which disappear once the file is written).
461 base::FilePath default_file_name = net::GenerateFileName(
462 GURL(), std::string(), std::string(),
463 params.default_file_name.AsUTF8Unsafe(), std::string(), std::string());
464 base::FilePath default_file_path =
465 profile_->last_selected_directory().Append(default_file_name);
466
467 #if defined(FULL_SAFE_BROWSING)
468 if (params.mode == FileChooserParams::Save &&
469 !safe_browsing::IsUnverifiedDownloadAllowed(
470 default_file_path)) {
471 NotifyRenderViewHostAndEnd(std::vector<ui::SelectedFileInfo>());
472 return;
473 }
474 #endif
475
444 select_file_dialog_ = ui::SelectFileDialog::Create( 476 select_file_dialog_ = ui::SelectFileDialog::Create(
445 this, new ChromeSelectFilePolicy(web_contents_)); 477 this, new ChromeSelectFilePolicy(web_contents_));
446 if (!select_file_dialog_.get()) 478 if (!select_file_dialog_.get())
447 return; 479 return;
448 480
449 dialog_mode_ = params.mode; 481 dialog_mode_ = params.mode;
450 switch (params.mode) { 482 switch (params.mode) {
451 case FileChooserParams::Open: 483 case FileChooserParams::Open:
452 dialog_type_ = ui::SelectFileDialog::SELECT_OPEN_FILE; 484 dialog_type_ = ui::SelectFileDialog::SELECT_OPEN_FILE;
453 break; 485 break;
454 case FileChooserParams::OpenMultiple: 486 case FileChooserParams::OpenMultiple:
455 dialog_type_ = ui::SelectFileDialog::SELECT_OPEN_MULTI_FILE; 487 dialog_type_ = ui::SelectFileDialog::SELECT_OPEN_MULTI_FILE;
456 break; 488 break;
457 case FileChooserParams::UploadFolder: 489 case FileChooserParams::UploadFolder:
458 dialog_type_ = ui::SelectFileDialog::SELECT_UPLOAD_FOLDER; 490 dialog_type_ = ui::SelectFileDialog::SELECT_UPLOAD_FOLDER;
459 break; 491 break;
460 case FileChooserParams::Save: 492 case FileChooserParams::Save:
461 dialog_type_ = ui::SelectFileDialog::SELECT_SAVEAS_FILE; 493 dialog_type_ = ui::SelectFileDialog::SELECT_SAVEAS_FILE;
462 break; 494 break;
463 default: 495 default:
464 // Prevent warning. 496 // Prevent warning.
465 dialog_type_ = ui::SelectFileDialog::SELECT_OPEN_FILE; 497 dialog_type_ = ui::SelectFileDialog::SELECT_OPEN_FILE;
466 NOTREACHED(); 498 NOTREACHED();
467 } 499 }
468 500
469 base::FilePath default_file_name = params.default_file_name.IsAbsolute() ?
470 params.default_file_name :
471 profile_->last_selected_directory().Append(params.default_file_name);
472
473 gfx::NativeWindow owning_window = platform_util::GetTopLevel( 501 gfx::NativeWindow owning_window = platform_util::GetTopLevel(
474 render_view_host_->GetWidget()->GetView()->GetNativeView()); 502 render_view_host_->GetWidget()->GetView()->GetNativeView());
475 503
476 #if defined(OS_ANDROID) 504 #if defined(OS_ANDROID)
477 // Android needs the original MIME types and an additional capture value. 505 // Android needs the original MIME types and an additional capture value.
478 std::pair<std::vector<base::string16>, bool> accept_types = 506 std::pair<std::vector<base::string16>, bool> accept_types =
479 std::make_pair(params.accept_types, params.capture); 507 std::make_pair(params.accept_types, params.capture);
480 #endif 508 #endif
481 509
482 select_file_dialog_->SelectFile( 510 select_file_dialog_->SelectFile(
483 dialog_type_, 511 dialog_type_, params.title, default_file_path, select_file_types_.get(),
484 params.title,
485 default_file_name,
486 select_file_types_.get(),
487 select_file_types_.get() && !select_file_types_->extensions.empty() 512 select_file_types_.get() && !select_file_types_->extensions.empty()
488 ? 1 513 ? 1
489 : 0, // 1-based index of default extension to show. 514 : 0, // 1-based index of default extension to show.
490 base::FilePath::StringType(), 515 base::FilePath::StringType(),
491 owning_window, 516 owning_window,
492 #if defined(OS_ANDROID) 517 #if defined(OS_ANDROID)
493 &accept_types); 518 &accept_types);
494 #else 519 #else
495 NULL); 520 NULL);
496 #endif 521 #endif
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
565 // of an extension or a "/" in the case of a MIME type). 590 // of an extension or a "/" in the case of a MIME type).
566 std::string unused; 591 std::string unused;
567 if (accept_type.length() <= 1 || 592 if (accept_type.length() <= 1 ||
568 base::ToLowerASCII(accept_type) != accept_type || 593 base::ToLowerASCII(accept_type) != accept_type ||
569 base::TrimWhitespaceASCII(accept_type, base::TRIM_ALL, &unused) != 594 base::TrimWhitespaceASCII(accept_type, base::TRIM_ALL, &unused) !=
570 base::TRIM_NONE) { 595 base::TRIM_NONE) {
571 return false; 596 return false;
572 } 597 }
573 return true; 598 return true;
574 } 599 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698