1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146

content / browser / renderer_host / blocked_scheme_navigation_throttle.cc [blame]

// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/renderer_host/blocked_scheme_navigation_throttle.h"

#include "base/feature_list.h"
#include "base/strings/stringprintf.h"
#include "build/build_config.h"
#include "content/browser/renderer_host/frame_tree.h"
#include "content/browser/renderer_host/frame_tree_node.h"
#include "content/browser/renderer_host/navigation_request.h"
#include "content/common/features.h"
#include "content/common/navigation_params_utils.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/render_frame_host.h"
#include "storage/browser/file_system/external_mount_points.h"
#include "storage/browser/file_system/file_system_url.h"
#include "storage/common/file_system/file_system_util.h"
#include "third_party/blink/public/mojom/devtools/console_message.mojom.h"
#include "url/url_constants.h"

namespace content {

namespace {
const char kConsoleError[] = "Not allowed to navigate top frame to %s URL: %s";
const char kAnyFrameConsoleError[] = "Not allowed to navigate to %s URL: %s";

bool IsExternalMountedFile(const GURL& url) {
  storage::FileSystemURL file_system_url =
      storage::ExternalMountPoints::GetSystemInstance()->CrackURL(
          url, blink::StorageKey::CreateFirstParty(url::Origin::Create(url)));
  return file_system_url.is_valid();
}
}

BlockedSchemeNavigationThrottle::BlockedSchemeNavigationThrottle(
    NavigationHandle* navigation_handle)
    : NavigationThrottle(navigation_handle) {}

BlockedSchemeNavigationThrottle::~BlockedSchemeNavigationThrottle() {}

NavigationThrottle::ThrottleCheckResult
BlockedSchemeNavigationThrottle::WillStartRequest() {
  NavigationRequest* request = NavigationRequest::From(navigation_handle());
  if (!request->GetURL().SchemeIs(url::kFileSystemScheme))
    return PROCEED;

  if (base::FeatureList::IsEnabled(blink::features::kFileSystemUrlNavigation))
    return PROCEED;

  RenderFrameHost* top_frame =
      request->frame_tree_node()->frame_tree().root()->current_frame_host();
  BrowserContext* browser_context = top_frame->GetBrowserContext();

  if (base::FeatureList::IsEnabled(
          blink::features::kFileSystemUrlNavigationForChromeAppsOnly) &&
      !IsExternalMountedFile(request->GetURL()) &&
      (url::Origin::Create(request->GetURL()) ==
       request->GetInitiatorOrigin()) &&
      GetContentClient()->browser()->IsFileSystemURLNavigationAllowed(
          browser_context, request->GetURL())) {
    return PROCEED;
  }

  top_frame->AddMessageToConsole(
      blink::mojom::ConsoleMessageLevel::kError,
      base::StringPrintf(kAnyFrameConsoleError,
                         request->GetURL().scheme().c_str(),
                         request->GetURL().spec().c_str()));

  return CANCEL;
}

NavigationThrottle::ThrottleCheckResult
BlockedSchemeNavigationThrottle::WillProcessResponse() {
  NavigationRequest* request = NavigationRequest::From(navigation_handle());
  if (request->IsDownload())
    return PROCEED;

  RenderFrameHost* top_frame =
      request->frame_tree_node()->frame_tree().root()->current_frame_host();
  top_frame->AddMessageToConsole(
      blink::mojom::ConsoleMessageLevel::kError,
      base::StringPrintf(kConsoleError, request->GetURL().scheme().c_str(),
                         request->GetURL().spec().c_str()));
  return CANCEL;
}

const char* BlockedSchemeNavigationThrottle::GetNameForLogging() {
  return "BlockedSchemeNavigationThrottle";
}

// static
std::unique_ptr<NavigationThrottle>
BlockedSchemeNavigationThrottle::CreateThrottleForNavigation(
    NavigationHandle* navigation_handle) {
  NavigationRequest* request = NavigationRequest::From(navigation_handle);
  // Create throttles when going to blocked schemes via renderer-initiated
  // navigations (which are cross-document in the main frame). Note that history
  // navigations can bypass this, because the blocked scheme must have
  // originally committed in a permitted case (e.g., omnibox navigation).
  if (request->IsInMainFrame() && request->IsRendererInitiated() &&
      !request->IsSameDocument() &&
      !NavigationTypeUtils::IsHistory(
          request->common_params().navigation_type) &&
      (request->GetURL().SchemeIs(url::kDataScheme) ||
       request->GetURL().SchemeIs(url::kFileSystemScheme)) &&
      !base::FeatureList::IsEnabled(
          features::kAllowContentInitiatedDataUrlNavigations)) {
    return std::make_unique<BlockedSchemeNavigationThrottle>(request);
  }
  // Block all renderer initiated navigations to filesystem: URLs except for
  // when explicitly allowed by the embedder. These won't load anyway since no
  // URL Loader exists for them, but the throttle lets us add a message to the
  // console.
  RenderFrameHost* current_frame_host =
      request->frame_tree_node()->current_frame_host();
  BrowserContext* browser_context = current_frame_host->GetBrowserContext();
  // A navigation is permitted if the relevant feature flag is enabled, the
  // request origin is equivalent to the initiator origin, and the embedder
  // explicitly allows it.
  bool is_navigation_allowed =
      base::FeatureList::IsEnabled(
          blink::features::kFileSystemUrlNavigationForChromeAppsOnly) &&
      (url::Origin::Create(request->GetURL()) ==
       request->GetInitiatorOrigin()) &&
      GetContentClient()->browser()->IsFileSystemURLNavigationAllowed(
          browser_context, request->GetURL());
  if (!is_navigation_allowed &&
      !base::FeatureList::IsEnabled(
          blink::features::kFileSystemUrlNavigation) &&
      request->IsRendererInitiated() &&
      request->GetURL().SchemeIs(url::kFileSystemScheme)) {
    return std::make_unique<BlockedSchemeNavigationThrottle>(request);
  }
  // Block any external mounted files.
  if (!base::FeatureList::IsEnabled(
          blink::features::kFileSystemUrlNavigation) &&
      IsExternalMountedFile(request->GetURL())) {
    return std::make_unique<BlockedSchemeNavigationThrottle>(request);
  }
  return nullptr;
}

}  // namespace content