Commit 3461f451 authored by Charlie Hu's avatar Charlie Hu Committed by Commit Bot

Reland "Replace param |disposition| with |should_report| on IsFeatureEnabled"

This reverts commit df399aaa.

Reason for revert: The dependency CL got reverted. Reland after the dependency CL is relanded.

Original change's description:
> Revert "Replace param |disposition| with |should_report| on IsFeatureEnabled"
>
> This reverts commit ccf8b883.
>
> Reason for revert: To revert https://chromium-review.googlesource.com/c/chromium/src/+/2028666
>
> https://chromium-review.googlesource.com/c/chromium/src/+/2028666 seems to cause LinuxCFI tests failures.
>
> LinuxCFI tests start failing at
> https://ci.chromium.org/p/chromium/builders/ci/Linux%20CFI/16433
> 16433 contains https://chromium-review.googlesource.com/c/chromium/src/+/2028666
>
> Original change's description:
> > Replace param |disposition| with |should_report| on IsFeatureEnabled
> >
> > Previously |disposition| on IsFeatureEnabled have an awkward type
> > base::Optional<mojom::FeaturePolicyDisposition>*, which has 3 states
> > correspond to FeatureEnabledState:
> > - base::nullopt <= kEnabled
> > - FeaturePolicyDisposition::kReport <= kReportOnly
> > - FeaturePolicyDisposition::kEnforce <= kDisabled
> >
> > This CL simplifies the representation of this piece of information to
> > |should_report|, and removes FeatureEnabledState enum.
> >
> > The mapping of FeatureEnabledState to new logic:
> > kEnabled: enabled=true; should_report=false
> > kReportOnly: enabled = true; should_report=true
> > kDisabled: enabled = false; should_report=true
> >
> > Change-Id: I1fa82fb5e9924b8eed448853b139772e024bd531
> > Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2037326
> >
> Change-Id: Id6531935bbc777662fc3af2af65612495cb5a803
> No-Presubmit: true
> No-Tree-Checks: true
> No-Try: true
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2059555

# Not skipping CQ checks because original CL landed > 1 day ago.

Change-Id: I016c9280e74db35d35d04dcbcf4dfe93ee022836
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2061397
Commit-Queue: Charlie Hu <chenleihu@google.com>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarIan Clelland <iclelland@chromium.org>
Cr-Commit-Position: refs/heads/master@{#743137}
parent 7fd508ee
......@@ -8328,7 +8328,7 @@ void Document::CountPotentialFeaturePolicyViolation(
}
void Document::ReportFeaturePolicyViolation(
mojom::blink::FeaturePolicyFeature feature,
mojom::FeaturePolicyDisposition disposition,
mojom::blink::FeaturePolicyDisposition disposition,
const String& message,
const String& source_file) const {
if (!RuntimeEnabledFeatures::FeaturePolicyReportingEnabled(this))
......@@ -8340,8 +8340,9 @@ void Document::ReportFeaturePolicyViolation(
// Construct the feature policy violation report.
const String& feature_name = GetNameForFeature(feature);
const String& disp_str =
(disposition == mojom::FeaturePolicyDisposition::kReport ? "report"
: "enforce");
(disposition == mojom::blink::FeaturePolicyDisposition::kReport
? "report"
: "enforce");
FeaturePolicyViolationReportBody* body =
source_file.IsEmpty()
......
......@@ -430,9 +430,9 @@ bool ExecutionContext::IsFeatureEnabled(
CountPotentialFeaturePolicyViolation(feature);
}
base::Optional<mojom::FeaturePolicyDisposition> disposition;
bool should_report;
bool enabled = GetSecurityContext().IsFeatureEnabled(feature, threshold_value,
&disposition);
&should_report);
if (enabled) {
// Report if the proposed header semantics change would have affected the
......@@ -449,8 +449,13 @@ bool ExecutionContext::IsFeatureEnabled(
}
}
if (disposition && report_on_failure == ReportOptions::kReportOnFailure)
ReportFeaturePolicyViolation(feature, *disposition, message, source_file);
if (should_report && report_on_failure == ReportOptions::kReportOnFailure) {
mojom::blink::FeaturePolicyDisposition disposition =
enabled ? mojom::blink::FeaturePolicyDisposition::kReport
: mojom::blink::FeaturePolicyDisposition::kEnforce;
ReportFeaturePolicyViolation(feature, disposition, message, source_file);
}
return enabled;
}
......
......@@ -184,16 +184,21 @@ bool SecurityContext::IsFeatureEnabled(
bool SecurityContext::IsFeatureEnabled(
mojom::blink::FeaturePolicyFeature feature,
PolicyValue threshold_value,
base::Optional<mojom::FeaturePolicyDisposition>* disposition) const {
FeatureEnabledState state = GetFeatureEnabledState(feature, threshold_value);
if (state == FeatureEnabledState::kEnabled)
return true;
if (disposition) {
*disposition = (state == FeatureEnabledState::kReportOnly)
? mojom::FeaturePolicyDisposition::kReport
: mojom::FeaturePolicyDisposition::kEnforce;
bool* should_report) const {
DCHECK(feature_policy_);
LogImagePolicies(feature, threshold_value);
bool feature_policy_result =
feature_policy_->IsFeatureEnabled(feature, threshold_value);
bool report_only_feature_policy_result =
!report_only_feature_policy_ ||
report_only_feature_policy_->IsFeatureEnabled(feature, threshold_value);
if (should_report) {
*should_report =
!feature_policy_result || !report_only_feature_policy_result;
}
return (state != FeatureEnabledState::kDisabled);
return feature_policy_result;
}
bool SecurityContext::IsFeatureEnabled(
......@@ -211,13 +216,9 @@ bool SecurityContext::IsFeatureEnabled(
return document_policy_->IsFeatureEnabled(feature, threshold_value);
}
FeatureEnabledState SecurityContext::GetFeatureEnabledState(
void SecurityContext::LogImagePolicies(
mojom::blink::FeaturePolicyFeature feature,
PolicyValue threshold_value) const {
// The policy should always be initialized before checking it to ensure we
// properly inherit the parent policy.
DCHECK(feature_policy_);
// Log metrics for unoptimized-*-images and oversized-images policies.
if ((feature >= mojom::blink::FeaturePolicyFeature::kUnoptimizedLossyImages &&
feature <= mojom::blink::FeaturePolicyFeature::
......@@ -241,15 +242,6 @@ FeatureEnabledState SecurityContext::GetFeatureEnabledState(
GetImagePolicyHistogramName(feature), 0, 100, 101, 0x1));
}
}
if (feature_policy_->IsFeatureEnabled(feature, threshold_value)) {
if (report_only_feature_policy_ &&
!report_only_feature_policy_->IsFeatureEnabled(feature,
threshold_value)) {
return FeatureEnabledState::kReportOnly;
}
return FeatureEnabledState::kEnabled;
}
return FeatureEnabledState::kDisabled;
}
} // namespace blink
......@@ -59,7 +59,6 @@ using ParsedFeaturePolicy = std::vector<ParsedFeaturePolicyDeclaration>;
// Whether to report policy violations when checking whether a feature is
// enabled.
enum class ReportOptions { kReportOnFailure, kDoNotReport };
enum class FeatureEnabledState { kDisabled, kReportOnly, kEnabled };
// Defines the security properties (such as the security origin, content
// security policy, and other restrictions) of an environment in which
......@@ -159,14 +158,12 @@ class CORE_EXPORT SecurityContext {
// Tests whether the policy-controlled feature is enabled in this frame.
// Use ExecutionContext::IsFeatureEnabled if a failure should be reported.
// If a non-null base::Optional<mojom::FeaturePolicyDisposition>* is provided
// and the feature is disabled via feature policy, it will be populated to
// indicate whether the feature usage should be blocked or merely reported.
// |should_report| is an extra return value that indicates whether
// the potential violation should be reported.
bool IsFeatureEnabled(mojom::blink::FeaturePolicyFeature) const;
bool IsFeatureEnabled(
mojom::blink::FeaturePolicyFeature,
PolicyValue threshold_value,
base::Optional<mojom::FeaturePolicyDisposition>* = nullptr) const;
bool IsFeatureEnabled(mojom::blink::FeaturePolicyFeature,
PolicyValue threshold_value,
bool* should_report = nullptr) const;
bool IsFeatureEnabled(mojom::blink::DocumentPolicyFeature) const;
bool IsFeatureEnabled(mojom::blink::DocumentPolicyFeature,
......@@ -180,9 +177,8 @@ class CORE_EXPORT SecurityContext {
std::unique_ptr<DocumentPolicy> document_policy_;
private:
FeatureEnabledState GetFeatureEnabledState(mojom::blink::FeaturePolicyFeature,
PolicyValue threshold_value) const;
void LogImagePolicies(mojom::blink::FeaturePolicyFeature,
PolicyValue threshold_value) const;
Member<ContentSecurityPolicy> content_security_policy_;
network::mojom::IPAddressSpace address_space_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment