Skip to content

Commit

Permalink
[COEP] Move BlockedByResponseReason enum to mojo
Browse files Browse the repository at this point in the history
This moves the enum BlockedByResponseReason to a mojo definition,
which makes it easily usable for DevTools' inspector issue machinery.

Bug: chromium:1051466
Change-Id: I65c4957721d076b258674043ecda55e037507ff8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2159223
Reviewed-by: Kinuko Yasuda <kinuko@chromium.org>
Commit-Queue: Sigurd Schneider <sigurds@chromium.org>
Cr-Commit-Position: refs/heads/master@{#761844}
  • Loading branch information
sigurdschneider authored and Commit Bot committed Apr 23, 2020
1 parent 9c288bd commit b1e6dfe
Show file tree
Hide file tree
Showing 19 changed files with 76 additions and 73 deletions.
11 changes: 6 additions & 5 deletions content/browser/devtools/protocol/network_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1532,20 +1532,21 @@ Maybe<String> GetBlockedReasonFor(
const network::URLLoaderCompletionStatus& status) {
if (status.blocked_by_response_reason) {
switch (*status.blocked_by_response_reason) {
case network::BlockedByResponseReason::kCoepFrameResourceNeedsCoepHeader:
case network::mojom::BlockedByResponseReason::
kCoepFrameResourceNeedsCoepHeader:
return {protocol::Network::BlockedReasonEnum::
CoepFrameResourceNeedsCoepHeader};
case network::BlockedByResponseReason::
case network::mojom::BlockedByResponseReason::
kCoopSandboxedIFrameCannotNavigateToCoopPage:
return {protocol::Network::BlockedReasonEnum::
CoopSandboxedIframeCannotNavigateToCoopPage};
case network::BlockedByResponseReason::
case network::mojom::BlockedByResponseReason::
kCorpNotSameOriginAfterDefaultedToSameOriginByCoep:
return {protocol::Network::BlockedReasonEnum::
CorpNotSameOriginAfterDefaultedToSameOriginByCoep};
case network::BlockedByResponseReason::kCorpNotSameOrigin:
case network::mojom::BlockedByResponseReason::kCorpNotSameOrigin:
return {protocol::Network::BlockedReasonEnum::CorpNotSameOrigin};
case network::BlockedByResponseReason::kCorpNotSameSite:
case network::mojom::BlockedByResponseReason::kCorpNotSameSite:
return {protocol::Network::BlockedReasonEnum::CorpNotSameSite};
}
NOTREACHED();
Expand Down
6 changes: 3 additions & 3 deletions content/browser/frame_host/navigation_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1901,7 +1901,7 @@ void NavigationRequest::OnResponseStarted(
/*report_only=*/false);
}
OnRequestFailedInternal(network::URLLoaderCompletionStatus(
network::BlockedByResponseReason::
network::mojom::BlockedByResponseReason::
kCoepFrameResourceNeedsCoepHeader),
false /* skip_throttles */,
base::nullopt /* error_page_content */,
Expand Down Expand Up @@ -1933,7 +1933,7 @@ void NavigationRequest::OnResponseStarted(
network::mojom::WebSandboxFlags::kNone)) {
OnRequestFailedInternal(
network::URLLoaderCompletionStatus(
network::BlockedByResponseReason::
network::mojom::BlockedByResponseReason::
kCoopSandboxedIFrameCannotNavigateToCoopPage),
false /* skip_throttles */, base::nullopt /* error_page_content */,
false /* collapse_frame */);
Expand Down Expand Up @@ -4130,7 +4130,7 @@ void NavigationRequest::ForceEnableOriginTrials(
commit_params_->force_enabled_origin_trials = trials;
}

base::Optional<network::BlockedByResponseReason>
base::Optional<network::mojom::BlockedByResponseReason>
NavigationRequest::IsBlockedByCorp() {
if (!base::FeatureList::IsEnabled(
network::features::kCrossOriginEmbedderPolicy)) {
Expand Down
4 changes: 2 additions & 2 deletions content/browser/frame_host/navigation_request.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
#include "net/base/proxy_server.h"
#include "net/dns/public/resolve_error_info.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
#include "services/network/public/cpp/blocked_by_response_reason.h"
#include "services/network/public/cpp/origin_policy.h"
#include "services/network/public/mojom/blocked_by_response_reason.mojom-shared.h"

#if defined(OS_ANDROID)
#include "base/android/scoped_java_ref.h"
Expand Down Expand Up @@ -895,7 +895,7 @@ class CONTENT_EXPORT NavigationRequest

void CreateCoepReporter(StoragePartition* storage_partition);

base::Optional<network::BlockedByResponseReason> IsBlockedByCorp();
base::Optional<network::mojom::BlockedByResponseReason> IsBlockedByCorp();

FrameTreeNode* frame_tree_node_;

Expand Down
1 change: 0 additions & 1 deletion services/network/public/cpp/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ jumbo_component("cpp_base") {
output_name = "network_cpp_base"

sources = [
"blocked_by_response_reason.h",
"cors/cors_error_status.cc",
"cors/cors_error_status.h",
"cross_origin_embedder_policy.cc",
Expand Down
33 changes: 18 additions & 15 deletions services/network/public/cpp/cross_origin_resource_policy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,23 @@ enum class CorpResult {
kMaxValue = kNotSameSite,
};

CorpResult ToCorpResult(const base::Optional<BlockedByResponseReason>& value) {
CorpResult ToCorpResult(
const base::Optional<mojom::BlockedByResponseReason>& value) {
if (!value) {
return CorpResult::kSuccess;
}
switch (*value) {
case BlockedByResponseReason::kCoepFrameResourceNeedsCoepHeader:
case BlockedByResponseReason::kCoopSandboxedIFrameCannotNavigateToCoopPage:
case mojom::BlockedByResponseReason::kCoepFrameResourceNeedsCoepHeader:
case mojom::BlockedByResponseReason::
kCoopSandboxedIFrameCannotNavigateToCoopPage:
NOTREACHED();
return CorpResult::kSuccess;
case BlockedByResponseReason::kCorpNotSameOrigin:
case mojom::BlockedByResponseReason::kCorpNotSameOrigin:
return CorpResult::kNotSameOrigin;
case BlockedByResponseReason::
case mojom::BlockedByResponseReason::
kCorpNotSameOriginAfterDefaultedToSameOriginByCoep:
return CorpResult::kNotSameOriginAfterDefaultedToSameOriginByCoep;
case BlockedByResponseReason::kCorpNotSameSite:
case mojom::BlockedByResponseReason::kCorpNotSameSite:
return CorpResult::kNotSameSite;
}
}
Expand Down Expand Up @@ -136,7 +138,7 @@ bool ShouldAllowSameSite(const url::Origin& initiator,
target_origin.scheme() != url::kHttpsScheme;
}

base::Optional<BlockedByResponseReason> IsBlockedInternal(
base::Optional<mojom::BlockedByResponseReason> IsBlockedInternal(
CrossOriginResourcePolicy::ParsedHeader policy,
const GURL& request_url,
const base::Optional<url::Origin>& request_initiator,
Expand Down Expand Up @@ -178,9 +180,9 @@ base::Optional<BlockedByResponseReason> IsBlockedInternal(
// > 4. If policy is `same-origin`, then return blocked.
if (policy == CrossOriginResourcePolicy::kSameOrigin) {
return upgrade_to_same_origin
? BlockedByResponseReason::
? mojom::BlockedByResponseReason::
kCorpNotSameOriginAfterDefaultedToSameOriginByCoep
: BlockedByResponseReason::kCorpNotSameOrigin;
: mojom::BlockedByResponseReason::kCorpNotSameOrigin;
}

// From https://fetch.spec.whatwg.org/#cross-origin-resource-policy-header:
Expand All @@ -196,10 +198,10 @@ base::Optional<BlockedByResponseReason> IsBlockedInternal(
// From https://fetch.spec.whatwg.org/#cross-origin-resource-policy-header:
// > 6. If policy is `same-site`, then return blocked.
DCHECK_EQ(CrossOriginResourcePolicy::kSameSite, policy);
return BlockedByResponseReason::kCorpNotSameSite;
return mojom::BlockedByResponseReason::kCorpNotSameSite;
}

base::Optional<BlockedByResponseReason> IsBlockedInternalWithReporting(
base::Optional<mojom::BlockedByResponseReason> IsBlockedInternalWithReporting(
CrossOriginResourcePolicy::ParsedHeader policy,
const GURL& request_url,
const GURL& original_url,
Expand All @@ -208,7 +210,7 @@ base::Optional<BlockedByResponseReason> IsBlockedInternalWithReporting(
base::Optional<url::Origin> request_initiator_site_lock,
const CrossOriginEmbedderPolicy& embedder_policy,
mojom::CrossOriginEmbedderPolicyReporter* reporter) {
constexpr auto kBlockedDueToCoep = BlockedByResponseReason::
constexpr auto kBlockedDueToCoep = mojom::BlockedByResponseReason::
kCorpNotSameOriginAfterDefaultedToSameOriginByCoep;
if (embedder_policy.report_only_value ==
mojom::CrossOriginEmbedderPolicyValue::kRequireCorp &&
Expand Down Expand Up @@ -250,7 +252,8 @@ const char CrossOriginResourcePolicy::kHeaderName[] =
"Cross-Origin-Resource-Policy";

// static
base::Optional<BlockedByResponseReason> CrossOriginResourcePolicy::IsBlocked(
base::Optional<mojom::BlockedByResponseReason>
CrossOriginResourcePolicy::IsBlocked(
const GURL& request_url,
const GURL& original_url,
const base::Optional<url::Origin>& request_initiator,
Expand Down Expand Up @@ -280,7 +283,7 @@ base::Optional<BlockedByResponseReason> CrossOriginResourcePolicy::IsBlocked(
}

// static
base::Optional<BlockedByResponseReason>
base::Optional<mojom::BlockedByResponseReason>
CrossOriginResourcePolicy::IsBlockedByHeaderValue(
const GURL& request_url,
const GURL& original_url,
Expand All @@ -303,7 +306,7 @@ CrossOriginResourcePolicy::IsBlockedByHeaderValue(
}

// static
base::Optional<BlockedByResponseReason>
base::Optional<mojom::BlockedByResponseReason>
CrossOriginResourcePolicy::IsNavigationBlocked(
const GURL& request_url,
const GURL& original_url,
Expand Down
7 changes: 4 additions & 3 deletions services/network/public/cpp/cross_origin_resource_policy.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "base/component_export.h"
#include "base/gtest_prod_util.h"
#include "base/optional.h"
#include "services/network/public/mojom/blocked_by_response_reason.mojom.h"
#include "services/network/public/mojom/cross_origin_embedder_policy.mojom-forward.h"
#include "services/network/public/mojom/fetch_api.mojom-shared.h"
#include "services/network/public/mojom/network_context.mojom.h"
Expand Down Expand Up @@ -38,7 +39,7 @@ class COMPONENT_EXPORT(NETWORK_CPP) CrossOriginResourcePolicy {
// For kNoCors fetches, the IsBlocked method checks whether the response has
// a Cross-Origin-Resource-Policy header which says the response should not be
// delivered to a cross-origin or cross-site context.
static base::Optional<BlockedByResponseReason> IsBlocked(
static base::Optional<mojom::BlockedByResponseReason> IsBlocked(
const GURL& request_url,
const GURL& original_url,
const base::Optional<url::Origin>& request_initiator,
Expand All @@ -51,7 +52,7 @@ class COMPONENT_EXPORT(NETWORK_CPP) CrossOriginResourcePolicy {

// Same as IsBlocked(), but this method can take a raw value of
// Cross-Origin-Resource-Policy header instead of using a URLResponseHead.
static base::Optional<BlockedByResponseReason> IsBlockedByHeaderValue(
static base::Optional<mojom::BlockedByResponseReason> IsBlockedByHeaderValue(
const GURL& request_url,
const GURL& original_url,
const base::Optional<url::Origin>& request_initiator,
Expand All @@ -64,7 +65,7 @@ class COMPONENT_EXPORT(NETWORK_CPP) CrossOriginResourcePolicy {

// The CORP check for navigation requests. This is expected to be called
// from the navigation algorithm.
static base::Optional<BlockedByResponseReason> IsNavigationBlocked(
static base::Optional<mojom::BlockedByResponseReason> IsNavigationBlocked(
const GURL& request_url,
const GURL& original_url,
const base::Optional<url::Origin>& request_initiator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,20 +192,21 @@ TEST(CrossOriginResourcePolicyTest, WithCOEP) {
const RequestMode request_mode;
const url::Origin origin;
mojom::URLResponseHeadPtr response_info;
const base::Optional<BlockedByResponseReason> expectation_with_coep_none;
const base::Optional<BlockedByResponseReason>
const base::Optional<mojom::BlockedByResponseReason>
expectation_with_coep_none;
const base::Optional<mojom::BlockedByResponseReason>
expectation_with_coep_require_corp;
} test_cases[] = {
// We don't have a cross-origin-resource-policy header on a response. That
// leads to blocking when COEP: kRequireCorp is used.
{RequestMode::kNoCors, another_origin, corp_none.Clone(), kAllow,
BlockedByResponseReason::
mojom::BlockedByResponseReason::
kCorpNotSameOriginAfterDefaultedToSameOriginByCoep},
// We have "cross-origin-resource-policy: same-origin", so regardless of
// COEP the response is blocked.
{RequestMode::kNoCors, another_origin, corp_same_origin.Clone(),
BlockedByResponseReason::kCorpNotSameOrigin,
BlockedByResponseReason::kCorpNotSameOrigin},
mojom::BlockedByResponseReason::kCorpNotSameOrigin,
mojom::BlockedByResponseReason::kCorpNotSameOrigin},
// We have "cross-origin-resource-policy: cross-origin", so regardless of
// COEP the response is allowed.
{RequestMode::kNoCors, another_origin, corp_cross_origin.Clone(), kAllow,
Expand Down Expand Up @@ -324,19 +325,20 @@ TEST(CrossOriginResourcePolicyTest, NavigationWithCOEP) {
struct TestCase {
const url::Origin origin;
mojom::URLResponseHeadPtr response_info;
const base::Optional<BlockedByResponseReason> expectation_with_coep_none;
const base::Optional<BlockedByResponseReason>
const base::Optional<mojom::BlockedByResponseReason>
expectation_with_coep_none;
const base::Optional<mojom::BlockedByResponseReason>
expectation_with_coep_require_corp;
} test_cases[] = {
// We don't have a cross-origin-resource-policy header on a response. That
// leads to blocking when COEP: kRequireCorp is used.
{another_origin, corp_none.Clone(), kAllow,
BlockedByResponseReason::
mojom::BlockedByResponseReason::
kCorpNotSameOriginAfterDefaultedToSameOriginByCoep},
// We have "cross-origin-resource-policy: same-origin",
// COEP the response is blocked.
{another_origin, corp_same_origin.Clone(), kAllow,
BlockedByResponseReason::kCorpNotSameOrigin},
mojom::BlockedByResponseReason::kCorpNotSameOrigin},
// We have "cross-origin-resource-policy: cross-origin", so regardless of
// COEP the response is allowed.
{another_origin, corp_cross_origin.Clone(), kAllow, kAllow},
Expand Down
5 changes: 3 additions & 2 deletions services/network/public/cpp/network_ipc_param_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "services/network/public/cpp/origin_policy.h"
#include "services/network/public/cpp/resource_request_body.h"
#include "services/network/public/cpp/url_loader_completion_status.h"
#include "services/network/public/mojom/blocked_by_response_reason.mojom-shared.h"
#include "services/network/public/mojom/cors.mojom-shared.h"
#include "services/network/public/mojom/fetch_api.mojom-shared.h"
#include "services/network/public/mojom/trust_tokens.mojom-shared.h"
Expand Down Expand Up @@ -92,8 +93,8 @@ IPC_ENUM_TRAITS_MAX_VALUE(network::mojom::RequestMode,
IPC_ENUM_TRAITS_MAX_VALUE(network::mojom::CorsPreflightPolicy,
network::mojom::CorsPreflightPolicy::kMaxValue)

IPC_ENUM_TRAITS_MAX_VALUE(network::BlockedByResponseReason,
network::BlockedByResponseReason::kMaxValue)
IPC_ENUM_TRAITS_MAX_VALUE(network::mojom::BlockedByResponseReason,
network::mojom::BlockedByResponseReason::kMaxValue)

IPC_STRUCT_TRAITS_BEGIN(network::CorsErrorStatus)
IPC_STRUCT_TRAITS_MEMBER(cors_error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ URLLoaderCompletionStatus::URLLoaderCompletionStatus(
}

URLLoaderCompletionStatus::URLLoaderCompletionStatus(
const BlockedByResponseReason& reason)
const mojom::BlockedByResponseReason& reason)
: URLLoaderCompletionStatus(net::ERR_BLOCKED_BY_RESPONSE) {
blocked_by_response_reason = reason;
}
Expand Down
7 changes: 4 additions & 3 deletions services/network/public/cpp/url_loader_completion_status.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
#include "net/base/proxy_server.h"
#include "net/dns/public/resolve_error_info.h"
#include "net/ssl/ssl_info.h"
#include "services/network/public/cpp/blocked_by_response_reason.h"
#include "services/network/public/cpp/cors/cors_error_status.h"
#include "services/network/public/mojom/blocked_by_response_reason.mojom-shared.h"
#include "services/network/public/mojom/cors.mojom-shared.h"
#include "services/network/public/mojom/trust_tokens.mojom-shared.h"

Expand All @@ -39,7 +39,8 @@ struct COMPONENT_EXPORT(NETWORK_CPP_BASE) URLLoaderCompletionStatus {
// Sets ERR_BLOCKED_BY_RESPONSE to |error_code|, |reason| to
// |blocked_by_response_reason|, and base::TimeTicks::Now() to
// |completion_time|.
explicit URLLoaderCompletionStatus(const BlockedByResponseReason& reason);
explicit URLLoaderCompletionStatus(
const mojom::BlockedByResponseReason& reason);

~URLLoaderCompletionStatus();

Expand Down Expand Up @@ -87,7 +88,7 @@ struct COMPONENT_EXPORT(NETWORK_CPP_BASE) URLLoaderCompletionStatus {

// More detailed reason for failing the response with
// ERR_net::ERR_BLOCKED_BY_RESPONSE |error_code|.
base::Optional<BlockedByResponseReason> blocked_by_response_reason;
base::Optional<mojom::BlockedByResponseReason> blocked_by_response_reason;

// Set when response blocked by CORB needs to be reported to the DevTools
// console.
Expand Down
1 change: 1 addition & 0 deletions services/network/public/mojom/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ mojom("websocket_mojom") {
mojom("mojom") {
generate_java = true
sources = [
"blocked_by_response_reason.mojom",
"content_security_policy.mojom",
"cookie_manager.mojom",
"cors.mojom",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,15 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef SERVICES_NETWORK_PUBLIC_CPP_BLOCKED_BY_RESPONSE_REASON_H_
#define SERVICES_NETWORK_PUBLIC_CPP_BLOCKED_BY_RESPONSE_REASON_H_

namespace network {
module network.mojom;

// This enum is used by to communicate the reason a request was blocked from
// the network service to the browser. The blocking reasons pertain to
// security features such as CORP, COEP, and COOP.
enum class BlockedByResponseReason : int {
enum BlockedByResponseReason {
kCoepFrameResourceNeedsCoepHeader,
kCoopSandboxedIFrameCannotNavigateToCoopPage,
kCorpNotSameOrigin,
kCorpNotSameOriginAfterDefaultedToSameOriginByCoep,
kCorpNotSameSite,
// `kMaxValue` needs be assigned to the max value in the enum.
kMaxValue = kCorpNotSameSite,
};

} // namespace network

#endif // SERVICES_NETWORK_PUBLIC_CPP_BLOCKED_BY_RESPONSE_REASON_H_
6 changes: 3 additions & 3 deletions services/network/url_loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,7 @@ void URLLoader::OnReceivedRedirect(net::URLRequest* url_request,
? factory_params_->client_security_state->cross_origin_embedder_policy
: kEmpty;

if (base::Optional<BlockedByResponseReason> blocked_reason =
if (base::Optional<mojom::BlockedByResponseReason> blocked_reason =
CrossOriginResourcePolicy::IsBlocked(
url_request_->url(), url_request_->original_url(),
url_request_->initiator(), *response, request_mode_,
Expand Down Expand Up @@ -1148,7 +1148,7 @@ void URLLoader::OnResponseStarted(net::URLRequest* url_request, int net_error) {
factory_params_->client_security_state
? factory_params_->client_security_state->cross_origin_embedder_policy
: kEmpty;
if (base::Optional<BlockedByResponseReason> blocked_reason =
if (base::Optional<mojom::BlockedByResponseReason> blocked_reason =
CrossOriginResourcePolicy::IsBlocked(
url_request_->url(), url_request_->original_url(),
url_request_->initiator(), *response_, request_mode_,
Expand Down Expand Up @@ -1814,7 +1814,7 @@ void URLLoader::OnHeadersReceivedComplete(
void URLLoader::CompleteBlockedResponse(
int error_code,
bool should_report_corb_blocking,
base::Optional<BlockedByResponseReason> reason) {
base::Optional<mojom::BlockedByResponseReason> reason) {
if (has_received_response_) {
// The response headers and body shouldn't yet be sent to the
// URLLoaderClient.
Expand Down
2 changes: 1 addition & 1 deletion services/network/url_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ class COMPONENT_EXPORT(NETWORK_SERVICE) URLLoader
void CompleteBlockedResponse(
int error_code,
bool should_report_corb_blocking,
base::Optional<BlockedByResponseReason> reason = base::nullopt);
base::Optional<mojom::BlockedByResponseReason> reason = base::nullopt);

enum BlockResponseForCorbResult {
// Returned when caller of BlockResponseForCorb doesn't need to continue,
Expand Down
Loading

0 comments on commit b1e6dfe

Please sign in to comment.